1

My teacher gave me the following rule for implementing a logical not in prolog:

not( X ) :- X, !, fail.
not( _ ).

I'm having a really hard time understanding it though. I somewhat get that ! means cut, but I'm baffled as to how this works in this scenario, as well as what the second line is trying to achieve. Can anybody please help me understand?

MarksCode
  • 8,074
  • 15
  • 64
  • 133
  • when `cut` operation is passed then the interpreter doesn't go back in the logical tree, therefore it stops trying to check other possible options – Oleksandr Fedotov Mar 18 '18 at 21:12

1 Answers1

2

The implementation is quite simple:

  • Rule: not( X ) :- X, !, fail. states call X to see if it succeeds, if X succeeds then not(X) should fail thus it calls fail at the end but before that you must tell Prolog not examine the second rule (which will succeed) that's why it places the cut (!).

  • Rule: not( _ ). matches the case where in the above rule X failed and thus the above rule failed and Prolog will try other rules that match, via backtracking.

Though the important thing here is to realize that Prolog doesn't have Logical negation but Negation As Failure which is a bit different. You could take a look at my previous question Logical Negation in Prolog to see some differences and understand why it is so difficult (maybe impossible) to have Logical negation.

You could also google about Negation As Failure to understand it better. A simple link that explains it (and also refer the main difference from logical negation) could be found here.

coder
  • 12,832
  • 5
  • 39
  • 53