Reasoning about Loops, Conclusion

Slides:



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

Program verification: flowchart programs Book: chapter 7.
Program verification: flowchart programs Book: chapter 7.
Copyright , Doron Peled and Cesare Tinelli. These notes are based on a set of lecture notes originally developed by Doron Peled at the University.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Reasoning About Code; Hoare Logic, continued
50.530: Software Engineering Sun Jun SUTD. Week 9: Hoare Logic.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
Predicate Transformers
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 A Autumn 2011 Today’s Process If you haven’t completed the solution sheet for Worksheet A, please leave.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLine’s CSE 503, Software Engineering University of Washington 26 Apr 2004.
K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond, WA part 0 Summer School on Logic and Theorem-Proving in Programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
1 Assignment 6. Developing Loops Due on Tuesday, 30 October, by midnight (submitted electronically). Note that each question will be graded on the following.
Specifications. Announcements HW1 is due Friday To submit answers, commit Error in HW1: In problem 8, part 2 z = 0 should be z = MAX_INT 2.
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.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Reading and Writing Mathematical Proofs
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
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.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
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.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Proving Loops Testing debugging and verification
Formal Methods in Software Engineering 1
Hoare-style program verification
Testing, conclusion Based on material by Michael Ernst, University of Washington.
Mathematical Structures for Computer Science Chapter 1
Representation Invariants and Abstraction Functions
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Specifications, conclusion. Abstract Data Types (ADTs)
Exam 1 Review.
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Equality, conclusion Based on material by Michael Ernst, University of Washington.
Semantics In Text: Chapter 3.
Java Reasoning About Code
Section 1: Code Reasoning
Formal Methods in software development
Predicate Transformers
Formal Methods in software development
Output Variables {true} S {i = j} i := j; or j := i;
The Zoo of Software Security Techniques
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Reasoning about Loops, Conclusion

Announcements QUIZ 1 today. You have 10 minutes Hand in quiz at the end of class

Announcements HW1 will be up tomorrow (due next Thursday) Check class Web page for an announcement You must clone a new repository, hw01 To submit answers, push to Git, then submit in Submitty If you have questions, please email us at csci2600@cs.lists.rpi.edu Use LMS discussion board

Outline Reasoning about loops (conclusion) Dafny basics Specifications (next time) Spring 18 CSCI 2600, K. Kuzmin, A Milanova

So Far We discussed reasoning about code Hoare Logic Forward reasoning and backward reasoning Hoare Logic Hoare Triples Rule for backward reasoning Assignment Sequence If-then-else Method call Reasoning about loops Spring 17 CSCI 2600, A Milanova

Reasoning About Loops Partial correctness Termination total correctness=partial correctness+termination Partial correctness “Guess”, then prove loop invariant Loop invariant and loop exit condition must imply the postcondition This gives us: “If the loop terminated then the postcondtion did hold”. But does the loop terminate? Termination “Guess” decrementing function D. (1) D >= 0, (2) strictly decreases, (3) D at 0 along with the loop invariant must imply loop exit condition

Let’s Catch the Bug Precondition: len ≥ 1 && a.length = len int sum = a[0]; int i = 1; while (i <= len) invariant sum = a[0]+…+a[i-1] && i<=len+1 { sum = sum + a[i]; i = i+1; } Postcondition: sum = a[0]+…+a[a.length-1] After a bit of observation, we’ll settle on the above loop invariant. Step 1. Invariant holds before the loop. Step 2. If Inv holds after kth iteration, it holds after k+1st iteration as well. Step 3. i > len AND i <= len+1 => i = len+1. Thus, Spring 18 CSCI 2600, K. Kuzmin, A Milanova

“Interesting” Invariant Another Factorial Precondition: t >= 0 r = 1; n = t; while (n != 0) { r = r*n; n = n-1; } Postcondition: r = t! r = t!/n! Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example due to Michael Ernst, UW)

Interesting Invariant Integer Division Precondition: x >= 0 && y > 0 r = x; q = 0; while (y <= r) { r = r-y; q = q+1; } Postcondition: x = y*q + r && r < y Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example due to Michael Ernst, UW)

Interesting Invariant Precondition: a > 0 && b > 0 y1 = a; y2 = b; while (y1 != y2) invariant gcd(y1,y2) = gcd(a,b) { if (y1 > y2) { y1 = y1-y2 } else { y2 = y2-y1; } Postcondition: y1 = gcd(a,b) gcd(y1,y2) = gcd(y1-y2,y2)

Dafny Dafny: programming language and verifier Author: K. Rustan M. Leino, Microsoft Research Programmer writes programs with specifications Verifier proves that program obeys specification Binary downloads at https://github.com/Microsoft/dafny Try online at http://www.rise4fun.com/dafny Fall 17 CSCI 2600, A Milanova

Dafny Basics The smallest unit of verification is the method method Foo(x: int, y: int) returns (z: int, w: int) Preconditions requires x == 0 && y >= 0 Postconditions ensures z != 0 || w != 0 Fall 17 CSCI 2600, A Milanova

Our Earlier Exercise, in Dafny // precondition: ?? y = x + 4 if (x > 0) { y = x*x – 1; } else { y = y+x; } { y = 0 } x = x/y; Find what input causes divide-by-zero at the last statement. Answer: Precondition: x=1 || x=-2 These are the inputs that cause divide-by-zero error

Our Division Example in Dafny method DivisionByZero(x: int) returns (y: int) requires x == 1 || x == -2 ensures y == 0 { y := x + 4; if (x > 0) { y := x*x - 1; } else { y := y + x; } Named returns, can have multiple output variables! Equality test: ==, NOT = Assignment: :=, NOT =