Skip to content

Commit

Permalink
io-sim: selectTraceEvents' and friends
Browse files Browse the repository at this point in the history
Added functions which returns the trace up to an exception.  This is
useful when presenting information about a failed simulation, e.g. in
QC's `counterexample`.
  • Loading branch information
coot committed Sep 17, 2021
1 parent c0f55a3 commit e444aa8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions io-sim/src/Control/Monad/IOSim.hs
Expand Up @@ -26,8 +26,11 @@ module Control.Monad.IOSim (
traceEvents,
traceResult,
selectTraceEvents,
selectTraceEvents',
selectTraceEventsDynamic,
selectTraceEventsDynamic',
selectTraceEventsSay,
selectTraceEventsSay',
printTraceEventsSay,
-- * Eventlog
EventlogEvent(..),
Expand Down Expand Up @@ -68,6 +71,19 @@ selectTraceEvents fn = go
go (TraceDeadlock _ threads) = throw (FailureDeadlock threads)
go (TraceMainReturn _ _ _) = []

selectTraceEvents'
:: (TraceEvent -> Maybe b)
-> Trace a
-> [b]
selectTraceEvents' fn = go
where
go (Trace _ _ _ ev trace) = case fn ev of
Just x -> x : go trace
Nothing -> go trace
go (TraceMainException _ _ _) = []
go (TraceDeadlock _ _) = []
go (TraceMainReturn _ _ _) = []

-- | Select all the traced values matching the expected type. This relies on
-- the sim's dynamic trace facility.
--
Expand All @@ -80,6 +96,16 @@ selectTraceEventsDynamic = selectTraceEvents fn
fn (EventLog dyn) = fromDynamic dyn
fn _ = Nothing

-- | Like 'selectTraceEventsDynamic' but returns partial trace if an excpetion
-- is found in it.
--
selectTraceEventsDynamic' :: forall a b. Typeable b => Trace a -> [b]
selectTraceEventsDynamic' = selectTraceEvents' fn
where
fn :: TraceEvent -> Maybe b
fn (EventLog dyn) = fromDynamic dyn
fn _ = Nothing

-- | Get a trace of 'EventSay'.
--
-- For convenience, this throws exceptions for abnormal sim termination.
Expand All @@ -91,6 +117,16 @@ selectTraceEventsSay = selectTraceEvents fn
fn (EventSay s) = Just s
fn _ = Nothing

-- | Like 'selectTraceEventsSay' but return partial trace if an exception is
-- found in it.
--
selectTraceEventsSay' :: Trace a -> [String]
selectTraceEventsSay' = selectTraceEvents' fn
where
fn :: TraceEvent -> Maybe String
fn (EventSay s) = Just s
fn _ = Nothing

-- | Print all 'EventSay' to the console.
--
-- For convenience, this throws exceptions for abnormal sim termination.
Expand Down

0 comments on commit e444aa8

Please sign in to comment.