Temporal sequence operator: then ... within#

A binary pattern combinator X then Y within T that compiles to the standard "strictly after X, within T" timestamp constraint, plus an optional latest prefix for the common "after the most-recent X" case. This doc specifies the semantics; the author-facing grammar is [LGRA].

Paired with:

  • [LGRA] — the author-facing grammar.

  • [LLOG] — context for how timestamps on facts participate in LHS matching.

The construct [LTHE.2]#

[LTHE.2.1] then is a binary pattern combinator with a mandatory within T bound.

checks: idris×3

Semantics [LTHE.2.2]#

  • [LTHE.2.2.1] Match X, bind its slots.

checks: idris

  • [LTHE.2.2.2] Match Y such that Y.ts > X.ts (strictly after) and Y.ts - X.ts < T (within the bound).

    Both comparisons are on the facts' ts values — when the events happened — not on when the engine received them. A sequence's order and window are therefore event time, and the monotonic clock takes no part in them ([LTIM.2.8.2]). A fact delivered late still orders by its ts, and a wall clock step between two facts whose ts the engine filled can invert their order, as it can for any two ts values.

ratified — checks: idris×16 — see also: LTIM.2.8.2

  • [LTHE.2.2.3] If both match, the activation holds both bindings.

checks: idris×2

Chains [LTHE.2.3]#

[LTHE.2.3.1] then is left-associative: each within T applies to the then immediately before it.

checks: idris×3

[LTHE.2.3.2] Reads aloud: "unlock, then open within 10 seconds of unlock, then motion within 30 seconds of open."

Correlation [LTHE.2.4]#

[LTHE.2.4.1] A constraint across the patterns of a sequence is written as a step's where, which sees every earlier binding ([LGRA.8.5.10]).

ratified — see also: LGRA.8.5.10

[LTHE.2.4.2] No new binding mechanism; where clauses on compound patterns access all bindings in scope.

Cardinality — default all-matches, latest opt-in [LTHE.2.5]#

[LTHE.2.5.1] By default, a sequence pattern follows Rete's all-matches semantics: if two DoorOpen events both fall within the window before Motion, the rule fires twice (one activation per valid pair). This matches how the rest of DS handles multi-match patterns and composes with refraction.

ratified — checks: idris×9

[LTHE.2.5.2] Authors who want the intuitive "after the most recent X" semantics prefix the sequence with latest.

[LTHE.2.5.3] The anchor (first pattern) is pinned to its most recent match before the first follower: an anchor A matched with a follower B is excluded exactly when another anchor A′, with A.ts < A′.ts, meets the sequence's order and windows in A's place with the same B and the same later facts, and none of the anchor's own where and the followers' wheres, with A′ in A's place, answers false. So A′ must precede B strictly ([LTHE.5.2]). An A′ for which one of those wheres answers null and none answers false excludes A: "latest" cannot be concluded while a newer anchor's eligibility is unknown, as a negated pattern is blocked by a candidate whose condition is null ([LNUL.4.7]). An anchor that arrives after its follower, or for which any of those wheres answers false, excludes nothing; the outcome depends on timestamps and slot values, never on arrival order. Later patterns in the sequence continue to match all candidates.

If the natural reading of the rule is "after the last X, did Y happen?", use latest; if every X should count, leave it off.

ratified — checks: idris×15 — see also: LNUL.4.7, LTHE.5.2

[LTHE.2.5.4] latest modifies only the first pattern. Middle patterns fan out normally.

[LTHE.2.5.5] latest pins the anchor; later steps still fan out. With latest, the sequence's first pattern matches only the most recent fact of its schema. Each later step still matches every fact that falls in its window, so the rule fires once per complete chain rooted at that anchor: if two facts match the second step and each is followed by a fact matching the third, the rule fires twice. Refraction suppresses a re-fire on a chain the rule has already fired on.

checks: idris×3

Grammar impact [LTHE.4]#

[LTHE.4.1] A line-leading then continues the nearest positive pattern. The pattern-level productions, pattern, sequence_pattern and then_step, are [LGRA.8.1]'s. The grammar does not say which pattern a line-leading then follows, and this is stated here ([STYL.2.15.4]). A line made of then_steps continues the nearest pattern above it that is positive: a pattern_body line becomes the head of a sequence, and a sequence_pattern line gains the steps. The one-line form and the line-leading form mean the same sequence, and may be mixed. A line-leading then with no pattern above it is refused ([LTHE.4.8]), and so is one whose nearest pattern above is a no pattern ([LTHE.4.9]). A latest in a then_step is admitted by the grammar so that [LTHE.5.4] can refuse it with its own code. A step's where_clause belongs to that step ([LGRA.8.5.10]).

see also: LGRA.8.1, LGRA.8.5.10, LTHE.4.8, LTHE.4.9, LTHE.5.4

[LTHE.4.3] Lowering compiles to the existing timestamp idiom: for each consecutive A then B within T step, B.ts > A.ts and B.ts - A.ts < T. When latest is present, the compiler adds, after the sequence's last join, a negated condition: no A′ with A.ts < A′.ts that excludes A under [LTHE.2.5.3] — equivalent to selecting, per match of the followers, the maximum-ts anchor that precedes them. The construct needs no engine machinery beyond the latest pruning; it uses the same Rete joins and the same ts semantics. Pure surface form with minimal runtime surface.

