好的,所以我已经弄清楚了如何使用该包来实现Reader
(并且ReaderT
未显示) :operational
{-# LANGUAGE GADTs, ScopedTypeVariables #-}
import Control.Monad.Operational
data ReaderI r a where
Ask :: ReaderI r r
type Reader r a = Program (ReaderI r) a
ask :: Reader r r
ask = singleton Ask
runReader :: forall r a. Reader r a -> r -> a
runReader = interpretWithMonad evalI
where evalI :: forall b. ReaderI r b -> (r -> b)
evalI Ask = id
但是我一生无法弄清楚如何使用免费的单子来做到这一点(我正在使用 Edward Kmett 的free
包)。我得到的最接近的是这个,我知道这是作弊(关于如何((->) r)
已经是一个单子的东西):
import Control.Monad.Free
type Reader r a = Free ((->) r) a
ask :: Reader r r
ask = Free Pure
runReader :: Reader r a -> r -> a
runReader (Pure a) _ = a
runReader (Free k) r = runReader (k r) r
-- Or, more simply and tellingly:
--
-- > runReader = retract
即使这不像我怀疑的那样愚蠢,这也不是我想要的,因为我想要的基本上是能够检查 a Reader
as 数据......