[project @ 2002-10-09 17:08:18 by malcolm]
[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 #ifdef __NHC__
39 import Prelude
40 import Prelude (Maybe(..), maybe)
41 import Maybe
42     ( isJust
43     , isNothing
44     , fromJust
45     , fromMaybe
46     , listToMaybe
47     , maybeToList 
48     , catMaybes
49     , mapMaybe
50     )
51 #else
52
53 #ifndef __HUGS__
54 -- ---------------------------------------------------------------------------
55 -- The Maybe type, and instances
56
57 -- | The 'Maybe' type encapsulates an optional value.  A value of type
58 -- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@), 
59 -- or it is empty (represented as 'Nothing').  Using 'Maybe' is a good way to 
60 -- deal with errors or exceptional cases without resorting to drastic
61 -- measures such as 'error'.
62 --
63 -- The 'Maybe' type is also a monad.  It is a simple kind of error
64 -- monad, where all errors are represented by 'Nothing'.  A richer
65 -- error monad can be built using the 'Data.Either.Either' type.
66
67 data  Maybe a  =  Nothing | Just a      
68   deriving (Eq, Ord)
69
70 instance  Functor Maybe  where
71     fmap _ Nothing       = Nothing
72     fmap f (Just a)      = Just (f a)
73
74 instance  Monad Maybe  where
75     (Just x) >>= k      = k x
76     Nothing  >>= _      = Nothing
77
78     (Just _) >>  k      = k
79     Nothing  >>  _      = Nothing
80
81     return              = Just
82     fail _              = Nothing
83
84 -- ---------------------------------------------------------------------------
85 -- Functions over Maybe
86
87 maybe :: b -> (a -> b) -> Maybe a -> b
88 maybe n _ Nothing  = n
89 maybe _ f (Just x) = f x
90 #endif  /* __HUGS__ */
91
92 isJust         :: Maybe a -> Bool
93 isJust Nothing = False
94 isJust _       = True
95
96 isNothing         :: Maybe a -> Bool
97 isNothing Nothing = True
98 isNothing _       = False
99
100 fromJust          :: Maybe a -> a
101 fromJust Nothing  = error "Maybe.fromJust: Nothing" -- yuck
102 fromJust (Just x) = x
103
104 fromMaybe     :: a -> Maybe a -> a
105 fromMaybe d x = case x of {Nothing -> d;Just v  -> v}
106
107 maybeToList            :: Maybe a -> [a]
108 maybeToList  Nothing   = []
109 maybeToList  (Just x)  = [x]
110
111 listToMaybe           :: [a] -> Maybe a
112 listToMaybe []        =  Nothing
113 listToMaybe (a:_)     =  Just a
114  
115 catMaybes              :: [Maybe a] -> [a]
116 catMaybes ls = [x | Just x <- ls]
117
118 mapMaybe          :: (a -> Maybe b) -> [a] -> [b]
119 mapMaybe _ []     = []
120 mapMaybe f (x:xs) =
121  let rs = mapMaybe f xs in
122  case f x of
123   Nothing -> rs
124   Just r  -> r:rs
125
126 #endif /* else not __NHC__ */