Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Similar documents
Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics

10/30/18. Dynamic Semantics DYNAMIC SEMANTICS. Operational Semantics. An Example. Operational Semantics Definition Process

Programming Languages

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Chapter 3. Describing Syntax and Semantics. Introduction. Why and How. Syntax Overview

3. DESCRIBING SYNTAX AND SEMANTICS

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Syntax and Semantics

An Annotated Language

Program Assignment 2 Due date: 10/20 12:30pm

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019

Defining Languages GMU

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Programming Languages Third Edition

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

Introduction to Axiomatic Semantics

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Contents. Chapter 1 SPECIFYING SYNTAX 1

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

CSE 307: Principles of Programming Languages

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

Formal Methods. CITS5501 Software Testing and Quality Assurance

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

Lectures 20, 21: Axiomatic Semantics

! Use of formal notations. ! in software system descriptions. ! for a broad range of effects. ! and varying levels of use. !

Statement Basics. Assignment Statements

Lecture 11 Lecture 11 Nov 5, 2014

Lecture 5 - Axiomatic semantics

Introduction to Axiomatic Semantics (1/2)

6. Hoare Logic and Weakest Preconditions

Hoare Logic: Proving Programs Correct

Introduction to Axiomatic Semantics (1/2)

Basic Verification Strategy

Semantic Analysis Type Checking

Abstract Interpretation

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Formal Verification. Lecture 10

Symbolic Execution and Proof of Properties

Formal Semantics of Programming Languages

CMSC 330: Organization of Programming Languages

A Partial Correctness Proof for Programs with Decided Specifications

Formal Semantics of Programming Languages

CSc 520 Principles of Programming Languages

Handout 9: Imperative Programs and State

CMSC 330: Organization of Programming Languages. Operational Semantics

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

Lecture 10 Design by Contract

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

Formal Verification! Prof. Leon Osterweil! Computer Science 520/620! Spring 2011!

CSC 501 Semantics of Programming Languages

Chapter 2 & 3: Representations & Reasoning Systems (2.2)

Com S 541. Programming Languages I

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Denotational Semantics

Reasoning about programs

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

An Evolution of Mathematical Tools

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

CS323 Lecture - Specifying Syntax and Semantics Last revised 1/16/09

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

CITS5501 Software Testing and Quality Assurance Formal methods

Software Quality Assurance

The Rule of Constancy(Derived Frame Rule)

Lecture Notes on Linear Search

FreePascal changes: user documentation

CS2104 Prog. Lang. Concepts

VS 3 : SMT Solvers for Program Verification

COMP 507: Computer-Aided Program Design

Chapter 5. Algorithms Pearson Addison-Wesley. All rights reserved

What if current foundations of mathematics are inconsistent? Vladimir Voevodsky September 25, 2010

Verification Condition Generation

Proving the Correctness of Distributed Algorithms using TLA

Functional Languages. Hwansoo Han

Introductory logic and sets for Computer scientists

Revisiting Kalmar completeness metaproof

Computing Fundamentals 2 Introduction to CafeOBJ

Axiomatic Specification. Al-Said, Apcar, Jerejian

In Our Last Exciting Episode

Warm-Up Problem. 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment: x (assignment)

Semantics with Applications 3. More on Operational Semantics

Lecture Notes: Hoare Logic

Specifications. Prof. Clarkson Fall Today s music: Nice to know you by Incubus

LOGIC AND DISCRETE MATHEMATICS

From Hoare Logic to Matching Logic Reachability. Grigore Rosu and Andrei Stefanescu University of Illinois, USA

Chapter 27 Formal Specification

Software Engineering: A Practitioner s s Approach, 6/e Roger Pressman. Chapter 28 Formal Methods

Transforming Programs into Recursive Functions

Formal Specification. Objectives

Computation Club: Gödel s theorem

Transcription:

There is no single widely acceptable notation or formalism for describing semantics Operational Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-29

To use operational semantics for a high-level language, a virtual machine is needed A hardware pure interpreter would be too expensive A software pure interpreter also has problems: The detailed characteristics of the particular computer would make actions difficult to understand Such a semantic definition would be machinedependent Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-30

Operational A better alternative: A complete computer simulation The process: Build a translator (translates source code to the machine code of an idealized computer) Build a simulator for the idealized computer State changes from execution define meaning Evaluation of operational semantics: Good if used informally (language manuals, etc.) Extremely complex if used formally (e.g., VDL) Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-31

Axiomatic Based on formal logic (predicate calculus) Original purpose: formal program verification Approach: Define axioms or inference rules for each statement type in the language (to allow transformations of expressions to other expressions) The expressions are called assertions Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-32

