Haskell syntax: input parsing error

As part of the mini-haskell compiler that I am writing, I have a function called app . I want this function to execute these arguments epp (App e1 e2) . The first step would be to recursively evaluate e1 ( epp e1 ) and check if the result is an error. If not, then evaluate e2 , and then call another eppVals function to evaluate the call outputs on e1 and e2 , which I defined as v1 and v2 respectively.

0
source share
1 answer

In your hpaste, you have the appVals function, which has been renamed eppVals in the above question. It's useless.

Let's look at some types:

  • epp :: Exp -> Error Val
  • appVals :: Val -> Val -> Error Val

Error message Failed to match the expected Val type with the actual Error Val type, which is trying to tell you that the first parameter appVals must be of type Val (see type label for appVals ), but with the actual type of value that you provided as the first parameter, is v1 , defined as epp e1 , which is of type Error Val (see type signature for epp ).

Val and Error Val not of the same type. It's your problem.

How to fix it? You need to somehow handle the error cases separately from the rest of the calculations. If you have implemented Applicative or Monad classes for your Error type, this is almost certainly what they do.

Edit: You do not include the definition of your Error , but I expect it to look like this:

 data Error a = S a | Error String 

Run a few classes for it (you need import Control.Applicative ):

 instance Functor Error where fmap f (S x) = S (fx) fmap _ (Error s) = Error s instance Applicative Error where pure = S Error s <*> _ = Error s _ <*> Error s = Error s S f <*> S x = S (fx) 

Now you can rewrite

 epp (App e1 e2) = eppVals <$> epp e1 <*> epp e2 
+1
source

Source: https://habr.com/ru/post/1446825/


All Articles