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] Christian
Hi Christian, 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 were allowed. Bernd 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]
Christian
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Mon, 27 Nov 2000 09:30:17 +0100, Bernd Holzmüller <holzmueller@ics-ag.de> pisze:
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?).
Union types don't help here. You cannot use standard map nor filter because they work on lists. Functor instance does not work too because your tree contain some specific types. You have to write your own map implementation anyway. Then just let it take separate functions of appropriate types for all kinds of nodes. Otherwise it cannot be type safe.
Finally, the result has again to be untagged to get a structure of the original types.
What if the original type demanded a particular type and the mapping function produced something different? The function to be mapped really corresponds to a set of functions, one for each kind of information. So it's simpler to be explicit and take separate things separately. If you think it's too constraining, represent the tree with less static type safety. You cannot have both: applying unsafely typed functions to get safely typed trees. If union types can be well defined to fit Haskell and be useful, it must be a different purpose. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
participants (3)
-
Bernd Holzmüller -
Christian Lescher -
qrczak@knm.org.pl