home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / programm / 3142 < prev    next >
Encoding:
Text File  |  1992-11-14  |  5.9 KB  |  140 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!munnari.oz.au!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!math.fu-berlin.de!unidui!Germany.EU.net!mcsun!news.funet.fi!network.jyu.fi!sakkinen
  3. From: sakkinen@jyu.fi (Markku Sakkinen)
  4. Subject: Re: Help - What Is Wrong?
  5. Message-ID: <1992Nov13.103153.13472@jyu.fi>
  6. Organization: University of Jyvaskyla, Finland
  7. References: <1992Nov9.154402.16285@hubcap.clemson.edu> <1992Nov10.135848.3730@jyu.fi> <1992Nov11.194809.18616@hubcap.clemson.edu>
  8. Date: Fri, 13 Nov 1992 10:31:53 GMT
  9. Lines: 129
  10.  
  11. In article <1992Nov11.194809.18616@hubcap.clemson.edu> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
  12. >
  13. >In article <1992Nov10.135848.3730@jyu.fi> sakkinen@jyu.fi (Markku Sakkinen) writes:
  14. >>In article <1992Nov9.154402.16285@hubcap.clemson.edu> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
  15. >>>Discussing a fragment of code of the form 
  16. >>>
  17. >>>    for ( i = 0;  i != 5;  i++ )
  18. >>>        ...
  19.  
  20. [A lot of discussion deleted.]
  21.  
  22. >>Infinite loops can be a particularly bad idea in multitasking systems.
  23. >
  24. >But someone would catch such a loop pretty quickly, or you would run
  25. >out of resources (you do set time limits on your batch jobs, don't
  26. >you?).
  27.  
  28. There are no time limits for background processes in vanilla UNIX systems,
  29. for instance.  A task caught in an infinite loop may go undetected for
  30. a long time, slowing down the whole system.  (I speak from experience.)
  31.  
  32. >I quote from the Master:
  33.  
  34. I never knew these things were treated already in the Gospels :-)
  35.  
  36. >    Some object at first to finding the guard in this way, because
  37. >    Tradition would use the guard "i < n" instead of "i != n". However, 
  38. >    "i != n" is better, because a software or hardware error that made
  39. >    i > n would result in nonterminating execution.  It is better to waste
  40. >    computer time than suffer the consequences of having an error go
  41. >    undetected, which would happen if the guard "i < n" were used.
  42.  
  43. (We'll keep in mind that this quote pertains to a more general situation.)
  44. In the specific example from which this discussion sprung up (still
  45. conserved above!), it is simply ludicrous to fear for 'i > 5'
  46. immediately after setting 'i' to 0!  Or, if the hardware or system software
  47. is as bad as that, there is not much idea to discuss the correctness
  48. of one's own code.
  49.  
  50. >I should also point out that this discussion is taken somewhat out of
  51. >context.  As someone pointed out in e-mail to me, the real reason for
  52.  
  53. I don't think that it necessarily matters.
  54. There is a neat little case, on which everybody can try to convince
  55. others about the virtues of his/her favourite principles or theory.
  56.  
  57. > ...
  58. >>In this specific example as is, there is not much difference
  59. >>between the alternatives.  But suppose a little modification:
  60. >>    for ( i = whiz(x);  i != 5;  i++ ) ...
  61. >>will be sensible only if you are absolutely sure that 'whiz(x)'
  62. >>returns a value < 5;  otherwise you should insert an explicit test.  
  63. >
  64. >That's right, and it is the point.  Remember that Gries's discussion
  65. >is in the context of deriving correct programs, so you have presumably
  66. >proved that the precondition "i < 5" is satisified in this case, but
  67. >if you should encounter a bug due to a typo, or someone else's code...
  68.  
  69. I don't understand.  If I don't know about 'whiz' with certainty,
  70. but am interested in getting a correct programme,
  71. why should I be lazy and let the loop run infinitely, instead of
  72. putting an assertion about 'i <= 5' in the first place?
  73.  
  74. >>On the contrary, 'i < 5' works perfectly also in this case,
  75. >>if the intent is not to execute the loop at all if the value is >= 5.
  76. >
  77. >But if the intent is that i > 5 is an error, you want to write code
  78. >that will catch it.
  79. >
  80. >>Also, the code might be modified so that the increment is 2 instead of 1;
  81. >>'i < 5' still works in a way that may be sensible.
  82. >>In general, it looks much more robust than 'i != 5'.
  83. >
  84. >Surely if you are going to change the increment, you should re-derive the
  85. >guard as well!
  86.  
  87. It seems a bit as if you (and the book?) were taking a pure top-down
  88. approach:  if the specification changes, you forget the old code and
  89. derive the whole thing again beginning from the specification.
  90. If one is modifying software more incrementally, considerations
  91. like this matter a lot.  (In fairness, such modifications to the example
  92. could be imagined in which 'i != 5' would remain correct but 'i < 5'
  93. would need modification.)
  94.  
  95. >>>On the other hand, Gries recommends that guards on "if" and "else"
  96. >>> ...
  97. >>Obviously you are quoting from memory (see below), and this does
  98. >>not make any sense.  'If ... then ... else ... endif'
  99. >>always covers all cases!
  100. >
  101. >To be sure, it was from (recent) memory.  But let me elaborate:
  102. >Consider a piece of code that takes action based on the value of
  103. >some nonnegative variable k.  You might write:
  104. > ...
  105. >Or you might write
  106. >
  107. >    if ( k > 0 ) {
  108. >    ...
  109. >    } else if ( k == 0 ) { 
  110. >    ...
  111. >    } else abort();
  112. >
  113. >Gries's principle would advocate the latter, "all other things being equal."
  114.  
  115. All right, this clarifies the point.
  116. But see the posting by Vadim Antonov that has come in the meantime.
  117.  
  118. >>If Gries strongly suggests infinite loops in many places,
  119. >>the book is evidently aiming at provably _partially_ correct programs. :-)
  120. >
  121. >Oh, cheap shot!
  122.  
  123. I don't think so.  After all, 'partially correct' means that the programme
  124. fulfills its specification _if_ it terminates, 'totally correct' meaning
  125. that also the termination is assured.  I could have said that
  126.   loop null end loop
  127. is a partially correct programme with respect to any specification,
  128. so we can go on to attend to some other business.  Now, _that_
  129. would have been a cheap shot.
  130.  
  131. ----------------------------------------------------------------------
  132. Markku Sakkinen (sakkinen@jytko.jyu.fi)
  133.        SAKKINEN@FINJYU.bitnet (alternative network address)
  134. Department of Computer Science and Information Systems
  135. University of Jyvaskyla (a's with umlauts)
  136. PL 35
  137. SF-40351 Jyvaskyla (umlauts again)
  138. Finland
  139. ----------------------------------------------------------------------
  140.