Writing a Prolog rule for messages before a given

2019-08-16 14:34发布

问题:

How do I write a Prolog rule that given two people, tests if there has been a message sent in either direction between them before a given date?

So far I have:

?- MBefore(x,y) :- message(x,y,d1), message(y,x,d1), d1@<Date.

回答1:

mbefore(X,Y,D) :- message(X,Y,D1), D > D1 ; message(Y,X,D1), D > D1.

Here ',' means "and", ';' means "or". Both alternatives of the "or" combination are tried, independently. First the system attempts to prove the first alternative (to the left of ;), then the search backtracks, any logical variables are restored to their previous states, and the second alternative (to the right of ';') is tried. That's why we can use the same logvar D1 in both alternatives of the "or"-combination.

Also, ':-' means "holds, if the following can be proven".

If any of the above is new for you, you really should study the basics of the language through some tutorials or textbooks. "The Art of Prolog" is an excellent introductory book.



标签: prolog