Too many stacked Strings

I call it like this

‘path_generic(‘B’,‘21’, P)’

ARCS are defined as below in python and the path generic is called like


prolog.query('path_generic(\'B\',\'21\', P)')

Please use code blocks for code. You can put three ticks on a line before and after the lines of code. There should be a stickied post about how to use markdown on Discourse

Boris is correct. However until just a moment ago Python was not one of the options. I just added Python to the list. See: Language highlighting

In parallel to figuring out the markdown on Discourse, try to narrow down your problem. This is a necessary first step to reporting it. As @j4n_bur53 suggested, you can try to use bisection of your code, among other things. Once you create a minimal reproducible example you can share, it becomes possible for others to reproduce your problem and troubleshoot it. You can increase the limit but it will only push the problem to a bigger input, not solve it, as @j4n_bur53 also explained.

You can also use sub_string/5, with either atom or string argument. This will also avoid adding to the atom table, if that is the cause of your problem:

?- sub_string(abc, 0, _, _, A).
A = "" ;
A = "a" ;
A = "ab" ;
A = "abc".

Which version of swipl are you using? e.g.:

?- prolog_flag(version, Z).
Z = 'SWI-Prolog 8.3.27 (x86_64-linux): Jul 13 2021, 13:43:28'.

(I ran this on Windows, using the Windows Subsystem for Linux (WSL).)

Anyway, from a quick look at your code, you appear to be doing a backtracking search for a path through a graph (with the graph defined by arc/2 facts). This can quickly produce a huge amount of backtracking. How many facts are in the arc/2 predicate? How large are the strings (or atoms) that are given to sub_atom/5?

One more thing you can do, if you don’t want to show your data, is to give the output of a call to jiti_list(user:_) (assuming your code is in the default user module).

1 Like

I am getting a feeling that nobody focuses on the lines pointed out in pl-buffer.c and pl-buffer.h.
Please can you focus on the solution. As you see, I am doing some path searches not String processing. I have ARCS and PATHS. I use ’ not " but when I pasted, somehow it looked different (as if I copied from a word doc.). The error message is below. I get this on larger data (meaning more number of arcs, in my case 38 arcs) not for smaller datasets (in my case 10 arcs). In the larger dataset I have some arcs starting with an alphabet continuing with numbers like ‘23_1’. The subatoms are needed to process that kind of arcs.
I hope this information becomes useful.
Many Thanks,

SWI-Prolog: [FATAL ERROR: at Sat Jul 31 11:35:15 2021
Too many stacked strings]I am getting

It is not easy to find as Markdown by itself because it is a link from this topic.

Discourse New User Tips and Tricks

The relevant links:

Markdown reference
Markdown Tutorial


In the topic Minimal and reproducible working examples is also a link to better Markdown documentation.

1 Like

This limit is there for a reason, we all assume. Maybe none of us truly understands the reason. With a reproducible example, it would be possible to establish that something that should obviously work is broken, and more importantly, know what it is. This way it is possible to fix it without violating the original reasoning behind this admittedly arbitrary limit.

You’re making a large assumption that these lines are the cause of the problem and not merely a symptom. Because you cannot give a “minimal reproducible example”, we are having to guess as to the cause.

@j4n_bur53 has suggested a way of finding the problem. You might want to modify that advice slightly, e.g.:

