Dr. Muhammed Al-Mulhem 1ICS535-101 ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

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.
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.
Simple Example {i = 0} j := i * i {j < 100} Can we ‘verify’ this triple? Only if we know the semantics of assignment.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
1 Discrete Structures Lecture 29 Predicates and Programming Read Ch
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
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.
CS 355 – Programming Languages
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Operational Semantics ICS.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
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
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
ISBN Chapter 3 Describing Syntax and Semantics.
Semantics “Semantics” has to do with the meaning of a program. We will consider two types of semantics: –Static semantics: semantics which can be enforced.
Proving Program Correctness The Axiomatic Approach.
Methods of Proof & Proof Strategies
Describing Syntax and Semantics
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 Syntax and Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
Dr. Muhammed Al-MulhemICS (Denotational Semantics)
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
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.
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.
B (The language of B-Method )
Syntax Questions 6. Define a left recursive grammar rule.
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.
Chapter 3 Describing Syntax and Semantics.
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics

Dr. Muhammed Al-Mulhem 2ICS Axiomatic Semantics Based on formal logic (predicate calculus) Based on formal logic (predicate calculus) Original purpose: proof correctness of programs. Original purpose: proof correctness of programs. The logical expressions are called assertions. The logical expressions are called assertions. An assertion before a statement (a precondition) describes the constraints on the program variables at that point in the program. An assertion before a statement (a precondition) describes the constraints on the program variables at that point in the program. An assertion following a statement (a postcondition) describes the new constraints on those variables after execution of the statement. An assertion following a statement (a postcondition) describes the new constraints on those variables after execution of the statement.

Dr. Muhammed Al-Mulhem 3ICS Example We examine assertions from the point of view that preconditions are computed from given postconditions. We examine assertions from the point of view that preconditions are computed from given postconditions. Assume all variables are integer. Assume all variables are integer. Postconditions and preconditions are presented in braces. Postconditions and preconditions are presented in braces. A simple example: A simple example: sum = 2 * x + 1 {sum > 1} sum = 2 * x + 1 {sum > 1} The postcondition is {sum > 1} The postcondition is {sum > 1} One possible precondition is {x > 10} One possible precondition is {x > 10}

Dr. Muhammed Al-Mulhem 4ICS Weakest precondition A weakest precondition is the least restrictive precondition that will guarantee the postcondition. A weakest precondition is the least restrictive precondition that will guarantee the postcondition. For example, in the above statement and postcondition, For example, in the above statement and postcondition, { x > 10 } { x > 50 } { x > 100 } Are all valid precondition. The weakest precondition of all preconditions in this case is { x > 10 } The weakest precondition of all preconditions in this case is { x > 10 }

Dr. Muhammed Al-Mulhem 5ICS Correctness proofs If the Weakest precondition can be computed from the given postconditions for each statement of a language, then correctness proofs can be constructed for programs in that language as follows: If the Weakest precondition can be computed from the given postconditions for each statement of a language, then correctness proofs can be constructed for programs in that language as follows: The proof is begun by using the desired result of the program’s execution as the postcondition of the last statement of the program. The proof is begun by using the desired result of the program’s execution as the postcondition of the last statement of the program. This postcondition, along with the last statement, is used to compute the weakest precondition for the last statement. This postcondition, along with the last statement, is used to compute the weakest precondition for the last statement. This precondition is then used as the postcondition for the second last statement. This precondition is then used as the postcondition for the second last statement. This process continues until the beginning of the program is reached. This process continues until the beginning of the program is reached.

Dr. Muhammed Al-Mulhem 6ICS Correctness proofs At that point, the precondition of the first statement states the condition under which the program will compute the desired results. At that point, the precondition of the first statement states the condition under which the program will compute the desired results. If this condition is implied by the input specification of the program, the program has been verified to be correct. If this condition is implied by the input specification of the program, the program has been verified to be correct. To use axiomatic semantics for correctness proofs or for formal semantic specifications, either an axiom or an inference rule must be available for each kind of statement in the language. To use axiomatic semantics for correctness proofs or for formal semantic specifications, either an axiom or an inference rule must be available for each kind of statement in the language. An axiom is a true logical statement. An axiom is a true logical statement. An inference rule is a method of inferring the truth of an assertion based on other assertions. An inference rule is a method of inferring the truth of an assertion based on other assertions.

Dr. Muhammed Al-Mulhem 7ICS Axiomatic Semantics: Assignment statement Let x = E be a general assignment statement and Q be the postcondition. Let x = E be a general assignment statement and Q be the postcondition. Then its weakest precondition P, is defined by the axiom Then its weakest precondition P, is defined by the axiom P = Q x→E P is computed as Q with all instances of x replaced by E. P is computed as Q with all instances of x replaced by E.

Dr. Muhammed Al-Mulhem 8ICS Example For example, consider the following statement and postcondition. For example, consider the following statement and postcondition. a = b / { a < 10} The weakest precondition is computed by subsituting b/2-1 in the postcondition The weakest precondition is computed by subsituting b/2-1 in the postcondition b / < 10 b < 22

