stdlib
stdlib copied to clipboard
Testing if recursion is TCOed
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.
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.
@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.
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
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!
Unless I am mistaken for the list module, these functions need some improvements:
flatten, fold_right, unique, sort, transpose
For list.flatten() we could delegate these to:
list.sort() fails on JS only (node verison v16.15.0):
❌ gleam/list_test..sort_test: RangeError: Maximum call stack size exceeded
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.
I have tried to port over https://github.com/lpil/gleeunit/pull/8/files#diff-2e01e4ff5ae575918bd323fb28192b743e863f3a0e22a408a43ca4ee7e0bd392 but if thrown error seems to be {}.
JS: list.sort and list.transpose are still not passing.
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.
Failing ci seems unrelated