Docsity

Summing Loops and Annotated Programs in Logic-Based Programming - Prof. James T. Sasaki, Study notes of Computer Science

Toggle sidebar
bg1
Science of Programming
Notes #6 — Proof Rules for System PD
Dr. Jim Sasaki
Today:
1. Finish Proof rules for System PD (Partial correctness of determinis-
tic programs)
2. Proof Outlines
3. Minimal Proof Outlines
4. Derived Rules
5. Termination and System TD (Total correctness of deterministic
programs)
Today’s Goals
•Finish list of proof rules, look at examples.
•Actual formal proofs involve a lot of repetition.
Science of Programming Spring’09 Notes #6
Ill. Inst. Tech. CS 1 of 27 Tue, Feb 24, 2009
•We'll avoid this by using proof outlines.
•Look at termination of loops.
1 Proof System PD [Partial correctness of Deterministic pro-
grams]
1.1 Finishing the major list of rules
Axiom 1 (Skip)
•{!p!} skip {!p!} (for every assertion p)
Axiom 2 (Assignment)
•Two versions of assignment rules
•{!p[u!:=!t]!} u :=!t {!p!}
•{!p!} u!:=!t {!!v : u!=!t[u!:=!v] " p[u!:=!v]!} where v is a "fresh" vari-
able (doesn't appear in p, u, or t).
Science of Programming Spring’09 Notes #6
Ill. Inst. Tech. CS 2 of 27 Tue, Feb 24, 2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Summing Loops and Annotated Programs in Logic-Based Programming - Prof. James T. Sasaki and more Study notes Computer Science in PDF only on Docsity!

Science of Programming

Notes #6 — Proof Rules for System PD

Dr. Jim Sasaki

Today:

  1. Finish Proof rules for System PD (Partial correctness of determinis- tic programs)
  2. Proof Outlines
  3. Minimal Proof Outlines
  4. Derived Rules
  5. Termination and System TD (Total correctness of deterministic programs) Today’s Goals
  • Finish list of proof rules, look at examples.
  • Actual formal proofs involve a lot of repetition. Ill. Inst. Tech. CS 1 of 27 Tue, Feb 24, 2009
  • We'll avoid this by using proof outlines.
  • Look at termination of loops.

1 Proof System PD [Partial correctness of Deterministic pro-

grams]

1.1 Finishing the major list of rules Axiom 1 (Skip)

  • {!p!}^ skip^ {!p!} (for every assertion p) Axiom 2 (Assignment)
  • Two versions of assignment rules
  • {!p[u!:=!t]!} u :=!t {!p!}
  • {!p!} u!:=!t {!!v : u!=!t[u!:=!v]^ "^ p[u!:=!v]!} where v is a "fresh" vari- able (doesn't appear in p, u, or t). Science of Programming Spring’09 Notes #

Rule 3 (Composition): {!p!} S" {!r!} {!r!} S# {!q!} {!p!} S" ; S# {!q!}

  • Antecedents above the line, conclusion below.
  • Sample formal proof: Below, let p^ #^0 !"!i!"!n^ "^ s!=!sum(0, i)
    1. {!p[s!:=!s+i]!} s!:=!s+i {!p!} by assignment
    2. {!p[s!:=!s+i][i!:=!i+1]!} i!:=!i+1 {!p[s!:=!s+i]!} by assignment
    3. {!p[s!:=!s+i][i!:=!i+1]!} i!:=!i+1; s!:=!s+i {!p!} by sequence 2, 1
  • Above,
    • p[s!:=!s+i]^ #^ (0!"!i!"!n^ "^ s!=!sum(0, i))[s!:=!s+i]

      0 !"!i!"!n " s+i!=!sum(0, i)

    • p[s!:=!s+i][i!:=!i+1]^ #^ (0!"!i!"!n^ "^ s+i!=!sum(0, i))[i!:=!i+1]

      0 !"!i+1!"!n " s+(i+1)!=!sum(0, i+1)

Ill. Inst. Tech. CS 3 of 27 Tue, Feb 24, 2009 Rule 4 (Conditional): {!p " B!} S" {!q!} {!p " ¬B!} S# {!q!} {!p!} if B then S" else S# fi {!q!}

  • If you know that:
    • Running the true branch S" in a state satisfying p and the if test es- tablishes q
    • And running the false branch S# in a state satisfying p and the ne- gation of the if test establishes q
    • Then you know that running the^ if-else^ in a state satisfying p es- tablishes q.
  • Sample formal proof:
    1. [not shown]
    2. [not shown]
    3. {! true " x!#! 0 !} y!:=!x {!y!#! 0 !} by [not shown] Science of Programming Spring’09 Notes #
  • Often we strengthen a precondition or weaken a postcondition but not both. We can derive two simpler rules to do this: Weaken Postcondition {p} S {q"} q" $ q# {p} S {q#} Strengthen Precondition p" $ p# {p#} S {q} {p"} S {q}
  • Whichever version of the rule you use, at least one of the antecedents (above the line) is a predicate calculus formula (an implication).
  • These implications get listed in the proof; each one introduces an ob- ligation for a proof using everyday predicate logic (which we'll al- Ill. Inst. Tech. CS 7 of 27 Tue, Feb 24, 2009 most always omit).
  • Example: Below, let p # 0 !"!i!"!n " s!=!sum(0, i) and let p$ # p[s!:=!s+i] [i!:=!i+1] # 0 !"!i+1!"!n " s+(i+1)!=!sum(0, i+1).
  1. [Lines 1 and 2 not shown]
  2. {!p$!} i!:=!i+1; s!:=!s+i {!p!} by [not shown]
  3. p " i!n $ p$ by predicate logic
  4. {p " i!<!n} i!:=!i+1; s!:=!s+i {!p!} by precond. strength., 4, 3
  • (In fact, the first three lines of the proof above can be the three lines from the composition rule example above.) 1.2 Formal PD proof example: Summing Loop 1
  • Let W # while i!<!n do i!:=!i+1; s!:=!s+i od and let S # i!:=!0; s!:=!0;!W. We’ve done almost all the pieces necessary to show {n!#!0}!S {s!=!sum(0, n)}; we just need to put them together.
  • Recall that we’re using Hilbert-style proofs: A proof is a sequence of Science of Programming Spring’09 Notes #

assertions, each of which is an axiom or a premiss or follows from above assertions using some rule of inference.

  • Most assertions are correctness triples, but since the consequence rule uses predicate logic implications, they can appear too.
  • So here’s a proof of {n!#!0}!S {s!=!sum(0, n)}. Below, p^ #^0 !"!i!"!n^ " s!=!sum(0, i). There are a number of substitutions into p; they're shown after the proof.
  • (Note:^ As long as it's clear which axiom or rule you're using with what antecedents, it's not critical whether you say "by assignment ax- iom" or "by assignment" or just "assignment", and so on.)
  1. {p[s!:=!0]} s!:=!0 {p} assignment
  2. {p[s!:=!0][i!:=!0]} i!:=!0 {p[s!:=!0]} assignment
  3. {p[s!:=!0][i!:=!0]} i!:=!0; s!:=!0 {p} composition 2, 1.
  4. n!#! 0 $ p[s!:=!0][i!:=!0] predicate logic
  5. {n!#!0} i!:=!0; s!:=!0 {p} consequence 4, 3 Ill. Inst. Tech. CS 9 of 27 Tue, Feb 24, 2009
  6. {p[s!:=!s+i]} s!:=!s+i {p} assignment
  7. {p[s!:=!s+i][i!:=!i+1]} i!:=!i+1 {p[s!:=!s+i]} assignment
  8. {p[s!:=!s+i][i!:=!i+1]} i!:=!i+1; s!:=!s+i {p} composition 7, 6
  9. p " i!n $ p[s!:=!s+i][i!:=!i+1] predicate logic
  10. {p " i!<!n} i!:=!i+1; s!:=!s+i {p} consequence 9, 8
  11. {p} W {p!" i!#!n} while 10
  12. {n!#!0} i!:=!0; s!:=!0; W {p " i!#!n} composition 5, 11.
  13. p " i!#!n $ s!=!sum(0, n) predicate logic
  14. {n!#!0} i!:=!0; s!:=!0; W {s!=!sum(0, n)} consequence 12, 13.
  • We had a number of substitutions above:
    • p[s!:=!0] # (0!"!i!"!n " s!=!sum(0, i))[s!:=!0] # 0 !"!i!"!n " 0 !=!sum(0, i)
    • p[s!:=!0][i!:=!0]!#^0 !"!^0 !"!n^ "^0 !=!sum(0, 0)
    • p[s!:=!s+i] # 0 !"!i!"!n " s+i!=!sum(0, i)
    • p[s!:=!s+i][i!:=!i+1]^ #^0 !"!i+1!"!n^ "^ s+i+1!=!sum(0, i+1) Science of Programming Spring’09 Notes #

rectness triple.

  • If two assertions lie next to each other, that stands for an implication (the first one implies the second) and a use of precondition strength- ening or postcondition weakening as appropriate.
  • To specify a loop invariant, we write { inv p} while B do S" od. 2.2 Proof outline example 1
  • The outline {n!#!0} {p"} i!:=!0; {p#} s!:=!0 {p} stands for the triples / implication:
  • n!#!^0 $^ p"
  • {p"} i!:=!0 {p#}
  • {p#} s!:=!0 {p}
  • {p"} i!:=!0; s!:=!0 {p}
  • {n!#!0} i!:=!0; s!:=!0 {p}
  • The second two triples can only be proved using the assignment ax- iom, so we must have p# # p[s!:=!0] and p" # p#[i!:=!0] # Ill. Inst. Tech. CS 13 of 27 Tue, Feb 24, 2009 p[s!:=!0][i!:=!0], so we get the formal proof
  1. n!#! 0 $ p[s!:=!0][i!:=!0] by predicate logic
  2. {p[s!:=!0][i!:=!0]} i!:=!0 {p[s!:=!0]} by assignment
  3. {p[s!:=!0]} s!:=!0 {p} by assignment
  4. {p[s!:=!0][i!:=!0]} i!:=!0; s!:=!0 {p} by composition 2, 3
  5. {n!#!0} i!:=!0; s!:=!0 {p} by precond. strength. 1, 4
  • Note these lines correspond to (a permutation) of lines 1–5 of the sample proof in Section 1.2 above. 2.3 Another two-assignment example
  • Here’s a (bottom-up) formal proof for the outline {p%"%i!n} {p[s!:=!s+i][i!:=!i+1]} i!:=!i+1; {p[s!:=!s+i]} s!:=!s+i {p}
  1. {p[s!:=!s+i]} s!:=!s+i {p} by assignment
  2. {p[s!:=!s+i][i!:=!i+1]} i!:=!i+1; {p[s!:=!s+i]} by assignment
  3. {p[s!:=!s+i][i!:=!i+1]} i!:=!i+1; s!:=!s+i {p} by composition 2, 1
  4. p%"%i!<!n $ p[s!:=!s+i][i!:=!i+1] by predicate logic Science of Programming Spring’09 Notes #
  1. {p%"%i!n} i!:=!i+1; s!:=!s+i {p} by consequence 4, 3
  • These lines correspond to (a permutation of) lines 6–10 of the sample proof in Section 1.2 above. 2.4 Outline for Summing Loop 1
  • The two examples above can be combined and extended into an out- line for the entire summing loop:
  • {n!#!0} {p[s!:=!0][i!:=!0]} i!:=!0; {p[s!:=!0]} s!:=!0; { inv p} while i!<!n do {p%"%i!<!n} {p[s!:=!s+i][i!:=!i+1]} i!:=!i+1; {p[s!:=!s+i]} s!:=!s+i {p} od {p%"%i!#!n} {s!=!sum(0, n)}
  • We’ve already covered the outlines for the triples
  • {n!#!0} S"^ {p} where S"^ #^ i!:=!0; s!:=!^0
  • {p%"%i!<!n} S# {p} where S# # i!:=!i+1; s!:=!s+i Ill. Inst. Tech. CS 15 of 27 Tue, Feb 24, 2009
  • Adding in the loop test and initialization gives us
  • {n!#!0} S" { inv p} while i!<!n do % {p%"%i!<!n} S# {p} od {p!"!i!#!n} {s!=!sum(0,!n)}
  • The triples and implications for this part of the outline are
  • { inv^ p}^ while^ i!<!n^ do !S#! od^ {p!"!i!#!n}
  • p!"!i!#!n^ $^ s!=!sum(0,!n)
  • { inv^ p}^ while^ i!<!n^ do !S#! od^ {s!=!sum(0,!n)}
  • {n!#!0} S"; { inv p} while i!<!n do !S#! od {s!=!sum(0,!n)}
  • The formal proof for these correspond to lines 11–14 of the example in Section 1.2 above.
  • Example: What are the triples and implication(s) for
  • { true } {p"} a!:=!1; {p#} b!:=!2 {a < b}
  • p#^ #^ a < 2 Science of Programming Spring’09 Notes #
  • {n!#!0} i!:=!0; s!:=!0; { inv p # 0 !"!i!"!n " s!=!sum(0, i)} while i!!0} i!:=!1; s!:= 1; { inv p} while i!! 0 $ p[s!:=!1][i!:=!1]) # (n!>! 0 $ 0 !"! 1 !"!n%"% 1 !=!sum(0,!1)) 3.3 Annotated Summing Loop 3
  • A^ downward-moving sum program (minimal annotation)
  • {n!#!0} i!:=!n; s!:=!0; { inv p # 0 !"!i!"!n%"%s!=!sum(i+1, n)} while i!>! 0 do s!:=!s+i; i!:=!i-1 od {s!=!sum(0, n)}
  • Full annotation:
  • {n!#!0} {p[s!:=!0][i :=!n]} i!:=!n; {p[s :=!0]} s!:=!0; { inv p # 0 !"!i!"!n%"%s!=!sum(i+1, n)} Science of Programming Spring’09 Notes #

while i!>! 0 do {p " i!>!0} {p[i :=!i-1][s :=!s+i]} s!:=!s+i; {p[i :=!i-1]} i!:=!i-1 {p} od {p " i!"!0} {s!=!sum(0, n)}

  • Substitutions:
    • p[s!:=!0] # (0!"!i!"!n%"%s!=!sum(i+1, n))[s :=!0]

      0 !"!i!"!n%"% 0 !=!sum(i+1, n)

    • p[s!:=!0][i :=!n] # 0 !"!n!"!n%"% 0 !=!sum(n+1, n)
    • p[i :=!i-1]^ #^0 !"!i-1!"!n%"%s!=!sum(i-1+1, n)
    • p[i :=!i-1][s :=!s+i] # 0 !"!i-1!"!n%"%s+i!=!sum(i-1+1, n)
  • Predicate logic obligations:
    • n!#!^0 $^ p[s!:=!0][i!:=!n]
      • I.e., n!#!^0 $^0 !"!n!"!n%"%^0 !=!sum(n+1, n)
    • p^ "^ i!>!^0 $^ p[i :=!i-1][s :=!s+i]
      • I.e., (0!"!i!"!n%"%s!=!sum(i+1, n)) " i!>! 0 Ill. Inst. Tech. CS 21 of 27 Tue, Feb 24, 2009 $ 0 !"!i-1!"!n%"%s+i!=!sum(i-1+1, n)
    • p%"%i!"! 0 $ s!=!sum(0, n) 3.4 Annotated Summing Loop 4
  • On your own, see if you can figure out the full annotation for Summing Loop 4 — replace the “?” in the following.
  • {n!#!0} {?"} i!:=!n; {?#} s!:=!n; { inv p # 0 !"!i!"!n%"%s!=!sum(i, n)} while i!>! 0 do {?&} {?'} s!:=!s+i; {?(} i!:=!i-1 {?)} od {?*} {s!=!sum(0, n)}
  • Filling in the missing conditions gives us:
  • {n!#!0} {p[s!:=!n][i!:=!n]} i!:=!n; {p[s!:=!n]} s!:=!n; { inv p # 0 !"!i!"!n%"%s!=!sum(i, n)} while i!>! 0 do Science of Programming Spring’09 Notes #
  • Case 3 (a!=!b): gcd(a, b)!=!a!=!b
  • E.g., gcd(720, 300)!=!gcd(420, 300), gcd(120, 300)!=!gcd(120, 180)!=! gcd(120, 60)!=!gcd(60, 60)!=!60.
  • An iterative gcd-calculating loop: /* a$ and b$ are the values of a and b before the program runs. */ {a!>! 0 !"!b!>! 0 !"!a!=!a$!"!b!=!b$} { inv p # a!>! 0 !"!b!>! 0 !"!gcd(a$,b$)!=!gcd(a,b)} while a!$!b do if a!>!b then a!:=!a!–!b else b!:=!b!–!a fi od {a!= gcd(a$, b$)}
  • A^ full annotation: {a!>! 0 !"!b!>! 0 !"!a!=!a$!"!b!=!b$} { inv p # a!>! 0 !"!b!>! 0 !"!gcd(a$,b$)!=!gcd(a,b)} while a!$!b do {p!"!a!$!b} Ill. Inst. Tech. CS 25 of 27 Tue, Feb 24, 2009 if a!>!b then {p!"!a!$!b!"!a!>!b} {p[a!:=!a!–!b]} a!:=!a!–!b {p} else {p!"!a!$!b!"!a!"!b} {p[b!:=!b!–!a]} b!:=!b!–!a {p} fi {p} od {p!"!a!=!b} {a!=!gcd(a$,b$)}
  • Substitutions
  1. p[a!:=!a!–!b] # (a!>! 0 !"!b!>! 0 !"!gcd(a$,b$)!=!gcd(a,b))[a :=!a!–!b]

a!–!b!>! 0 !"!b!>! 0 !"!gcd(a$, b$)!=!gcd(a!–!b, b)

  1. p[b!:=!b!–!a] # a!>! 0 !"!b!–!a!>! 0 !"!gcd(a$, b$)!=!gcd(a, b!–!a)
  • Predicate logic proof obligations (where p^ #^ a!>!^0 !"!b!>!^0 !"!gcd(a$,b$)!=! gcd(a,b))
  1. (a!>! 0 !"!b!>! 0 !"!a!=!a$!"!b!=!b$)!$ p [gcd(a$, b$)!=!gcd(a,b) is trivial since a!=!a$, b!=!b$] Science of Programming Spring’09 Notes #
  1. p!"!a!$!b!"!a!>!b!$ p[a!:=!a!–!b] [if a!>!b then gcd(a, b)!=!gcd(a!–!b, b) by case 1 above]
  2. p!"!a!$!b!"!a!"!b!$ p[b!:=!b!–!a] [if a!b then gcd(a, b)!=!gcd(a, b!–!a) by case 2 above]
  3. p!"!a!=!b!$ a!=!gcd(a$, b$) [if a!=!b, then gcd(a, b)!=!gcd(a, a)!=!a!=!b by case 3 above]