I'm trying to create a very simple monad in Haskell. The monad does nothing special but holding a counter as state.
module EmptyMonad
( EmptyMonad
) where
import Control.Monad
data EmptyMonad a = EmptyMonad
{ myValue :: a
, myState :: Int
} deriving (Show)
instance (Eq a) => Eq (EmptyMonad a) where
EmptyMonad x1 y1 == EmptyMonad x2 y2 = x1 == x2 && y1 == y2
instance Monad (EmptyMonad a) where
return x = EmptyMonad x 0
(EmptyMonad x y) >>= f = EmptyMonad x (y + 1)
After spending few hours on Monads, I cannot get my head around the error from the compiler:
EmptyMonad.hs:16:10: error:
• Expecting one fewer argument to ‘Monad EmptyMonad’
Expected kind ‘k0 -> Constraint’,
but ‘Monad EmptyMonad’ has kind ‘Constraint’
• In the instance declaration for ‘Monad EmptyMonad a’
Failed, modules loaded: none.
instance Monad EmptyMonad where(withouta). - Willem Van Onsem(EmptyMonad x y) >>= f = EmptyMonad (f x) (y + 1). (withf), otherwise the types do not match. - Willem Van Onsemreturnis an identity" law saying thatreturn x >>= f = f x, since there are fewer binds on the right-hand side of the equation. (It seems to be everybody's first idea for a new monad, though, including mine!) - Daniel Wagner