Skip to content

std.grammar.ccg

Combinatory categorial grammar: its combinators as deduction signatures.

Categories are built from a grammar's own atoms with two slashes, written result first: Fwd(X, Y) is X/Y, which takes a Y on its right, and Bwd(X, Y) is X\Y, which takes a Y on its left. A chart item span(I, J, X) says that the words from position I up to J derive X.

The signatures compose: Categorial declares the slashes, the item, and the sentence category S; Application adds the two application rules; HarmonicComposition and CrossedComposition add Steedman's generalized composition of degree one to three as rule schemata, and Composition takes both. A grammar extends the signatures it uses, restricted to the degrees it wants, and declares its other atoms in a signature extending Categorial:

import std.grammar.ccg as C

object Word : FinSet 8

deduction signature English
    extends C.Categorial
    atoms NP, N

deduction Grammar : Word -> Word [start=S, depth=6]
    extends English, C.Application, C.HarmonicComposition[n <= 1]
    atoms kim, sleeps
    lexicon
        "Kim" : NP = kim
        "sleeps" : Bwd(S, NP) = sleeps

A spine Y |e Z ... n is Y taking n arguments through the slash e, so the instance of forward_harmonic at degree n is B^n>: X/Y with (...(Y/Z1)...)/Zn derives (...(X/Z1)...)/Zn.

A grammar that also extends Reified reifies its chart items as Category values: a slash as Over or Under, an item as the category it spans, and each atom it adds, its lexicon's included, as the clause it gives in its own Quote (there is no default clause), written with atom and atoms:

import std.grammar.ccg as C

object Word : FinSet 8

deduction signature English
    extends C.Categorial
    atoms NP

meta define english : Morph !{} =
    let categories <- C.atoms("English", SeqCons("NP", SeqNil))
    let words <- C.atoms("Grammar", SeqCons("kim", SeqCons("sleeps", SeqNil)))
    let clauses <- seq_append[MorphClause](categories, words)
    return Morph(clauses)

deduction Grammar : Word -> Word [start=S, depth=6] deriving (Quote[C.Category via english])
    extends English, C.Reified, C.Application
    atoms kim, sleeps
    lexicon
        "Kim" : NP = kim
        "sleeps" : Bwd(S, NP) = sleeps

Such a grammar has the inclusion of the core's items and the property that its reification of an included item is the core's; qvr test runs it.

The examples on this page run in this module, which opens std.grammar.ccg:

import std.grammar.ccg
open std.grammar.ccg

The rules of its deduction signatures are pinned by the specification module, which declares grammars over them and tests the sentences each derives.

Application (deduction signature)

deduction signature Application

Forward and backward application: X/Y, Y derives X, and Y, X\Y derives X.

Categorial (deduction signature)

deduction signature Categorial

The sentence category, the slashes, and the chart item every categorial grammar shares.

Category (family)

family Category : Type deriving (Eq, Ord, Show)

Constructors:

  • Atom: constructor Atom : String -> Category
  • Over: constructor Over : Category * Category -> Category
  • Times: constructor Times : Category * Category -> Category
  • Under: constructor Under : Category * Category -> Category

A category written out: an atom by its name, X/Y as Over(X, Y), X\Y as Under(X, Y), and the product of X and Y as Times(X, Y).

Example: a transitive verb.

example
expect Over(Under(Atom("S"), Atom("NP")), Atom("NP")) == Over(Under(Atom("S"), Atom("NP")), Atom("NP"))
expect not (Over(Atom("S"), Atom("NP")) == Under(Atom("S"), Atom("NP")))

Composition (deduction signature)

deduction signature Composition

Harmonic and crossed composition together.

CrossedComposition (deduction signature)

deduction signature CrossedComposition

Crossed composition of degree one to three: X/Y composes with a category that takes its arguments through backward slashes (B^n×>), and X\Y with one that takes them through forward slashes (B^n×<). Restrict the degree with extends CrossedComposition[n <= k].

Dir (index)

index Dir = Fwd | Bwd

Constructors:

  • Bwd
  • Fwd

The direction of a slash: Fwd takes its argument on the right, Bwd on the left.

HarmonicComposition (deduction signature)

deduction signature HarmonicComposition

Harmonic composition of degree one to three: X/Y composes with a category that takes its arguments through forward slashes (B^n>), and X\Y with one that takes them through backward slashes (B^n<). Restrict the degree with extends HarmonicComposition[n <= k].

Reified (deduction signature)

deduction signature Reified deriving (Quote[Category via categorial_quote])

Reification: a deduction extending Reified quotes its chart items as Category values, and gives a clause for each atom it adds.

atom (computation)

meta define atom(constructor : String, label : String) : MorphClause !{}

The clause quoting the item constructor constructor as Atom(label).

atoms (computation)

meta define atoms(owner : String, labels : Seq[String]) : Seq[MorphClause] !{}

The clauses quoting each atom label that owner declares as Atom(label): atoms("English", SeqCons("NP", SeqCons("N", SeqNil))) quotes English.NP and English.N.