# Calling predicates from source code & backtracking

**URL:** <https://swi-prolog.discourse.group/t/calling-predicates-from-source-code-backtracking/5704>\
**Category:** General\
**Created:** [August 21, 2022, 10:58am UTC](https://swi-prolog.discourse.group/t/calling-predicates-from-source-code-backtracking/5704 "2022-08-21T10:58:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexeyKosarchuk](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/alexeykosarchuk/32/4466_2.png) [@AlexeyKosarchuk](https://swi-prolog.discourse.group/u/AlexeyKosarchuk)\
**Post date:** [August 21, 2022, 10:58am UTC](https://swi-prolog.discourse.group/t/calling-predicates-from-source-code-backtracking/5704/1 "2022-08-21T10:58:14Z")

</div>

Hello.  
I’ve encountered some strange behaviour.  
I call some predicate from source by using following syntax:

:- my\_predicate(parameters).

inside my\_predicates uses undo/1 as part of backtrackable assertions. But they are undone immediately after predicate is finished. Using cuts didn’t help me. I assume that prolog engine undoes everything because undo/1 has nothing to do with choicepoints and cuts. It simply cleares of everything that is put in trail. Problem is that my\_predicate is supposed to be used in other contexts where undoing asserts are expected, so I can’t just use non-backtrackable version of assert inside it.

Is there a way to manipulate undo records to emulate something like cut for them?  
Or I just better use some global flag to supress call to undo/1?

EDIT #1: Another question arised: if undo records are not purged but cuts, does it imply that theese records might accumulate over program working even if by design program should never backtrack past undo/1 calls? Can it possibly lead to memory leaks?

EDIT #2: What is the logic of backtracking after :- or ?- calls in source code?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [August 21, 2022, 3:06pm UTC](https://swi-prolog.discourse.group/t/calling-predicates-from-source-code-backtracking/5704/2 "2022-08-21T15:06:18Z")

</div>

> [@AlexeyKosarchuk](#):
>
> What is the logic of backtracking after :- or ?- calls in source code?

The control structure of the compiler is _failure driven_. I.e., there is a predicate that on backtracking produces (expanded) Prolog terms and the next step which adds these to the database and fails until we see `end_of_file`.

I have little clue what problem you are trying to solve, but this route is not going to work.

---

<div class="post-metadata">

**Author:** ![AlexeyKosarchuk](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/alexeykosarchuk/32/4466_2.png) [@AlexeyKosarchuk](https://swi-prolog.discourse.group/u/AlexeyKosarchuk)\
**Post date:** [August 21, 2022, 4:29pm UTC](https://swi-prolog.discourse.group/t/calling-predicates-from-source-code-backtracking/5704/3 "2022-08-21T16:29:29Z")

</div>

I’am writing program which procedurally generates some model, expressed as set of dynamic facts. In that process program backtracks to find consistent solution, therefore I use backtrackable assertions using undo/1. For testing purposes I sometimes call it’s predicates from REPL or from sources directly. REPL can be switched to recursive mode, but with source-code calls it is not possible as I understand.

As a stopgap measure I switched to another b\_assert implementation, using ;/2. For now it works. Then I will do some scaffolding code to work around this issue.
