Lecture 2: Axiomatic semantics

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
Copyright , Doron Peled and Cesare Tinelli. These notes are based on a set of lecture notes originally developed by Doron Peled at the University.
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
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
Chair of Software Engineering The alias calculus Bertrand Meyer ITMO Software Engineering Seminar June 2011.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
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.
Predicate Transformers
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
Design by Contract. Specifications Correctness formula (Hoare triple) {P} A {Q} – A is some operation (for example, a routine body) – P and Q are predicates.
Axiomatic Semantics Dr. M Al-Mulhem ICS
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 Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Describing Syntax and Semantics
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Proving Program Correctness The Axiomatic Approach.
Reading and Writing Mathematical Proofs
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.
1 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
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.
CS6133 Software Specification and Verification
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 8: Control Structures I.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
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.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Reasoning About Code.
Lecture 8: Control Structures
Formal Methods in Software Engineering 1
Lecture 5 Floyd-Hoare Style Verification
Lecture 2: Axiomatic semantics
Axiomatic semantics Points to discuss: The assignment statement
CSE 311 Foundations of Computing I
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
CSE 311: Foundations of Computing
Denotational Semantics (Denotational Semantics)
Logic for Computer Security Protocols
Semantics In Text: Chapter 3.
Complement to lecture 11 : Levenshtein distance algorithm
Negations of quantifiers
Axiomatic Verification I
Predicate Transformers
Formal Methods in software development
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Axiomatic Verification I
This Lecture Substitution model
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Lecture 2: Axiomatic semantics

Program Verification: the very idea P: a program S: a specification max (a, b: INTEGER): INTEGER do if a > b then Result := a else Result := b end require true ensure Result >= a Result >= b Does P ⊧ S hold? The Program Verification problem: Given: a program P and a specification S Determine: if every execution of P, for every value of input parameters, satisfies S

What is a theory? (Think of any mathematical example, e.g. elementary arithmetic) A theory is a mathematical framework for proving properties about a certain object domain Such properties are called theorems Components of a theory: Grammar (e.g. BNF), defines well-formed formulae (WFF) Axioms: formulae asserted to be theorems Inference rules: ways to derive new theorems from previously obtained theorems, which can be applied mechanically

Soundness and completeness How do we know that an axiomatic semantics (or logic) is “right”? Sound: every theorem (i.e., deduced property) is a true formula Complete: every true formula can be established as a theorem (i.e., by applying the inference rules). Decidable: there exists an effective (terminating) process to establish whether an arbitrary formula is a theorem.

Notation Let f be a well-formed formula Then ⊢ f expresses that f is a theorem

Inference rule An inference rule is written f1, f2, …, fn ___________ It expresses that if f1, f2, … fn are theorems, we may infer f0 as another theorem

Example inference rule “Modus Ponens” (common to many theories): p, p  q ________ q

How to obtain theorems Theorems are obtained from the axioms by zero or more* applications of the inference rules. *Finite of course

Example: a simple theory of integers Grammar: Well-Formed Formulae are boolean expressions i1 = i2 i1 < i2 ¬ b1 b1 ⇒ b2 where b1 and b2 are boolean expressions, i1 and i2 integer expressions An integer expression is one of A variable n f’ where f is an integer expression (represents “successor”)

An axiom and axiom schema ⊢ 0 < 0’ ⊢ f < g ⇒ f’ < g’

An inference rule P (0), P (f)  P (f’) ________________ P (f)

Axiomatic semantics Floyd (1967), Hoare (1969), Dijkstra (1978) Purpose: Describe the effect of programs through a theory of the underlying programming language, allowing proofs

The theories of interest Grammar: a well-formed formula is a “Hoare triple” {P} A {Q} Instructions Informal meaning: A, started in any state satisfying P, will satisfy Q on termination Assertions

Software correctness (a quiz) Consider {P } A {Q } Take this as a job ad in the classifieds Should a lazy employment candidate hope for a weak or strong P ? What about Q ? Two “special offers”: 1. {False} A {...} 2. {...} A {True}

Axiomatic semantics “Hoare semantics” or “Hoare logic”: a theory describing the partial correctness of programs, plus termination rules

What is an assertion? Predicate (boolean-valued function) on the set of computation states s True False State True: Function that yields True for all states False: Function that yields False for all states P implies Q: means  s : State, P (s )  Q (s ) and so on for other boolean operators

Another view of assertions We may equivalently view an assertion P as a subset of the set of states (the subset where the assertion yields True): True P State True: Full State set False: Empty subset implies: subset (inclusion) relation and: intersection or: union

Application to a programming language: Eiffel extend (new : G ; key : H ) -- Assuming there is no item of key key, -- insert new with key ; set inserted. require key_not_present: not has (key) ensure insertion_done: item (key) = new key_present: has (key) inserted: inserted one_more: count = old count + 1

The case of postconditions Postconditions are often predicates on two states Example (Eiffel, in a class COUNTER): increment require count >= 0 … ensure count = old count + 1

Partial vs total correctness {P} A {Q} Total correctness: A, started in any state satisfying P, will terminate in a state satisfying Q Partial correctness: A, started in any state satisfying P, will, if it terminates, yield a state satisfying Q

