[GHC] #15490: Can Template Haskell and RULES be combined?

GHC ghc-devs at haskell.org
Wed Aug 8 12:18:14 UTC 2018


#15490: Can Template Haskell and RULES be combined?
-------------------------------------+-------------------------------------
           Reporter:  ChaiTRex       |             Owner:  (none)
               Type:  feature        |            Status:  new
  request                            |
           Priority:  normal         |         Milestone:  8.6.1
          Component:  Compiler       |           Version:  8.4.3
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 It would be nice if there was something like this:

 {{{#!hs
 {-# RULES
     "(literal*)" forall n.
         (n*) = $( makeOptimizedMultiplication [|| n ||] ) #-}
 }}}

 where `makeOptimizedMultiplication` is a Template Haskell function that,
 if `n` is an integer literal, produces an optimized multiplication
 function for that literal. However, that code above gives a `VarE`
 corresponding to the `n` in the quoter rather than the AST of the `n`
 that's actually being matched by the rewrite rule, so I can't get a `LitE`
 that way.

 This sort of thing would come in very handy for a large fixed-width
 integer type, because I don't think GHC and LLVM will know that standard
 fixed-width integer optimizations apply to it. For example, with a
 `Word65536` type, multiplying blindly at runtime by the literal
 `1099511627777` requires a lot of extra work. At the very least, all 65536
 bits of that literal have to be read and dealt with pretty blindly.

 However, at compile time, the compiler can take the time to figure out
 that `(1099511627777*)` can be replaced with the much, much lighter `(\x
 -> shiftL x 40 + x)` (and not only that, but if `Word65536`s are stored in
 a byte array, 1 byte evenly divides the shift amount of 40 bits, which can
 be exploited to speed things up greatly). With appropriate `RULES` and
 Template Haskell functions, the compiler gains the ability to apply lots
 of fixed-width integer optimizations.

 Since the above sort of code doesn't work, is there some other way to do
 this that I don't know about? If not, can the feature be added?

 ----

 The only downside I can think of is that the user of the `Word65536` type
 might not knowingly be expecting Template Haskell to be at work since they
 didn't use any splices or quoters and might not have even included `{-#
 LANGUAGE TemplateHaskell #-}` in their code, so perhaps some easy method
 of consenting to the application of TH-based rewrites might be in order.

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/15490>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list