[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / std / Maybe.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4 \section[Maybe]{Module @Maybe@}
5
6 The standard Haskell 1.3 library for working with
7 @Maybe@ values.
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module Maybe
13    (
14     Maybe(..),          -- non-standard
15                         -- instance of: Eq, Ord, Show, Read,
16                         --              Functor, Monad, MonadZero, MonadPlus
17
18     maybe,              -- :: b -> (a -> b) -> Maybe a -> b
19
20     isJust,             -- :: Maybe a -> Bool
21     fromJust,           -- :: Maybe a -> a
22     fromMaybe,          -- :: a -> Maybe a -> a
23     listToMaybe,        -- :: [a] -> Maybe a
24     maybeToList,        -- :: Maybe a -> [a]
25     catMaybes,          -- :: [Maybe a] -> [a]
26     mapMaybe,           -- :: (a -> Maybe b) -> [a] -> [b]
27     unfoldr             -- :: (a -> Maybe (b,a)) -> a -> (a,[b])
28
29    ) where
30
31 import PrelErr  ( error )
32 import Monad    ( filter )
33 import PrelList
34 import PrelMaybe
35 import PrelBase
36 \end{code}
37
38
39 %*********************************************************
40 %*                                                      *
41 \subsection{Functions}
42 %*                                                      *
43 %*********************************************************
44
45 \begin{code}
46 isJust         :: Maybe a -> Bool
47 isJust Nothing = False
48 isJust _       = True
49
50 fromJust          :: Maybe a -> a
51 fromJust Nothing  = error "Maybe.fromJust: Nothing" -- yuck
52 fromJust (Just x) = x
53
54 fromMaybe     :: a -> Maybe a -> a
55 fromMaybe d x = case x of {Nothing -> d;Just v  -> v}
56
57 maybeToList            :: Maybe a -> [a]
58 maybeToList  Nothing   = []
59 maybeToList  (Just x)  = [x]
60
61 listToMaybe           :: [a] -> Maybe a
62 listToMaybe []        =  Nothing
63 listToMaybe (a:_)     =  Just a
64  
65 {- OLD, NOT EXPORTED:
66 findMaybe              :: (a -> Bool) -> [a] -> Maybe a
67 findMaybe p            =  listToMaybe . filter p
68 -}
69
70 catMaybes              :: [Maybe a] -> [a]
71 catMaybes ls = [x | Just x <- ls]
72
73 mapMaybe          :: (a -> Maybe b) -> [a] -> [b]
74 mapMaybe f []     = []
75 mapMaybe f (x:xs) =
76  let rs = mapMaybe f xs in
77  case f x of
78   Nothing -> rs
79   Just r  -> r:rs
80
81 {- OLD, NOT EXPORTED:
82 joinMaybe         :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a 
83 joinMaybe f m1 m2 =
84  case m1 of
85   Nothing -> m2
86   Just v1 -> case m2 of {Nothing -> m1; Just v2 -> Just (f v1 v2)}
87 -}
88
89 \end{code}
90
91 \begin{verbatim}
92   unfoldr f' (foldr f z xs) == (z,xs)
93
94  if the following holds:
95
96    f' (f x y) = Just (x,y)
97    f' z       = Nothing
98 \end{verbatim}
99
100 \begin{code}
101 unfoldr       :: (a -> Maybe (b, a)) -> a -> (a,[b])
102 unfoldr f x   =
103   case f x of
104    Just (y,x') -> let (x'',ys) = unfoldr f x' in (x'',y:ys)
105    Nothing     -> (x,[])
106 \end{code}