Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facts in Prolog

Tags:

prolog

I'm writing a program about roads...

For example I have road(1, a, b, 2) (road no. 1 from a to b taking up 2 units of fuel). The point is if I have road(1, a, b, 2) I must also have road(1, b, a, 2) but if I use one I cannot use another, otherwise the program will loop.

What can I write so that if the program uses this one fact, it cannot use another?

like image 583
Rachel Anne Farrell Avatar asked May 20 '26 11:05

Rachel Anne Farrell


2 Answers

I'm not sure I understood you, but maybe -

    direct_road(1,a,b,2).
    direct_road(1,b,a,2).

    road(X,A,B,Y) :-
      direct_road(X,A,B,Y),
      direct_road(X,B,A,Y).
like image 160
TamarG Avatar answered May 22 '26 07:05

TamarG


You can use one rule to capture the symmetry, but put it after all other rules with same name and arity. If there exist any solutions for whatever your query is, they will be shown before getting into any sort of infinite loop.

road(R,A,B,F) :- road(R,B,A,F).

like image 40
prusswan Avatar answered May 22 '26 07:05

prusswan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!