Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
  }
}
like image 410
Mysquff Avatar asked Mar 04 '19 23:03

Mysquff


1 Answers

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

like image 158
radrow Avatar answered Nov 04 '22 09:11

radrow