schedule-0.0: Schedule sub-computations to run later, in a pure way

Safe HaskellNone
LanguageHaskell2010

Control.Clock

Description

Pure abstractions for time and clocks.

Synopsis

Documentation

type Tick = Sum Integer Source

The smallest discrete unit of time, in a pure scheduled computation.

checkNonNeg :: (Num a, Ord a, Show a) => a -> a Source

Check for a non-negative number.

checkPos :: (Num a, Ord a, Show a) => a -> a Source

Check for a positive number.

tickOp :: (Real a, Real b) => (Rational -> Rational -> Rational) -> a -> b -> Tick Source

Apply a binary operation to two real numbers, returning a Tick.

data Clock c Source

A maybe-impure supplier of time, to a pure scheduled computation.

The type c is the computational context where clock operations occur, e.g. a Monad such as IO.

Clock implementations must be monotic. See System.Time.Monotonic for an example on how to wrap non-monotonic clocks. TODO: provide a generic monotonic wrapper.

Constructors

Clock 

Fields

clockNow :: c Tick

Get the current time.

clockDelay :: Tick -> c ()

Suspend the current computation for a given number of ticks.

Nothing else in the computation runs until the suspension is over. Afterwards, clockNow will give the expected value, i.e. for all n:

do
    old <- clockNow
    clockDelay n
    new <- clockNow
    let new' = assert (old + n <= new) new

The relation is <= not ==, because the computer might have slept during the mean time or something. On the other hand, if the underlying physical clock might delay for a shorter period than requested, then implementations of this function must loop-delay until the <= condition is satisfied.

The above is the only condition that scheduled computations should rely on, and any actual physical real delay is up to the implementation.