[project @ 1999-01-14 18:12:47 by sof]
[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 _ Nothing  = n
19 maybe _ f (Just x) = f x
20
21 instance  Functor Maybe  where
22     fmap _ Nothing       = Nothing
23     fmap f (Just a)      = Just (f a)
24
25 instance  Monad Maybe  where
26     (Just x) >>= k      = k x
27     Nothing  >>= _      = Nothing
28
29     (Just _) >>  k      = k
30     Nothing  >>  _      = Nothing
31
32     return              = Just
33     fail _              = Nothing
34
35 \end{code}
36
37
38
39