@@ -21,6 +21,7 @@ import Control.Applicative (Applicative(..), liftA2)
21
21
import Control.Arrow ((***) )
22
22
import Control.Monad.Trans.State.Strict
23
23
import Data.Array (listArray )
24
+ import Data.Coerce (coerce )
24
25
import Data.Foldable (Foldable (foldl , foldl1 , foldr , foldr1 , foldMap , fold ), toList , all , sum , foldl' , foldr' )
25
26
import Data.Functor ((<$>) , (<$) )
26
27
import Data.Maybe
@@ -43,8 +44,10 @@ import Control.Monad.Zip (MonadZip (..))
43
44
import Control.DeepSeq (deepseq )
44
45
import Control.Monad.Fix (MonadFix (.. ))
45
46
import Test.Tasty.HUnit
47
+ import Test.ChasingBottoms.IsBottom (isBottom )
46
48
import qualified Language.Haskell.TH.Syntax as TH
47
49
50
+ import Utils.Strictness (Bot (.. ), Func2 , applyFunc2 )
48
51
49
52
main :: IO ()
50
53
main = defaultMain $ testGroup " seq-properties"
@@ -56,11 +59,9 @@ main = defaultMain $ testGroup "seq-properties"
56
59
, testProperty " (<$)" prop_constmap
57
60
, testProperty " foldr" prop_foldr
58
61
, testProperty " foldr'" prop_foldr'
59
- , testProperty " lazy foldr'" prop_lazyfoldr'
60
62
, testProperty " foldr1" prop_foldr1
61
63
, testProperty " foldl" prop_foldl
62
64
, testProperty " foldl'" prop_foldl'
63
- , testProperty " lazy foldl'" prop_lazyfoldl'
64
65
, testProperty " foldl1" prop_foldl1
65
66
, testProperty " (==)" prop_equals
66
67
, testProperty " compare" prop_compare
@@ -156,6 +157,12 @@ main = defaultMain $ testGroup "seq-properties"
156
157
, testProperty " Right view pattern" prop_viewr_pat
157
158
, testProperty " Right view constructor" prop_viewr_con
158
159
, testProperty " stimes" prop_stimes
160
+ , testGroup " strictness"
161
+ [ testProperty " foldr" prop_strictness_foldr
162
+ , testProperty " foldl" prop_strictness_foldl
163
+ , testProperty " foldr'" prop_strictness_foldr'
164
+ , testProperty " foldl'" prop_strictness_foldl'
165
+ ]
159
166
]
160
167
161
168
------------------------------------------------------------------------
@@ -310,16 +317,6 @@ prop_foldr' xs =
310
317
f = (:)
311
318
z = []
312
319
313
- prop_lazyfoldr' :: Seq () -> Property
314
- prop_lazyfoldr' xs =
315
- not (null xs) ==>
316
- foldr'
317
- (\ e _ ->
318
- e)
319
- (error " Data.Sequence.foldr': should be lazy in initial accumulator" )
320
- xs ===
321
- ()
322
-
323
320
prop_foldr1 :: Seq Int -> Property
324
321
prop_foldr1 xs =
325
322
not (null xs) ==> foldr1 f xs == Data.List. foldr1 f (toList xs)
@@ -339,16 +336,6 @@ prop_foldl' xs =
339
336
f = flip (:)
340
337
z = []
341
338
342
- prop_lazyfoldl' :: Seq () -> Property
343
- prop_lazyfoldl' xs =
344
- not (null xs) ==>
345
- foldl'
346
- (\ _ e ->
347
- e)
348
- (error " Data.Sequence.foldl': should be lazy in initial accumulator" )
349
- xs ===
350
- ()
351
-
352
339
prop_foldl1 :: Seq Int -> Property
353
340
prop_foldl1 xs =
354
341
not (null xs) ==> foldl1 f xs == Data.List. foldl1 f (toList xs)
@@ -903,6 +890,42 @@ test_mfix = toList resS === resL
903
890
resL :: [Int ]
904
891
resL = fmap ($ 12 ) $ mfix (\ f -> [facty f, facty (+ 1 ), facty (+ 2 )])
905
892
893
+ -- * Strictness tests
894
+
895
+ -- See Note [Testing strictness of folds] in map-strictness.hs
896
+
897
+ prop_strictness_foldr :: [A ] -> Func2 A B (Bot B ) -> Bot B -> Property
898
+ prop_strictness_foldr xs fun (Bot z) =
899
+ isBottom (foldr f z s) ===
900
+ isBottom (foldr f z xs)
901
+ where
902
+ s = fromList xs
903
+ f = coerce (applyFunc2 fun) :: A -> B -> B
904
+
905
+ prop_strictness_foldl :: [A ] -> Func2 B A (Bot B ) -> Bot B -> Property
906
+ prop_strictness_foldl (xs) fun (Bot z) =
907
+ isBottom (foldl f z s) ===
908
+ isBottom (foldl f z xs)
909
+ where
910
+ s = fromList xs
911
+ f = coerce (applyFunc2 fun) :: B -> A -> B
912
+
913
+ prop_strictness_foldr' :: [A ] -> Func2 A B (Bot B ) -> Bot B -> Property
914
+ prop_strictness_foldr' xs fun (Bot z) =
915
+ isBottom (foldr' f z s) ===
916
+ isBottom (z `seq` foldr' f z xs)
917
+ where
918
+ s = fromList xs
919
+ f = coerce (applyFunc2 fun) :: A -> B -> B
920
+
921
+ prop_strictness_foldl' :: [A ] -> Func2 B A (Bot B ) -> Bot B -> Property
922
+ prop_strictness_foldl' xs fun (Bot z) =
923
+ isBottom (foldl' f z s) ===
924
+ isBottom (foldl' f z xs)
925
+ where
926
+ s = fromList xs
927
+ f = coerce (applyFunc2 fun) :: B -> A -> B
928
+
906
929
-- Simple test monad
907
930
908
931
data M a = Action Int a
0 commit comments