本地提升Monad Reader [英] Lift Monad Reader local

查看:91
本文介绍了本地提升Monad Reader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

如果要将诸如local之类的功能提升为内部monad,则需要map...T类的功能.

If you want a function such as local to be lifted to an inner monad you need the map...T class of functions.

例如,如果您有WriterT [String] (ReaderT Int (Either String)) a,并且想在其他阅读器环境中运行something:

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:

mapWriterT (local (+1)) something

这篇关于本地提升Monad Reader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