我试图将IResult
单子从attoparsec解构为几块。这里是IResult
data IResult t r = Fail t [String] String
| Partial (t -> IResult t r)
| Done t r
这感觉应该是效果、“偏袒”和失败的结合。如果失败只是一种表现形式,Either ([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
(当你使用它时,它的名字来自Danielsson 的一篇论文Partiality ()
)
我可以Partiality
用作基本单子,但是有PartialityT
单子转换器吗?