# How does this program work?

**URL:** <https://swi-prolog.discourse.group/t/how-does-this-program-work/9174>\
**Category:** General\
**Created:** [August 11, 2025, 10:54pm UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174 "2025-08-11T22:54:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![emummel20](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emummel20/32/4952_2.png) [@emummel20](https://swi-prolog.discourse.group/u/emummel20)\
**Post date:** [August 11, 2025, 10:54pm UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/1 "2025-08-11T22:54:26Z")

</div>

Hi, quick question

```prolog
main(X) :-
  foo(a,X),
  foo(b,X).

foo(V,[V]).
foo(V,[V|_]).
foo(V,[_|Rest]) :- foo(V,Rest).

```

we get `X = [a,b]` but I’m a little confused how backtracking works in this case given my (mis)understanding of DFS.

Is this not how DFS backtracking should work in this case?

1. 

```prolog
X = [a].

```

2. 

```prolog
X = [a], X = [b]. % fail

```

3. 

```prolog
X = [a], X = [b|_]. % fail

```

4. 

```prolog
X = [a], X = [_|[b]]. % fail

```

5. 

```prolog
X = [a], X = [_|[b|_]]. % fail

```

6. 

```prolog
X = [a], X = [_|[_|[b]]]. % fail

```

7. 

```prolog
X = [a], X = [_|[_|[b|_]]]. % fail

```

8. 

```prolog
X = [a], X = [_|[_|[_|[b]]]]. % fail

```

and so on.

If we’re making a recursive call to foo, which itself has to exhaustively search each foo rule, which includes a recursive call to foo, which itself has to exhaustively search each foo rule …. then how do we ever backtrack to foo(a,X) unifying as `X = [a|_]`?

---

<div class="post-metadata">

**Author:** ![emummel20](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emummel20/32/4952_2.png) [@emummel20](https://swi-prolog.discourse.group/u/emummel20)\
**Post date:** [August 11, 2025, 11:14pm UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/2 "2025-08-11T23:14:53Z")

</div>

I mean I see how it _happens_ to work

```prolog
main(X) :-
  foo(a,X),writeln(X),
  foo(b,X),writeln(X).

foo(V,[V]) :- write(V),writeln(" using one").
foo(V,[V|_]) :- write(V),writeln(" using two").
foo(V,[_|Rest]) :- write(V),writeln(" using three"),foo(V,Rest).

```

```prolog
?- main(X).
a using one
[a]
b using three
a using two
[a|_4518]
b using three
b using one
[a,b]
X = [a, b] .

```

It looks like we backtrack to foo(a,X) after foo(b,X) fails the _first_ recursive call **[1]**? Why? Are predicates called from the body of a rule treated differently than predicates called from a “higher level”? In the rule `foo(V,[_|Rest]) :- foo(V,Rest).` why isn’t the call to `foo(V,Rest).` exhaustive?

**[1]** It’s also interesting in my output here we go from `b using three` to `a using two` without `b using one` first. How does that work? It’s almost like `foo(V,Rest).` wasn’t actually called? I feel like I’m fundamentally misunderstanding dfs backtracking then.

Likewise, after `a` unifies we go straight to `b using three` without one and two? Is it some optimization that the compiler knows they won’t work so they’re not tried?

I guess another way of asking this:

Is there an optimization at play here that detects an infinite loop of failure and backtracks based on that optimization? If so, how does that work?

---

<div class="post-metadata">

**Author:** ![emummel20](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emummel20/32/4952_2.png) [@emummel20](https://swi-prolog.discourse.group/u/emummel20)\
**Post date:** [August 12, 2025, 10:09am UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/4 "2025-08-12T10:09:09Z")

</div>

Thanks @anon19431257 . Let me chew on this and get back to you.

---

<div class="post-metadata">

**Author:** ![emummel20](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emummel20/32/4952_2.png) [@emummel20](https://swi-prolog.discourse.group/u/emummel20)\
**Post date:** [August 13, 2025, 12:22am UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/6 "2025-08-13T00:22:41Z")

</div>

Damn it, caught my mistake. It’s because the recursive call `foo(b,[]) = foo(V,[_|Rest]).` fails.

Got it. Sorry, thanks!

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [August 13, 2025, 1:02am UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/7 "2025-08-13T01:02:36Z")

</div>

> [@emummel20](#):
>
> ```prolog
> foo(V,[V]).
> foo(V,[V|_]).
> 
> ```

This will succeed _twice_ for:

```prolog
?- foo(X, [X]).
true ;
true.

```

Could rewrite as:

```prolog
foo(V, [V|T]) :-
    foo_v_tail(T).

foo_v_tail([]).
foo_v_tail([_|_]).

```

---

<div class="post-metadata">

**Author:** ![emummel20](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emummel20/32/4952_2.png) [@emummel20](https://swi-prolog.discourse.group/u/emummel20)\
**Post date:** [August 13, 2025, 10:00am UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/8 "2025-08-13T10:00:33Z")

</div>

Sorry, can you clarify?

```prolog
?- [user].
|: main(X) :-
|: foo(a,X),
|: foo(b,X).
|: 
|: foo(V, [V|T]) :-
|: foo_v_tail(T).
|: foo_v_tail([]).
|: foo_v_tail([_|_]).
|: ^D% user://1 compiled 0.01 sec, 4 clauses
true.

?- main(X).
false.

```

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [August 13, 2025, 10:13am UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/9 "2025-08-13T10:13:13Z")

</div>

> [@emummel20](#):
>
> can you clarify?

I did not say to _remove_ the `foo(V,[_|Rest])`predicate 😉

---

<div class="post-metadata">

**Author:** ![emummel20](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emummel20/32/4952_2.png) [@emummel20](https://swi-prolog.discourse.group/u/emummel20)\
**Post date:** [August 13, 2025, 10:29am UTC](https://swi-prolog.discourse.group/t/how-does-this-program-work/9174/10 "2025-08-13T10:29:54Z")

</div>

ah gotcha thanks!
