Programming Languages and Compilers (CS 421)

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
Axiomatic Semantics The meaning of a program is defined by a formal system that allows one to deduce true properties of that program. No specific meaning.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
1 Discrete Structures Lecture 29 Predicates and Programming Read Ch
Predicate Transformers
Program Proving Notes Ellen L. Walker.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
CS 355 – Programming Languages
Axiomatic Semantics Dr. M Al-Mulhem ICS
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Describing Syntax and Semantics
Proving Program Correctness The Axiomatic Approach.
Proving Program Correctness The Axiomatic Approach.
Reading and Writing Mathematical Proofs
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Proofs of Correctness: An Introduction to Axiomatic Verification Prepared by Stephen M. Thebaut, Ph.D. University of Florida CEN 5035 Software Engineering.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
1 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
CS 363 Comparative Programming Languages Semantics.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Chapter 3 Part II Describing Syntax and Semantics.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
2/6/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Axiomatic Verification II Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture Notes 18.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Formal Methods in Software Engineering 1
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages 2nd edition Tucker and Noonan
Axiomatic Verification II
Programming Languages and Compilers (CS 421)
Logic for Computer Security Protocols
Programming Languages and Compilers (CS 421)
Semantics In Text: Chapter 3.
Axiomatic Verification II
Axiomatic Verification I
Predicate Transformers
Formal Methods in software development
Proofs of Correctness: An Introduction to Axiomatic Verification
Axiomatic Verification I
PZ03D - Program verification
PZ03D - Program verification
Programming Languages and Compilers (CS 421)
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
Lecture 2: Axiomatic semantics
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 11/29/2018

Axiomatic Semantics Also called Floyd-Hoare Logic Based on formal logic (first order predicate calculus) Axiomatic Semantics is a logical system built from axioms and inference rules Mainly suited to simple imperative programming languages 11/29/2018

Axiomatic Semantics Used to formally prove a property (post-condition) of the state (the values of the program variables) after the execution of program, assuming another property (pre-condition) of the state holds before execution 11/29/2018

Goal: Derive statements of form {P} C {Q} Axiomatic Semantics Goal: Derive statements of form {P} C {Q} P , Q logical statements about state, P precondition, Q postcondition, C program Example: {x = 1} x := x + 1 {x = 2} 11/29/2018

Axiomatic Semantics Approach: For each type of language statement, give an axiom or inference rule stating how to derive assertions of form {P} C {Q} where C is a statement of that type Compose axioms and inference rules to build proofs for complex programs 11/29/2018

Axiomatic Semantics An expression {P} C {Q} is a partial correctness statement For total correctness must also prove that C terminates (i.e. doesn’t run forever) Written: [P] C [Q] Will only consider partial correctness here 11/29/2018

Language We will give rules for simple imperative language <command> ::= <variable> := <term> | <command>; … ;<command> | if <statement> then <command> else <command> | while <statement> do <command> Could add more features, like for-loops 11/29/2018

Substitution Notation: P[e/v] (sometimes P[v <- e]) Meaning: Replace every v in P by e Example: (x + 2) [y-1/x] = ((y – 1) + 2) 11/29/2018

The Assignment Rule {P [e/x] } x := e {P} Example: { ? } x := y {x = 2} 11/29/2018

The Assignment Rule {P [e/x] } x := e {P} Example: { _ = 2 } x := y { x = 2} 11/29/2018

The Assignment Rule {P [e/x] } x := e {P} Example: { y = 2 } x := y { x = 2} 11/29/2018

The Assignment Rule {y = 2} x := 2 {y = x} {P [e/x] } x := e {P} Examples: {y = 2} x := y {x = 2} {y = 2} x := 2 {y = x} {x + 1 = n + 1} x := x + 1 {x = n + 1} {2 = 2} x := 2 {x = 2} 11/29/2018

The Assignment Rule – Your Turn What is the weakest precondition of x := x + y {x + y = w – x}? {(x + y) + y = w – (x + y)} x := x + y {x + y = w – x} ? 11/29/2018

The Assignment Rule – Your Turn What is the weakest precondition of x := x + y {x + y = w – x}? {(x + y) + y = w – (x + y)} x := x + y {x + y = w – x} 11/29/2018

Precondition Strengthening P  P’ {P’} C {Q} {P} C {Q} Meaning: If we can show that P implies P’ (P P’) and we can show that {P’} C {Q}, then we know that {P} C {Q} P is stronger than P’ means P  P’ 11/29/2018

Precondition Strengthening Examples: x = 3  x < 7 {x < 7} x := x + 3 {x < 10} {x = 3} x := x + 3 {x < 10} True  2 = 2 {2 = 2} x:= 2 {x = 2} {True} x:= 2 {x = 2} x=n  x+1=n+1 {x+1=n+1} x:=x+1 {x=n+1} {x=n} x:=x+1 {x=n+1} 11/29/2018

