[Haskell] Implicit parameters

Iavor S. Diatchki diatchki at cse.ogi.edu
Wed Jun 9 17:32:25 EDT 2004

i don't think this is a bug, and this is a situation where it matters
if you use ($) or parens.  the same probelm occurs when you work
with polymorohism, rank-2 and above, e.g. when you use runST.
the problem occurs because ($) has a monomorphic (non-overloaded)
($) :: (a -> b) -> (a -> b)
now consider the type of runST (same example applies to your problem 
bellow, but with constraints)
runST :: (forall s. ST s b) -> b

now if we were to apply ($) to runST, we must make sure that
the type of runST is at least as polymorphic as what is required by ($),i.e.
(forall s. ST s b) -> b <= a -> b
(type on the left should be _more_ polymorphic, i.e. has _less_ elements 
hence the notation)
the above would be true, if
1. b <= b      (which is ok)
2. a <= forall s. ST s b  (which is not ok, as "a" is an arbitry 
_monomorphic_ type, and is not more polymorphic then a schema)

hope this helps, for details on the polymorphism & subtyping you may 
take a look at a number of papers
over the past few years.  there are some on simon pj's page i forget the 
exact title, but it is easy to find.


Per Larsson wrote:

>When using implicit parameters I have noticed (at least for me) a rather 
>puzzling behaviour with GHC and Hugs.
>Given the declarations
>data Env = Env {numlines :: Int, numcols  :: Int}
>initEnv = Env {numlines = 0, numcols = 1}
>withEnv :: ((?env :: Env) => IO a) -> IO a
>withEnv io = let ?env = initEnv in io
>I can write code like:
>main = withEnv (do
>  let lines = numlines ?env
>  putStrLn ("Initial number of lines is " ++ (show lines)))
>which works as expected, but the version below
>main = withEnv $ do
>  let lines = numlines ?env
>  putStrLn ("Initial number of lines is " ++ (show lines))
>is not accepted by either GHC or Hugs! Is this a bug or have stumbled into a 
>context where it actually matters if you use '$' or explicit grouping with 
>Per Larsson
>Haskell mailing list
>Haskell at haskell.org

More information about the Haskell mailing list