Has
Has

Reputation: 121

Prolog DCG with variables

I have DCG sentences with two persons, representing a male and a female. I want to refer to the person metioned in a previous sentence using "he" or "she".

Suppose we have these DCGs:

father --> [Peter].
mother --> [Isabel].

child --> [Guido].
child --> [Claudia].

verb --> [is].
relation --> [father, of].
relation --> [mother, of].

pronoun --> [he].
pronoun --> [she].

adjective --> [a, male].
adjective --> [a, female].

s --> father, verb, relation, child.
s --> mother, verb, relation, child.
s --> pronoun, verb, adjective.

Querying ?- s([Peter, is, father, of, Guido], []). returns true.

How can I make sure that when I query now for ?- s([he, is, a, male], []). should return true only because I already mentioned Peter (a male) in the previous sentence. Otherwise it should return false.

This question uses the same example as here.

Upvotes: 4

Views: 452

Answers (1)

gusbro
gusbro

Reputation: 22585

You can augment your DCG to keep some state (the gender of the last sentence):

father --> ['Peter'].
mother --> ['Isabel'].

child --> ['Guido'].
child --> ['Claudia'].

verb --> [is].
relation --> [father, of].
relation --> [mother, of].

pronoun(he) --> [he].
pronoun(she) --> [she].

adjective --> [a, male].
adjective --> [a, female].

s(G) --> s(none,G).

s(_,he) --> father, verb, relation, child.
s(_,she) --> mother, verb, relation, child.
s(G,G) --> pronoun(G), verb, adjective.

And now you can chain queries using this state:

?- phrase(s(G1),['Peter', is, father, of, 'Guido']), phrase(s(G1,G2),[he, is, a, male]).
G1 = G2, G2 = he

You may want to modify a bit the DCG to constrain the relations (using the Gender parameter). For example you DCG currently accepts 'Peter' is mother of 'Guido' which I'm not sure was intended.

Upvotes: 3

Related Questions