Verifiable Programming Reason about imperative sequential programs such as Java programs Imperative program –defines state space defined by collection.

Slides:



Advertisements
Similar presentations
Program verification: flowchart programs Book: chapter 7.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Copyright , Doron Peled and Cesare Tinelli. These notes are based on a set of lecture notes originally developed by Doron Peled at the University.
Functional Verification III Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture Notes 23.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
11111 Functional Program Verification CS 4311 A. M. Stavely, Toward Zero Defect Programming, Addison-Wesley, Y. Cheon and M. Vela, A Tutorial on.
This Week Finish relational semantics Hoare logic Interlude on one-point rule Building formulas from programs.
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 13.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Having Proofs for Incorrectness
Basic Properties of Relations
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.
Complexity 26-1 Complexity Andrei Bulatov Interactive Proofs.
Spin Tutorial (some verification options). Assertion is always executable and has no other effect on the state of the system than to change the local.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Discrete Mathematics Lecture 4 Harper Langston New York University.
Inverses and GCDs Supplementary Notes Prepared by Raymond Wong
Discrete Structures Chapter 2 Part B Mathematical Induction
Data Flow Analysis Compiler Design Nov. 8, 2005.
4/17/2017 Section 3.6 Program Correctness ch3.6.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Describing Syntax and Semantics
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
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.
Induction and recursion
Chapter one Linear Equations
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
CSE 755, part3 Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP: ::=skip | | | | ; | | Only integer vars; no procedures/fns; vars declared.
Reading and Writing Mathematical Proofs
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
Methods of Proof Lecture 3: Sep 9. This Lecture Now we have learnt the basics in logic. We are going to apply the logical rules in proving mathematical.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Lazy Annotation for Program Testing and Verification Speaker: Chen-Hsuan Adonis Lin Advisor: Jie-Hong Roland Jiang November 26,
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Recursive Algorithms &
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Complexity 24-1 Complexity Andrei Bulatov Interactive Proofs.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Verifiable Programming Reason about imperative sequential programs such as Java Imperative program –defines state space defined by collection of typed.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE15 Discrete Mathematics 04/12/17
Functional Verification III
Formal verification in SPIN
Reasoning About Code.
Formal Methods in Software Engineering 1
Copyright © Cengage Learning. All rights reserved.
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Predicate Transformers
Functional Program Verification
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Application: Algorithms
Application: Algorithms
Functional Verification III
Program correctness Axiomatic semantics
COP4020 Programming Languages
Presentation transcript:

Verifiable Programming Reason about imperative sequential programs such as Java programs Imperative program –defines state space defined by collection of typed program variables are coordinate axis of state space –pattern of actions operating in state space

View of imperative programming State space –program variables x1,x2, … xn –Cartesian product of the variable types T1, T2, …,Tn (each type stands for the associated value set) –one state: n-tuple of values

View of imperative programming Actions performed – change current state by assigning new values to program variables –assignment statement x:=e evaluate expression e; old value of x is lost e may have free variables; value of x depends on current state

View of imperative programming Program execution: sequence of actions A1, A2, A3, … causing transitions through a sequence of states s1,s2,… possibly terminating in a final state sn. Initial state s1 represents input data and final state sn represents the results

View of imperative programming Conditions satisfied after each action Special case: assignment statements Si only P0 PnPn-1P2P1 S1 SnS2 Pi express properties of the states possible at the ith stage

Sloppy use: Interlude Terms like condition, requirement, assertion and relation are often sloppy in a technical sense. Formulas or predicates (functions with range BOOL) Formula x<2y: which predicate?

Sloppy use: Interlude Formula x<2y: which predicate?

Sloppy use: Interlude Formula x<2y: which predicate?

Sloppy use: Interlude x is a list of distinct variables and e is a corresponding list of expressions.

Sloppy use: Interlude When we use the term predicate for a formula P, we mean where x stands for the list of all program variables. We assume that functions have never hidden arguments as, for instance

Sloppy use: Interlude We assume that functions have never hidden arguments as, for instance The value of an expression e can depend on, at most, the free variables in e.

Definitions

Predicate P as a set of states.

Definitions More later.

