-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSemialign.hs
More file actions
407 lines (341 loc) · 14.5 KB
/
Semialign.hs
File metadata and controls
407 lines (341 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- Const instances
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Tests.Semialign (alignProps, semialignLaws) where
import Prelude ()
import Prelude.Compat hiding (repeat, unzip, zip, zipWith)
-- import qualified Prelude.Compat as Prelude
import Control.Applicative (Const (..), ZipList (..))
import Control.Monad (join)
import Control.Monad.Trans.Instances ()
import Data.Bifunctor (bimap)
import Data.Bifunctor.Assoc (assoc)
import Data.Bifunctor.Swap (swap)
import Data.Foldable (toList)
import Data.Functor.Compose (Compose (..))
import Data.Functor.Identity (Identity (..))
import Data.Functor.Product (Product (..))
import Data.HashMap.Strict (HashMap)
import Data.IntMap (IntMap)
import Data.List.NonEmpty (NonEmpty)
import Data.Map (Map)
import Data.Maybe (mapMaybe)
import Data.Proxy (Proxy)
import Data.Sequence (Seq)
import Data.Tagged (Tagged)
import Test.QuickCheck
(Arbitrary (..), Property, counterexample, (.&&.), (===))
import Test.QuickCheck.Function (Fun (..))
import Test.QuickCheck.Instances ()
import Test.QuickCheck.Poly (A, B, C)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)
#ifdef MIN_VERSION_lattice
import Algebra.Lattice
(BoundedJoinSemiLattice (..), BoundedMeetSemiLattice (..), Lattice (..))
import Algebra.Lattice.M2 (M2)
#endif
import qualified Data.Tree as T
import qualified Data.Vector as V
import Data.Semialign
import Data.These
import Data.These.Combinators
#ifdef MIN_VERSION_lens
import Control.Lens (folded, toListOf)
import Data.These.Lens
#endif
import Tests.Orphans ()
#if MIN_VERSION_base(4,7,0)
#define Typeable1 Typeable
import Data.Typeable (Typeable, typeOf1)
#else
import Data.Typeable (Typeable1, typeOf1)
#endif
-------------------------------------------------------------------------------
-- Props
-------------------------------------------------------------------------------
alignProps :: TestTree
alignProps = testGroup "Align"
[ semialignLaws (CAll :: CSemialign [])
, semialignLaws (CUnalign :: CSemialign (HashMap String))
, semialignLaws (CUnalign :: CSemialign (Map Char))
, semialignLaws (CUnalign :: CSemialign IntMap)
, semialignLaws (CUnAll :: CSemialign Maybe)
, semialignLaws (CAll :: CSemialign (Product [] Maybe))
, semialignLaws (CUnAll :: CSemialign (Product Maybe Maybe))
, semialignLaws (CAll :: CSemialign (Compose [] Maybe))
, semialignLaws (CAlign :: CSemialign Seq)
, semialignLaws (CAlign :: CSemialign V.Vector)
, semialignLaws (CAlign :: CSemialign ZipList)
, semialignLaws (CZip :: CSemialign T.Tree)
, semialignLaws (CZip :: CSemialign NonEmpty)
, semialignLaws (CZip :: CSemialign Identity)
, semialignLaws (CUnAll :: CSemialign Proxy)
, semialignLaws (CZip :: CSemialign (Tagged Char))
#ifdef MIN_VERSION_lattice
-- note: with e.g. N5 (which isn't distributive lattice) distributivity laws fail!
, semialignLaws (CZip :: CSemialign (Const M2))
, semialignLaws (CAlign :: CSemialign (ReaderT Int []))
#endif
]
-------------------------------------------------------------------------------
-- Const
-------------------------------------------------------------------------------
#ifdef MIN_VERSION_lattice
instance Lattice a => Semialign (Const a) where
alignWith _ (Const a) (Const b) = Const (a \/ b)
-- This is valid when @a@ is distributive lattice
-- otherwise distributivity laws don't hold.
instance Lattice a => Zip (Const a) where
zipWith _ (Const a) (Const b) = Const (a /\ b)
-- Note: idempotency of lattice makes this valid!
instance Lattice a => Unzip (Const a) where
unzip (Const a) = (Const a, Const a)
instance BoundedJoinSemiLattice a => Align (Const a) where
nil = Const bottom
instance BoundedMeetSemiLattice a => Repeat (Const a) where
repeat _ = Const top
#endif
-------------------------------------------------------------------------------
-- Align laws
-------------------------------------------------------------------------------
data CSemialign f where
-- CSemialign :: Semialign f => CSemialign f
CAlign :: Align f => CSemialign f
CUnalign :: (Align f, Unalign f) => CSemialign f
CZip :: Repeat f => CSemialign f
CAll :: (Align f, Repeat f) => CSemialign f
CUnAll :: (Align f, Repeat f, Unalign f) => CSemialign f
semialignLaws
:: forall (f :: * -> *).
( Semialign f, Unzip f, Foldable f, Typeable1 f
, Eq (f A), Show (f A), Arbitrary (f A)
, Eq (f B), Show (f B), Arbitrary (f B)
, Eq (f C), Show (f C), Arbitrary (f C)
, Eq (f (A, (B, C))), Show (f (A, (B, C)))
, Eq (f (A, A)), Show (f (A, A))
, Eq (f (A, B)), Show (f (A, B)), Arbitrary (f (A, B))
, Eq (f (C, Int)), Show (f (C, Int))
, Eq (f (These (A, B) C)), Show (f (These (A, B) C))
, Eq (f (These (A, C) (B, C))), Show (f (These (A, C) (B, C)))
, Eq (f (These A (A, B))), Show (f (These A (A, B)))
, Eq (f (These A (These B C))), Show (f (These A (These B C)))
, Eq (f (These A A)), Show (f (These A A))
, Eq (f (These A B)), Show (f (These A B)), Arbitrary (f (These A B))
, Eq (f (These C Int)), Show (f (These C Int))
)
=> CSemialign f
-> TestTree
semialignLaws p = testGroup name $ case p of
-- CSemialign -> semialignLaws'
CAlign -> [semialignLaws' p, unzipLaws' p, alignLaws' p]
CUnalign -> [semialignLaws' p, unzipLaws' p, unalignLaws' p, alignLaws' p]
CZip -> [semialignLaws' p, unzipLaws' p, zipLaws' p]
CAll -> [semialignLaws' p, unzipLaws' p, alignLaws' p, zipLaws' p]
CUnAll -> [semialignLaws' p, unzipLaws' p, unalignLaws' p, alignLaws' p, zipLaws' p]
where
name = show (typeOf1 (undefined :: f ()))
{-# NOINLINE semialignLaws' #-}
{-# NOINLINE unzipLaws' #-}
{-# NOINLINE alignLaws' #-}
{-# NOINLINE zipLaws' #-}
{-# NOINLINE unalignLaws' #-}
semialignLaws'
:: forall f proxy. (Zip f, Foldable f
, Eq (f A), Show (f A), Arbitrary (f A)
, Eq (f B), Show (f B), Arbitrary (f B)
, Eq (f C), Show (f C), Arbitrary (f C)
, Eq (f (A, A)), Show (f (A, A))
, Eq (f (These A A)), Show (f (These A A))
, Eq (f (These C Int)), Show (f (These C Int))
, Eq (f (C, Int)), Show (f (C, Int))
, Eq (f (These A B)), Show (f (These A B))
, Eq (f (These A (These B C))), Show (f (These A (These B C)))
, Eq (f (A, B)), Show (f (A, B)), Arbitrary (f (A, B))
, Eq (f (A, (B, C))), Show (f (A, (B, C)))
, Eq (f (These A (A, B))), Show (f (These A (A, B)))
, Eq (f (These (A, B) C)), Show (f (These (A, B) C))
, Eq (f (These (A, C) (B, C))), Show (f (These (A, C) (B, C)))
)
=> proxy f -> TestTree
semialignLaws' _ = testGroup "Semialign"
[ testProperty "idempotency align" idempAlign
, testProperty "idempotency zip" idempZip
, testProperty "commutativity align" swapProp
, testProperty "commutativity zip" zipSwapProp
, testProperty "associativity align" assocProp
, testProperty "associativity zip" zipAssocProp
, testProperty "absoption 1" absorb1Prop
, testProperty "absoption 2" absorb2Prop
, testProperty "alignWith" alignWithProp
, testProperty "zipWith" zipWithProp
, testProperty "functoriality align" bimapAlignProp
, testProperty "functoriality zip" bimapZipProp
, testProperty "fst-zip" fstZipProp
, testProperty "snd-zip" sndZipProp
, testProperty "zip-fst-snd" zipFstSndProp
#ifdef MIN_VERSION_lens
, testProperty "alignToList" alignToListProp
#endif
, testProperty "distributivity 1" distr1'Prop
, testProperty "distributivity 2" distr2Prop
, testProperty "distributivity 3'" distr2'Prop
-- testProperty "distributivity 4" distr1Prop
]
where
idempAlign :: f A -> Property
idempAlign xs = join align xs === fmap (join These) xs
idempZip :: f A -> Property
idempZip xs = join zip xs === fmap (join (,)) xs
bimapAlignProp :: f A -> f B -> Fun A C -> Fun B Int -> Property
bimapAlignProp xs ys (Fun _ f) (Fun _ g) =
align (f <$> xs) (g <$> ys) === (bimap f g <$> align xs ys)
bimapZipProp :: f A -> f B -> Fun A C -> Fun B Int -> Property
bimapZipProp xs ys (Fun _ f) (Fun _ g) =
zip (f <$> xs) (g <$> ys) === (bimap f g <$> zip xs ys)
alignWithProp :: f A -> f B -> Fun (These A B) C -> Property
alignWithProp xs ys (Fun _ f) =
alignWith f xs ys === (f <$> align xs ys)
zipWithProp :: f A -> f B -> Fun (A, B) C -> Property
zipWithProp xs ys (Fun _ f) =
zipWith (curry f) xs ys === (f <$> zip xs ys)
swapProp :: f A -> f B -> Property
swapProp xs ys = align xs ys === fmap swap (align ys xs)
assocProp :: f A -> f B -> f C -> Property
assocProp xs ys zs = lhs === fmap assocThese rhs
where
rhs = (xs `align` ys) `align` zs
lhs = xs `align` (ys `align` zs)
#ifdef MIN_VERSION_lens
alignToListProp :: f A -> f B -> Property
alignToListProp xs ys =
toList xs === toListOf (folded . here) xys
.&&.
toList xs === mapMaybe justHere (toList xys)
.&&.
toList ys === toListOf (folded . there) xys
where
xys = align xs ys
#endif
fstZipProp :: f A -> Property
fstZipProp xs = fmap fst (zip xs xs) === xs
sndZipProp :: f A -> Property
sndZipProp xs = fmap fst (zip xs xs) === xs
zipFstSndProp :: f (A, B) -> Property
zipFstSndProp xs = zip (fmap fst xs) (fmap snd xs) === xs
zipSwapProp :: f A -> f B -> Property
zipSwapProp xs ys = zip xs ys === fmap swap (zip ys xs)
zipAssocProp :: f A -> f B -> f C -> Property
zipAssocProp xs ys zs = lhs === fmap assoc rhs
where
rhs = (xs `zip` ys) `zip` zs
lhs = xs `zip` (ys `zip` zs)
absorb1Prop :: f A -> f B -> Property
absorb1Prop xs ys = fmap fst (zip xs (align xs ys)) === xs
absorb2Prop :: f A -> f B -> Property
absorb2Prop xs ys = lhs === rhs where
lhs = fmap toThis (align xs (zip xs ys))
rhs = fmap This xs
toThis (This a) = This a
toThis (These a _) = This a
toThis (That b) = That b
-- distr1Prop :: f A -> f B -> f C -> Property
-- distr1Prop xs ys zs = lhs === rhs where
-- lhs = distrThesePair <$> align (zip xs ys) zs
-- rhs = zip (align xs zs) (align ys zs)
distr1'Prop :: f A -> f B -> f C -> Property
distr1'Prop xs ys zs = lhs === rhs where
lhs = align (zip xs ys) zs
rhs = undistrThesePair <$> zip (align xs zs) (align ys zs)
distr2Prop :: f A -> f B -> f C -> Property
distr2Prop xs ys zs = lhs === rhs where
lhs = distrPairThese <$> zip (align xs ys) zs
rhs = align (zip xs zs) (zip ys zs)
distr2'Prop :: f A -> f B -> f C -> Property
distr2'Prop xs ys zs = lhs === rhs where
lhs = distrPairThese <$> zip (align xs ys) zs
rhs = align (zip xs zs) (zip ys zs)
alignLaws'
:: forall f proxy. (Align f
, Eq (f A), Show (f A), Arbitrary (f A)
, Eq (f B), Show (f B), Arbitrary (f B)
, Eq (f (These A B)), Show (f (These A B))
)
=> proxy f -> TestTree
alignLaws' _ = testGroup "Align"
[ testProperty "right identity" rightIdentityProp
, testProperty "left identity" leftIdentityProp
]
where
rightIdentityProp :: f A -> Property
rightIdentityProp xs = (xs `align` (nil :: f B)) === fmap This xs
leftIdentityProp :: Align f => f B -> Property
leftIdentityProp xs = ((nil :: f A) `align` xs) === fmap That xs
zipLaws'
:: forall f proxy. (Repeat f
, Eq (f A), Show (f A), Arbitrary (f A)
)
=> proxy f -> TestTree
zipLaws' _ = testGroup "Zip"
[ testProperty "right identity" zipRightIdentityProp
, testProperty "left identity" zipLeftIdentityProp
]
where
zipRightIdentityProp :: f A -> B -> Property
zipRightIdentityProp xs y = (fst <$> zip xs (repeat y)) === xs
zipLeftIdentityProp :: B -> f A -> Property
zipLeftIdentityProp x ys = (snd <$> zip (repeat x) ys) === ys
unalignLaws'
:: forall f proxy. (Unalign f
, Eq (f A), Show (f A), Arbitrary (f A)
, Eq (f B), Show (f B), Arbitrary (f B)
, Eq (f C), Show (f C), Arbitrary (f C)
, Eq (f (These A B)), Show (f (These A B)), Arbitrary (f (These A B))
)
=> proxy f -> TestTree
unalignLaws' _ = testGroup "Unalign"
[ testProperty "right inverse" invProp
, testProperty "left inverse" leftProp
, testProperty "unalignWith via unalign" unalignWithProp
, testProperty "unalign via unalignWith" unalignProp
]
where
unalignWithProp :: f A -> Fun A (These B C) -> Property
unalignWithProp xs (Fun _ f) = unalignWith f xs === unalign (f <$> xs)
unalignProp :: f (These A B) -> Property
unalignProp xs = unalign xs === unalignWith id xs
invProp :: f (These A B) -> Property
invProp xs = uncurry align (unalign xs) === xs
leftProp :: f A -> f B -> Property
leftProp xs ys = counterexample (show xys) $ unalign xys === (xs, ys) where
xys = align xs ys
unzipLaws'
:: forall f proxy. (Unzip f
, Eq (f A), Show (f A), Arbitrary (f A)
, Eq (f B), Show (f B), Arbitrary (f B)
, Eq (f C), Show (f C), Arbitrary (f C)
, Eq (f (A, B)), Show (f (A, B)), Arbitrary (f (A, B))
)
=> proxy f -> TestTree
unzipLaws' _ = testGroup "Unzip"
[ testProperty "unzip = unzipDefault" def
, testProperty "unzipWith via unzip" unzipWithProp
, testProperty "unzip via unzipWith" unzipProp
, testProperty "right inverse" invProp
, testProperty "left inverse" leftProp
]
where
def :: f (A, B) -> Property
def xs = unzip xs === unzipDefault xs
unzipWithProp :: f A -> Fun A (B, C) -> Property
unzipWithProp xs (Fun _ f) = unzipWith f xs === unzip (f <$> xs)
unzipProp :: f (A, B) -> Property
unzipProp xs = unzip xs === unzipWith id xs
invProp :: f (A, B) -> Property
invProp xs = uncurry zip (unzip xs) === xs
leftProp :: f A -> Property
leftProp xs = unzip (zip xs xs) === (xs, xs)