RE: [Haskell-cafe] RFE: Extensible algebraic user-defined data types?

| Suppose I want to extend this datatype to the one including | possibility of neither Left or Right (i. e. None). Currently I have to | use Maybe Either, so my options are: | | Just (Left a) | Just (Right b) | Nothing | | If I could extend the Either datatype I might have (syntax may be | different, this is just an example) | | data NEither a b = <Either a b> | None As Ben says, this is altogether a bigger deal. You can imagine two main approaches 1. NEither simply "macro-includes" the constructors of Either in NEither, but they are different constructors. So Either.Left and NEither.Left would be different, and incompatible constructors. This is simple, but clunky; for example, now you'd need to use a qualified name to disambiguate which you meant, whenever you used Left. 2. Use sub-typing, so that a value (Left x) is *both* in type Either and in type NEither. This opens a very large and complicated design space, as Ben mentioned. Simon

On Fri, 29 Apr 2005, Simon Peyton-Jones wrote:
2. Use sub-typing, so that a value (Left x) is *both* in type Either and in type NEither. This opens a very large and complicated design space, as Ben mentioned.
I've been playing with this design space for a while and think there's at least one reasonable solution in it - I'm slowly piecing together a paper, though personal circumstances're causing this to take quite some time (#haskellers will have heard much of this before in chan). If you avoid doing anything too strenuous with records and maintain some reasonably simple restrictions, you only need annotations to disambiguate 'multiple inheritance'-like scenarios and _ patterns. -- flippa@flippac.org "My religion says so" explains your beliefs. But it doesn't explain why I should hold them as well, let alone be restricted by them.

Philippa Cowderoy
On Fri, 29 Apr 2005, Simon Peyton-Jones wrote:
2. Use sub-typing, so that a value (Left x) is *both* in type Either and in type NEither. This opens a very large and complicated design space, as Ben mentioned.
I've been playing with this design space for a while and think there's at least one reasonable solution in it
What about "polymorphic variants" [1] ? They are definitely in the solution space, and work very well in Ocaml. Jacques [1] multiple papers, all downloadable from http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/papers/
participants (3)
-
Jacques Carette
-
Philippa Cowderoy
-
Simon Peyton-Jones