我写了代码:
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE FlexibleInstances #-}
module MonoidApp where
class Monoid' a where
mempty' :: a
mappend' :: a -> a -> a
mconcat' :: [a] -> a
mconcat' = foldr mappend' mempty'
instance Monoid' Int where
mempty' :: Int a => a
mempty' = 0
mappend' :: Int a => a -> a -> a
mappend' a b = (+) a b
但它遇到了错误:
‘Int’ is applied to too many type arguments
In the type signature for ‘mempty'’: mempty' :: Int a => a
In the instance declaration for ‘Monoid' Int’
Failed, modules loaded: none.
任何想法为什么?
答案 0 :(得分:3)
应该是:
instance Monoid' Int where
mempty' :: Int
mempty' = 0
mappend' :: Int -> Int -> Int
mappend' a b = (+) a b