Lloyd Allison | 2 May 03:24 2003
Picon
Picon

a nice Pair,, was: Multiparameter classes in HUGS and GHC

On Wed, Apr 30, 2003 at 07:20:06PM +0100, Graham Klyne wrote:
> I've trying to understand better how to use multiparameter classes, and in
> particular the things that can be declared as instances.  I've consulted
> the following:
> ...

> [[
> class (Eq k, Show k) => Pair a k v where
>      newPair :: (k,v) -> a k v
>      getPair :: a k v -> (k,v)
>
> newtype MyPair1 k v = P1 (Int,String)
>
> instance Pair MyPair1 Int String where
>      newPair (x,y)     = P1 (x,y)
>      getPair (P1 (x,y)) = (x,y)
> ...

--The nicest Pair that I've ever managed is not:

   class Pair p a b where  -- if we use fst' then b is "loose" and v.v.
     fst' :: p->a   -- seems to be :: forall a.(p->a), regardless class Pair p.
     snd' :: p->b   -- can't seem to get a and b from the instances.
   -- Seems to be a problem overloading both fst and snd in one class
   -- but OK if one operator per class ...
--But rather:
   -- ... so we can split Pair into two classes, "left" and "right"!
   class PairL p a where fst'' :: p->a  -- OK but a needs to be implied by use
   class PairR p b where snd'' :: p->b  -- ditto

(Continue reading)

Derek Elkins | 2 May 18:05 2003

Re: a nice Pair,, was: Multiparameter classes in HUGS and GHC

On Fri, 2 May 2003 11:24:16 +1000
Lloyd Allison <lloyd <at> mail.csse.monash.edu.au> wrote:

> On Wed, Apr 30, 2003 at 07:20:06PM +0100, Graham Klyne wrote:
> > I've trying to understand better how to use multiparameter classes,
> > and in particular the things that can be declared as instances. 
> > I've consulted the following:
> > ...
> 
> > [[
> > class (Eq k, Show k) => Pair a k v where
> >      newPair :: (k,v) -> a k v
> >      getPair :: a k v -> (k,v)
> 
> > newtype MyPair1 k v = P1 (Int,String)
> 
> > instance Pair MyPair1 Int String where
> >      newPair (x,y)     = P1 (x,y)
> >      getPair (P1 (x,y)) = (x,y)
> > ...
> 
> 
> --The nicest Pair that I've ever managed is not:
> 
>    class Pair p a b where  -- if we use fst' then b is "loose" and
>    v.v.
>      fst' :: p->a   -- seems to be :: forall a.(p->a), regardless
>      class Pair p. snd' :: p->b   -- can't seem to get a and b from
>      the instances.
>    -- Seems to be a problem overloading both fst and snd in one class
(Continue reading)


Gmane