16383debc95f08405288993adc621e9ca81add1a
[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 -fcompiling-prelude -fno-implicit-prelude #-}
10
11 module PrelMaybe where
12
13 import PrelBase
14 \end{code}
15
16
17 %*********************************************************
18 %*                                                      *
19 \subsection{Standard numeric classes}
20 %*                                                      *
21 %*********************************************************
22
23 \begin{code}
24 data  Maybe a  =  Nothing | Just a      deriving (Eq, Ord)
25
26 maybe :: b -> (a -> b) -> Maybe a -> b
27 maybe n _ Nothing  = n
28 maybe _ f (Just x) = f x
29
30 instance  Functor Maybe  where
31     fmap _ Nothing       = Nothing
32     fmap f (Just a)      = Just (f a)
33
34 instance  Monad Maybe  where
35     (Just x) >>= k      = k x
36     Nothing  >>= _      = Nothing
37
38     (Just _) >>  k      = k
39     Nothing  >>  _      = Nothing
40
41     return              = Just
42     fail _              = Nothing
43 \end{code}
44
45
46 %*********************************************************
47 %*                                                      *
48 \subsection{Standard numeric classes}
49 %*                                                      *
50 %*********************************************************
51
52 \begin{code}
53 data  Either a b  =  Left a | Right b   deriving (Eq, Ord )
54
55 either                  :: (a -> c) -> (b -> c) -> Either a b -> c
56 either f _ (Left x)     =  f x
57 either _ g (Right y)    =  g y
58 \end{code}
59
60
61
62