views:

234

answers:

4

Is there any build-in predicate in SWI-Prolog that will always fail AND prevent machine from backtracking - it is stop the program from executing 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 more locations and this is what I don't like.

A: 

Too bad, that's what cuts are for.

Amadan
+2  A: 

You could use the mechanism explicitly designed to help you accomplish something, but you don't like it?

You can always use not, which is syntactic sugar for cut fail

patros
+3  A: 

You could use exceptions. Based on your question - it should help. Refer link

Xonix
+1  A: 

Two alternatives come to mind:

  1. Pass around a backtrack(true) or backtrack(false) term through the code you want to control, and interpret it in the definition of the predicates you're writing to fail quickly if it is set to backtrack(false), or to continue if backtrack(true). Note that this won't actually prevent backtracking; it should just enable fast-failure. Even if your proof tree is deep, this should provide a fast way of preventing the execution of certain code on backtracking.
  2. Use exceptions, as suggested by @Xonix (+1). Throwing an exception will terminate the proof tree construction immediately, and you can pass any term data through the exception up to the handler, bypassing any more execution - it will probably be faster than the first option, but may not be as portable.

Personally I've used both methods before - the first where I've anticipated the need before writing the code, the latter where I haven't.

sharky