Programming Languages 2nd edition Tucker and Noonan

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
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
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.
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
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
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.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
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.
4/17/2017 Section 3.6 Program Correctness ch3.6.
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.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
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)
Semantics In Text: Chapter 3.
Predicate Transformers
The Zoo of Software Security Techniques
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:

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

Contents 18.1 Axiomatic Semantics 18.1.1 Fundamental Concepts 18.1.2 The Assignment Rule 18.1.3 Rules of Consequence 18.1.4 Correctness of the Max Function 18.1.5 Correctness of Programs with Loops 18.2 Formal Methods Tools: JML 18.3 Correctness of Object-Oriented Programs 18.4 Correctness of Functional Programs

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.

Correctness Tools Theorem provers Modeling languages PVS Modeling languages UML and OCL Specification languages JML Programming language support Eiffel Java Spark/Ada Specification Methodology Design by contract

18.1 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.

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

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! = 51090942171709440000. 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.

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}: The Assignment Rule The Sequence Rule The Skip Rule The Conditional Rule The Loop Rule Precondition Consequence Rule Postcondition Consequence Rule Used for basic statement types Used to simplify intermediate triples

Proof Methodology A proof is naturally represented as a proof tree. 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)} An inference is written as , and means “if p and q are valid, then r is inferred to be valid.” Using appropriate rules of inference, break the triple into a group of inferences in which: Each triple is individually valid, and The inferences (logically) combine to form a tree whose root is the program’s original Hoare triple.

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:

The Conditional Rule We can infer this when we reason backwards from here. E.g.,

Rules of Consequence Precondition strengthening: Postcondition weakening: E.g.,

Correctness of the Max Function Note: the assignment rule, the precondition strengthening rule, and the conditional rule are all used in this proof.

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

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!}

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.

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:

Proof tree for factorial (part 2 of 2) Notes: The left-hand premise was proved earlier. 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! The conclusion follows from the loop rule.

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?