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