Skip to content

Composable deductions and rule schemata

A deduction can be assembled from parts. A deduction signature declares atoms and rules with no lexicon, a lexicon module declares entries typed against a signature, and a deduction extends signatures and takes lexicon modules. A rule schema states a family of rules indexed by static parameters, and a rules line instantiates it. Every one of these is interpreted away before any engine reads the deduction (compose_deductions): the reference machine, the compiled runtime, and every transpile target run the one monolithic deduction the parts stand for, and a refusal is reported at check time with code qiec-deduction. The semantics states what composition means.

Form Where Meaning
deduction signature S statement atoms, binders, rules, and schemata; no lexicon and no header
extends S, T[n <= 1] body line of a deduction or a signature include the signatures, restricting T's schema instances
lexicon L for S statement lexical entries whose categories are atoms of S
lexicon L, M body line of a deduction take the entries of the lexicon modules
extends S shared, lexicon L per instance an entry of either line weigh the rules or entries taken (weight modes)
rule r[n : Nat, e : Dir] : ... body line a rule schema over the parameters n and e
rules r[n <= 2, e in Dir] where c body line the instances of r the constraints admit
Y \|e Z ... n rule pattern of a schema Y taking n arguments through the slash e
e(z ... n, B) rule pattern of a schema e applied once per name z_1 through z_n, nested to the right (name sequences)
#[shared = F(n)] a rule's pragma the rule's weight is the learnable family F at n
module D : Deduction extends S computation parameter any deduction extending S

The words extends, rules, and Deduction are keywords only where these forms start, so each can still name an atom, a rule, a computation, or a signature; lexicon starts a lexicon module only at the start of a statement, and deduction signature : A -> B is still a deduction named signature.

Deduction signatures and lexicon modules

object Term : FinSet 16

deduction signature CCGCore
    atoms NP, S, N, Fwd, Bwd, span
    rule fwd_app : span(I, K, Fwd(X, Y)), span(K, J, Y) |- span(I, J, X)
    rule bwd_app : span(I, K, Y), span(K, J, Bwd(X, Y)) |- span(I, J, X)

deduction signature Composition
    extends CCGCore
    rule fwd_comp : span(I, K, Fwd(X, Y)), span(K, J, Fwd(Y, Z)) |- span(I, J, Fwd(X, Z))
    rule bwd_comp : span(I, K, Bwd(Y, Z)), span(K, J, Bwd(X, Y)) |- span(I, J, Bwd(X, Z))

lexicon Nominals for CCGCore
    atoms the, dog
    "the" : Fwd(NP, N) = the
    "dog" : N = dog

lexicon Verbals for CCGCore
    atoms barks
    "barks" : Bwd(S, NP) = barks

deduction CCG : Term -> Term [start=S, depth=6]
    extends CCGCore, Composition
    lexicon Nominals, Verbals

CCG is the deduction whose atoms are those of its signatures, then of its lexicon modules, then its own; whose rules are those of its signatures, then its own; and whose lexicon is the entries of its lexicon modules in the order they are named, then its own inline entries. It keeps its own header: its options, its semiring, and its deriving clause. A signature's body takes atoms, binders, rule, rules, and extends lines; a lexicon block in it does not parse. A lexicon module's atoms line declares the logical-form constants it introduces.

Parts are identified by where they are declared, never by spelling:

  • a signature reached along two paths (CCG extends CCGCore directly and through Composition) contributes its atoms and rules once;
  • two declarations introducing one atom name (two lexicon modules, a lexicon module and a signature, or a deduction and a signature it extends) are refused, naming both;
  • two rules of one name from different declarations are refused;
  • every symbol a signature's rule applies is an atom of the signature or of one it extends;
  • a rule's identifiers are resolved where the rule is written, so an atom a deduction or a signature declares downstream never captures a variable of a rule it inherits: a grammar may name a category A or X while extending rules whose variables are spelled A and X;
  • every atom of an entry's category in lexicon L for S is an atom of S or of a signature S extends, and a deduction taking L extends S.

