简体   繁体   English

提升 Monad Reader 本地

[英]Lift Monad Reader local

import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Error

data Context
data Memory 
data Functions

data InterpreterM a = ExeInterpreter a | PropInterpreter a

newtype InterpreterMT m a = InterpreterMT { runInterpreterMT :: m (InterpreterM a) }
type Interpreter = InterpreterMT (StateT (Memory, Functions) (ReaderT (Context, Context) (ErrorT String IO)))

data Stmt
data Stmts = EmptyStmts | Statements Stmt Stmts

interpretStmt :: Stmt -> Interpreter Context

interpreter :: Stmts -> Interpreter ()
interpreter EmptyStmts = return () 
interpreter (Statements s stmts) = do
    currEnv <- interpretStmt s
    local (\(prev, _) -> (prev, currEnv)) $ interpreter stmts

The problem is in last line- there is no lifting- I know it.问题出在最后一行——没有提升——我知道。 But I don't know how to put lift here because my experiments also gives me errors.但我不知道如何把升力放在这里,因为我的实验也给了我错误。 I am asking for help.我在寻求帮助。

If you want a function such as local to be lifted to an inner monad you need the map...T class of functions.如果您希望将local等函数提升到内部 monad,则需要map...T类函数。

For example, if you have a WriterT [String] (ReaderT Int (Either String)) a and you want to do run something with a different reader environment:例如,如果你有一个WriterT [String] (ReaderT Int (Either String)) a并且你想用不同的阅读器环境运行something

mapWriterT (local (+1)) something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM