Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Last active January 22, 2018 19:16
Show Gist options
  • Save Icelandjack/bc09dbc0b14541f4db0953cb41440699 to your computer and use it in GitHub Desktop.
Save Icelandjack/bc09dbc0b14541f4db0953cb41440699 to your computer and use it in GitHub Desktop.
Monoid to Semigroup
demoteMonoid :: (forall m. Monoid m => m) -> Semigroup m => Maybe m
demoteMonoid k = eval k where
  eval :: MONOID m -> Semigroup m => Maybe m
  eval = getOption . foldMap (Option . Just)

data MONOID a = NIL | SING a | MONOID a :<> MONOID a
  deriving Foldable

instance Monoid (MONOID a) where
  mempty  = NIL
  mappend = (:<>)

like http://comonad.com/reader/2015/free-monoids-in-haskell/

However, one might wonder what a free monoid would look like as something closer to a traditional data type. To construct that, first ignore the required equations, and consider only the generators; we get:

data FMG a = None | Single a | FMG a :<> FMG a
@Icelandjack
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment