[Haskell-cafe] nooby question on typing

Sebastian Sylvan sebastian.sylvan at gmail.com
Sat Sep 13 10:01:41 EDT 2008


On Sat, Sep 13, 2008 at 2:49 PM, Han Joosten <j.m.m.joosten at hccnet.nl>wrote:

>
> Hi,
>
> I have a question about types in Haskell. I feel that I am overlooking some
> obvious solution, but I do not manage to get it right. Here is the plot:
>
> I have got 4 different types of 'rules', each with it's own constructor.
>  So
> i defined:
>
> >  type Rules = [Rule]
> >  data Rule = RuRule
> >            | SgRule
> >            | GcRule
> >            | FrRule
> >                deriving (Eq,Show)


This effectively creates an enum type. I.e. each case here doesn't contain
any data other than the "tag". I think you're getting confused because the
constructor is named the same as the type you're expecting to store. Try
something like:

>  type Rules = [Rule]
>  data Rule = RuRule
>            | MkSgRule SgRule
>            | MkGcRule GcRule
>            | MkFrRule FrRule
>                deriving (Eq,Show)

So MkSgRule is a "tag" or a "label" deciding which version of Rule you're
building, and it also has a value of type SgRule.

Now you can create a list or Rule like so:

>mylist :: [Rule]
>mylist = [ MkSgRule mysgrule, MkGcRule mygcrule ]

where mysgrule :: SgRule and mygcrule :: GcRule.


-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080913/68609bb9/attachment.htm


More information about the Haskell-Cafe mailing list