`empty',`<>':不是`Alternative'类的(可见)方法

我正在尝试用 Haskell 编写一个解析器,但我对它的替代定义有问题(Functor、Applicative 和 Monad 已完成并正在工作):

instance Alternative Parser where
empty = P (env input -> [])
p <|> q = P (env input -> case parse p env input of 
                [] -> parse q env input 
                [(env, v, out)]-> [(env, v, out)])

一旦我使用 Stack (stack build; stack run;) 编译,我就会收到这些错误,包括空运算符和 <|> 运算符:

`empty' is not a (visible) method of class `Alternative'
`<|>' is not a (visible) method of class `Alternative'

有任何想法吗?

回答

您需要导入empty<|>实现它们:

import Control.Applicative (Alternative, empty, (<|>))

或者,也可以通过导入Control.Applicative合格来避免将它们纳入范围:

import Control.Applicative (Alternative)
import qualified Control.Applicative

  • When importing a typeclass, it’s also common to group the members with the class name, either with a wildcard import like `Alternative(..)`, or listing the names explicitly like `Alternative((<|>), empty)`. When importing a module qualified, it’s also typical to introduce an alias, often based on the final component of the name (e.g. `import qualified Control.Applicative as Applicative; import qualified Data.Text as Text`) or an abbreviated mnemonic (`import qualified Data.Foldable as F; import qualified Data.ByteString.Lazy as BL`).

以上是`empty',`&lt;&gt;':不是`Alternative'类的(可见)方法的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>