我正在使用Hoopl庫,並希望在重寫時進行一些操作。重寫函數對於使用的monad是多態的,但是我不知道如何將State
monad與其中一個庫的Fuel
monad結合在一起。如何將CheckingFuelMonad與Hoopl中的狀態單元組合起來?
下面是一個簡單的例子。 MyMonad
是Hoopl的CheckingFuelMonad
和State
monad攜帶一個標誌的同義詞。 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
函數?
我超級不服,認爲這個重寫是正確的。這是非常狡猾的生意。 –