Logic for Program Call chapter 7

Slides:



Advertisements
Similar presentations
In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and shows.
Advertisements

Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
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.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 2 – Reasoning About Code With Logic 1CSE 331 Winter 2014.
CS 355 – Programming Languages
Formal Methods of Systems Specification Logical Specification of Hard- and Software Prof. Dr. Holger Schlingloff Institut für Informatik der Humboldt.
CSE115/ENGR160 Discrete Mathematics 01/31/12 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 02/01/11
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Describing Syntax and Semantics
Chapter 10 Sequences, Induction, and Probability Copyright © 2014, 2010, 2007 Pearson Education, Inc Mathematical Induction.
1 Section 3.3 Mathematical Induction. 2 Technique used extensively to prove results about large variety of discrete objects Can only be used to prove.
Simplifying a Variable Expression
Mathematical Induction
Methods of Proof & Proof Strategies
Reading and Writing Mathematical Proofs
CSE 311: Foundations of Computing Fall 2013 Lecture 8: More Proofs.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
1 Sections 1.5 & 3.1 Methods of Proof / Proof Strategy.
9.4 Mathematical Induction
Chapter 3 Part II Describing Syntax and Semantics.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 3 The Foundations: Logic and Proof,
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
1+2+3+…+n = n(n+1)/2 We want to prove the above statement by mathematical Induction for all natural numbers (n=1,2,3,…) Next SlideSlide 1.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
CS104:Discrete Structures Chapter 2: Proof Techniques.
Quiz 7 The way I expected it.. How to do it! 1. Let P(n) be the statement that n 2 = n(n + 1)(2n + 1)/6 for the positive integer n. Be.
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
CSE 311 Foundations of Computing I Lecture 8 Proofs Autumn 2012 CSE
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 2 Formal Reasoning.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Hoare Logic LN chapter 5, 6 but without 6.8, 6.12, 6.13 (to be discussed later) Hoare Logic is used to reason about the correctness of programs. In the.
Chapter 1 Logic and Proof.
CSE15 Discrete Mathematics 02/01/17
Further with Hoare Logic Sections 6.12, 6.10, 6.13
CSE 311 Foundations of Computing I
Spring 2017 Program Analysis and Verification
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Chapter 3 The Real Numbers.
Predicate Calculus Validity
CSE 311: Foundations of Computing
CSE 331 Software Design & Implementation
Hoare Logic LN chapter 5, 6 but without 6. 8, 6. 12, 6
Chapter 5 Induction and Recursion
Quiz 6 The way I expected it..
Proving Properties of Recursive List Functions
CS201: Data Structures and Discrete Mathematics I
Formal Program Specification
Programming Languages and Compilers (CS 421)
Mathematical Reasoning
CSE 311: Foundations of Computing
Hoare Logic LN chapter 5, 6 Hoare Logic is used to reason about the correctness of programs. In the end, it reduces a program and its specification to.
Copyright © Cengage Learning. All rights reserved.
MA/CSSE 474 More Math Review Theory of Computation
Predicate Transformers
This Lecture Substitution model
CS201: Data Structures and Discrete Mathematics I
Copyright © Cengage Learning. All rights reserved.
Mathematical Reasoning
Program correctness Axiomatic semantics
Programming Languages and Compilers (CS 421)
Copyright © Cengage Learning. All rights reserved.
Formal Program Specification
Hoare Logic LN chapter 5, 6, 9.3 Hoare Logic is used to reason about the correctness of programs. In the end, it reduces a program and its specification.
Presentation transcript:

Logic for Program Call chapter 7

Dealing with program calls Consider a program with a known specification: {* P *} Pr(x) {* Q *} Suppose another program S that calls Pr. To reason about S’ correctness we will have to include reasoning about Pr as well. Options: Inline the code of Pr  does not work if we do not have Pr’s code, or if Pr is recursive. We “plug-in” the specification of Pr. How? The following slides will focus on this approach.

Program call in uPL For simplicity, uPL only allows a program to be called is as a “statement” in either of this variant : pname(actual-params) var := pname(actual-params) Calling a program in expression is not allowed, e.g: y := ifoo(x) +1 ;

Instantiating a specification Inevitably, when handing a program call we will need to instantiate the callee’s specification with the used actual parameters. Example, consider this specification: Consider a call incr(b+1,b). Simply replacing the formal with actual parameters can give a wrong specification on a program that can do side effect: {* base>0 *} R:=res; incr(base, OUT res) {* res>R+base *} {* b+1>0 *} B:=b; incr(b+1,b) {* b>B+b+1 *} But this is not a valid specification (take b=0 initially). The issue is caused by confused reference to two different b’s. The red b replaces “base”, which is a pass-by-value parameter, so in the post-condition it refers to its initial value. The purple b replaces “res” which is an OUT parameter, so it refers to its final value.

Instantiating a specification To make it safe, we will require that a program-level specification can only be instantiated by renaming its parameters and auxiliary variables, and furthermore they all must be distinct. Rule 7.2.1 (for two parameters, with one is an OUT param): We will handle complex actual parameters via program transformation (later). {* P *} X,Y:=x,y ; Pr(x, OUT y) {* Q *} ------------------------------------------------------------------------------------ {* P[x’,y’/x,y] *} X’,Y’:= x’,y’ ; Pr(x’,y’) {* Q[x’,y’,X’,Y’/x,y,X,Y] *}

