Making implicit parameters explicit

Stefan Reich doc at
Wed Nov 5 21:13:12 EST 2003


I discovered implicit parameters today and I'm very excited about them 
because they allow to express certain code constructs more elegantly. 
However, I stumbled upon a problem. Suppose I have these definitions 
(the definition of Request is irrelevant):

type Controller = (?req :: Request) => String

controller1 :: Controller
controller2 :: Controller

controllerList = [controller1, controller2]

GHC complains with:

     Unbound implicit parameter (?req :: Request)
       arising from use of `controller1'
     In the list element: controller1
     In the definition of `controllerList':
         controllerList = [controller1, controller2]

I can see why this isn't allowed; controllerList requires an implicit 
parameter ?req because it references functions needing this parameter. 
But I want it to be parameterless - it's just a static list of 
controller functions after all. The actual request shouldn't be supplied 
when selecting a controller from the list, but rather later, when 
invoking it.

A solution is to make the parameter explicit by binding it:

controllerList = [\req -> let ?req = req in controller1,
                   \req -> let ?req = req in controller2]

This works, but can't really be called elegant (the binding has to be 
repeated for every controller). So I tried to at least move the 
rebinding into a function:

explicitRequest controller req = let ?req = req in controller

controllerList = [explicitRequest controller1,
                   explicitRequest controller2]

This, however, is rejected by GHC. GHC doesn't seem to infer that 
explicitRequest hides the implicit parameter required by the controllers.

Am I confused here or is this a bug in GHC?

Thanks in advance,


More information about the Glasgow-haskell-users mailing list