Implicit Parameters

John Hughes rjmh@cs.chalmers.se
Mon, 4 Feb 2002 11:25:46 +0100 (MET)


	Suppose I have the following function definition:

	  app :: (?ys :: [a]) =3D> [a] -> [a]
	  app xs =3D
	    case ?ys of
	      []      -> xs
	      (y:ys') -> y : (app xs with ?ys =3D ys')

	This function appends its argument to its implicit argument,
	by recursively changing the implicit argument. For example:

	  Hugs> app [1,2] with ?ys =3D [3,4]
	  [3,4,1,2]

	So far so good! Now, since Haskell has type inference, we
	can leave out the type signature:

	  -- app :: (?ys :: [a]) =3D> [a] -> [a]
	  app xs =3D
	    case ?ys of
	      []      -> xs
	      (y:ys') -> y : (app xs with ?ys =3D ys')

	Let us check if it still works again:

	  Hugs> app [1,2] with ?ys =3D [3,4]
	  [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,{Interrupted!}

	And, stunningly, it doesn't! Why doesn't this work?

	That is because type inference assumes that the body of
	`app' is monomorphic, i.e. has no context in its type.
	Therefore, the recursive call to app where ?ys is changed,
	had no effect at all.

	It works with the type signature there, because in order to
	implement type checking (note: not type inference) with
	polymorphic recursion demands to use the stated type in
	checking the body of the function.

	Mark Shields, Simon Peyton-Jones, and I, and also J=F6rgen
	Gustavsson have been discussing several modest solutions to
	this (we believe it is not needed to do full type inference
	that can deal with polymorphic recursion to solve this
	problem).

Not so fast! Proposing a "solution" means this is regarded as a "problem"! But
what is to say that the first behaviour is "right" in any general sense?

The important thing is that the language semantics is clear, and this is a
semantic question.  The static semantics of Haskell *is* clear: recursive
calls are monomorphic unless a type signature is given; this is the basis for
understanding the behaviour above. When implicit parameters are used, it's
very important to be aware whether a binding is monomorphic or not (can't
resist plugging :=3D again!). Will your "solution" make understanding when a
binding is monomorphic simpler? If not, it could be worse than the "problem"
-- and the fact that it makes this one example behave as you want is no
justification.

John