# Choice points with lists

**URL:** <https://swi-prolog.discourse.group/t/choice-points-with-lists/8882>\
**Category:** Help!\
**Created:** [March 14, 2025, 1:30am UTC](https://swi-prolog.discourse.group/t/choice-points-with-lists/8882 "2025-03-14T01:30:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjkillen](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sjkillen/32/5466_2.png) [@sjkillen](https://swi-prolog.discourse.group/u/sjkillen)\
**Post date:** [March 14, 2025, 1:30am UTC](https://swi-prolog.discourse.group/t/choice-points-with-lists/8882/1 "2025-03-14T01:30:24Z")

</div>

I’m using: SWI-Prolog version 9.0.4 for x86\_64-linux

With

```prolog
a(1).
a(2).

```

Querying `a(1)` seems to “yield a single choice point” as the REPL finishes immediately and does not prompt for another answer.

However, with the program

```prolog
a([_]).
a([_, _]).

```

Querying with `a([1])` returns true but prompts for another answer.  
Is a choice point being created for `a([_, _])`? if so, why since `a([_, _])` does not unify with `a([1])

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 14, 2025, 5:51am UTC](https://swi-prolog.discourse.group/t/choice-points-with-lists/8882/2 "2025-03-14T05:51:06Z")

</div>

> [@sjkillen](#):
>
> Querying `a(1)` seems to “yield a single choice point” as the REPL finishes immediately and does not prompt for another answer.

Where did you find this terminology? I would have said that “there are no choice points” after `?- a(1).`

> [@sjkillen](#):
>
> Is a choice point being created for `a([_, _])`? if so, why since `a([_, _])` does not unify with `a([1])`

A choice point means there **might be** additional solutions. In this case there might be, but upon further computation it turns out that there are none.

In some cases the Prolog implementation can correctly **predict** that there will be no more solutions. This is somehow related to the [topic of indexing](https://www.swi-prolog.org/pldoc/man?section=jitindex).

---

<div class="post-metadata">

**Author:** ![sjkillen](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sjkillen/32/5466_2.png) [@sjkillen](https://swi-prolog.discourse.group/u/sjkillen)\
**Post date:** [March 14, 2025, 8:04am UTC](https://swi-prolog.discourse.group/t/choice-points-with-lists/8882/3 "2025-03-14T08:04:59Z")

</div>

> [@Boris](#):
>
> Where did you find this terminology? I would have said that “there are no choice points” after `?- a(1).`

From myself, mostly due to a lack of understanding of Prolog implementation 🙂 Thanks for the correction.

> [@Boris](#):
>
> In some cases the Prolog implementation can correctly **predict** that there will be no more solutions. This is somehow related to the [topic of indexing](https://www.swi-prolog.org/pldoc/man?section=jitindex).

I think this is the answer I was looking for, thanks for the rabbit hole to explore 🙂

---

<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:** [March 14, 2025, 9:05am UTC](https://swi-prolog.discourse.group/t/choice-points-with-lists/8882/4 "2025-03-14T09:05:39Z")

</div>

> [@sjkillen](#):
>
> ```prolog
> a([_]).
> a([_, _]).
> 
> ```

The indexing distinction for lists is `[]` (empty list) vs `[_|_]` (non-empty list).

`[_]` and `[_, _]` are both non-empty lists, so have no indexing distinction between them.

Perhaps they could be put inside meaningful terms having different term names, so that the term name is the indexing distinction? This depends on the meaning/significance/classification of 1 vs 2 elements in the list, of course. Basic example:

```prolog
a(one([_])).
a(two([_, _])).

```

```prolog
?- a(one(L)).
L = [_]. % No unwanted choicepoint

```

---

<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:** [April 26, 2025, 11:36am UTC](https://swi-prolog.discourse.group/t/choice-points-with-lists/8882/6 "2025-04-26T11:36:20Z")

</div>

> [@sjkillen](#):
>
> Querying with `a([1])` returns true but prompts for another answer.

An easy fix is to use table/1, e.g.:

```prolog
:- table a/1.
a([_]).
a([_, _]).

```

Results, showing no unwanted choicepoint:

```prolog
?- a([1]).
true.

?- a([_]).
true.

```
