Proof Calculus for Partial Correctness

Similar documents
Hoare Logic: Reasoning About Imperative Programs

Axiomatic Semantics. Lecture 9 CS 565 2/12/08

Dynamic Semantics. Dynamic Semantics. Operational Semantics Axiomatic Semantics Denotational Semantic. Operational Semantics

What happens to the value of the expression x + y every time we execute this loop? while x>0 do ( y := y+z ; x := x:= x z )

Program verification. Hoare triples. Assertional semantics (cont) Example: Semantics of assignment. Assertional semantics of a program

Floyd-Hoare Style Program Verification

Weakest Precondition Calculus

Natural Deduction for Propositional Logic

Programming Languages and Compilers (CS 421)

Hoare Calculus and Predicate Transformers

Normal Forms of Propositional Logic

Hoare Logic: Part II

Lecture 17: Floyd-Hoare Logic for Partial Correctness

Hoare Logic I. Introduction to Deductive Program Verification. Simple Imperative Programming Language. Hoare Logic. Meaning of Hoare Triples

Foundations of Computation

CSE 331 Winter 2018 Reasoning About Code I

Last Time. Inference Rules

COP4020 Programming Languages. Introduction to Axiomatic Semantics Prof. Robert van Engelen

Hoare Logic and Model Checking

Spring 2016 Program Analysis and Verification. Lecture 3: Axiomatic Semantics I. Roman Manevich Ben-Gurion University

Introduction to Z3. Bow-Yaw Wang. December 19, Institute of Information Science Academia Sinica, Taiwan

A Short Introduction to Hoare Logic

Spring 2015 Program Analysis and Verification. Lecture 4: Axiomatic Semantics I. Roman Manevich Ben-Gurion University

Hoare Logic: Reasoning About Imperative Programs

Formal Methods in Software Engineering

The Assignment Axiom (Hoare)

Proofs of Correctness: Introduction to Axiomatic Verification

Lecture Notes: Axiomatic Semantics and Hoare-style Verification

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

Reasoning About Imperative Programs. COS 441 Slides 10b

CSE 331 Software Design & Implementation

Mid-Semester Quiz Second Semester, 2012

COMP2111 Glossary. Kai Engelhardt. Contents. 1 Symbols. 1 Symbols 1. 2 Hoare Logic 3. 3 Refinement Calculus 5. rational numbers Q, real numbers R.

Axiomatic Semantics. Operational semantics. Good for. Not good for automatic reasoning about programs

Solutions to exercises for the Hoare logic (based on material written by Mark Staples)

Program Analysis Part I : Sequential Programs

Software Engineering

Axiomatic Semantics. Hoare s Correctness Triplets Dijkstra s Predicate Transformers

First Order Logic vs Propositional Logic CS477 Formal Software Dev Methods

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

CSE 331 Winter 2018 Homework 1

Axiomatic Semantics: Verification Conditions. Review of Soundness and Completeness of Axiomatic Semantics. Announcements

Classical Program Logics: Hoare Logic, Weakest Liberal Preconditions

CS156: The Calculus of Computation Zohar Manna Autumn 2008

Program verification. 18 October 2017

Hoare Logic (I): Axiomatic Semantics and Program Correctness

Axiomatic Semantics. Semantics of Programming Languages course. Joosep Rõõmusaare

Deterministic Program The While Program

AN INTRODUCTION TO SEPARATION LOGIC. 2. Assertions

Verifying Properties of Parallel Programs: An Axiomatic Approach

CIS (More Propositional Calculus - 6 points)

Formal Specification and Verification. Specifications

Axiomatic semantics. Semantics and Application to Program Verification. Antoine Miné. École normale supérieure, Paris year

Deductive Verification

THE AUSTRALIAN NATIONAL UNIVERSITY Second Semester COMP2600 (Formal Methods for Software Engineering)

Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11 CSE

Formal Reasoning CSE 331. Lecture 2 Formal Reasoning. Announcements. Formalization and Reasoning. Software Design and Implementation

Unifying Theories of Programming

THE AUSTRALIAN NATIONAL UNIVERSITY Second Semester COMP2600/COMP6260 (Formal Methods for Software Engineering)

