2 | import Control.Function
9 | isNothing : Maybe a -> Bool
10 | isNothing Nothing = True
11 | isNothing (Just _) = False
14 | isJust : Maybe a -> Bool
15 | isJust Nothing = False
16 | isJust (Just _) = True
20 | data IsJust : Maybe a -> Type where
21 | ItIsJust : IsJust (Just x)
24 | Uninhabited (IsJust Nothing) where
25 | uninhabited ItIsJust
impossible
29 | isItJust : (v : Maybe a) -> Dec (IsJust v)
30 | isItJust (Just _) = Yes ItIsJust
31 | isItJust Nothing = No absurd
36 | fromMaybe : (Lazy a) -> Maybe a -> a
37 | fromMaybe def Nothing = def
38 | fromMaybe _ (Just j) = j
42 | fromJust : (v : Maybe a) -> (0 _ : IsJust v) => a
43 | fromJust (Just x) = x
44 | fromJust Nothing impossible
49 | toMaybe : Bool -> Lazy a -> Maybe a
50 | toMaybe True j = Just j
51 | toMaybe False _ = Nothing
54 | decToMaybe : Dec a -> Maybe a
55 | decToMaybe (Yes a) = Just a
56 | decToMaybe (No _) = Nothing
59 | Injective Just where
60 | injective Refl = Refl
65 | lowerMaybe : Monoid a => Maybe a -> a
66 | lowerMaybe Nothing = neutral
67 | lowerMaybe (Just x) = x
71 | raiseToMaybe : Monoid a => Eq a => a -> Maybe a
72 | raiseToMaybe x = if x == neutral then Nothing else Just x
75 | filter : (a -> Bool) -> Maybe a -> Maybe a
76 | filter _ Nothing = Nothing
77 | filter f (Just x) = toMaybe (f x) x
82 | [Deep] Semigroup a => Semigroup (Maybe a) where
83 | Nothing <+> Nothing = Nothing
84 | Just l <+> Nothing = Just l
85 | Nothing <+> Just r = Just r
86 | Just l <+> Just r = Just $
l <+> r
91 | [Deep] Semigroup a => Monoid (Maybe a) using Semigroup.Deep where
95 | Zippable Maybe where
96 | zipWith f x y = [| f x y |]
97 | zipWith3 f x y z = [| f x y z |]
99 | unzipWith f Nothing = (Nothing, Nothing)
100 | unzipWith f (Just x) = let (a, b) = f x in (Just a, Just b)
102 | unzipWith3 f Nothing = (Nothing, Nothing, Nothing)
103 | unzipWith3 f (Just x) = let (a, b, c) = f x in (Just a, Just b, Just c)
111 | maybeCong : (f : a -> b) -> Maybe (x = y) -> Maybe (f x = f y)
112 | maybeCong f = map (\eq => cong f eq)
116 | maybeCong2 : (f : a -> b -> c) -> Maybe (x = y) -> Maybe (v = w) -> Maybe (f x v = f y w)
117 | maybeCong2 f mxy mvw = (\xy, vw => cong2 f xy vw) <$> mxy <*> mvw