[Haskell-cafe] A Question of Restriction

Ross Mellgren rmm-haskell at z.odi.ac
Mon Jul 27 00:11:03 EDT 2009


I've seen this expressed with GADTs (which I guess many things can),  
though I'm not sure if it's the best way, and I'm no type system  
wizard. If I recall correctly, this use is normally called phantom  
types.

{-# LANGUAGE EmptyDataDecls, GADTs #-}

data Even
data Odd

data Foo a where
     One :: Foo Odd
     Two :: Foo Even
     Three :: Foo Odd
     Four :: Foo Even

Then you can write functions that accept only evens:

f :: Foo Even -> ...

or only odds:

f :: Foo Odd -> ...

or either:

f :: Foo a -> ...

Hope that helps,

-Ross

On Jul 27, 2009, at 12:01 AM, Brian Troutwine wrote:

> Hello all.
>
> I would like to define a data type that is the super-set of several
> types and then each of the proper subset types. For example:
>
>   data Foo = One | Two | Three | Four
>   data Odd = One | Three
>   data Even = Two | Four
>
> This, of course, does not work. It seems that such a thing should
> possible to express entirely in the type system, but I cannot think of
> how. Would someone be so kind as to explain how this sort of thing can
> be accomplished?
>
> Thanks,
> Brian
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list