Logical facts: truth-maintained derived facts#
The logical emit keyword:
what emit X logical observably does, how truth maintenance
interacts with patterns authors typically write, and the
correctness concerns authors need to internalise.
Paired with:
[LGRA] for the
logical/ttlschema lifecycle keywords.[LFAC] for the two lifecycle categories and the no-op reassertion discipline
logicalemit correctness depends on.
What this is not#
Not a truth-maintenance-system primer. This doc says what DS guarantees, not how the underlying engine implements its truth maintenance.
Not a debugger-protocol spec. Operator observability of logical facts lives in the runtime's debug surface; this doc names what must be observable, not how.
Not a cross-tier mechanism spec. Cross-tier logical fact propagation lives in [RARC].
What emit X logical observably does [LLOG.1]#
[LLOG.1.1] When a rule's RHS uses emit X logical, the asserted fact's
lifetime is tied to the matching LHS. The fact lives while the
LHS continues to match and retracts automatically when it stops.
ratified
[LLOG.1.2] Occupied is asserted when the motion sensor reports "on". It
stays asserted for as long as the sensor continues reporting
"on". When the sensor's state changes (say, to "off"), the
LHS stops matching; the Occupied fact retracts automatically.
The rule's RHS does not run on the retract — retraction is a
maintenance operation, not a rule firing.
checks: souffle×3
[LLOG.1.3] Hard requirement: X must be declared with the logical
schema lifecycle. emit X logical against a ttl-declared
schema is a compile error. See [LGRA]
and [LFAC.1].
see also: LFAC.1
Multi-justifier semantics [LLOG.2]#
[LLOG.2.1] The property authors need to internalise most. Several rules may emit the same logical fact independently, each from its own LHS.
ratified
[LLOG.2.2] Each activation is tracked as a separate justification. The fact is:
[LLOG.2.3] Asserted by whichever rule activates first.
[LLOG.2.4] Kept alive by any active justification.
ratified
[LLOG.2.5] Retracted only when the last justification goes away.
ratified — checks: tla×3 souffle×6 idris×66
[LLOG.2.6] The author's mental model is "the fact exists while any evidence supports it." Usually the right semantics for derived state. The subtlety is that it's not last-writer-wins, and not rule-exclusive — it's a union over all justifiers.
Identical vs distinct facts [LLOG.2.7]#
[LLOG.2.7.1] Identical slot values coalesce. Two rules emitting a logical fact of the same schema with the same slot values produce one fact with multiple justifiers.
ratified
[LLOG.2.7.2] Distinct slot values don't coalesce. Two rules emitting logical facts of the same schema with different slot values produce two separate facts with independent lifetimes. This is the key-all (relation) case of the declared-key model ([LFAC.1.7.2]): with every slot marked
key, distinct tuples are distinct identities and coexist, while identical tuples still coalesce per [LLOG.2.7.1].
ratified — see also: LFAC.1.7.2, LLOG.2.7.1
[LLOG.2.7.3] Authors using slots to differentiate per-source state get independent lifetimes automatically. Authors emitting slotless state facts get the coalesced union.
ratified
[LLOG.2.7.4] A multi-row relation passes the containers gate ([LCNT.2.1]). The
key = allrelation of [LLOG.2.7.2] / [LFAC.1.7.2] is a set of distinct facts in working memory, each matched individually by the engine's bounded, non-authored match loop — a container, not a collection. The binding constraint: DS must not grow a surface that hands the author the row-set as an iterable or cursor value. Row aggregation goes through closed reductions (series /count); set-materialisation goes across the adapter boundary. The relation is the fact population, never a value.
see also: LCNT.2.1, LFAC.1.7.2, LLOG.2.7.2
[LLOG.2.7.5] Distinct values under one key supersede, and the superseded fact's support leaves with it. When an activation emits a
logicalfact whose key matches a live fact and whose other slots differ, the new fact supersedes the old ([LFAC.1.7.2], [LFAC.2.5.3]). The engine must withdraw the old fact's justifications with the old fact. It must not transfer them to the new fact or keep them for later use. The new fact then lives on its own justifications alone ([LLOG.2.5]).So when the new fact's last justification goes, the key holds no fact, even if the activation that derived the superseded value still matches. That activation has already fired, and it does not fire again while its match holds.
A program that means both values to hold marks the differing slot
key, so the two are distinct facts ([LLOG.2.7.2]).
ratified — see also: LFAC.1.7.2, LFAC.2.5.3, LLOG.2.5, LLOG.2.7.2
Implications [LLOG.2.8]#
[LLOG.2.8.1] To express exclusive assertion (only one source at a time), use slots to differentiate. Don't rely on "last writer wins" — that's not how logical facts work.
[LLOG.2.8.2]
withdrawdoes not reach alogicalfact ([LLOG.11.8]). Alogicalfact's lifetime belongs to the justifying activations, not to explicit author control. See [LLOG.8].
The tick problem [LLOG.3]#
[LLOG.3.1] A time-dependent test (now(), dwell, a within window, a series
read; [LTIM.2.4.1]) can go from true to false while no fact changes. Evaluated only on fact change, such a
test would keep its activation, and the logical fact that activation
justifies, alive after the test has gone false. [LLOG.3.11.1] is how the engine
prevents that.
see also: LLOG.3.11.1, LTIM.2.4.1
The engine-side resolution [LLOG.3.11]#
[LLOG.3.11.1] The engine must re-evaluate every time-dependent test at
each pump, under that pump's clock sample ([LLOG.11.2], [LTIM.2.4.2]). A match
the new sample refuses ends, and the logical facts it justified are retracted
at that pump ([LLOG.15.1]). The tick that drives this is engine-internal
([LDET.5.8]), and an author writes nothing for it.
ratified — checks: idris×5 — see also: LDET.5.8, LLOG.11.2, LLOG.15.1, LTIM.2.4.2
[LLOG.3.11.3] A time-bound logical fact is accurate to one pump: one whose
justification lapsed between pumps is retracted at the next pump.
ratified
Negation under logical [LLOG.4]#
[LLOG.4.1] A logical fact whose rule's LHS contains a negated pattern
(no …) is justified in part by the absence that pattern matches.
ratified
[LLOG.4.2] While the absence persists, the fact lives. When a fact the negated pattern matches is asserted, the pattern fails, its activation ends, and the logical fact retracts.
ratified
[LLOG.4.3] This is genuinely what authors want for "quiet house" / "armed-and-empty" / "no one home" idioms. The dependency graph of such a fact describes a negated justification, which looks different in debugger output from positive dependencies — worth knowing, not avoidable.
ratified
Test CEs (arithmetic, aggregations) [LLOG.5]#
[LLOG.5.1] Arithmetic and series aggregations inside a where clause work
under logical.
ratified
[LLOG.5.2] The
Sensorfact pattern is the logical match; its existence justifiesAnomaly.
[LLOG.5.3] The
wheretest is re-evaluated each time the sensor's slot values change (and each engine tick, per [LLOG.3]).
see also: LLOG.3
[LLOG.5.4] When a new sample makes the test false (state back within range), the activation ends;
Anomalyretracts.
[LLOG.5.5] Series aggregations compose the same way — `where mean(t)
threshold` is a test over the sensor fact's slot stream; the aggregation re-evaluates on slot change. No special handling needed.
No-op reassertion dependency [LLOG.6]#
[LLOG.6.1] emit X logical correctness depends on the no-op
reassertion discipline spec'd in [LFAC.2]. Rule authors don't
need to do anything to get it — the engine's dedup path is the
contract (see [LFAC.2.5.1]).
ratified — see also: LFAC.2, LFAC.2.5.1
[LLOG.6.2] The one knob exposed to authors is slot-declared
tolerance on numeric slots (see
[LGRA]), for noisy sensors
where "meaningful change" means something other than exact
equality.
Interaction with hydrated facts [LLOG.7]#
Deferred: not binding in this edition.
[LLOG.7.1] A hydrated fact lives for its hydration's retain: window. A
logical fact whose justifying LHS matches hydrated facts therefore lives no
longer than they do: when the window ends and they expire, its justification
ends and the engine retracts it ([LLOG.15.1]).
see also: LLOG.15.1
[LLOG.7.4] Rule of thumb: the effective lifetime of a logical fact is
bounded above by the shortest-lived element in its LHS. Tune
retain: and logical patterns together.
Explicit retract of logical facts [LLOG.8]#
[LLOG.8.1] The lifetime of a logical fact belongs to its justifications,
not to the author. Explicitly retracting one works at runtime
but the next activation pass re-asserts it — observable as
flicker.
[LLOG.8.3] The diagnostic is a warning rather than an error because cleanup-at-shutdown patterns are legitimate.
Cost of logical facts [LLOG.10]#
[LLOG.10.1] Logical emits carry modest extra bookkeeping compared to plain
emit:
[LLOG.10.2] Per-fact: a justification list (small, constant per justifying rule).
[LLOG.10.3] Per-change: an extra pass through the justification graph on retract.
[LLOG.10.5] Authors should know the rough cost so the logical vs ttl
schema choice is informed: state-like facts that benefit from
truth maintenance use logical; event-like facts that
accumulate use ttl.
Justification linkage: what truth maintenance covers [LLOG.15]#
checks: alloy
[LLOG.15.1] Justification linkage is established where the fact is asserted,
not inherited from where the fact came from. A fact asserted against a
scope logical schema (emit X logical) is TMS-linked,
at the asserting tier, to its justifier activation(s) ([LLOG.1] /
[LLOG.2]); the engine must retract it when the last justifier's LHS no longer
matches. "No longer matches" includes a positive pattern's within-recency
window lapsing and a no-negation being broken (the cascade case) — the same
justification withdrawal, whatever ends the match. The cross-tier receiving
side takes its lifecycle from the receiving schema ([LTIE.9.4], [LLOG.11.7]);
this clause governs only the local assertion.
A match that ends inside its own firing ends its justification there.
An action of the firing can end the activation's
own match: a withdraw of one of its bindings, an emit that replaces a fact it
matched ([LFAC.2.5.3]), or an emit that breaks one of its no patterns. From
that action on the activation justifies nothing. A logical fact the firing
emitted before that action loses this justification there, and the engine must
retract it if no other holds it ([LLOG.2.5]). A logical fact the firing emits
after that action gains no justification from it, and the engine must retract
it as it is made unless another justification already holds it. [LLOG.11.9]
refuses the shapes a compiler can see. This binds every other, such as an emit
that replaces the matched fact only because two values are equal at run time.
The logical flag restates the schema's lifecycle; it does not select it.
The author must write the flag on an assertion against a scope logical schema
and must not write it on any other ([LLOG.11.1]), so the assertion site carries
no per-fact choice. What makes the linkage local is the tier the assertion
happens in, not anything the emit site decides.
ratified — checks: alloy idris×7 — see also: LTIE.9.4, LFAC.2.5.3, LLOG.1, LLOG.11.1, LLOG.11.7, LLOG.11.9, LLOG.2, LLOG.2.5
[LLOG.15.2] A fact not asserted with the logical flag carries no TMS
justification link. A ttl event, an explicit fact, an activation-scoped
fact ([LSCP.3]), or a synthetic after-timer fact (asserted plainly, [LTAC.3.3])
lives by its own declared lifecycle — TTL expiry, activation completion, explicit
retract, or one-shot timer firing — and the engine must not retract it on the
withdrawal of whatever asserted it. An after-timer's only cancellation channel is
as-keyed supersedure ([LTAC.2.2.1]), never justifier withdrawal.
The three surfaces that touch a derived or timer fact's justification lifecycle —
the after-timer, the within-window logical retract, and the event-trace order of
a truth-maintenance cascade against its trigger — are one principle, not three
behaviours: [LLOG.15.1] fixes what is truth-maintained, [LLOG.15.2] what is not.
ratified — checks: alloy×2 — see also: LLOG.15.1, LSCP.3, LTAC.2.2.1, LTAC.3.3
[LLOG.15.3] A rule ends a justification it depends on only by a flip the cycle check refutes. A logical fact is retracted when its last justifier's match ends ([LLOG.15.1]). A rule that reads the logical fact and ends that match has an edge back into the justifying rule unless the check drops it:
An emit the literal break refutes compiles, unless it replaces a fact the rule itself matched, which [LLOG.11.9] refuses. It cannot trigger the justifier, so the edge drops ([LCYC.6.1]). What its firing leaves is [LLOG.15.1]'s, and the rule reads the fact whose justification it ends.
Any other way of ending the match is refused. A
withdrawof the fact the justifier matches ([LCYC.3.2.2]), or an emit whose value the break cannot compare, keeps the edge. Both rules are then in one SCC with the logical fact emitted and matched inside it, and the compiler refuses the program withDS_VALIDATE_RULE_CYCLE([LCYC.3.1.1]). Truth maintenance earns no exception: that the retraction would end the loop is not an argument the cycle check takes ([LCYC.3.5.1]).
A refused program, and a transition that must outlast the rule that makes it,
have two shapes that compile. A state that holds exactly
while a condition holds is itself a logical fact: emitted logical from the
condition, it is retracted when the condition stops matching, and no rule
turns it off. A transition no condition justifies enters from outside the
engine: the host or an adapter asserts the changed fact, which is no edge of
the rule graph, and the cascade follows it as it follows any input. A loop
closed through an adapter is the adapter's to keep finite ([LCYC.7.1]).
ratified — see also: LCYC.3.1.1, LCYC.3.2.2, LCYC.3.5.1, LCYC.6.1, LCYC.7.1, LLOG.11.9, LLOG.15.1
Logical-fact commitments [LLOG.11]#
[LLOG.11.1] Logical lifetime is a schema-level property, declared on the schema header (
schema X scope logical:). Not a per-rule opt-in.emit X logicalrequiresXto be declaredlogical, and anemit Xof a schema declaredlogicalrequires thelogicalkeyword; either mismatch isDS_VALIDATE_BAD_LIFECYCLE_FLAG. Declaring it per schema keeps the lifetime visible at the declaration and prevents accidental mixing.
ratified
[LLOG.11.2] Tick-based re-evaluation for time-bound LHS tests. The engine re-evaluates every time-dependent test at each pump, under that pump's clock sample ([LTIM.2.8.4]). A match the new sample refuses ends, and the logical facts it justified are retracted ([LTIM.2.5.1], [LTIM.2.7.2]). Resolution is one pump: the integrator's pump cadence decides it, and the engine has no tick interval of its own. Restates [LTIM.2.5.1].
ratified — see also: LTIM.2.8.4, LTIM.2.5.1, LTIM.2.7.2
[LLOG.11.3] No-op reassertion discipline is load-bearing for logical stability. The engine is the primary enforcement layer ([LFAC.2.5.1]); adapter-side suppression is an optimisation.
ratified — see also: LFAC.2.5.1
[LLOG.11.4] Multi-justifier union semantics for logical facts with identical slot values. Use slots to differentiate when per-source independent lifetimes are wanted.
ratified
[LLOG.11.5] Negation under
logicalis supported with absence-justified semantics.
ratified — checks: idris×2
[LLOG.11.7] A
logicalfact's justification does not cross a tier. Alogicalfact published to another tier arrives as its schema name and slot values, and the receiving tier asserts it under its own declaration of that schema ([LTIE.9.4]). Truth maintenance is per-tier ([LTIE.4.6.2]): the receiving tier holds one fact per identity, justified by nothing in the publishing tier.
ratified — see also: LTIE.4.6.2, LTIE.9.4
[LLOG.11.8]
withdrawof alogicalbinding is a compile refusal.withdrawisscope explicitorinternalonly ([LSCO.4.5.9]); alogicalfact's lifetime belongs to its justifying activations ([LLOG.2.8.2]), and a refcount that decrements past the withdrawal can land on a fact no longer live, corrupting the truth-maintenance cascade rather than merely flickering.
ratified — see also: LLOG.2.8.2, LSCO.4.5.9
[LLOG.11.9] A
logicalemit whose activation ends inside its own firing is a compile error (DS_VALIDATE_LOGICAL_EMIT_SELF_ENDING). The body of anafterclause is such a place, and the compiler must refuse alogicalemit there: the body runs in the follow-up rule, which matches the timer fact and retracts it ([LTAC.3.4]). The timer is that rule's only pattern, so the match is gone before the firing ends.So is a rule's RHS after a
withdrawof one of its bindings.withdraw bremoves a fact the activation matched ([LSCO.4.5.9]), so the match is gone from that action on. The compiler must refuse alogicalemit that follows awithdrawin the same RHS, at the emit, naming thewithdraw. An emit before thewithdrawis not refused: its fact is justified while the match holds, and thewithdrawends that justification ([LLOG.15.1]). A body nested in a later action is not this RHS. It runs in a rule of its own, a continuation rule ([LCCO.5.2]) or anafterfollow-up, and thewithdrawdoes not end that rule's match; [LGRA.10.1.3] says what such a body may read.So is a rule's RHS from an emit that replaces a fact the activation matched. Take an emit of schema
Xwhere the rule bindsbto a positiveXpattern, every identity-key slot of the emit ([LFAC.1.7]) is the same slot ofb(id: b.id), and some other slot of the emit is a literal that differs from the literal the pattern requires of that slot, as [LCYC.6.1.2] compares them. That emit is a meaningful change tob's fact and replaces it ([LFAC.2.5.3]), so the match is gone from that action on, as afterwithdraw b. The compiler must refuse such an emit when it islogical, and alogicalemit that follows such an emit in the same RHS, at the emit, naming the one that replaced the fact. A replacement that only run-time values decide is not refused here; [LLOG.15.1] governs it.A fact emitted in any of these places is withdrawn as it is made: the activation that justified it has ended, so justification withdrawal retracts it in the same firing ([LLOG.15.1]). Accepting the program would give an author a fact that never exists, or — on a runtime that skipped the withdrawal — one that no live activation supports and nothing ever retracts.
What the author writes instead. A plain
emitis not the answer: a plain emit requires attlschema ([LFAC.1.3.1]), andemit X logicalrequiresXto be declaredlogical([LLOG.11.1]), so the choice is between the two lifetimes rather than between two spellings of one. The derived fact is expressed by emitting attlfact from the deferred body and joining it to the condition that should keep the logical fact alive: a second rule matching both emits thelogicalfact, and its justifying activation is that rule's, which lives exactly as long as the join holds. After awithdraw, the fact the author wants is likewise attlorexplicitfact, or alogicalfact that another rule's join justifies.
ratified — see also: LCCO.5.2, LCYC.6.1.2, LFAC.1.3.1, LFAC.1.7, LFAC.2.5.3, LGRA.10.1.3, LLOG.11.1, LLOG.15.1, LSCO.4.5.9, LTAC.3.4
Test obligations [LLOG.14]#
[LLOG.14.2] A conforming implementation must exercise
LLOG.1 (emit-logical semantics), LLOG.2 (multi-justifier
coalescence), and LLOG.2.7.2 (distinct slot values stay
independent) against its own logical-emit (is_logical) lowering.
see also: LLOG.1, LLOG.2, LLOG.2.7.2