Union Types for Haskell!?
Mon, 27 Nov 2000 09:30:17 +0100
Your suggestion is of course possible. However, it imposes an overhead
which I would like to avoid. Consider you have a data structure (e.g. an
abstract syntax tree) consisting of nodes belonging to different data
types. The difficulty is to write a function that abstracts from the
traversal of that tree. I.e. try to write a map and a filter function on
such data structures (what's the type of the function argument to
map/filter?). In using a discriminated union type as you propose, one
solution would be to traverse the data structure once and generate for
each node a corresponding "tagged" node (a B becomes an UB B etc.). The
result can then be mapped over or filtered by a function of type A -> A
or A -> [A]. Finally, the result has again to be untagged to get a
structure of the original types. Creating the tagged variant from the
beginning is undesirable because this would lead to less type safety in
the construction and node-specific manipulation of the data structure.
These disadvantages could be avoided when (undiscriminated) union types
Christian Lescher wrote:
> Hi Bernd,
> > data B = ...
> > data C = ...
> > type A = B | C | D -- A accepts values of either B or C or D (cf. the
> > "Either a" type in the Prelude)
> What about the construction of a union type in Haskell like this?:
> data B = B1 | B2
> data C = C1 | C2 | C3
> data D = D1 | D2
> data A = UB B | UC C | UD D -- here's the union type
> For instance a list of type [A] may look like this: [UB B1,UC C2,UC C1,UD D1]
> Haskell mailing list