see also: LTHE.2.5.3

[LTHE.4.4] See [LGRA] for the author-facing grammar.

[LTHE.4.5] ts slot requirement. Every schema participating in a then sequence — anchor and each follower — must declare a ts slot, which is a Time ([LFAC.9.1]). The lowering reads ?binding-ts to build the B.ts > A.ts predicate. The compiler must reject sequences where any participating schema lacks a ts slot with error code DS_VALIDATE_WITHIN_NO_TS_SLOT and a hint to add ts: Time to the schema.

ratified — see also: LFAC.9.1

What this lowers to [LTHE.4.6]#

For the two-step sequence DoorOpen d then Motion m within 30_s, the compiler emits a DoorOpen alpha node joined to a Motion alpha node, with the join carrying two predicates: m.ts > d.ts (strict ordering) and m.ts - d.ts < 30_s (the window bound). Both must hold for an activation to fire.

With latest DoorOpen d then Motion m within 30_s, the compiler adds a NOT sub-node after the Motion join, where both d and m are bound, blocking the match whenever a DoorOpen d2 excludes d with this m ([LTHE.2.5.3]): d.ts < d2.ts < m.ts, m.ts - d2.ts < 30_s, and neither the anchor's where nor the trailing where, with d2 in d's place, answers false.

Common non-fire causes:

  1. ts slot absent. Compile error DS_VALIDATE_WITHIN_NO_TS_SLOT. Fix: add ts: Time to every participating schema.

  2. ts not being updated. A logical fact's ts is the assertion time and does not change when the fact is no-op reasserted. A door that was opened hours ago still has that old ts. Sequences involving logical facts fire against the assertion timestamp, not "now." See [LTHE.5.5].

  3. Simultaneous timestamps. The predicate is strict: B.ts > A.ts. If A and B were asserted at the same engine tick (identical ts values), the rule does not fire. Adapters that assert events in batches can produce this; add a small ordering offset or use >= via a direct where clause if simultaneity is expected.

  4. Window too small for source granularity. If the adapter publishes readings at 5_s intervals and within T is set to 2_s, no sequence of two consecutive readings will ever match.

see also: LTHE.2.5.3, LTHE.5.5

[LTHE.4.7] within takes a positive duration. then ... within 0_s is a compile error (DS_VALIDATE_ZERO_WITHIN): a zero window admits no pair of distinct instants, so no sequence could ever match it.

ratified

[LTHE.4.8] A then needs a predecessor. then is a binary combinator that wires its follower to the preceding pattern's binding, so it may not open a rule's pattern list. A sequence whose first pattern is a then follower is a compile error (DS_VALIDATE_THEN_NO_PREDECESSOR).

[LTHE.4.9] A then may not follow a negated pattern. The follower reads its predecessor's ts, and a negated pattern binds no fact to read one from, so no X then Y within T is a compile error (DS_VALIDATE_THEN_AFTER_NEGATION).

then commitments [LTHE.5]#

  • [LTHE.5.1] Cardinality: all-matches by default, latest opt-in. The default follows Rete's standard all-matches semantics (one activation per valid sequence); latest pins the first pattern to its most-recent match for the "after the last X" case. See [LTHE.2.5].

ratified — see also: LTHE.2.5

  • [LTHE.5.2] Strict temporal order. Y.ts > X.ts — "then" in English implies afterwards, and simultaneity is usually a correlation bug (same event reported twice through different adapters) rather than a real sequence.

checks: idris

  • [LTHE.5.3] within T is mandatory. X then Y with no bound means "eventually Y after X" — unbounded temporal match, which requires keeping X bindings live indefinitely. Required within T prevents this; "effectively unbounded" is expressible with a large T when the author really means it.

ratified

  • [LTHE.5.4] latest modifies only the first pattern. Interior latest — one per step — is not available; a latest on any pattern but the first is a compile error.

ratified

  • [LTHE.5.5] Logical facts use assertion ts. A logical-schema pattern's ts is the assertion time. dwell and a pattern's within do not read ts; they measure from the fact's entry stamp ([LTIM.2.8.6]). A then step's within compares ts ([LTHE.2.2.2]).

checks: alloy — see also: LTHE.2.2.2, LTIM.2.8.6

What this doesn't close [LTHE.6]#

  • [LTHE.6.1] Count-based sequences. "X at least 3 times within 30_s" — series-space, not then-space. Series aggregations already cover part of this.

  • [LTHE.6.2] Branching sequences. "X, then either Y or Z within T" — compose via separate rules.

  • [LTHE.6.3] Absence-in-window without preceding event. "No Y for 30_s straight" — different pattern; use series boolean aggregation.

  • [LTHE.6.4] Exact-interval matching. "Y exactly 30_s after X" — then Y within 30_s fires as soon as Y arrives; to enforce "no earlier than 30_s" add a timestamp predicate in where.