Axiomatic An assertion before a statement (a precondition) states the relationships and constraints among variables that are true at that point in execution An assertion following a statement is a postcondition A weakest precondition is the least restrictive precondition that will guarantee the postcondition If weakest precondition computable for each language statement, correctness proofs possible Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-33

Axiomatic Pre-post form: {P} statement {Q} An example: a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0} Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-34

Axiomatic Program proof process: The postcondition for the whole program is the desired result. Work back through the program to the first statement. If the precondition on the first statement is the same as the program spec, the program is correct. Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-35

Axiomatic An axiom for assignment statements (x = E): {Q x->e } x = E {Q} The Rule of Consequence: {P} S{Q}, P' = P, Q = Q' {P' }S {Q' } Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-36

Axiomatic An inference rule for sequences For a sequence S1;S2: {P1} S1 {P2} {P2} S2 {P3} the inference rule is: {P1} S1 {P2}, {P1} S1; {P2} S2 {P3} S2 {P3} Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-37

Axiomatic An inference rule for logical pretest loops For the loop construct: {P} while B do S end {Q} the inference rule is: (I and B) S {I} {I} while B do S{I and (not B)} where I is the loop invariant (the inductive hypothesis) Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-38

Axiomatic Characteristics of the loop invariant I must meet the following conditions: P => I (the loop invariant must be true initially) {I} B {I} (evaluation of the Boolean must not change the validity of I) {I and B} S {I} (I is not changed by executing the body of the loop) {I and (not B)} => Q (if I is true and B is false, Q is implied) The loop terminates (this can be difficult to prove) Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-39

Axiomatic The loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. I must be weak enough to be satisfied prior to the beginning of the loop, but when combined with the loop exit condition, it must be strong enough to force the truth of the postcondition Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-40

Evaluation of axiomatic semantics: Developing axioms or inference rules for all of the statements in a language is difficult It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-41

Denotational Based on recursive function theory The most abstract semantics description method Originally developed by Scott and Strachey (1970) Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-42

Denotational The process of building a denotational spec for a language (not necessarily easy): Define a mathematical object for each language entity Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects The meaning of language constructs are defined by only the values of the program's variables Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-43

The difference between denotational and operational semantics: In operational semantics, the state changes are defined by coded algorithms; in denotational semantics, they are defined by rigorous mathematical functions Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-44

Denotational The state of a program is the values of all its current variables s = {<i 1, v 1 >, <i 2, v 2 >,, <i n, v n >} Let VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(i j, s) = v j Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-45

Decimal Numbers The following denotational semantics description maps decimal numbers as strings of symbols into numeric values Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-46

<dec_num> 0 1 2 3 4 5 6 7 8 9 <dec_num> (0 1 2 3 4 5 6 7 8 9) M dec ('0') = 0, M dec ('1') = 1,, M dec ('9') = 9 M dec (<dec_num> '0') = 10 * M dec (<dec_num>) M dec (<dec_num> '1 ) = 10 * M dec (<dec_num>) + 1 M dec (<dec_num> '9') = 10 * M dec (<dec_num>) + 9 Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-47

Expressions Map expressions onto Z {error} We assume expressions are decimal numbers, variables, or binary expressions having one arithmetic operator and two operands, each of which can be an expression Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-48

M e (<expr>, s) = case <expr> of <dec_num> => M dec (<dec_num>, s) <var> => if VARMAP(<var>, s) == undef then error else VARMAP(<var>, s) <binary_expr> => if (M e (<binary_expr>.<left_expr>, s) == undef OR M e (<binary_expr>.<right_expr>, s) == undef) then error else if (<binary_expr>.<operator> == + then M e (<binary_expr>.<left_expr>, s) + M e (<binary_expr>.<right_expr>, s) else M e (<binary_expr>.<left_expr>, s) * M e (<binary_expr>.<right_expr>, s)... Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-49

Assignment Statements Maps a state to a state M a (x := E, s) = if M e (E, s) == error then error else s = {<i 1,v 1 >,<i 2,v 2 >,...,<i n,v n >}, where for j = 1, 2,..., n, v j = VARMAP(i j, s) if i j <> x = M e (E, s) if i j == x Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-50

Logical Pretest Loops Maps state to state Assume M b and M sl M l (while B do L, s) = if M b (B, s) == undef then error else if M b (B, s) == false then s else if M sl (L, s) == error then error else M l (while B do L, M sl (L, s)) Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-51

The meaning of the loop is the value of the program variables after the statements in the loop have been executed the prescribed number of times, assuming there have been no errors In essence, the loop has been converted from iteration to recursion, where the recursive control is mathematically defined by other recursive state mapping functions Recursion, when compared to iteration, is easier to describe with mathematical rigor Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-52

Evaluation of denotational semantics: Can be used to prove the correctness of programs Provides a rigorous way to think about programs Can be an aid to language design Has been used in compiler generation systems Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-53