Which Inferences Are Correct? {x > 0 & x < 5} x := x * x {x < 25} {x = 3} x := x * x {x < 25} {x * x < 25 } x := x * x {x < 25} 11/29/2018

Which Inferences Are Correct? {x > 0 & x < 5} x := x * x {x < 25} {x = 3} x := x * x {x < 25} {x * x < 25 } x := x * x {x < 25} 11/29/2018

{z = z & z = z} x := z {x = z & z = z} Sequencing {P} C1 {Q} {Q} C2 {R} {P} C1; C2 {R} Example: {z = z & z = z} x := z {x = z & z = z} {x = z & z = z} y := z {x = z & y = z} {z = z & z = z} x := z; y := z {x = z & y = z} 11/29/2018

{z = z & z = z} x := z {x = z & z = z} Sequencing {P} C1 {Q} {Q} C2 {R} {P} C1; C2 {R} Example: {z = z & z = z} x := z {x = z & z = z} {x = z & z = z} y := z {x = z & y = z} {z = z & z = z} x := z; y := z {x = z & y = z} 11/29/2018

Postcondition Weakening {P} C {Q’} Q’  Q {P} C {Q} Example: {z = z & z = z} x := z; y := z {x = z & y = z} (x = z & y = z)  (x = y) {z = z & z = z} x := z; y := z {x = y} 11/29/2018

P  P’ {P’} C {Q’} Q’  Q {P} C {Q} Rule of Consequence Logically equivalent to the combination of Precondition Strengthening and Postcondition Weakening Uses P  P and Q  Q 11/29/2018

if x < 0 then y:= y-x else y:= y+x {y=a+|x|} If Then Else {P and B} C1 {Q} {P and (not B)} C2 {Q} {P} if B then C1 else C2 {Q} Example: Want {y=a} if x < 0 then y:= y-x else y:= y+x {y=a+|x|} Suffices to show: (1) {y=a&x<0} y:=y-x {y=a+|x|} and (4) {y=a&not(x<0)} y:=y+x {y=a+|x|} 11/29/2018

{y=a&x<0} y:=y-x {y=a+|x|} (3) (y=a&x<0) y-x=a+|x| (2) {y-x=a+|x|} y:=y-x {y=a+|x|} (1) {y=a&x<0} y:=y-x {y=a+|x|} (1) Reduces to (2) and (3) by Precondition Strengthening (2) Follows from assignment axiom (3) Because x<0  |x| = -x 11/29/2018

{y=a&not(x<0)} y:=y+x {y=a+|x|} (6) (y=a&not(x<0))(y+x=a+|x|) (5) {y+x=a+|x|} y:=y+x {y=a+|x}} (4) {y=a&not(x<0)} y:=y+x {y=a+|x|} (4) Reduces to (5) and (6) by Precondition Strengthening (5) Follows from assignment axiom (6) Because not(x<0)  |x| = x 11/29/2018

(1) {y=a&x<0}y:=y-x{y=a+|x|} . If then else (1) {y=a&x<0}y:=y-x{y=a+|x|} . (4) {y=a&not(x<0)}y:=y+x{y=a+|x|} . {y=a} if x < 0 then y:= y-x else y:= y+x {y=a+|x|} By the if_then_else rule 11/29/2018

We need a rule to be able to make assertions about while loops. Inference rule because we can only draw conclusions if we know something about the body Let’s start with: { ? } C { ? } { ? } while B do C { P } 11/29/2018

While The loop may never be executed, so if we want P to hold after, it had better hold before, so let’s try: { ? } C { ? } { P } while B do C { P } 11/29/2018

While If all we know is P when we enter the while loop, then we all we know when we enter the body is (P and B) If we need to know P when we finish the while loop, we had better know it when we finish the loop body: { P and B} C { P } { P } while B do C { P } 11/29/2018

{ P } while B do C { P and not B } We can strengthen the previous rule because we also know that when the loop is finished, not B also holds Final while rule: { P and B } C { P } { P } while B do C { P and not B } 11/29/2018

{ P } while B do C { P and not B } { P and B } C { P } { P } while B do C { P and not B } P satisfying this rule is called a loop invariant because it must hold before and after the each iteration of the loop 11/29/2018

While While rule generally needs to be used together with precondition strengthening and postcondition weakening There is NO algorithm for computing the correct P; it requires intuition and an understanding of why the program works 11/29/2018

Example Let us prove {x>= 0 and x = a} fact := 1; while x > 0 do (fact := fact * x; x := x –1) {fact = a!} 11/29/2018

