Skip to content

Commit 19f22ce

Browse files
committed
io-sim: improved haddocks
1 parent 7e25bfa commit 19f22ce

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ import System.IO.Unsafe
133133
import qualified Debug.Trace as Debug
134134

135135

136+
-- | Select events according to the predicate function. It throws an error if
137+
-- the simulation ends with 'Failure'.
138+
--
136139
selectTraceEvents
137140
:: (Time -> SimEventType -> Maybe b)
138141
-> SimTrace a
@@ -150,6 +153,9 @@ selectTraceEvents fn =
150153
[]
151154
. traceSelectTraceEvents fn
152155

156+
-- | Like 'selectTraceEvents', but it returns even if the simulation trace ends
157+
-- with 'Failure'.
158+
--
153159
selectTraceEvents'
154160
:: (Time -> SimEventType -> Maybe b)
155161
-> SimTrace a
@@ -216,10 +222,10 @@ detachTraceRaces = Trace.filter (\a -> case a of
216222
SimRacesFound {} -> False
217223
_ -> True)
218224

219-
-- | Select all the traced values matching the expected type. This relies on
220-
-- the sim's dynamic trace facility.
225+
-- | Select all the traced values matching the expected type. It relies on the
226+
-- sim's dynamic trace facility.
221227
--
222-
-- For convenience, this throws exceptions for abnormal sim termination.
228+
-- For convenience, it throws exceptions for abnormal sim termination.
223229
--
224230
selectTraceEventsDynamic :: forall a b. Typeable b => SimTrace a -> [b]
225231
selectTraceEventsDynamic = selectTraceEvents fn
@@ -228,7 +234,8 @@ selectTraceEventsDynamic = selectTraceEvents fn
228234
fn _ (EventLog dyn) = fromDynamic dyn
229235
fn _ _ = Nothing
230236

231-
-- | Like 'selectTraceEventsDynamic' but also captures time of the trace event.
237+
-- | Like 'selectTraceEventsDynamic' but it also captures time of the trace
238+
-- event.
232239
--
233240
selectTraceEventsDynamicWithTime :: forall a b. Typeable b => SimTrace a -> [(Time, b)]
234241
selectTraceEventsDynamicWithTime = selectTraceEvents fn
@@ -237,8 +244,8 @@ selectTraceEventsDynamicWithTime = selectTraceEvents fn
237244
fn t (EventLog dyn) = (t,) <$> fromDynamic dyn
238245
fn _ _ = Nothing
239246

240-
-- | Like 'selectTraceEventsDynamic' but returns partial trace if an exception
241-
-- is found in it.
247+
-- | Like 'selectTraceEventsDynamic' but it returns even if the simulation trace
248+
-- ends with 'Failure'.
242249
--
243250
selectTraceEventsDynamic' :: forall a b. Typeable b => SimTrace a -> [b]
244251
selectTraceEventsDynamic' = selectTraceEvents' fn
@@ -247,7 +254,8 @@ selectTraceEventsDynamic' = selectTraceEvents' fn
247254
fn _ (EventLog dyn) = fromDynamic dyn
248255
fn _ _ = Nothing
249256

250-
-- | Like `selectTraceEventsDynamic'` but also captures time of the trace event.
257+
-- | Like `selectTraceEventsDynamic'` but it also captures time of the trace
258+
-- event.
251259
--
252260
selectTraceEventsDynamicWithTime' :: forall a b. Typeable b => SimTrace a -> [(Time, b)]
253261
selectTraceEventsDynamicWithTime' = selectTraceEvents' fn
@@ -258,7 +266,7 @@ selectTraceEventsDynamicWithTime' = selectTraceEvents' fn
258266

259267
-- | Get a trace of 'EventSay'.
260268
--
261-
-- For convenience, this throws exceptions for abnormal sim termination.
269+
-- For convenience, it throws exceptions for abnormal sim termination.
262270
--
263271
selectTraceEventsSay :: SimTrace a -> [String]
264272
selectTraceEventsSay = selectTraceEvents fn
@@ -267,7 +275,7 @@ selectTraceEventsSay = selectTraceEvents fn
267275
fn _ (EventSay s) = Just s
268276
fn _ _ = Nothing
269277

