representation getting verbose...

Claus Reinke claus.reinke@talk21.com
Tue, 22 Oct 2002 23:44:25 +0100


> Variable (VVariable(varName, (Value (Number
>                                      (NNumber (varValue, varDimension))))))
> 
> Here VVariable and NNumber are newtype constructors of tuples, and the
> entire expression is an "Expression" which, among other things has:
> 
> data Expression = 
>   Value Value
> | Variable Variable
> | ...
> 
> and Value has "data Value = Number Number | ..."
> 
> Now the newtype constructors seem a bit unnecessary, perhaps, but I
> guess they increase the type-checking.  So I still feel that the above
> construtor is overly verbose.

Not every embedding has to add constructors. If you look at the types:

VVariable :: (String,Value) -> Variable
Variable :: Variable -> Expression,

you see that you are just using the constructor as a simple way to embed
your subtype of variables in the type of expressions. To leave out the 
intermediate constructor, redefine 

data Expression = .. | Variable (String,Value) | ..

and write your own embedding function, with the same type as the old
constructor, that does not preserve the intermediate constructor:

variable :: Variable -> Expression
variable (VVariable (s,v)) = Variable (s,v)
 
However, my real reason for posting is to recommend a paper you might
enjoy, which deals with extensible union types in the context of interpreters,
using type classes to automate the embeddings:

Monad Transformers and Modular Interpreters, 
Sheng Liang, Paul Hudak, and Mark P. Jones, 
In Conference Record of POPL'95: 22nd ACM SIGPLAN-SIGACT 
Symposium on Principles of Programming Languages, 
San Francisco, CA, January 1995.
http://www.cse.ogi.edu/~mpj/pubs/modinterp.html

Hth,
Claus

--
Haskell Communities and Activities Report (November 2002 edition)
All contributions are due in by the end of October!
http://www.haskell.org/communities/