New to Haskell: constructing Data Types

As an exercise, I'm attempting to create an abstract syntax tree for a very small language: data Expression = Variable String | Integer | Expression BinOp Expression deriving Show data Statement = Assignment Variable Expression --error! | If Expression Statement Statement | While Expression Statement | Compound Statement deriving Show data BinOp = Add | Mult | LessThan | GreaterThan deriving Show However, there is a problem with having "Variable" as an argument to the "Assignment" constructor of the "Statement" data type: Prelude> :l test.hs [1 of 1] Compiling Test ( test.hs, interpreted ) test.hs:8:28: Not in scope: type constructor or class `Variable' Failed, modules loaded: none. An 'assignment' is a variable followed by an expression in this example, so what can I do to fix this? Thanks, Tyler

Constructor definitions take type arguments but you have given it
another constructor. To fix this, you might want to create a Variable
datatype and reference it in the Expresion datatype and the Statement
datatype but it's hard (for me) to know without further information.
On Sat, Feb 5, 2011 at 12:33 PM, Tyler Hayes
As an exercise, I'm attempting to create an abstract syntax tree for a very small language:
data Expression = Variable String | Integer | Expression BinOp Expression deriving Show
data Statement = Assignment Variable Expression --error! | If Expression Statement Statement | While Expression Statement | Compound Statement deriving Show
data BinOp = Add | Mult | LessThan | GreaterThan deriving Show
However, there is a problem with having "Variable" as an argument to the "Assignment" constructor of the "Statement" data type:
Prelude> :l test.hs [1 of 1] Compiling Test ( test.hs, interpreted )
test.hs:8:28: Not in scope: type constructor or class `Variable' Failed, modules loaded: none.
An 'assignment' is a variable followed by an expression in this example, so what can I do to fix this?
Thanks, Tyler
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Fri, Feb 4, 2011 at 10:33 PM, Tyler Hayes
As an exercise, I'm attempting to create an abstract syntax tree for a very small language:
data Expression = Variable String | Integer | Expression BinOp Expression deriving Show
data Statement = Assignment Variable Expression --error! | If Expression Statement Statement | While Expression Statement | Compound Statement deriving Show
data BinOp = Add | Mult | LessThan | GreaterThan deriving Show
However, there is a problem with having "Variable" as an argument to the "Assignment" constructor of the "Statement" data type:
In your example, you've declared three types: Expression, Statement and BinOp. So when you ask for a type named 'Variable' it can't find one. Perhaps you meant 'String' ? In your above example, you do have a data constrictor 'Variable', which can be used as a value to construct values of type 'Expression' (or in a pattern match over values of type Expression). The difference between types and data constructors can be tricky at first, especially since they appear with each other in code and have similar naming conventions. If I'm speaking a foreign language I can probably dig up a tutorial to point you to :-) Antoine
participants (3)
-
Antoine Latter
-
Lyndon Maydwell
-
Tyler Hayes