Loop invariants can give you coding superpowers
yourbasic.org
Loop invariants can give you coding superpowers
1–10 of 88 posts
Re: Loop invariants can give you coding superpowers
#2Which doesn't mean the article is _wrong_ so much as it demonstrates one needs to prove the invariants are actually true.
Re: Loop invariants can give you coding superpowers
#3Re: Loop invariants can give you coding superpowers
#4Interesting article, but if one considers the possibility of overflow, the invariants in some of the examples aren't true. Which doesn't mean the article is _wrong_ so much as it demonstrates one needs to prove the invariants are actually true.
Re: Loop invariants can give you coding superpowers
#5Re: Loop invariants can give you coding superpowers
#6Re: Loop invariants can give you coding superpowers
#7Re: Loop invariants can give you coding superpowers
#8 Cornell:
Invariants Playlist: https://www.youtube.com/playlist?list=PLTD_NtzzD4VC6l2uLdzbsm9wW21mMaWwj
http://www.cs.cornell.edu/courses/cs2110/2017sp/online/loops/01aloop1.html
https://www.cs.cornell.edu/courses/cs1110/2018sp/materials/loop_invariants.pdf
CMU:
https://www.cs.cmu.edu/~15122/handouts/01-contracts.pdf
http://www.cs.cmu.edu/%7Efp/courses/15122-s11/recitations/recitation02.html
(former CMU prof) https://www.youtube.com/watch?v=lNITrPhl2_A
Of course if you really wanted to understand the challenge of finding loop invariants, there's the software foundations series of books which even if I don't understand all of it, has been still worth my time to go through https://news.ycombinator.com/item?id=19565365 and Cornell has some good introductory Coq material https://www.cs.cornell.edu/courses/cs3110/2018sp/a5/coq-tact...Re: Loop invariants can give you coding superpowers
#9Sum(0..X)=Sum(0..i)+sum(i+1..X) with the moving i. So it is true at first because the unsummed part is empty and the missing part is the goal. Then we take away an i from the solution to-go pile and put it on the done pile and when the loop has done X the to-do pile is enpty and per invariant the done pile equals the goal.
Including the goal in the invariant sometimes gives you nicer properties. The equivalence relation often also implies an algorithm.
For max I would say the equivalence is: Max(0..X)=Max(Max(0..I),max(I+1..X)) This of course only easier if the max of empty is negative infinity. Then you can move the i from 0 to X.
Re: Loop invariants can give you coding superpowers
#10(I guess I am a bit surprised by the short length of this article because when I write code, I tend to think of it in those terms already, so I expected it to go further; but I guess not everyone has been exposed to that idea or taught to program like that in the first place so I realize that that's my own bias).
What should I check out if I want to be able to formally check invariants for e.g. my Python or Swift code?