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.
You could use exceptions. Based on your question - it should help. Refer link
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
Two alternatives come to mind:
- Pass around a
backtrack(true)
orbacktrack(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 tobacktrack(false)
, or to continue ifbacktrack(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. - 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.
Too bad, that's what cuts are for.
精彩评论