Search code examples
prologdcg

Definite Clause Grammars Prolog


I am trying to write a grammar for the English language in Prolog, with some basic rules, such as:

s --> np, vp.
np --> pn.
np --> det, noun.
pn --> [vincent].
pn --> [mia].
det --> [a].

I do understand how these work and how to query them using the phrase statement, but when it comes to rules like:

noun(X, woman(X)) --> [woman].
iv(Y, snort(Y)) --> [snorts].

I'm lost. What do these mean? Why is variable X repeated?


Solution

  • noun(X, woman(X)) --> [woman].
    

    is the same as

    noun(X, woman(X), A, B) :- A = [woman | B].
    

    This of course leaves the X variable uninstantiated. Perhaps you intended

    noun(X, woman(X)) --> [woman, X].
    

    which is equivalent to

    noun(X, woman(X), A, B) :- A = [woman, X | B].
    

    The repeated logical variables, as usual, indicate the sameness in unification:

    4 ?- noun(X,Y,[woman,1],Z).
    X = 1,
    Y = woman(1),
    Z = [].

    5 ?- phrase(noun(X,Y),[woman,1]).
    X = 1,
    Y = woman(1).