2 | import Control.Function
9 | data These a b = This a | That b | Both a b
12 | fromEither : Either a b -> These a b
13 | fromEither = either This That
16 | fromThis : These a b -> Maybe a
17 | fromThis (This a) = Just a
18 | fromThis (That _) = Nothing
19 | fromThis (Both a _) = Just a
22 | fromThat : These a b -> Maybe b
23 | fromThat (This _) = Nothing
24 | fromThat (That b) = Just b
25 | fromThat (Both _ b) = Just b
28 | fromBoth : (defaultL : Lazy a) -> (defaultR : Lazy b) -> These a b -> (a, b)
29 | fromBoth _ y (This x) = (x, y)
30 | fromBoth x _ (That y) = (x, y)
31 | fromBoth _ _ (Both x y) = (x, y)
34 | these : (a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c
35 | these l r lr (This a) = l a
36 | these l r lr (That b) = r b
37 | these l r lr (Both a b) = lr a b
40 | these' : (defualtL : Lazy a) -> (defaultR : Lazy b) -> (a -> b -> c) -> These a b -> c
41 | these' _ y f (This x) = f x y
42 | these' x _ f (That y) = f x y
43 | these' _ _ f (Both x y) = f x y
46 | swap : These a b -> These b a
47 | swap (This a) = That a
48 | swap (That b) = This b
49 | swap (Both a b) = Both b a
52 | Injective This where
53 | injective Refl = Refl
56 | Injective That where
57 | injective Refl = Refl
60 | {x : _} -> Injective (Both x) where
61 | injective Refl = Refl
64 | {y : _} -> Injective (`Both` y) where
65 | injective Refl = Refl
68 | Biinjective Both where
69 | biinjective Refl = (Refl, Refl)
72 | (Show a, Show b) => Show (These a b) where
73 | showPrec d (This x) = showCon d "This" $
showArg x
74 | showPrec d (That x) = showCon d "That" $
showArg x
75 | showPrec d (Both x y) = showCon d "Both" $
showArg x ++ showArg y
78 | Eq a => Eq b => Eq (These a b) where
79 | This x == This x' = x == x'
80 | That x == That x' = x == x'
81 | Both x y == Both x' y' = x == x' && y == y'
85 | Semigroup a => Semigroup b => Semigroup (These a b) where
86 | This x <+> This x' = This $
x <+> x'
87 | This x <+> That y = Both x y
88 | This x <+> Both x' y = Both (x <+> x') y
90 | That y <+> This x = Both x y
91 | That y <+> That y' = That $
y <+> y'
92 | That y <+> Both x y' = Both x $
y <+> y'
94 | Both x y <+> This x' = Both (x <+> x') y
95 | Both x y <+> That y' = Both x (y <+> y')
96 | Both x y <+> Both x' y' = Both (x <+> x') (y <+> y')
100 | Bifunctor These where
101 | bimap f g (This a) = This (f a)
102 | bimap f g (That b) = That (g b)
103 | bimap f g (Both a b) = Both (f a) (g b)
107 | Bifoldable These where
108 | bifoldr f _ acc (This a) = f a acc
109 | bifoldr _ g acc (That b) = g b acc
110 | bifoldr f g acc (Both a b) = f a (g b acc)
112 | bifoldl f _ acc (This a) = f acc a
113 | bifoldl _ g acc (That b) = g acc b
114 | bifoldl f g acc (Both a b) = g (f acc a) b
120 | Bitraversable These where
121 | bitraverse f _ (This a) = This <$> f a
122 | bitraverse _ g (That b) = That <$> g b
123 | bitraverse f g (Both a b) = [| Both (f a) (g b) |]
127 | Functor (These a) where
131 | bifold : Semigroup m => These m m -> m
132 | bifold (This a) = a
133 | bifold (That b) = b
134 | bifold (Both a b) = a <+> b
140 | Semigroup a => Applicative (These a) where
143 | This e <*> That _ = This e
144 | This e <*> This e' = This $
e <+> e'
145 | This e <*> Both e' _ = This $
e <+> e'
147 | That f <*> That x = That $
f x
148 | That f <*> This e = This e
149 | That f <*> Both e x = Both e $
f x
151 | Both e _ <*> This e' = This $
e <+> e'
152 | Both e f <*> That x = Both e $
f x
153 | Both e f <*> Both e' x = Both (e <+> e') $
f x
156 | Foldable (These a) where
157 | foldr _ init $
This _ = init
158 | foldr op init $
That x = x `op` init
159 | foldr op init $
Both _ x = x `op` init
161 | foldl _ init $
This _ = init
162 | foldl op init $
That x = init `op` x
163 | foldl op init $
Both _ x = init `op` x
165 | null $
This _ = True
166 | null $
That _ = False
167 | null $
Both _ _ = False
170 | Traversable (These a) where
171 | traverse _ $
This e = pure $
This e
172 | traverse f $
That x = That <$> f x
173 | traverse f $
Both x y = Both x <$> f y
176 | Semigroup a => Zippable (These a) where
177 | zipWith f x y = [| f x y |]
178 | zipWith3 f x y z = [| f x y z |]
180 | unzipWith f (This x) = (This x, This x)
181 | unzipWith f (That x) = let (u, v) = f x in (That u, That v)
182 | unzipWith f (Both x y) = let (u, v) = f y in (Both x u, Both x v)
184 | unzipWith3 f (This x) = (This x, This x, This x)
185 | unzipWith3 f (That x) = let (u, v, w) = f x in (That u, That v, That w)
186 | unzipWith3 f (Both x y) = let (u, v, w) = f y in (Both x u, Both x v, Both x w)