Skip to content

Commit 047f435

Browse files
authored
fromSetA follow up changes (#1165)
* remove unneeded Bot instances * check maps are well constructed in fromSetA action order props * use liftA3 instead of other applicative operations * add validity to `fromSet` testing
1 parent 9f9742b commit 047f435

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

containers-tests/tests/Utils/Strictness.hs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ instance Arbitrary a => Arbitrary (Bot a) where
2727
, (4, Bot <$> arbitrary)
2828
]
2929

30-
instance CoArbitrary a => CoArbitrary (Bot a) where
31-
coarbitrary (Bot x) = coarbitrary x
32-
33-
instance Function a => Function (Bot a) where
34-
function = functionMap (\(Bot x) -> x) Bot
35-
3630
{--------------------------------------------------------------------
3731
Lazy functions
3832
--------------------------------------------------------------------}

containers-tests/tests/intmap-properties.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,16 +1701,24 @@ prop_keysSet keys =
17011701
prop_fromSet :: [Int] -> Fun Int A -> Property
17021702
prop_fromSet keys funF =
17031703
let f = apply funF
1704-
in fromSet f (IntSet.fromList keys) === fromList (fmap (id &&& f) keys)
1704+
m = fromSet f (IntSet.fromList keys)
1705+
in
1706+
valid m .&&.
1707+
m === fromList (fmap (id &&& f) keys)
17051708

17061709
prop_fromSetA_action_order :: [Int] -> Fun Int A -> Property
17071710
prop_fromSetA_action_order keys funF =
1708-
let iSet = IntSet.fromList keys
1711+
let set = IntSet.fromList keys
1712+
setList = IntSet.toList set
17091713
f = apply funF
17101714
action = \k ->
17111715
let v = f k
17121716
in tell [v] $> v
1713-
in execWriter (fromSetA action iSet) === List.map f (IntSet.toList iSet)
1717+
(writtenMap, writtenOutput) = runWriter (fromSetA action set)
1718+
in
1719+
valid writtenMap .&&.
1720+
writtenOutput === List.map f setList .&&.
1721+
toList writtenMap === fmap (id &&& f) setList
17141722

17151723
newtype Identity a = Identity a
17161724
deriving (Eq, Show)

containers-tests/tests/map-properties.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,16 +1723,24 @@ prop_argSet xs =
17231723
prop_fromSet :: [OrdA] -> Fun OrdA B -> Property
17241724
prop_fromSet keys funF =
17251725
let f = apply funF
1726-
in fromSet f (Set.fromList keys) === fromList (fmap (id &&& f) keys)
1726+
m = fromSet f (Set.fromList keys)
1727+
in
1728+
valid m .&&.
1729+
m === fromList (fmap (id &&& f) keys)
17271730

17281731
prop_fromSetA_action_order :: [OrdA] -> Fun OrdA B -> Property
17291732
prop_fromSetA_action_order keys funF =
1730-
let iSet = Set.fromList keys
1733+
let set = Set.fromList keys
1734+
setList = Set.toList set
17311735
f = apply funF
17321736
action = \k ->
17331737
let v = f k
17341738
in tell [v] $> v
1735-
in execWriter (fromSetA action iSet) === List.map f (Set.toList iSet)
1739+
(writtenMap, writtenOutput) = runWriter (fromSetA action set)
1740+
in
1741+
valid writtenMap .&&.
1742+
writtenOutput === List.map f setList .&&.
1743+
toList writtenMap === fmap (id &&& f) setList
17361744

17371745
prop_fromArgSet :: [(OrdA, B)] -> Property
17381746
prop_fromArgSet ys =

containers/src/Data/Map/Internal.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,10 +3551,7 @@ fromSet f = runIdentity . fromSetA (pure . f)
35513551
fromSetA :: Applicative f => (k -> f a) -> Set.Set k -> f (Map k a)
35523552
fromSetA _ Set.Tip = pure Tip
35533553
fromSetA f (Set.Bin sz x l r) =
3554-
flip (Bin sz x)
3555-
<$> fromSetA f l
3556-
<*> f x
3557-
<*> fromSetA f r
3554+
liftA3 (flip (Bin sz x)) (fromSetA f l) (f x) (fromSetA f r)
35583555
#if __GLASGOW_HASKELL__
35593556
{-# INLINABLE fromSetA #-}
35603557
#else

containers/src/Data/Map/Strict/Internal.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,10 +1482,8 @@ fromSet f = runIdentity . fromSetA (pure . f)
14821482
fromSetA :: Applicative f => (k -> f a) -> Set.Set k -> f (Map k a)
14831483
fromSetA _ Set.Tip = pure Tip
14841484
fromSetA f (Set.Bin sz x l r) =
1485-
flip (Bin sz x $!)
1486-
<$> fromSetA f l
1487-
<*> f x
1488-
<*> fromSetA f r
1485+
liftA3 (flip (Bin sz x $!)) (fromSetA f l) (f x) (fromSetA f r)
1486+
14891487
#if __GLASGOW_HASKELL__
14901488
{-# INLINABLE fromSetA #-}
14911489
#else

0 commit comments

Comments
 (0)