Engine

3.6. Invariants🔗

An invariant says what must always be true of the events of a key: the engine rejects an event that would break it. A rule that needs another entity or the incoming event is a precondition. The account's balance may never be negative:

    - always: sum(MoneyDeposited.amount) - sum(MoneyWithdrawn.amount) >= 0

A withdrawal of 101 from an account holding 100 is rejected: Withdrawing 101 from an account holding 100 is rejected. . The same file's rule 1 rejects it in the full account example (A withdrawal above the balance is rejected. ).

🔗inductive type
Spec.Inv : Type
Spec.Inv : Type

Invariants.

Spec.Inv.always (c : CondExpr) : Spec.Inv

always: condition, on every key

Spec.Inv.grouped (q : Quantity) (per : List Per)
  (values : List (String × ValueExpr))
  (active : Option CondExpr) : Spec.Inv

always: quantity per: ... values: ... active: ...

3.6.1. Always true🔗

An active invariant holds at every position: no stored state of the entity breaks it, and it reads only the entity's own events.

3.6.2. Lower bounds🔗

A lower bound on a count needs a state to wait for: "a team has at least 2 members" is false for a new team, so it is written as "once activated, at least 2", with a precondition that activation needs 2.

3.6.3. Across keys🔗

A grouped invariant counts keys per group instead of events: here, one user per email, whatever the case, as long as the user is not deleted.

    - always: at most 1
      per: email
      values:
        email: lower((last of [UserCreated, UserEmailChanged]).email)
      active: never UserDeleted

A second user whose email differs only in case (ann@X.ch and Ann@x.ch) is rejected (A second user whose email differs only in case is rejected. ). Two users can swap emails in one batch, through a temporary address (Two users can swap emails in one batch through a temporary address. ).

🔗def
Spec.groupSize (world : World) (per : List Per) (values : List (String × ValueExpr)) (active : Option CondExpr) (store : List Event) (g : List Value) : Nat
Spec.groupSize (world : World) (per : List Per) (values : List (String × ValueExpr)) (active : Option CondExpr) (store : List Event) (g : List Value) : Nat

Number of keys of store in group g.

🔗def
Spec.storeGroups (world : World) (per : List Per) (values : List (String × ValueExpr)) (active : Option CondExpr) (store : List Event) : List (List Value)
Spec.storeGroups (world : World) (per : List Per) (values : List (String × ValueExpr)) (active : Option CondExpr) (store : List Event) : List (List Value)

The groups that occur in a store: the groups of each of its keys.