CSE 331 SOFTWARE DESIGN & IMPLEMENTATION WORKSHEET A Autumn 2011 Today’s Process If you haven’t completed the solution sheet for Worksheet A, please leave.

Slides:



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

In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and shows.
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.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
1 Discrete Structures Lecture 29 Predicates and Programming Read Ch
Predicate Transformers
Program Proving Notes Ellen L. Walker.
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.
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.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION WORKSHEET B Autumn 2011 Today’s Process If you haven’t completed the solution sheet for Worksheet B, please leave.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
4/17/2017 Section 3.6 Program Correctness ch3.6.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic 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.
Semantics “Semantics” has to do with the meaning of a program. We will consider two types of semantics: –Static semantics: semantics which can be enforced.
Proving Program Correctness The Axiomatic Approach.
Reading and Writing Mathematical Proofs
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
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.
1 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
1 cs205: engineering software university of virginia fall 2006 Avoiding Software Disasters.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Reasoning about programs March CSE 403, Winter 2011, Brun.
CSE 311 Foundations of Computing I Lecture 7 Logical Inference Autumn 2012 CSE
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
CSE 311 Foundations of Computing I Lecture 9 Proofs and Set Theory Autumn 2012 CSE
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.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
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.
SECTION 1: CODE REASONING + VERSION CONTROL slides borrowed and adapted from Alex Mariakis and CSE 390a Justin Bare & Deric Pang.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
CSE 311 Foundations of Computing I Lecture 8 Proofs Autumn 2012 CSE
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
11/22/2016IT 3271 A formal system:Axioms and Rules, for inferring valid specification x := m; y := n; while ¬(x=y) do if x>y then x := x-y else y := y-x.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
CSE 331 Software Design & Implementation
Formal Methods in Software Engineering 1
Mathematical Structures for Computer Science Chapter 1
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Reasoning about Loops, Conclusion
CSE 311 Foundations of Computing I
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Section 1: Code Reasoning
Formal Methods in software development
Predicate Transformers
Formal Methods in software development
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
CIS 720 Lecture 3.
Programming Languages and Compilers (CS 421)
CIS 720 Lecture 3.
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Program Correctness an introduction.
Presentation transcript:

CSE 331 SOFTWARE DESIGN & IMPLEMENTATION WORKSHEET A Autumn 2011 Today’s Process If you haven’t completed the solution sheet for Worksheet A, please leave (and go finish it) Make sure your student ID (or name) is on your solution sheet We’ll collect them all, shuffle them, and hand them out – if you get your own, let us know ASAP, since grading your own is not allowed Then use a post-it to put your student ID (and name) on the sheet you are grading – otherwise we cannot give you the extra credit you should earn

y≤3 {x=y+1} 2x+y≤11 CSE331 11au 2  Apply assignment rule Q(E) {x = E} Q(x)  In this case, directly from the triple  E  y+1  Q(x)  2x+y≤11  So  Q(E)  2(y+1)+y≤11  2y+2+y≤11  3y≤9  y≤3  The precondition is y≤3, which implies Q(E), so the triple is true 2 points for true 0 points for false 1 point for false with an arithmetic mistake ABAB B FT A FTT TFT Reminder: logical implication

n is even ∧ n≤100 {n = n + 2} n is even ∧ n≤101 CSE331 11au 3  Apply assignment rule; in this case, directly from the triple  E  n+2  Q(n)  n is even ∧ n≤101  So  Q(E)  (n+2 is even) ∧ n+2≤101  (n+2 is even) ∧ n≤99  The precondition is n is even ∧ n≤100  The precondition conjunct n is even  n+2 is even  The precondition conjunct n≤100 does not  n ≤99  So the triple is false 2 points for false; 0 points for true 1 point for true with an arithmetic mistake but not for getting the implication between n≤100 and n≤99 wrong

true {x = 5; y = 0} x=5 CSE331 11au 4  Introduce intermediate assertion  true {x = 5} x=5 {y = 0} x=5  Work backwards (forwards works, too)  x=5 {y = 0} x=5 is true by trivial application of assignment rule  true {x = 5} x=5 is also true by trivial application of assignment rule  So the triple is true 2 points for true 0 points for false No partial credit

x=2 ∧ x=3 {x = 5} x=0 CSE331 11au 5  Simplify to false {x = 5} x=0  A Hoare triple with a false precondition is always true – the same as false precondition allowing a piece of code to provide any behavior  Or push the assignment rule through to get false  0=5, which also yields true since false  false is true  So the triple is true 2 points for true 0 points for false No partial credit

