fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

tail call optimization not possible, should probably trigger an error

Open michaellilltokiwa opened this issue 2 months ago • 2 comments

here TCO can not be done since c.tail.concat_list t is eventually wrapped in a lambda.

  public concat_list (t Lazy (list A)) list A =>
    match list.this
      nil    => t()
      c Cons =>
        # tricky, Lazy wrapping a Lazy.
        # The expression following the colon
        # is a Lazy and t is also a Lazy.
        c.head : c.tail.concat_list t

We should probably show an error for cases like this since programmers will expect TCO being applied here.

michaellilltokiwa avatar Oct 27 '25 21:10 michaellilltokiwa

This is not only wrapped in a lambda, but also wrapped into c.head.infix : (...), so this is IMHO not a tail call.

This code is maybe problematic since it re-wraps the whole first list into a new list. Did you run into any case where iterating the resulting list then causes deep recursion or quadratic performance? What is done this the result of concat_list to provoke such behaviour?

fridis avatar Oct 28 '25 10:10 fridis

@fridis I experienced a StackOverflow, this was the reason for creating this issue.

michaellilltokiwa avatar Oct 28 '25 11:10 michaellilltokiwa