[project @ 2002-07-04 16:22:02 by simonmar]
[ghc-base.git] / Data / Maybe.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Data.Maybe
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  portable
11 --
12 -- The Maybe type, and associated operations.
13 --
14 -----------------------------------------------------------------------------
15
16 module Data.Maybe
17    (
18      Maybe(Nothing,Just)-- instance of: Eq, Ord, Show, Read,
19                         --              Functor, Monad, MonadPlus
20
21    , maybe              -- :: b -> (a -> b) -> Maybe a -> b
22
23    , isJust             -- :: Maybe a -> Bool
24    , isNothing          -- :: Maybe a -> Bool
25    , fromJust           -- :: Maybe a -> a
26    , fromMaybe          -- :: a -> Maybe a -> a
27    , listToMaybe        -- :: [a] -> Maybe a
28    , maybeToList        -- :: Maybe a -> [a]
29    , catMaybes          -- :: [Maybe a] -> [a]
30    , mapMaybe           -- :: (a -> Maybe b) -> [a] -> [b]
31    ) where
32
33 #ifdef __GLASGOW_HASKELL__
34 import {-# SOURCE #-} GHC.Err ( error )
35 import GHC.Base
36 #endif
37
38 -- ---------------------------------------------------------------------------
39 -- The Maybe type, and instances
40
41 -- | The 'Maybe' type encapsulates an optional value.  A value of type
42 -- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@), 
43 -- or it is empty (represented as 'Nothing').  Using 'Maybe' is a good way to 
44 -- deal with errors or exceptional cases without resorting to drastic
45 -- measures such as 'error'.
46 --
47 -- The 'Maybe' type is also a monad.  It is a simple kind of error
48 -- monad, where all errors are represented by 'Nothing'.  A richer
49 -- error monad can be built using the 'Data.Either.Either' type.
50
51 data  Maybe a  =  Nothing | Just a      
52   deriving (Eq, Ord)
53
54 instance  Functor Maybe  where
55     fmap _ Nothing       = Nothing
56     fmap f (Just a)      = Just (f a)
57
58 instance  Monad Maybe  where
59     (Just x) >>= k      = k x
60     Nothing  >>= _      = Nothing
61
62     (Just _) >>  k      = k
63     Nothing  >>  _      = Nothing
64
65     return              = Just
66     fail _              = Nothing
67
68 -- ---------------------------------------------------------------------------
69 -- Functions over Maybe
70
71 maybe :: b -> (a -> b) -> Maybe a -> b
72 maybe n _ Nothing  = n
73 maybe _ f (Just x) = f x
74
75 isJust         :: Maybe a -> Bool
76 isJust Nothing = False
77 isJust _       = True
78
79 isNothing         :: Maybe a -> Bool
80 isNothing Nothing = True
81 isNothing _       = False
82
83 fromJust          :: Maybe a -> a
84 fromJust Nothing  = error "Maybe.fromJust: Nothing" -- yuck
85 fromJust (Just x) = x
86
87 fromMaybe     :: a -> Maybe a -> a
88 fromMaybe d x = case x of {Nothing -> d;Just v  -> v}
89
90 maybeToList            :: Maybe a -> [a]
91 maybeToList  Nothing   = []
92 maybeToList  (Just x)  = [x]
93
94 listToMaybe           :: [a] -> Maybe a
95 listToMaybe []        =  Nothing
96 listToMaybe (a:_)     =  Just a
97  
98 catMaybes              :: [Maybe a] -> [a]
99 catMaybes ls = [x | Just x <- ls]
100
101 mapMaybe          :: (a -> Maybe b) -> [a] -> [b]
102 mapMaybe _ []     = []
103 mapMaybe f (x:xs) =
104  let rs = mapMaybe f xs in
105  case f x of
106   Nothing -> rs
107   Just r  -> r:rs
108