I am trying to toy around with monoids using the Semigroup
typeclass, and I am trying to define a monoid on the natural numbers. I put the following class and instance declarations into 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| :}
I receive the message:
<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
I then entered import qualified Prelude as P
, so as to avoid the clash, but this does not work, and I get the error message:
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)