Playground Documentation Challenges Full Docs

Label Commands (+, -, *, ^, expect)

Set operations on label collections and the check expect

Overview

Labels in Formall hold collections (packages) of statements. The standalone label commands +, -, *, ^ perform set-like operations on these collections. They take one or more arguments and produce a new collection of statements that can be captured by a label.

+(statementSource1, statementSource2, ...)-(statementSource1, statementSource2, ...)*(statementSource1, statementSource2, ...)^(statementSource1, statementSource2, ...)expect(left -> right; … | statementSource1, ...)expect(left -> right; …)
statementSource
A statement label or a nested command returning statements.
left -> right, …
expect only, at most one: the expected set, read as the statements of if (without -> — the value of notation("default")). Alone, it names visible statements: an error if one is not visible here, otherwise they are returned as a label.
+ − * ^ expect
Union, difference, intersection, symmetric difference of the sets of statements; expect checks that all sources are equal sets (an error shows the difference) and returns that set. Inclusion is written by composition: expect(| *(A, B), A).

Command Properties

Group
Labels
Produces
Statements (result of set operations on statement collections)
Delivery & Label
Into the current scope. A label on a label command captures the resulting statement collection at this scope.
Creates scope
No.
Mechanism
Processes arguments left to right. The first argument initializes the result (copied in full). Each remaining argument is combined using the set operation (+ union, - subtract, * intersect, ^ symmetric difference). Arguments are labels or commands returning statements.

Basic examples

Union: +(first, second, ...)

Initializes the result with the first argument, then for each remaining argument adds its statements to the result, skipping those already present:

Union of two labels

▶ Run

Subtract: -(first, second, ...)

Initializes the result with the first argument, then for each remaining argument removes from the result any statements that match:

Subtraction of labels

▶ Run

Intersect: *(first, second, ...)

Initializes the result with the first argument, then for each remaining argument keeps in the result only those statements that also appear in that argument:

Intersection of labels

▶ Run

Symmetric Difference: ^(first, second, ...)

Initializes the result with the first argument, then for each remaining argument performs a symmetric difference (XOR) with the accumulated result. For each statement in the current argument:

  1. If the statement exists in the result (as it was before processing this argument), it is removed.
  2. If the statement did not exist in the result, it is added.

After processing an argument, the result contains only statements that were in exactly one of the two sets. When there are more than two arguments, this process repeats — the next argument is XORed with the accumulated result, and so on.

Symmetric difference of labels

▶ Run

How It Works

Label commands process arguments left to right. The first argument initializes the result collection (copied in full, unaffected by the command symbol). Then each remaining argument is combined with the accumulated result using the set operation indicated by the command symbol.

With a single argument, all four commands produce the same result — a copy of that argument's statements. With multiple arguments, each one is folded into the result in order.

💡
The result of a label command is itself a statement collection that can be captured by a label: +L: +(A, B) declares L and fills it with the union of A and B.

Arguments

Each argument is a source of statements:

  • Label — a label name; uses the statements accessible from that label (filtered by scope visibility, so only statements reachable from the current scope are included)
  • Anything that gives statements in place — a proof <…>, a call T[…], a command (decide(X), expect(…), another label command)
  • The statements of expect — before the bar expect takes one set of statements, read as in if: expect(P -> true; Q -> false | L) checks that L holds exactly these; alone, expect(P -> true) checks that each is visible here

At least one argument is required. If no arguments are provided, the interpreter reports an error.

Statements Named in Place

Label commands work on proofs: labels and commands. To use a visible statement that no label holds, name it whole with expect: it checks that the statement is visible here and returns it as a label.

Statements named in place

▶ Run

Multiple Arguments

When more than two arguments are provided, the operation is applied sequentially — each argument is combined with the accumulated result from all previous steps:

Multiple arguments

▶ Run

Nested Label Commands

Label commands can be nested. The result of one command can serve as an argument to another, allowing complex set expressions in a single statement:

Nested label commands

▶ Run

Checking: expect

expect(A, B, ...) checks that all its arguments contain the same set of statements (brought to the current scope, ignoring order and duplicates). If they differ, it is an error that lists what is only in one side and only in the other. If they are equal, it returns that set, so it can be labeled and passed on like any label command.

Use it to state important facts explicitly instead of writing them in comments: a comment can silently become wrong, a check cannot.

What expect states, the independent kernel checks too: a statement of another file it names here (a fact of an imported module, a theorem of an interpretation at the model) is proved at this place in the file's certificate, by that statement's own proof applied here.

Explicit checks

▶ Run

A statements string in expect is the expected set — with labels beside it, it does not have to exist in the scope; there is one such string at most. Inclusion is expressed by combining expect with intersection: expect(*(A, B), A) means A ⊆ B.

Inclusion through intersection

▶ Run

Statement Equality

All set operations rely on statement equality to determine matches. Two label statements are considered equal when both conditions hold:

  • Their expression content is identical (same left-hand and right-hand sides)
  • They were defined in the same scope

This means two statements with identical text but originating from different scopes are treated as distinct entries. Union will not deduplicate them, subtract will not match them, and so on. This scope-awareness ensures that statements retain their provenance through set operations.

⚠
The symbols +, -, *, ^ are reserved in Formall and cannot be used in atom names.

Scope Visibility Filtering

When a label is passed as an argument to a label command, its statements are filtered by scope visibility. A statement visible here is a fact of this scope. A statement standing in a closed block inside the label's declaration scope is a theorem of that label (see Theorems keep their place) and is kept with its place. The receiving label holds it only when declared in a scope that encloses that place; otherwise the statement is considered foreign to it and is excluded (with a warning).

Scope filtering in action

Statements from sibling scopes are invisible. +(Collected) inside the second if can only see statements defined in its own scope branch.

▶ Run

This is why L:OtherLabel; (defining a label directly with another label) is not allowed — the filtering would happen implicitly, and the user might expect all of OtherLabel's statements to be copied. Using a label command makes the filtering explicit:

resultLabel: +(sourceLabel); // explicit: copy accessible statements from sourceLabel into resultLabel

Theorems keep their place

A statement proved inside for and if blocks and delivered to a label declared outside them is a theorem: its contract — parameters, conditions, lock axioms — is the path from its place to the label's declaration (as with T::). Label commands keep that place: +(T, S) holds both theorems with their own contracts, and a call applies each with its own conditions. Two theorems that look the same but rest on different conditions stay different statements, so -, *, ^ tell them apart. String arguments still find visible facts only: a theorem is given by its label.

Label commands with theorems

Both theorems have the same form, but different conditions: OnlyT needs not(y) -> false, the symmetric difference — y -> true.

▶ Run

A label on type or rule holds what the declaration generates — its schemas, each with its place (a package to pass to a call).

Augmentation vs. Label Commands

Label commands (+(A, B)) and augmentation postfixes (L+: cmd) use the same underlying set operations, but differ in how they are applied:

  • Label commands create a new collection from existing labels and statements. They are standalone commands that produce a result to be captured.
  • Augmentation postfixes modify an existing label in place by combining its current contents with the result of a command. They are part of the label definition syntax.

See Labels for details on augmentation postfixes.