[project @ 2002-02-12 11:44:54 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelMaybe.lhs
1 % ------------------------------------------------------------------------------
2 % $Id: PrelMaybe.lhs,v 1.6 2000/06/30 13:39:36 simonmar Exp $
3 %
4 % (c) The University of Glasgow, 1992-2000
5 %
6
7 \section[PrelMaybe]{Module @PrelMaybe@}
8
9 The @Maybe@ type.
10
11 \begin{code}
12 {-# OPTIONS -fno-implicit-prelude #-}
13
14 module PrelMaybe where
15
16 import PrelBase
17 \end{code}
18
19
20 %*********************************************************
21 %*                                                      *
22 \subsection{Standard numeric classes}
23 %*                                                      *
24 %*********************************************************
25
26 \begin{code}
27 data  Maybe a  =  Nothing | Just a      deriving (Eq, Ord)
28
29 maybe :: b -> (a -> b) -> Maybe a -> b
30 maybe n _ Nothing  = n
31 maybe _ f (Just x) = f x
32
33 instance  Functor Maybe  where
34     fmap _ Nothing       = Nothing
35     fmap f (Just a)      = Just (f a)
36
37 instance  Monad Maybe  where
38     (Just x) >>= k      = k x
39     Nothing  >>= _      = Nothing
40
41     (Just _) >>  k      = k
42     Nothing  >>  _      = Nothing
43
44     return              = Just
45     fail _              = Nothing
46 \end{code}
47
48
49 %*********************************************************
50 %*                                                      *
51 \subsection{Standard numeric classes}
52 %*                                                      *
53 %*********************************************************
54
55 \begin{code}
56 data  Either a b  =  Left a | Right b   deriving (Eq, Ord )
57
58 either                  :: (a -> c) -> (b -> c) -> Either a b -> c
59 either f _ (Left x)     =  f x
60 either _ g (Right y)    =  g y
61 \end{code}
62
63
64
65