Third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including the full text).

12.6.5 Enforcing Constraints

Natural language imposes constraints which, for example, disallow sentences such as "a students eat." Words in a sentence must satisfy some agreement criteria. "A students eat" fails to satisfy the criterion of number agreement, which specifies whether the nouns and verbs are singular or plural.

Number agreement can be enforced in the grammar by parametrizing the non-terminals by the number and making sure that the numbers of the different parts of speech agree. You only add an extra argument to the relevant non-terminals.


% A sentence is a noun phrase followed by a verb phrase.

sentence(T0,T2,Num,s(NP,VP))←
     noun_phrase(T0,T1,Num,NP)∧
     verb_phrase(T1,T2,Num,VP).

% A noun phrase is empty or a determiner followed by modifiers followed by a noun followed by an optional prepositional phrase.

noun_phrase(T,T,Num,nonp).
noun_phrase(T0,T4,Num,np(Det,Mods,Noun,PP))←
     det(T0,T1,Num,Det)∧
     modifiers(T1,T2,Mods)∧
     noun(T2,T3,Num,Noun)∧
     pp(T3,T4,PP).

% A verb phrase is a verb, followed by a noun phrase, followed by an optional prepositional phrase.

verb_phrase(T0,T3,Num,vp(V,NP,PP))←
     verb(T0,T1,Num,V)∧
     noun_phrase(T1,T2,N2,NP)∧
     pp(T2,T3,PP).

% An optional prepositional phrase is either nothing or a preposition followed by a noun phrase. Only the null case is given here.

pp(T,T,nopp).

% Modifiers is a sequence of adjectives. Only the null case is given.

modifiers(T,T,[]).

% The dictionary.

det([a|T],T,singular,indefinite).
det([the|T],T,Num,definite).
noun([student|T],T,singular,student).
noun([students|T],T,plural,student).
verb([eats|T],T,singular,eat).
verb([eat|T],T,plural,eat).
Figure 12.9: Grammar to enforce number agreement and build parse tree

Example 12.37: The grammar of Figure 12.9 does not allow "a students," "the student eat," or "the students eats," because all have number disagreement, but it allows "a green student eats," "the students," or "the student," because "the" can be either singular or plural.

To parse the sentence "the student eats," you issue the query

ask sentence([the,student,eats],[],Num,T)

and the answer returned is

Num=singular,
T=s(np(definite,[],student,nopp),vp(eat,nonp,nopp)).

To parse the sentence "the students eat," you issue the query

ask sentence([the,students,eat],[],Num,T)

and the answer returned is

Num=plural,
T=s(np(definite,[],student,nopp),vp(eat,nonp,nopp)).

To parse the sentence "a student eats," you issue the query

ask sentence([a,student,eats],[],Num,T)

and the answer returned is

Num=singular,
T=s(np(indefinite,[],student,nopp),vp(eat,nonp,nopp)).

Note that the only difference between the answers is whether the subject is singular and whether the determiner is definite.