Reasoning about programs March 09 2011 CSE 403, Winter 2011, Brun.

Slides:



Advertisements
Similar presentations
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
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.
Partial correctness © Marcelo d’Amorim 2010.
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.
Simple Example {i = 0} j := i * i {j < 100} Can we ‘verify’ this triple? Only if we know the semantics of assignment.
1 Discrete Structures Lecture 29 Predicates and Programming Read Ch
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
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.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 2 – Reasoning About Code With Logic 1CSE 331 Winter 2014.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
Proving the Correctness of Algorithms Algorithm Design and Analysis Week 2
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Axiomatic Semantics Dr. M Al-Mulhem ICS
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
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.
4/17/2017 Section 3.6 Program Correctness ch3.6.
Describing Syntax and Semantics
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Proving Program Correctness The Axiomatic Approach.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Proving Program Correctness The Axiomatic Approach.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Reading and Writing Mathematical Proofs
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Semantics In Text: Chapter 3.
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.
Week 10: Loop Invariants, Code correctness Jimmy Voss Disclaimer: Some material may have been borrowed from both the Official Course slides as well as.
Understanding ADTs CSE 331 University of Washington.
1 Software Correctness Proofs CIS 376 Bruce R. Maxim UM-Dearborn.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 2 Formal Reasoning.
Interface specifications At the core of each Larch interface language is a model of the state manipulated by the associated programming language. Each.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Proving the Correctness of Algorithms
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Proving Loops Testing debugging and verification
CSE 331 Software Design & Implementation
Formal Methods in Software Engineering 1
CSE 331 Software Design and Implementation
Reasoning About Code; Hoare Logic
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Predicate Transformers
The Zoo of Software Security Techniques
Program Verification with Hoare Logic
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Program Correctness an introduction.
Presentation transcript:

Reasoning about programs March CSE 403, Winter 2011, Brun

Team member contribution #3

Collaborative development survey 10 simple questions On catalyst Each participation points No wrong answers Due by 11pm on Friday March 11th

It’s the home stretch Final (1.0) release due Thursday presentation due in class Final (1.0) release due Thursday presentation due in class

Second midterm Design patterns Testing – guest speaker: unit testing Debugging Consistent and complete specifications Reasoning about programs – guest speaker: language annotations Wednesday, March 16, 2:30 PM – 4:20 PM, EEB 045

Ways to verify your code The hard way: – Make up some inputs – If it doesn't crash, ship it – When it fails in the field, attempt to debug The easier way: – Reason about possible behaviors and desired outcomes – Construct simple tests that exercise those behaviors Another way that can be easy – Prove that the system does what you want Rep invariants are preserved Implementation satisfies specification – Proof can be formal or informal (we will be informal) – Complementary to testing

Reasoning about code Determine what facts are true during execution – x > 0 – for all nodes n: n.next.previous == n – array a is sorted – x + y == z – if x != null, then x.a > x.b Applications: – Ensure code is correct (via reasoning or testing) – Understand why code is incorrect

Forward reasoning You know what is true before running the code What is true after running the code? Given a precondition, what is the postcondition? Applications: Representation invariant holds before running code Does it still hold after running code? Example: // precondition: x is even x = x + 3; y = 2x; x = 5; // postcondition: ??

Backward reasoning You know what you want to be true after running the code What must be true beforehand in order to ensure that? Given a postcondition, what is the corresponding precondition? Application: (Re-)establish rep invariant at method exit: what’s required? Reproduce a bug: what must the input have been? Example: // precondition: ?? x = x + 3; y = 2x; x = 5; // postcondition: y > x How did you (informally) compute this?

Forward vs. backward reasoning Forward reasoning is more intuitive for most people – Helps understand what will happen (simulates the code) – Introduces facts that may be irrelevant to goal Set of current facts may get large – Takes longer to realize that the task is hopeless Backward reasoning is usually more helpful – Helps you understand what should happen – Given a specific goal, indicates how to achieve it – Given an error, gives a test case that exposes it

Forward reasoning example assert x >= 0; i = x; // x ≥ 0 & i = x z = 0; // x ≥ 0 & i = x & z = 0 while (i != 0) { z = z + 1; i = i –1; } // x ≥ 0 & i = 0 & z = x assert x == z;  What property holds here?

Backward reasoning Technique for backward reasoning: Compute the weakest precondition (“wp”) There is a wp rule for each statement in the programming language Weakest precondition yields strongest specification for the computation (analogous to function specifications)

Assignment // precondition: ?? x = e; // postcondition: Q Precondition = Q with all (free) occurrences of x replaced by e Example: // assert: ?? x = x + 1; // assert x > 0 Precondition = (x+1) > 0

Method calls // precondition: ?? x = foo(); // postcondition: Q If the method has no side effects: just like ordinary assignment If it has side effects: an assignment to every variable in modifies Use the method specification to determine the new value

If statements // precondition: ?? if (b) S1 else S2 // postcondition: Q Essentially case analysis: wp(“if (b) S1 else S2”, Q) = ( b  wp(“S1”, Q) ∧  b  wp(“S2”, Q) )

If: an example // precondition: ?? if (x == 0) { x = x + 1; } else { x = (x/x); } // postcondition: x  0 Precondition: wp(“if (x==0) {x = x+1} else {x = x/x}”, x  0) = ( x = 0  wp(“x = x+1”, x  0) & x  0  wp(“x = x/x”, x  0) ) = (x = 0  x + 1  0) & (x  0  x/x  0) = 1  0 & 1  0 = true

Reasoning About Loops A loop represents an unknown number of paths – Case analysis is problematic – Recursion presents the same issue Cannot enumerate all paths – That is what makes testing and reasoning hard

Loops: values and termination 1) Pre-assertion guarantees that x  y 2) Every time through loop x  y holds and, if body is entered, x > y y is incremented by 1 x is unchanged Therefore, y is closer to x (but x  y still holds) 3) Since there are only a finite number of integers between x and y, y will eventually equal x 4) Execution exits the loop as soon as x = y // assert x  0 & y = 0 while (x != y) { y = y + 1; } // assert x = y

Understanding loops by induction We just made an inductive argument Inducting over the number of iterations Computation induction Show that conjecture holds if zero iterations Assume it holds after n iterations and show it holds after n+1 There are two things to prove: Some property is preserved (known as “partial correctness”) loop invariant is preserved by each iteration The loop completes (known as “termination”) The “decrementing function” is reduced by each iteration

Loop invariant for the example So, what is a suitable invariant? What makes the loop work? LI = x  y 1)x  0 & y = 0  LI 2)LI & x  y { y = y+1; } LI 3)(LI &  (x  y))  x = y // assert x  0 & y = 0 while (x != y) { y = y + 1; } // assert x = y

In practice I don’t routinely write loop invariants I do write them when I am unsure about a loop and when I have evidence that a loop is not working – Add invariant and decrementing function if missing – Write code to check them – Understand why the code doesn't work – Reason to ensure that no similar bugs remain