Skip to content

ghci is not tail recursive

Maybe ghci is not supposed to be tail recursive. I couldn't find anything specific to ghci or ghc but I found several places that said Haskell is tail recursive To duplicate the bug create a file that contains

fib :: Int -> Int -> Int -> Int
fib n prev curr  = 
    if n == 0 
      then curr 
      else fib (n -1) curr (prev + curr) 

then use :load to load it:

GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help[[BR]] Loading package base ... linking ... done.[[BR]] Prelude> :load work[[BR]] [1 of 1] Compiling Main ( work.hs, interpreted )[[BR]] Ok, modules loaded: Main.[[BR]]

  • Main> fib 10 0 1[[BR]]

89[[BR]]

  • Main> fib 1234567 0 1[[BR]]
  • ** Exception: stack overflow[[BR]]
  • Main> [[BR]]

same result if I do :set -fobject-code before loading except that it happens quicker

The function is tail recursive. It executes in Scheme with the same arguments (although extremely slowly because of the bignum arithmetic

If ghci and/or ghc are not tail recursive it would be good to document it. Hopefully I have not missed something in the doc that explains this behavior

Trac metadata
Trac field Value
Version 6.8.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component GHCi
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information