开发者

Prolog, fail and do not backtrack

开发者 https://www.devze.com 2022-12-31 03:09 出处:网络
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)开发者_开发知

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:

  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.


Too bad, that's what cuts are for.

0

精彩评论

暂无评论...
验证码 换一张
取 消