CS558 Programming Languages

Spring 2014 Program Analysis and Verification. Lecture 6: Axiomatic Semantics III. Roman Manevich Ben-Gurion University

Central Algorithmic Techniques. Iterative Algorithms

Axiomatic Semantics: Verification Conditions. Review of Soundness of Axiomatic Semantics. Questions? Announcements

CS156: The Calculus of Computation

Hoare Examples & Proof Theory. COS 441 Slides 11

Proof Rules for Correctness Triples

CS156: The Calculus of Computation

Verifying Java-KE Programs

Program verification using Hoare Logic¹

THE AUSTRALIAN NATIONAL UNIVERSITY Second Semester COMP2600 (Formal Methods for Software Engineering)

Bilateral Proofs of Safety and Progress Properties of Concurrent Programs (Working Draft)

Program Analysis and Verification

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

What? Correctness? Bugs!

Introduction to Temporal Logic. The purpose of temporal logics is to specify properties of dynamic systems. These can be either

Soundness and Completeness of Axiomatic Semantics

Loop Convergence. CS 536: Science of Programming, Fall 2018

CSC 7101: Programming Language Structures 1. Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11.

Spring 2015 Program Analysis and Verification. Lecture 6: Axiomatic Semantics III. Roman Manevich Ben-Gurion University

5. Program Correctness: Mechanics

Strength; Weakest Preconditions

Decision Procedures. Jochen Hoenicke. Software Engineering Albert-Ludwigs-University Freiburg. Winter Term 2016/17

Lecture 2: Axiomatic semantics

Program Construction and Reasoning

Data Structures and Algorithms Chapter 2

Proving Programs Correct

Ch 3.2: Direct proofs

Introduction to Divide and Conquer

CIS 500: Software Foundations

Design of Distributed Systems Melinda Tóth, Zoltán Horváth

(La méthode Event-B) Proof. Thanks to Jean-Raymond Abrial. Language of Predicates.

Formal Methods for Probabilistic Systems

Supplementary Notes on Inductive Definitions

Lecture Notes on Compositional Reasoning

Proof Techniques (Review of Math 271)

Notation for Logical Operators:

INVARIANT RELATIONS: A CONCEPT FOR ANALYZING WHILE LOOPS. Ali Mili, NJIT NII, Tokyo, Japan December 20, 2011

Transcription:

Proof Calculus for Partial Correctness Bow-Yaw Wang Institute of Information Science Academia Sinica, Taiwan September 7, 2016 Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 1 / 41

Outline 1 Proof calculus for partial correctness Proof rules Proof tableaux A case study: minimal-sum section Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 2 / 41

Proof Calculus for Partial Correctness We will discuss a proof calculus for partial correctness of core programs. It was developed by R. Floyd and C. A. R. Hoare. Similar to proof calculus for propositional or predicate logics, we will give proof rules. We could write formal proofs in proof trees. But we will use a linear presentation called proof tableaux Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 3 / 41

Outline 1 Proof calculus for partial correctness Proof rules Proof tableaux A case study: minimal-sum section Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 4 / 41

Proof Rules Composition For each statement P, we present a proof rule for ( φ )P( ψ ). The proof rule for ( φ )C 0 ;C 1 ( ψ ) is ( φ )C 0 ( η ) ( η )C 1 ( ψ ) ( φ )C 0 ;C 1 ( ψ ) Composition That is, if we want to show ( φ )C 0 ;C 1 ( ψ ), it suffices to find η and show ( φ )C 0 ( η ) and ( η )C 1 ( ψ ). η is called a midcondition. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 5 / 41

Proof Rules I The proof rule for assignment statements is ( ψ[e/x] )x = E( ψ ) That is, if we start from a state satisfying ψ[e/x], we end with a state satisfying ψ after executing x = E. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 6 / 41

Proof Rules II The proof rule may look strange at first. Let us see how it works. Consider the assignment statement x = x + 1 with the postcondition x 1. By the proof rule for assignments, we have ( x + 1 1 )x = x + 1( x 1 ) That is, if we start from a state with x 0, we will arrive at a state with x 1 after executing x = x + 1. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 7 / 41

