Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.

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.
Intermediate Code Generation
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.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Principles of programming languages 3: Answers for exercises Isao Sasano Department of Information Science and Engineering.
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.
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
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Axiomatic Semantics Dr. M Al-Mulhem ICS
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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.
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
Imperative Programming
Reading and Writing Mathematical Proofs
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CPS120: Introduction to Computer Science Decision Making in Programs.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Compiler Chapter# 5 Intermediate code generation.
Principle of Programming Lanugages 2: Imperative languages Isao Sasano Department of Information Science and Engineering ( structured programming, control.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CPS120: Introduction to Computer Science Decision Making in Programs.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen 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.
Languages and Compilers
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
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.
Imperative Programming Statements and invariants.
Chapter 3 of Programming Languages by Ravi Sethi
Principle of Programming Lanugages 2: Imperative languages
Principles of programming languages 8: Types
Principles of programming languages 4: Parameter passing, Scope rules
Formal Methods in Software Engineering 1
Lecture 5 Floyd-Hoare Style Verification
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Program correctness Axiomatic semantics
Programming Languages and Compilers (CS 421)
Controlling Program Flow
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering Isao Sasano

Translation of statements While statements are translated to efficient machine code. … translation of E if E is false jump translation of S jump … Translation of while statemtents of the form while E do S

Translation of selection statements Selection statements like case in Pascal can be translated in various ways, which may affect the programming style. (ex. 1) A selection is translated to code with a jump table (array), where a selection is recommended to be used only when the constants are almost adjacent. (ex. 2) A selection is translated to code depending on the distribution of the constants. Selections with, say less than 7, cases are translated to nesting of conditionals. Selection with a larger number of cases are translated to code with a jump table or hash table depending on the distribution of cases.

Syntax of statements in C Statements in C are defined using extended BNF as follows. ::= ; | ; | { } | if ( ) | if ( ) else | while ( ) | do while ( ) ; | for ( ; ; ) | switch ( ) | case : | default : | break; | continue; | return; | return ; | goto ; | : ::= * ::=  |

About C Basic types do not include the bool type. In conditional expression 0 is used as false and the others are used as true. = is the assignment operator, == and != are comparison operator. Conditional expressions in while statements must be surrounded by parentheses. Braces { and } are used in stead of begin and end. Logical and and or are && and ||. C is different from Pascal in the following points.

Short circuit evaluation Most languages like C, Modula2 and Pascal use short- circuit evaluation for the boolean and and or expressions. (In boolean expressions, the second operand is evaluated only when it is necessary. ) (ex. ) x = 1; if (x == 1 || y == 2) … In the above code fragment in C, x==1 is evaluated to 1 (true) so the conditional expression is evaluated to 1 (true) without evaluating the expression y==2.

Control flow in short-circuit evaluation (ex. ) if (x==1 || y==2) x = x + 1; Entry x==1 Exit F T x = x+1 y==2 T F In short-circuit evaluation the control flow becomes a little complex but run-time efficiency is a little improved.

Tree-width (out of scope of the lecture) Control flow graphs for programs without goto statements have complexity with respect to some criteria called tree- width no more than some constants. Algol without goto, Pasal without goto: ≤ 3 Modula-2: ≤ 5 (Modula-2 does not have goto) C without goto: ≤ 6 The constans decrease by 1 without short-circuit evalutaion. Different from Algol and Pascal, Modula-2 allows multiple exits (break) in a loop construct and multiple exits (return) in a function declaration. Additionally C has continue statements. (Reference) Mikkel Thorup, “All structured programs have small tree-width and good register allocation”, Information and Computation, Vol. 142, pp. 159–181, 1998.

Exercise (1) Illustrate the control flow graph of the following program fragment in C. if (y==1 || y==2) if (x > 0) x = x - 1; (2) Illustrate the control flow graph of the following program fragment in C. while (x > 0) { if (x%2==0 || x%3==0) s = s + x; x = x – 1; }

Invariants An invariant is a conditional expression (assertion) that holds every time control reaches a program point. (ex. ) x := 10; y := 2; while x  y do x := x – y x  y x := x-y T F y := 2 x := 10 A conditional expression x  y holds every time control reaches this point.

Assertions An assertion is a conditional expression. Java has a syntax for describing assertions. In C++ we can use assertions as a macro by including assert.h. (An ex. in Java) int sum (int n) { assert (n > 0); … } If the programmer intends that the sum method always takes as its argument a positive integer, he can insert the assertion for finding bugs concerning this point.

Examples of assertions Here we enclose assertions by curly braces. while x  y do { x  y } x := x – y Suppose an assertion { x  0 and y > 0 } always holds just before the while statement. Then the assertion { y > 0 and x  y } in the program below is an invariant. (the previous ex.) { x  0 and y > 0 } while x  y do { y > 0 and x  y } x := x – y

Examples of assertions (cont.) { x  0 and y > 0 } while x  y do { y > 0 and x  y } x := x – y { x  0 and y > 0 } If the assertion just before the while statement is an invariant, the three assertions are all invariants. The assertion { x  0 and y > 0 } holds every time in the loop and is called a loop invariant.

Precondition and postcondition We can characterize the meaning of programs of single entry/single exit by assertions at the entry and the exit. (The meaning of a statement in an imperative language is the change of state before and after the statement. Statement Assertion at the exit of a statement (postcondition) Assertion at the entry of a statement (precondition)

Hoare triple We can describe the meaning of a statement by writing assertions before and after the statement. It was Charles Antony Richard Hoare (Tony Hoare in short) who proposed this notion. A statement with the assertions surrounded by curly braces { } is called a Hoare triple. The meaning of a Hoare triple {P} S {Q} For any state  that satisfies P, if the execution of S from the state  terminates in  ’ then  ’ satisfies Q.

Examples of Hoare triple { a = 0 } a := a + 1 { a = 1 } For any state  that satisfies a+1=1, if the execution of a:=a+1 from the state  terminates in  ’ then  ’ satisfies a=1. { a = 1 } a := a – 1; a := a + 1 { a = 1 } { a = 5 } while (a > 0) do a := a - 1 { a = 0 } For any state  that satisfies a=1, if the execution of a:=a-1; a:=a+1 from the state  terminates in  ’ then  ’ satisfies a=1. For any state  that satisfies a=5, if the execution of the while statement from the state  terminates in  ’ then  ’ satisfies a=0.

Partial correctness A Hoare triple {P} S {Q} does not say that the statement S terminates (since a while statement may not terminate). In order to show the termination we need some other way. A Hoare triple is said to be a partial correctness assertion. (Note) By extending the while rule, we get a proof system for proving total correctness assertions. (ex.) {true} while true do x := 1 {false} This Hoare triple holds. (Note) Total correctness --- Partial correctness + Termination We may write [P] S [Q] for a total correctness assertion.

Hoare logic We present proof rules which generate valid Hoare triples. The proof rules are syntax-directed. Hoare logic is a proof system consisting of the collection of rules. Hoare logic is an axiomatic semantics. (There are various kinds of axiomatic semantics.) (Ref. 1) C. A. R. Hoare, "An axiomatic basis for computer programming“, Communications of the ACM, 12(10):576–580,583, (Ref. 2) R. W. Floyd, “Assigning meanings to programs”, Proceedings of the American Mathematical Society Symposium on Applied Mathematics, Vol. 19, pp. 19– (Note) Floyd considered similar thing on flowchart.

Hoare logic (composition rule) {P} S 1 {Q} {Q} S 2 {R} {P} S 1 ; S 2 {R} (conditional rule) {P  E} S 1 {Q} {P   E} S 2 {Q} {P} if E then S 1 else S 2 {Q} (while rule) {P  E} S {P} {P} while E do S {P   E} { Q[E/x] } x := E {Q} (assignment axiom) P  P’ {P’} S {Q’} Q’  Q {P} S {Q} (consequence rule)

An example We prove a Hoare triple { x  0 } while x > 0 do x := x – 1 {x = 0} by the rules of Hoare logic. { x  0 } while x > 0 do x := x – 1 {x = 0} { x  0 } while x > 0 do x := x – 1 { x  0   x > 0}, x  0   x > 0  x = 0 { x  0  x > 0} x := x – 1 { x  0 } { x-1  0 } x := x – 1 { x  0 } x  0  x > 0  x-1  0, (consequence) (while) (consequence) (assignment) (Note) There are more than one Hoare triple that holds for a statement. (Note) We allow the consequence rule is applied to just one side.

Another example r := x; q := 0; while y  r do (r := r – y; q := 1 + q) {true} {  y  r  x = r + y * q } We omit the derivation (proof) of the above Hoare triple. This Hoare triple states that for any state , if the execution of the statement from the state  terminates in  ’, then  ’ satisfies that the quotient and the remainder of x and y is stored at q and r respectively.

A note about assertions As for assertions, in this class, we allow true, false, variables, integers, addition, subtraction, multiplication, comparison (, , , =), logical operators ( , , ,  ). Although ,  can be used, in this class we do not use them. Although we should write rules for arithmetic operations, comparisons and so on, we do not write them and allow inferences for such kind to be done by common sense and be omitted. (Ref. 1) Glynn Winskel, “The formal semantics of programming languages”, 1993, MIT Press, Chapter 6. (Ref. 2) C. A. R. Hoare, "An axiomatic basis for computer programming“, Communications of the ACM, 12(10):576–580,583, 1969.

Exercises Derive (prove) the following Hoare triples. (1){ a = 0 } a := a + 2 { a = 2 } (2){ a = 3 } if a = 3 then a := a + 1 else a := a – 1 { a = 4 } (3){ a = 1 } while (a < 5) do a := a + 1 { a = 5 }

Reference In actual computers basic types have limits and we need rules concerning the limits. (ex.) overflow about the type of integers more than or equal to 0 (1) Program terminates with error if an overflow occurs.  x (x = max + 1) (2) If an overflow occurs, we let the result to be the maximum number. max + 1 = max (3) If an overflow occurs, we let the result to be the remainder of the devision by the maximum number. max + 1 = 0