CSC 530 Lecture Notes Week 8 Wrap Up of Denotational Semantics - - PDF document

csc 530 lecture notes week 8 wrap up of denotational
SMART_READER_LITE
LIVE PREVIEW

CSC 530 Lecture Notes Week 8 Wrap Up of Denotational Semantics - - PDF document

CSC530-S02-L8 Slide 1 CSC 530 Lecture Notes Week 8 Wrap Up of Denotational Semantics Introduction to Axiomatic Semantics CSC530-S02-L8 Slide 2 I. Readings: papers 23-33. II. Tennent Wrap Up A. Check out remaining sections of ch 13 (sections


slide-1
SLIDE 1

CSC530-S02-L8 Slide 1

CSC 530 Lecture Notes Week 8 Wrap Up of Denotational Semantics Introduction to Axiomatic Semantics

slide-2
SLIDE 2

CSC530-S02-L8 Slide 2

  • I. Readings: papers 23-33.
  • II. Tennent Wrap Up
  • A. Check out remaining sections of ch 13

(sections

  • B. Is all the formalism worth it?
slide-3
SLIDE 3

CSC530-S02-L8 Slide 3

  • III. Relation of axiomatic to

attr and denotational semantics

  • A. Knuth/Tennent semantics amount to

translator spec.

  • B. Verification-oriented semantics suit-

able for proving programs.

  • C. Soundness of axiomatic def appeals to

denotational def.

slide-4
SLIDE 4

CSC530-S02-L8 Slide 4

  • IV. Basic components of axiomatic def
  • A. Set of proof rules
  • B. A verification strategy
slide-5
SLIDE 5

CSC530-S02-L8 Slide 5

  • V. Floyd-style verification
  • A. Base PL is SFPs
  • B. Semantics defined for SFP constructs.
  • C. Floyd-style verification strategy:
slide-6
SLIDE 6

CSC530-S02-L8 Slide 6

Floyd-style verification, cont’d

  • 1. Assert precondition
  • 2. Assert postcondition
  • 3. Assert invariant condition for each

loop.

  • 4. Verify that precond implies post-

cond via backwards substitution.

slide-7
SLIDE 7

CSC530-S02-L8 Slide 7

  • VI. Hoare-style verification
  • A. Base PL is textual.
  • B. Semantics defined syntax-directed.
  • C. Hoare-style strategy essentially same

as Floyd, denoted with Hoare triple of the form precond {program} postcond

slide-8
SLIDE 8

CSC530-S02-L8 Slide 8

  • VII. Applying proof rules
  • A. Goal to prove precond implies post-

cond through the program.

  • B. May work either direction
  • C. Easier to work backwards, using back-

wards substitution.

  • D. Proof of termination is separate
slide-9
SLIDE 9

CSC530-S02-L8 Slide 9

  • VIII. SFP proof rules
  • A. Flowcharts are helpful
  • B. We’ll examine basic constructs:
  • 1. assignment
  • 2. if-then-else
  • 3. top-of-loop node
  • 4. function call
slide-10
SLIDE 10

CSC530-S02-L8 Slide 10

SFP proof rules, cont’d

  • C. Rule of assignment

var = expr P(..., expr, ...) P(..., var, ...)

slide-11
SLIDE 11

CSC530-S02-L8 Slide 11

SFP proof rules, cont’d

  • D. Rule of if-then-else

. . . P(. . .) . . . Q(. . .) expr if expr then P(. . .) or if not expr then Q(. . .) true false

  • R(. . .)

R(. . .) R(. . .)

slide-12
SLIDE 12

CSC530-S02-L8 Slide 12

SFP proof rules, cont’d

  • E. Rule for loops

expr true

. . .

false

. . . . . .

programmer-supplied loop condition

slide-13
SLIDE 13

CSC530-S02-L8 Slide 13

SFP proof rules, cont’d

  • F. The rule for function calls:

var = f(...); Pre(f) and P(..., Post(f), ...) P(..., Post(var), ...)

slide-14
SLIDE 14

CSC530-S02-L8 Slide 14

  • IX. A stunning result
  • A. Here’s the program:

int Duh() { /* * Add 2 to 2 and return * the result. * * pre: ; * post: return == 4; * */ int x,y; x = 2; y = x + 2; return y; }

slide-15
SLIDE 15

CSC530-S02-L8 Slide 15

Stunning result, cont’d

  • B. Here’s the SFP:
slide-16
SLIDE 16

CSC530-S02-L8 Slide 16

x = 2 4 == 2+2 4 == x+2 y = x + 2 4 == y return = y Post: return == 4 Pre: true VC: if true then 4 == 2+2

slide-17
SLIDE 17

CSC530-S02-L8 Slide 17

  • X. A stunned result
  • A. Let’s try to prove

int ReallyDuh() { /* * Add 2 to 3 and return * the result. * * pre: ; * post: return == 4; */ int x,y; x = 2; y = x + 3; return = y; }

slide-18
SLIDE 18

CSC530-S02-L8 Slide 18

Stunned result, cont’d

  • B. Here’s the proof attempt
slide-19
SLIDE 19

CSC530-S02-L8 Slide 19

x = 2 4 == 2+3 4 == x+3 y = x +3 4 == y return = y Post: return == 4 Pre: true VC: if true then 4 == 2+3

slide-20
SLIDE 20

CSC530-S02-L8 Slide 20

Stunned result, cont’d

  • C. We are left with the VC

true ⊃ 4 == 2 + 3 ==> true ⊃ false which is false.

  • D. In general, proofs will go wrong at the

VC nearest the statement in which the error occurs.

slide-21
SLIDE 21

CSC530-S02-L8 Slide 21

  • XI. Implication proofs
  • A. Recall truth table for logical implica-

tion.

  • B. p ⊃ q is only false if p is true and q is

false.

  • C. In a program verification, we assume p

is true.

  • D. Hence, VC will fail to be proved is if

q is false.

slide-22
SLIDE 22

CSC530-S02-L8 Slide 22

  • XII. Proof of Factorial example.
  • A. The definition:

int Factorial(int N) { /* * Compute factorial of x, * for positive x, using * an iterative technique. * * pre: N >= 0 * * post: return == N! * */

slide-23
SLIDE 23

CSC530-S02-L8 Slide 23

Proof of Factorial, cont’d int x,y; /* Temp vars */ x = N; y = 1; while (x > 0) { y = y * x; x = x - 1; } return y; }

slide-24
SLIDE 24

CSC530-S02-L8 Slide 24

Proof of Factorial, cont’d

  • B. Figure 1 outlines Floyd-style proof
  • C. Figure 2 outlines Hoare-style proof
slide-25
SLIDE 25

CSC530-S02-L8 Slide 25

Proof of Factorial, cont’d

slide-26
SLIDE 26

CSC530-S02-L8 Slide 26

VC1: if N >= 0 then 1 * N! == N! and N >= 0 x > 0

true false

Loop: y * x! == N! and x >= 0

x = N 1 * x! == N! and x >= 0 y = 1 y * x * (x-1)! == N! and (x-1) >= 0 y = y * x y * (x-1)! == N! and (x-1) >= 0 x = x - 1 1 * N! == N! and N >= 0 return = y VC2: if y * x! == N! and x >= 0 then if x > 0 then y * x * (x-1)! == N! and (x-1) >= 0 VC3: if y * x! == N! and x >= 0 then if x<= 0 then y == N!

Post: return == N!

y == N!

Pre: N >= 0 Programmer-Supplied Condition

Verification Condition Derived Asserition

FONT LEGEND:

slide-27
SLIDE 27

CSC530-S02-L8 Slide 27

  • XIII. Logical derivation ‘‘y * x! = N!’’
  • XIV. Further tips on doing the proofs
slide-28
SLIDE 28

CSC530-S02-L8 Slide 28

  • XV. Factorial (VC’s)
  • A. Obligated to prove each VC
  • B. VC1 is trivial.
  • C. Proof of factorial VC2:

if (y*x! == N! and x>=0) then if (x>0) then y*x*(x-1)! == N! and (x-1)>=0 => if (y*x! == N! and x>=0) then if (x>0) y*x! == N! and x>=1 => if (y*x! == N! and x>=0) then if (x>0) y*x! == N! => if (y*x! == N! and x>=0) then y*x! == N! and x>0 => true

  • D. Proof of factorial VC3:

if (y*x! == N and x>=0) then if (x<=0) then y==N! => if (y*x! == N! and x==0) then y==N! => if (y*0! == N!) then y==N! => if (y*1 == N!) then y==N! => true

slide-29
SLIDE 29

CSC530-S02-L8 Slide 29

  • XVI. Possible errors in factorial
  • A. Transpose loop body statements.
  • B. We’ll get erroneous VC3:

y * x! = N! and x≥0 and x>0 ⊃ y * (x-1) * (x-1)! = N! and x-1 ≥ 0 ==> y * x! = N! and x>0 ⊃ y * (x-1) * (x-1)! = N! (oops)

  • C. ‘‘x ≥ 0’’ (instead of strictly > 0)

y * x! = N! and x≥0 and ¬ (x≥0) ⊃ y = N! ==> y * x! = N! and x≥0 and x<0 ⊃ y = N!

slide-30
SLIDE 30

CSC530-S02-L8 Slide 30

  • XVII. Automatic inductive assertions
  • A. A mechanical technique
  • B. Looks like this:
slide-31
SLIDE 31

CSC530-S02-L8 Slide 31

Automatic inductive assertions, cont’d

y = N! ↓ y = N! ↓ y * x = N! ↓ y * (x-1) = N! ↓ y * x * (x-1) = N! ↓ y * (x-1) * (x-1-1) = N! ↓ y * x * (x-1) * (x-2) = N! ↓ . . . ↓ y * x * (x-1) * ... * (x-N) = N!

slide-32
SLIDE 32

CSC530-S02-L8 Slide 32

Automatic inductive assertions, cont’d

  • C. Inspecting the result, notice relation-

ship y * x! = N!.

  • D. Also interesting to look at the erro-

neous case

slide-33
SLIDE 33

CSC530-S02-L8 Slide 33

Automatic inductive assertions, cont’d

y = N! ↓ y * x = N! ↓ y * (x-1) = N! ↓ y * x * (x-1) = N! ↓ y * (x-1) * (x-2) = N! ↓ . . . ↓ y * (x-1) * (x-2) * ... * (x-N) = N!

slide-34
SLIDE 34

CSC530-S02-L8 Slide 34

Automatic inductive assertions, cont’d

  • E. In erroneous case, symbolic eval leads

to wrong loop assertion.

  • F. This will ultimately cause the verifica-

tion to fail.

slide-35
SLIDE 35

CSC530-S02-L8 Slide 35

  • XVIII. Factorial is never called with false

precond.

y = fact(x) P2 y = x P3 x>=0 true false

P1 P1 P1 P5 Pre P4 x = readint() Post return = y VC

slide-36
SLIDE 36

CSC530-S02-L8 Slide 36

Details of the proof

Label Predicate VC: true => forall (x: integer) Rule of verification if (x>=0) then x!==x! else x==x condition generation => true Induction Pre: true Given P5: forall (x: integer) Rule of readint if (x>=0) then x!==x! else x==x P4: if (x>=0) then Rule of if-then-else if (x>=0) then x!==x! else x!==x else if (x>=0) then y==x! else x==x => if (x>=0) then x!==x! else x==x Simplification P3: if (x>=0) then y==x! else x==x Rule of assignment P2: if (x>=0) then x!==x! else x!==x Rule of function call P1: if (x>=0) then y==x! else y==x Rule of assignment Post: if (x>=0) then return==x! else return==x Given

slide-37
SLIDE 37

CSC530-S02-L8 Slide 37

  • XIX. Verification & program style ...
  • XX. Critical questions
  • A. Question: Can it scale up?
  • B. Question: Why hasn’t it caught on

(yet)?

  • C. Question: Will it ever catch on?