Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Semantics Static semantics Dynamic semantics attribute grammars
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
(c) 2007 Mauro Pezzè & Michal Young Ch 7, slide 1 Symbolic Execution and Proof of Properties.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
Predicate Transformers
Program Proving Notes Ellen L. Walker.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
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.
CS 355 – Programming Languages
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
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.
Axiomatic Semantics Dr. M Al-Mulhem ICS
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Software Quality: Testing and Verification II. 2 1.A failure is an unacceptable behaviour exhibited by a system — The frequency of failures measures software.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Describing Syntax and Semantics
Proving Program Correctness The Axiomatic Approach.
Proving Program Correctness The Axiomatic Approach.
Reading and Writing Mathematical Proofs
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
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.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Chapter 3 Part II Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
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.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
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.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Programming Languages 2nd edition Tucker and Noonan
Formal Methods in Software Engineering 1
Mathematical Structures for Computer Science Chapter 1
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Predicate Transformers
Program correctness Axiomatic semantics
CIS 720 Lecture 3.
Programming Languages and Compilers (CS 421)
CIS 720 Lecture 3.
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Program Correctness an introduction.
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming scientifically, it must be possible to specify the required properties of programs precisely. Formality is certainly not an end in itself. The importance of formal specifications must ultimately rest in their utility - in whether or not they are used to improve the quality of software or to reduce the cost of producing and maintaining software. J. Horning

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 18.1 Axiomatic Semantics Fundamental Concepts The Assignment Rule Rules of Consequence Correctness of the Max Function Correctness of Programs with Loops 18.2 Formal Methods Tools: JML 18.3 Correctness of Object-Oriented Programs 18.4 Correctness of Functional Programs

Copyright © 2006 The McGraw-Hill Companies, Inc. Motivation A correct program is one that does exactly what it is intended to do, no more and no less. A formally correct program is one whose correctness can be proved mathematically. –This requires a language for specifying precisely what the program is intended to do. –Specification languages are based in mathematical logic. –Hoare invented “axiomatic semantics” in 1969 as a tool for specifying program behavior and proving correctness. Until recently, correctness has been an academic exercise. –Now it is a key element of critical software systems.

Copyright © 2006 The McGraw-Hill Companies, Inc. Correctness Tools Theorem provers –PVS Modeling languages –UML and OCL Specification languages –JML Programming language support –Eiffel –Java –Spark/Ada Specification Methodology –Design by contract

Copyright © 2006 The McGraw-Hill Companies, Inc Axiomatic Semantics Axiomatic semantics is a language for specifying what a program is supposed to do. Based on the idea of an assertion: –An assertion is a predicate that describes the state of a program at a point in its execution. A postcondition is an assertion that states the program’s result. A precondition is an assertion that states what must be true before the program begins running.

Copyright © 2006 The McGraw-Hill Companies, Inc. A “Hoare Triple” has the form {P}s{Q} {true} int Max (int a, int b) { int m; if (a >= b) m = a; else m = b; fi return m; } {m = max(a, b)} Precondition P: there are no constraints on the input for this particular function. Postcondition Q: max is the mathematical idea of a maximum. Program body s

Copyright © 2006 The McGraw-Hill Companies, Inc. Partial correctness There is no guarantee that a program will terminate normally. That is, for some inputs, It may enter an infinite loop, or It may fail to complete its calculations. E.g., consider a C-like factorial function n! whose parameter n and result are int values. Passing 21 as an argument should return n! = But that value cannot be stored as an int, so the function fails. A program s is partially correct for pre- and postconditions P and Q if, whenever s begins in a state that satisfies P, it terminates and computes a result that satisfies Q.

Copyright © 2006 The McGraw-Hill Companies, Inc. Proving Partial Correctness Program s is partially correct for pre- and postconditions P and Q if the Hoare triple {P}s{Q} is valid. There are seven rules of inference that can be used to prove the validity of {P}s{Q}: 1.The Assignment Rule 2.The Sequence Rule 3.The Skip Rule 4.The Conditional Rule 5.The Loop Rule 6.Precondition Consequence Rule 7.Postcondition Consequence Rule Used for basic statement types Used to simplify intermediate triples

Copyright © 2006 The McGraw-Hill Companies, Inc. Proof Methodology 1.A proof is naturally represented as a proof tree. 2.The proof starts with this triple {P}s{Q}. E.g., {true} if (a >= b) m = a; else m = b; fi {m = max(a, b)} 3.An inference is written as, and means “if p and q are valid, then r is inferred to be valid.” 4.Using appropriate rules of inference, break the triple into a group of inferences in which: 1.Each triple is individually valid, and 2.The inferences (logically) combine to form a tree whose root is the program’s original Hoare triple.

Copyright © 2006 The McGraw-Hill Companies, Inc. The Assignment Rule If Q is a postcondition of an assignment, then replacing all occurrences of t in Q by e is a valid precondition. Examples:

Copyright © 2006 The McGraw-Hill Companies, Inc. The Conditional Rule We can infer this when we reason backwards from here. E.g.,

Copyright © 2006 The McGraw-Hill Companies, Inc. Rules of Consequence E.g., Precondition strengthening: Postcondition weakening:

Copyright © 2006 The McGraw-Hill Companies, Inc. Correctness of the Max Function Note: the assignment rule, the precondition strengthening rule, and the conditional rule are all used in this proof.

Copyright © 2006 The McGraw-Hill Companies, Inc. The Sequence Rule Here, the challenge is to find an R that will allow us to break a triple into two valid triples. Notes: 1.The second triple above the line is valid because of the assignment rule. 2.The first triple is valid because both and are valid, using precondition strengthening and the assignment rule. E.g.,

Copyright © 2006 The McGraw-Hill Companies, Inc. The Loop Rule R is called the loop invariant. The loop invariant remains true before and after each iteration. E.g., in is a good choice for a loop invariant. Note: when the test i < n finally becomes false, the only value for i that satisfies R is i = n, justifying the postcondition {f = n!}

Copyright © 2006 The McGraw-Hill Companies, Inc. Correctness of Programs with Loops Consider the following triple for a factorial calculation: int f = 1; int i = 1; while (i < n) i = i + 1; f = f * i; od Below is a sketch of its correctness proof, as two proof trees: –part 1 for for the first two statements, and –part 2 for the loop.

Copyright © 2006 The McGraw-Hill Companies, Inc. Proof tree for factorial (part 1 of 2) This part uses precondition strengthening, the assignment rule, and the sequence rule. It also establishes the loop invariant as:

Copyright © 2006 The McGraw-Hill Companies, Inc. Proof tree for factorial (part 2 of 2) Notes: 1.The left-hand premise was proved earlier. 2.The right-hand premise is true when the loop terminates. Its validity can be shown mathematically. I.e., if i  n and  (i < n) then i = n. But if f = i! and i = n, then f = n! 3.The conclusion follows from the loop rule.

Copyright © 2006 The McGraw-Hill Companies, Inc. Perspectives on Formal Methods Theory developed in the ‘60s and ‘70s Effectively used to verify hardware design Not widely used in software design, but –New tools are emerging (e.g., JML, Spark/Ada) –Techniques have been effective in some critical systems (e.g., the Paris metro system). Many software designers reject formal methods: –Too complex for programmers –Time better spent with alternative testing methods. Maybe there’s a middle ground?