如何在monad变换器解析器组合器中限制回溯

时间:2012-12-06 15:55:13

标签: parsing haskell monads monad-transformers parser-combinators

tl; dr,如何实现可以限制回溯的解析器,解析器是monad转换器堆栈?

我没有找到任何论文,博客或这种方法的示例实现;似乎限制回溯的典型方法是具有额外构造函数的数据类型,或者默认情况下关闭回溯的Parsec方法。

我目前的实施 - 使用commit组合子,见下文 - 是错误的;我不确定类型,它是否属于类型类,并且我的实例不像它们应该的那样通用。

任何人都可以描述如何干净利落地完成这项工作,还是指向资源?

我在下面添加了我当前的代码;对不起帖子这么久了!


筹码:

StateT
MaybeT/ListT
Either e

意图是回溯在中间层运行 - Nothing或空列表不一定会产生错误,它只是意味着应该尝试不同的分支 - 而底部layer用于错误(带有一些上下文信息),可以立即中止解析。

{-# LANGUAGE NoMonomorphismRestriction, FunctionalDependencies, 
             FlexibleInstances, UndecidableInstances #-}

import Control.Monad.Trans.State   (StateT(..))
import Control.Monad.State.Class   (MonadState(..))
import Control.Monad.Trans.Maybe   (MaybeT(..))
import Control.Monad.Trans.List    (ListT(..))
import Control.Monad               (MonadPlus(..), guard)    

type Parser e t mm a = StateT [t] (mm (Either e)) a

newtype DParser e t a = 
    DParser {getDParser :: Parser e t MaybeT a}

instance Monad (DParser e t) where
  return = DParser . return
  (DParser d) >>= f = DParser (d >>= (getDParser . f))

instance MonadPlus (DParser e t) where
  mzero = DParser (StateT (const (MaybeT (Right Nothing))))
  mplus = undefined   -- will worry about later

instance MonadState [t] (DParser e t) where
  get = DParser get
  put = DParser . put

一些解析类:

class (Monad m) => MonadParser t m n | m -> t, m -> n where
  item  :: m t
  parse :: m a -> [t] -> n (a, [t])

class (Monad m, MonadParser t m n) => CommitParser t m n where
  commit :: m a -> m a

他们的实例:

instance MonadParser t (DParser e t) (MaybeT (Either e)) where
  item = 
      get >>= \xs -> case xs of
                          (y:ys) -> put ys >> return y;
                          []     -> mzero;
  parse = runStateT . getDParser

instance CommitParser t (DParser [t] t) (MaybeT (Either [t])) where
  commit p =
      DParser (
        StateT (\ts -> MaybeT $ case runMaybeT (parse p ts) of
                            Left e          ->  Left e;
                            Right Nothing   ->  Left ts;
                            Right (Just x)  ->  Right (Just x);))

还有几个组合器:

satisfy f = 
    item >>= \x ->
    guard (f x) >>
    return x

literal x = satisfy (== x)

然后是这些解析器:

ab = literal 'a' >> literal 'b'

ab' = literal 'a' >> commit (literal 'b')

给出这些结果:

> myParse ab "abcd"
Right (Just ('b',"cd"))  -- succeeds

> myParse ab' "abcd"
Right (Just ('b',"cd"))  -- 'commit' doesn't affect success

> myParse ab "acd"
Right Nothing       -- <== failure but not an error

> myParse ab' "acd"
Left "cd"           -- <== error b/c of 'commit'

1 个答案:

答案 0 :(得分:1)

答案似乎在MonadOr type class(遗憾的是,我不是标准库的一部分):

class MonadZero m => MonadOr m where   
  morelse :: m a -> m a -> m a 
     

满足Monoid和Left Catch:

morelse mzero b = b 
morelse a mzero = a 
morelse (morelse a b) c = morelse a (morelse b c) 
morelse (return a) b = return a