I want to write a data type to evaluate the expressions like this:
a
is evaluated to a
a + b * c / d -e
is evaluated to a + (b * (c / (d - e)))
So the first argument is always a number and the second one is either a number or an expression
I have tried to define a data type like this
data Exp = Single Int
| Mult (Single Int) Exp
But it gives me this compilation error:
Not in scope: type constructor or class ‘Single’
A data constructor of that name is in scope; did you mean DataKinds?
|
9 | | Mult (Single Integer ) Exp
|
I can define it like this:
data Exp = Single Integer
| Mult Exp Exp
But it does not give the precise definition of the Type I want. How can I define this?
But it does not give the precise definition of the Type I want.
What is the type you want then? Your first version fails becauseSingle a
is a value, not a type. The second one looks correct to me, if it differs from what you need then please explain what the difference is. (Well if you want it polymorphic it should bedata Exp a = Single a | Mult (Exp a) (Exp a)
) – AblautSingle a
is not a type. You can either define it as aMult a Exp
, orMult Expr Expr
. – IntercommunicateMult a Expr
is closest to what I want. I was thinking, How can I tell compiler that the first argument should be a specific definition, instead ofMult Exp Exp
– Argentumdata Exp = Single Int | Mult Int Exp
? – HetrickMult Int Exp
in the language, I'm trying to evaluate. It has 'Mult Single Int Exp' . The next best option isMult Exp Exp
, but it allowsMult Exp (Single Int)
which is illegal. – ArgentumMult (Single Int) Exp
to mean and whatMult Int Exp
actually means? – Hetrick