[Haskell-cafe] Specializing classes with classes

ajb at spamcop.net ajb at spamcop.net
Fri Dec 28 23:18:01 EST 2007


G'day all.

Quoting alex <maskif at yahoo.com.au>:

> I would like to do this:
>
>     class Foo t where
>         hi :: t -> Bool
>
>     class Foo t => Bar t where
>         hi x = True

This is arguably one of the most requested features in Haskell.  The only
reason why it hasn't been implemented yet is that some of the corner cases
are a little subtle.

But this will get fixed, won't it, haskell-prime people?

What you'll have to do until then is something like this:

     class Num a => Alg a where
         (<>)    :: Mat m => m -> a -> a

     multByVec :: (Vec v, Mat m) => m -> a -> a
     multByVec m v = fromColumn (m <> toColumn v)

     class Alg v 	=> Vec v where
         toRow, toColumn     :: Mat m => v -> m
         fromRow, fromColumn :: Mat m => m -> v

         fromRow     = fromColumn . transpose
         toRow       = transpose . toColumn

     class Alg m     => Mat m where
         transpose   :: m -> m

     class Alg SomeType where
         (<>) = multByVec

     class Vec SomeType where
         toRow = {- etc etc -}
         {- and so on -}

Sorry, I wish there was a better answer.

Cheers,
Andrew Bromage


More information about the Haskell-Cafe mailing list