270-
-- | Like 'selectTraceEventsSay' but also captures time of the trace event.
278+
-- | Like 'selectTraceEventsSay' but it also captures time of the trace event.
271279
--
272280
selectTraceEventsSayWithTime :: SimTrace a -> [(Time, String)]
273281
selectTraceEventsSayWithTime = selectTraceEvents fn
@@ -276,8 +284,8 @@ selectTraceEventsSayWithTime = selectTraceEvents fn
276284
fn t (EventSay s) = Just (t, s)
277285
fn _ _ = Nothing
278286

279-
-- | Like 'selectTraceEventsSay' but return partial trace if an exception is
280-
-- found in it.
287+
-- | Like 'selectTraceEventsSay' but it returns even if the simulation trace
288+
-- ends with 'Failure'.
281289
--
282290
selectTraceEventsSay' :: SimTrace a -> [String]
283291
selectTraceEventsSay' = selectTraceEvents' fn
@@ -286,7 +294,7 @@ selectTraceEventsSay' = selectTraceEvents' fn
286294
fn _ (EventSay s) = Just s
287295
fn _ _ = Nothing
288296

289-
-- | Like `selectTraceEventsSay'` but also captures time of the trace event.
297+
-- | Like `selectTraceEventsSay'` but it also captures time of the trace event.
290298
--
291299
selectTraceEventsSayWithTime' :: SimTrace a -> [(Time, String)]
292300
selectTraceEventsSayWithTime' = selectTraceEvents' fn
@@ -297,13 +305,13 @@ selectTraceEventsSayWithTime' = selectTraceEvents' fn
297305

298306
-- | Print all 'EventSay' to the console.
299307
--
300-
-- For convenience, this throws exceptions for abnormal sim termination.
308+
-- For convenience, it throws exceptions for abnormal sim termination.
301309
--
302310
printTraceEventsSay :: SimTrace a -> IO ()
303311
printTraceEventsSay = mapM_ print . selectTraceEventsSay
304312

305313

306-
-- | The most general select function. It is a _total_ function.
314+
-- | The most general select function. It is a /total function/.
307315
--
308316
traceSelectTraceEvents
309317
:: (Time -> SimEventType -> Maybe b)
@@ -324,7 +332,7 @@ traceSelectTraceEvents fn = bifoldr ( \ v _acc -> Nil v )
324332
)
325333
undefined -- it is ignored
326334

327-
-- | Select dynamic events. It is a _total_ function.
335+
-- | Select dynamic events. It is a /total function/.
328336
--
329337
traceSelectTraceEventsDynamic :: forall a b. Typeable b
330338
=> SimTrace a -> Trace (SimResult a) b
@@ -335,7 +343,7 @@ traceSelectTraceEventsDynamic = traceSelectTraceEvents fn
335343
fn _ _ = Nothing
336344

337345

338-
-- | Select say events. It is a _total_ function.
346+
-- | Select say events. It is a /total function/.
339347
--
340348
traceSelectTraceEventsSay :: forall a. SimTrace a -> Trace (SimResult a) String
341349
traceSelectTraceEventsSay = traceSelectTraceEvents fn
@@ -417,7 +425,7 @@ runSimOrThrow mainAction =
417425
runSimStrictShutdown :: forall a. (forall s. IOSim s a) -> Either Failure a
418426
runSimStrictShutdown mainAction = traceResult True (runSimTrace mainAction)
419427

