繁体   English   中英

Haskell列表中的Monad

[英]Monad of list in Haskell

我正在尝试使它成为Haskell中Monad的一个实例:

data Parser a = Done ([a], String) | Fail String

现在,我尝试使用以下代码使其成为Monad的实例:

instance Functor Parser where
  fmap = liftM

instance Applicative Parser where
  pure = return
  (<*>) = ap

instance Monad Parser where
  return xs = Done ([], xs)
  Done (xs, s) >>= f = Done (concat (map f xs)), s)

但这显然不起作用,因为bind-function中的函数f是a- a -> M b类型。 因此(map f xs)函数产生M b事物的列表。 它实际上应该列出b的列表。 如何在Haskell中执行此操作?

注意:GHC 7.10.3给出的实际错误是:

SuperInterpreter.hs:71:27:
Couldn't match expected type `String' with actual type `a'
  `a' is a rigid type variable bound by
      the type signature for return :: a -> Parser a
      at SuperInterpreter.hs:71:5
Relevant bindings include
  xs :: a (bound at SuperInterpreter.hs:71:12)
  return :: a -> Parser a (bound at SuperInterpreter.hs:71:5)
In the expression: xs
In the first argument of `Done', namely `([], xs)'

SuperInterpreter.hs:72:45:
Couldn't match type `Parser b' with `[b]'
Expected type: a -> [b]
  Actual type: a -> Parser b
Relevant bindings include
  f :: a -> Parser b (bound at SuperInterpreter.hs:72:22)
  (>>=) :: Parser a -> (a -> Parser b) -> Parser b
    (bound at SuperInterpreter.hs:72:5)
In the first argument of `map', namely `f'
In the first argument of `concat', namely `(map f xs)'
Failed, modules loaded: none.

leftaroundabout已经向您显示了一些问题。

通常,我希望解析器是一种接受输入String的函数,可能会消耗其中的一些字符串,然后将结果与未使用的输入一起返回。

基于这个想法,您可以扩展代码来做到这一点:

data ParserResult a
  = Done (a, String)
  | Fail String
  deriving Show

data Parser a = Parser { run :: String -> ParserResult a }

instance Functor Parser where
  fmap = liftM

instance Applicative Parser where
  pure = return
  (<*>) = ap

instance Monad Parser where
  return a = Parser $ \ xs -> Done (a, xs)
  p >>= f = Parser $ \ xs ->
    case run p xs of
      Done (a, xs') -> run (f a) xs'
      Fail msg      -> Fail msg

一个简单的例子

这是一个可以接受任何字符的简单解析器:

parseAnyChar :: Parser Char
parseAnyChar = Parser f
  where f (c:xs) = Done (c, xs)
        f ""     = Fail "nothing to parse"

这是您将如何使用它:

λ> run parseAnyChar "Hello"
Done ('H',"ello")
λ> run parseAnyChar ""
Fail "nothing to parse"

虽然定义fmap = liftM并不少见,但这在IMO方面有些落后。 如果您首先定义更基本的实例,并在其中包含更多的实例,那么事情往往会变得更加清晰。 我将离开<*> = ap ,但要转换其他所有内容:

instance Functor Parser where  -- Note that you can `derive` this automatically
  fmap f (Done vs rest) = Done (map f vs) rest
  fmap f (Fail err) = Fail err

instance Applicative Parser where
  pure xs = Done ([], xs)
  (<*>) = ap

现在已经有了fmap ,我可以用“更数学的”方式定义Monad :定义join而不是>>=

instance Monad Parser where
  return = pure
  q >>= f = joinParser $ fmap f q

这意味着您将使用直观可处理的具体值,而不必担心通过解析器线程化函数。 因此,您可以清楚地看到发生了什么,只需写出递归即可:

joinParser :: Parser (Parser a) -> Parser a
  joinParser (Fail err) = Fail err
  joinParser (Done [] rest) = Done [] rest
  joinParser (Done (Fail err : _) _) = Fail err
  joinParser (Done (Done v0 rest0 : pss) rest) = ??

在这一点上,您可以清楚地看到Carsten已经说过的话: Parser类型对于解析器来说实际上没有任何意义。 内部的和外部的Done包装器都以某种方式包含rest数据; 结合起来就意味着您要结合未完成的工作……这不是解析器所做的。

在网上搜索一下,有很多关于如何在Haskell中实现解析器的材料。 毫无疑问,请看一些已建立的库是如何做到的, 例如parsec

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM