14

I wrote some TemplateHaskell that emits rewrite rules, but GHC (8.6.5) rejects my rules with the following error:

Rule "mapKWith/Pure":
    Illegal expression: ((mapKWith @Pure) constraintProxy)
    in left-hand side: ((mapKWith @Pure) constraintProxy) func
LHS must be of form (f e1 .. en) where f is not forall'd

If I build with -ddump-splices and look at the rule, I can see that it looks like this (reformatted):

{-# RULES "mapKWith/Pure"
    forall
    (constraintProxy :: Proxy constraint)
    (func :: forall child. constraint child => Tree m child -> Tree n child).
    ((mapKWith @Pure) constraintProxy) func =
    \case MkPure x -> MkPure (func x)
#-}

If I copy this rule into my code and edit it, I only need to remove redundant parentheses of the LHS for GHC to accept it (so that the LHS becomes mapKWith @Pure constraintProxy func without parentheses).

Is there a way to emit code from TH without redundant parentheses so that GHC will accept it for rewrite rule LHSs? Is there another solution or a workaround?

For context, I'm trying to generate these rules to help GHC inline functions which get RankNTypes values, and the code for my attempt is available at https://github.com/lamdu/syntax-tree/blob/rewrite-rules/src/AST/TH/Functor.hs#L31

montrealist
  • 5,593
  • 12
  • 46
  • 68
yairchu
  • 23,680
  • 7
  • 69
  • 109
  • You should include the code for your attempt in the question itself. – dfeuer Aug 22 '19 at 11:58
  • @dfeuer: I shared the splices resulting from my code, as the TH code itself would be quite long and not interesting or indicative of the problem, which I believe that the splices do a better job in demonstrating. – yairchu Aug 23 '19 at 17:24
  • Have you tried asking on haskell-cafe and/or glasgow-haskell-users? – dfeuer Aug 30 '19 at 15:01
  • @dfeuer: No, I haven't. I ended up doing other teaks and specialise pragmas and became content with the resulting core. Do you also have a similar problem? – yairchu Sep 01 '19 at 12:48
  • No, I've never learned Template Haskell. – dfeuer Sep 01 '19 at 22:35
  • You are only showing the result of pretty printing. The TH AST would be useful. There are bugs in the pretty printer but they should not prevent splices from working since splices use the AST directly. – Jonas Duregård Nov 21 '19 at 15:45

0 Answers0