理解attoparsec解析器monad

时间:2014-12-08 14:18:32

标签: haskell monads

我目前正在尝试研究attoparsec库的源代码,现在我被卡在Parser的monad定义中。见here.

instance Monad (Parser i) where

   fail err = Parser $ \t pos more lose _succ -> lose t pos more [] msg
      where msg = "Failed reading: " ++ err

    return v = Parser $ \t pos more _lose succ -> succ t pos more v

    m >>= k = Parser $ \t !pos more lose succ ->
        let succ' t' !pos' more' a = runParser (k a) t' pos' more' lose succ
        in runParser m t pos more lose succ'

特别是我无法绕过(>>=)运算符的定义。 例如,在快递let succ' t' !pos' more' a = runParser (k a) t' pos' more' lose succ中:参数pos't'来自哪里?

也许我现在太困惑了,看不出明显的,所以如果有人能解释Parser类型的monad定义以帮助我更好地理解它,我将不胜感激......

提前谢谢!

1 个答案:

答案 0 :(得分:2)

这是定义函数succ'

succ' :: Success i (State i) a r
succ' t' !pos' more' a = runParser (k a) t' pos' more' lose succ

-- type Success i t a r = t -> Pos -> More -> a -> IResult i r
-- t' :: t
-- pos' :: Pos
-- more' :: More
-- a :: a
-- runParser (k a) t' pos' more' lose succ :: IResult i r

没有什么特别的,它只是一个本地函数定义。然后将此函数作为最后一个参数传递给runParser