26

According to http://www.reddit.com/r/programming/comments/gwqa2/the_real_point_of_laziness/c1rslxk

Some algorithms don't terminate in an eager language, that do in a lazy one, and (a mild shocker for me to find,) vice-versa.

The former is of course well known, but the latter strikes me as, if true, considerably more than a mild shocker.

Does anyone know an algorithm that terminates in an eager language but not in a lazy one?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 2
    I'm assuming that you don't want modification of state? Because an eager call that would modify program state could, say terminate the program, but some kind of chaining of lazy calls could start evaluating an infinite loop. – Gleno Jul 09 '11 at 14:14
  • Right, the context is functional programming, so I'm assuming purity in both cases. – rwallace Jul 09 '11 at 15:04
  • Some lazy algorithms don't terminate because the lazy structure they build in memory is huge compared to a strict language. A good example would be the foldl / foldr (see http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27). I guess this isn't what you meant though! – Jeff Foster Jul 11 '11 at 09:04
  • The issue with foldr has absolutely nothing to do with lazyness, because the same issue arises in an eager language. This is why one typical definition of the reduce idiom includes an initial or neutral value and the reduction is made tail-recursively. – Nowhere man Jul 11 '11 at 15:10

2 Answers2

11

Wikipedia answers this question for lambda calculus: Lambda Calculus Reduction Strategies

The key parts are:

Applicative order is not a normalising strategy. [...] In contrast, normal order is so called because it always finds a normalising reduction if one exists.

This shows an even stronger property of lazy evaluation: if there is an evaluation strategy that makes a particular program terminate, then the program also terminates with lazy evaluation. So in particular strict evaluation (applicative order) does not allow any program to terminate that loops under lazy evaluation.

The references on the wikipedia page provide proofs.

Jules
  • 6,318
  • 2
  • 29
  • 40
  • +1! A bit anticlimactic - I got my hopes up that this wasn't true. ;) – Gleno Jul 11 '11 at 20:29
  • Right, it had been my understanding that this was the case, which is why I would have been more than mildly shocked by a counterexample. Thanks! – rwallace Jul 12 '11 at 02:45
  • But the question was not about an expression having a normal form, but a computation terminating. I may be wrong, but I'm pretty sure there are some nasty differences between the two. – Nowhere man Jul 12 '11 at 21:28
  • 1
    In the context of the lambda calculation, reduction to normal form **is** the model of computation. And in that model, I'm pretty sure nontermination is exactly the failure to find a normal form (because the reduction sequence is infinite). – Ben Jul 14 '11 at 02:48
  • But a lazy programming language is **not** a model of computation, and lacks the mathematical purity of lamdba calculus. Are you saying that all lazy programming languages are a perfect embodiment of the normal order reduction, to the point that all mathematical proofs that apply to normal order apply to all lazy programming languages? That seems a bit like a stretch… – Nowhere man Jun 28 '15 at 23:00
3

I'm going to go out on a limb and state that no algorithm that terminates in a pure functional eager environment, will fail to terminate in a pure functional lazy environment.

The article that was being discussed does not mention this, the comment is followed by a request for an example that is not meet. Therefore until an example is found I'm going to say no.

David Waters
  • 11,979
  • 7
  • 41
  • 76
  • Any reason other than your opinion? Not that I don't think that you are right; I think you are, but your answer adds nothing of value. – Jules Jul 11 '11 at 18:23
  • Nope :) , I have not provided a formal prof of this conjecture. And nothing less then a formal prof will prove the no answer, all the yes camp need to do is provide one example and my straw man argument will fall. My reasoning was all values that are required will be evaluated at some stage for in a lazy system, so while a eager may evaluate too much preventing termination e.g. first(infiniteList) a lazy system will only evaluate a subset of what the eager system would evaluate. There is my straw, show me the counter example. – David Waters Jul 11 '11 at 18:32
  • 4
    "P!=NP, and if you *dare* question me, provide a counter-example!" That's not how math or computer science questions are answered. Your explanation in this comment is a small step in the right direction, but nowhere near convincing. – Jules Jul 11 '11 at 19:27
  • I know "I have not provided a formal prof of this conjecture. And nothing less then a formal prof will prove the no answer," the smilie face at the end of my comment was meant to indicate a tonuge in check response. I believe I am correct but by no means have proven it. I would love for someone to start an answer saying yes and why they believe that. That will provide a framework for discussion on this site and we can see what the unwashed masses believe. – David Waters Jul 11 '11 at 20:36
  • I don't know if an anthropology or psychology stackoverflow exists, but this certainly isn't it ;) I agree that reasons why you believe what you say are helpful, but your answer was just an assertion. The question was "Is X true", and your answer was "I state that X is true". – Jules Jul 11 '11 at 23:04