41a086b980bfa1d4d5064e6d8d8812a5176fd571
[packages/haskell2010.git] / Data / Maybe.hs
1 {-# LANGUAGE CPP, PackageImports #-}
2 #if __GLASGOW_HASKELL__ >= 701
3 {-# LANGUAGE Safe #-}
4 #endif
5
6 module Data.Maybe (
7 -- * The @Maybe@ type and operations
8
9 Maybe(Nothing,Just)-- instance of: Eq, Ord, Show, Read,
10 -- Functor, Monad, MonadPlus
11
12 , maybe -- :: b -> (a -> b) -> Maybe a -> b
13
14 , isJust -- :: Maybe a -> Bool
15 , isNothing -- :: Maybe a -> Bool
16 , fromJust -- :: Maybe a -> a
17 , fromMaybe -- :: a -> Maybe a -> a
18 , listToMaybe -- :: [a] -> Maybe a
19 , maybeToList -- :: Maybe a -> [a]
20 , catMaybes -- :: [Maybe a] -> [a]
21 , mapMaybe -- :: (a -> Maybe b) -> [a] -> [b]
22
23 -- * Specification
24
25 -- $code
26
27 ) where
28 import "base" Data.Maybe
29
30 {- $code
31 > module Data.Maybe(
32 > Maybe(Nothing, Just),
33 > isJust, isNothing,
34 > fromJust, fromMaybe, listToMaybe, maybeToList,
35 > catMaybes, mapMaybe,
36 > maybe
37 > ) where
38 >
39 > maybe :: b -> (a -> b) -> Maybe a -> b
40 > maybe n _ Nothing = n
41 > maybe _ f (Just x) = f x
42 >
43 > isJust :: Maybe a -> Bool
44 > isJust (Just a) = True
45 > isJust Nothing = False
46 >
47 > isNothing :: Maybe a -> Bool
48 > isNothing = not . isJust
49 >
50 > fromJust :: Maybe a -> a
51 > fromJust (Just a) = a
52 > fromJust Nothing = error "Maybe.fromJust: Nothing"
53 >
54 > fromMaybe :: a -> Maybe a -> a
55 > fromMaybe d Nothing = d
56 > fromMaybe d (Just a) = a
57 >
58 > maybeToList :: Maybe a -> [a]
59 > maybeToList Nothing = []
60 > maybeToList (Just a) = [a]
61 >
62 > listToMaybe :: [a] -> Maybe a
63 > listToMaybe [] = Nothing
64 > listToMaybe (a:_) = Just a
65 >
66 > catMaybes :: [Maybe a] -> [a]
67 > catMaybes ms = [ m | Just m <- ms ]
68 >
69 > mapMaybe :: (a -> Maybe b) -> [a] -> [b]
70 > mapMaybe f = catMaybes . map f
71 -}