extends takes signatures only: a deduction cannot be extended, since its lexicon and header are not a core. Signatures that extend one another in a cycle are refused with the cycle.

Rule schemata

index Dir = Fwd | Bwd

object Word : FinSet 32

deduction CCG : Word -> Word [start=S, depth=7]
    atoms S, NP, N, Fwd, Bwd, span
    rule forward[n : Nat, e : Dir] : span(I, K, Fwd(X, Y)), span(K, J, Y |e Z ... n) |- span(I, J, X |e Z ... n)
    rule backward[n : Nat, e : Dir] : span(I, K, Y |e Z ... n), span(K, J, Bwd(X, Y)) |- span(I, J, X |e Z ... n)
    rules forward[n <= 2] where n > 0 || e == Fwd, backward[n <= 2] where n > 0 || e == Bwd
    lexicon
        "Mary" : NP = NP
        "might" : Fwd(Bwd(S, NP), Bwd(S, NP)) = S
        "give" : Fwd(Fwd(Bwd(S, NP), NP), NP) = S

A rule with a static telescope is a schema. Its parameters range over Nat, Bool (false and true), or a declared index sort whose constructors are all nullary (the values in declaration order). A rules line names schemata in scope (the deduction's own and its signatures') and instantiates each at every assignment its constraints admit:

  • p in Dir keeps the values of p's sort, and p in {Fwd} the listed ones;
  • any other bracketed constraint, and the where condition, is a Boolean expression over the parameters and the index values, with ==, !=, <, <=, >, >=, +, -, *, &&, ||, and not;
  • a Nat parameter is bounded by a bracketed n <= k, n < k, or n == k, and refused without one.

An instance replaces each parameter in the schema's patterns by its value: an index value is the atom of the same spelling, as an argument or as the head of an application (e(X, Y) is Fwd(X, Y) at e = Fwd), and a numeral is a numeral pattern. The spine Y |e Z ... n is Y at n = 0 and e(...e(Y, Z_1)..., Z_n) otherwise; every spine over one sequence variable in a rule has one length, so a premise and the conclusion share Z_1 through Z_n. The instances above are application, harmonic and crossed composition, and their degree-two generalizations in both directions: exactly the ten hand-written rules, with the same charts.

Each instance is named after the schema and its values, forward_1_Bwd, and is an ordinary rule of the deduction. An instance named like another rule is refused, and so are two instances of one schema that are the same rule up to the names of their variables (above, forward at n = 0 would otherwise be one rule for each slash), since the chart would count every derivation by it twice. extends S[n <= 1] keeps, of the instances of schemata declared in S or a signature it extends, those whose parameters satisfy the constraint; a constraint naming no parameter of those schemata is refused. A schema no rules line names contributes no rule.

A slash spine is a pattern of a schema only; anywhere else it does not parse.

Name sequences

A spine folds its slash to the left, so it builds a category taking n arguments. Its dual, the name sequence z ... n, folds an application to the right: written as the first of an application's two arguments, e(z ... n, B) is B at n = 0 and e(z_1, e(z_2, ... e(z_n, B))) otherwise. Over a binder it abstracts n names, and a spine over the same variable, with an application atom as its slash, applies a meaning to them in order. The schema below uses the combinator \(B^n\) to give forward composition a meaning at each degree.

index Dir = Fwd | Bwd

object Word : FinSet 32

deduction Meanings : Word -> Word [start=S, depth=8]
    atoms S, NP, span, App, probably, gave, rex, kim
    binders Lam
    rule forward_application : span(I, K, Fwd(X, Y), F), span(K, J, Y, A) |- span(I, J, X, App(F, A))
    rule backward_application : span(I, K, Y, A), span(K, J, Bwd(X, Y), F) |- span(I, J, X, App(F, A))
    rule compose[n : Nat, e : Dir] : span(I, K, Fwd(X, Y), F), span(K, J, Y |e Z ... n, G) |- span(I, J, X |e Z ... n, Lam(z ... n, App(F, G |App z ... n)))
    rules compose[n <= 2, e in {Fwd}] where n > 0
    lexicon
        "probably" : Fwd(S, S) = probably
        "gave" : Fwd(Fwd(S, NP), NP) = gave
        "Rex" : NP = rex
        "Kim" : NP = kim

