2 | import public Control.Function
12 | getLeft : Either a b -> Maybe a
13 | getLeft (Left a) = Just a
18 | getRight : Either a b -> Maybe b
19 | getRight (Right b) = Just b
20 | getRight _ = Nothing
24 | isLeft : Either a b -> Bool
25 | isLeft (Left _) = True
26 | isLeft (Right _) = False
30 | isRight : Either a b -> Bool
31 | isRight (Left _) = False
32 | isRight (Right _) = True
36 | data IsRight : Either a b -> Type where
37 | ItIsRight : IsRight (Right x)
40 | Uninhabited (IsRight (Left x)) where
41 | uninhabited ItIsRight
impossible
45 | fromRight : (e : Either l r) -> {auto 0 isRight : IsRight e} -> r
46 | fromRight (Right r) = r
47 | fromRight (Left _) impossible
51 | data IsLeft : Either a b -> Type where
52 | ItIsLeft : IsLeft (Left x)
55 | Uninhabited (IsLeft (Right x)) where
56 | uninhabited ItIsLeft
impossible
60 | fromLeft : (e : Either l r) -> {auto 0 isLeft : IsLeft e} -> l
61 | fromLeft (Right _) impossible
62 | fromLeft (Left l) = l
70 | compress : List (Either a b) -> List (Either (List1 a) (List1 b))
72 | compress (Left x :: abs) = compressLefts (singleton x) abs where
73 | compressLefts : List1 a -> List (Either a b) -> List (Either (List1 a) (List1 b))
74 | compressLefts acc (Left a :: abs) = compressLefts (cons a acc) abs
75 | compressLefts acc abs = Left (reverse acc) :: compress abs
76 | compress (Right y :: abs) = compressRights (singleton y) abs where
77 | compressRights : List1 b -> List (Either a b) -> List (Either (List1 a) (List1 b))
78 | compressRights acc (Right b :: abs) = compressRights (cons b acc) abs
79 | compressRights acc abs = Right (reverse acc) :: compress abs
84 | decompress : List (Either (List1 a) (List1 b)) -> List (Either a b)
85 | decompress = concatMap $
\case
86 | Left as => map Left $
forget as
87 | Right bs => map Right $
forget bs
91 | lefts : List (Either a b) -> List a
93 | lefts (Left l :: xs) = l :: lefts xs
94 | lefts (Right _ :: xs) = lefts xs
98 | rights : List (Either a b) -> List b
100 | rights (Left _ :: xs) = rights xs
101 | rights (Right r :: xs) = r :: rights xs
105 | partitionEithers : List (Either a b) -> (List a, List b)
106 | partitionEithers l = (lefts l, rights l)
110 | fromEither : Either a a -> a
111 | fromEither (Left l) = l
112 | fromEither (Right r) = r
116 | mirror : Either a b -> Either b a
117 | mirror (Left x) = Right x
118 | mirror (Right x) = Left x
121 | Zippable (Either a) where
122 | zipWith f x y = [| f x y |]
123 | zipWith3 f x y z = [| f x y z |]
125 | unzipWith f (Left e) = (Left e, Left e)
126 | unzipWith f (Right xy) = let (x, y) = f xy in (Right x, Right y)
128 | unzipWith3 f (Left e) = (Left e, Left e, Left e)
129 | unzipWith3 f (Right xyz) = let (x, y, z) = f xyz in (Right x, Right y, Right z)
135 | pushInto : c -> Either a b -> Either (c, a) (c, b)
136 | pushInto c = bimap (\ a => (c, a)) (\ b => (c, b))
146 | maybeToEither : (def : Lazy e) -> Maybe a -> Either e a
147 | maybeToEither def (Just j) = Right j
148 | maybeToEither def Nothing = Left def
152 | eitherToMaybe : Either e a -> Maybe a
153 | eitherToMaybe (Left _) = Nothing
154 | eitherToMaybe (Right x) = Just x
157 | eitherMapFusion : (f : _) -> (g : _) -> (p : _) -> (e : Either a b) -> either f g (map p e) = either f (g . p) e
158 | eitherMapFusion f g p $
Left x = Refl
159 | eitherMapFusion f g p $
Right x = Refl
162 | eitherBimapFusion : (f : _) -> (g : _) -> (p : _) -> (q : _) -> (e : _) -> either f g (bimap p q e) = either (f . p) (g . q) e
163 | eitherBimapFusion f g p q $
Left z = Refl
164 | eitherBimapFusion f g p q $
Right z = Refl
170 | Injective Left where
171 | injective Refl = Refl
175 | Injective Right where
176 | injective Refl = Refl