From: ross Date: Wed, 23 Jun 2004 10:31:02 +0000 (+0000) Subject: [project @ 2004-06-23 10:31:02 by ross] X-Git-Tag: Initial_conversion_from_CVS_complete~1794 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=eb4e9631e911bb4e9adc10def1f20de5fcaaf173;p=ghc-hetmet.git [project @ 2004-06-23 10:31:02 by ross] rearrange documentation of SPECIALIZE. --- diff --git a/ghc/docs/users_guide/glasgow_exts.sgml b/ghc/docs/users_guide/glasgow_exts.sgml index 000d427..626cfdd 100644 --- a/ghc/docs/users_guide/glasgow_exts.sgml +++ b/ghc/docs/users_guide/glasgow_exts.sgml @@ -4285,40 +4285,20 @@ hammeredLookup :: Ord key => [(key, value)] -> key -> value A SPECIALIZE pragma for a function can be put anywhere its type signature could be put. -A SPECIALIZE has the effect of generating (a) a specialised -version of the function and (b) a rewrite rule (see ) that -rewrites a call to the un-specialised function into a call to the specialised -one. You can, instead, provide your own specialised function and your own rewrite rule. -For example, suppose that: - - genericLookup :: Ord a => Table a b -> a -> b - intLookup :: Table Int b -> Int -> b - -where intLookup is an implementation of genericLookup -that works very fast for keys of type Int. Then you can write the rule - - {-# RULES "intLookup" genericLookup = intLookup #-} - -(see ). It is Your - Responsibility to make sure that - intLookup really behaves as a specialised - version of genericLookup!!! + A SPECIALIZE has the effect of generating + (a) a specialised version of the function and (b) a rewrite rule + (see ) that rewrites a call to the + un-specialised function into a call to the specialised one. - An example in which using RULES for - specialisation will Win Big: + In earlier versions of GHC, it was possible to provide your own + specialised function for a given type: - toDouble :: Real a => a -> Double - toDouble = fromRational . toRational - - {-# RULES "toDouble/Int" toDouble = i2d #-} - i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly +{-# SPECIALIZE hammeredLookup :: [(Int, value)] -> Int -> value = intLookup #-} - The i2d function is virtually one machine - instruction; the default conversion—via an intermediate - Rational—is obscenely expensive by - comparison. + This feature has been removed, as it is now subsumed by the + RULES pragma (see ). @@ -4904,43 +4884,62 @@ Prelude definitions of the above functions to see how to do so. Rewrite rules can be used to get the same effect as a feature -present in earlier version of GHC: +present in earlier versions of GHC. +For example, suppose that: - {-# SPECIALIZE fromIntegral :: Int8 -> Int16 = int8ToInt16 #-} +genericLookup :: Ord a => Table a b -> a -> b +intLookup :: Table Int b -> Int -> b -This told GHC to use int8ToInt16 instead of fromIntegral whenever -the latter was called with type Int8 -> Int16. That is, rather than -specialising the original definition of fromIntegral the programmer is -promising that it is safe to use int8ToInt16 instead. - - - -This feature is no longer in GHC. But rewrite rules let you do the -same thing: +where intLookup is an implementation of +genericLookup that works very fast for +keys of type Int. You might wish +to tell GHC to use intLookup instead of +genericLookup whenever the latter was called with +type Table Int b -> Int -> b. +It used to be possible to write -{-# RULES - "fromIntegral/Int8/Int16" fromIntegral = int8ToInt16 -#-} +{-# SPECIALIZE genericLookup :: Table Int b -> Int -> b = intLookup #-} -This slightly odd-looking rule instructs GHC to replace fromIntegral -by int8ToInt16 whenever the types match. Speaking more operationally, -GHC adds the type and dictionary applications to get the typed rule +This feature is no longer in GHC, but rewrite rules let you do the same thing: -forall (d1::Integral Int8) (d2::Num Int16) . - fromIntegral Int8 Int16 d1 d2 = int8ToInt16 +{-# RULES "genericLookup/Int" genericLookup = intLookup #-} -What is more, -this rule does not need to be in the same file as fromIntegral, -unlike the SPECIALISE pragmas which currently do (so that they +This slightly odd-looking rule instructs GHC to replace +genericLookup by intLookup +whenever the types match. +What is more, this rule does not need to be in the same +file as genericLookup, unlike the +SPECIALIZE pragmas which currently do (so that they have an original definition available to specialise). +It is Your Responsibility to make sure that +intLookup really behaves as a specialised version +of genericLookup!!! + +An example in which using RULES for +specialisation will Win Big: + + +toDouble :: Real a => a -> Double +toDouble = fromRational . toRational + +{-# RULES "toDouble/Int" toDouble = i2d #-} +i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly + + +The i2d function is virtually one machine +instruction; the default conversion—via an intermediate +Rational—is obscenely expensive by +comparison. + +