representation getting verbose...
17 Oct 2002 11:08:57 -0400
Greetings Haskellers,
I'm running into a problem representing some fairly complicated types,
and I'll try to put together a simpler example to get your
suggestions.
In Paul Hudak's SOE, I find a definition of expression:
data Expr = C Float | V String | Expr :+ Expr | Expr :- Expr
| Expr :* Expr | Expr :/ Expr
Now this is compelling, but sometimes, I might want to have a function
that takes a variable only, not just any kind of expression. I could
write something like:
typeOfVariable :: Expression -> Type
typeOfVariable (V s) = ...
_ = error...
But thats not very satisfying from a type-checking perspective. So it
makes sense to create a constructor:
data Variable = VVariable String
data Expr = C Float | V Variable | Expr :+ Expr
| Expr :- Expr | Expr :* Expr | Expr :/ Expr
and make typeOfVariable :: Variable -> Type. But then when I want to
match or create a variable expression, things are starting to get
verbose:
case expr of
C f -> ...
V (Variable (VVariable s)) -> ...
...
And if I want a still more accurate hierarchy, the construction and
destruction of Variables can really become cumbersome. For an
interpreter I'm writing, I found myself writing a function
"constructVarExpr :: String -> Expr" just to make it easier. This all
seems very inelegant, and I get the feeling that there's a better way
to do this.
Any suggestions on how I could better represent Expressions or
Variables to keep the type-checking but decrease the verbosity?
peace,
Isaac Jones