[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / std / PrelMaybe.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[PrelMaybe]{Module @PrelMaybe@}
5
6 The @Maybe@ type.
7
8 \begin{code}
9 {-# OPTIONS -fno-implicit-prelude #-}
10
11 module PrelMaybe where
12
13 import PrelBase
14
15 data  Maybe a  =  Nothing | Just a      deriving (Eq, Ord, Show {- Read -})
16
17 maybe :: b -> (a -> b) -> Maybe a -> b
18 maybe n f Nothing  = n
19 maybe n f (Just x) = f x
20
21 instance  Functor Maybe  where
22     map f Nothing       = Nothing
23     map f (Just a)      = Just (f a)
24
25 instance  Monad Maybe  where
26     (Just x) >>= k      = k x
27     Nothing  >>= k      = Nothing
28
29     (Just x) >>  k      = k
30     Nothing  >>  k      = Nothing
31
32     return              = Just
33
34 instance  MonadZero Maybe  where
35     zero                = Nothing
36
37 instance  MonadPlus Maybe  where
38     Nothing ++ ys       = ys
39     xs      ++ ys       = xs
40 \end{code}
41
42
43
44