|
1 | 1 | module Data.Maybe where |
2 | 2 |
|
3 | | - data Maybe a = Nothing | Just a |
4 | | - |
5 | | - maybe :: forall a b. b -> (a -> b) -> Maybe a -> b |
6 | | - maybe b _ Nothing = b |
7 | | - maybe _ f (Just a) = f a |
8 | | - |
9 | | - fromMaybe :: forall a. a -> Maybe a -> a |
10 | | - fromMaybe a = maybe a (id :: forall a. a -> a) |
11 | | - |
12 | | - isJust :: forall a. Maybe a -> Boolean |
13 | | - isJust = maybe false (const true) |
14 | | - |
15 | | - isNothing :: forall a. Maybe a -> Boolean |
16 | | - isNothing = maybe true (const false) |
17 | | - |
18 | | - instance functorMaybe :: Functor Maybe where |
19 | | - (<$>) fn (Just x) = Just (fn x) |
20 | | - (<$>) _ _ = Nothing |
21 | | - |
22 | | - instance applyMaybe :: Apply Maybe where |
23 | | - (<*>) (Just fn) x = fn <$> x |
24 | | - (<*>) Nothing _ = Nothing |
25 | | - |
26 | | - instance applicativeMaybe :: Applicative Maybe where |
27 | | - pure = Just |
28 | | - |
29 | | - instance alternativeMaybe :: Alternative Maybe where |
30 | | - empty = Nothing |
31 | | - (<|>) Nothing r = r |
32 | | - (<|>) l _ = l |
33 | | - |
34 | | - instance bindMaybe :: Bind Maybe where |
35 | | - (>>=) (Just x) k = k x |
36 | | - (>>=) Nothing _ = Nothing |
37 | | - |
38 | | - instance monadMaybe :: Monad Maybe |
39 | | - |
40 | | - instance showMaybe :: (Show a) => Show (Maybe a) where |
41 | | - show (Just x) = "Just (" ++ show x ++ ")" |
42 | | - show Nothing = "Nothing" |
43 | | - |
44 | | - instance eqMaybe :: (Eq a) => Eq (Maybe a) where |
45 | | - (==) Nothing Nothing = true |
46 | | - (==) (Just a1) (Just a2) = a1 == a2 |
47 | | - (==) _ _ = false |
48 | | - (/=) a b = not (a == b) |
49 | | - |
50 | | - instance ordMaybe :: (Ord a) => Ord (Maybe a) where |
51 | | - compare (Just x) (Just y) = compare x y |
52 | | - compare Nothing Nothing = EQ |
53 | | - compare Nothing _ = LT |
54 | | - compare _ Nothing = GT |
| 3 | +import Control.Alt |
| 4 | +import Control.Plus |
| 5 | +import Control.Alternative |
| 6 | +import Control.MonadPlus |
| 7 | + |
| 8 | +data Maybe a = Nothing | Just a |
| 9 | + |
| 10 | +maybe :: forall a b. b -> (a -> b) -> Maybe a -> b |
| 11 | +maybe b _ Nothing = b |
| 12 | +maybe _ f (Just a) = f a |
| 13 | + |
| 14 | +fromMaybe :: forall a. a -> Maybe a -> a |
| 15 | +fromMaybe a = maybe a (id :: forall a. a -> a) |
| 16 | + |
| 17 | +isJust :: forall a. Maybe a -> Boolean |
| 18 | +isJust = maybe false (const true) |
| 19 | + |
| 20 | +isNothing :: forall a. Maybe a -> Boolean |
| 21 | +isNothing = maybe true (const false) |
| 22 | + |
| 23 | +instance functorMaybe :: Functor Maybe where |
| 24 | + (<$>) fn (Just x) = Just (fn x) |
| 25 | + (<$>) _ _ = Nothing |
| 26 | + |
| 27 | +instance applyMaybe :: Apply Maybe where |
| 28 | + (<*>) (Just fn) x = fn <$> x |
| 29 | + (<*>) Nothing _ = Nothing |
| 30 | + |
| 31 | +instance applicativeMaybe :: Applicative Maybe where |
| 32 | + pure = Just |
| 33 | + |
| 34 | +instance altMaybe :: Alt Maybe where |
| 35 | + (<|>) Nothing r = r |
| 36 | + (<|>) l _ = l |
| 37 | + |
| 38 | +instance plusMaybe :: Plus Maybe where |
| 39 | + empty = Nothing |
| 40 | + |
| 41 | +instance alternativeMaybe :: Alternative Maybe |
| 42 | + |
| 43 | +instance bindMaybe :: Bind Maybe where |
| 44 | + (>>=) (Just x) k = k x |
| 45 | + (>>=) Nothing _ = Nothing |
| 46 | + |
| 47 | +instance monadMaybe :: Monad Maybe |
| 48 | + |
| 49 | +instance monadPlusMaybe :: MonadPlus Maybe |
| 50 | + |
| 51 | +instance showMaybe :: (Show a) => Show (Maybe a) where |
| 52 | + show (Just x) = "Just (" ++ show x ++ ")" |
| 53 | + show Nothing = "Nothing" |
| 54 | + |
| 55 | +instance eqMaybe :: (Eq a) => Eq (Maybe a) where |
| 56 | + (==) Nothing Nothing = true |
| 57 | + (==) (Just a1) (Just a2) = a1 == a2 |
| 58 | + (==) _ _ = false |
| 59 | + (/=) a b = not (a == b) |
| 60 | + |
| 61 | +instance ordMaybe :: (Ord a) => Ord (Maybe a) where |
| 62 | + compare (Just x) (Just y) = compare x y |
| 63 | + compare Nothing Nothing = EQ |
| 64 | + compare Nothing _ = LT |
| 65 | + compare _ Nothing = GT |
0 commit comments