0

I am having trouble with the Mod operator in Haskell Its inferring the wrong type based on what I have. Mod is at the bottom of eval. I am new to Haskell and am making an AST with expr for class. Here is the code I have so far. I am sure its a simple fix but cant seem to get it.

data Expr = Add Expr Expr
      | Sub Expr Expr
      | Mult Expr Expr
      | EqualTo Expr Expr
      | GreaterThan Expr Expr
      | LessThan Expr Expr
      | Mod Expr
      | NotEqual Expr Expr

      | Const Integer
      | VarName String
      | TrueE
      | FalseE
  deriving (Show,Eq) 


eval :: Expr -> [(String, Integer)] -> Integer
eval (Add e1 e2) env = eval e1 env + eval e2 env
eval (Sub e1 e2) env = eval e1 env - eval e2 env
eval (Mult e1 e2) env = eval e1 env * eval e2 env
eval (EqualTo e1 e2) env | eval e1 env == eval e2 env = 1
                     | otherwise = 0 
eval (GreaterThan e1 e2) env | eval e1 env > eval e2 env = 1
             | otherwise = 0    
eval (LessThan e1 e2) env | eval e1 env < eval e2 env = 1
          | otherwise = 0
eval (NotEqual e1 e2) env | eval e1 env /= eval e2 env = 1
                      | otherwise = 0
eval (Mod e1) env =  mod eval e1 env  

2 Answers 2

2

I assume that you are building an evaluator for AST, which represents a mathematical expression.

Haskell function mod receives two arguments, but your AST receives only one argument, and function mod in your evaluator receives three arguments. Try to change the function eval and data Expr like this

data Expr =
   | Mod Expr Expr

eval :: Expr -> [(String, Integer)] -> Integer
eval (Mod e1 e2) env = mod (eval e1 env) (eval e2 env)

In the above code, function mod receives two arguments, eval e1 env and eval e2 env respectively.

Sign up to request clarification or add additional context in comments.

Comments

0

I was using the wrong notation for mod it should be

eval e1 env `mod` eval e2 env 

Stupid mistake.. mod should be surrounded by back ticks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.