X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fglasgow_exts.xml;h=a25d42e89712e066a786d6f5cda416e9ea96dd2b;hb=8e14697c6e2cccc0a632233ffe95505765db2eef;hp=ff46b3943af6e73e483ab71c4735d140bb2e2541;hpb=e25528e60aef15cf6697b4a30217f1f1f22e9aa0;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/glasgow_exts.xml b/ghc/docs/users_guide/glasgow_exts.xml index ff46b39..a25d42e 100644 --- a/ghc/docs/users_guide/glasgow_exts.xml +++ b/ghc/docs/users_guide/glasgow_exts.xml @@ -925,18 +925,34 @@ Nevertheless, they can be useful when defining "phantom types". -Infix type constructors +Infix type constructors and classes -GHC allows type constructors to be operators, and to be written infix, very much +GHC allows type constructors and classes to be operators, and to be written infix, very much like expressions. More specifically: - A type constructor can be an operator, beginning with a colon; e.g. :*:. + A type constructor or class can be an operator, beginning with a colon; e.g. :*:. The lexical syntax is the same as that for data constructors. - Types can be written infix. For example Int :*: Bool. + Data type and type-synonym declarations can be written infix, parenthesised + if you want further arguments. E.g. + + data a :*: b = Foo a b + type a :+: b = Either a b + class a :=: b where ... + + data (a :**: b) x = Baz a b x + type (a :++: b) y = Either (a,b) y + + + + Types, and class constraints, can be written infix. For example + + x :: Int :*: Bool + f :: (a :=: b) => a -> b + Back-quotes work @@ -944,7 +960,7 @@ like expressions. More specifically: Int `a` Bool. Similarly, parentheses work the same; e.g. (:*:) Int Bool. - Fixities may be declared for type constructors just as for data constructors. However, + Fixities may be declared for type constructors, or classes, just as for data constructors. However, one cannot distinguish between the two in a fixity declaration; a fixity declaration sets the fixity for a data constructor and the corresponding type constructor. For example: @@ -958,13 +974,6 @@ like expressions. More specifically: Function arrow is infixr with fixity 0. (This might change; I'm not sure what it should be.) - Data type and type-synonym declarations can be written infix. E.g. - - data a :*: b = Foo a b - type a :+: b = Either a b - - - The only thing that differs between operators in types and operators in expressions is that ordinary non-constructor operators, such as + and * are not allowed in types. Reason: the uniform thing to do would be to make them type @@ -1722,7 +1731,7 @@ means Instance declarations - + Overlapping instances In general, GHC requires that that it be unambiguous which instance @@ -1778,6 +1787,11 @@ So GHC rejects the program. If you add the flag + +Because overlaps are checked and reported lazily, as described above, you need +the in the module that calls +the overloaded function, rather than in the module that defines it. + @@ -2664,22 +2678,19 @@ for rank-2 types. -A pattern type signature can introduce a scoped type -variable. For example - - - - +A lexically scoped type variable can be bound by: + +A declaration type signature () +A pattern type signature () +A result type signature () + +For example: f (xs::[a]) = ys ++ ys where ys :: [a] ys = reverse xs - - - - The pattern (xs::[a]) includes a type signature for xs. This brings the type variable a into scope; it scopes over all the patterns and right hand sides for this equation for f. @@ -2687,8 +2698,6 @@ In particular, it is in scope at the type signature for y. - Pattern type signatures are completely orthogonal to ordinary, separate -type signatures. The two can be used independently or together. At ordinary type signatures, such as that for ys, any type variables mentioned in the type signature that are not in scope are implicitly universally quantified. (If there are no type variables in @@ -2711,10 +2720,10 @@ So much for the basic idea. Here are the details. -What a pattern type signature means +What a scoped type variable means -A type variable brought into scope by a pattern type signature is simply -the name for a type. The restriction they express is that all occurrences +A lexically-scoped type variable is simply +the name for a type. The restriction it expresses is that all occurrences of the same name mean the same type. For example: f :: [Int] -> Int -> Int @@ -2884,7 +2893,33 @@ scope over the methods defined in the where part. For exampl - + +Declaration type signatures +A declaration type signature that has explicit +quantification (using forall) brings into scope the +explicitly-quantified +type variables, in the definition of the named function(s). For example: + + f :: forall a. [a] -> [a] + f (x:xs) = xs ++ [ x :: a ] + +The "forall a" brings "a" into scope in +the definition of "f". + +This only happens if the quantification in f's type +signature is explicit. For example: + + g :: [a] -> [a] + g (x:xs) = xs ++ [ x :: a ] + +This program will be rejected, because "a" does not scope +over the definition of "f", so "x::a" +means "x::forall a. a" by Haskell's usual implicit +quantification rules. + + + + Where a pattern type signature can occur @@ -3001,10 +3036,12 @@ in f4's scope. +Pattern type signatures are completely orthogonal to ordinary, separate +type signatures. The two can be used independently or together. - + Result type signatures