Facts and values#
Asserting and retracting#
assert_fact#
:- pred assert_fact(string::in, list(storable)::in, assert_step::out, session::in, session::out) is det.
Asserts a fact of the named schema, one value per slot in declaration order: the capability of ds_engine_assert.
The fact’s deadline is its schema’s TTL. The first row that applies is the refusal.
|
Condition |
|---|---|
|
the session has halted |
|
no schema a host can see has this own name |
|
the list is not one value per slot |
|
a value’s constructor is not one its slot takes; a |
|
a |
|
a |
|
a |
|
a |
|
a |
|
a |
|
the fact’s deadline, or the deadline this assert’s first sample would give a held cache seed, is outside the |
|
a |
A json(j_null) in a nullable Json slot is stored as null.
Every row but contract_breach leaves assert_step_reports free of stream events. A contract_breach reports the input’s fact_assert and a bounds_error.
If the call halts the engine, the outcome is error with engine_halted, whatever else it did.
:- pred assert_reading(assert_outcome::out, session::in, session::out) is det.
assert_reading(Outcome, !S) :-
assert_fact("Reading", [text("a"), float(1.5), int(1i64), text("x"), bool(yes), int(0i64)], Step, !S),
Outcome = Step ^ assert_step_outcome.
(Source: bindings/mercury/src/mercury_ds.session.m, runtime/mercury/src/ds.engine.m, runtime/mercury/src/ds.fact.m, runtime/mercury/src/ds.wm.m, Spec: [NFCT.2.11], [NFCT.2.3], [LNUL.3.4], [NCBK.2.9], [LTIM.10.9], [LSTR.11.3], [LSER.13.14], [LGRA.5.8.10])
assert_step#
:- type assert_step ---> assert_step(assert_step_outcome :: assert_outcome, assert_step_reports :: list(report), assert_step_duties :: list(duty)).
What assert_fact did, reported and left the host to do.
assert_step_reports holds reports, in the order the call emitted them. assert_step_duties is the duty for each cache expiry the assert’s own reads met, in the order it produced them.
(Source: bindings/mercury/src/mercury_ds.session.m)
assert_outcome#
:- type assert_outcome ---> asserted(fact_id) ; unchanged(fact_id) ; replaced(fact_id, fact_id) ; error(ds_error).
What an assert did to working memory, as Reassertion states.
Member |
Meaning |
|---|---|
|
a new fact entered |
|
the same fact was live, and working memory is unchanged; |
|
|
|
a |
(Source: bindings/mercury/src/mercury_ds.session.m, runtime/mercury/src/ds.engine.m, Spec: [RFCS.6.7])
retract_fact#
:- pred retract_fact(fact_id::in, step::out, session::in, session::out) is det.
Retracts a live fact: the capability of ds_engine_retract.
|
Condition |
|---|---|
|
the session had halted, or this call halted the engine |
|
no live fact has this id |
(Source: bindings/mercury/src/mercury_ds.session.m, Spec: [NCBK.2.9])
assert_external#
:- pred assert_external(bitmap::in, step::out, session::in, session::out) is det.
Asserts a fact published by another tier, from a FactExternal’s bytes: the capability of ds_engine_assert_external.
The runtime’s encode_fact_external writes the bytes from a publish’s fact_external. Each fact it asserts takes this engine’s own event id, and its lifetime from this program’s schema. A reference equal to a live fact is that fact.
Refused with nothing asserted:
|
Condition |
|---|---|
|
the session has halted |
|
the bytes are not a |
|
the engine holds no schema of this own name that a record may name |
|
a schema of the same name has another shape |
|
a value is not one its slot takes |
|
a |
|
a |
|
a reference meets a live fact with its key and other values |
|
a |
|
a |
|
a |
Only a halt an assert raises stops the delivery after these checks. The outcome is error with engine_halted, and a fact already asserted is not taken back. The state after the halt is as Halt latch states.
(Source: bindings/mercury/src/mercury_ds.session.m, runtime/mercury/src/ds.fact_receive.m, runtime/mercury/src/ds.wire.m, runtime/mercury/src/ds.json_text.m, Spec: [NCBK.10.4], [RFCS.23.7], [NLFC.9.5], [LSTR.1.5], [LSTR.11.3], [LTIM.10.9], [RFCS.6.7], [LERR.1.4.7], [LGRA.24.3])
Values#
storable#
:- type storable =< ds_value ---> int(int64) ; float(float) ; text(string) ; bool(bool) ; bytes(bitmap) ; json(json_term) ; fact_ref(fact_id) ; null.
A value a slot holds: the subtype of the runtime’s ds_value with the constructors a slot takes. A value read back is one too.
Slot’s declared type |
Constructor |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
a schema’s name |
|
any, declared nullable |
|
(Source: bindings/mercury/src/mercury_ds.value.m, runtime/mercury/src/ds.fact.m)
to_value#
:- func to_value(storable) = ds_value.
The runtime’s value a storable is.
(Source: bindings/mercury/src/mercury_ds.value.m)
json_from_text#
:- func json_from_text(string) = maybe_error(json_term, ds_error).
A Json value from its text, read with the grammar and limits of ds_json_parse.
The first row that applies is the refusal.
|
Condition |
|---|---|
|
the text passes the text limit |
|
the text is not well-formed UTF-8 |
|
the value passes the depth, node or string-byte limit |
|
the text is not one JSON document: its syntax, a repeated key, an integer outside |
:- func json_canonical(string) = maybe_error(string, ds_error).
json_canonical(Text) = Result :-
Parsed = json_from_text(Text),
( Parsed = ok(J), Result = json_to_text(J)
; Parsed = error(E), Result = error(E) ).
(Source: bindings/mercury/src/mercury_ds.value.m, Spec: [LSTR.8.5], [LGRA.5.8.10])
json_to_text#
:- func json_to_text(json_term) = maybe_error(string, ds_error).
A Json value’s text, byte for byte what ds_json_serialize writes.
A value holding an infinite or NaN float is refused with json_nonfinite.
(Source: bindings/mercury/src/mercury_ds.value.m)
Working memory#
fact_view#
:- type fact_view ---> fact_view(fact_view_id :: fact_id, fact_view_schema :: string, fact_view_slots :: list(storable), fact_view_asserted :: instant, fact_view_deadline :: maybe(instant)).
A fact as a host reads it, through the capability of the ds_fact_get_* calls: a snapshot taken at the read.
Field |
Holds |
|---|---|
|
the fact’s id, as |
|
the schema’s own name, as |
|
the slot values, in declaration order |
|
when the fact entered working memory |
|
when it expires, or |
(Source: bindings/mercury/src/mercury_ds.value.m, runtime/mercury/src/ds.id.m, runtime/mercury/src/ds.wm.m)
fact_count#
:- func fact_count(session) = read(int).
How many live facts a host can see: the capability of ds_engine_fact_count.
(Source: bindings/mercury/src/mercury_ds.session.m, runtime/mercury/src/ds.engine.m)
facts#
:- func facts(session) = read(list(fact_view)).
Every live fact a host can see: the capability of ds_engine_for_each_fact.
The list runs schema by schema, in schema-id order, and each schema’s facts in insertion order.
(Source: bindings/mercury/src/mercury_ds.session.m, runtime/mercury/src/ds.engine.m, runtime/mercury/src/ds.wm.m)
search_fact#
:- pred search_fact(session::in, fact_id::in, read(fact_view)::out) is semidet.
The live fact with this id: the capability of ds_engine_fact_find.
It fails if a host can see no live fact with this id, and answers halted after the halt, whatever the id.
(Source: bindings/mercury/src/mercury_ds.session.m)
lookup_fact#
:- func lookup_fact(session, fact_id) = read(fact_view).
search_fact, throwing an exception where that fails.
(Source: bindings/mercury/src/mercury_ds.session.m)