Flow charts May be generalized to arbitrary flow charts with branchings and cycles whose arcs are annotated with state predicates. P0 PnPn-1P2P1 S1 SnS2

Flow charts Arcs: represent sets of states Nodes: represent state transitions Only nodes are important for execution but state predicates are of fundamental importance to understand programs and to reason about them

Flow chart-based verification 50 year anniversary J. von Neumann and H.H. Goldstine 1947 –assertion boxes –operation boxes Robert Floyd (1967): 20 years later A predicate P associated with an arc A is intended to signify that the state satisfies P whenever control passes along A.

x,y := m,n q,r := x/y, x mod y r= 0 x,y=y,r YES NO

x,y := m,n q,r := x/y, x mod y r= 0 x,y=y,r YES NO x,y|25,15|15,10|10,5 q,r|1,10| 1,5| 2,0 gcd(25,15)

Proof approach: Induction Show that assertion on each arc is satisfied whenever control reaches that arc, provided P 0 holds initially. For each node in the graph and every pair of incoming and outgoing arcs a1,a2 we prove that if the assertion on a1 holds before the operation, then the assertion on a2 holds after the operation.

Proof approach: Induction As a result of these proofs the theorem follows by induction on the number of operations performed.

Proof approach: Induction P1 and P5 imply P1 implies P2. P5 implies P2.

Proof approach: Induction On the YES branch, we know x is a multiple of y and therefore P3 follows.

Proof approach: Induction On the NO branch, r is non-zero, which by P2 gives 0<r in P4.

Proof approach: Induction The simultaneous assignment x,y:=y,r shows that what is true for y,r in P4 must hold for x,y in P5. x>0 and y>0 is clear. To justify gcd(x,y) = gcd(m,n): Crux

Proof approach: Induction Must show that P4 implies gcd(y,r)=gcd(m,n). P4 gives x=q*y+r. If y and r are evenly divisible by k, then so is x. F(x) = {k s.t. k divides x evenly}

Proof approach: Induction P4 also gives r=x -q*y.

Proof approach: Induction Intersection on both sides with F(y)

Proof approach: Induction gcd(y,r)=gcd(x,y) QED

Conditional correctness y=gcd(m,n) whenever the algorithm terminates (control eventually reaches the YES branch). Next prove termination: show that progress toward a goal is being made. Show that y decreases with each execution of the loop: y:=r is the only assignment to y in loop.

Prove termination P4 asserts that r<y. y remains positive. Thus the execution must stop.

External/internal documentation P0 and P3 together comprise the external specification: What a user must know. The other assertions: internal documentation: explain to a reader how and why the algorithm works. Not all assertions are equally important: if P2 is given, can easily find the others.

Loop invariant P2 is called a loop invariant: remains true every time around the loop. Loop invariant provides essential and non- obvious information. Note that most proof steps are trivial mathematically.

Use program notation const m,n : Int; {m >=0 and n>0} var x,y : Int = m,n; loop {const q : Int = x/y;} const r : Int = x mod y; {x=q*y+r and 0<=r<y and gcd(x,y)=gcd(m,n)} while r != 0; x,y := y,r; repeat {y = gcd(m,n)}

Two important remarks about Floyd/Hoare style verification Not applicable to life-size programs unless one is very careful about program structure. Consider only small part of total state space. State assertions should not be afterthoughts; they belong to the program design phase. Non-trivial invariants are difficult to come up with.

Help during program design phase Compute y for given x with relative accuracy eps : Real > 0. Use iteration: y, z (current term), k plausible initializations: –y:=0 or 1 or 1+x –z:=1 or x –k:=0 or 1

Help during program design phase Plausible loop assignment statements –y:=y+z or y+z*x/k or y+z*x/(k+1) –z:=z*x/k or z*x/(k+1) –k:=k+1 3*2*2*3*2 = 72 possible statement sets

Help during program design phase Choose a good loop invariant first k=1 y=1+x; z=x while abs(z) > = eps*y; –k=k+1; z:=z*x/k; y:=y+z; repeat

Help during program design phase/ignore rounding errors Const x, eps : Real; {eps > 0} var k : Int =1; var y,z : Real =1+x; x; loop –while abs(z) > = eps*y; –k:=k+1; z:=z*x/k; y:=y+z; repeat