abstract#
Abstract categorial grammars after de Groote’s Towards abstract categorial
grammars (2001): a vocabulary
generates a free closed category and a Lexicon is a functor between
two of them, which is all there is to a grammar.
Summary#
An implicative type, the type of an abstract categorial grammar. |
|
An exponential object in an abstract categorial grammar. |
|
An abstract diagram is a closed diagram with words as boxes, i.e. a derivation in an abstract categorial grammar. |
|
An abstract box is a closed box in an abstract diagram. |
|
The evaluation of an exponential type. |
|
The coevaluation of an exponential type, i.e. the dagger of an Eval. |
|
The currying of an abstract diagram. |
|
The copy of an abstract type, or its discard when |
|
The discard of an abstract type, i.e. a copy with zero legs. |
|
A permutation of abstract types. |
|
The symmetric swap of two abstract types. |
|
A trace in an abstract categorial grammar. |
|
A formal sum of abstract diagrams. |
|
An abstract functor is a closed functor with abstract diagrams as domain. |
|
A term in the internal language of an abstract categorial grammar. |
|
A constant of the vocabulary of an abstract categorial grammar. |
|
A variable term in an abstract categorial grammar. |
|
The application of an abstract term to another. |
|
The abstraction of a variable in an abstract term. |
Vocabularies and lexicons#
A vocabulary is a higher-order signature: atomic types and constants, each
with an implicative type x >> y built from the atoms, e.g. the words of a
language with their grammatical types. It generates a free closed category,
whose morphisms are the lambda terms of its internal language, here
Term. Nothing forces a term to be linear: the closed categories of
DisCoPy are markov by design, so a variable may be copied and discarded, as
the variables of ground type are in
the semantics of the example below; the paper’s linear terms are the special
case where every variable occurs once, see is_linear. A Lexicon
from one vocabulary to another sends atomic types to types and constants to
terms of the image of their types: it is a Functor between two free
closed categories, and lexicons compose. A lexicon is all there is to a
grammar: its abstract vocabulary is the atoms and constants it is defined on,
its abstract language the closed terms over them and its object language their
image. The paper’s distinguished type s picks the sentences out of the
abstract language, the input of a parser to come.
Strings#
Strings are the paper’s, section 4: one atomic type Position and a
string is a map from positions to positions, a term of type
String = Position >> Position. A word is a constant of that type, the
empty string is the identity Position(lambda x: x) and concatenation is
composition, John.compose(seeks) for John seeks, see
discopy.closed.TermBase.compose(): the paper writes it
lambda z. x (y z) with the first word applied last, here the first word
is applied first, so a string reads its positions in order.
Example
The two readings of Montague’s John seeks a unicorn share one syntax, with a constant for each reading of seeks in the abstract vocabulary:
>>> n, np, s = map(Ty, ("n", "np", "s"))
>>> J, U, A = np("J"), n("U"), (n >> np)("A")
>>> S_re, S_dicto = ((np >> (np >> s))(S) for S in ("S_re", "S_dicto"))
>>> John, seeks, a, unicorn = map(
... String, ("John", "seeks", "a", "unicorn"))
>>> seek = String(lambda x: String(lambda y: x.compose(seeks, y)))
>>> syntax = Lexicon(
... ob_map={n: String, np: String, s: String},
... ar_map={J: John, U: unicorn, A: String(lambda x: a.compose(x)),
... S_re: seek, S_dicto: seek})
>>> from discopy.python import Function
>>> words = Functor(
... {Position: list}, lambda w: lambda: lambda xs: xs + [w.name],
... cod=Function)
>>> for reading in (S_re(J)(A(U)), S_dicto(J)(A(U))):
... print(*words(syntax(reading))()([]))
John seeks a unicorn
John seeks a unicorn
The paper’s semantic lexicon reads noun phrases as quantifiers. The image of
a copies its variable of ground type, so the semantic terms are not linear,
and the two readings are two closed terms of type t:
>>> e, t = Ty("e"), Ty("t")
>>> Predicate, Quantifier = e >> t, (e >> t) >> t
>>> JOHN, UNICORN = e("JOHN"), Predicate("UNICORN")
>>> TRY_TO, FIND = (e >> Quantifier)("TRY_TO"), (e >> Predicate)("FIND")
>>> exists, and_ = Quantifier("exists"), (t >> (t >> t))("and")
>>> some = Predicate(lambda P: Predicate(lambda Q: exists(
... e(lambda x: and_(P(x))(Q(x))))))
>>> seek_re = Quantifier(lambda P: Quantifier(lambda Q: Q(e(lambda x: P(
... e(lambda y: TRY_TO(y)(e(lambda z: FIND(z)(x)))))))))
>>> seek_dicto = Quantifier(lambda P: Quantifier(lambda Q: P(e(lambda x:
... TRY_TO(x)(e(lambda y: Q(e(lambda z: FIND(y)(z)))))))))
>>> semantics = Lexicon(
... ob_map={n: Predicate, np: Quantifier, s: t},
... ar_map={J: Predicate(lambda P: P(JOHN)), U: UNICORN,
... A: some, S_re: seek_re, S_dicto: seek_dicto})
>>> assert not some.is_linear
>>> de_re, de_dicto = semantics(S_re(J)(A(U))), semantics(S_dicto(J)(A(U)))
>>> assert de_re != de_dicto and de_re.cod == de_dicto.cod == t
>>> assert not de_re.freevars and not de_dicto.freevars
A categorial grammar is a vocabulary of its own, a planar one: a lexicon out
of it is a discopy.grammar.categorial.Functor into abstract terms,
sending each atom to a type and each word to a term of the image of its type,
its strings written per word like every lexicon of the paper.
Diagram.from_categorial() forgets planarity instead, collapsing left and
right exponentials into the one exponential of a closed category, so that a
derivation becomes a closed term: application evaluates, composition and type
raising curry, and crossed composition composes.
>>> from discopy.grammar import categorial
>>> N, S = categorial.Ty("n"), categorial.Ty("s")
>>> Alice, loves, Bob = N("Alice"), ((N >> S) << N)("loves"), N("Bob")
>>> LOVES = String("loves")
>>> strings = categorial.Functor(
... ob_map={N: String, S: String},
... ar_map={Alice: String("Alice"), Bob: String("Bob"),
... loves: String(lambda o: String(
... lambda x: x.compose(LOVES, o)))},
... cod=Diagram)
>>> print(*words(strings(Alice(loves(Bob), left=True)))()([]))
Alice loves Bob
>>> print(Alice(loves(Bob), left=True).to_abstract())
loves(Bob)(Alice)