wolfgangj / bone-lisp

The Bone Lisp programming language
Other
321 stars 21 forks source link

iota segfaults when generating a long list #6

Closed dubek closed 8 years ago

dubek commented 8 years ago

iota is implemented with unfold, which is not tail-recursive, and therefore segfaults if trying to generate long lists (300 elements on my machine).

$ ./bone
Bone Lisp 0.4.0-pre
@0: (use std/math)
(std/math)
@1: (iota* 1 300 1)
Segmentation fault

Or directly with unfold:

$ ./bone
Bone Lisp 0.4.0-pre
@0: (unfold (partial =? 30) id ++ 0)
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29)
@1: (unfold (partial =? 300) id ++ 0)
Segmentation fault

One can make unfold tail-recursive with cat instead of cons, but that will make unfold O(n^2), which we'd like to avoid. Not sure if there's a way to make unfold tail-recursive, or to use unfoldr for iota (for example use unfoldr and then reverse).

wolfgangj commented 8 years ago

I think the real problem is that we currently fail to resize things like the call stack dynamically. If this is solved, it should not be much of a problem to have non-tail-recursive stuff.

wolfgangj commented 8 years ago

The stacks are now resized on demand, so this problem is solved now. I can run (iota* 1 10000 1) without problems. It still crashes when increasing the number to 100000, but this seems to be due to a limit of the C stack (at a point when it is about 50000 functions deep).