Elementary mathematics Assume we want to prove, on integers {x > 0} A {y  0} [1] but have actually proved {x > 0} A {y = z ^ 2} [2] We need properties from other theories, e.g. arithmetic

“EM”: Elementary Mathematics The mark [EM] will denote results from other theories, taken (in this discussion) without proof Example: y = z ^ 2 implies y  0 [EM]

Rule of consequence {P} A {Q}, P’ implies P, Q implies Q’ _____________________________ {P’} A {Q’} Example: {x > 0} y := x + 2 {y > 0}

Rule of conjunction {P} A {Q}, {P} A {R} ________________ {P} A {Q and R} Example: {True} x := 3 {x > 1 and x > 2}

Axiomatic semantics for a programming language Example language: Graal (from Introduction to the theory of Programming Languages) Scheme: give an axiom or inference rule for every language construct

Skip {P} skip {P}

Abort {False} abort {P}

Sequential composition {P} A {Q}, {Q} B {R} __________________ {P} A ; B {R} Example: {x > 0} x := x + 3 ; x := x + 1 {x > 4}

Assignment axiom (schema) {P [e / x]} x := e {P} P [e/x] is the expression obtained from P by replacing (substituting) every occurrence of x by e.

Substitution x [x/x] = x [y/x] = x [x/y] = x [z/y] = 3 * x + 1 [y/x] =

Applying the assignment axiom {y > z – 2} x := x + 1 {y > z – 2} {2 + 2 = 5} x := x + 1 {2 + 2 = 5} {y > 0} x := y {x > 0} {x + 1 > 0} x := x + 1 {x > 0}

Limits to the assignment axiom No side effects in expressions! asking_for_trouble (x: in out INTEGER): INTEGER do x := x + 1; global := global + 1; Result := 0 end Do the following hold? {global = 0} u := asking_for_trouble (a) {global = 0} {a = 0} u := asking_for_trouble (a) {a = 0}

{P} A {Q}, FV(R)  modifies(A) =  __________________ The rule of constancy {P} A {Q}, FV(R)  modifies(A) =  __________________ {P and R} A {Q and R} FV(F) = variables free in formula F modifies(A) = variables assigned to in code A “Whatever A doesn’t modify stays the same”

The rule of constancy: examples { y = 3 } x := x + 1 { y = 3 } { y  0: y2 > 0 } y := y + 1 { y  0: y2 > 0 } { y = 3 } x := sqrt(y) { y = 3 } { a[3] = 0 } a[i] := 2 { a[3] = 0 } { bob.age = 65 } tony.age := 78 { bob.age = 65 }

The frame rule: examples and caveats { y = 3 } x := x + 1 { y = 3 } { y  0: y2 > 0 } y := y + 1 { y  0: y2 > 0 } { y = 3 } x := sqrt(y) { y = 3 } Only if sqrt doesn’t have side effects on y! { a[3] = 0 } a[i] := 2 { a[3] = 0 } Only if i  3! { bob.age = 65 } tony.age := 78 { bob.age = 65 } Only if bob  tony, i.e., they are not aliases!

The assignment axiom for arrays { P [ if k = i then e else j / a[k] ] } a[i] := e { P } Example: { 3 = i or (3  i and a[3] = 2) } a[i] := 2 { a[3] = 2 }

Example: Conditional rule {P and c} A {Q}, {P and not c} B {Q} ______________________________ {P} if c then A else B end {Q} Example: {y > 0} if x > 0 then y := y + x else y := y - x

Conditional rule: example proof Prove: { m, n, x, y > 0 and x ≠ y and gcd (x, y ) = gcd (m, n ) } if x > y then x := x – y else y := y – x end { m, n, x, y > 0 and gcd (x, y ) = gcd (m, n ) }

Loop rule (partial correctness) {P} A {I} {I and not c} B {I} ______________________________ {P} from A until c loop B end {I and c}

Loop rule (partial correctness, variant) {P} A {I}, {I and not c} B {I}, {(I and c) implies Q} _____________________________________ {P} from A until c loop B end {Q} Example: {y > 3 and n > 0} from i := 0 until i = n loop i := i + 1 y := y + 1 end {y > 3 + n}

Loop termination Must show there is a variant: Expression v of type INTEGER such that (for a loop from A until c loop B end with precondition P): 1. {P} A {v ≥ 0} 2.  v0 > 0: {v = v0 and not c} B {v < v0 and v ≥ 0}

Loop termination: example {y > 3 and n > 0} from i := 0 until i = n loop i := i + 1 y := y + 1 variant ?? end {y > 3 + n}

Computing the maximum of an array from i := 0 ; Result := a [1] until i = a.upper loop i := i + 1 Result := max (Result , a [i ]) end

B B E E E E T T H H O O V V E E N N A L S Levenshtein distance 1 1 2 3 “Beethoven” to “Beatles” B B E E E E T T H H O O V V E E N N A L S Operation R D R D R Distance 1 1 2 3 4 4 5 44

