0
votes

I'm kind of new to prolog and logical programming in general. Can anyone tell me the difference between these two set of prolog rules?

  1. number_of_parents(adam,0):-!.
    number_of_parents(eve,0):-!.
    number_of_parents(X,2).

  2. number_of_parents2(adam,N):-!, N=0.
    number_of_parents2(eve,N):-!, N=0.
    number_of_parents2(X,2).

To me they pretty much represent the same set of logic. But, why does it return "yes" if I call "number_of_parents(eve,2)."? I did a trace on it, but am still having a hard time figuring it out. A step by step explanation is much appreciated. Thanks.

1

1 Answers

0
votes

let's talk Prolog:

9 ?- [stackoverflow].
Warning: /home/carlo/prolog/stackoverflow.pl:8:
    Singleton variables: [X]
Warning: /home/carlo/prolog/stackoverflow.pl:12:
    Singleton variables: [X]
% stackoverflow compiled into stackoverflow 0.01 sec, 4 clauses
true.

10 ?- leash(-all),trace.
true.

[trace] 11 ?- number_of_parents(eve,2).
   Call: (6) stackoverflow:number_of_parents(eve, 2)
   Exit: (6) stackoverflow:number_of_parents(eve, 2)
true.

[trace] 12 ?- number_of_parents2(eve,2).
   Call: (6) stackoverflow:number_of_parents2(eve, 2)
   Call: (7) 2=0
   Fail: (7) 2=0
   Fail: (6) stackoverflow:number_of_parents2(eve, 2)
false.

It's clear that the third rule of first set is (wrongly) engaged, while in the second set identities are correctly represented.

BTW nice code...