I am trying to make a tic-tac-toe game, and I decided to construct types for cells (elements of the board) and the board as follows:
data Cell = X | O deriving (Show, Eq)
type Board = [[Maybe Cell]]
Here, Nothing represents an empty cell, (Just X) and (Just O) represent cells filled with X and O respectively.
I would like to define (Maybe Cell) as a monoid as follows:
instance Monoid (Maybe Cell) where
mempty = Nothing
mappend Nothing x = x
mappend (Just x) _ = (Just x)
And Board as another monoid with
instance Monoid Board where
mempty = [[Nothing, Nothing, Nothing]
,[Nothing, Nothing, Nothing]
,[Nothing, Nothing, Nothing]]
mappend = zipWith (zipWith mappend)
-- where the right-hand-side mappend is the addition on (Maybe Cell)
I know I could implement this absolutely without monoids, but I'm trying to explore the field, and it's just a really neat way to write it.
The problem that I get is that a Maybe monoid instance is already defined in GHC.Base as follows:
instance Semigroup a => Monoid (Maybe a)
This has a very different definition than what I want, but it causes duplicate instance declarations, so I can't just ignore it.
What I'm trying to do is to hide the Monoid instance of (Maybe a) from GHC.Base to avoid duplicate instances. I tried searching a lot for that, but couldn't really find a way to hide that. I can't hide all of Monoid or all of Semigroup, because I need their functions, but I need to hide this specific instance declaration. Could anyone help me with that?
NOTE: I'm using FlexibleInstances.
instancedeclarations. What you can do though is use anewtypeto "wrap" the underlying types. This is done quite a lot even in the standard libraries - for example theSumandProducttypes are both wrappers forInt(actually any instance ofNum), with differentMonoidinstances. - Robin ZigmondMonoidinstance forMaybe Cellalready exists as anewtypewrapper: hackage.haskell.org/package/base-4.12.0.0/docs/… - Robin ZigmondFirstsolved the problem altogether. Thanks a lot! - Jad Issa