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

David Menendez zednenem at psualum.com
Fri Apr 29 01:10:23 EDT 2005


Benjamin Franksen writes:

> On Thursday 28 April 2005 16:48, Dimitry Golubovsky wrote:
> > PS Or is there a similar feature in the language already?
> 
> Not one I know of.

The type-indexed co-products from Appendix C of the HList paper[1] are
along those lines, but probably not convenient enough for casual use.

For example,

    data Left a  = Inl a  deriving (Typeable)
    data Right a = Inr a  deriving (Typeable)
    data Nada    = Nada   deriving (Typeable)
    
    type HEither a b = Left a :+: Right b :+: HNil
    
    type HNEither a b = Nada :+: HEither a b

You can write functions which expect a TIC that contains Left a or Right
b, and they will accept TIC (HEither a b) and TIC (HNEither a b) without
modification.

The downside is that a function that might normally be typed "Either A B
-> C" now will have the type:

    (HTypeIndexed l,
     HTypeProxied l,
     HOccurs (Proxy (Left A)) l,
     HOccurs (Proxy (Right B)) l) =>
    TIC l -> C
    
But it will accept a TIC (HEither A B) and a TIC (HNEither A B) and any
other TIC that contains Left A and Right B among its possible values.

[1] <http://homepages.cwi.nl/~ralf/HList/>
-- 
David Menendez <zednenem at psualum.com> | "In this house, we obey the laws
<http://www.eyrie.org/~zednenem>      |        of thermodynamics!"


More information about the Haskell-Cafe mailing list