问题
In this example, action
is an infinite loop created by mistake. Is there a way to detect such loops in a GHC program?
action bucket manager url = catch
(action bucket manager url)
(\(e :: HttpException) -> Logger.warn $ "Problems with " ++ url)
回答1:
Short answer: no.
It certainly isn't possible to notice every infinite loop one could write down; this is famously known as the halting problem and the formal proof that one cannot write a loop-detecting program is so famous that there's even a Dr. Seuss version of it.
Of course, there's also an entire branch of computer science devoted to taking best-effort approaches to undecidable problems, and in theory we know a lot about ways to detect simple versions of such infinite loops. However, as far as I know nobody has done the engineering work needed to turn that theory into a tool that one can easily run on Haskell source.
回答2:
I presume this is a follow up to What is the format of GHC hp profile?.
In general there is no way to automatically detect every infinite loop. In practice and not specific to GHC, I think it is commonly reasonable to detect them manually by looking at the CPU and memory usage. This particular case should exhaust memory because it is not tail recursive (at least, I think that's the reason). Something like action x y z = action x y z
won't allocate and so would spin indefinitely, maxing the CPU with no increase in memory usage. It would be up to you to have an expectation of execution time and memory usage and investigate any deviations.
I haven't tried this, but if you suspect an infinite loop perhaps you could use the xc
RTS option and interrupt the execution to get a stack trace.
来源:https://stackoverflow.com/questions/47127758/detect-infinite-loops-in-a-ghc-program