420-
-- | Fold through the trace and return either a 'Failure' or the simulation
428+
-- | Fold through the trace and return either 'Failure' or a simulation
421429
-- result, i.e. the return value of the main thread.
422430
--
423431
traceResult :: Bool
@@ -502,19 +510,18 @@ runSimTrace mainAction = runST (runSimTraceST mainAction)
502510
-- slot. In /IOSim/ and /IOSimPOR/ time only moves explicitly through timer
503511
-- events, e.g. things like `Control.Monad.Class.MonadTimer.SI.threadDelay`,
504512
-- `Control.Monad.Class.MonadTimer.SI.registerDelay` or the
505-
-- `Control.Monad.Class.MonadTimer.NonStandard.MonadTimeout` API. The usual
513+
-- `Control.Monad.Class.MonadTimer.MonadTimeout.NonStandard` API. The usual
506514
-- QuickCheck techniques can help explore different schedules of
507515
-- threads too.
508516

509517
-- | Execute a simulation, discover & revert races. Note that this will execute
510518
-- the simulation multiple times with different schedules, and thus it's much
511519
-- more costly than a simple `runSimTrace` (also the simulation environments has
512-
-- much more state to track and hence is slower).
520+
-- much more state to track and hence it is slower).
513521
--
514522
-- On property failure it will show the failing schedule (`ScheduleControl`)
515-
-- which can be plugged to `controlSimTrace`.
516-
--
517-
-- Note: `exploreSimTrace` evaluates each schedule in parallel (using `par`).
523+
-- which can be passed to `controlSimTrace` to reproduce the failure without
524+
-- discovering the schedule.
518525
--
519526
exploreSimTrace
520527
:: forall a test. Testable test
@@ -533,8 +540,6 @@ exploreSimTrace optsf main k =
533540
-- | An 'ST' version of 'exploreSimTrace'. The callback also receives
534541
-- 'ScheduleControl'. This is mostly useful for testing /IOSimPOR/ itself.
535542
--
536-
-- Note: `exploreSimTraceST` evaluates each schedule sequentially.
537-
--
538543
exploreSimTraceST
539544
:: forall s a test. Testable test
540545
=> (ExplorationOptions -> ExplorationOptions)
@@ -689,15 +694,15 @@ raceReversals ControlDefault = 0
689694
raceReversals (ControlAwait mods) = length mods
690695
raceReversals ControlFollow{} = error "Impossible: raceReversals ControlFollow{}"
691696

692-
-- compareTraces is given (maybe) a passing trace and a failing trace,
697+
-- `compareTracesST` is given (maybe) a passing trace and a failing trace,
693698
-- and identifies the point at which they diverge, where it inserts a
694699
-- "sleep" event for the thread that is delayed in the failing case,
695700
-- and a "wake" event before its next action. It also returns the
696701
-- identity and time of the sleeping thread. Since we expect the trace
697702
-- to be consumed lazily (and perhaps only partially), and since the
698703
-- sleeping thread is not of interest unless the trace is consumed
699704
-- this far, then we collect its identity only if it is reached using
700-
-- unsafePerformIO.
705+
-- `unsafePerformIO`.
701706

702707
-- TODO: return StepId
703708
compareTracesST :: forall a b s.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -991,10 +991,10 @@ lookupThreadLabel :: IOSimThreadId -> Map IOSimThreadId (Thread s a) -> Maybe Th
991991
lookupThreadLabel tid threads = join (threadLabel <$> Map.lookup tid threads)
992992

993993

994-
-- | The most general method of running 'IOSim' is in 'ST' monad. One can
995-
-- recover failures or the result from 'SimTrace' with
996-
-- 'Control.Monad.IOSim.traceResult', or access 'SimEventType's generated by the
997-
-- computation with 'Control.Monad.IOSim.traceEvents'. A slightly more
994+
-- | The most general method of running 'IOSim' is in the lazy 'ST' monad. One
995+
-- can recover failures or the result from 'SimTrace' with
996+
-- 'Control.Monad.IOSim.traceResult', or access 'SimEventType's generated by
997+
-- the computation with 'Control.Monad.IOSim.traceEvents'. A slightly more
998998
-- convenient way is exposed by 'Control.Monad.IOSim.runSimTrace'.
999999
--
10001000
runSimTraceST :: forall s a. IOSim s a -> ST s (SimTrace a)

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ newtype IOSim s a = IOSim { unIOSim :: forall r. (a -> SimA s r) -> SimA s r }
143143
runIOSim :: IOSim s a -> SimA s a
144144
runIOSim (IOSim k) = k Return
145145

