Hi I have a data type Expr for handling Expressions data Expr = Lit1 Int | Lit2 Bool | Var String | BinOp Op Expr Expr As you can see, I am trying to have an Expression have both Int and Bool values so that I can work with both arithmetic (+,-,*,/) and logical(and,or,<,<=,>,>=) operators. What I am aiming at is to parse a line which contains an expression, evaluate it and return the result. I am facing some problems in this regard. When I write the evaluate function, the Haskell compiler refuses to compile my code as it says, instance of Num Expr required. Any ideas what I am supposed to do to fix that? My evaluate code is as follows... evaluate :: Expr -> Store -> Expr evaluate ( Lit1 n ) st = n evaluate ( Lit2 n ) st = n evaluate ( Var v ) st = value st v -- returns the value of v evaluate ( BinOp op e1 e2 ) st = opValue op v1 v2 where v1 = evaluate e1 st v2 = evaluate e2 st opValue :: Op -> Expr -> Expr -> Expr opValue Add e1 e2 = e1 + e2 opValue Sub e1 e2 = e1 - e2 opValue Mul e1 e2 = e1 * e2 opValue Div e1 e2 = e1 / e2 opValue LT e1 e2 = e1 < e2 opValue LE e1 e2 = e1 <= e2 opValue GT e1 e2 = e1 > e2 opValue GE e1 e2 = e1 >= e2 opValue And e1 e2 = e1 && e2 opValue Or e1 e2 = e1 || e2 Now I can't return an Int as I return a Bool at times and hence I return an Expr. What does the error...instance of Num Expr or instance of Fractional Expr required mean? Thanks in advance! Pratik -------------------------------- Pratik Bhadra Undergraduate Section Leader The University of Texas at Austin College of Natural Sciences Junior BS Computer Sciences ---------------------------------
Pratik Bhadra wrote:
Hi
I have a data type Expr for handling Expressions
data Expr = Lit1 Int | Lit2 Bool | Var String | BinOp Op Expr Expr
As you can see, I am trying to have an Expression have both Int and Bool values so that I can work with both arithmetic (+,-,*,/) and logical(and,or,<,<=,>,>=) operators.
What I am aiming at is to parse a line which contains an expression, evaluate it and return the result. I am facing some problems in this regard. When I write the evaluate function, the Haskell compiler refuses to compile my code as it says, instance of Num Expr required. Any ideas what I am supposed to do to fix that?
My evaluate code is as follows...
evaluate :: Expr -> Store -> Expr
evaluate ( Lit1 n ) st = n evaluate ( Lit2 n ) st = n evaluate ( Var v ) st = value st v -- returns the value of v evaluate ( BinOp op e1 e2 ) st = opValue op v1 v2 where v1 = evaluate e1 st v2 = evaluate e2 st
opValue :: Op -> Expr -> Expr -> Expr opValue Add e1 e2 = e1 + e2
Look at the RHS of the above. [Hint: What is its type? And what are the types of `e1' and `e2'? And why would it be a valid expression?]
opValue Sub e1 e2 = e1 - e2 opValue Mul e1 e2 = e1 * e2 opValue Div e1 e2 = e1 / e2 opValue LT e1 e2 = e1 < e2
Similarly.
opValue LE e1 e2 = e1 <= e2 opValue GT e1 e2 = e1 > e2 opValue GE e1 e2 = e1 >= e2 opValue And e1 e2 = e1 && e2 opValue Or e1 e2 = e1 || e2
Now I can't return an Int as I return a Bool at times and hence I return an Expr. What does the error...instance of Num Expr or instance of Fractional Expr required mean?
See above. I think you'll figure it out. HTH, --ag PS - say `Hi' to Ham for me. ;-) -- Artie Gold -- Austin, Texas Oh, for the good old days of regular old SPAM.
At 9:20 PM -0500 10/22/03, Pratik Bhadra wrote:
Hi
I have a data type Expr for handling Expressions
data Expr = Lit1 Int | Lit2 Bool | Var String | BinOp Op Expr Expr
As you can see, I am trying to have an Expression have both Int and Bool values so that I can work with both arithmetic (+,-,*,/) and logical(and,or,<,<=,>,>=) operators.
That makes sense.
What I am aiming at is to parse a line which contains an expression, evaluate it and return the result. I am facing some problems in this regard. When I write the evaluate function, the Haskell compiler refuses to compile my code as it says, instance of Num Expr required. Any ideas what I am supposed to do to fix that?
I think that's the same question I answered in my previous email. Right? It's saying that Expr needs to be an instance of Num, because (+), or some other numeric operation, is being applied to an Expr. --HR -- ------------------------------------------------------------------ Hamilton Richards, PhD Department of Computer Sciences Senior Lecturer The University of Texas at Austin 512-471-9525 1 University Station C0500 Taylor Hall 5.138 Austin, Texas 78712-1188 ham@cs.utexas.edu hrichrds@swbell.net ------------------------------------------------------------------
On Thu, 23 Oct 2003 12:20 pm, Pratik Bhadra wrote:
data Expr = Lit1 Int | Lit2 Bool | Var String | BinOp Op Expr Expr
My evaluate code is as follows...
evaluate :: Expr -> Store -> Expr
evaluate ( Lit1 n ) st = n evaluate ( Lit2 n ) st = n
A function must match its type signature and the type signature must be consistent irrespective of the function arguments. The first line of the evaluation has the type, Expr -> Store -> Int The second line has the type, Expr -> Store -> Bool Tom
participants (4)
-
Artie Gold -
Hamilton Richards -
Pratik Bhadra -
Thomas L. Bevan