.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleContexts #-}
module Scratch where

import Data.Monoid

class MonadPlus m where
    withMonoid :: (Monoid (m a) => r) -> r

instance MonadPlus [] where
    withMonoid k = k
                   

withMonoid' :: forall m a. MonadPlus m => (Monoid (m a) => m a) -> m a
withMonoid' x = (withMonoid :: (Monoid (m a) => m a) -> m a) x

mzero :: MonadPlus m => m a
mzero = withMonoid' mempty

mplus :: MonadPlus m => m a -> m a -> m a
mplus x y = withMonoid' (x `mappend` y)