# Phrase\_from\_file fails after 17 iterations

**URL:** <https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624>\
**Category:** General\
**Created:** [June 8, 2023, 1:52pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624 "2023-06-08T13:52:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiec](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/eddiec/32/3846_2.png) [@eddiec](https://swi-prolog.discourse.group/u/eddiec)\
**Post date:** [June 8, 2023, 1:52pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/1 "2023-06-08T13:52:08Z")

</div>

Hi, having an odd problem calling _phrase\_from\_file_ in a loop.

consulted file:

```prolog
get_neighbors(FileName, NbrInfos):-
    phrase_from_file(device_neighbors(NbrInfos), FileName), !.

```

called as:

```prolog
process_nbrs_file(Switch):-
    get_neighbors(Switch, NbrInfos),
    do_stuff_with(Switch,NbrInfos).

main:-
    expand_file_name('*',Switches), 
    forall(member(Switch,Switches),
	   once(process_nbrs_file(Switch))).

```

Every file parses correctly individually but when called like this it stops on the 18th file.  
I was guessing maybe a limit of 20 file descriptors but I can’t find any reference to that.  
And _phrase\_from\_file_ calls _close_ (inside cleanup).  
What I see when debugging is that _device\_neighbors_ when called on the 18th time gets an empty list as the DCG tail.

I can do them individually but there are hundreds so it slows the process down.

Happens on 9.0.4 and 8.4.2  
Any help appreciated.

---

<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:** [June 8, 2023, 2:35pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/2 "2023-06-08T14:35:04Z")

</div>

“Stops”? With what text output and/or exit code?

Try running on just that 18th file.

---

<div class="post-metadata">

**Author:** ![eddiec](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/eddiec/32/3846_2.png) [@eddiec](https://swi-prolog.discourse.group/u/eddiec)\
**Post date:** [June 8, 2023, 3:03pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/3 "2023-06-08T15:03:02Z")

</div>

Stops → fails.  
I’ve run it on all files and it succeeds if done individually.  
Since the DCG list is empty the 18th time it will fail to parse and hence fail the _forall_.

---

<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:** [June 8, 2023, 3:11pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/4 "2023-06-08T15:11:59Z")

</div>

Zero-in on the code line which fails, and determine _why_. Standard debugging.

---

<div class="post-metadata">

**Author:** ![eddiec](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/eddiec/32/3846_2.png) [@eddiec](https://swi-prolog.discourse.group/u/eddiec)\
**Post date:** [June 8, 2023, 3:14pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/5 "2023-06-08T15:14:17Z")

</div>

There aren’t any lines that fail.  
The parse works when done individually.  
The debugging tells me that the contents of the file is missing.

---

<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:** [June 8, 2023, 4:06pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/6 "2023-06-08T16:06:58Z")

</div>

If it fails, as you said, then it fails at a particular point.

So, identify that point. This is standard debugging.

---

<div class="post-metadata">

**Author:** ![eddiec](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/eddiec/32/3846_2.png) [@eddiec](https://swi-prolog.discourse.group/u/eddiec)\
**Post date:** [June 8, 2023, 4:39pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/7 "2023-06-08T16:39:22Z")

</div>

I’ve identified the failure 3 times now!  
When _phrase\_from\_file_ calls _device\_neighbors_ the DCG list that should be the contents of the file is empty. It’s not failing in my code. Please read what I’ve written.

I was hoping there would be some deep prology reason why it couldn’t open another file or something.  
On reflection, I’ll just submit a bug report.

---

<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:** [June 8, 2023, 6:51pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/8 "2023-06-08T18:51:14Z")

</div>

In your bug report, please include a [Minimal reproducible example - Wikipedia](https://en.wikipedia.org/wiki/Minimal_reproducible_example)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 8, 2023, 6:52pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/9 "2023-06-08T18:52:06Z")

</div>

> [@eddiec](#):
>
> I’ll just submit a bug report

Sorry to say this but I really don’t see enough evidence here to support that you have found a bug.

* * *

> [@eddiec](#):
>
> `expand_file_name('*',Switches)`

Some ideas.

Have you tried using gtrace/0 to debug the code?

e.g.

```prolog
main:-
    gtrace,
    expand_file_name('*',Switches), 
    forall(member(Switch,Switches),
	   once(process_nbrs_file(Switch))).

```

1. Did you closely read the documentation for expand\_file\_name/2

> Unify List with a sorted list of files or directories matching WildCard.

I suspect that the list contains directories such as `.` and `..`, make sense?

Can you show us what is in the list `Switches` before forall/2.

```prolog
main:-
    expand_file_name('*',Switches), 
    print_term(Switches,[]),
    forall(member(Switch,Switches),
	   once(process_nbrs_file(Switch))).

```

I have not tried this but you should get the idea. use [file\_exists/1](https://www.swi-prolog.org/pldoc/doc_for?object=file_systems%3Afile_exists/1)

```prolog
:- use_module(library(file_systems)).

main:-
    expand_file_name('*',Switches), 
    print_term(Switches,[]),
    forall(
        member(Switch,Switches),
       (
            file_exists(Switch),
	        once(process_nbrs_file(Switch))
        ;
            true
       )
    ).

```

---

<div class="post-metadata">

**Author:** ![eddiec](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/eddiec/32/3846_2.png) [@eddiec](https://swi-prolog.discourse.group/u/eddiec)\
**Post date:** [June 9, 2023, 8:55am UTC](https://swi-prolog.discourse.group/t/phrase-from-file-fails-after-17-iterations/6624/10 "2023-06-09T08:55:24Z")

</div>

No, there’s no bug, not in swipl (not in my code either).  
A couple of the input files had been truncated and it never occured to me to check for that before running it.

I was using gtrace, but I’ve never been able to navigate the stack without getting lost.

Anway, it was a stupid mistake not to look outside the code. Somehow those are usually the ones that end up embarassing me.
