Passing values taking implicit parameters

Simon Peyton-Jones simonpj@microsoft.com
Thu, 31 Jan 2002 01:18:40 -0800


You don't point it out explicitly, but you are using rank-2 types here!

This is indeed a bug in 5.02, fixed in the current release 5.02.2

Simon

| -----Original Message-----
| From: Mike Gunter [mailto:m@ryangunter.com]=20
| Sent: 31 January 2002 05:36
| To: glasgow-haskell-users@haskell.org
| Subject: Passing values taking implicit parameters
|=20
|=20
|=20
| I'd like write a function taking values with implicit=20
| parameters.  I can't get this to work in a straightforward=20
| way in GHC 5.02 (with -fno-monomorphism-restriction.)  In=20
| particular, if I try:
|=20
| > wF :: ((?x :: Int) =3D> a) -> Int -> a	-- compiles but=20
| tf won't work
| > wF s z =3D s with ?x =3D z
| >=20
| > f :: (?x :: Int) =3D> Int
| > f =3D (?x + 3)
| > -- tf :: Int 				-- If=20
| uncommented GHC fails because can't deduce context (?x::Int)=20
| > -- tf :: (?x :: Int) =3D> Int		-- Type GHC assigns to tf
| > tf =3D wF f 8				-- Trying to evaluate=20
| gives: "Unbound implicit parameter (?x :: Int)"
|=20
| Trying to evaluate tf gives: "Unbound implicit parameter (?x :: Int)"
|=20
|=20
| On the other hand, if the passed value has another class in=20
| its context, things work as expected:
|=20
| > wF' :: Integral a =3D> (forall a . (Integral a, ?x :: Int) =3D>=20
| a) -> Int -> a=20
| > wF' s z =3D s with ?x =3D z=20
| > f' :: (Integral a, ?x :: Int) =3D> a
| > f' =3D fromIntegral (?x + 3)
| > tf' =3D wF' f' 8			-- GHCi gives me 11, as expected
|=20
|=20
| At the cost of having to wrap each value with an extra call=20
| (to "dummy" here), I seem to be able to get the
| desired effect for general types with this code:
|=20
| > class DummyCl c		where dummy :: a -> c a
| > newtype Dummy a	=3D Dummy { unDummy :: a }
| > instance DummyCl Dummy	where dummy =3D Dummy
| >=20
| > wF'' :: (forall c . (DummyCl c, ?x :: Int) =3D> c a) -> Int -> a
| > wF'' s z =3D unDummy (s with ?x =3D z)
|=20
| > -- f'' :: (DummyCl c, ?x :: Int) =3D> c Int	-- Type=20
| signature unnecessary
| > f'' =3D dummy f
|=20
| > tf''	=3D wF'' f'' 100				-- evaluates
|=20
| > fHO	=3D dummy (?x +)
| > tfHO	=3D (wF'' fHO 31) 22			-- evaluates
|=20
| > tf''' =3D wF'' (dummy f) 1000			-- gets:=20
| "Unbound implicit parameter (?x :: Int)"=20
| > tf4	=3D wF'' (dummy (?x + 7)) 300		-- evaluates
| > tfHO'	=3D (wF'' (dummy (show . (?x +))) 50) 63	-- evaluates
|=20
|=20
|=20
| So my questions are: Is there a straightforward way to pass=20
| values taking implicit parameters in GHC?  And, if
| not, is there a better workaround than my "dummy" code above?
|=20
|         thanks,
|         mike
|=20
|=20
| _______________________________________________
| Glasgow-haskell-users mailing list
| Glasgow-haskell-users@haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
|=20