3f1ffad76562cb7ab74e796fff2fc264e8539f43
[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/core/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  portable
11 --
12 -- $Id: Maybe.hs,v 1.3 2001/07/03 14:13:32 simonmar Exp $
13 --
14 -- The Maybe type, and associated operations.
15 --
16 -----------------------------------------------------------------------------
17
18 module Data.Maybe
19    (
20      Maybe(Nothing,Just)-- instance of: Eq, Ord, Show, Read,
21                         --              Functor, Monad, MonadPlus
22
23    , maybe              -- :: b -> (a -> b) -> Maybe a -> b
24
25    , isJust             -- :: Maybe a -> Bool
26    , isNothing          -- :: Maybe a -> Bool
27    , fromJust           -- :: Maybe a -> a
28    , fromMaybe          -- :: a -> Maybe a -> a
29    , listToMaybe        -- :: [a] -> Maybe a
30    , maybeToList        -- :: Maybe a -> [a]
31    , catMaybes          -- :: [Maybe a] -> [a]
32    , mapMaybe           -- :: (a -> Maybe b) -> [a] -> [b]
33    ) where
34
35 #ifdef __GLASGOW_HASKELL__
36 import {-# SOURCE #-} GHC.Err ( error )
37 import GHC.Base
38 #endif
39
40 -- ---------------------------------------------------------------------------
41 -- The Maybe type, and instances
42
43 data  Maybe a  =  Nothing | Just a      deriving (Eq, Ord)
44
45 instance  Functor Maybe  where
46     fmap _ Nothing       = Nothing
47     fmap f (Just a)      = Just (f a)
48
49 instance  Monad Maybe  where
50     (Just x) >>= k      = k x
51     Nothing  >>= _      = Nothing
52
53     (Just _) >>  k      = k
54     Nothing  >>  _      = Nothing
55
56     return              = Just
57     fail _              = Nothing
58
59 -- ---------------------------------------------------------------------------
60 -- Functions over Maybe
61
62 maybe :: b -> (a -> b) -> Maybe a -> b
63 maybe n _ Nothing  = n
64 maybe _ f (Just x) = f x
65
66 isJust         :: Maybe a -> Bool
67 isJust Nothing = False
68 isJust _       = True
69
70 isNothing         :: Maybe a -> Bool
71 isNothing Nothing = True
72 isNothing _       = False
73
74 fromJust          :: Maybe a -> a
75 fromJust Nothing  = error "Maybe.fromJust: Nothing" -- yuck
76 fromJust (Just x) = x
77
78 fromMaybe     :: a -> Maybe a -> a
79 fromMaybe d x = case x of {Nothing -> d;Just v  -> v}
80
81 maybeToList            :: Maybe a -> [a]
82 maybeToList  Nothing   = []
83 maybeToList  (Just x)  = [x]
84
85 listToMaybe           :: [a] -> Maybe a
86 listToMaybe []        =  Nothing
87 listToMaybe (a:_)     =  Just a
88  
89 catMaybes              :: [Maybe a] -> [a]
90 catMaybes ls = [x | Just x <- ls]
91
92 mapMaybe          :: (a -> Maybe b) -> [a] -> [b]
93 mapMaybe _ []     = []
94 mapMaybe f (x:xs) =
95  let rs = mapMaybe f xs in
96  case f x of
97   Nothing -> rs
98   Just r  -> r:rs
99