Lexicons: polymorphic entries, lexical rules, and refinements¶
A deduction's lexicon need not list every entry. A
category set names a family of categories, a polymorphic entry ranges over one, its meaning can be
synthesized from the semantic type of each category it takes, a template states the shape of a
family of entries once, a lexical rule derives entries from entries, and a refinement adds features
to the atoms of another deduction. Every one of these is expanded into ordinary atoms, rules, and
entries before any engine reads the deduction
(quivers.dsl.lexical.expand_lexical), so the reference
machine, the compiled runtime, and every transpile target run the expanded deduction, and a
refusal is reported at check time with code qiec-lexicon.
| Form | Where | Meaning |
|---|---|---|
categories Conj = S, N, Bwd(Conj, Arg) |
body line | a set of categories |
semantics Ty and Fwd(A, B) => Arr(B, A) lines |
body block | the semantic type of each category, checked by the judgment Ty |
"and" : ∀ X in Conj. Fwd(Bwd(X, X), X) = coord[X] |
lexicon entry | one entry per category of Conj |
coord[X] |
an entry's meaning | the meaning the meta computation coord synthesizes from the semantic type of X |
template noun(p) : N = Lam(x, App(p, x)) |
body line | entries "dog" : noun = dog take N and Lam(x, App(dog, x)) |
#[shared = F] |
an entry or a template | the entry reads the learned weight family F |
"jo" : Cat #[entry=jo_word] |
lexicon entry | name the lexical production for a proof interpretation |
lexical rule r : span(I, J, NP, x) \|- span(I, J, C, M) |
body line | a unary rule over lexical items |
refines Base |
body line | the deduction refines Base |
atoms NP[num : Num], NP[sg], NP[n] |
atoms line, categories | an atom with a feature over the set Num |
The words categories, semantics, template, lexical, and refines are keywords only at the
start of a deduction body line, so each can still name an atom, a rule, or a program. A category
variable is bound by the token ∀.
Named entries in proof interpretations¶
Give a lexical production a name with #[entry=NAME] when a
reindexed interpretation
assigns its value. The clause axiom jo_word = jo_meaning attaches jo_meaning to
the production named jo_word. Each named production in an interpreted deduction
has a distinct name, even when two productions have the same word and category.
Their derivations and weights remain distinct in the proof rows.
For span(I, J, Cat), omit the entry's = form when it has an entry name.
For span(I, J, Cat, Form), provide = form as well. The name selects the
handler and the form fills the chart item's fourth argument. The compiler checks
the lexical handler's result at the image of Cat under the interpretation's
index map. A handler can be nullary or take the matched word as a String.
Category sets¶
A set is the least set of categories that holds each member, where a member names a declared set
at every position it may take any category of that set. A declaration is thus a
regular tree grammar whose nonterminals are
the sets: Conj holds S, N, and every category built from them by applying Bwd or Fwd to
an argument in Arg. Membership is decided by recursion on a category. A set that contains itself
under a constructor is infinite; one that contains itself with no constructor in between is
refused, since its members would not be decided by their shape.
Polymorphic entries¶
A polymorphic entry is one entry for every assignment of categories to its variables:
- a variable over a finite set takes every member of the set;
- a variable over an infinite set takes the members the grammar attests up to a bound: the
categories the deduction's rules derive from the categories of its monomorphic entries, and every
part of one, of constructor depth at most the entry's
#[bound=n], else the deduction'sdepth. Without either, the entry is refused, naming the set.
The instances of one entry read one learned weight, the family the entry stands for, so a
#[learnable] polymorphic entry trains one weight on every use of any of its instances.
#[learnable, per_instance], or lexicon L per instance on the deduction taking the entry,
gives each instantiation a learned weight of its own, the entry's family at the categories its
variables take, so a lazily instantiated entry reads the weight of the instance a rule meets. A
category variable may occur in the meaning, where it stands for its category as a term.
With #[instantiate=lazy] the entry is the same deduction as with bounded instantiation, run
differently by the agenda engine. Its item family, the classes derived for it, its proofs, the
reference machine, and every transpile target see the bounded entry: one entry per instance, so a
case over D.Item or a Quote morphism needs a clause only for the atoms the deduction writes.
The agenda instead puts one item per token of the entry's word in the chart, whose category stands
for the entry with its variables unbound and whose meaning is the entry's, and specializes every
rule premise that can take an instance: in the specialized rule that premise takes the entry's
item, keeping its meaning pattern, and the rest of the rule is instantiated by the
unifier of the premise's category
and the instance's. An instance is thus built only where a rule uses it, and a specialized rule
reads its source rule's learned weight on the source's own bindings (under normalize=lhs, its
source rule's production and the consumed instance's). The meaning travels in the item rather than
in a rule pattern, so it may apply a binder.
Before a run's chart leaves the engine it is read back as the deduction's: the entry's item stands for each of its instances, with its weight, and a specialized rule's application stands for its source rule's application to the instance it consumed. The chart, its derivations, its axiom occurrences, and its goal items are thus those of the bounded run, and so is everything that reads them (a rule-attached loss, a program's result, a fold over the chart). Lazy instantiation composes with lexical rules in chart mode and with refinements, whose base may instantiate the entry either way. A specialized rule runs its source rule's guard and computed weight on its own bindings and the variables the specialization fixed, as the reference machine runs them on the instance.
A premise whose category is a variable nothing else constrains, or a premise that is a bare variable, unifies with every instance, so it is specialized to each: for that rule alone, lazy instantiation is bounded instantiation. The rule as written never takes the entry's item, which stands for every instance at once and is none of them: the agenda gives a rule an item of its own making only where the plan wrote that item into the rule.
Semantic types and synthesized meanings¶
The clauses give each category its semantic type by structural recursion: the first clause whose
category pattern matches gives the type, each pattern variable standing for the type of the
category it matched. The types are terms of the syntax that the judgment Ty(ctx, term, type)
types (Object languages), and every meaning the lexicon
assigns, written or synthesized, must have a derivation of Ty in the empty context at the
semantic type of its category; otherwise the entry is refused, naming the type, the category, and
the constraint that failed.
A meaning coord[X] calls the meta computation coord on the semantic
type of the category X takes. The computation receives the type as a SynValue (a nullary
constructor E as ValueVar("E"), an application Arr(E, T) as ValueCall("Arr", ...)) and
returns the meaning as one. Generalized conjunction recurs on the type:
meta define conj(t : SynValue, m : SynValue, n : SynValue, depth : Int) : SynValue !{refusal} =
case t
ValueCall("Arr", SeqCons(_, SeqCons(result, SeqNil))) =>
let x = "x" + string(depth)
let left = ValueCall("App", SeqCons(m, SeqCons(ValueVar(x), SeqNil)))
let right = ValueCall("App", SeqCons(n, SeqCons(ValueVar(x), SeqNil)))
let body <- conj(result, left, right, depth + 1)
return ValueCall("Lam", SeqCons(ValueVar(x), SeqCons(body, SeqNil)))
ValueVar("T") =>
return ValueCall("And", SeqCons(m, SeqCons(n, SeqNil)))
_ =>
let refused <- refuse[SynValue]("generalized conjunction needs a type ending in T")
return refused
meta define coord(t : SynValue) : SynValue !{refusal} =
let body <- conj(t, ValueVar("p"), ValueVar("q"), 0)
return ValueCall("Lam", SeqCons(ValueVar("q"), SeqCons(ValueCall("Lam", SeqCons(ValueVar("p"), SeqCons(body, SeqNil))), SeqNil)))
With these, "and" : ∀ X in Conj. Fwd(Bwd(X, X), X) = coord[X] gives "and" the meaning
Lam(q, Lam(p, Lam(x0, And(App(p, x0), App(q, x0))))) at Bwd(S, NP), the same entry a writer
would give by hand. A refusal of the meta computation, or a synthesized meaning with no typing, is
reported at the entry.
Templates and shared weights¶
An entry whose category names a template takes the template's category and the template's meaning with the parameter replaced by the entry's meaning, and the template's options followed by its own. A template's category may be polymorphic. Options of an entry or a template:
| Option | Meaning |
|---|---|
learnable |
the entry has a learned weight |
shared = F |
the entry reads the learned weight family F, one weight shared by every entry and rule naming F |
per_instance |
with learnable, each instance of a polymorphic entry has a learned weight of its own; refused with shared |
bound = n |
the depth bound of a polymorphic entry's attested categories |
instantiate = bounded \| lazy |
how a polymorphic entry is instantiated |
Lexical rules¶
A lexical rule derives a lexical item from a lexical item over the same span. In compile mode
(the default), expansion closes the lexicon under the rule: each entry its premise matches yields
an entry for the conclusion, and entries derived this way are closed again, up to #[bound=n]
applications (one by default). In chart mode (#[mode=chart]) the rule applies in the chart,
to lexical items and to the items the chart's lexical rules derive from them, up to the largest
#[bound=n] of the chart rules, and never to an item an ordinary rule derived. Either way a
learnable lexical rule has one learned weight, the family __lexical_rule_<r>, which each entry
it derives multiplies into its source entry's weight, so both modes give the same weights.
Chart mode is a way of running the deduction, which is the one compile mode gives with the same
bound: the item family, proofs, the reference machine, and every transpile target see each entry
the rule derives as an entry. The agenda engine instead wraps each lexical item as
__lexical_item(C, __lexical_level_d), where d counts the chart rule applications behind it,
unwraps it at any level with a generated rule __lexical_lift, and applies one copy of each
lexical rule per level below the bound, from a wrapped item at its level to one at the next. It
reads every run back as the deduction's before its chart leaves the engine: a wrapped item stands
for nothing, and the lift's conclusion is an axiom occurrence of the lexical item it unwraps, a
written entry's or one a lexical rule derives. A premise whose category is a bare variable takes
the lifted items and never a wrapped one. Under normalize=lhs an entry's weight is its own
production's, normalized over its category's group, so a derived item's weight owes nothing to its
source item's; there the agenda reads each derived entry as the entry it is.
Refinements¶
object Word : FinSet 8
deduction CCG : Word -> Word [start=S, depth=4]
atoms S, NP, N, Fwd, Bwd, span, the, dog, sleeps
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)
lexicon
"the" : Fwd(NP, N) = the
"dog", "dogs" : N = dog
"sleeps", "sleep" : Bwd(S, NP) = sleeps
deduction Agree : Word -> Word [start=S, depth=4]
refines CCG
atoms NP[num : Num], N[num : Num], sg, pl
categories Num = sg, pl
lexicon
"the" : ∀ n in Num. Fwd(NP[n], N[n]) = the
"dog" : N[sg] = dog
"dogs" : N[pl] = dog
"sleeps" : Bwd(S, NP[sg]) = sleeps
"sleep" : Bwd(S, NP[pl]) = sleeps
refines CCG adds features to atoms of CCG: NP[num : Num] makes NP a family of atoms, one
per member of the finite set of atoms Num, written NP[sg] and NP[pl] (and named NP__sg,
NP__pl in the item family). A rule written over a feature variable, NP[n], is one rule per
value. Agree accepts the dog sleeps and the dogs sleep and rejects the dog sleep, all of
which CCG accepts.
A refinement inherits every part of its base it does not restate: rules, entries, templates, lexical rules, category sets, and semantics. A base part that reads an atom the refinement gives features bare is inherited only when restated, since its features are undetermined: a rule, lexical rule, or category set naming one, a template or entry whose category names one, and anything ranging over or naming such a set or template. A rule is restated by declaring one of the same name.
An inherited rule is the base's rule, not a copy. It reads the base rule's learned weight on the
forgetful images of its bindings, so training the refinement trains the base, and a sentence both
accept gets the same rule weights from either; under normalize=lhs its free log-weight is the base
rule's. Its guard is the base guard transported to refined items: each branch on a base
constructor becomes a branch on every refined constructor that forgets to it, generated as
__lexical_<R>_guard_<g> (a guard may call no computation, so it cannot forget its items itself).
Its computed weight is the base weight read on the forgotten items, generated as
__lexical_<R>_weight_<w>, which applies R.Item.forget to each item and calls w. A restated
rule keeps parameters of its own: restating is how a refinement opts out of sharing.
The forgetful map sends NP[sg] to NP and every other symbol to itself. The compiler checks
that a refinement only restricts its base: the image of every refined rule must be an instance of
a base rule, and the image of every refined entry must be an entry of the base, else the rule or
entry is refused, naming its image. The image of every derivation of the refinement is then a
derivation of the base. The map is generated as the derived instance Agree.Item.forget (class
Forget of the meta library), and on host derivations as
Refinement.forget_derivation, with
Program.refinements giving each refinement's
Refinement.
Every class the base derives for its item family is derived for the refinement's too. A signature
morphism (Quote[T via q], Include[T via q]) is transported along the forgetful map: the
refinement's Agree.Item.quote keeps the base's clause for the image of each constructor and
recurs over the refined family. When T has an equality instance, a generated
property states that the transported reification is the base's
reification after forgetting, and qvr test checks it. A function written over the base's items
transports by applying it to Agree.Item.forget(x).
Refusals¶
| Refused | Because |
|---|---|
| a variable over an infinite set with no bound | its instances are infinitely many |
| a set that contains itself with no constructor in between | membership would not be decided by shape |
| a meaning with no typing at its category's semantic type | the semantics declare the type every meaning has |
coord[X] without semantics |
synthesis reads the semantic type of X |
| an inherited guard that builds a base item | a base item has no single refinement |
| a lexical rule whose conclusion leaves its premise's span | a lexical item covers its word |
| a refined rule or entry whose image is not in the base | a refinement restricts its base |
NP written bare where it has features, or NP[v] with v outside its set |
features are part of the atom |
a rule whose premise is a bare variable X that a term also holds, such as X \|- Mark(X), beside a lazily instantiated entry or a chart lexical rule |
the agenda spells the instance item there, which would hold its positions as terms; instantiate the entry bounded or compile the lexical rule |
The typing and expansion rules are stated in Lexicons.