Skip to content

Commit

Permalink
io-sim: generalise selector types
Browse files Browse the repository at this point in the history
Generalised `selectTraceEvents'` and friends.  It can work with any
return type, not just `SimResult`.
  • Loading branch information
coot committed Dec 10, 2024
1 parent 785dc08 commit e4ebb82
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 8 additions & 0 deletions io-sim/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
the value to other waiting threads.
- Faster handling of timeouts and timers by using a more efficient
internal representation.
- The signature of:
- `selectTraceEvents'`,
- `selectTraceEventsDynamic'`,
- `selectTraceEventsDynamicWithTime'`,
- `selectTraceEventsSay'` and
- `selectTraceEventsSayWithTime'`
is more general. These functions now accepts trace with any result, rather
than one that finishes with `SimResult`.

## 1.6.0.0

Expand Down
26 changes: 15 additions & 11 deletions io-sim/src/Control/Monad/IOSim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ selectTraceEvents fn =
. traceSelectTraceEvents fn

-- | Like 'selectTraceEvents', but it returns even if the simulation trace ends
-- with 'Failure'.
-- with 'Failure'. It also works with any return type, not only `SimResult`
-- like `selectTraceEvents` does.
--
selectTraceEvents'
:: (Time -> SimEventType -> Maybe b)
-> SimTrace a
-> Trace a SimEvent
-> [b]
selectTraceEvents' fn =
bifoldr ( \ _ _ -> [] )
Expand Down Expand Up @@ -225,20 +226,22 @@ selectTraceEventsDynamicWithTime = selectTraceEvents fn
fn t (EventLog dyn) = (t,) <$> fromDynamic dyn
fn _ _ = Nothing

-- | Like 'selectTraceEventsDynamic' but it returns even if the simulation trace
-- ends with 'Failure'.
-- | Like 'selectTraceEventsDynamic' but it returns even if the simulation
-- trace ends with 'Failure'. It also works with any return type, not only
-- `SimResult` like `selectTraceEventsDynamic` does.
--
selectTraceEventsDynamic' :: forall a b. Typeable b => SimTrace a -> [b]
selectTraceEventsDynamic' :: forall a b. Typeable b => Trace a SimEvent -> [b]
selectTraceEventsDynamic' = selectTraceEvents' fn
where
fn :: Time -> SimEventType -> Maybe b
fn _ (EventLog dyn) = fromDynamic dyn
fn _ _ = Nothing

-- | Like `selectTraceEventsDynamic'` but it also captures time of the trace
-- event.
-- | Like `selectTraceEventsDynamicWithTime'` but it also captures time of the
-- trace event. It also works with any return type, not only `SimResult` like
-- `selectTraceEventsDynamicWithTime` does.
--
selectTraceEventsDynamicWithTime' :: forall a b. Typeable b => SimTrace a -> [(Time, b)]
selectTraceEventsDynamicWithTime' :: forall a b. Typeable b => Trace a SimEvent -> [(Time, b)]
selectTraceEventsDynamicWithTime' = selectTraceEvents' fn
where
fn :: Time -> SimEventType -> Maybe (Time, b)
Expand Down Expand Up @@ -266,9 +269,10 @@ selectTraceEventsSayWithTime = selectTraceEvents fn
fn _ _ = Nothing

-- | Like 'selectTraceEventsSay' but it returns even if the simulation trace
-- ends with 'Failure'.
-- ends with 'Failure'. It also works with any return type, not only `SimResult`
-- like `selectTraceEventsSay` does.
--
selectTraceEventsSay' :: SimTrace a -> [String]
selectTraceEventsSay' :: Trace a SimEvent -> [String]
selectTraceEventsSay' = selectTraceEvents' fn
where
fn :: Time -> SimEventType -> Maybe String
Expand All @@ -277,7 +281,7 @@ selectTraceEventsSay' = selectTraceEvents' fn

-- | Like `selectTraceEventsSay'` but it also captures time of the trace event.
--
selectTraceEventsSayWithTime' :: SimTrace a -> [(Time, String)]
selectTraceEventsSayWithTime' :: Trace a SimEvent -> [(Time, String)]
selectTraceEventsSayWithTime' = selectTraceEvents' fn
where
fn :: Time -> SimEventType -> Maybe (Time, String)
Expand Down

0 comments on commit e4ebb82

Please sign in to comment.