Skip to content

Judgments

A judgment declaration defines a relation between terms of a syntax, such as a typing relation, by inference rules, and gives each argument a mode. The compiler checks the modes, and the declaration then runs three ways: as a checker and inferencer, as a deduction on the chart under any semiring, and as a static check of a deduction's lexicon at compile time. The rules reuse the deduction rule form, read over the second-order terms of Equations and normalization §1, so the formalism of Weighted deduction covers object languages with binding as well as grammars (Sewell et al., 2010; Pientka and Dunfield, 2010).

syntax Lambda
    sorts
        Term : object
        Type : object
        Name : data
    constructors
        Base : Name -> Type
        Arr : Type, Type -> Type
        Const : Name -> Term
        App : Term, Term -> Term
        Lam : (x : Term).Term -> Term

judgment Ty(ctx : Context[Type] in, t : Term in, ty : Type out)
    rule var : lookup(ctx, i, A) |- Ty(ctx, Term.var(i), A)
    rule lam : Ty((ctx, x : A), M[x], B) |- Ty(ctx, Lam(x. M[x]), Arr(A, B))
    rule app : Ty(ctx, F, Arr(A, B)), Ty(ctx, N, A) |- Ty(ctx, App(F, N), B)
    rule c : |- Ty(ctx, Const("c"), Base("o"))
    rule id[a] : |- Ty(ctx, Const("id"), Arr(a, a))

1. Forms, contexts, and rules

A judgment form \(J(a_1 : S_1\ m_1, \dots, a_n : S_n\ m_n)\) names a sort \(S_i\) of one syntax and a mode \(m_i \in \{\mathtt{in}, \mathtt{out}\}\) for each argument. At most one argument has the sort \(\mathtt{Context}[T]\), a list of terms of sort \(T\). The argument whose sort the syntax binds variables of is scoped by the context: its free variable with de Bruijn index \(i\) has the \(i\)-th entry of the context, counted from the innermost, as its \(T\). The context extension \((\Gamma, x : A)\) is the context of a body that binds \(x\); the built-in judgment \(\mathtt{lookup}(\Gamma, i, A)\), with modes in, in, out, reads an entry; and the pattern S.var(i) matches a variable of the scoped sort \(S\).

A rule

\[ \frac{P_1 \quad \cdots \quad P_k}{C} \qquad \text{written} \qquad \mathtt{rule}\ r[\vec a] : P_1, \dots, P_k \vdash C \]

has premises and a conclusion that are instances of judgment forms over terms with metavariables. The names in the bracket \([\vec a]\) are schematic: each use of the rule renames them apart, so a rule without premises is an axiom scheme, as for the constant id above, whose type is \(a \to a\) for every type \(a\).

2. Modes

Modes fix which arguments a derivation search is given and which it computes, in the manner of bidirectional typing (Dunfield and Krishnaswami, 2021). The mode check reads a rule as a procedure: the inputs of the conclusion determine their variables; the premises are read left to right, and each premise may use the variables determined so far and determines the variables of its outputs; at the end, every variable of an output of the conclusion must be determined. A rule that fails is refused at the rule, naming the variable no input or earlier premise determines. A variable the inputs do not fix, such as \(A\) in app before the first premise has run, becomes a unification variable whose value a later premise solves.

3. Checking and inference

A well-moded judgment runs as a syntax-directed search. For a goal \(J(\vec u)\) with its inputs given:

  1. The candidate rules are those whose conclusion has the head constructor of the scoped input, found by an index on head constructors.
  2. The scoped input is matched against the conclusion's pattern by second-order pattern matching, which binds each metavariable of a scope to an abstraction of the input. The other arguments are unified with the conclusion's arguments by first-order unification.
  3. The premises are solved in order. An extended context \((\Gamma, x : A)\) in a premise makes \(x\) the new innermost variable of the scoped argument, and an instance \(M[x]\) instantiates the abstraction bound to \(M\) at that variable.
  4. Unification variables shared between premises carry their solutions forward, as in constraint-based elaboration (Pottier, 2014). Where a premise relates terms under a binder, the constraint is solved by higher-order pattern unification, which is decidable with most general unifiers on Miller's fragment (Miller, 1991; Abel and Pientka, 2011).

