如何从模板Haskell发出重写规则

yai*_*chu 14 haskell template-haskell

我写了一些TemplateHaskell发出重写规则的代码,但是GHC(8.6.5)拒绝了我的规则,并出现以下错误:

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
Run Code Online (Sandbox Code Playgroud)

如果我使用编译-ddump-splices并查看该规则,则可以看到它看起来像这样(重新格式化):

{-# 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)
#-}
Run Code Online (Sandbox Code Playgroud)

如果我将此规则复制到代码中并进行编辑,则只需要删除LHS的多余括号,GHC就可以接受它(这样LHS就变得mapKWith @Pure constraintProxy func没有括号了)。

有没有一种方法可以从TH发出没有多余括号的代码,以便GHC可以接受它来重写规则LHS?还有其他解决方案或解决方法吗?

对于上下文,我正在尝试生成这些规则来帮助GHC内联函数获取RankNTypes值,并且我尝试的代码可在https://github.com/lamdu/syntax-tree/blob/rewrite-rules/src中找到。/AST/TH/Functor.hs#L31