问题描述
我正在使用图书馆,并希望在重写时带上一些状态。重写函数对于使用monad是多态的,但我不知道如何将 State
monad与其中一个 Fuel
monads。
下面是一个简单的例子。 MyMonad
是Hoopl的 CheckingFuelMonad
和a State
monad的同义词携带一面旗帜。 Stmt
只是我的中间语言的占位符,并不重要。
{ - #LANGUAGE GADTs,RankNTypes# - }
import Compiler.Hoopl
import Control.Monad.State
type MyMonad = CheckingFuelMonad(State Bool)
data Stmt ex where
Bind ::() - > Stmt O O
rewriter :: forall e x。 Stmt e x - >事实x() - > MyMonad(Maybe(Graph Stmt ex))
重写器(Bind())()= return $ do
f< - 获得
if f
然后返回$只是emptyGraph
else return Nothing
但是这不会编译 - GHC抱怨重写
的类型错误:
无法匹配预期的类型`Graph'Block Stmt e x '
对推断类型'Maybe(gn OO)'
预期类型:CheckingFuelMonad
(State Bool)(Maybe(Graph Stmt ex))
推断类型:CheckingFuelMonad
(State Bool)(Maybe(Maybe(gn OO)))
我想要做什么可能?如何正确写入 rewrite
函数?
浏览hoopl代码揭示了CheckingFuelMonad不是MonadTrans的一个实例,并且你不能将它作为一个实例,因为它的构造函数没有被导出。您可以 围绕CheckingFuelMonad包装StateT,如下所示: # - } I am using the Hoopl library and would like to carry some state around while rewriting. The rewrite functions are polymorphic regarding the monad used, but I cannot figure out how to combine a Below is a minimal example. But this will not compile -- GHC complains that Is what I want to do possible? How can I write the A browse through hoopl code reveals that CheckingFuelMonad isn't an instance of MonadTrans, and you can't make it one, since its constructors are not exported. You can however wrap a StateT around CheckingFuelMonad, like so: 这篇关于如何将CheckingFuelMonad与Hoopl中的状态单元组合起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
import Compiler.Hoopl
import Control.Monad.State
type MyMonad = StateT Bool SimpleFuelMonad
data Stmt ex其中
Bind ::() - > Stmt O O
rewriter :: forall e x。 Stmt e x - >事实x() - > MyMonad(也许(Graph Stmt ex))
重写器(Bind())()= do
f< - 获得
如果f
则返回$只是emptyGraph
否则返回Nothing
State
monad with one of the library's Fuel
monads.MyMonad
is a synonym combining Hoopl's CheckingFuelMonad
and a State
monad carrying a flag. Stmt
is just a placeholder for my intermediate language and isn't really important. {-# 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
rewrite
has the wrong type: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
function correctly?{-# LANGUAGE GADTs, RankNTypes #-}
import Compiler.Hoopl
import Control.Monad.State
type MyMonad = StateT Bool SimpleFuelMonad
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 ()) () = do
f <- get
if f
then return $ Just emptyGraph
else return Nothing