[Haskell-cafe] Re: type class question

Felipe Lessa felipe.lessa at gmail.com
Wed Dec 5 20:10:31 EST 2007


On Dec 5, 2007 10:38 PM, Ben Franksen <ben.franksen at online.de> wrote:
> data Command = Skip
>
> class Java block command where
>   block_ :: [command] -> block
>
>   compBlock :: [Command] -> block
>   --compBlock = block_ . map compCommand
>
>   compCommand :: Command -> command

My guess is that nothing's guaranteeing the calls from block_ and
compCommand to be using the same 'command' type as the class head. For
example, having

> instance Java B C1
> instance Java B C2

you can have both

> compBlock = (block_ :: [C1] -> B) . map (compCommand :: Command -> C1)
> compBlock = (block_ :: [C2] -> B) . map (compCommand :: Command -> C2)

Also, there's another problem: from compCommand you can't know the
type of block (as it's not appearing in the signature). The modified
version below typechecks:

> data Command = Skip
>
> class Java block command | command -> block where
>  block_ :: [command] -> block
>
>  compBlock :: [Command] -> block
>  compBlock = block_ . map (compCommand :: Command -> command)
>
>  compCommand :: Command -> command

(Note that (compCommand :: Command -> command) actually is restricting
to a monomorphic type.)

So, this seems to me to be a problem with multi-parameter type classes
when you prune the types (on compBlock and on compCommand one of the
types of the class head is missing).

I'm not a wizard on this subject, please anybody correct me if I'm mistaken =).

Cheers,

-- 
Felipe.


More information about the Haskell-Cafe mailing list