X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=inline;f=docs%2Fusers_guide%2Fglasgow_exts.xml;h=80c40087a066679209616befad389e5cd70ee61e;hb=0093a2827f6b4007c4fcb298a559c9b7dd17aec1;hp=b1e9c3127296a53b15563143520271b46c4a3688;hpb=a975e8341a53ec9cef25af816a9bf9ce96c16391;p=ghc-hetmet.git
diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml
index b1e9c31..80c4008 100644
--- a/docs/users_guide/glasgow_exts.xml
+++ b/docs/users_guide/glasgow_exts.xml
@@ -1152,16 +1152,16 @@ 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.
@@ -2388,14 +2388,14 @@ Haskell 98, you can inherit instances of Eq, Ord
+
newtype Dollars = Dollars Int
-
+
and you want to use arithmetic on Dollars, you have to
explicitly define an instance of Num:
-
+
instance Num Dollars where
Dollars a + Dollars b = Dollars (a+b)
...
@@ -2413,17 +2413,17 @@ dictionary, only slower!
GHC now permits such instances to be derived instead,
using the flag ,
so one can write
-
+
newtype Dollars = Dollars Int deriving (Eq,Show,Num)
-
+
and the implementation uses the sameNum dictionary
for Dollars as for Int. Notionally, the compiler
derives an instance declaration of the form
-
+
instance Num Int => Num Dollars
-
+
which just adds or removes the newtype constructor according to the type.
@@ -2433,27 +2433,27 @@ We can also derive instances of constructor classes in a similar
way. For example, suppose we have implemented state and failure monad
transformers, such that
-
+
instance Monad m => Monad (State s m)
instance Monad m => Monad (Failure m)
-
+
In Haskell 98, we can define a parsing monad by
-
+
type Parser tok m a = State [tok] (Failure m) a
-
+
which is automatically a monad thanks to the instance declarations
above. With the extension, we can make the parser type abstract,
without needing to write an instance of class Monad, via
-
+
newtype Parser tok m a = Parser (State [tok] (Failure m) a)
deriving Monad
In this case the derived instance declaration is of the form
-
+
instance Monad (State [tok] (Failure m)) => Monad (Parser tok m)
-
+
Notice that, since Monad is a constructor class, the
instance is a partial application of the new type, not the
@@ -2468,12 +2468,12 @@ newtype is the last class parameter. In this case, a ``partial
application'' of the class appears in the deriving
clause. For example, given the class
-
+
class StateMonad s m | m -> s where ...
instance Monad m => StateMonad s (State s m) where ...
-
+
then we can derive an instance of StateMonad for Parsers by
-
+
newtype Parser tok m a = Parser (State [tok] (Failure m) a)
deriving (Monad, StateMonad [tok])
@@ -2481,7 +2481,7 @@ then we can derive an instance of StateMonad for Par
The derived instance is obtained by completing the application of the
class to the new type:
-
+
instance StateMonad [tok] (State [tok] (Failure m)) =>
StateMonad [tok] (Parser tok m)
@@ -2501,9 +2501,9 @@ the newtype and its representation.
Derived instance declarations are constructed as follows. Consider the
declaration (after expansion of any type synonyms)
-
+
newtype T v1...vn = T' (t vk+1...vn) deriving (c1...cm)
-
+
where
@@ -2532,17 +2532,17 @@ where
Then, for each ci, the derived instance
declaration is:
-
+
instance ci t => ci (T v1...vk)
As an example which does not work, consider
-
+
newtype NonMonad m s = NonMonad (State s m s) deriving Monad
-
+
Here we cannot derive the instance
-
+
instance Monad (State s m) => Monad (NonMonad m)
-
+
because the type variable s occurs in State s m,
and so cannot be "eta-converted" away. It is a good thing that this
@@ -2556,7 +2556,7 @@ Notice also that the order of class parameters becomes
important, since we can only derive instances for the last one. If the
StateMonad class above were instead defined as
-
+
class StateMonad m s | m -> s where ...
@@ -3144,7 +3144,7 @@ typechecker loop:
class F a b | a->b
instance F [a] [[a]]
instance (D c, F a c) => D [a] -- 'c' is not mentioned in the head
-
+
Similarly, it can be tempting to lift the coverage condition:
class Mul a b c | a b -> c where
@@ -6024,7 +6024,7 @@ happen.
h :: Eq a => a -> a -> a
{-# SPECIALISE h :: (Eq a) => [a] -> [a] -> [a] #-}
-
+
The last of these examples will generate a
RULE with a somewhat-complex left-hand side (try it yourself), so it might not fire very
well. If you use this kind of specialisation, let us know how well it works.