Prolog, fallar y no lo hacen retroceso
-
04-10-2019 - |
Pregunta
¿Hay alguna acumulación en el predicado en SWI-Prolog que siempre va a fallar y prevenir la máquina de dar marcha atrás - es detener el programa se ejecute inmediatamente (esto no es lo que hace fail/0
)?
Podría utilizar los recortes, pero no como ellos.
Hacer algo como !, fail
no es un problema para mí, pero a fin de lograr lo que yo quiero, yo tendría que recurrir a recortes en más lugares y esto es lo que no me gusta.
Solución
You could use exceptions. Based on your question - it should help. Refer link
Otros consejos
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.