X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=docs%2Fusers_guide%2Fglasgow_exts.xml;h=f8ad5c1b0ac8a79f9ee96c6a4227e6abafcaa6e7;hp=5339c4323f710fd7589811526e4324c480fe3be3;hb=9da4639011348fb6c318e3cba4b08622f811d9c4;hpb=c949e1a9af96cd5241d8cfc74fe3c622258edd7e diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index 5339c43..f8ad5c1 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -905,6 +905,38 @@ fromInteger :: Integer -> Bool -> Bool you should be all right. + + +Postfix operators + + +GHC allows a small extension to the syntax of left operator sections, which +allows you to define postfix operators. The extension is this: the left section + + (e !) + +is equivalent (from the point of view of both type checking and execution) to the expression + + ((!) e) + +(for any expression e and operator (!). +The strict Haskell 98 interpretation is that the section is equivalent to + + (\y -> (!) e y) + +That is, the operator must be a function of two arguments. GHC allows it to +take only one argument, and that in turn allows you to write the function +postfix. + +Since this extension goes beyond Haskell 98, it should really be enabled +by a flag; but in fact it is enabled all the time. (No Haskell 98 programs +change their behaviour, of course.) + +The extension does not extend to the left-hand side of function +definitions; you must define such a function in prefix form. + + + @@ -3198,7 +3230,27 @@ for rank-2 types. - + +Impredicative polymorphism + +GHC supports impredicative polymorphism. This means +that you can call a polymorphic function at a polymorphic type, and +parameterise data structures over polymorphic types. For example: + + f :: Maybe (forall a. [a] -> [a]) -> Maybe ([Int], [Char]) + f (Just g) = Just (g [3], g "hello") + f Nothing = Nothing + +Notice here that the Maybe type is parameterised by the +polymorphic type (forall a. [a] -> +[a]). + +The technical details of this extension are described in the paper +Boxy types: +type inference for higher-rank types and impredicativity, +which appeared at ICFP 2006. + + Lexically scoped type variables @@ -3216,7 +3268,7 @@ f xs = ys ++ ys </programlisting> The type signature for <literal>f</literal> brings the type variable <literal>a</literal> into scope; it scopes over the entire definition of <literal>f</literal>. -In particular, it is in scope at the type signature for <varname>y</varname>. +In particular, it is in scope at the type signature for <varname>ys</varname>. In Haskell 98 it is not possible to declare a type for <varname>ys</varname>; a major benefit of scoped type variables is that it becomes possible to do so. @@ -3249,11 +3301,10 @@ changing the program.</para></listitem> A <emphasis>lexically scoped type variable</emphasis> can be bound by: <itemizedlist> <listitem><para>A declaration type signature (<xref linkend="decl-type-sigs"/>)</para></listitem> +<listitem><para>An expression type signature (<xref linkend="exp-type-sigs"/>)</para></listitem> <listitem><para>A pattern type signature (<xref linkend="pattern-type-sigs"/>)</para></listitem> <listitem><para>Class and instance declarations (<xref linkend="cls-inst-scoped-tyvars"/>)</para></listitem> </itemizedlist> -In addition, GHC supports result type signatures (<xref -linkend="result-type-sigs"/>), although they never bind type variables. </para> <para> In Haskell, a programmer-written type signature is implicitly quantifed over @@ -3302,6 +3353,23 @@ quantification rules. </para> </sect3> +<sect3 id="exp-type-sigs"> +<title>Expression type signatures + +An expression type signature that has explicit +quantification (using forall) brings into scope the +explicitly-quantified +type variables, in the annotated expression. For example: + + f = runST ( (op >>= \(x :: STRef s Int) -> g x) :: forall s. ST s Bool ) + +Here, the type signature forall a. ST s Bool brings the +type variable s into scope, in the annotated expression +(op >>= \(x :: STRef s Int) -> g x). + + + + Pattern type signatures @@ -3310,7 +3378,7 @@ signature. For example: -- f and g assume that 'a' is already in scope - f = \(x::Int, y) -> x + f = \(x::Int, y::a) -> x g (x::a) = x h ((x,y) :: (Int,Bool)) = (y,x) @@ -3352,6 +3420,9 @@ illegal if a was not already in scope. + + + Class and instance declarations @@ -3585,16 +3658,19 @@ declaration (after expansion of any type synonyms) where - The type t is an arbitrary type + The ci are partial applications of + classes of the form C t1'...tj', where the arity of C + is exactly j+1. That is, C lacks exactly one type argument. - The vk+1...vn are type variables which do not occur in - t, and + The k is chosen so that ci (T v1...vk) is well-kinded. - The ci are partial applications of - classes of the form C t1'...tj', where the arity of C - is exactly j+1. That is, C lacks exactly one type argument. + The type t is an arbitrary type. + + + The type variables vk+1...vn do not occur in t, + nor in the ci, and None of the ci is Read, Show, @@ -3607,13 +3683,8 @@ where Then, for each ci, the derived instance declaration is: - instance ci (t vk+1...v) => ci (T v1...vp) + instance ci t => ci (T v1...vk) -where p is chosen so that T v1...vp is of the -right kind for the last parameter of class Ci. - - - As an example which does not work, consider newtype NonMonad m s = NonMonad (State s m s) deriving Monad @@ -3725,9 +3796,9 @@ pattern binding must have the same context. For example, this is fine: -Generalised Algebraic Data Types +Generalised Algebraic Data Types (GADTs) -Generalised Algebraic Data Types (GADTs) generalise ordinary algebraic data types by allowing you +Generalised Algebraic Data Types generalise ordinary algebraic data types by allowing you to give the type signatures of constructors explicitly. For example: data Term a where @@ -3748,9 +3819,20 @@ for these Terms: eval (If b e1 e2) = if eval b then eval e1 else eval e2 eval (Pair e1 e2) = (eval e1, eval e2) -These and many other examples are given in papers by Hongwei Xi, and Tim Sheard. +These and many other examples are given in papers by Hongwei Xi, and +Tim Sheard. There is a longer introduction +on the wiki, +and Ralf Hinze's +Fun with phantom types also has a number of examples. Note that papers +may use different notation to that implemented in GHC. - The extensions to GHC are these: + +The rest of this section outlines the extensions to GHC that support GADTs. +It is far from comprehensive, but the design closely follows that described in +the paper Simple +unification-based type inference for GADTs, +which appeared in ICFP 2006. Data type declarations have a 'where' form, as exemplified above. The type signature of @@ -3842,8 +3924,8 @@ declaration, but only if the data type could also have been declared in Haskell-98 syntax. For example, these two declarations are equivalent data Maybe1 a where { - Nothing1 :: Maybe a ; - Just1 :: a -> Maybe a + Nothing1 :: Maybe1 a ; + Just1 :: a -> Maybe1 a } deriving( Eq, Ord ) data Maybe2 a = Nothing2 | Just2 a @@ -3893,19 +3975,26 @@ the result type of the case expression. Hence the addition < Template Haskell -Template Haskell allows you to do compile-time meta-programming in Haskell. There is a "home page" for -Template Haskell at -http://www.haskell.org/th/, while -the background to +Template Haskell allows you to do compile-time meta-programming in +Haskell. +The background to the main technical innovations is discussed in " Template Meta-programming for Haskell" (Proc Haskell Workshop 2002). -The details of the Template Haskell design are still in flux. Make sure you -consult the online library reference material + + +There is a Wiki page about +Template Haskell at +http://www.haskell.org/th/, and that is the best place to look for +further details. +You may also +consult the online +Haskell library reference material (search for the type ExpQ). [Temporary: many changes to the original design are described in "http://research.microsoft.com/~simonpj/tmp/notes2.ps". -Not all of these changes are in GHC 6.2.] +Not all of these changes are in GHC 6.6.] The first example from that paper is set out below as a worked example to help get you started. @@ -5994,7 +6083,7 @@ r) GHCziBase.ZMZN GHCziBase.Char -> GHCziBase.ZMZN GHCziBase.Cha r) -> tpl2}) - (%note "foo" + (%note "bar" eta); @@ -6096,9 +6185,6 @@ that it is well typed. Generic classes - (Note: support for generic classes is currently broken in - GHC 5.02). - The ideas behind this extension are described in detail in "Derivable type classes", Ralf Hinze and Simon Peyton Jones, Haskell Workshop, Montreal Sept 2000, pp94-105.