At n = 2, probably gave derives Fwd(Fwd(S, NP), NP) with the meaning Lam(z_1, Lam(z_2, App(probably, App(App(gave, z_1), z_2)))), that is λz1 z2. probably (gave z1 z2). Every sequence and spine over one variable in a rule has one length, a name sequence anywhere but the first of two arguments is refused, and so is a sequence whose names z_1 through z_n the rule already uses. Like a spine, a name sequence is a pattern of a schema only.

Shared weights

#[shared = F] makes a rule's weight the learnable weight family F, one log weight that every rule naming F reads, whatever its bindings; #[shared = F(n, e)] indexes the family by the schema's parameters, so the instances that agree on (n, e) share a weight:

rule forward[n : Nat, e : Dir] : ... #[shared = composition(n)]
rule backward[n : Nat, e : Dir] : ... #[shared = composition(n)]

Forward and backward composition of one degree then read one parameter, and its gradient is the sum of the gradients of every instance that fires. The checked elaboration reads the family's index as the parameter <D>.family.composition[1]; the compiled runtime keeps it in the deduction's families parameter dictionary. A shared weight is a rule's whole learned weight, so shared with parent or with a computed weight is refused.

Weight modes

A signature's rules and a lexicon module's entries are unweighted; the deduction that extends the signature or takes the module chooses, on each entry of its extends and lexicon lines:

deduction Grammar : Word -> Word [start=S]
    extends English, Combinators[n <= 2] shared, Raising per instance
    lexicon Nouns per instance, Verbs
mode rules and schemata polymorphic lexicon entries
(none) unweighted unweighted
shared one learned weight per rule, read by every instance of a schema one weight across the categories the entry is instantiated at
per instance each instance #[learnable] one weight per instantiation: the entry's family at the categories it takes, read alike under bounded and lazy instantiation

A shared rule reads the family [S,r], keyed by its declaring signature and its name, which no written #[shared = F] can spell. Every weight belongs to the extending deduction's own parameters, so two deductions extending one signature with one mode learn apart. A mode on a signature's own extends line is refused, since weights belong to the deduction that extends it, as are a rule or entry that its library already weighs (learnable, shared, parent, per_instance) and a rule two extends entries weigh differently (qiec-deduction).

Generic code over extensions

define category(module D : Deduction extends CCGCore, item : D.Item) : D.Item !{} =
    case item
        D.span(i, j, category) =>
            return category
        _ =>
            return item

Deduction extends S is the signature of every deduction extending S: its members are the item family Item and one constructor per atom and binder of S, named D.span, D.Fwd (and D.bound, the bound-variable item, when S has binders). The body is checked once, against an item family that has the core's constructors and one more that no code can name, standing for every constructor an extension adds; a case that covers only the core's constructors is therefore refused (module-abstraction), and a constructor the core lacks is unknown (name-unknown). Each call category(CCG, item) is specialized at the deduction passed (signatures and functors); a deduction that does not extend S, a module passed instead of a deduction, and a deduction that types a core constructor's slots otherwise are refused (signature-mismatch).

A constructor's slots are typed as S's own rules type them. A slot they fill with a numeral (or a variable shared with such a slot) is Int; a slot they fill with an atom or an application, a binder's bound variable, and any slot a variable connects to one, is an item; every other slot belongs to an open class, the slots a shared variable connects, whose sort each extension fixes. span's first two slots are such a class in CCGCore: positions (Int) in a deduction reading a sentence through a lexicon, items in one taking its axioms as input. Generic code holds an open slot abstractly, so category and D.span(j, i, category) check while j - i is refused (module-abstraction), and the same generic code runs on both kinds of extension. A deduction matches when it gives every fixed slot the sort S gives it and every slot of an open class one sort.

