Zulip Chat Archive

Stream: lean4

Topic: stack overflow with StateM


Alexandre Rademaker (Jul 02 2025 at 00:11):

I am implementing the Gale-Shapley in Lean. My code will need to handle real data. One of the inputs has 18430 candidates and 43 posts, resulting in 792490 potential interactions. I am getting Stack overflow detected. Aborting. in the command line after building and executing it with lake exec.

I am using StateM monad, can it be the problem? Is there any limitation in the StateM? I am running on MacOS, with

% ulimit -s
65520

Notification Bot (Jul 02 2025 at 00:12):

A message was moved here from #lean4 > stack overflow by Alexandre Rademaker.

Eric Wieser (Jul 02 2025 at 01:01):

Can you share an example?

Alexandre Rademaker (Jul 02 2025 at 01:05):

It will be hard to reduce to a MWE.. but I will share the link …

Alexandre Rademaker (Jul 02 2025 at 01:11):

https://gist.github.com/arademaker/67982be24c455756ecd43d4b21817e5b

Alexandre Rademaker (Jul 02 2025 at 01:19):

The way I call the function is

def main (args : List String) : IO Unit := do
 match args with
 | [] => println! "Usage: lake cpnu NUM"
 | a :: _ =>
  let (cs, ps)  buildInstance "/Users/ar/r/cpnu/2025.2/dados" a.toNat!
  println! s!"candidates x posts: {cs.size} x {ps.size} = {cs.size * ps.size}"
  let m := (galeShapley cs ps).run (initialState cs)
  for k in m.2.assignments.keys do
   IO.println s!"Assignments: {k} => {m.2.assignments[k]?}"

Alexandre Rademaker (Jul 02 2025 at 14:33):

I would be interested in any ideas about how to debug the code. How can I add some extra flags to understand the functional call stack that is causing the stack overflow?

candidate 281972 selected from 1586 free

Stack overflow detected. Aborting.
zsh: abort      .lake/build/bin/cpnu 7

This happened almost at the end of the loop... But the next dataset can be 40% bigger.

Robin Arnez (Jul 02 2025 at 14:58):

Does gdb help?

Kyle Miller (Jul 02 2025 at 15:31):

It might also be worth experimenting with abbrev GS := StateRefT MatchState IO, just to get it to run.

It'd be nice to understand what's causing the stack overflow with StateM though.

Alexandre Rademaker (Jul 02 2025 at 15:50):

@Robin Arnez how to flag the lake build to add -g in the compilation? I suppose this would be my first question for trying to use gdb. Debugging at the C level will probably be more complex, but I am willing to try anything to solve the problem.

Robin Arnez (Jul 02 2025 at 15:57):

You can put moreLeancArgs = ["-g"] into your lakefile (if I read the code correctly)

Alexandre Rademaker (Jul 02 2025 at 16:01):

@Kyle Miller ... I will try, but I need to understand StateRefT first.

Kyle Miller (Jul 02 2025 at 16:19):

I expect you won't need to change very much. It just changes the underlying implementation of how the state is threaded through the computation, though while also making your code be in the IO monad rather than be pure.

Alexandre Rademaker (Jul 02 2025 at 22:23):

Yep. no need to make changes, but I ended up with the same problem... Stack overflow detected. Aborting.


Last updated: Dec 20 2025 at 21:32 UTC