Output Variables {true} S {i = j} i := j; or j := i;

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.
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
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.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
Weakest pre-conditions and towards machine consistency Saima Zareen.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Introduction to Data Structures and Algorithms Chapter 7 Quick Sort.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
© Love Ekenberg The Algorithm Concept, Big O Notation, and Program Verification Love Ekenberg.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Analyzing the Requirements with Formal Specifications Vienna Development Method Specification Language (VDM-SL) Book: Formal Software Development From.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
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.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
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.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Fortran: Control Structures Session Three ICoCSIS.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Chapter 6: User-Defined Functions I
REPETITION CONTROL STRUCTURE
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Correctness issues and Loop invariants
Reasoning with invariants
GC211Data Structure Lecture2 Sara Alhajjam.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Algorithm Analysis CSE 2011 Winter September 2018.
User-Defined Functions
Computation.
Copyright © Cengage Learning. All rights reserved.
Axiomatic semantics Points to discuss: The assignment statement
Chapter 7 Additional Control Structures
Programming Languages 2nd edition Tucker and Noonan
Types of Flow of Control
Semantics In Text: Chapter 3.
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Some heuristics for deriving invariant LN sections 6.7 , 6.8
3.1 Iteration Loops For … To … Next 18/01/2019.
Chapter 6: User-Defined Functions I
Asymptotic complexity Searching/sorting
Predicate Transformers
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Loop Construct.
Binary Search and Loop invariants
Application: Algorithms
Asymptotic complexity
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Matlab Basics.
Chap 7. Advanced Control Statements in Java
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Review of Previous Lesson
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Searching and Sorting Hint at Asymptotic Complexity
Program Correctness an introduction.
Presentation transcript:

Output Variables {true} S {i = j} i := j; or j := i; Which one is the input variable and which one is the output variable?

Ghost Variables Suppose we want to specify that the sum of two variables i and j should remain constant We specify this by introducing a ghost variable C This variable should not be used anywhere else in the program. Then S specified by { i + j = C} S { i + j = C}

Simultaneous Assignment The left side is a list of variables and the right side is a list of expressions of the same length as the list of variables. x, y, z := 2*y, x+y, 3*z

Simultaneous Assignment The assignment x, y := y, x has the effect of swapping the values of x and y

Calculating Assignments Suppose that the requirement is to maintain the value of j + k constant while incrementing k by 1 Our task is to calculate and expression X such that {j + k = C} j , k := X, k + 1 { j + k = C} Applying the assignment axiom, we get {X + k + 1 = C} j , k := X, k + 1 { j + k = C} j+k = C  X + k + 1 = C

Now j + k = j + k + 1 – 1 = (j - 1) + k + 1 Thus a suitable value of X is j – 1 So, { j + k = C} j , k := j – 1, k + 1 { j + k = C}

Suppose variables s and n satisfy the property s = n2 We want to increment n by 1 whilst maintaining this relationship between s and n Our goal is to calculate an expression X involving only addition such that { s = n2 } s, n := s + X, n+1 { s = n2 } Applying the assignment axiom we get { s + X = (n + 1)2 } s, n := s + X, n + 1 { s = n2 }

So, we need s = n2  s + X = (n + 1)2 Now (n + 1)2 = n2 + 2*n + 1 That is s = n2  s + 2*n + 1 = (n + 1)2 So, { s = n2 } s, n := s + 2*n + 1, n+1 { s = n2 }

Max of two numbers two number x and y pre-condition: {true} max(x,y) = x  y  x max(x,y) = y  x  y pre-condition: {true} post-condition: {z = max(x,y)} two cases: (x  y) and (y  x) {true}  (x  y)  (y  x)

Max of two numbers {true} if x  y  z := e1 {z = max(x,y)} [] y  x  z := e2 {z = max(x,y)} fi {z = max(x,y)}

Max of two numbers using the assignment axiom we calculate that {e = max(x,y)} z := e {z = max(x,y)} in particular {x = max(x,y)} z := x {z = max(x,y)} that is {y  x} z := x {z = max(x,y)} and {x  y} z := y {z = max(x,y)} we have thus determined e1 and e2

Iteration The do-od statement do b  S od

Constructing Loops Invariant property and bound function Loops are designed so that each iteration of the loop body maintains the invariant whilst making progress to the required post-condition by always decreasing the bound function.

Constructing Loops Suppose a problem is specified by precondition P and post-condition Q. We identify and invariant property inv and the bound function bf.

Constructing Loops The bound function is an integer-value function of the program variables and is a measure of the size of the problem to be solved. It is guaranteed to be greater than zero when loop is executed. A guarantee that the value of such a bound function is always decreased at each iteration is a guarantee that the loop will terminate.

Constructing Loops The post condition Q is split into a termination condition, say done, and the invariant property inv, in such a way that inv  done  Q The invariant property is designed, in combination with the termination condition, by generalizing the required post-condition. The termination condition is typically related to the bound function.

Constructing Loops The invariant should also guarantee that the value of the bound function is greater than zero unless the loop has terminated. That is: inv  (bf > 0)  done The invariant property is chosen so that it is easy to design an initialization statement, S, that establishes the invariant property {P} S {inv}

Constructing Loops The design is completed by constructing a loop body T that maintains the invariant whilst making progress towards the termination condition. {inv  done  bf = C} T {inv  (done  bf < C)}

Constructing loops If the termination condition, done, the bound function, bf, the invariant, inv, and the loop body, T, have all been constructed as above, then we have {inv} do done  T od {Q} Moreover, if S has been constructed then {P} S; do done  T od {Q}