Skip to content

Commit e9749f9

Browse files
committed
io-sim: selectTraceEvents' and friends
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`.
1 parent e221354 commit e9749f9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

io-sim/src/Control/Monad/IOSim.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ module Control.Monad.IOSim (
2626
traceEvents,
2727
traceResult,
2828
selectTraceEvents,
29+
selectTraceEvents',
2930
selectTraceEventsDynamic,
31+
selectTraceEventsDynamic',
3032
selectTraceEventsSay,
33+
selectTraceEventsSay',
3134
printTraceEventsSay,
3235
-- * Eventlog
3336
EventlogEvent(..),
@@ -68,6 +71,19 @@ selectTraceEvents fn = go
6871
go (TraceDeadlock _ threads) = throw (FailureDeadlock threads)
6972
go (TraceMainReturn _ _ _) = []
7073

74+
selectTraceEvents'
75+
:: (TraceEvent -> Maybe b)
76+
-> Trace a
77+
-> [b]
78+
selectTraceEvents' fn = go
79+
where
80+
go (Trace _ _ _ ev trace) = case fn ev of
81+
Just x -> x : go trace
82+
Nothing -> go trace
83+
go (TraceMainException _ _ _) = []
84+
go (TraceDeadlock _ _) = []
85+
go (TraceMainReturn _ _ _) = []
86+
7187
-- | Select all the traced values matching the expected type. This relies on
7288
-- the sim's dynamic trace facility.
7389
--
@@ -80,6 +96,16 @@ selectTraceEventsDynamic = selectTraceEvents fn
8096
fn (EventLog dyn) = fromDynamic dyn
8197
fn _ = Nothing
8298

99+
-- | Like 'selectTraceEventsDynamic' but returns partial trace if an excpetion
100+
-- is found in it.
101+
--
102+
selectTraceEventsDynamic' :: forall a b. Typeable b => Trace a -> [b]
103+
selectTraceEventsDynamic' = selectTraceEvents' fn
104+
where
105+
fn :: TraceEvent -> Maybe b
106+
fn (EventLog dyn) = fromDynamic dyn
107+
fn _ = Nothing
108+
83109
-- | Get a trace of 'EventSay'.
84110
--
85111
-- For convenience, this throws exceptions for abnormal sim termination.
@@ -91,6 +117,16 @@ selectTraceEventsSay = selectTraceEvents fn
91117
fn (EventSay s) = Just s
92118
fn _ = Nothing
93119

120+
-- | Like 'selectTraceEventsSay' but return partial trace if an exception is
121+
-- found in it.
122+
--
123+
selectTraceEventsSay' :: Trace a -> [String]
124+
selectTraceEventsSay' = selectTraceEvents' fn
125+
where
126+
fn :: TraceEvent -> Maybe String
127+
fn (EventSay s) = Just s
128+
fn _ = Nothing
129+
94130
-- | Print all 'EventSay' to the console.
95131
--
96132
-- For convenience, this throws exceptions for abnormal sim termination.

0 commit comments

Comments
 (0)