# Test failure that resolves after consulting again

**URL:** <https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584>\
**Category:** Help!\
**Created:** [July 10, 2024, 1:48pm UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584 "2024-07-10T13:48:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![tatut](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/tatut/32/5300_2.png) [@tatut](https://swi-prolog.discourse.group/u/tatut)\
**Post date:** [July 10, 2024, 1:48pm UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/1 "2024-07-10T13:48:43Z")

</div>

I’m using: SWI-Prolog version 9.2.5

I have a test case that initially fails, but if I reconsult the file and run again (without any changes) it passes.

![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/5/5c9b9e38b0a1d74140fad5ce367fe0271b726954.png)

I don’t understand why simply reconsulting and running again changes the outcome. There should be no state accumulated between the runs (no dynamic predicates being asserted).

The code is at [muggin/state.pl at 0815e8bbca59aef24d16cb3fdea69a0ca0f96d4c · tatut/muggin · GitHub](https://github.com/tatut/muggin/blob/0815e8bbca59aef24d16cb3fdea69a0ca0f96d4c/state.pl#L225)

---

<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:** [July 10, 2024, 1:54pm UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/2 "2024-07-10T13:54:52Z")

</div>

Do you happen to write to files in your tests? Any other side-effects?

---

<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:** [July 10, 2024, 2:20pm UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/3 "2024-07-10T14:20:01Z")

</div>

After downloading the file, the test failing is the one that fails for me with

```prolog
ERROR: /home/jan/Downloads/state.pl:225:
ERROR: test state:patch1: received error: source_sink `"test/01-patch.json"' does not exist

```

Didn’t check the code much, but maybe it is changing directory? The only quick suspicious things I see is the `:- use_module(state).` inside the test block. That causes recursive loading. The system handles loops in use\_module/1 fine, but I’m not sure about loading the module you are defining inside the same module 🙂

Tests units create a _sub module_ that has full access to the predicates in the rest of the file (but definitions inside the block hide the ones from outside).

---

<div class="post-metadata">

**Author:** ![tatut](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/tatut/32/5300_2.png) [@tatut](https://swi-prolog.discourse.group/u/tatut)\
**Post date:** [July 11, 2024, 4:55am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/4 "2024-07-11T04:55:23Z")

</div>

No writing, just reading the file `test/01-patch.json`.

But that test that reads a file is the only one that fails.

---

<div class="post-metadata">

**Author:** ![tatut](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/tatut/32/5300_2.png) [@tatut](https://swi-prolog.discourse.group/u/tatut)\
**Post date:** [July 11, 2024, 4:58am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/5 "2024-07-11T04:58:40Z")

</div>

Good to know, I removed the use\_module from the test… no effect.

The test requires the external file that is in the repo. Working directory doesn’t seem to be the problem, I tried with an absolute path to the file, no effect.

---

<div class="post-metadata">

**Author:** ![tatut](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/tatut/32/5300_2.png) [@tatut](https://swi-prolog.discourse.group/u/tatut)\
**Post date:** [July 11, 2024, 5:18am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/6 "2024-07-11T05:18:39Z")

</div>

Found the problematic line in example(File) goal:

`maplist([Codes,Str]>>string_codes(Str, Codes), Examples, Strs),`

If I change the name Codes to Codes1, it works… I don’t know why that lambda expression is evaluated differently on the 2nd consultation,

---

<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:** [July 11, 2024, 6:41am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/7 "2024-07-11T06:41:30Z")

</div>

> [@tatut](#):
>
> I don’t know why that lambda expression is evaluated differently on the 2nd consultation,

library(yall) has different semantics on whether the code is compiled or interpreted wrt. variables shared with the remainder of the clause. If all is right, the execution should print a warning when autoloading `>>/2`.

As a rule of thumb, when using library(yall), make sure you load it explicitly ☹

---

<div class="post-metadata">

**Author:** ![tatut](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/tatut/32/5300_2.png) [@tatut](https://swi-prolog.discourse.group/u/tatut)\
**Post date:** [July 11, 2024, 7:01am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/8 "2024-07-11T07:01:34Z")

</div>

Thank you for the explanation, important thing to know.

I don’t remember seeing any warning. I thought that the `{Free}/List` form would share, the comment about compilation wasn’t clear to me.

---

<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:** [July 11, 2024, 7:18am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/9 "2024-07-11T07:18:45Z")

</div>

> [@tatut](#):
>
> the comment about compilation wasn’t clear to me

Library yall installs a rule for goal\_expansion/2 that translates calls to lambda expressions into normal Prolog code, possibly adding a helper predicate. That is, if the library is loaded. If you call a lambda expression without library(yall) loaded, it is not translated at compile time. Instead. the call to (in this case) `>>/4` triggers _auto loading_ of library(yall) and uses the runtime version of `>>/4`. Ideally, goal expansion may improve performance, but should never change the semantics. Unfortunately that is not true for library(yall).

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 15, 2024, 1:13am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/10 "2024-07-15T01:13:42Z")

</div>

> [@jan](#):
>
> Instead. the call to (in this case) `>>/4` triggers _auto loading_ of library(yall) and uses the runtime version of `>>/4`.

Maybe library(yall) shouldn’t be autoloaded but require an explicit `use_module(library(yall))`?

(I’ve run into weird error messages due to a typos with one the operators that library(yall) uses (`/` or `>>`), which unexpectedly triggered an autoload of library(yall) when I had no intention of using it.)

---

<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:** [July 15, 2024, 7:25am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/11 "2024-07-15T07:25:18Z")

</div>

> [@peter.ludemann](#):
>
> Maybe library(yall) shouldn’t be autoloaded but require an explicit `use_module(library(yall))`?

Since some time, autoloading should give a warning, e.g.

```prolog
?- a/b.
Warning: Auto-loading (/)/2 from library(yall) into module user is deprecated due to term- or goal-expansion
ERROR: Unknown procedure: b/0 (DWIM could not correct goal)

```

I think that should be enough. Oh, I see this (still) depends on the experimental flag

```
:- set_prolog_flag(warn_autoload, true).

```

Possibly that should be enabled by default. Unfortunately it is not always right in the sense that some autoloaded calls are just fine. That notably applies to library(debug), which is indeed better loaded explicitly when called in code, but calling e.g., debug/1 interactively is just fine. I dislike a list of exceptions, but possibly that is the best option …

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 15, 2024, 11:00am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/12 "2024-07-15T11:00:09Z")

</div>

> [@jan](#):
>
> Possibly [warn\_autoload] should be enabled by default. Unfortunately it is not always right in the sense that some autoloaded calls are just fine.

If I understand correctly the code for `warn_autoload/2` (in `boot/Autoload.pl`), it checks for an expansion hook in the auto-loaded file – `library(debug)` is interesting in that its goal expansions turn _off_ some built-in goal expansion.

---

<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:** [July 15, 2024, 11:42am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/13 "2024-07-15T11:42:14Z")

</div>

> [@peter.ludemann](#):
>
> If I understand correctly the code for `warn_autoload/2` (in `boot/Autoload.pl`), it checks for an expansion hook in the auto-loaded file – `library(debug)` is interesting in that its goal expansions turn _off_ some built-in goal expansion.

Yes. The only thing that matters is that if there is an expansion hook, using the goal may behave differently when called without the hook than with the hook. The main issue is that the hook typically applies to only a subset of the exported predicates and we do not know which goals are affected. Also, the general intend is that goal expansion should not affect semantics, only performance. For many, that is true. For library(yall), it is not.

A possible alternative might be some declaration that tells the system whether the expansion affects semantics or only performance. Given that, we could offer more precise feedback. Another issue is that some goal expansion acts globally (default) and other only affect files that explicitly load the library with the expansion.

Expansion is a hard topic. I’ve spend some time on a new design quite a while ago. Some systems have a better designed expansion mechanism, but none really satisfactory IMO and many are seriously incompatible and we cannot afford that.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 20, 2024, 6:59pm UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/14 "2024-07-20T18:59:57Z")

</div>

Could library(debug) refine the message hook so that no message is done if `warn_autoload` is on? That would allow changing the default to `on` (which would have helped me a few times).

(I don’t think I’ve ever seen a macro system (m4, cpp, lisp macros, LaTex, etc.) that didn’t have some significant problem – macros seem to be a “deep problem” in computer science that so far doesn’t have good solutions.

---

<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:** [July 22, 2024, 9:51am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/15 "2024-07-22T09:51:26Z")

</div>

> [@peter.ludemann](#):
>
> Could library(debug) refine the message hook so that no message is done if `warn_autoload` is on?

Probably. Either that or some more general declaration about what the macro expansion does. I’m more tempted to look into the latter.

> [@peter.ludemann](#):
>
> macros seem to be a “deep problem” in computer science that so far doesn’t have good solutions.

Probably true ☹ Unfortunately the alternatives tend to come with their problems and limitations as well ☹

---

<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:** [July 22, 2024, 10:03am UTC](https://swi-prolog.discourse.group/t/test-failure-that-resolves-after-consulting-again/7584/16 "2024-07-22T10:03:57Z")

</div>

DSLs come to mind, esp. if homegrown or proprietary those can be a real blocker in some contexts
