我正在使用Hoopl库,并希望在重写时携带一些状态。重写函数对于所使用的 monad 是多态的,但我无法弄清楚如何将State
monad 与库的 monad 之一结合起来Fuel
。
下面是一个最小的例子。MyMonad
是 Hoopl'sCheckingFuelMonad
和State
携带旗帜的单子的同义词。Stmt
只是我的中间语言的占位符,并不重要。
{-# LANGUAGE GADTs, RankNTypes #-}
import Compiler.Hoopl
import Control.Monad.State
type MyMonad = CheckingFuelMonad (State Bool)
data Stmt e x where
Bind :: () -> Stmt O O
rewriter :: forall e x. Stmt e x -> Fact x () -> MyMonad (Maybe (Graph Stmt e x))
rewriter (Bind ()) () = return $ do
f <- get
if f
then return $ Just emptyGraph
else return Nothing
但这不会编译——GHC 抱怨rewrite
类型错误:
Couldn't match expected type `Graph' Block Stmt e x'
against inferred type `Maybe (g n O O)'
Expected type: CheckingFuelMonad
(State Bool) (Maybe (Graph Stmt e x))
Inferred type: CheckingFuelMonad
(State Bool) (Maybe (Maybe (g n O O)))
我想做的事可能吗?如何rewrite
正确编写函数?