Open magniff opened 3 years ago
Thanks for reporting this! To set expectations:
Finally, please be patient with the core team. They are trying their best with limited resources.
This is the compiled output:
var $author$project$T$g = F2(
function (value, cont) {
g:
while (true) {
if (value === 1) {
return cont(1);
} else {
var $temp$value = value - 1,
$temp$cont = function (result) {
return cont(result * value);
};
value = $temp$value;
cont = $temp$cont;
continue g;
}
}
});
It looks like tail-call optimization accidentally broke capturing of arguments, changing the captured values. This problem is evident in perhaps another simpler example:
f x l = if x == 0 then l else f (x - 1) ((\_ -> x) :: l)
> List.map (\g -> g ()) (f 10 [])
[0,0,0,0,0,0,0,0,0,0]
: List number
The output should be [10, 9, ... , 1]
For people being bitten by this in the future, using https://github.com/micahhahn/elm-safe-recursion is both going to fix this and allow mutual recursion in TCO
Quick Summary: I've been playing with continuation passing styled functions and stumbled upon the code, that seems to confuse the compiler.
Here is the code:
Expected: function
g
computesvalue
's factorial Actual (repl):