Proof Rules III One might think the proof rule would be ( φ )x = E( φ[e/x] ) Wrong From a state satisfying φ, we will get a state satisfying φ[e/x] after executing x = E. But this is wrong! Consider the assignment x = 5 with the precondition x = 0. We would have ( x = 0 )x = 5( x[5/x] = 0 ). WRONG Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 8 / 41

Proof Rules IV Let us rethink the proof rule for assignments: ( ψ[e/x] )x = E( ψ ) The right way to understand this proof rule is to think backward. Suppose we have a postcondition ψ after executing x = E. What can make the postcondition hold before x = E? We want all occurrences of x in ψ equal to E after executing x = E. Hence ψ[e/x] should hold before executing x = E. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 9 / 41

Proof Rules V Let P be x = 2. We have 1 ( 2 = 2 )P( x = 2 ). 2 ( 2 = 4 )P( x = 4 ). 3 ( 2 = y )P( x = y ). 4 ( 2 > 0 )P( x > 0 ). Let Q be x = x + 1. We have 1 ( x + 1 = 2 )Q( x = 2 ). 2 ( x + 1 = y )Q( x = y ). 3 ( x + 1 + 5 = y )Q( x + 5 = y ). 4 ( x + 1 > 0 y > 0 )Q( x > 0 y > 0 ). Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 10 / 41

Proof Rules If-Statement The proof rule for if-statements is: ( φ B )C 0 ( ψ ) ( φ B )C 1 ( ψ ) ( φ )if B { C 0 } else { C 1 }( ψ ) If statement That is, if we want to show ( φ )if B { C 0 } else { C 1 }( ψ ), it suffices to show ( φ B )C 0 ( ψ ) and ( φ B )C 1 ( ψ ). Note that φ B and φ B mix a logic formula φ with a Boolean expression B or B. Strictly speaking, such mixed notations need be defined formally. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 11 / 41

Proof Rules Partial-While The proof rule for while-statements is: ( ψ B )C( ψ ) ( ψ )while B { C }( ψ B ) Partial while That is, if we want to show ( ψ )while B { C }( ψ B ), it suffices to show ( ψ B )C( ψ ). Note that B must hold after executing the while-statement. ( ψ B )C( ψ ) in fact asserts that ψ remains unchanged after executing the loop body C. No matter how many times the body C is executed, ψ always holds after each execution. To prove properties about while-statements, the key is to find such an invariant ψ. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 12 / 41

Proof Rules Implied We have an additional proof rule to strengthen (weaken) the precondition (postcondition): AR φ φ ( φ )C( ψ ) AR ψ ψ ( φ )C( ψ ) Implied where AR η is valid if there is a natural deduction proof for η in standard laws of arithmetic. This proof rule is not really about program statements. Rather, it links program logic (Hoare triples) with predicate logic ( AR ). Particularly, the implied proof rule helps us simplify pre- and post-conditions. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 13 / 41

Proof Rules Summary ( φ )C 0 ( η ) ( η )C 1 ( ψ ) ( φ )C 0 ;C 1 ( ψ ) Composition ( ψ[e/x] )x = E( ψ ) ( φ B )C 0 ( ψ ) ( φ B )C 1 ( ψ ) ( φ )if B { C 0 } else { C 1 }( ψ ) If statement ( ψ B )C( ψ ) ( ψ )while B { C }( ψ B ) Partial while AR φ φ ( φ )C( ψ ) AR ψ ψ ( φ )C( ψ ) Implied Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 14 / 41

Outline 1 Proof calculus for partial correctness Proof rules Proof tableaux A case study: minimal-sum section Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 15 / 41

