9

好的,所以我已经弄清楚了如何使用该包来实现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 Readeras 数据......

4

2 回答 2

3

我不认为这是可以做到的,除非他们按照你的方式去做。但是,我不认为这对读者来说是独一无二的。考虑免费的monad版本的writer

data WriterF m a = WriterF m a deriving (Functor)

type Writer m = Free (WriterF m)

显然,WriterF与 writer 同构,但这确实符合我们对简单代数的期望

algebraWriter :: Monoid m => WriterF m (m,a) -> (m,a)
algebraWriter (WriterF m1 (m2,a)) = (m1 <> m2,a)

因此

runWriter :: Monoid m => Writer m a -> (m,a)
runWriter (Pure a) = (mempty,a)
runWriter (Free x) = algebraWriter . fmap runWriter $ x

同样,我认为免费阅读器是

type ReaderF r = (->) r

type Reader r = Free (ReaderF r)

我喜欢这个,因为添加它们会给你状态单子

type State x = Free ((ReaderF x) :+: (WriterF x))

runState :: State x a -> x -> (a,x)
runState (Pure a) x                    = (a,x)
runState (Free (Inl f)) x              = runState (f x) x
runState (Free (Inr (WriterF x f))) _  = runState f x

请注意,您的操作解决方案可以Free通过使用“自由函子”来使用,任何适用于操作的解决方案都可以使用

data FreeFunctor f x = forall a. FreeFunctor (f a) (a -> x)

但是,这FreeFunctor ReaderI也同构于(->).

于 2013-03-15T08:39:52.583 回答
1

好吧,我已经看了 3 个小时了,我想我找到了我更喜欢的东西。由于Readerapplicative 与Readermonad 相同,我们可以尝试一个 applicative 版本operational

{-# LANGUAGE RankNTypes, GADTs, FlexibleInstances #-}

import Control.Applicative

data ProgramA instr a where
    Pure  :: a -> ProgramA r a
    Ap    :: ProgramA r (a -> b) -> ProgramA r a -> ProgramA r b
    Instr :: instr a -> ProgramA instr a

infixl `Ap`

instance Functor (ProgramA instr) where
    fmap f (Pure a) = Pure (f a)
    fmap f (ff `Ap` fa) = ((f .) <$> ff) `Ap` fa
    fmap f instr = Pure f `Ap` instr

instance Applicative (ProgramA instr) where
    pure = Pure
    (<*>) = Ap

interpretA :: Applicative f =>
              (forall a. instr a -> f a)
           -> ProgramA instr a
           -> f a
interpretA evalI (Pure a) = pure a
interpretA evalI (ff `Ap` fa) = interpretA evalI ff <*> interpretA evalI fa
interpretA evalI (Instr i) = evalI i

data ReaderI r a where
    Ask :: ReaderI r r

type Reader r a = ProgramA (ReaderI r) a

ask :: Reader r r
ask = Instr Ask

runReader :: Reader r a -> r -> a
runReader = interpretA (\Ask -> id)

instance Monad (ProgramA (ReaderI r)) where
    return = pure
    ma >>= f = runReader <$> fmap f ma <*> ask

a 的结构可以比orProgramA (ReaderI r) a)更直接地检查。Program (ReaderI r) aFree ((->) r) a

于 2013-03-15T11:19:27.230 回答