[Haskell-cafe] Type extensions
Thomas Davie
tom.davie at gmail.com
Wed Jun 1 10:51:25 EDT 2005
Hi,
I was wondering if I hat missed something and it was possible to
do this within the Haskell type system or not...
Essentially I would like some sort of inderritance property for
Haskell types, I often find myself wanting to for example extend a
tree with black/white colouring, or later extend the tree with some
sort of ID, etc.
So I would eno up with three data types
data MyTree = Branch MyTree MyTree | Leaf
type BwTag = Bool
data MyBwTree = Branch BwTag MyBwTree MyBwTree | Node BwTag
data MyBwTaggedTree = Branch BwTag Int MyBwTaggedTree MyBwTaggedTree
| Node BwTag Int
and several functions to move from one to another. (Or define the
most complex and not always use all the attrdbutes). What I would
prefer is to be able to spocify something like:
data MyTree = Branch MyTree MyTree | Leaf
type BwTag = Bool
data MyBwTree extends MyTree with BwTag=True
data MyBwTaggedTree extends MyBwTree with Int=0
Specifying that myBwTree is the same as MyTree but with an extra
argument on each node, and to generate functions to translate between
them that fill in True as a default value for the extra tag.
Is this possible?
Thanks
Tom Davie
More information about the Haskell-Cafe
mailing list