146-
-- | 'IOSim' has the ability to story any 'Typeable' value in its trace which
146+
-- | 'IOSim' has the ability to store any 'Typeable' value in its trace which
147147
-- can then be recovered with `selectTraceEventsDynamic` or
148148
-- `selectTraceEventsDynamic'`.
149149
--
@@ -234,7 +234,9 @@ data StmA s a where
234234
LiftSTStm :: StrictST.ST s a -> (a -> StmA s b) -> StmA s b
235235
FixStm :: (x -> STM s x) -> (x -> StmA s r) -> StmA s r
236236

237-
-- Exported type
237+
-- | `IOSim`'s 'MonadSTM.STM' monad, as 'IOSim' it is parametrised by @s@, e.g.
238+
-- @STMSim s a@ is monadic expression of type @a@.
239+
--
238240
type STMSim = STM
239241

240242
--
@@ -630,8 +632,9 @@ instance MonadST (IOSim s) where
630632

631633
-- | Lift an 'StrictST.ST' computation to 'IOSim'.
632634
--
633-
-- Note: you can use 'MonadST' to lift 'StrictST.ST' computations, this is just
635+
-- Note: you can use 'MonadST' to lift 'StrictST.ST' computations, this is
634636
-- a more convenient function just for 'IOSim'.
637+
--
635638
liftST :: StrictST.ST s a -> IOSim s a
636639
liftST action = IOSim $ oneShot $ \k -> LiftST action k
637640

@@ -816,14 +819,15 @@ data SimResult a
816819
-- ^ Return value of the main thread.
817820
| MainException !Time !(Labelled IOSimThreadId) SomeException ![Labelled IOSimThreadId]
818821
-- ^ Exception thrown by the main thread.
819-
| Deadlock !Time ![Labelled IOSimThreadId]
822+
| Deadlock !Time ![Labelled IOSimThreadId]
820823
-- ^ Deadlock discovered in the simulation. Deadlocks are discovered if
821824
-- simply the simulation cannot do any progress in a given time slot and
822825
-- there's no event which would advance the time.
823826
| Loop
824827
-- ^ Only returned by /IOSimPOR/ when a step execution took longer than
825828
-- 'explorationStepTimelimit` was exceeded.
826829
| InternalError String
830+
-- ^ An `IOSim` bug, please report to <https://github.com/input-output-hk/io-sim>
827831
deriving (Show, Functor)
828832

829833
ppSimResult :: Show a
@@ -874,6 +878,12 @@ type SimTrace a = Trace.Trace (SimResult a) SimEvent
874878

875879
-- | Pretty print simulation trace.
876880
--
881+
-- Note: this is not a streaming function, it will evaluate the whole trace
882+
-- before printing it. If you need to print a very large trace, you might want
883+
-- to use
884+
--
885+
-- @'Trace.ppTrace' show ('ppSimEvent' 0 0 0)@
886+
--
877887
ppTrace :: Show a => SimTrace a -> String
878888
ppTrace tr = Trace.ppTrace
879889
(ppSimResult timeWidth tidWidth labelWidth)
@@ -1083,6 +1093,8 @@ data SimEventType
10831093
| EventPerformAction StepId
10841094
-- ^ /IOSimPOR/ event: perform action of the given step
10851095
| EventReschedule ScheduleControl
1096+
-- ^ /IOSimPOR/ event: reschedule a thread following the given
1097+
-- `ScheduleControl`
10861098

10871099
| EventEffect VectorClock Effect
10881100
-- ^ /IOSimPOR/ event: executed effect; Useful for debugging IOSimPOR or

0 commit comments

Comments
 (0)