# Easy iterative deepening of recursion depth

**URL:** <https://swi-prolog.discourse.group/t/easy-iterative-deepening-of-recursion-depth/1140>\
**Category:** General\
**Created:** [September 2, 2019, 8:42am UTC](https://swi-prolog.discourse.group/t/easy-iterative-deepening-of-recursion-depth/1140 "2019-09-02T08:42:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [September 2, 2019, 8:42am UTC](https://swi-prolog.discourse.group/t/easy-iterative-deepening-of-recursion-depth/1140/1 "2019-09-02T08:42:55Z")

</div>

I am almost done with a directive and a meta-calling predicate that I have been [working](https://swi-prolog.discourse.group/t/declarative-predicates-by-limiting-resources/1114/30) on.

The meta-calling predicate looks like this:

```prolog
%! call_iterating_depth(Goal, Options) is multi.
%  
% Calls Goal successively trying deeper recursion depths until Goal succeeds.
% * fails silently if Goal fails.
% * Stops trying new depths on the first depth that succeeds.
% * fails silently if the end(Depth) given as an option is reached.
%
% Options:
% * start(Start): set initial depth to try, default 3.
% * step(Step): set increment between depths, default 1.
% * end(End): set last depth to try, default `inf` (infinite)
 

```

I can contribute this code to the community if there is interest.

There is also a directive called `:- iterate_depth PredIndicator` which wraps the original predicate and allows iterating the recursion depth on existing predicates.

I am now writing the test cases, so I would appreciate any test scenarios that you can think of.

Also I am debating between `inf` and a large number for the default value for the `end(Depth)` option. What do you think makes more sense?

---

<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:** [September 2, 2019, 9:00am UTC](https://swi-prolog.discourse.group/t/easy-iterative-deepening-of-recursion-depth/1140/2 "2019-09-02T09:00:48Z")

</div>

> [@swi](#):
>
> `inf`

I’d go for `infinite` (as between/3). _inf_ is also an abbreviation for clp(fd) bounds meaning negative infinity.

One way to distribute is as a pack. It might be a nice thing for the library as well. I’m still a bit unsure what should be where. Most working eco systems seems to be working mostly with external packages.

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [September 2, 2019, 9:06am UTC](https://swi-prolog.discourse.group/t/easy-iterative-deepening-of-recursion-depth/1140/3 "2019-09-02T09:06:41Z")

</div>

> [@jan](#):
>
> I’d go for `infinite` (as between/3). _inf_ is also an abbreviation for clp(fd) bounds meaning negative infinity.

I see, actually I was meaning to choose between `inf` and a large number like 20 000. `inf` would cause non-termination by default for predicates which are infinitely recursive and 20 000 will cause the predicate to terminate.

I chose `inf` because I can do `Start =< End`, with `End == inf`. I don’t think that will work with `infinite`.

> [@jan](#):
>
> It might be a nice thing for the library as well. I’m still a bit unsure what should be where

If in the library I would think in the same place as the other limiting meta-calls, together with `call_with_depth_limit/3` and the other ones. That seems reasonable to me.

Do you prefer a pack though?

---

<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:** [September 2, 2019, 9:22am UTC](https://swi-prolog.discourse.group/t/easy-iterative-deepening-of-recursion-depth/1140/4 "2019-09-02T09:22:14Z")

</div>

I though `Start =< inf` would not work, but it does as `inf` is the arithmetic function for the float _Inf_ NaN value. Not very elegant to rely on that, I fear. I would go for

```
( End == infinite -> true ; Start =< End )

```

It is a bit longer, but it compiles quite efficiently and doesn’t depend on corner cases.

> [@swi](#):
>
> f in the library I would think in the same place as the other limiting meta-calls, together with `call_with_depth_limit/3` and the other ones. That seems reasonable to me.

Yes, except that these are built-ins. I want to keep the stuff in `boot` relatively small. What would make sense is to make a new real library and move this stuff together with your’s there. We only need a good library name, but we can combine all the resource limiting calls in there, possibly except for the call\_with\_time\_limit/2 as that depends on nasty OS dependent stuff that doesn’t belong to the core.
