Search code examples
haskelltail-recursionstatic-assert

Is there a way to assert that a function is recognized as tail-recursive by the compiler?


Let's say I've written a function in Haskell and I want to assert that it is tail-recursive and it will be optimized by the compiler. Is there a way to do it?

I know there's a way to do it in Scala with @tailrec annotation.

Example:

import scala.annotation.tailrec

class Factorial2 {
  def factorial(n: Int): Int = {
    @tailrec def factorialAcc(acc: Int, n: Int): Int = {
      if (n <= 1) acc
      else factorialAcc(n * acc, n - 1)
    }
    factorialAcc(1, n)
  }
}

Solution

  • As for GHC 8.8.2 tail recursion cannot be forcefully asserted by any pragma or keyword.