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