[Haskell-cafe] pattern matching on data families constructors
electreg at list.ru
Thu Apr 25 18:29:16 CEST 2013
suppose that there is following data family:
> data family D a
> data instance D Int = DInt Int
> data instance D Bool = DBool Bool
it is not possible to match on constructors from different instances:
> -- type error
> a :: D a -> a
> a (DInt x) = x
> a (DBool x) = x
however, following works:
> data G :: * -> * where
> GInt :: G Int
> GBool :: G Bool
> b :: G a -> D a -> a
> b GInt (DInt x) = x
> b GBool (DBool x) = x
The reason why second example works is equality constraints (a ~ Int) and (a ~ Bool) introduced by GADT's constructors, I suppose.
I'm curious - why data families constructors (such as DInt and DBool) doesn't imply such constraints while typechecking pattern matching?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe