Haskell 偏单体变压器

Haskell 偏单体变压器,haskell,monads,Haskell,Monads,我正试图将来自的IResultmonad分解为几部分。下面是IResult data IResult t r = Fail t [String] String | Partial (t -> IResult t r) | Done t r 这让人感觉它应该是效果、“偏爱”和失败的组合。如果失败仅表示为一个或([String],String),则可能存在偏好 data Partiality t a = Now a | La

我正试图将来自的
IResult
monad分解为几部分。下面是
IResult

data IResult t r = Fail t [String] String
                 | Partial (t -> IResult t r)
                 | Done t r
这让人感觉它应该是效果、“偏爱”和失败的组合。如果失败仅表示为一个
或([String],String)
,则可能存在偏好

data Partiality t a = Now a | Later (t -> Partiality t a)

instance Monad (Partiality t) where
  return = pure
  (Now a) >>= f = f a
  (Later go) >>= f = Later $ \t -> go t >>= f

class MonadPartial t m where
  feed  :: t -> m a -> m a
  final :: m a -> Bool

instance MonadPartial t (Partiality t) where
  feed _ (Now a) = Now a
  feed t (Later go) = go t
  final (Now _) = True
  final (Later _) = False
(当您使用
Partiality()
时,它将从中获取其名称)


我可以使用
Partiality
作为基本单子,但是有
PartialityT
monad转换器吗?

当然有!您的
偏好
单子是一个免费单子:

import Control.Monad.Free  -- from the `free` package

type Partiality t = Free ((->) t)
。。。相应的
PartialityT
是一个自由单声道转换器:

import Control.Monad.Trans.Free  -- also from the `free` package

type PartialityT t = FreeT ((->) t)
下面是一个示例程序,演示如何使用它:

import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Free

type PartialityT t = FreeT ((->) t)

await :: (Monad m) => PartialityT t m t
await = liftF id

printer :: (Show a) => PartialityT a IO r
printer = forever $ do
    a <- await
    lift $ print a

runPartialityT :: (Monad m) => [a] -> PartialityT a m r -> m ()
runPartialityT as p = case as of
    []   -> return ()
    a:as -> do
        x <- runFreeT p
        case x of
            Pure _ -> return ()
            Free k -> runPartialityT as (k a)
我建议你阅读。然而,免费monad transformer的新官方主页是
free
软件包


另外,如果您正在寻找一个有效的增量解析器,我将在几天内将其作为
pipesparse
包发布。您可以查看添加到主要问题中的。

您的monad实例对于
偏好t
是什么。哦,当然是!我的缺陷是,我无法确定内部一元层应该位于何处并定义
data PartialityT t t m a=PT{runPT::m(Partiality t a)}
。。。这只给了我一个单一的内部一元层!感谢您向正确的方向推进(以及由此产生的明显概括!)
>>> runPartialityT [1..] printer
1
2
3
...