An atom S's rules never mention has no arity in S: the category atoms and span of a rule-free core that each extension lexicalizes are such open atoms. Generic code over S that names one fixes its arity and slot sorts by its uses, and those uses are what it requires of a deduction passed for D, its principal signature. Over a rule-free English, D.span(i, j, D.S) requires span to take three slots, the third an item, and S to take none:

deduction signature English
    atoms S, span

export define is_sentence(module D : Deduction extends English, item : D.Item) : Bool !{} =
    case item
        D.span(i, j, D.S) =>
            return true
        _ =>
            return false

A slot is an item where a use puts a constructor of D's items or a variable of type D.Item there, Int where it puts a numeral or a variable of type Int, and otherwise an open class that generic code passes on unchanged; one variable filling two slots puts them in one class, so D.span(i, j, x) => D.span(j, i, x) requires span's first two slots to take one sort. A requirement of a generic computation that another passes D on to is one of the caller too. The requirements are part of the computation's interface: qvr doc lists them after its declaring line, as requires D.span(a, b, D.Item) and requires D.S, and the interface's digest covers them, so changing what generic code requires changes its identity. Generic code applying one open atom to two numbers of arguments, or using one slot as an Int and as an item, is refused at the second use (module-abstraction); a deduction giving a required atom another arity or other slot sorts is refused at the application, with the use that fixed the requirement (signature-mismatch). An open atom generic code never names is left to the wildcard over the extension's constructors and matches whatever arity a deduction gives it.

Reification along an extension

A signature may derive classes for its item family S.Item, whose constructors are S.NP, S.span, and so on. Every deduction extending it then

  • has the inclusion CCG.include.S : S.Item -> CCG.Item;
  • derives each class the signature derives. A class derived through morphisms, a fold such as Quote[Term via q] (deriving), is the copair of the deduction's own via morphisms and the signature's, each clause renamed to the deduction's constructor (CCG.Fwd); the deduction's own clause for a constructor is taken first;
  • has the generated property quote of S.Item is natural along its inclusion into CCG.Item, which states that the deduction's fold of an included core item is the core's fold of it, compared by the target's eq; qvr test runs it as a test of the module declaring the deduction. A signature of an imported module is named by its module path, as in quote of std.grammar.ccg.Categorial.Item is natural along its inclusion into G.Item, and never by the sort instantiation below, which the deduction identifies.

A refusal names the file and line of the declaration it concerns: a signature's own fold is refused where the signature derives it, even when an importing module triggered it, and a deduction's fold where the deduction is declared.

Constructors are identified by origin, as composition identifies them, so a clause may name one through any part that reaches it: the signature declaring it, any included signature extending that one, or the signature or deduction whose fold the clause belongs to. With

family Category : Type deriving (Eq)
    constructor Atom : String -> Category
    constructor Over : Category * Category -> Category
    constructor Under : Category * Category -> Category

meta define reified_quote : Morph !{} =
    let s = MorphClause("Categorial.S", ValueCall("Atom", SeqCons(ValueString("S"), SeqNil)))
    let fwd = MorphClause("Reified.Fwd", ValueCall("Over", SeqCons(ValueVar("arg0"), SeqCons(ValueVar("arg1"), SeqNil))))
    let bwd = MorphClause("Reified.Bwd", ValueCall("Under", SeqCons(ValueVar("arg0"), SeqCons(ValueVar("arg1"), SeqNil))))
    let span = MorphClause("Categorial.span", ValueVar("arg2"))
    return Morph(SeqCons(s, SeqCons(fwd, SeqCons(bwd, SeqCons(span, SeqNil)))))

deduction signature Categorial
    atoms S, Fwd, Bwd, span

deduction signature Reified deriving (Quote[Category via reified_quote])
    extends Categorial

object Word : FinSet 4

