1
votes

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?

1
noun//2 it's unrelated to noun//0 that appears in np --> det, noun. You should show the context where noun//2 is actually used... The X argument it's most likely a kind of lambda expression, it will 'transfer' semantic information among phrase' POS (part of speech). BTW you're not obliged to accept an answer, just because it has been posted, if it doesn't help you to understand your quest - CapelliC
When you see a variable repeated such as in noun(X, woman(X)) it means that unification is occurring between all those variables labeled X. For example, if you had a rule, same(X, X). then queried, same(Foo, bar) you would get the solution, Foo = bar.. noun(X, woman(X)) --> [woman]. will only do this unfication if woman is seen in the input. This is very basic Prolog behavior. A study of introductory material on the Prolog language will provide more information. - lurker
@lurker so what would be the difference if I simply wrote noun --> [woman]. ? P.S. i had a look at introductory material, but some of it is really confusing - Alessandro
noun --> [woman] will recognize the woman in the input stream and that's all. It will collect no further information based upon that success. noun(X, woman(X)) allows you to call noun(A, B) and then if woman is seen on the input, you get B = woman(A). That's a bit abstract I realize, but I have no other context of your problem to know why that's of value in this case, but it is evidently useful in the broader scope of the code you're looking for. You need to check contexts in the code that call noun(...) to see what it does with those arguments. - lurker
@lurker Thanks, that made it a lot clearer - Alessandro

1 Answers

2
votes
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).