B E A T L E S 1 2 3 4 5 6 7 1 2 3 4 5 6 7 B 1 1 1 2 3 4 5 6 E 2 2 1 1 2 3 4 5 E 3 3 2 1 1 2 3 3 4 T 4 4 3 2 2 1 2 3 4 H 4 5 5 4 3 3 2 2 3 4

Levenshtein distance algorithm distance (source, target: STRING): INTEGER -- Minimum number of operations to turn source into target local dist : ARRAY_2 [INTEGER] i, j, del, ins, subst : INTEGER do create dist.make (source.count, target.count) from i := 0 until i > source.count loop dist [i, 0] := i ; i := i + 1 end from j := 0 until j > target.count loop dist [0, j ] := j ; j := j + 1 end -- (Continued) 46

Levenshtein distance algorithm from i := 1 until i > source.count loop from j := 1 until j > target.count invariant loop if source [i ] = target [ j ] then dist [i, j ] := dist [ i -1, j -1] else deletion := dist [i -1, j ] insertion := dist [i , j - 1] substitution := dist [i - 1, j - 1] dist [i, j ] := minimum (deletion, insertion, substitution) + 1 end j := j + 1 i := i + 1 Result := dist (source.count, target.count) ??? 47

Reversing a list 1 2 3 4 5 right i first_element pivot from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot.right first_element.put_right (i ) end

Reversing a list 1 2 3 4 5 right i first_element pivot from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot.right first_element.put_right (i ) end

Reversing a list 1 2 3 4 5 right i first_element pivot from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot.right first_element.put_right (i ) end

Reversing a list 1 2 3 4 5 right i first_element pivot from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot.right first_element.put_right (i ) end

Reversing a list 1 2 3 4 5 right i i first_element pivot pivot from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot.right first_element.put_right (i ) end

Loop as approximation strategy a i a n Loop body: Result = a 1 = Max (a 1 .. a 1) i := i + 1 Result := max (Result , a [i ]) Result = Max (a 1 .. a 2) Result = Max (a 1 .. a i ) Result = Max (a 1 .. a n ) The loop invariant

Loops as problem-solving strategy A loop invariant is a property that: Is easy to establish initially (even to cover a trivial part of the data) Is easy to extend to cover a bigger part If covering all data, gives the desired result!

source [1..i ] to target [1..j ] B L E S 1 2 3 4 5 6 7 1 2 3 4 5 6 7 Invariant: each a [i, j ] is distance from source [1..i ] to target [1..j ] B 1 1 1 2 3 4 5 6 E 2 2 1 1 2 3 4 5 E 3 3 2 1 1 2 3 3 4 K D R T 4 4 3 2 2 1 I 2 H 5 I K D R Keep Insert Delete Replace

Levenshtein loop from i := 1 until i > source.count loop from j := 1 until j > target.count invariant loop if source [i ] = target [ j ] then new := dist [ i -1, j -1] else deletion := dist [i -1, j ] insertion := dist [i , j - 1] substitution := dist [i - 1, j - 1] new := deletion.min (insertion.min (substitution)) + 1 end dist [i, j ] := new j := j + 1 i := i + 1 Result := dist (source.count, target.count) -- For all p : 1 .. i, q : 1 .. j –1, we can turn source [1 .. p ] -- into target [1 .. q ] in dist [p, q ] operations 56

Reversing a list 1 2 3 4 5 right i first_element pivot from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot.right first_element.put_right (i ) end Invariant: from first_element following right, initial items in inverse order; from pivot, rest of items in original order

________________________ {P [a/x]} f (a) {Q [a/x]} Routines (1) For: f (x: T) do Body end {P} Body {Q} ________________________ {P [a/x]} f (a) {Q [a/x]}

Routines (2) For: f (x: T) do Body end ( a | {P [a/x]} f (a) {Q [a/x]} ) implies {P} Body {Q} ___________________________________________ {P [a/x]} f (a) {Q [a/x]}

Hoare (1971) The solution to the infinite regress is simple and dramatic: to permit the use of the desired conclusion as a hypothesis in the proof of the body itself. Thus we are permitted to prove that the procedure body possesses a property, on the assumption that every recursive call possesses that property, and then to assert categorically that every call, recursive or otherwise, has that property. This assumption of what we want to prove before embarking on the proof explains well the aura of magic which attends a programmer's first introduction to recursive programming. Procedures and Parameters: An Axiomatic Approach, in E. Engeler (ed.), Symposium on Semantics of Algorithmic Languages, Lecture Notes in Mathematics 188, pp. 102-16 (1971).

Functions The preceding rule applies to procedures (routines with no results) Extension to functions?

A model To examine soundness and completeness we need a model: a mathematical description of program executions Basic model (programs without input): M: Instruction  (State State) State Variable  Value Also needed: E: Expression  (State Value)  / Partial functions = D  /

Example interpretations M [x := e] (s) = M [i1 ; i2] (s) = M [if c then i1 else i2 end] (s) = s  [x, E [e] (s)] \ “Overriding union”

Notation For an axiomatic theory A, a model M and a property p: M |= p means that p can be proved from M A |- p means that p can be proved from A