Purescript: TCO does not trigger in `case` expressions with assign guards

Created on 20 Nov 2020  路  2Comments  路  Source: purescript/purescript

Description

The compiler misses an opportunity for tail-call optimization when the self call happens inside a case clause guarded with assign (<-) expression.

To Reproduce

Compile module

module Test where

fixPokemonHp :: Int -> Int
fixPokemonHp x = case x of
  n | _ <- n -> fixPokemonHp x
  _ -> fixPokemonHp x

Expected behavior

Produce some tail recursive code

Actual behavior

// Generated by purs version 0.13.8
"use strict";
var fixPokemonHp = function (x) {
    var v = function (v1) {
        return fixPokemonHp(x);
    };
    return fixPokemonHp(x);
};
module.exports = {
    fixPokemonHp: fixPokemonHp
};

Additional context

btw I am using Arch Linux

Add any other context about the problem here.

PureScript version

0.13.8

bug

Most helpful comment

Context:
Together with @radrow we are working on an Erlang to Purescript transpiler - we rely on tail recursion heavily.
https://github.com/gorbak25/erlscripten

All 2 comments

Same thing for

fixPokemonHp :: Int -> Int
fixPokemonHp x = case x of
  n | n == n && true -> fixPokemonHp x
  _ -> fixPokemonHp x

(works)

fixPokemonHp :: Int -> Int
fixPokemonHp x = case x of
  n | n == n, true -> fixPokemonHp x
  _ -> fixPokemonHp x

(does not)

Context:
Together with @radrow we are working on an Erlang to Purescript transpiler - we rely on tail recursion heavily.
https://github.com/gorbak25/erlscripten

Was this page helpful?
0 / 5 - 0 ratings