[Haskell-beginners] mempty and "No instance for (Monoid Int)"

Baa aquagnu at gmail.com
Wed Jun 7 16:33:52 UTC 2017


Maybe a is the Monoid:

  instance Monoid a => Monoid (Maybe a) -- Defined in ‘GHC.Base’

so I can compare its values with empty value:

  mempty == Nothing
  => True

But if I try:

  mempty == Just 4

I get:

  <interactive>:1:1: error:
      • Ambiguous type variable ‘a0’ arising from a use of ‘mempty’
        prevents the constraint ‘(Monoid a0)’ from being solved.
        Probable fix: use a type annotation to specify what ‘a0’ should
         be. These potential instances exist:
          instance Monoid a => Monoid (IO a) -- Defined in ‘GHC.Base’
          instance Monoid Ordering -- Defined in ‘GHC.Base’
          instance Monoid a => Monoid (Maybe a) -- Defined in ‘GHC.Base’
          ...plus 7 others
          (use -fprint-potential-instances to see them all)
      • In the first argument of ‘(==)’, namely ‘mempty’
        In the expression: mempty == Just 4
        In an equation for ‘it’: it = mempty == Just 4

OK, I try:

  mempty::Maybe Int

and get:

  <interactive>:1:1: error:
      • No instance for (Monoid Int) arising from a use of ‘mempty’
      • In the expression: mempty :: Maybe Int
        In an equation for ‘it’: it = mempty :: Maybe Int

so, how is related Int to Monoid, why does ghc expect from mempty::Maybe
Int, Int to be Monoid?! As I understand, this means only that I
mean "mempty" from (Maybe Int) type, which is Monoid and exists sure.

Interesting is, that:

  mempty::Maybe [Int]
  => Nothing

but how is related "monoidality" of "Maybe a" with "monoidality of
"a" ???

Initial idea was to make comparison:

  mempty :: Maybe Int == Just 4
  => False


/Best regards,
  Paul


More information about the Beginners mailing list