I have the following exercise about natural language in Prolog:
Implement the following two operators has and of in such a way that with phrases like: peter has car of john answers to questions such as: Who has What of X
Now, I know that in English language this sound bad because in English we usually say: "peter has john's car", but I am Italian and I have tried to translate the Italian request in English. I hope the concept is however clear.
So I have found the following Prolog solution, saved in a file:
:-op(200,xfx,has).
:-op(100,xfx,of).
peter has car of john.
After that I consult this file in Prolog shell and I can perform the following operations:
?- peter has car of john.
true.
Or:
?- X has Y of Z.
X = peter,
Y = car,
Z = john.
Great, it work, but I have not so clear how it work and I have some questions about the operator priority:
has operator have 200 as priority value. of operator have 100 as priority value.
Referring to the phrase: peter has car of john so it means that Prolog first evaluate this part of the sentence: car of john (because of operator have lower priority respect has operator) and, if it is true, then it evaluate: pater has (result of the previous evalutatuion).
In a few words, I can interpret the original sentence in this way:
peter has (car of john)
Is it correct?
An other question is related to the operator's typology.
In my solution I use the xfx typology for both operators because in this kind of sentences I have no problems related to potential ambiguity with several operators having the same precedence (as in the case: a - b - c in which the operator - must have the form yfx)
My question is: Can I obtain the same result using xfy and\or yfx mixing in some way on my has and of operators?
xfx
means that the operator 'dominates' upon its branches, whilexfy
oryfx
stand for 'list construction', in sense that allow chaining expressions of same priority.With your phrase, you're using the correct op declaration, but you could do with other associativity specifiers, if you see necessity.
I see a possible usage of xfy for
of
, like 'peter has car of john of mary`. AfterI get