I can confirm that the problem was that elm-repl got a stack overflow when trying to print the value. Stupid me for not thinking about that... :(
On Saturday, 21 January 2017 03:16:57 UTC+1, Joey Eremondi wrote: > > But Succ shouldn't be called recursively, it should just take its > argument, wrap it in a record, and return immediately. So Succ is getting > called 5000 times, but never in a stack 5000 deep. (This is clearly the > case, because if it wasn't returning, we'd never hit the continue to jump > back to the while loop and loop again). > > Are you perhaps trying to print it? The print function would likely be > recursive, and it could easily stack-overflow. > > On Fri, Jan 20, 2017 at 6:12 PM, GordonBGood <[email protected] > <javascript:>> wrote: > >> The code is TCO'ed but look at the data structure: the code is >> successively calling the `Succ` function to build a successively nested >> record structure, which overflows the stack at about 5000 recursive nests >> in the case of the OP. >> >> >> On Saturday, 21 January 2017 01:36:36 UTC+7, OvermindDL1 wrote: >>> >>> On Friday, January 20, 2017 at 11:17:32 AM UTC-7, mrbackend (Roy >>> Brokvam) wrote: >>>> >>>> Consider: >>>> type Nat >>>> = Zero >>>> | Succ Nat >>>> >>>> >>>> nat : Int -> Nat >>>> nat n = >>>> nat_ n Zero >>>> >>>> >>>> nat_ : Int -> Nat -> Nat >>>> nat_ n curr = >>>> if n <= 0 then >>>> curr >>>> else >>>> nat_ (n - 1) (Succ curr) >>>> >>>> Using tail recursion, this should be stack safe in Elm (as far as I >>>> understand). But I get a stack overflow for large values of n (>= 5000). >>>> Is >>>> my function really tail recursive, or might there be a bug in Elm? >>>> >>> >>> It compiles into this: >>> ```elm >>> var _user$project$Temp1484937158714761$Succ = function (a) { >>> return {ctor: 'Succ', _0: a}; >>> }; >>> var _user$project$Temp1484937158714761$nat_ = F2( >>> function (n, curr) { >>> nat_: >>> while (true) { >>> if (_elm_lang$core$Native_Utils.cmp(n, 0) < 1) { >>> return curr; >>> } else { >>> var _v0 = n - 1, >>> _v1 = _user$project$Temp1484937158714761$Succ(curr); >>> n = _v0; >>> curr = _v1; >>> continue nat_; >>> } >>> } >>> }); >>> var _user$project$Temp1484937158714761$Zero = {ctor: 'Zero'}; >>> var _user$project$Temp1484937158714761$nat = function (n) { >>> return A2(_user$project$Temp1484937158714761$nat_, n, >>> _user$project$Temp1484937158714761$Zero); >>> }; >>> ``` >>> So it does look TCO'd, what is the exact stack trace that you got? >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Elm Discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Elm Discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
