幺半群类型的进一步问题 class
Further problems with the monoid type class
我正在尝试使用 Semigroup
类型 class 来玩弄幺半群,并且我正在尝试在自然数上定义一个幺半群。我将以下 class 和实例声明放入 GHCI
Prelude:{
Prelude| class Semigroup a where
Prelude| (<>) :: a -> a -> a)
Prelude| newtype Sum a = Sum { getSum :: a }
Prelude| deriving (Eq, Ord, Show)
Prelude| instance Num a => Monoid (Sum a) where
Prelude| (<>) = coerce ((+) :: a -> a -> a)
Prelude| instance Num a => Monoid (Sum a) where
Prelude| mempty = Sum 0
Prelude| :}
我收到消息:
<interactive>:7:4: error:
Ambiguous occurrence ‘<>’
It could refer to either ‘Prelude.<>’,
imported qualified from ‘Prelude’
(and originally defined in ‘GHC.Base’)
or ‘<>’, defined at <interactive>:3:4
然后我输入import qualified Prelude as P
,以避免冲突,但这不起作用,我得到错误信息:
code<interactive>:26:19: error:
Not in scope: type constructor or class ‘Monoid’
Perhaps you meant one of these:
‘P.Monoid’ (imported from Prelude),
‘P.Monad’ (imported from Prelude)
您应该不自己定义类型类。已经定义好了,再定义一个,就意味着你所有的(<>)
函数等等现在都可以指向Semigroup
你定义的,或者那个在 Prelude
.
中定义
您因此可以定义您的类型并使其成为 Semigroup
和 Monoid
的实例:
import Data.Semigroup
newtype Sum a = Sum { getSum :: a } deriving (Eq, Ord, Show)
instance Num a => Semigroup (Sum a) where
Sum a <> Sum b = Sum (a + b)
instance Num a => Monoid (Sum a) where
mempty = Sum 0
mappend = (<>)
如果我们因此在 shell 中 运行 这个,我们得到:
Prelude> import Data.Semigroup
Prelude Data.Semigroup> :{
Prelude Data.Semigroup| newtype Sum a = Sum { getSum :: a } deriving (Eq, Ord, Show)
Prelude Data.Semigroup|
Prelude Data.Semigroup| instance Num a => Semigroup (Sum a) where
Prelude Data.Semigroup| Sum a <> Sum b = Sum (a + b)
Prelude Data.Semigroup|
Prelude Data.Semigroup| instance Num a => Monoid (Sum a) where
Prelude Data.Semigroup| mempty = Sum 0
Prelude Data.Semigroup| mappend = (<>)
Prelude Data.Semigroup| :}
Prelude Data.Semigroup> Sum 0 <> Sum 1 <> Sum 4 <> Sum 5
Sum {getSum = 10}
我正在尝试使用 Semigroup
类型 class 来玩弄幺半群,并且我正在尝试在自然数上定义一个幺半群。我将以下 class 和实例声明放入 GHCI
Prelude:{
Prelude| class Semigroup a where
Prelude| (<>) :: a -> a -> a)
Prelude| newtype Sum a = Sum { getSum :: a }
Prelude| deriving (Eq, Ord, Show)
Prelude| instance Num a => Monoid (Sum a) where
Prelude| (<>) = coerce ((+) :: a -> a -> a)
Prelude| instance Num a => Monoid (Sum a) where
Prelude| mempty = Sum 0
Prelude| :}
我收到消息:
<interactive>:7:4: error:
Ambiguous occurrence ‘<>’
It could refer to either ‘Prelude.<>’,
imported qualified from ‘Prelude’
(and originally defined in ‘GHC.Base’)
or ‘<>’, defined at <interactive>:3:4
然后我输入import qualified Prelude as P
,以避免冲突,但这不起作用,我得到错误信息:
code<interactive>:26:19: error:
Not in scope: type constructor or class ‘Monoid’
Perhaps you meant one of these:
‘P.Monoid’ (imported from Prelude),
‘P.Monad’ (imported from Prelude)
您应该不自己定义类型类。已经定义好了,再定义一个,就意味着你所有的(<>)
函数等等现在都可以指向Semigroup
你定义的,或者那个在 Prelude
.
您因此可以定义您的类型并使其成为 Semigroup
和 Monoid
的实例:
import Data.Semigroup
newtype Sum a = Sum { getSum :: a } deriving (Eq, Ord, Show)
instance Num a => Semigroup (Sum a) where
Sum a <> Sum b = Sum (a + b)
instance Num a => Monoid (Sum a) where
mempty = Sum 0
mappend = (<>)
如果我们因此在 shell 中 运行 这个,我们得到:
Prelude> import Data.Semigroup
Prelude Data.Semigroup> :{
Prelude Data.Semigroup| newtype Sum a = Sum { getSum :: a } deriving (Eq, Ord, Show)
Prelude Data.Semigroup|
Prelude Data.Semigroup| instance Num a => Semigroup (Sum a) where
Prelude Data.Semigroup| Sum a <> Sum b = Sum (a + b)
Prelude Data.Semigroup|
Prelude Data.Semigroup| instance Num a => Monoid (Sum a) where
Prelude Data.Semigroup| mempty = Sum 0
Prelude Data.Semigroup| mappend = (<>)
Prelude Data.Semigroup| :}
Prelude Data.Semigroup> Sum 0 <> Sum 1 <> Sum 4 <> Sum 5
Sum {getSum = 10}