prologprolog-cut

The meaning of (:- !,) in Prolog


I am a newbie to Prolog and having a hard time understanding the meaning of the cut operator, !, in different contexts.

For instance, what is the effect of it in this line here? Or how to read this line in plane English

rewrite(p => q, F) :- !, rewrite(~ p or q, F).

Solution

  • This will be one of several rewrite/2 predicates.

    The cut is intended to mean that this is the only rule that handles p => q as the first argument, so do not backtrack and check the other rewrite/2 predicates below this one.

    Such a cut is useful for performance, and to avoid a "catch-all"/default rewrite(P, F) predicate which might exist.

    Prolog's debugging features are hugely useful to show what is going on. Start with trace, at https://www.swi-prolog.org/pldoc/man?section=debugger