Skip to content

Commit ffb82fd

Browse files
committed
io-sim:test - refactored traceNoDuplicates
Refactored `traceNoDuplicates` so that `GHC` doesn't put `r` outside of the function. This could also be achieved with `-fno-full-laziness`.
1 parent 000874c commit ffb82fd

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

io-sim/test/Test/Control/Monad/IOSimPOR.hs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
{-# LANGUAGE CPP #-}
23
{-# LANGUAGE DeriveGeneric #-}
34
{-# LANGUAGE FlexibleContexts #-}
@@ -434,15 +435,31 @@ doit n = do
434435
threadDelay 1
435436
readTVarIO r
436437

437-
438-
traceNoDuplicates :: (Testable prop1, Show a1) => ((a1 -> a2 -> a2) -> prop1) -> Property
439-
traceNoDuplicates k = r `pseq` (k addTrace .&&. maximum (traceCounts ()) == 1)
438+
traceNoDuplicates :: forall a b.
439+
(Show a)
440+
=> ((a -> b -> b) -> Property)
441+
-> Property
442+
-- this NOINLINE pragma is useful for debugging if `r` didn't flow outside of
443+
-- `traceNoDuplicate`.
444+
{-# NOINLINE traceNoDuplicates #-}
445+
traceNoDuplicates k = unsafePerformIO $ do
446+
r <- newIORef (Map.empty :: Map String Int)
447+
return $ r `pseq`
448+
(k (addTrace r) .&&. counterexample "trace counts" (maximum (Map.elems (traceCounts r)) === 1))
440449
where
441-
r = unsafePerformIO $ newIORef (Map.empty :: Map String Int)
442-
addTrace t x = unsafePerformIO $ do
443-
atomicModifyIORef r (\m->(Map.insertWith (+) (show t) 1 m,()))
450+
addTrace :: IORef (Map String Int) -> a -> b -> b
451+
addTrace r t x = unsafePerformIO $ do
452+
let s = show t
453+
atomicModifyIORef r
454+
(\m->
455+
let m' = Map.insertWith (+) s 1 m
456+
in (m', ())
457+
)
444458
return x
445-
traceCounts () = unsafePerformIO $ Map.elems <$> readIORef r
459+
460+
traceCounts :: IORef (Map String Int) -> Map String Int
461+
traceCounts r = unsafePerformIO $ readIORef r
462+
446463

447464
-- | Checks that IOSimPOR is capable of analysing an infinite simulation
448465
-- lazily.

0 commit comments

Comments
 (0)