Dr. Muhammed Al-Mulhem 9ICS Notations for axiomatic semantics The usual notations are: The usual notations are: {P} S {Q} Where P is the precondition, Q is the postcondition and S is the statement. Where P is the precondition, Q is the postcondition and S is the statement. For the assignment statement, the notation is For the assignment statement, the notation is {Q x→E } x = E {Q}

Dr. Muhammed Al-Mulhem 10ICS Example Compute the precondition for the assignment statement Compute the precondition for the assignment statement x = 2 * y - 3 { x > 25 } The weakest precondition is computed as The weakest precondition is computed as 2 * y -3 > 25 y > 14

Dr. Muhammed Al-Mulhem 11ICS Example What about if the left side of the assignment appears in the right side of the assignment? What about if the left side of the assignment appears in the right side of the assignment? x = x + y - 3 {x > 10} The weakest precondition is The weakest precondition is x + y - 3 > 10 y > 13 – x Has no effect on the process of computing the precondition. Has no effect on the process of computing the precondition.

Dr. Muhammed Al-Mulhem 12ICS Axiomatic Semantics: Sequences The precondition for a sequence of statements cannot be described by an axiom, because the precondition depends on the particular kind of statements in the sequence. The precondition for a sequence of statements cannot be described by an axiom, because the precondition depends on the particular kind of statements in the sequence. The precondition can only be described with an inference rule. The precondition can only be described with an inference rule. Let S1 and S2 be adjacent statements. Let S1 and S2 be adjacent statements. Assume that S1 and S2 have the following pre/postconditions: Assume that S1 and S2 have the following pre/postconditions: {P1} S1 {P2} {P2} S2 {P3} The inference rule for such two-statement sequence is The inference rule for such two-statement sequence is The axiomatic semantics of the sequence S1; S2 is The axiomatic semantics of the sequence S1; S2 is

Dr. Muhammed Al-Mulhem 13ICS Axiomatic Semantics: Sequences The above inference rule states that to get the sequence precondition, the precondition of the second statement is computed. The above inference rule states that to get the sequence precondition, the precondition of the second statement is computed. This new assertion is used as the postcondition of the first statement, which can then be used to compute the precondition of the first statement. This new assertion is used as the postcondition of the first statement, which can then be used to compute the precondition of the first statement. This precondition can be used as the precondition for the whole sequence. This precondition can be used as the precondition for the whole sequence.

Dr. Muhammed Al-Mulhem 14ICS Example Assume we have the following sequence of statements: Assume we have the following sequence of statements: x1 = E1 x2 = E2 Then we have Then we have {P3 x2→E2 } x2 = E2 {P3} {P3 x2→E2 } x1→E1 x1 = E1 {P3 x2→E2 } Therefore, the precondition for the sequence x1=E1; x2=E2 with postcondition P3 is {P3 x2→E2 } x1→E1 Therefore, the precondition for the sequence x1=E1; x2=E2 with postcondition P3 is {P3 x2→E2 } x1→E1

Dr. Muhammed Al-Mulhem 15ICS Example Consider the following sequence and postcondition: Consider the following sequence and postcondition: y = 3 * x + 1; x = y + 3; {x < 10} The precondition for the last assignment statement is The precondition for the last assignment statement is y < 7 Which is used as the postcondition for the first statement. Which is used as the postcondition for the first statement. The precondition for the first statement and the sequence can be now computed. The precondition for the first statement and the sequence can be now computed. 3 * x + 1 < 7 x < 2

Dr. Muhammed Al-Mulhem 16ICS Axiomatic Semantics: Selection The general form of the selection statement is The general form of the selection statement is If B then S1 elese S2 The inference rule is The inference rule is This rule indicates that selection statements must be proven for both when the condition expression is true and when it is false. This rule indicates that selection statements must be proven for both when the condition expression is true and when it is false. The first logical statement above the line represents the then clause; the second represents the else clause. The first logical statement above the line represents the then clause; the second represents the else clause. We need a precondition P that can be used in the precondition of both the then and else clauses. We need a precondition P that can be used in the precondition of both the then and else clauses.

Dr. Muhammed Al-Mulhem 17ICS Example Consider the following selection statement: Consider the following selection statement: if ( x > 0 ) y = y - 1 else y = y + 1 Suppose the postcondition, Q for the selection statement is {y>0} Suppose the postcondition, Q for the selection statement is {y>0} We can then use the axiom for assignment on the then clause. We can then use the axiom for assignment on the then clause. y = y - 1 { y > 0} This produces {y -1 > 0} or {y > 1}. It can be used as the P part of the precondition of the then clause It can be used as the P part of the precondition of the then clause Now, Apply the same axiom for the else clause Now, Apply the same axiom for the else clause y = y + 1 { y > 0} which produces y = y + 1 { y > 0} or { y > -1} Because {y > 1} → {y > -1} Because {y > 1} → {y > -1} The rule uses {y > 1} for the precondition of the whole selection statement. The rule uses {y > 1} for the precondition of the whole selection statement.