summaryrefslogtreecommitdiff
path: root/libraries/base/Data/Maybe.hs
diff options
context:
space:
mode:
authorAustin Seipp <austin@well-typed.com>2014-04-22 06:09:40 -0500
committerAustin Seipp <austin@well-typed.com>2014-04-22 06:09:40 -0500
commit88c9403264950326e39a05f262bbbb069cf12977 (patch)
tree84821fe0ccac02131a7685eeb9224e366a638763 /libraries/base/Data/Maybe.hs
parent33e585d6eacae19e83862a05b650373b536095fa (diff)
downloadhaskell-wip/amp.tar.gz
Make Applicative a superclass of Monadwip/amp
Signed-off-by: Austin Seipp <austin@well-typed.com>
Diffstat (limited to 'libraries/base/Data/Maybe.hs')
-rw-r--r--libraries/base/Data/Maybe.hs16
1 files changed, 16 insertions, 0 deletions
diff --git a/libraries/base/Data/Maybe.hs b/libraries/base/Data/Maybe.hs
index fe2a0abc1e..991a25cb12 100644
--- a/libraries/base/Data/Maybe.hs
+++ b/libraries/base/Data/Maybe.hs
@@ -49,10 +49,26 @@ import GHC.Base
data Maybe a = Nothing | Just a
deriving (Eq, Ord)
+-- | Lift a semigroup into 'Maybe' forming a 'Monoid' according to
+-- <http://en.wikipedia.org/wiki/Monoid>: \"Any semigroup @S@ may be
+-- turned into a monoid simply by adjoining an element @e@ not in @S@
+-- and defining @e*e = e@ and @e*s = s = s*e@ for all @s ∈ S@.\" Since
+-- there is no \"Semigroup\" typeclass providing just 'mappend', we
+-- use 'Monoid' instead.
+instance Monoid a => Monoid (Maybe a) where
+ mempty = Nothing
+ Nothing `mappend` m = m
+ m `mappend` Nothing = m
+ Just m1 `mappend` Just m2 = Just (m1 `mappend` m2)
+
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just a) = Just (f a)
+instance Applicative Maybe where
+ pure = return
+ (<*>) = liftA2 id
+
instance Monad Maybe where
(Just x) >>= k = k x
Nothing >>= _ = Nothing