Judgments¶
A JudgmentSystem runs moded judgments over the
terms of a signature: it checks modes, searches for derivations with pattern matching and
unification, infers outputs, and builds a goal's derivation chart under any semiring
(Judgments). Program.judgments maps each declared judgment to its
system.
judgments
¶
Moded judgments over binding signatures: checkers, inference, and charts.
A judgment declares a relation between terms, such as a typing relation
Ty(ctx, t, A), with a mode for each argument: in arguments are
given and out arguments are computed. Its rules use the deduction rule
form over second-order terms [@sewell2010ott; @pientka2010beluga]:
rule lam : Ty((ctx, x : A), M[x], B) |- Ty(ctx, Lam(x. M[x]), Arr(A, B))
A JudgmentSystem gives a judgment three readings.
- Mode check. Read the premises left to right after the conclusion's
inputs: a variable is determined once a conclusion input or any premise
mentions it, and every variable of a conclusion output must be
determined [@dunfield2021bidirectional]. A variable declared in the
rule's bracket telescope, such as the
aof a polymorphic constant's scheme, is fresh at every use.check_modesrefuses a rule that fails, naming the variable. - Checker and inferencer. A well-moded judgment runs as a
syntax-directed search. Inputs are matched against the rules'
conclusions by second-order pattern matching (the scoped term argument)
and first-order unification (the other arguments); variables that no
input determines become unification variables, solved by
constraint-based elaboration [@pottier2014hindley] with higher-order
pattern unification where a binder is involved
[@miller1991logic; @abel2011higher].
JudgmentSystem.solveyields every derivation of a goal. - Chart. A judgment is a deduction: its least fixed point in the
Boolean semiring is derivability.
JudgmentSystem.charthands the derivation hypergraph of a goal to the agenda engine ofquivers.stochastic.agendaunder any chart semiring, so the log-probability semiring gives a distribution over typings and the Boolean semiring gives derivability.
A judgment has at most one argument of the context sort Context[T], a
list of T values. The argument whose sort the signature binds variables
of is scoped by it: its free variable i has the i-th entry,
counted from the innermost, as its T. (ctx, x : A) extends a
context, and the built-in judgment lookup(ctx, i, A) (modes in,
in, out) reads an entry. A variable of the scoped sort S is
matched by the pattern S.var(i).
A variable at a data position, such as the n of
rule constant : |- Ty(ctx, Const(n, A), A), stands for the data value
there (a data metavariable of quivers.structural.patterns): matching the
scoped term binds it, unification compares it with another data value by
equality, and a rule passes it through to its premises and outputs.
VARIABLE
module-attribute
¶
The member name of a scoped sort's variable pattern, as in Term.var(i).
AtomArgument
¶
AtomArgument = Pattern | ContextPattern | VariablePattern
An argument of a judgment instance in a rule.
GoalArgument
¶
GoalArgument = ContextValue | Term | Pattern | DataLeaf
A goal argument: a context, a scoped term, a closed pattern, or a data value.
JudgmentError
¶
Bases: ValueError
A judgment declaration is ill-formed or ill-moded.
JudgmentFailure
¶
Bases: JudgmentError
A goal has no derivation; the message names the failing constraint.
JudgmentArgument
didactic-model
¶
Bases: Model
One argument of a judgment form.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Its name.
TYPE:
|
sort
|
Its sort, or the entry sort of a context.
TYPE:
|
mode
|
TYPE:
|
context
|
Whether it is a context
TYPE:
|
JudgmentForm
didactic-model
¶
JudgmentForm(*, name: str, arguments: tuple[JudgmentArgument, ...] = ())
Bases: Model
A judgment's name and moded arguments.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The judgment.
TYPE:
|
arguments
|
Its arguments.
TYPE:
|
ContextPattern
didactic-model
¶
ContextPattern(*, base: str, names: tuple[str, ...] = (), types: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
A context in a rule: a context variable, possibly extended.
| PARAMETER | DESCRIPTION |
|---|---|
base
|
The context variable.
TYPE:
|
names
|
The names of the added entries, outermost first.
TYPE:
|
types
|
Their values.
TYPE:
|
VariablePattern
didactic-model
¶
Bases: Model
S.var(i): a variable of the scoped sort, its index named i.
| PARAMETER | DESCRIPTION |
|---|---|
sort
|
The scoped sort.
TYPE:
|
index
|
The name its index is bound to.
TYPE:
|
Atom
didactic-model
¶
Bases: ValueModel
A judgment applied to patterns: a premise or conclusion.
| PARAMETER | DESCRIPTION |
|---|---|
judgment
|
The judgment.
TYPE:
|
arguments
|
Its arguments, in the form's order.
TYPE:
|
JudgmentRule
didactic-model
¶
JudgmentRule(*, name: str, premises: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), conclusion: Atom = field(opaque=True, extras=OPAQUE_BY_VALUE), schematic: tuple[str, ...] = (), weight: float = 0.0, line: int = 0, col: int = 0)
Bases: ValueModel
An inference rule of a judgment.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Its name.
TYPE:
|
premises
|
Its premises, in order.
TYPE:
|
conclusion
|
Its conclusion.
TYPE:
|
schematic
|
Variables fresh at every use, such as the type variables of a constant's scheme.
TYPE:
|
weight
|
The log weight of each application, zero by default.
TYPE:
|
line
|
The source position, zero when built in Python.
TYPE:
|
col
|
The source position, zero when built in Python.
TYPE:
|
ContextValue
didactic-model
¶
Bases: ValueModel
A context while solving: its entries, outermost first.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
The values of the entries, possibly with unsolved variables.
TYPE:
|
Goal
didactic-model
¶
Bases: ValueModel
A judgment instance to derive.
| PARAMETER | DESCRIPTION |
|---|---|
judgment
|
The judgment.
TYPE:
|
arguments
|
Its arguments; an output is a pattern, typically a fresh variable.
TYPE:
|
Derivation
didactic-model
¶
Derivation(*, rule: str, goal: Goal = field(opaque=True, extras=OPAQUE_BY_VALUE), premises: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), weight: float = 0.0)
Bases: ValueModel
A derivation found by the search.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule applied at the root.
TYPE:
|
goal
|
The instance derived.
TYPE:
|
premises
|
The derivations of the premises.
TYPE:
|
weight
|
The rule's log weight.
TYPE:
|
JudgmentSystem
¶
JudgmentSystem(signature: Signature, forms: Sequence[JudgmentForm], rules: Sequence[JudgmentRule], max_depth: int = 200)
The judgments of a signature, their rules, and their readings.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature of the terms.
TYPE:
|
forms
|
The judgments.
TYPE:
|
rules
|
Their rules.
TYPE:
|
max_depth
|
The deepest derivation the search builds.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
JudgmentError
|
If a form or rule is ill-formed or ill-moded. |
Source code in src/quivers/structural/theories/judgments.py
scoped_position
¶
scoped_position(form: JudgmentForm) -> int | None
The argument a judgment's context scopes.
| PARAMETER | DESCRIPTION |
|---|---|
form
|
The form.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int or None
|
The position of the first non-context argument whose sort has
variables, or |
Source code in src/quivers/structural/theories/judgments.py
goal
¶
goal(judgment: str, *arguments: GoalArgument | None) -> Goal
A goal with a fresh variable at each output given as None.
| PARAMETER | DESCRIPTION |
|---|---|
judgment
|
The judgment.
TYPE:
|
*arguments
|
The inputs, and
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Goal
|
The goal. |
| RAISES | DESCRIPTION |
|---|---|
JudgmentError
|
If the arity is wrong. |
Source code in src/quivers/structural/theories/judgments.py
solve
¶
solve(goal: Goal) -> Iterator[tuple[Derivation, UnificationProblem]]
Every derivation of a goal, with the unification state it needs.
| PARAMETER | DESCRIPTION |
|---|---|
goal
|
The goal.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
tuple of Derivation and UnificationProblem
|
A derivation and the solution of the goal's variables; apply
|
| RAISES | DESCRIPTION |
|---|---|
JudgmentError
|
If the search exceeds |
Source code in src/quivers/structural/theories/judgments.py
derivations
¶
derivations(goal: Goal, limit: int | None = None) -> tuple[tuple[Derivation, UnificationProblem], ...]
Every derivation of a goal, or the failure that explains none.
| PARAMETER | DESCRIPTION |
|---|---|
goal
|
The goal.
TYPE:
|
limit
|
Stop after this many derivations.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
The derivations with their solutions, at least one. |
| RAISES | DESCRIPTION |
|---|---|
JudgmentFailure
|
If the goal has no derivation; the message is the constraint the deepest attempt could not satisfy. |
Source code in src/quivers/structural/theories/judgments.py
infer
¶
infer(judgment: str, *arguments: GoalArgument | None) -> tuple[tuple[PatternArgument, ...], ...]
The outputs of every derivation of a goal.
| PARAMETER | DESCRIPTION |
|---|---|
judgment
|
The judgment.
TYPE:
|
*arguments
|
Inputs, with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of tuple of PatternArgument
|
One tuple of outputs per derivation, with unsolved variables left as metavariables; an output of a data sort is its data value. |
| RAISES | DESCRIPTION |
|---|---|
JudgmentFailure
|
If the goal has no derivation; the message is the constraint the deepest attempt could not satisfy. |
Source code in src/quivers/structural/theories/judgments.py
chart
¶
chart(goal: Goal, semiring: ChartSemiring, weights: Mapping[str, float] | None = None) -> dict[tuple, Tensor]
Run a goal's derivations on the agenda chart under a semiring.
Every derivation the search finds is read as ground hyperedges: an item is a judgment instance with its variables named in order of appearance, and an edge is a rule application. The agenda engine then computes each item's semiring value, the sum over its derivations of the product of their rule weights, so the derivations are not enumerated twice in the semiring.
| PARAMETER | DESCRIPTION |
|---|---|
goal
|
The goal.
TYPE:
|
semiring
|
The semiring.
TYPE:
|
weights
|
Log weights by rule name, overriding the declared ones.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict of tuple to torch.Tensor
|
The value of every item that instantiates the goal, keyed by the item. |
Source code in src/quivers/structural/theories/judgments.py
build_atom
¶
build_atom(syntax: PatternSyntax, forms: Mapping[str, JudgmentForm], signature: Signature) -> Atom
Resolve a written judgment instance J(a_1, ..., a_n) to an atom.
A context argument is a context variable or an extension
(ctx, x : A); the names an extension adds are bound in the scoped
argument of the same instance, where S.var(i) is a variable of the
scoped sort S with index i. Every other name is resolved by
quivers.structural.patterns.resolve.
| PARAMETER | DESCRIPTION |
|---|---|
syntax
|
The written instance.
TYPE:
|
forms
|
The declared judgments.
TYPE:
|
signature
|
The signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Atom
|
The atom. |
| RAISES | DESCRIPTION |
|---|---|
JudgmentError
|
If the judgment is undeclared, the arity is wrong, or an argument has the wrong form for its position. |
Source code in src/quivers/structural/theories/judgments.py
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 | |
rule_from_text
¶
rule_from_text(name: str, text: str, forms: Mapping[str, JudgmentForm], signature: Signature, *, schematic: tuple[str, ...] = (), weight: float = 0.0) -> JudgmentRule
Read a rule written P_1, ..., P_n |- C.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The rule's name.
TYPE:
|
text
|
The premises,
TYPE:
|
forms
|
The declared judgments.
TYPE:
|
signature
|
The signature.
TYPE:
|
schematic
|
Variables fresh at each use.
TYPE:
|
weight
|
The rule's log weight.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
JudgmentRule
|
The rule. |
| RAISES | DESCRIPTION |
|---|---|
JudgmentError
|
If the text is not a rule. |
Source code in src/quivers/structural/theories/judgments.py
check_modes
¶
check_modes(rule: JudgmentRule, system: JudgmentSystem) -> None
Refuse a rule whose outputs its premises do not determine.
The conclusion's inputs determine their variables. Each premise, read
left to right, determines every variable it mentions: an input
variable not yet determined becomes a unification variable, which the
premise's derivation constrains. Every variable of the conclusion's
outputs must then be determined, unless the rule declares it in its
bracket telescope as fresh at each use. A premise's scoped term
argument and the index of a lookup must be determined before the
premise, since they are matched rather than unified.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
system
|
Its system.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
JudgmentError
|
If an output variable is undetermined or a premise's scoped input is not yet determined; the message names the variable and the rule. |
Source code in src/quivers/structural/theories/judgments.py
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 | |
probability_weights
¶
Log weights for rules given as probabilities.
| PARAMETER | DESCRIPTION |
|---|---|
probabilities
|
A probability per rule.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict of str to float
|
Their logarithms. |
| RAISES | DESCRIPTION |
|---|---|
JudgmentError
|
If a probability is not positive. |