From 64e7f7111464db0c8f704c2bc2252c8b99b0a048 Mon Sep 17 00:00:00 2001 From: simonpj Date: Fri, 22 Jul 2005 08:57:58 +0000 Subject: [PATCH 1/1] [project @ 2005-07-22 08:57:58 by simonpj] Docs on SPECIALISE pragma --- ghc/docs/users_guide/glasgow_exts.xml | 34 ++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/ghc/docs/users_guide/glasgow_exts.xml b/ghc/docs/users_guide/glasgow_exts.xml index a1c44ba..3049d60 100644 --- a/ghc/docs/users_guide/glasgow_exts.xml +++ b/ghc/docs/users_guide/glasgow_exts.xml @@ -4612,7 +4612,7 @@ key_function :: Int -> String -> (Bool, Double) overloaded function: -hammeredLookup :: Ord key => [(key, value)] -> key -> value + hammeredLookup :: Ord key => [(key, value)] -> key -> value If it is heavily used on lists with @@ -4620,7 +4620,7 @@ hammeredLookup :: Ord key => [(key, value)] -> key -> value follows: -{-# SPECIALIZE hammeredLookup :: [(Widget, value)] -> Widget -> value #-} + {-# SPECIALIZE hammeredLookup :: [(Widget, value)] -> Widget -> value #-} A SPECIALIZE pragma for a function can @@ -4631,7 +4631,35 @@ hammeredLookup :: Ord key => [(key, value)] -> key -> value (see ) that rewrites a call to the un-specialised function into a call to the specialised one. - In earlier versions of GHC, it was possible to provide your own + The type in a SPECIALIZE pragma can be any type that is less + polymorphic than the type of the original function. In concrete terms, + if the original function is f then the pragma + + {-# SPECIALIZE f :: <type> #-} + + is valid if and only if the defintion + + f_spec :: <type> + f_spec = f + + is valid. Here are some examples (where we only give the type signature + for the original function, not its code): + + f :: Eq a => a -> b -> b + {-# SPECIALISE g :: Int -> b -> b #-} + + g :: (Eq a, Ix b) => a -> b -> b + {-# SPECIALISE g :: (Eq a) => a -> Int -> Int #-} + + 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. + + + Note: In earlier versions of GHC, it was possible to provide your own specialised function for a given type: -- 1.7.10.4