[[project @ 2002-10-23 15:56:39 by simonpj] simonpj**20021023155639 Document implicit parameter bindings ] { hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1558 -An implicit parameter is introduced by the special form ?x, +An implicit parameter occurs in an exprssion using the special form ?x, hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1560 -any valid identifier. Use if this construct also introduces new -dynamic binding constraints. For example, the following definition +any valid identifier (e.g. ord ?x is a valid expression). +Use of this construct also introduces a new +dynamic-binding constraint in the type of the expression. +For example, the following definition hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1572 + + + +Implicit-parameter type constraints + hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1593 -An implicit parameter differs from other type class constraints in the +An implicit-parameter type constraint differs from other type class constraints in the hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1600 + + You can't have an implicit parameter in the context of a class or instance +declaration. For example, both these declarations are illegal: + + class (?x::Int) => C a where ... + instance (?x::a) => Foo [a] where ... + +Reason: exactly which implicit parameter you pick up depends on exactly where +you invoke a function. But the ``invocation'' of instance declarations is done +behind the scenes by the compiler, so it's hard to figure out exactly where it is done. +Easiest thing is to outlaw the offending types. + + + +Implicit-parameter bindings + hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1617 -An implicit parameter is bound using the standard -let binding form, where the bindings must be a -collection of simple bindings to implicit-style variables (no -function-style bindings, and no type signatures); these bindings are -neither polymorphic or recursive. This form binds the implicit -parameters arising in the body, not the free variables as a -let or where would do. For -example, we define the min function by binding -cmp. +An implicit parameter is bound using the standard +let or where binding forms. +For example, we define the min function by binding +cmp. hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1625 + hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1627 +A group of implicit-parameter bindings may occur anywhere a normal group of Haskell +bindings can occur, except at top level. That is, they can occur in a let +(including in a list comprehension, or do-notation, or pattern guards), +or a where clause. hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1634 +An implicit-parameter binding group must be a +collection of simple bindings to implicit-style variables (no +function-style bindings, and no type signatures); these bindings are +neither polymorphic or recursive. + + hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1643 +(In the case of where you are stuck, since you can't nest where clauses.) hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1648 -single let expression; they are not treated +single binding group; but they are not treated hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1650 -Instead they are treated as a non-recursive group, each scoping over the bindings that -follow. For example, consider: +Instead they are treated as a non-recursive group, simultaneously binding all the implicit +parameter. The bindings are not nested, and may be re-ordered without changing +the meaning of the program. +For example, consider: hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1655 - f y = let { ?x = y; ?x = ?x+1 } in ?x + f t = let { ?x = t; ?y = ?x+(1::Int) } in ?x + ?y hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1657 -This function adds one to its argument. - - - -You may not have an implicit-parameter binding in a where clause, -only in a let binding. - - - - You can't have an implicit parameter in the context of a class or instance -declaration. For example, both these declarations are illegal: +The use of ?x in the binding for ?y does not "see" +the binding for ?x, so the type of f is hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1660 - class (?x::Int) => C a where ... - instance (?x::a) => Foo [a] where ... + f :: (?x::Int) => Int -> Int hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1662 -Reason: exactly which implicit parameter you pick up depends on exactly where -you invoke a function. But the ``invocation'' of instance declarations is done -behind the scenes by the compiler, so it's hard to figure out exactly where it is done. -Easiest thing is to outlaw the offending types. - + hunk ./ghc/docs/users_guide/glasgow_exts.sgml 1666 + }