0

I am getting this error, and I don't know what I am doing wrong. I am new to Haskell, so please elaborate everything for me.

import Data.Maybe

data Op = Add | Sub | Mul | Div | And | Or | Not | Eq | Less | Great
    deriving (Eq, Show)

data Exp = Literal Value
     | Primitive Op [Exp]
     | Variable String
     | If Exp Exp Exp
     | Let [(String, Exp)] Exp
    deriving (Show, Eq)

data Value = Number Int
       | Bool Bool
       | String String
    deriving (Eq, Show)

type Env = [(String, Value)]

eval :: Env -> Exp -> Value
eval e (Literal v) = v
eval e (Variable x) = fromJust (lookup x e)   --22

prim :: Op -> [Value] -> Value
prim Add [Number a, Number b] = Number (a + b)
prim And [Bool a, Bool b] = Bool (a && b)
prim Sub [Number a, Number b] = Number (a - b)
prim Mul [Number a, Number b] = Number (a * b)
prim Div [Number a, Number b] = Number (a `div` b)
prim Or [Bool a, Bool b] = Bool (a || b)
prim Not [Bool a] = Bool (not a)
prim Eq [Number a, Number b] = Bool (a == b)
prim Eq [String a, String b] = Bool (a == b) 
prim Less [Number a, Number b] = Bool (a < b)
prim Less [String a, String b] = Bool (a < b)
prim Great [Number a, Number b] = Bool (a > b)
prim Great [String a, String b] = Bool (a > b) --37

main = do
    n = "n"    -- parse error on input `='
    nv = Variable "n"   -- parse error on input `='
    lit n = Literal (Number n)  

    t0 = Primitive Mul [lit 5, lit 2]    
    t1 = Let [(n, t0)] (If (Primitive Great [nv, lit 9]) (lit 1) (lit 0))  -- parse error on input `='

I wrote in comments where I am getting the errors. I checked the validness of the parameters, and everything looks fine. It's maybe an error with my syntax... I guess.

Thank you in advance.

1
  • 1
    Please don't edit the question to remove the parts that the answers depend on to make sense. Commented Feb 4, 2013 at 7:53

1 Answer 1

4

Your main function is broken in almost every way. Have you learned monads or for this case only about the IO monad?

main :: IO Exp
main = do   
  let n = "n"
      nv = Variable "n"
      lit n = Literal (Number n)
      t0 = Primitive Mul [lit 5, lit 2]
      in return $ Let [(n, t0)] (If (Primitive Great [nv, lit 9]) (lit 1) (lit 0))

You can't just use = in the context of that do-Block. This version works as intended. Learn about the let expression and the return. If you don't know this, read for example here.

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

6 Comments

Thank you for the help. It now complies, but it doesn't compute. It's supposed to give me a value.
In my main, I am trying this "eval [("y", (Number 40))] (Let [("x", (Literal (Number 2)))] (Primitive Add [(Variable "x"), (Variable "y")]))" which is supposed to 42. But it gives me this error "Couldn't match expected type IO t0' with actual type Value'" Do you know what I might be missing?
You didn't tell it to do. You initial code does not contain any reference to your eval function.
You have to feed the result of eval to return. Like return $ eval .... Your code above will btw still not work because your eval function is incomplete.
There is no for the in in the last line.
|

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.