Type-Logical Grammar (Lambek calculus)¶
Overview¶
This example is a weighted chart parser whose every rule is a theorem of the associative Lambek calculus L (Lambek, 1958). Categories are built from the atoms S, NP, N with the two slashes and the product, and a chart item span(I, J, X) records that the words in [I, J) derive X. Because each rule is an L-theorem and the chart only combines adjacent spans in order, everything the chart derives is L-derivable: the parser is a sound fragment of L. It is not complete, since L also proves sequents that need hypothetical reasoning, and a sequent requiring that reasoning is given below.
QVR source¶
The grammar is a package of three modules. Its manifest names it and exposes the root module:
type_logical.english adds the fragment's atoms to the categories of L, which std.grammar.lambek builds on the categorial core of std.grammar.ccg:
# The categories of a small English fragment over std.grammar.lambek.
#
# std.grammar.ccg's `Categorial` declares the sentence category S, the
# slashes Fwd(X, Y) = X/Y and Bwd(X, Y) = X\Y (result first, the slash
# saying where the argument is sought), and the chart item
# span(I, J, X); std.grammar.lambek's `Product` adds the product
# Tns(A, B) = A*B with its rules. This signature adds the noun phrase
# and noun atoms.
import std.grammar.lambek as L
#! The atoms of the fragment besides S, with the product of L.
export deduction signature English
extends L.Product
atoms NP, N
type_logical.words is a lexicon module typed against that signature:
# The lexicon of the fragment: one learnable entry per word, its
# meaning a constant naming the word. "gives" takes its two objects as
# one product argument, which currying splits.
import type_logical.english (English)
#! The fragment's words.
export lexicon Words for English
atoms kim, lee, books, every, linguist, book, sleeps, admires, gives, that, today
"Kim" : NP = kim #[learnable]
"Lee" : NP = lee #[learnable]
"books" : NP = books #[learnable]
"every" : Fwd(NP, N) = every #[learnable]
"linguist" : N = linguist #[learnable]
"book" : N = book #[learnable]
"sleeps" : Bwd(S, NP) = sleeps #[learnable]
"admires" : Fwd(Bwd(S, NP), NP) = admires #[learnable]
"gives" : Fwd(Bwd(S, NP), Tns(NP, NP)) = gives #[learnable]
"that" : Fwd(Bwd(N, N), Fwd(S, NP)) = that #[learnable]
"today" : Bwd(S, S) = today #[learnable]
The root module type_logical composes the grammar from the library's rules and scores sentences with it:
# Weighted Type-Logical Grammar: a sound fragment of the Lambek calculus
#
# The grammar extends std.grammar.lambek's `Lambek` signature, the chart
# fragment of the associative Lambek calculus L (Lambek 1958), with
# composition restricted to degree one:
#
# forward_application X/Y, Y |- X
# backward_application Y, X\Y |- X
# forward_harmonic_1_Fwd X/Y, Y/Z |- X/Z
# backward_harmonic_1_Bwd Y\Z, X\Y |- X\Z
# product_introduction A, B |- A*B
# curry_forward X/(A*B) |- (X/B)/A
# curry_backward X\(A*B) |- (X\A)\B
#
# Categories are written result first: Bwd(X, Y) is X\Y, an X that seeks
# a Y on its left. Every rule licenses a sequent with a cut-free L proof
# and uses each premise once, in order, so the chart has no weakening,
# contraction, or permutation and derives only L-theorems. Type raising
# leaves its result category free, so the grammar states the one
# instance it needs, NP |- S/(S\NP), itself. The library's rules carry
# no weight; the learned weights are the lexicon's and the raising
# rule's. The module type_logical.words supplies the words.
import std.grammar.lambek as L
import type_logical.english (English)
import type_logical.words (Words)
object Term : FinSet 16
object LogWeight : Real 1
#! The grammar: L's application, composition of degree one, and
#! product, subject raising, and the fragment's words.
deduction Lambek : Term -> Term [start=S, depth=4, tolerance=0.0]
extends English, L.Lambek[n <= 1]
rule raise_subject : span(I, J, NP) |- span(I, J, Fwd(S, Bwd(S, NP))) #[learnable]
lexicon Words
#! The grammar's contribution to a probabilistic program: the inside
#! log-sum, over every derivation of S that spans the whole input, of
#! the learnable log-weights the derivation uses.
program fit_grammar : Term -> LogWeight
let chart = parse(Lambek, sentence)
score log_Z = chart.goal_weight()
return log_Z
export fit_grammar
Understanding the model¶
L is a logic of ordered resources. A sequent X_1, ..., X_n |- Y holds when the words typed X_1 through X_n, in that order and each used exactly once, form an expression of type Y. L has no structural rules: it cannot reorder premises (exchange), drop one (weakening), or reuse one (contraction). A chart over spans enforces two of those restrictions by construction, since a binary rule combines two adjacent spans and its conclusion covers exactly their union. What the chart cannot enforce by itself is that the category bookkeeping respects resources: a rule Tns(A, B) |- A would discard the B resource while keeping its words in the span, which is weakening in disguise. Every rule here passes a stricter test, namely that its premises entail its conclusion in L.
The lexicon uses this to do linguistic work. that has type (N\N)/(S/NP), so a relative clause must be a sentence missing an NP at its right edge. "Kim admires" is such a clause: type raising lifts Kim to S/(NP\S), and composition with admires : (NP\S)/NP yields S/NP. "Kim gives Lee" is another, once currying turns gives : (NP\S)/(NP*NP) into ((NP\S)/NP)/NP. A clause with a medial gap such as "Kim admires _ today" has no such analysis, which is correct for L: extraction from a non-peripheral position needs a structural rule that L lacks (the multimodal example adds a controlled one).
Walkthrough¶
The constructors are std.grammar.ccg's, written result first: Fwd(A, B) is A/B, an A that seeks a B on its right, and Bwd(A, B) is an A that seeks a B on its left, written B\A in Lambek's notation, which the prose and the sequents below keep. Tns(A, B) is the product A*B. In Lambek's notation the residuation laws read A*B |- C iff A |- C/B iff B |- A\C.
extends English, L.Lambek[n <= 1] takes the chart fragment of L from std.grammar.lambek, with composition restricted to degree one, and the grammar adds one rule of its own. Each licenses a sequent with a cut-free L derivation:
| Rule | Sequent | Cut-free L derivation |
|---|---|---|
forward_application |
A/B, B \|- A |
/L on the axioms B \|- B and A \|- A |
backward_application |
B, B\A \|- A |
\L on the axioms |
forward_harmonic_1_Fwd |
A/B, B/C \|- A/C |
A/B, B/C, C \|- A by /L twice, then /R |
backward_harmonic_1_Bwd |
C\B, B\A \|- C\A |
C, C\B, B\A \|- A by \L twice, then \R |
raise_subject |
NP \|- S/(NP\S) |
NP, NP\S \|- S by \L, then /R. The instance of type raising A \|- B/(A\B) at A = NP, B = S |
product_introduction |
A, B \|- A*B |
*R |
curry_forward |
C/(A*B) \|- (C/B)/A |
A, B \|- A*B by *R, so C/(A*B), A, B \|- C by /L. Then /R twice |
curry_backward |
(A*B)\C \|- B\(A\C) |
A, B \|- A*B by *R, so A, B, (A*B)\C \|- C by \L. Then \R twice |
Composition and type raising are the standard derived rules of L (Lambek, 1958; Moortgat, 1997). The order in curry_forward matters: the argument adjacent to the functor, A, becomes the outer argument, so C/(A*B) becomes (C/B)/A and not (C/A)/B. The converse (C/B)/A |- C/(A*B) is also an L-theorem, but adding it as a rule would close a cycle between the two forms, so the library keeps one direction. curry_backward is its mirror image; no word of this lexicon seeks a product on its left, so it never fires here.
Pattern variables (single uppercase letters such as A, B, I, J) bind at firing time. Type raising is stated at one instance because a rule's conclusion may only mention variables its premises bind: the general schema A |- B/(A\B) leaves B free, which is why the library leaves raising to the grammar. The depth=4 option bounds the constructor depth of a rule's conclusion category. It is what keeps product_introduction from building ever-deeper products over a span, and the lexicon's deepest derived category, the curried ((NP\S)/NP)/NP, sits at that bound. No rule closes a cycle, so the chart needs no #[bounded] rule and no convergence tolerance.
fit_grammar is the probabilistic program over the grammar: it parses the data parameter sentence and scores the chart's goal weight, \(\log Z(s; \mathbf{w}) = \log \sum_d \exp \langle \mathbf{w}, \phi(d) \rangle\), the log-sum over every derivation \(d\) of S over the whole input of the learnable log-weights it uses. The grammar is lexicalized: the library's rules carry no weight of their own, so the learned weights are the lexicon's and that of raise_subject, the one rule the grammar states. A learnable rule's log-weight is allocated per binding of its category variables (§2.1), and raise_subject binds none, so it has one weight, shared by every firing.
Try it¶
Check the chart rules against the calculus¶
The first block types each word, as in the source, and implements an independent decision procedure for L: cut-free backward proof search, which terminates because every sequent rule removes a connective and is complete by Lambek's cut-elimination theorem. It then checks that each chart rule's sequent is an L-theorem and that the two projections a weakening rule would use are not.
import itertools
from functools import lru_cache
import torch
from quivers.dsl import load, loads
PACKAGE = "docs/examples/source/type_logical"
SOURCE = f"{PACKAGE}/type_logical.qvr"
prog = load(SOURCE)
ded = prog.deductions["Lambek"]
atom = lambda name: ("atom", name)
S, NP, N = atom("S"), atom("NP"), atom("N")
over = lambda a, b: ("Fwd", a, b) # a/b
under = lambda b, a: ("Bwd", a, b) # b\a, written result first
prod = lambda a, b: ("Tns", a, b) # a*b
# The lexical type of each word, as the source assigns it; the loop
# confirms that each one is an item of the word's one-word chart.
LEX = {
"Kim": NP, "Lee": NP, "books": NP,
"every": over(NP, N), "linguist": N, "book": N,
"sleeps": under(NP, S), "admires": over(under(NP, S), NP),
"gives": over(under(NP, S), prod(NP, NP)),
"that": over(under(N, N), over(S, NP)), "today": under(S, S),
}
for word, category in LEX.items():
cats = {item[3] for item, _ in ded([word]).chart.items()}
assert category in cats, word
@lru_cache(maxsize=None)
def lambek_proves(gamma, goal):
"""Decide gamma |- goal in the Lambek calculus L by cut-free
backward search. Every rule removes one connective, so the search
terminates, and cut elimination makes it complete."""
if len(gamma) == 1 and gamma[0] == goal and goal[0] == "atom":
return True
if goal[0] == "Fwd" and lambek_proves(gamma + (goal[2],), goal[1]):
return True # /R
if goal[0] == "Bwd" and lambek_proves((goal[2],) + gamma, goal[1]):
return True # \R
if goal[0] == "Tns" and any( # *R
lambek_proves(gamma[:k], goal[1]) and lambek_proves(gamma[k:], goal[2])
for k in range(1, len(gamma))
):
return True
for i, f in enumerate(gamma):
left, right = gamma[:i], gamma[i + 1:]
if f[0] == "Tns" and lambek_proves(left + (f[1], f[2]) + right, goal):
return True # *L
if f[0] == "Fwd" and any( # /L
lambek_proves(gamma[i + 1:j], f[2])
and lambek_proves(left + (f[1],) + gamma[j:], goal)
for j in range(i + 2, len(gamma) + 1)
):
return True
if f[0] == "Bwd" and any( # \L
lambek_proves(gamma[k:i], f[2])
and lambek_proves(gamma[:k] + (f[1],) + right, goal)
for k in range(i)
):
return True
return False
def types(words):
return tuple(LEX[w] for w in words)
def parses(sentence):
return bool(ded(sentence.split()).goal_items)
# Each rule's sequent is an L-theorem.
A, B, C = atom("A"), atom("B"), atom("C")
rule_sequents = {
"forward_application": ((over(A, B), B), A),
"backward_application": ((B, under(B, A)), A),
"forward_harmonic_1_Fwd": ((over(A, B), over(B, C)), over(A, C)),
"backward_harmonic_1_Bwd": ((under(C, B), under(B, A)), under(C, A)),
"raise_subject": ((NP,), over(S, under(NP, S))),
"product_introduction": ((A, B), prod(A, B)),
"curry_forward": ((over(C, prod(A, B)),), over(over(C, B), A)),
"curry_backward": ((under(prod(A, B), C),), under(B, under(A, C))),
}
for name, (gamma, goal) in rule_sequents.items():
assert lambek_proves(gamma, goal), name
# The two projections a "tensor elimination" by weakening would need
# are not L-theorems.
assert not lambek_proves((prod(A, B),), A)
assert not lambek_proves((prod(A, B),), B)
Soundness and the fragment's limits¶
Each positive sentence parses, is L-derivable, and contains a chart item that only the named rule can produce. Each negative sentence would need a structural rule L lacks, and neither the chart nor the decision procedure accepts it. The soundness check then runs the decision procedure on every item of every chart: each must be an L-theorem over the words of its span. The last lines exhibit the incompleteness: NP |- (S/NP)\S is an L-theorem that no chart rule derives.
# Positive sentences, each with the chart item that only a given rule
# can produce.
positives = {
"Kim sleeps": ("backward_application", None),
"every linguist sleeps": ("forward_application", (0, 2, NP)),
"Kim sleeps today": ("backward_harmonic_1_Bwd", (1, 3, under(NP, S))),
"Kim gives Lee books": ("product_introduction", (2, 4, prod(NP, NP))),
"every linguist that Kim admires sleeps": ("forward_harmonic_1_Fwd", (3, 5, over(S, NP))),
"every book that Kim gives Lee sleeps": ("curry_forward", (4, 6, over(under(NP, S), NP))),
}
for sentence, (rule, witness) in positives.items():
words = sentence.split()
chart = ded(words)
assert chart.goal_items, sentence
assert lambek_proves(types(words), S), sentence
if witness is not None:
i, j, category = witness
assert ("span", i, j, category) in dict(chart.chart.items()), (sentence, rule)
print(f"parses {sentence!r:44} (uses {rule})")
# Non-theorems. A permuted order needs exchange; "Kim sleeps Lee" needs
# weakening (Lee is never consumed); "Kim admires" as a sentence needs
# contraction (Kim as both subject and object).
negatives = ["Lee Kim admires", "admires Kim Lee", "Kim sleeps Lee",
"Kim admires", "every linguist that admires Kim sleeps"]
for sentence in negatives:
assert not parses(sentence), sentence
assert not lambek_proves(types(sentence.split()), S), sentence
print(f"rejected {sentence!r}")
# Soundness: every item in every chart is an L-theorem over its span.
checked = 0
for sentence in [*positives, *negatives]:
words = sentence.split()
for item, _ in ded(words).chart.items():
_, i, j, category = item
assert lambek_proves(types(words[i:j]), category), (sentence, item)
checked += 1
print(f"{checked} chart items, every one an L-theorem over its span")
# Incompleteness: object type raising NP |- (S/NP)\S is an L-theorem,
# but no rule of the fragment derives it.
object_raised = under(over(S, NP), S)
assert lambek_proves((NP,), object_raised)
assert ("span", 0, 1, object_raised) not in dict(ded(["Kim"]).chart.items())
Derivations versus readings¶
L has spurious ambiguity: distinct proofs of one sequent that a semantics would not distinguish. The goal weight sums over proofs, so a sentence with more proofs carries more mass at equal parameter values. The Counting semiring makes the proof count visible. "Kim sleeps" has two proofs (backward_application directly, or raise_subject then forward_application) and "Kim gives Lee books" has seven.
# Derivations, not readings: one sentence can have several L proofs.
# Re-scoring the bare deduction in the Counting semiring, with every
# weight left at the semiring's one, counts them.
import re
def unweighted(path):
"""A module's source with every `#[learnable]` annotation removed."""
return re.sub(r"\s*#\[learnable\]", "", open(path).read())
grammar_only = unweighted(SOURCE).split("#! The grammar's contribution")[0]
counting = loads(
"import std.semiring\n"
+ grammar_only.replace("deduction Lambek :", "deduction Lambek[W = std.semiring.Counting] :"),
modules={"type_logical.words": unweighted(f"{PACKAGE}/type_logical/words.qvr")},
roots=(PACKAGE,),
)
count = counting.deductions["Lambek"]
for sentence, expected in [("Kim sleeps", 2), ("Kim admires Lee", 3), ("Kim gives Lee books", 7)]:
n = int(count(sentence.split()).goal_weight())
print(f"{sentence!r}: {n} derivations")
assert n == expected, (sentence, n)
Recovering a distribution over sentences¶
The goal weight \(Z(s; \mathbf{w})\) is unnormalized. Conditioning on sentence length makes it a distribution: \(p(s \mid |s| = n, \mathbf{w}) = Z(s; \mathbf{w}) / \sum_{|s'| = n} Z(s'; \mathbf{w})\), the distribution that sample_corpus draws from exactly by enumerating yields. The block draws every learnable log-weight from a Normal(0, 1) prior, samples 2,000 three-word sentences, refits the weights from zero by MAP (maximum a posteriori) under the same prior, and requires the fitted distribution over the grammatical yields to lie within total-variation distance 0.05 of the generating one. Individual weights are not identified (a sentence's probability depends only on sums of weights along its derivations), so the assertion is on the distribution, which is.
from quivers.stochastic.deduction import sample_corpus
torch.manual_seed(0)
vocabulary = list(LEX)
yields = [
list(words) for words in itertools.product(vocabulary, repeat=3)
if ded(list(words)).goal_items
]
print(f"{len(yields)} grammatical yields of length 3")
def log_probs():
"""log p(s) = log Z(s) - log sum_s' Z(s') over the length-3 yields."""
log_z = torch.stack([ded(words).goal_weight() for words in yields])
return log_z - torch.logsumexp(log_z, dim=0)
# Ground truth: every learnable weight drawn from its Normal(0, 1) prior.
params = list(ded.parameters())
with torch.no_grad():
for p in params:
p.copy_(torch.randn(()))
true_probs = log_probs().exp()
N_SENTENCES = 2000
corpus = sample_corpus(ded, length=3, n_samples=N_SENTENCES, seed=0)
index = {tuple(words): k for k, words in enumerate(yields)}
counts = torch.bincount(
torch.tensor([index[tuple(s)] for s in corpus]), minlength=len(yields)
).to(torch.get_default_dtype())
# MAP fit from a neutral start, under the same Normal(0, 1) prior.
with torch.no_grad():
for p in params:
p.zero_()
optim = torch.optim.Adam(params, lr=0.1)
for step in range(150):
optim.zero_grad()
loss = -(counts * log_probs()).sum() + 0.5 * sum((p ** 2).sum() for p in params)
loss.backward()
optim.step()
with torch.no_grad():
fitted_probs = log_probs().exp()
empirical = counts / counts.sum()
tv = lambda p, q: 0.5 * float((p - q).abs().sum())
uniform = torch.full_like(true_probs, 1.0 / len(yields))
print(f"TV(fitted, true) = {tv(fitted_probs, true_probs):.3f}; "
f"TV(empirical, true) = {tv(empirical, true_probs):.3f}; "
f"TV(uniform, true) = {tv(uniform, true_probs):.3f}")
assert tv(fitted_probs, true_probs) < 0.05
Categorical perspective¶
L is the internal language of a residuated monoid or, proof-relevantly, of a biclosed monoidal category: the product * is the monoidal tensor and the two slashes are its right adjoints, - * B -| -/B and A * - -| A\-. Application is the counit of these adjunctions, composition is composition of internal homs, and curry_forward is one direction of the isomorphism C/(A*B) ≅ (C/B)/A. The chart computes, for each span, a finite set of objects reachable from the lexical types by these maps, weighted in the LogProb semiring. Because the category has no symmetry, diagonal, or projections, no chart item can permute, copy, or discard a word.
Connections to other formalisms¶
The application rules alone give AB grammar (custom rules). Adding composition and type raising gives the core of combinatory categorial grammar (CCG), whose crossed composition rules are not L-theorems. The multimodal example extends this fragment with unary modalities whose structural rule licenses the medial extraction that L rejects here, and the quantifier-scope example adds logical forms.
Cited sources¶
Joachim Lambek. The Mathematics of Sentence Structure. The American Mathematical Monthly, 65(3):154–170, 1958. URL: https://www.tandfonline.com/doi/abs/10.1080/00029890.1958.11989160, doi:10.1080/00029890.1958.11989160.
Michael Moortgat. Categorial Type Logics. In Johan van Benthem and Alice ter Meulen, editors, Handbook of Logic and Language, pages 93–177. Elsevier, 1997. URL: https://www.sciencedirect.com/book/9780444817143/handbook-of-logic-and-language, doi:10.1016/B978-044481714-3/50005-9.