Skip to content

Commit e840ca0

Browse files
committed
io-sim-por: refactor the invariant function
Now ghc can report which condition is not met.
1 parent 262268a commit e840ca0

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

io-sim/src/Control/Monad/IOSimPOR/Internal.hs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,22 @@ initialState =
205205
where
206206
epoch1970 = UTCTime (fromGregorian 1970 1 1) 0
207207

208-
invariant :: Maybe (Thread s a) -> SimState s a -> Bool
208+
invariant :: Maybe (Thread s a) -> SimState s a -> x -> x
209209

210210
invariant (Just running) simstate@SimState{runqueue,threads,clocks} =
211-
not (threadBlocked running)
212-
&& threadId running `Map.notMember` threads
213-
&& threadId running `List.notElem` runqueue
214-
&& threadClockId running `Map.member` clocks
215-
&& invariant Nothing simstate
211+
assert (not (threadBlocked running))
212+
. assert (threadId running `Map.notMember` threads)
213+
. assert (threadId running `List.notElem` runqueue)
214+
. assert (threadClockId running `Map.member` clocks)
215+
. invariant Nothing simstate
216216

217217
invariant Nothing SimState{runqueue,threads,clocks} =
218-
all (`Map.member` threads) runqueue
219-
&& and [ (threadBlocked t || threadDone t) == (threadId t `notElem` runqueue)
220-
| t <- Map.elems threads ]
221-
&& and (zipWith (>) runqueue (drop 1 runqueue))
222-
&& and [ threadClockId t `Map.member` clocks
223-
| t <- Map.elems threads ]
218+
assert (all (`Map.member` threads) runqueue)
219+
. assert (and [ (threadBlocked t || threadDone t) == (threadId t `notElem` runqueue)
220+
| t <- Map.elems threads ])
221+
. assert (and (zipWith (>) runqueue (drop 1 runqueue)))
222+
. assert (and [ threadClockId t `Map.member` clocks
223+
| t <- Map.elems threads ])
224224

225225
-- | Interpret the simulation monotonic time as a 'NominalDiffTime' since
226226
-- the start.
@@ -274,7 +274,7 @@ schedule thread@Thread{
274274
deschedule Sleep thread simstate
275275

276276
| otherwise =
277-
assert (invariant (Just thread) simstate) $
277+
invariant (Just thread) simstate $
278278
case control of
279279
ControlFollow (s:_) _ ->
280280
id --Debug.trace ("Performing action in step "++show s++"\n")
@@ -777,7 +777,7 @@ reschedule simstate@SimState{ runqueue, threads,
777777
} =
778778
assert (tid `elem` runqueue) $
779779
assert (tid `Map.member` threads) $
780-
assert (invariant Nothing simstate) $
780+
invariant Nothing simstate $
781781
let thread = threads Map.! tid in
782782
assert (threadId thread == tid) $
783783
--assert (threadStep thread == tstep) $
@@ -794,7 +794,7 @@ reschedule simstate@SimState{ runqueue, threads,
794794
-- When there is no current running thread but the runqueue is non-empty then
795795
-- schedule the next one to run.
796796
reschedule simstate@SimState{ runqueue = tid:runqueue', threads } =
797-
assert (invariant Nothing simstate) $
797+
invariant Nothing simstate $
798798

799799
let thread = threads Map.! tid in
800800
schedule thread simstate { runqueue = runqueue'
@@ -803,7 +803,7 @@ reschedule simstate@SimState{ runqueue = tid:runqueue', threads } =
803803
-- But when there are no runnable threads, we advance the time to the next
804804
-- timer event, or stop.
805805
reschedule simstate@SimState{ runqueue = [], threads, timers, curTime = time, races } =
806-
assert (invariant Nothing simstate) $
806+
invariant Nothing simstate $
807807

808808
-- time is moving on
809809
--Debug.trace ("Rescheduling at "++show time++", "++

0 commit comments

Comments
 (0)