Transforming the call first Consider the following program, containing a single call to another program: To safely instantiate the specification of incr, we first transform the program to an equivalent one (right). Fresh and distinct variables @b and @r are introduced as proxies of the actual parameters. {* b>0 *} x:=b ; @b,@r := b+1,b incr(@b,@r) b := @r {* b  x *} {* b>0 *} x:=b ; incr(b+1,b) {* b  x *}

Handling the resulting transformation {* b>0 *} x:=b ; @b,@r := b+1,b incr(@b,@r) b := @r {* b  x *} How to proceed now to prove the correctness of this program? Through wp we can calculate the post-condition for incr(@b,@r), namely @r  x. But then, how to reason over the program call itself? Idea: given the specification of incr, what would be the best pre-condition that guarantees incr to end up with @r  x ?

“Black box” reduction (1) The given specification of incr: (2) After instantiation with the actual parameters: Consider now a call incr(@b,@r) with some arbitrary post condition Q’ to establish. Question: given (2) come up with the “best” pre-condition for the call so that it will establish Q’. {* base>0 *} R:=res; incr(base, OUT res) {* res>R+base *} {* @b>0 *} R:=@r; incr(@b,@r) {* @r>R+@b *}

“Black box” reduction So, given this (the formula 2 from prev. slide) : What is the best P’ such that {* P’ *} incr(@b,@r) {* Q’ *} ? Idea: Require Q ⇒ Q’ , but to interpret this as a pre-condition requires us to “detach” references to final values of variables that receive side effect of incr. We also need to include P, to make sure that incr will end up in Q at all. {* @b>0 *} R:=@r; incr(@b,@r) {* @r>R+@b *} P Q

{* P’ *} incr(@b,@r) {* @r  x *} “Black box” reduction Q Given : In our example, the P’ to construct is for: P {* @b>0 *} R:=@r; incr(@b,@r) {* @r>R+@b *} Q’ {* P’ *} incr(@b,@r) {* @r  x *} Constructing P’ : We need Q⇒Q’ Replace occurrences of out parameters with fresh variables to detach them. Replace occurrences of auxiliary variables representing initial values with the variables they represent. We also need to pre-condition P. (1) @r>R+@b ⇒ @r  x (2) @b>0 /\ @r’>@r+@b ⇒ @r’  x (3)

Back to the problem 1 3 {* b>0 *} x:=b ; @b,@r := b+1,b incr(@b,@r) {* b+1>0 /\ @r’>b+b+1 ⇒ @r’  b *} (by calculating wp) {* b+1>0 /\ @r’>b+b+1 ⇒ @r’  x *} (by calculating wp) (by black box reduction we did in the previous slide) {* @b>0 /\ @r’>@r+@b ⇒ @r’  x *} The calculation produces (3) as a sufficient pre-condition for (2). The correctness of the program can be proven by proving the implication (1) ⇒ (3). {* @r  x *} (by calculating wp) 2

Black Box Rule Formally Rule 7.2.2 (for two parameters, with y being an OUT param): {* P *} X,Y := x,y ; Pr(x,OUT y) {* Q *} ----------------------------------------------------------------------- {* P /\ (Q  Q’) [y’/y] [x,y/X,Y] *} Pr(x,y) {* Q’ *} where y’ must be a fresh variable. Notice: Occurrences of OUT param/var in Q is replaced by fresh variables. Occurrences of X and Y (initial values of the params) are replaced by x and y (the vars they represent).

Black Box Rule Let x be a vector of distinct parameter names, and similarly X be a vector of distinct (auxiliary) variables. Rule 7.2.2, general form: {* P *} X := x ; Pr(x) {* Q *} ----------------------------------------------------------------------- {* P /\ (Q  Q’)[x’//x][x/X] *} Pr(x) {* Q’ *} where x’//x is a substitution that replaces out-params with fresh variables.

“Functional” box Calls to a program that behaves functionally can be handled more easily via Rules 7.3.2. Let x be a vector of (distinct) formal parameters and d be a vector of actual parameters (which can be complex): {* P *} Pr(x) {* return = e *} ----------------------------------------------------- {* P[d/x] /\ R[e’/y] *} y:=Pr(d) {* R *} where e’ = e[d/x]. all parameters are assumed to be passed-by-val. An example can be useful. y := e’ See this as wp of the assignment y := e’

Recursion Let Pr(int n,x) be a program that is recursive over n. Rule 7.4.1: a (simplified) induction rule to handle recursion: P  n0 \\n is bounded below {* P /\ n=0 *} Pr(n,x) {* Q *} \\base case {* P /\ n<K *} Pr(n,x) {* Q *} implies {* P /\ n=K /\ K>0 *} Pr(n,x) {* Q *} \\induct. Case ------------------------------------------------------------------------------- {* P *} Pr(n,x) {* Q *} Some details omitted; see LN. Rule 7.4.2 provides a simplified form to handle recursive programs that behave functionally.

Example As a simple example, consider the following recursive program that simply returns 0 if given a integer n  0. Let’s try to prove its specification: {* n  0 *} P(int n) { if n = 0 then return n else return P(n−1) } {* return = 0 *} The base case is not difficult. Let’s see the induction case.

The induction case The induction case boils down to proving the following, after reducing the spec to P’s body: {* n  0 /\ n=K /\ K>0 *} if n=0 then return:=n else return := P(n−1) {* return = 0 *} By applying the functional Black Box rule 7.3.2, and using the induction hypothesis we can calculate the needed pre-condition for this call, namely: n-10 /\ n-1< K /\ 0=0 Then we can calculate the wp of the if-then-else, and finish the proof.