我正在尝试在 Graham Hutton 的《Haskell 编程》一书中执行示例(http://www.cs.nott.ac.uk/~gmh/book.html)。即使这些示例是在 literate haskell 中,我也可以启动 ghci 来加载示例;例如ghci cipher.lhs
(http://www.cs.nott.ac.uk/~gmh/cipher.lhs):
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( cipher.lhs, interpreted )
Ok, modules loaded: Main.
*Main> let2int 'a'
0
但是对于一些例子,由于 ghci 的变化,我有一些问题;例如在第 8 章的 Parsing.ls 中,我有No instance for (Applicative ...)
错误。
从https://ghc.haskell.org/trac/ghc/wiki/Migration/7.10我得到了通过添加一些代码来消除一些错误的提示。
> instance Applicative Parser where
> pure = return
> (<*>) = ap -- defined in Control.Monad
>
> instance Functor Parser where
> fmap = liftM
>
> instance Alternative Parser where
> (<|>) = mplus
> empty = mzero
但是,我无法解决此错误消息:
Not in scope: type constructor or class ‘Alternative’
这有什么问题,以及如何解决这个问题?导致问题的原始代码来自:http ://www.cs.nott.ac.uk/~gmh/Parsing.lhs
解决方案
添加此代码工作正常:
import qualified Control.Applicative as CA
instance CA.Alternative Parser where ...