path_generic(A,B,Path) :- 
    writeln(user_error, 1-path_generic(A,B,Path)),
    writeln(user_error, 2-path_generic(A,B,Path)-[x=X,y=Y]),
    writeln(user_error, 3-path_generic(A,B,Path)-[x=X,y=Y,a=A]),
    writeln(user_error, 4-path_generic(A,B,Path)-[x=X,y=Y,a=A,b=B).

This might produce a lot of output, so you might wish to redirect the output to a file. I don’t know how to do this on Windows; on Linux, I’d do something like swipl ... 2>/tmp/msgs.

(Instead of using writeln/2, you might do:
format(user_error, '~q~n', [1-path_generic(A,B,Path)])

1 Like

This reply is meant for the OP.

Two options using SWI-Prolog:

  1. Protocol/1 (Used to capture screen to file)
  2. Use tell/1 and told/0. e.g. tell('my_file.txt'), goal(Arguments), told.

1- @j4n_bur53 It writes the following output when I add the writelns,

reached to here 3
reached to here 2
reached to here 3
reached to here 2
reached to here 3
reached to here 2
reached to here 3
reached to here 2
reached to here 3
reached to here 2
reached to here 3
SWI-Prolog: [FATAL ERROR: at Sat Jul 31 12:50:07 2021
	Too many stacked strings]

Process finished with exit code 3

2-It stops at the call path_generic(‘B’,‘21’, P). But not at its first call. I mean I call this several times before. But at its xth time this occurs not at first.

3-When I run

?- path_generic('B', '21', P).`I get
ERROR: Unknown procedure: arc/2
ERROR:   However, there are definitions for:
ERROR:         arg/3
ERROR:   [13] arc(_8442,_8444)
ERROR:   [12] travel(_8468,_8470,[_8478],[_8484,_8490]) at d:/path/python/
ERROR:   [11] path(_8516,_8518,_8520) at d:/path/python/
ERROR:   [10] path_generic('B','21',_8548) at d:/path/python/
ERROR:    [9] <user>
   Exception: (13) arc(_1528, _1534) ?

4-@peter.ludemann I am not sure if I can do this with substring. I will think about it, thanks for the idea.

5-Prolog Version

?-  prolog_flag(version, Z).
Z = 'SWI-Prolog 8.2.4 (x64-win64): Jan 27 2021, 07:50:16'.

6-Is there a comamnd which I can call to clear the stack, prior to each path_generic call?

Many thanks for your interest and efforts,

In searching (search) the GitHub SWI-Prolog repository found

The last manual response is

@Marco299 the fix was introduced in PySwip, not in SWI-Prolog. If you’re using PySwip, then install it from master and see if you’re still experiencing the issue.



Since I added a link in this post to the issue, Discourse automatically added a reply in the issue with a link to this post.


I do not use pyswip but it has a closed issue

but more importantly has a Minimal Reproducible Example. (Which I did not try.)


This fix looks to be after your current SWI-Prolog version SWI-Prolog 8.2.4 (x64-win64): Jan 27 2021, 07:50:1 so you need to also update SWI-Prolog if you have not done so.

1 Like

I added the ARC facts manually to the pl file but I made an error. This time I fixed it.
When I call I get no error (which is normal cause it does not happen the in first call).

1 ?- path_generic('B', '21', P).
P = ['B_1', '21_1'] .

I already saw that link which says calling function from pl not from python. It does not suit my code structure, unfortunately :frowning:

Is there a way to fix this bug or whatever with its current structure please?

We don’t know where the bug is, as discussed above

I can not decrease the number of path_generic calls, actually, it will increase once the metadata becomes complete. I couldn’t understand the other stuff related to PL_STRINGS_MARK(), RELEASE(). Is there a way to call that lines from python?

Many thanks,

The PL_STRINGS_MARK()/RELEASE() macros are part of the SWI-Prolog foreign interface. Like the somewhat related PL_open_foreign_frame() and PL_close_foreign_frame() pair, they are required to avoid building up garbage on the Prolog side. It is up to the Python interface to deal with this.

1 Like

I downloaded the SWI-Prolog code and bypassed that control which checks the MAX_LG_STACKED_STRINGS and rebuild it. Now it works for me. This may not be the best idea but I needed a quick solution for now. I may provide the compiled package if anyone wants.

Thanks for all the contributions,

1 Like

I will check that header file tomorrow. You have a reason, I understand. I just neeced something to fix.
Kind Regards,