stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Testing if recursion is TCOed

Open inoas opened this issue 3 years ago • 1 comments
trafficstars

So this is an inductive/brute force test (no proof).

Before I continue, because this is a bit tedious, I wanted to get a heads up if my approach is good and mergable in principle.

Obviously the TODO comments will be removed. I just went through some files and checked where I think recursion occurs, so that I can go through the test files and add a test case.

I started to omit should.-calls because the test case here is a non-crash. I have also added a comment to show why these strange test exists.

inoas avatar Sep 15 '22 20:09 inoas

It'd be interesting if the compiler was able to help with that. As is done in Kotlin with tailrec: https://stackoverflow.com/questions/51638557/what-is-the-point-of-tailrec-in-kotlin

If the function is not tail recursive the compiler flags it as an error.

lucasavila00 avatar Sep 19 '22 18:09 lucasavila00

@lpil is this the right way to proceed then I will go through the whole stdlib and add tests wherever I see (hope I don't miss) (tail) recursion.

inoas avatar Oct 05 '22 05:10 inoas

Hello! Looks like a good approach to me! Though looks like a lot of work so may not be a good use of your time.

Thanks for waiting while I was on holiday for my reply

lpil avatar Oct 06 '22 18:10 lpil

That's fine, I will get some more practise seeing (non)tcoed-recursion. I just don't want it be for nothing. I will work on this once I have some spare time. Thanks!

inoas avatar Oct 06 '22 19:10 inoas

Unless I am mistaken for the list module, these functions need some improvements:

flatten, fold_right, unique, sort, transpose

inoas avatar Oct 08 '22 14:10 inoas

list.sort() fails on JS only (node verison v16.15.0):

❌ gleam/list_test..sort_test: RangeError: Maximum call stack size exceeded

inoas avatar Oct 08 '22 15:10 inoas

For list.unique on Beam the test runner times out on large numbers such as 999_999 different items, on the beam which makes sense because for 999_999 different items it needs to run 999_999 + 999_998 + 999_997 + .. + 1 AFAIU.

inoas avatar Oct 08 '22 16:10 inoas

I have tried to port over https://github.com/lpil/gleeunit/pull/8/files#diff-2e01e4ff5ae575918bd323fb28192b743e863f3a0e22a408a43ca4ee7e0bd392 but if thrown error seems to be {}.

inoas avatar Oct 08 '22 16:10 inoas

JS: list.sort and list.transpose are still not passing.

inoas avatar Oct 08 '22 16:10 inoas

Thanks to @schurhammer list.sort is now passing the TCO tests. list.transpose has got a notice added to its docblock.

This is done from my side, so some review would be good to see if its ready to get merged.

inoas avatar Oct 21 '22 01:10 inoas

Failing ci seems unrelated

inoas avatar Oct 22 '22 14:10 inoas