true {System.out.println("Hello world")} false CSE331 11au 6  Because the print statement doesn’t change the program state, this reduces to true {no-op} false  Does true  false ? No  So the triple is false 2 points for false 0 points for true No partial credit

false {System.out.println("Hello world")} true CSE331 11au 7  See x=2 ∧ x=3 {x = 5} x=0  The triple is true 2 points for true 0 points for false No partial credit

x=0 {while x == 0 do x = x+1} x=1 CSE331 11au 8  Loops of the form P {while B do S} Q require a loop invariant I to be defined and three properties to A. The invariant holds on entry to the loop: P  I B. The invariant holds after execution of the loop body: BI { S } I C. If the loop terminates, the postcondition can be proved: BI  Q  Define I as x=0x=1 A. x=0  x=0x=1 B. x=0(x=0x=1) { x=x+1 } x=0x=1 x=0 { x=x+1 } x=0x=1 Q(x) x=0x=1 so Q(E)  x+1=0x+1=1 Q(E) x=-1x=0 Precondition x=0  Q(E) C. x!=0(x=0x=1)  x=1  So the triple is true 2 points for true 1 point for false with correct invariant 0 points for false with incorrect invariant

x=1 {while x != 0 do x = x+1} x=100 CSE331 11au 9  Define I as x100 A. x=1  x100 B. Q(x) x100 so Q(E)  x+1100 Q(E) x99 Precondition x=1  Q(E) C. x=0(x100) does not  x=100  Could there be an I that works? No. Negating the condition for the third part will never let x=0I  x=100 for any I 2 points for false 0 points for true

2x=3y {x = 2x} x=3y CSE331 11au 10  Q(x)  x=3y  Q(E) Q(2x)  ( 2x)=3y  2x=3y  So the precondition 2x=3y  2x=3y [Q(E)]  Yes, so the tuple is true 2 points for true 0 points for false No partial credit Q(E) {x = E} Q(x)

x>2 {if x>2 then y=1 else y=-1} y>0 CSE331 11au 11  Conditionals of the form P {if C S 1 else S 2 } Q require (PC { S 1 } Q)  (P C { S 2 } Q)  (x>2x>2 { y=1 } y>0)  (x>2x2 { y=-1 } y>0)  The first conjunct is true: x>2 {y=1} y>0  The second conjunct is true: false { y=-1 } y>0  So the triple is true 2 points for true 0 points for false No partial credit

x/y – remainder ( r ) and quotient ( q ) CSE331 11au 12 true { r = x; q = 0; while (y <= r) { r = r − y; q = 1 + q; } x = r + yq ∧ y > r I  x=r+yq A.(r=xq=0) implies x=r+yq (r=x) implies x=r B.(yr)^I { r=r-y;q=q+1 } I x=(r-y)+y(q+1) x=r-y+yq+y x=r+yq C.y>rx=r+y×q implies x=r+yq ∧ y>r QED -2 for reporting that the triple is false -3 points for incorrect loop invariant -1 point for each of the three subparts that is missing -1 or -2 for confusing reasoning

CSE331 11au 13 n > 0 { k=0; j=1; while (k!=n){ k=k+1; j=2*j; } } j = 2 n I  j=2 k A.(k=0j=1)  j=2 k B.(k!=n)j=2 k { k=k+1;j=2*j } j=2 k 2j=2 (k+1) 2j=2 k 2 j=2 k C.(k=nj=2 k )  j=2 n QED -2 for reporting that the triple is false -3 points for incorrect loop invariant -1 point for each of the three subparts that is missing -1 or -2 for confusing reasoning

CSE331 11au 14 n > 0 { k=0; j=1; while (k!=n){ k=k+1; j=2+j; } } j = 2n+1 I  j=2k+1 A.(k=0j=1)  j=2k+1 B.(k!=n)j=2k+1 { k=k+1;j=2+j } j=2k+1 2+j=2(k+1)+1 2+j=2k j=2k+3 j=2k+1 C.(k=nj=2 k )  j=2 n QED QED -3 for reporting that the triple is false -4 for an entirely separate proof from the previous item -1 to -3 for confusing association between the previous proof and this proof I  j=2 k A.(k=0j=1)  j=2 k B.(k!=n)j=2 k { k=k+1;j=2*j } j=2 k 2j=2 (k+1) 2j=2 k 2 j=2 k C.(k=nj=2 k )  j=2 n QED

CSE331 11au