ats

Why can't generic function templates use fnx to ensure they are tail-recursive?


Just the title, really.

Compiling

fnx {a:t@ype} repeat {n:nat} .<n>.
( x: a
, t: int n
, f: a -> a
) : a =
  if t = 0 then x
  else repeat (f x, t - 1, f)

gives

warning(2): [fnx] is treated as [fun] for initiating function templates!

Am i supposed to take it on faith that I didnt screw up the definition and accidentally made a non-tail-recursive function? Is there some standard way to circumvent this?


Solution

  • Note that 'fnx' is only meaningful for defining mutually recursive functions.

    In your example, using 'fun' is just fine: the compiler can recognize the tail-recursive call in the body of 'repeat' and turn it into a local jump.