Haskell程序输出`<< loop>>` [英] Haskell program outputs `<<loop>>`
问题描述
我写了一个Haskell程序,在列表上进行二进制搜索。至少这是我以为它是。当我用 ghc v7.6.3
编译程序并运行程序时,我得到以下输出:
I wrote a Haskell program that preforms a binary search on a list. At least that's what I thought it does. When I compiled the program with ghc v7.6.3
and ran the program I got the following output:
progname: <<loop>>
这个输出是什么意思?是否意味着我有一个无限循环 ghc
优化掉了?
What on earth does this output mean? Does it mean I had an infinite loop that ghc
optimized away? How am I supposed to debug this?
推荐答案
正如几个意见所说,这是Haskell RTS检测到一个无限循环在运行时。它无法始终检测到此类循环,但在简单的情况下它可以。
As several of the comments have said, this is the Haskell RTS detecting an infinite loop at run-time. It cannot always detect such loops, but in simple cases it can.
例如,
x = x + 1
将编译正常,但在运行时引发异常。 (顺便说一下,这是一个例外 - 特别是你可以捕捉它,如果你想要的,但你可能不是想要。)
will compile just fine, but provoke an exception at run-time. (Incidentally, this is an exception - in particular, you can catch it if you want. But you probably don't "want".)
那么为什么GHC甚至让这个编译?好吧,因为如果我用 +
替换,例如:
,那么表达式现在终止了。 (它代表一个1元素的循环列表。)编译器不能告诉编译时什么是和不是明智的递归。 RTS不能总是在运行时告诉;但是当它可以说出错误时,它会通过向您抛出异常来通知您。
So why does GHC even let this compile? Well, because if I replace +
with, say, :
, then the expression now terminates just fine. (It represents a 1-element circular list.) The compiler can't tell at compile-time what is and is not sensible recursion. The RTS can't always tell at run-time; but when it can tell something's wrong, it'll let you know by throwing an exception at you.
这篇关于Haskell程序输出`<< loop>>`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!