Multiparameter classes in HUGS and GHC

Ashley Yakeley ashley@semantic.org
Wed, 28 May 2003 00:51:14 -0700


In article <5.1.0.14.2.20030430182613.00ba1dc8@127.0.0.1>,
 Graham Klyne <gk@ninebynine.org> wrote:

> class (Eq k, Show k) => Pair a k v where
>      newPair :: (k,v) -> a k v
>      getPair :: a k v -> (k,v)

I would consider one of these instead:

  class Pair1 a where
      newPair1 :: (Eq k, Show k) => (k,v) -> a k v
      getPair1 :: (Eq k, Show k) => a k v -> (k,v)

  class (Eq k, Show k) => Pair2 p k v | p -> k,v where
      newPair2 :: (k,v) -> p
      getPair2 :: p -> (k,v)

Pair2 is more general than Pair, for instance:

  data CharBoolPair = MkCharBoolPair Char Bool

  instance Pair2 CharBoolPair Char Bool where
      newPair2 (c,b) = MkCharBoolPair c b
      getPair2 (MkCharBoolPair c b) = (c,b)

CharBoolPair cannot be made an instance of your Pair (since it has kind 
"*").

Pair is more general than my Pair1, there may be type constructors that 
are instances of Pair but not Pair1; but I can't imagine them turning up 
in well-written code.

-- 
Ashley Yakeley, Seattle WA