ouroboros-consensus
Safe HaskellNone
LanguageHaskell2010

Ouroboros.Consensus.Util.TimeBoundedLoop

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

Iteration

iterateUntilOrTimeout Source #

Arguments

∷ MonadMonotonicTime m 
⇒ DiffTime

Wall-clock budget

→ (a → Bool)

End predicate; when True, stops with EndReached

→ (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

data DoneType Source #

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.

Instances

Instances details
Eq DoneType Source # 
Instance details

Defined in Ouroboros.Consensus.Util.TimeBoundedLoop

Methods

(==) ∷ DoneType → DoneType → Bool #

(/=) ∷ DoneType → DoneType → Bool #

Show DoneType Source # 
Instance details

Defined in Ouroboros.Consensus.Util.TimeBoundedLoop

Methods

showsPrec ∷ Int → DoneType → ShowS #

show ∷ DoneType → String #

showList ∷ [DoneType] → ShowS #

type StepCount = Int Source #

Number of stepAction invocations performed.

iterateUntilOrTimeout_ Source #

Arguments

∷ MonadMonotonicTime m 
⇒ DiffTime

Wall-clock budget

→ (a → Bool)

End predicate; when True, stops with EndReached

→ (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.

foldOrTimeout Source #

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.