A Proof Tree Using proof rules, we can formally prove that par ( )Fac1( y = x! ) is valid. Here is the proof tree: ( 1 = 1 )y = 1( y = 1 ) ( )y = 1( y = 1 ) ( y = 1 0 = 0 )z = 0( y = 1 z = 0 ) ( y = 1 )z = 0( y = 1 z = 0 ) ( )y = 1; z = 0( y = 1 z = 0 ) ( y (z + 1) = (z + 1)! )z = z + 1( y z = z! ) ( y = z! z x )z = z + 1( y z = z! ) ( y z = z! )y = y z( y = z! ) ( y = z! z x )z = z + 1; y = y z( y = z! ) ( y = z! )while(z! = x){z = z + 1; y = y z}( y = z! z = x ) ( y = 1 z = 0 )while(z! = x){z = z + 1; y = y z}( y = x! ) ( )y = 1; z = 0; while(z! = x){z = z + 1; y = y z}( y = x! ) Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 16 / 41

Proof Tableaux I Similar to proof systems for propositional and predicate logic, we will use a linear presentation of proof trees called proof tableaux. Let P = C 1 ; C 2 ; ; C n be a program. Suppose we would like to show par ( φ 0 )P( φ n ) is valid. We will write its proof as follows. ( φ 0 ) C 1 ( φ 1 ) ( φ n 1 ) justification justification C n ( φ n ) where φ 1, φ 2,..., φ n 1 are midconditions. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 17 / 41

Proof Tableaux II Recall the proof rule for assignments: ( ψ[e/x] )x = E( ψ ) Since it is easier to compute preconditions from postconditions for assignments, we often start with φ n and work backward until φ 0. Ideally, we should compute the weakest precondition φ i 1 from φ i for each i. A logical formula ψ is weaker than φ if φ ψ holds; similarly, we say φ is stronger than ψ. That is, we would like to compute the weakest condition φ i 1 to guarantee φ i after execution C i. When we get to φ 0, we can then apply the Implied proof rule to strengthen φ 0 if needed. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 18 / 41

Proof Tableaux Let us begin with a proof tableau for assignments. ( ψ[e/x] ) x = E ( ψ ) Proof tableaux are read from top. We obtain the postcondition ψ by the proof rule. Hence the justification for ψ is. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 19 / 41

Proof Tableaux Implied A proof tableau for the Implied proof rule links predicate logic with arithmetic with program logic. C ( φ ) ( φ ) ( ψ ) ( ψ ) Implied Implied provided AR φ φ and AR ψ ψ. We do not give a formal proof of AR φ φ nor of AR ψ ψ in proof tableaux. Oftentimes such proofs are simple. You should know how to give their formal proofs by now. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 20 / 41

Examples I Example Show par ( y = 5 )x = y + 1( x = 6 ) is valid. Proof. ( y = 5 ) ( y + 1 = 6 ) x = y + 1 ( x = 6 ) Implied Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 21 / 41

Examples II Example Show par ( y < 3 )y = y + 1( y < 4 ). Proof. ( y < 3 ) ( y + 1 < 4 ) y = y + 1 ( y < 4 ) Implied Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 22 / 41

Examples III Example Let P be z = x; z = z + y; u = z. Show par ( )P( u = x + y ). Proof. ( ) ( x + y = x + y ) z = x; ( z + y = x + y ) z = z + y; ( z = x + y ) u = z ( u = x + y ) Implied Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 23 / 41

Non-Examples What is wrong in the following proof? ( ) ( x + 1 = x + 1 ) x = x + 1 ( x = x + 1 ) Implied The proof rule replaces all occurrences of x by x + 1. We have ( x + 1 = x + 1 + 1 )x = x + 1( x = x + 1 ). What is wrong in the following proof? ( x + 2 = y + 1 ) y = y + 10001; x = x + 2 ( x = y + 1 ) The proof rule must apply to every assignment statement. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 24 / 41

Proof Tableaux If Given ψ, consider a Hoare triple for if-statement: ( φ )if B { C 0 } else { C 1 }( ψ ) How do we compute the weakest φ to guarantee ψ? φ is computed as follows. Compute φ 0 such that ( φ 0 )C 0 ( ψ ); Compute φ1 such that ( φ 1 )C 1 ( ψ ); Define φ as (B φ0 ) ( B φ 1 ). Observe that AR (φ B) φ 0 and AR (φ B) φ 1. Hence ( φ 0 )C 0 ( ψ ) ( φ B )C 0 ( ψ ) Implied ( φ 1 )C 1 ( ψ ) ( φ B )C 1 ( ψ ) Implied If statement ( φ )if B { C 0 } else { C 1 }( ψ ) Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 25 / 41