A derivation is a tree of rule instances whose leaves are axioms and lookup facts. JudgmentSystem.solve yields every derivation of a goal, and JudgmentSystem.infer the outputs of the first. When no derivation exists, the failure reported is the one that got furthest: the search ranks failed attempts by the kind of constraint that failed and by depth, and reports the unsolvable constraint, such as cannot unify Base('o') with Base('nat'), with the goal it arose in.

For the simply typed fragment shown above, inference agrees with algorithm W on every closed well-typed term, which the test suite checks differentially on seeded random terms.

4. Judgments as deductions

A judgment is a deduction in the sense of Weighted deduction: its items are judgment instances, its rules are the declared rules, and its least fixed point in the Boolean semiring is derivability. JudgmentSystem.chart builds the derivation hypergraph of a goal and evaluates it with the agenda engine of the deduction runtime, so a judgment runs on the same chart as a grammar, under any chart semiring. With rule weights declared by the pragma #[weight=w], the log-probability semiring gives the total weight of a goal's derivations and thus a distribution over typings; the Boolean semiring gives derivability, and the Viterbi semiring the best derivation. No second chart implementation is involved.

5. Static discharge of a lexicon

The option [lexicon=G] on a typing judgment of the form \(J(\Gamma, t, A)\) names a deduction \(G\) whose lexicon holds terms of the syntax. At compile time each lexical entry's logical form is read as a closed term and typed in the empty context. An entry whose logical form has no derivation fails qvr check at the entry, with the constraint that could not be solved, so an ill-typed lexical entry is rejected before any sentence is parsed. This check requires an explicit logical form for each entry. A named entry in a category-only deduction can omit its form when a proof interpretation builds its meaning separately.

6. Guarantees and limits

The search is sound: every derivation it returns is a derivation of the declared rules. It is complete for well-moded rules whose search terminates, which holds when each premise's scoped input is a proper subterm of the conclusion's, as in the syntax-directed rules above. A rule set that is not syntax directed in this sense may search without end; the search bounds its depth and reports the bound. Higher-order unification outside the pattern fragment is undecidable in general, and a constraint outside the fragment is refused rather than approximated.

Cited sources

Peter Sewell, Francesco Zappa Nardelli, Scott Owens, Gilles Peskine, Thomas Ridge, Susmit Sarkar, and Rok Strniša. Ott: Effective Tool Support for the Working Semanticist. Journal of Functional Programming, 20(1):71–122, 2010. doi:10.1017/S0956796809990293.

Brigitte Pientka and Jana Dunfield. Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description). In Automated Reasoning (IJCAR 2010), volume 6173 of Lecture Notes in Computer Science, 15–21. Springer, 2010. doi:10.1007/978-3-642-14203-1_2.

Jana Dunfield and Neel Krishnaswami. Bidirectional Typing. ACM Computing Surveys, 54(5):98:1–98:38, 2021. doi:10.1145/3450952.

François Pottier. Hindley-Milner Elaboration in Applicative Style. In Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming, 203–212. ACM, 2014. doi:10.1145/2628136.2628145.

Dale Miller. A Logic Programming Language with Lambda-Abstraction, Function Variables, and Simple Unification. Journal of Logic and Computation, 1(4):497–536, 1991. doi:10.1093/logcom/1.4.497.

Andreas Abel and Brigitte Pientka. Higher-Order Dynamic Pattern Unification for Dependent Types and Records. In Typed Lambda Calculi and Applications (TLCA 2011), volume 6690 of Lecture Notes in Computer Science, 10–26. Springer, 2011. doi:10.1007/978-3-642-21691-6_5.