meta define words_quote : Morph !{} =
    let np = MorphClause("G.NP", ValueCall("Atom", SeqCons(ValueString("NP"), SeqNil)))
    let kim = MorphClause("G.kim", ValueCall("Atom", SeqCons(ValueString("kim"), SeqNil)))
    let sleeps = MorphClause("G.sleeps", ValueCall("Atom", SeqCons(ValueString("sleeps"), SeqNil)))
    return Morph(SeqCons(np, SeqCons(kim, SeqCons(sleeps, SeqNil))))

deduction G : Word -> Word [start=S, depth=3] deriving (Quote[Category via words_quote])
    extends Reified
    atoms NP, kim, sleeps
    rule forward : span(I, K, Fwd(X, Y)), span(K, J, Y) |- span(I, J, X)
    rule backward : span(I, K, Y), span(K, J, Bwd(X, Y)) |- span(I, J, X)
    lexicon
        "Kim" : NP = kim
        "sleeps" : Bwd(S, NP) = sleeps

reified_quote names S and span through Categorial and the slashes through Reified; each spelling is the one constructor, and G, like any deduction extending Reified directly or through a signature that extends it, inherits every clause. A lexicon's atoms are reached through their lexicon module (Nominals.dog).

A constructor no part gives a clause for is refused, naming it and the declaration that introduces it: the fold Quote[Category] over CCG.Item has no algebra for constructor CCG.dog, which lexicon module Nominals introduces. There is no default clause.

Binders take part like any other constructor. A term's bound variables are items S.bound(k), with k the variable's de Bruijn index, the number of binders between an occurrence and its own, so alpha-equivalent terms are one item and a binder's first slot holds S.bound(0). S.Item then has the binders S declares and S.bound; the inclusion keeps every number and so preserves binding; a fold's clause for a binder receives the abstraction, the bound variable (arg0, folded by the clause for S.bound) and the body (arg1); and the naturality law is checked over binding items as over first-order ones. A deduction that declares binders has bound among its constructors whether or not an entry uses one, so every extension of one signature has the same constructors.

A fold's clauses read each slot at one type, so the item family is instantiated once for each sort assignment the extensions give S's open slots. A slot is open when S's rules leave its sort to the extension, and every slot of an atom S's rules never apply is open, its arity included. A signature with nothing open has the one family S.Item. Otherwise each assignment has its own family S.at_<sorts>.Item: the sorts of the open classes in order, then each atom S's rules never apply with the sorts of its slots. CCGCore.at_int.Item is the family for lexicalized extensions, whose span positions are Int, and CCGCore.at_item.Item for extensions taking their axioms as input; a Categorial with no rules is instantiated at Categorial.at_Fwd_item_item_Bwd_item_item_span_int_int_item.Item by a lexicalized extension. Each has its constructors (CCGCore.at_int.span) and the classes S derives; each extension includes its own instantiation and states its law along it, and extensions with one assignment share one family. A clause still names a core constructor through a part (CCGCore.span), whatever the instantiation.

Classifying an extension

classify_extensions classifies each deduction's extension of each signature it extends as definitional (it adds no rules and no symbols), conservative (from any axioms in the signature's language it derives no item of that language the signature's rules do not), neither (with a witness sentence and item), or undetermined. qvr check reports every extension that is neither definitional nor conservative as a note with code deduction-extension: a result of generic code over the signature holds for the signature only. Composition is not conservative over application: on Mary saw it derives S/NP, which application alone does not.

Refusals

Code Refused
qiec-deduction an unknown signature or lexicon module, an atom or rule declared by two origins, an entry category using an atom its signature lacks, a lexicon for a signature the deduction does not extend, a signature rule applying an undeclared symbol, an ill-formed schema, constraint, or instance, shared with parent
qiec-deriving a fold over an extension's items with no clause for a constructor
module-abstraction generic code assuming the core's constructors are all there are, or using an open atom with two arities or one slot as an Int and as an item
signature-mismatch a deduction that does not extend the signature, a module for a deduction, other slot sorts, an open atom with another arity or other slot sorts than generic code's uses require
name-unknown an extension's constructor named in generic code over the core