Multiparameter classes in HUGS and GHC

Graham Klyne gk@ninebynine.org
Wed, 30 Apr 2003 19:20:06 +0100


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:

[1] http://www.haskell.org/onlinereport/decls.html#sect4.3.2

[2] 
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#MULTI-PARAM-TYPE-CLASSES 
(particularly 7.3.5.1 item 3)

[3] http://research.microsoft.com/Users/simonpj/Papers/type-class-design-space/

[4] http://cvs.haskell.org/Hugs/pages/hugsman/exts.html (section 7.1)

 From my reading, it seems that while strict Haskell 98 does not permit 
type expressions or synonyms to be declared as class instances, both GHC 
and HUGS claim to relax this restriction (presumably following the analysis 
in [3]).

My problem is that I can't get HUGS to accept a type expression or synonym 
to be declared as an instance of a (multiparameter) class;  I'm getting the 
error "Illegal type in class constraint".  After some wrestling with the 
system, I have managed to figure how to declare instances that use type 
constructors.   I feel I may be missing something blindingly obvious.  My 
spike code is below:  the uncommented sections compile OK, but the 
commented-out sections do not.

#g
--

[[
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)


data MyPair2 k v = P2 Int String

instance Pair MyPair2 Int String where
     newPair (x,y)   = P2 x y
     getPair (P2 x y) = (x,y)


data (Eq a, Show a) => MyPair3 a b = P3 a b

instance Pair MyPair3 Int String where
     newPair (x,y) = P3 x y
     getPair (P3 x y) = (x,y)


{-
--  The following DO NOT work because instances
--  "must take the form of a type constructor T applied
--  to simple type variables" (though this may be relaxed
--  in the multiparameter case: see [1]).
--
--  Apparently, GHC *does* allow this, though HUGS
--  apparently does not, though it does claim to [2].
--
--  [1] 
http://research.microsoft.com/Users/simonpj/Papers/type-class-design-space/
--
--  [2] http://cvs.haskell.org/Hugs/pages/hugsman/exts.html (section 7.1)
-}

type MyPair4 k v = (k,v)

{-
instance Pair (Int,String) Int String where
     newPair = id
     getPair = id

instance Pair (MyPair4 Int String) Int String where
     newPair = id
     getPair = id

instance Pair (MyPair4 k v) Int String where
     newPair = id
     getPair = id
-}
]]


I'm using the November 2002 release of HUGS with Hugs extensions enabled:
[[
Current settings: +fewuiRWX -stgGl.qQkoOIHTN -h250000 -p"%s> " -r$$ -c40
Search path     : 
-P{Hugs}\lib:{Hugs}\lib\exts:{Hugs}\lib\win32:{Hugs}\lib\hugs;
{Hugs}\libraries\HUnit-1.0
Project Path    :
Source suffixes : -S.hs;.lhs
Editor setting  : -E"C:\\Program Files\\TextPad 4\\TextPad.exe"
Preprocessor    : -F
Compatibility   : Hugs Extensions (-98)
]]



-------------------
Graham Klyne
<GK@NineByNine.org>
PGP: 0FAA 69FF C083 000B A2E9  A131 01B9 1C7A DBCA CB5E