From b8554efd031fc4ff0d9b3950f8320733058358d8 Mon Sep 17 00:00:00 2001 From: simonpj Date: Mon, 21 Jul 2003 11:06:22 +0000 Subject: [PATCH] [project @ 2003-07-21 11:06:22 by simonpj] More on rules and specialisations --- ghc/docs/users_guide/flags.sgml | 3 ++- ghc/docs/users_guide/glasgow_exts.sgml | 46 +++++++++++++++++--------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/ghc/docs/users_guide/flags.sgml b/ghc/docs/users_guide/flags.sgml index b9464a6..0afa9df 100644 --- a/ghc/docs/users_guide/flags.sgml +++ b/ghc/docs/users_guide/flags.sgml @@ -735,7 +735,8 @@ - Switch off all rewrite rules + Switch off all rewrite rules (including rules + generated by automatic specialisation of overloaded functions) static diff --git a/ghc/docs/users_guide/glasgow_exts.sgml b/ghc/docs/users_guide/glasgow_exts.sgml index 8f1a41f..ded5567 100644 --- a/ghc/docs/users_guide/glasgow_exts.sgml +++ b/ghc/docs/users_guide/glasgow_exts.sgml @@ -4188,35 +4188,34 @@ hammeredLookup :: Ord key => [(key, value)] -> key -> value A SPECIALIZE pragma for a function can be put anywhere its type signature could be put. - To get very fancy, you can also specify a named function - to use for the specialised value, as in: - +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: -{-# RULES "hammeredLookup" hammeredLookup = blah #-} + genericLookup :: Ord a => Table a b -> a -> b + intLookup :: Table Int b -> Int -> b - - where blah is an implementation of - hammerdLookup written specialy for - Widget lookups. It's Your +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 - blah really behaves as a specialised - version of hammeredLookup!!! - - Note we use the RULE pragma here to - indicate that hammeredLookup applied at a - certain type should be replaced by blah. See - for more information on - RULES. + 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 + toDouble :: Real a => a -> Double + toDouble = fromRational . toRational -{-# RULES "toDouble/Int" toDouble = i2d #-} -i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly + {-# RULES "toDouble/Int" toDouble = i2d #-} + i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly The i2d function is virtually one machine @@ -4266,7 +4265,10 @@ of the pragma. The programmer can specify rewrite rules as part of the source program -(in a pragma). GHC applies these rewrite rules wherever it can. +(in a pragma). GHC applies these rewrite rules wherever it can, provided (a) +the flag () is on, +and (b) the flag +() is not specified. @@ -4714,7 +4716,7 @@ will fuse with one but not the other) - + So, for example, the following should generate no intermediate lists: -- 1.7.10.4