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