Writing a Prolog rule for messages before a given

2019-08-16 14:36发布

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.

标签: prolog
1条回答
我命由我不由天
2楼-- · 2019-08-16 15:23

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.

查看更多
登录 后发表回答