Example I Example Let Succ be a = x + 1; if (a - 1 == 0) { y = 1 } else { y = a } Show par ( )Succ( y = x + 1 ). Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 26 / 41

Example II Proof. ( ) ( (x + 1 1 = 0 1 = x + 1) ( (x + 1 1 = 0) x + 1 = x + 1) ) Implied a = x + 1; ( (a 1 = 0 1 = x + 1) ( (a 1 = 0) a = x + 1) ) if (a 1 == 0) { ( 1 = x + 1 ) If-statement y = 1 ( y = x + 1 ) } else { ( a = x + 1 ) If-statement y = a ( y = x + 1 ) } ( y = x + 1 ) If-statement Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 27 / 41

Proof Tableaux While I Recall the following proof rule: ( η B )C( η ) ( η )while B { C }( η B ) Partial while Suppose the while statement terminates from a state satisfying η and ( η B )C( η ). If B is false at the start of the while statement, the statement is never executed. We end up a state satisfying η B. If B is true at the start of the while statement, we execute C from a state satisfying η B. Since ( η B )C( η ), η is true after executing C. If B is now false, we stop with η B; If B is still true, we execute C from a state satisfying η B again and have η after executing C. The while statement terminates iff B becomes false after executing C finitely many times. Hence we have η B when the statement terminates. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 28 / 41

Proof Tableaux While II More generally, suppose we are asked to show ( φ )while B { C }( ψ ). How do we proceed? We apply the Partial-while and Implied proof rules. AR φ η ( η B )C( η ) ( η )while B { C }( η B ) AR η B ψ ( φ )while B { C }( ψ ) The key is to find a proper invariant η! Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 29 / 41

Proof Tableaux While III Definition An invariant of while B { C } is a formula η that par ( η B )C( η ) holds. A while statement have many invariants. For instance, and are trivial invariants for any while statement. We are looking for an invariant η that establishes the precondition φ and postcondition ψ. That is, we must have AR φ η and AR η B ψ. But how do we find such an invariant? we can examine program traces carefully. is it possible to find invariants automatically? Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 30 / 41

Example I Example Recall the program Fac1: y = 1; z = 0; while (z!= x) { z = z + 1; y = y * z; } Show par ( )Fac1( y = x! ) is valid. What is an invariant η such that 1 AR y = 1 z = 0 η; 2 AR η z = x y = x!; and 3 ( η (z = x) )z = z + 1; y = y z( η )? Take η to be y = z!. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 31 / 41

Example II Proof. ( ) ( 1 = 0! ) Implied y = 1; ( y = 0! ) z = 0; ( y = z! ) while (z! = x) { ( y = z! (z = x) ) ( y (z + 1) = (z + 1)! ) Implied z = z + 1; ( y z = z! ) y = y z; ( y = z! ) } ( y = z! z = x ) Partial-while ( y = x! ) Implied Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 32 / 41

Outline 1 Proof calculus for partial correctness Proof rules Proof tableaux A case study: minimal-sum section Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 33 / 41

Minimal-Sum Sections of Arrays Let int a[n] declare an integer array with elements a[0], a[1],..., a[n 1]. Definition Let a be an array with elements a[0],..., a[n 1]. A section of a consists of elements a[i],..., a[j] for some 0 i j < n. We write S i,j for a[i] + a[i + 1] + + a[j] (the sum of the section). A minimal-sum section is a section a[i],..., a[j] of a such that S i,j is less than or equal to S i,j for every section a[i ],..., a[j ]. Example: consider the array [ 1, 3, 15, 6, 4, 5]. [3, 15, 6] and [ 6] are sections but [ 1, 15, 6] isn t. A minimal-sum section of the array is [ 6, 4, 5]. Minimal-sum sections are in general not unique. We will write a program that computes a minimal-sum section for any array and verify the program with our proof calculus. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 34 / 41

Solutions to Minimal-Sum Sections I A simple solution to minimal-sum sections is by enumeration. We enumerate all possible sections and evaluate their sums. There are O(n 2 ) sections. It takes O(n) to evaluate the sum of a section. We need O(n 3 ) to solve the problem. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 35 / 41