(P and not x > 0)  (fact = a!) Example We need to find a condition P that is true both before and after the loop is executed, and such that (P and not x > 0)  (fact = a!) 11/29/2018

Example First attempt: {a! = fact * (x!)} Motivation: What we want to compute: a! What we have computed: fact which is the sequential product of a down through (x + 1) What we still need to compute: x! 11/29/2018

Example By post-condition weakening suffices to show 1. {x>=0 and x = a} fact := 1; while x > 0 do (fact := fact * x; x := x –1) {a! = fact * (x!) and not (x > 0)} and 2. {a! = fact * (x!) and not (x > 0) }  {fact = a!} 11/29/2018

Problem 2. {a! = fact * (x!) and not (x > 0)}  {fact = a!} Don’t know this if x < 0 Need to know that x = 0 when loop terminates Need a new loop invariant Try adding x >= 0 Then will have x = 0 when loop is done 11/29/2018

P = {a! = fact * (x!) and x >=0} Example Second try, combine the two: P = {a! = fact * (x!) and x >=0} Again, suffices to show 1. {x>=0 and x = a} fact := 1; while x > 0 do (fact := fact * x; x := x –1) {P and not x > 0} and 2. {P and not x > 0}  {fact = a!} 11/29/2018

Example For 2, we need {a! = fact * (x!) and x >=0 and not (x > 0)}  {fact = a!} But {x >=0 and not (x > 0)}  {x = 0} so fact * (x!) = fact * (0!) = fact Therefore 11/29/2018

Example For 1, by the sequencing rule it suffices to show 3. {x>=0 and x = a} fact := 1 {a! = fact * (x!) and x >=0 } And 4. {a! = fact * (x!) and x >=0} while x > 0 do (fact := fact * x; x := x –1) {a! = fact * (x!) and x >=0 and not (x > 0)} 11/29/2018

Example Suffices to show that {a! = fact * (x!) and x >= 0} holds before the while loop is entered and that if {(a! = fact * (x!)) and x >= 0 and x > 0} holds before we execute the body of the loop, then {(a! = fact * (x!)) and x >= 0} holds after we execute the body 11/29/2018

{a! = fact * (x!) and x >= 0} Example By the assignment rule, we have {a! = 1 * (x!) and x >= 0} fact := 1 {a! = fact * (x!) and x >= 0} Therefore, to show (3), by precondition strengthening, it suffices to show (x>= 0 and x = a)  (a! = 1 * (x!) and x >= 0) 11/29/2018

Have that {a! = fact * (x!) and x >= 0} Example (x>= 0 and x = a)  (a! = 1 * (x!) and x >= 0) holds because x = a  x! = a! Have that {a! = fact * (x!) and x >= 0} holds at the start of the while loop 11/29/2018

{(a! = fact * (x!)) and x >= 0} Example To show (4): {a! = fact * (x!) and x >=0} while x > 0 do (fact := fact * x; x := x –1) {a! = fact * (x!) and x >=0 and not (x > 0)} we need to show that {(a! = fact * (x!)) and x >= 0} is a loop invariant 11/29/2018

{(a! = fact * (x!)) and x >= 0 and x > 0} Example We need to show: {(a! = fact * (x!)) and x >= 0 and x > 0} ( fact = fact * x; x := x – 1 ) {(a! = fact * (x!)) and x >= 0} We will use assignment rule, sequencing rule and precondition strengthening 11/29/2018

Example By the assignment rule, we have {(a! = fact * ((x-1)!)) and x – 1 >= 0} x := x – 1 {(a! = fact * (x!)) and x >= 0} By the sequencing rule, it suffices to show {(a! = fact * (x!)) and x >= 0 and x > 0} fact = fact * x 11/29/2018

Example By the assignment rule, we have that {(a! = (fact * x) * ((x-1)!)) and x – 1 >= 0} fact = fact * x {(a! = fact * ((x-1)!)) and x – 1 >= 0} By Precondition strengthening, it suffices to show that ((a! = fact * (x!)) and x >= 0 and x > 0)  ((a! = (fact * x) * ((x-1)!)) and x – 1 >= 0) 11/29/2018

Example However fact * x * (x – 1)! = fact * x and (x > 0)  x – 1 >= 0 since x is an integer,so {(a! = fact * (x!)) and x >= 0 and x > 0}  {(a! = (fact * x) * ((x-1)!)) and x – 1 >= 0} 11/29/2018

Example Therefore, by precondition strengthening {(a! = fact * (x!)) and x >= 0 and x > 0} fact = fact * x {(a! = fact * ((x-1)!)) and x – 1 >= 0} This finishes the proof 11/29/2018