PZ03D - Program verification

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03D - Program verification Programming Language Design.
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.
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
PZ08A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ08A - Prime programs Programming Language Design and.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
Partial correctness © Marcelo d’Amorim 2010.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
Duminda WijesekeraSWSE Program Correctness1 SWSE 623 Program Correctness -Pre-condition, Post-conditions and Loop invariants.
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.
CS 355 – Programming Languages
PZ12A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12A - Guarded commands Programming Language Design.
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.
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.
4/17/2017 Section 3.6 Program Correctness ch3.6.
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 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
CS 363 Comparative Programming Languages Semantics.
Recursive Algorithms &
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.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
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.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX - Recursive descent parsing Programming Language.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
11/22/2016IT 3271 A formal system:Axioms and Rules, for inferring valid specification x := m; y := n; while ¬(x=y) do if x>y then x := x-y else y := y-x.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
PZ11A Programming Language design and Implementation -4th Edition
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Spring 2017 Program Analysis and Verification
Reasoning About Code.
Formal Methods in Software Engineering 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
This Lecture Substitution model
Logic for Computer Security Protocols
PZ08A - Prime programs Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section PZ08A.
Semantics In Text: Chapter 3.
Predicate Transformers
Formal Methods in software development
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
PZ03D - Program verification
This Lecture Substitution model
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Program Verification with Hoare Logic
This Lecture Substitution model
Lecture 2: Axiomatic semantics
CIS 720 Lecture 3.
Programming Languages and Compilers (CS 421)
CIS 720 Lecture 3.
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
PZ03BX - Recursive descent parsing
Program Correctness an introduction.
Presentation transcript:

PZ03D - Program verification Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 4.2.4 PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Formal Specifications Every program can be represented by a flowchart: Fcn3 S1 Fcn2 P3 S4 P1 S3 Fcn4 P2 Fcn1 S2 Signatures: function: domain  range fun1: S1 and P1  S3 fun2: S1 and not(P1)  S3 fun3: S3 and not(P3)  S4 fun4: S3 and P3  S4 S2 and P2 = S4 S2 and not(P2) = S3 PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Formal specifications (continued) A program is then some complex function C of its primitive components: C(fun1, fun2, fun3, fun4, p1, p2, p3):S1S4 If we could derive these relationships formally, then we have a mathematical proof that given S1 we would HAVE to end with S4. Problem: Such proofs are hard. Note, however, that we are always showing the equivalence between a program and a given specification. The statement “Is this program correct?” makes no sense if the specification is not also given. PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Axiomatic verification Tony Hoare developed method in 1969. Each program statement obeys a formal axiomatic definition. Validation: Demonstrating that a program meets its specifications by testing the program against a series of data sets Verification: Demonstrating that a program meets its specifications by a formal argument based upon the structure of the program. Validation assumes an execution of the program; verification generally does not. PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Axioms for predicate logic {P1}S{P2} means that if P1 is true and S executes, then if S terminates, P2 will be true. A B if A is true then you can state that B is true. Axioms of logic: Composition {P}S1{Q},{Q}S2{R} [Combine statements] {P}S1;S2{R} Consequence1 {P}S{R}, RQ [Add postcondition] {P}S{Q} Consequence2 PR, {R}S{Q} [Add precondition] PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Axioms for statement types Conditional1 {P^B}S{Q}, P^not(B)Q {P}if B then S{Q} Conditional2 {P^B}S1{Q},{P^not(B)S2{Q} {P}if B then S1 else S2{Q} While {P^B}S{P} {P}while B do S{P ^ not(B)} where P is an invariant Assignment {P(e)}x:=e{P(x)} where P(x) is some predicate in x. PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Axiomatic program proof Given program: s1; s2; s3; s4; … sn and specifications P and Q: P is the precondition Q is the postcondition Show that {P}s1;…sn{Q} by showing: {P}s1{p1} {p1}s2{p2} … {pn-1}sn{Q} Repeated applications of composition yield: {P}s1; …; sn{Q} PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Example proof {B>=0} 1 X:=B 2 Y:=0 3 while X>0 do 4 begin 5 Y:= Y+A 6 X:= X-1 7 end {Y=AB} That is, given B non-negative, show that when the program halts, Y=A*B. PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Proof outline General method is to work backwards. Look for loop invariant first: Y is part of partial product, and X is count of what remains So Y and X*A should be the product needed, i.e., invariant Y+X*A = A*B is proposed invariant Try: (Y+X*A=A*B and X³0) PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Proof Axiom of assignment(6): {(Y+(X-1)A=A*B and X-1>=0)} X:=X-1 {(Y+X*A=A*B and X>=0)} Axiom of assignment(5): {((Y+A)+(X-1)A=A*B and X-1>=0)} Y:=Y+A {(Y+(X-1)*A=A*B and X-1>=0)} Axiom of composition(5,6): {(Y+A)+(X-1)*A=A*B and X-1>=0)} Y:=Y+A; X:=X-1 {(Y+X*A=A*B and X>=0)} Mathematical Theorem: Y+X*A=A*B and X>0  ((Y+A)+(X-1)*A=A*B and X-1>=0) Axiom of consequence: {Y+X*A=A*B and X>0} Y:=Y+A; X:=X-1 {(Y+X*A=A*B and X>=0)} Mathematical theorem: (Y+X*A=A*B and X>=0) and (X>0)  Y+X*A=A*B and X>0 Axiom of consequence: {(Y+X*A=A*B and X>=0)and (X>0)} Y:=Y+A; X:=X-1 {(Y+X*A=A*B and X>=0)} [Call this **] While axiom: ** {Y+X*A=A*B and X>=0}while X>0 do Y:=Y+A; X:=X-1 {(Y+X*A=A*B and X>=0) and not(X>0)} PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Proof (continued) Mathematical theorem: (Y+X*A=A*B and X>=0) and not(X>0) (Y+X*A=A*B and X=0) Y=AB Axiom of consequence: {Y+X*A=A*B and X>=0}while X>0 do Y:=Y+A; X:=X-1 {Y+A*B} Axiom of assignment: {0+X*A=A*B and X>=0} Y:=0 {Y+X*A=A*B and X>=0} Axiom of assignment: {0+B*A=A*B and B>=0} X:=B {0+X*A=A*B and X>=0)} Axiom of composition: {0+B*A=A*B and B>=0} X:=B; Y:=0 {Y+X*A=A*B and X>=0)} Mathematical theorem: (B>=0)  0+B*A=A*B and B>=0 Axiom of consequence: {B>=0} X:=B; Y:=0{Y+X*A=A*B and X>=0)} Composition: {B>=0} program {Y=A*B} PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Summary – Axiomatic verification Need to develop axioms for all language features. Can do it for simple arrays, procedure calls, parameters Difficult, even on small programs; very hard to scale up to larger programs Does not translate well to non-mathematical problems; extremely expensive to implement Hard to automate. Manual process leads to many errors. But Precise; clearly delineates the “what” with the “how” Basis for most other verification methods, including semiformal specification notations. For critical applications, it may be worth the cost. PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000