gpt4 book ai didi

haskell - `empty'、`<|>' : not a (visible) method of class `Alternative'

转载 作者:行者123 更新时间:2023-12-04 16:38:44 26 4
gpt4 key购买 nike

我正在尝试用 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

关于haskell - `empty'、`<|>' : not a (visible) method of class `Alternative' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65202145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com