[Haskell-cafe] Writing guards shorthand

Neil Mitchell ndmitchell at gmail.com
Thu Apr 19 09:21:34 EDT 2007


Hi Jules,

> What this leads us towards is that it might be rather nice (perhaps even
> nice enough to build into a compiler) to be able to derive, for each
> type with multiple constructors, a type 'which is the enumeration of the
> constructors'. I.e. a type with the same constructors (up to some
> namespace fix like prepending with C) but all nullary:
>
> data Atom = Null | MyInt Int | MyString String
>
> derives...
>
> data AtomCons = CNull | CMyInt | CMyString deriving (Ord,Eq,Enum,Show)
>
> and a function
>
> constr :: Atom -> AtomCons

If you can derive Enum for all possible constructors (supplying
undefined for all fields) then people can do this already:

isBool x = fromEnum x `elem` [fromEnum LT .. fromEnum GT]

And I'm sure with an "elemCtorSet" you could automate most of this
away in to a tidy little function. As a side note, this is what Derive
already does :-)

Thanks

Neil


More information about the Haskell-Cafe mailing list