Solutions to Minimal-Sum Sections II Here is a better program MinSum: k = 1; t = a [0]; s = a [0]; while (k!= n) { t = min (t + a[k], a[k ]); s = min (s, t); k = k + 1; } The variable s stores the minimal-sum of all sections in the subarray a[0 k]. The variable t stores the minimal-sum of all sections in the subarray a[0 k] ending at a[k]. We will prove its correctness and get some insights from our proof. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 36 / 41

Specifications of MinSum In order to prove its correctness, let us specify properties about MinSum by Hoare triples. We want to show that the variable s will be the minimal sum after execution. We first specify s is less than or equal to the sum of any section. S1. ( )MinSum( i j(0 i j < n s S i,j ) ) i and j are logical variables. Next, we specify s must be the sum of some section. S2. ( )MinSum( i j(0 i j < n s = S i,j ) ) We will show S1 must hold after executing MinSum. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 37 / 41

How to Find Invariants? We need to come up with an invariant for the while statement. This often requires creativity. Some guidelines would not hurt. Here are some characteristics about invariants that may help us find invariants from the textbook: Invariants express the fact that the computation performed so far by the while-statement is correct. Invariants typically have the same form as the desired postcondition of the while-statement. Invariants express relationships between the variables manipulated by the while-statement which are re-established each time the body of the while-statement is executed. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 38 / 41

par ( )MinSum( S1 ) I Let us show par ( )MinSum( S1 ). Consider the invariant: Inv1(s, k) = i j(0 i j < k s S i,j ). If we tried to prove MinSum with the invariant, we would find the invariant is not strong enough. We simply ignore the variable t. Intuitively, this cannot be right. Consider another invariant: Inv2(t, k) = i(0 i < k t S i,k 1 ). We use Inv1(s, k) Inv2(t, k) as an invariant to prove MinSum. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 39 / 41

par ( )MinSum( S1 ) II Here is the proof: ( ) ( Inv1(a[0], 1) Inv2(a[0], 1) ) k = 1; ( Inv1(a[0], k) Inv2(a[0], k) ) t = a[0]; ( Inv1(a[0], k) Inv2(t, k) ) s = a[0]; ( Inv1(s, k) Inv2(t, k) ) while (k! = n) { ( Inv1(s, k) Inv2(t, k) k n ) ( Inv1(min(s, min(t + a[k], a[k])), k + 1) Inv2(min(t + a[k], a[k]), k + 1) ) t = min(t + a[k], a[k]); ( Inv1(min(s, t), k + 1) Inv2(t, k + 1) ) s = min(s, t); ( Inv1(s, k + 1) Inv2(t, k + 1) ) k = k + 1 ( Inv1(s, k) Inv2(t, k) ) } ( Inv1(s, k) Inv2(t, k) k = n ) ( Inv1(s, n) ) Implied Implied Partial-while Implied Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 40 / 41

par ( )MinSum( S1 ) III Lemma Let s, t Z, a an array of size n 0, and 0 < k < n. Then Inv1(s, k) Inv2(t, k) k n implies 1 Inv2(min(t + a[k], a[k]), k + 1); and 2 Inv1(min(s, min(t + a[k], a[k])), k + 1). Proof. 1 Let 0 i < k + 1, we want to show min(t + a[k], a[k]) S i,k. If i < k, S i,k = S i,k 1 + a[k]. We have min(t + a[k], a[k]) S i,k 1 + a[k] for t S i,k 1 from Inv2(t, k). If i = k, S i,k = a[k]. Clearly, min(t + a[k], a[k]) S i,k = a[k]. 2 Let 0 i j < k + 1. We show min(s, min(t + a[k], a[k])) S i,j. If i j < k, we have s S i,j for Inv1(s, k). Clearly, min(s, min(t + a[k], a[k])) S i,j. If i j = k, we have min(t + a[k], a[k]) S i,k from the previous case. Then min(s, min(t + a[k], a[k])) S i,j. Bow-Yaw Wang (Academia Sinica) Proof Calculus for Partial Correctness September 7, 2016 41 / 41