| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Ouroboros.Consensus.Util.TimeBoundedLoop
Contents
Description
Time-bounded iteration.
This is based on simple single-threaded iterative time budgeting and as such not suitable for defensive timeouts since no attempts are made at stopping the computation that breached the time limit.
Synopsis
- iterateUntilOrTimeout ∷ MonadMonotonicTime m ⇒ DiffTime → (a → Bool) → (a → m a) → a → m (a, StepCount, DoneType)
- data DoneType
- type StepCount = Int
- iterateUntilOrTimeout_ ∷ MonadMonotonicTime m ⇒ DiffTime → (a → Bool) → (a → m a) → a → m a
- foldOrTimeout ∷ MonadMonotonicTime m ⇒ DiffTime → (b → a → m b) → b → [a] → m (b, [a], StepCount, DoneType)
- propIterImmediateEndOnInit ∷ (MonadMonotonicTime m, Eq a) ⇒ DiffTime → (a → m a) → a → ExceptT (String, IterResult a) m (IterResult a)
- propIterTimeoutOverrunBoundedByOneStep ∷ ∀ (m ∷ Type → Type). MonadDelay m ⇒ DiffTime → DiffTime → ExceptT (String, (DiffTime, IterResult ())) m (IterResult ())
- propFoldEmptyListTerminates ∷ (MonadMonotonicTime m, Eq b) ⇒ DiffTime → (b → a → m b) → b → ExceptT (String, FoldResult b a) m (FoldResult b a)
- propFoldStepsMatchConsumed ∷ MonadMonotonicTime m ⇒ DiffTime → (b → a → m b) → b → [a] → ExceptT (String, FoldResult b a) m (FoldResult b a)
- propFoldEndReachedIffConsumedAll ∷ MonadMonotonicTime m ⇒ DiffTime → (b → a → m b) → b → [a] → ExceptT (String, FoldResult b a) m (FoldResult b a)
- propFoldJustFoldl ∷ ∀ (m ∷ Type → Type) b a. (MonadMonotonicTime m, Eq b) ⇒ DiffTime → (b → a → b) → b → [a] → ExceptT (String, FoldResult b a) m (FoldResult b a)
Iteration
iterateUntilOrTimeout Source #
Arguments
| ∷ MonadMonotonicTime m | |
| ⇒ DiffTime | Wall-clock budget |
| → (a → Bool) | End predicate; when |
| → (a → m a) | One step of work |
| → a | Initial state |
| → m (a, StepCount, DoneType) |
(val, steps, done) <- iterateUntilOrTimeout ttl endReached stepAction initSt runs
stepAction on a state repeatedly, with initSt as the initial state value, until either
endReached holds or the wall-clock budget ttl is exceeded.
Returns the final result val, the steps it took to compute it and done denoting
whether the computation terminated due to the provided end condition or timeout.
Strict on the accumulator.
Single-threaded: everything runs in one thread. If stepAction blocks
longer than ttl, the timeout is detected only on the next iteration —
the in-flight call is not cancelled. The total elapsed time can therefore
exceed ttl by at most one step duration (see propIterTimeoutOverrunBoundedByOneStep).
If the endReached holds on the initSt the loop returns immediately with
steps == 0 and done == EndReached (see propIterImmediateEndOnInit).
Naming reflects https://hackage.haskell.org/package/monad-loops-0.4.3/docs/Control-Monad-Loops.html#v:iterateUntilM
Why time capped loop stopped.
Constructors
| TimeoutReached | The time budget was exhausted before the end of computation was reached. |
| EndReached | The computation reached an end before the time budget was exhausted. |
iterateUntilOrTimeout_ Source #
Arguments
| ∷ MonadMonotonicTime m | |
| ⇒ DiffTime | Wall-clock budget |
| → (a → Bool) | End predicate; when |
| → (a → m a) | One step of work |
| → a | Initial state |
| → m a |
Like iterateUntilOrTimeout but discards the step count and DoneType,
returning only the final state. Use when you don't need to distinguish a
timeout from a normal completion.
Arguments
| ∷ MonadMonotonicTime m | |
| ⇒ DiffTime | Wall-clock budget |
| → (b → a → m b) | Fold action applied to each element |
| → b | Initial accumulator |
| → [a] | Elements to fold over |
| → m (b, [a], StepCount, DoneType) |
(val, remaining, steps, done) <- foldOrTimeout ttl foldAction initSt xs runs a time capped
left fold using iterateUntilOrTimeout.
Returns the final result in val, the remaining elements of xs that weren't processed,
the steps it took to compute it and whether the
computation terminated due to the input xs being fully traversed or ttl timeout.
Strict on the accumulator.
Naming reflects https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Monad.html#v:foldM
Properties
propIterImmediateEndOnInit ∷ (MonadMonotonicTime m, Eq a) ⇒ DiffTime → (a → m a) → a → ExceptT (String, IterResult a) m (IterResult a) Source #
If endReached holds on the initial state, iterateUntilOrTimeout returns
immediately with zero steps and EndReached, regardless of the budget.
propIterTimeoutOverrunBoundedByOneStep ∷ ∀ (m ∷ Type → Type). MonadDelay m ⇒ DiffTime → DiffTime → ExceptT (String, (DiffTime, IterResult ())) m (IterResult ()) Source #
The total elapsed time never exceeds ttl + stepDuration: the timeout is
detected at the top of the loop, so at most one in-flight step can overshoot
before the budget breach is observed.
propFoldEmptyListTerminates ∷ (MonadMonotonicTime m, Eq b) ⇒ DiffTime → (b → a → m b) → b → ExceptT (String, FoldResult b a) m (FoldResult b a) Source #
foldOrTimeout on an empty list terminates immediately: no change, zero steps,
EndReached, and an empty remainder.
propFoldStepsMatchConsumed ∷ MonadMonotonicTime m ⇒ DiffTime → (b → a → m b) → b → [a] → ExceptT (String, FoldResult b a) m (FoldResult b a) Source #
The step count always equals the number of elements consumed.
propFoldEndReachedIffConsumedAll ∷ MonadMonotonicTime m ⇒ DiffTime → (b → a → m b) → b → [a] → ExceptT (String, FoldResult b a) m (FoldResult b a) Source #
EndReached if and only if the entire input was consumed.
propFoldJustFoldl ∷ ∀ (m ∷ Type → Type) b a. (MonadMonotonicTime m, Eq b) ⇒ DiffTime → (b → a → b) → b → [a] → ExceptT (String, FoldResult b a) m (FoldResult b a) Source #
When foldAction is pure then it's just a 'Foldable.foldl'' on processed xs.