The prologue fails and does not back down

Is there a built-in predicate in SWI-Prolog that will always fail And does not allow the machine to backtrack - this means that the program does not start immediately (this is not what fail/0 does)? I could use cuts, but I don't like them.

Doing something like !, fail is not a problem for me, but in order to accomplish what I want, I would have to use cuts in other places, and this is what I don't like.

+4
source share
4 answers

You can use exceptions. Based on your question - this should help. Refer link

+7
source

You can use a mechanism explicitly designed to help you do something, but you don't like it?

You can always use not, which is syntactic sugar for cutting

+4
source

Two alternatives come to mind:

  • Pass the code backtrack(true) or backtrack(false) through the code you want to control and interpret it in the definition of the predicates that you write to quickly execute if it is set to backtrack(false) , or continue if backtrack(true) . Note that this does not actually prevent rollback; it should just enable a quick glitch. Even if your evidence tree is deep, this should provide a quick way to prevent certain code from executing with backtracking.
  • Use exceptions as pointed out by @Xonix (+1). Throwing exceptions will immediately stop creating the evidence tree, and you can pass any term data except to the handler, bypassing any execution - it will probably be faster than the first option, but may not be as portable.

Personally, I used both methods before - the first where I expected the need before writing code, and the second where I do not.

+3
source

Too bad that cuts are needed.

+2
source

Source: https://habr.com/ru/post/1310661/


All Articles