gpt4 book ai didi

haskell - 引用半群或幺半群

转载 作者:行者123 更新时间:2023-12-02 05:57:04 25 4
gpt4 key购买 nike

我尝试解决 haskellbook 中的练习并创建了以下模块:

module Exercises where

import Data.Semigroup
import Data.Monoid
import Test.QuickCheck

data Trivial = Trivial deriving (Eq, Show)

instance Semigroup Trivial where
_ <> _ = Trivial

instance Monoid Trivial where
mempty = Trivial
mappend x y = x <> y

编译器提示:

file: 'file:///d%3A/haskell/chapter15/src/Exercises.hs'
severity: 'Error'
message: 'Ambiguous occurrence `<>'
It could refer to either `Data.Semigroup.<>',
imported from `Data.Semigroup' at src\Exercises.hs:3:1-21
or `Data.Monoid.<>',
imported from `Data.Monoid' at src\Exercises.hs:4:1-18'
at: '14,19'
source: ''

如何解决这个问题?

最佳答案

通常情况下,您只需

import Data.Monoid hiding ((<>))

(或者根本不导入 Data.Monoid – 正如 Alec 评论的那样, Monoid 类本身已经从 Prelude 导出。)那么很明确的是x <> y意味着 x Data.Semigroup.<> y ,因为Data.Monoid版本不在范围内。

或者,您可以导入符合条件的模块之一,例如

import qualified Data.Semigroup as SG
import Data.Monoid
import Test.QuickCheck

data Trivial = Trivial deriving (Eq, Show)

instance SG.Semigroup Trivial where
_ <> _ = Trivial

instance Monoid Trivial where
mempty = Trivial
mappend x y = x SG.<> y

关于haskell - 引用半群或幺半群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805103/

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