[m-users.] Avoiding a segmentation violation caused by a DCG

Julien Fischer jfischer at opturion.com
Wed Oct 19 14:05:47 AEDT 2022



On Wed, 19 Oct 2022, Razetime wrote:

> I am trying to make a DCG that solves part of of advent of code 2016
> day 9: https://adventofcode.com/2016/day/9
>
> The code I have so far is here:
> https://github.com/razetime/aoc/blob/main/16/a09.m
>
> pattern/3 takes a character list and checks if it matches
>
> (<numberA>x<numberB>)<A*B characters>
>
> substNxN consumes a single alphabet character, adding 1 to the length
> until a different replaceable pattern is reached.
>
> My problem here is that the test code
>
> https://github.com/razetime/aoc/blob/main/16/inp/09
>
> works fine, returning the final length of all the given input, but the
> larger test case
>
> https://github.com/razetime/aoc/blob/main/16/inp/09
>
> fails. After debugging I think this is due to a stack overflow.
>
> My assumption was that I could make this DCG tail recursive, or use
> semicontext notation to pass the final length through the states. How
> can I resolve this problem?

If you are using a low-level C grade (e.g. asm_fast.gc), then there are
a couple of options:

1. The size of the Mercury stacks can be set using the MERCURY_OPTIONS
environment variable.  (See the "Enviroment Varaibles" section of the
user's guide for details.)

On my machine I did the following:

      $ export MERCURY_OPTIONS="--nondetstack-size=4096"

and a09 ran sucessfully.  (I don't understand what the output
means, so I am assuming it was successful.)

2. If your Mercury installation has a stack segments grade (component:
stseg) installed, then you can use that. In stack segments grades, the
size of the stacks is not fixed and they will grow dynamically

If you are using a high-level C grade (e.g. hlc.gc) then you can adjust
the maximum allowed stack size using, e.g. ulimit.

Julien.


More information about the users mailing list