Modeling dated history tables (util/history)
Many designs keep an append-only table of dated rows (policy changes, prices,
memberships, effective dates) and look up βthe value in force at time tβ.
Elinvar ships a small built-in module util/history[Tick] for this pattern.
Opening util/history
open util/history[Tick]
sig Tick {}
enum Policy { Open, Closed }
sig Entry { at: one Tick, policy: one Policy }
sig Read { when: one Tick, saw: one Policy }
Opening util/history[Tick] automatically opens util/ordering[Tick] on the
same clock signature Tick, so first, next, lt, lte, gt, and gte
are available immediately.
Helpers provided
| Name | Meaning |
|---|---|
at_or_before[t1, t2] | t1 <= t2 in the clock order |
before_tick[t1, t2] | t1 < t2 in the clock order |
rows_at_or_before[rows, at, t] | every row in rows whose at is <= t |
latest[rows, at, t] | the row(s) in rows with the greatest at <= t (inclusive) |
latest_strict[rows, at, t] | the row(s) in rows with the greatest at < t (exclusive) |
same_tick_conflict[rows, at, t] | whether a row in rows was written at the exact same tick t |
monotonic_append[rows, at, t] | whether every row in rows has at <= t |
The same-tick pitfall
When a read and a policy change happen in the same clock tick (for example, the
same Unix second), latest[Entry, at, t] and latest_strict[Entry, at, t]
disagree:
latest(<= t) includes the change written at tickt, so a read in the same second sees the new policy.latest_strict(< t) ignores changes at tickt, so a read in the same second sees the previous policy.- If two entries share the same tick
t,latestreturns both rows unless a uniqueness rule forbids two entries for the same key at the same tick.
Checking a promise like:
assert SameTickAgrees {
all r: Read | latest[Entry, at, r.when].policy = latest_strict[Entry, at, r.when].policy
}
check SameTickAgrees for 3
immediately produces a minimal counterexample where an Entry and a Read
occur at the same Tick, forcing an explicit decision in the specification
before code is written.