Semantics. A. Demers Jan This material is primarily from Ch. 2 of the text. We present an imperative

Similar documents
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.

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

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

2 Introduction to operational semantics

3.7 Denotational Semantics

Programming Languages Third Edition

In Our Last Exciting Episode

CS422 - Programming Language Design

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Handout 9: Imperative Programs and State

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

Application: Programming Language Semantics

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

Propositional Logic Formal Syntax and Semantics. Computability and Logic

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

Chapter 3. The While programming language

1 Introduction. 3 Syntax

This book is licensed under a Creative Commons Attribution 3.0 License

Abstract Interpretation

Lecture 5: Predicate Calculus. ffl Predicate Logic ffl The Language ffl Semantics: Structures

Exercises on Semantics of Programming Languages

Denotational Semantics

The Substitution Model

6.001 Notes: Section 1.1

Propositional Logic. Part I

Now that we have keys defined for the maps, we can start talking about maps. The first of these are Total Maps. Total Maps are defined as follows:

Induction and Semantics in Dafny

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

An Annotated Language

Denotational Semantics

Denotational semantics

Lecture 3: Recursion; Structural Induction

CS4215 Programming Language Implementation. Martin Henz

Fundamental Concepts. Chapter 1

AXIOMS FOR THE INTEGERS

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

1 Definition of Reduction

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

Linear Time Unit Propagation, Horn-SAT and 2-SAT

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

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

Contents. Chapter 1 SPECIFYING SYNTAX 1

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Lecture 6: Arithmetic and Threshold Circuits

Functional programming with Common Lisp

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

STABILITY AND PARADOX IN ALGORITHMIC LOGIC

LECTURE 17. Expressions and Assignment

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Denotational Semantics. Domain Theory

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

CS103 Spring 2018 Mathematical Vocabulary

CS 512, Spring 2017: Take-Home End-of-Term Examination

Programming Languages Fall 2013

Organization of Programming Languages CS3200/5200N. Lecture 11

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic.

Checks and Balances - Constraint Solving without Surprises in Object-Constraint Programming Languages: Full Formal Development

3.2 Big-Step Structural Operational Semantics (Big-Step SOS)

CSE 12 Abstract Syntax Trees

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Semantics with Applications 3. More on Operational Semantics

6.001 Notes: Section 15.1

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Design

Summary of Course Coverage

The Substitution Model. Nate Foster Spring 2018

Intermediate Code Generation

Predicate Logic CHAPTER What This Chapter Is About

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Declarative programming. Logic programming is a declarative style of programming.

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

Mutable References. Chapter 1

CS2104 Prog. Lang. Concepts

LTCS Report. Concept Descriptions with Set Constraints and Cardinality Constraints. Franz Baader. LTCS-Report 17-02

Foundations: Syntax, Semantics, and Graphs

Propositional Calculus: Boolean Algebra and Simplification. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

Typed Racket: Racket with Static Types

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

8.1 Polynomial-Time Reductions

Formal Semantics of Programming Languages

CS 320: Concepts of Programming Languages

Reasoning About Programs Panagiotis Manolios

CSC Discrete Math I, Spring Sets

(Refer Slide Time: 4:00)

Chapter 10 Part 1: Reduction

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

Formal Semantics of Programming Languages

Verilog Tutorial - Edited for CS141

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

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

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Transcription:

CS411 Notes 1: IMP and Large Step Operational Semantics A. Demers 23-25 Jan 2001 This material is primarily from Ch. 2 of the text. We present an imperative language called IMP; wegive a formal definition of its syntax and the first of several operational semantic definitions. 1 The Syntax of IMP Here we present the syntax of an imperative programming language called IMP. IMP is about the simplest imperative language you can imagine. It has a fixed (possibly infinite) set of integer-valued global variables with assignments, conditional commands and while-loops, and that's about all. In particular, it lacks procedures, functions, variable declarations, type declarations, any many other creature comforts you would expect to find in a real" programming language. Despite its simplicity, IMP is Turing-complete, as we shall see later on. The syntax of IMP will be defined by simultaneous definition of the following six syntactic sets: N the (positive and negative) integers. T the (Boolean) truth values, true and false. Loc the set of locations of program variables. Aexp the set of arithmetic expressions. Bexp the set of Boolean expressions. Com the set of commands. We assume the first three syntactic sets numbers, Booleans and locations are already fully specified, and the reader's intuitive understanding will be sufficient. There are just couple of points we should note: 1

ffl Technically these are syntactic sets, and we probably should not be identifying N with the integers. In any real programming language there are multiple literals denoting the same number, e.g. `1', `01', `001',.... To simplify the exposition, we shall initially define this possibility away we shall assume elements of N are canonicalized so there is exactly one way to write each (signed) integer. ffl The name Loc suggests numeric memory addresses, but we can use any set that can uniquely identify memory locations; it will be most convenient to assume Loc is an infinite set of program variable names. For the remaining three syntactic sets the arithmetic and Boolean expressions, and the program commands (or statements) we give formation rules describing how to build up elements of each syntactic set from smaller elements of the same set, or from elements of the other syntactic sets. In these rules (as well as in the operational semantics to follow) we use simple notational conventions to associate variables with the sets they range over. N contains n; n 0 ;n 00 ;n 0 ;n 1 ;::: T contains t; t 0 ;t 00 ;t 0 ;t 1 ;:::. Loc the set of locations of program variables. Aexp contains a; a 0 ;a 00 ;a 0 ;a 1 ;::: Bexp contains b; b 0 ;b 00 ;b 0 ;b 1 ;::: Com contains c; c 0 ;c 00 ;c 0 ;c 1 ;::: Using these conventions, the formation rules for IMP are given by the following BNF description: a ::= n j X j a 0 + a 1 j a 0 a 1 j a 0 Λ a 1 b ::= true j false j a 0 = a 1 j a 0» a 1 j:b 0 j b 0 _ b 1 j b 0 ^ b 1 c ::= skip j X ψ a 0 j c 0 ; c 1 j if b 0 then c 0 else c 1 j while b 0 do c 0 These rules have the usual interpretation for BNF definitions (inductive definition of the syntactic sets). That is, they define the least sets (under subset ordering, ) containing the basic sets and closed under the formation rules. 2 Large Step Operational Semantics Here we give the first of several operational semantic definitions we shall discuss for IMP. 2

Our first definition is called a Large Step" operational semantics. The basic approach is first to define formally the notion of a program execution configuration intuitively, a pair consisting of some program text to be executed and a memory state from which execution is to begin and then to give asetof logical inference rules that define a reduction relation! between such configurations and (final) memory states. A configuration is related by! to the final state that would result from a terminating program execution starting from that initial configuration. We begin by defining the states: the set ± of (memory) states consists of the total functions ff : Loc! N. Such a function maps each location to a number; intuitively, ff(x) gives the value (or contents) of location X in state ff. We next define a command configuration as a pair hc; ffi, wherec 2 Com and ff 2 ±. We lied just a bit in the first paragraph of this section. There really need to be three different (but related) notions of a configuration. IMP programs do not just execute commands; they also evaluate arithmetic and Boolean expressions. Thus, we require the additional definitions: An arithmetic expression configuration is a pair ha; ffi, where a 2 Aexp and ff 2 ±. A Boolean expression configuration is a pair hb; ffi, where b 2 Bexp and ff 2 ±. Technically, there are also three different reduction relations; but since no confusion should result we overload the symbol! to represent all three of them: ha; ffi!n holds when evaluation of a starting in state ff produces the number n 2 N; hb; ffi!t holds when evaluation of b starting in state ff produces the Boolean value t 2 T; and hc; ffi!ff 0 holds when execution of c starting in state ff terminates in state ff 0. Below we give inference rules to define each of these relations. 2.1 Arithmetic Expression Rules These are the rules for evaluating arithmetic expressions. As noted above, each rule has a conclusion of the form ha; ffi!n 3

which you should interpret to mean expression a in state ff evaluates to n. Constants (L1.n) hn; ffi!n Anumber (a numeric literal) evaluates to itself independent of the state. Variables hx; ffi!ff(x) (L1.vref) A variable reference evaluates to the contents of the variable's location in the memory state. Binary Operations ha 0 + a 1 ;ffi!n where n = n 0 + n 1 (L1.add) The condition where n = n 0 + n 1 " should be interpreted to mean that the rule is applicable only if n 0 ;n 1 and n are instantiated by numbers whose values satisfy the stated equality. We can interpret the rule to mean that if the two subterms of a sum evaluate respectively to n 0 and n 1, then the sum evaluates to n 0 + n 1. ha 0 a 1 ;ffi!n where n = n 0 n 1 (L1.sub) This is the analogous rule for subtraction. Finally, ha 0 Λ a 1 ;ffi!n is the analogous rule for multiplication. where n = n 0 Λ n 1 (L1.mul) 2.2 Boolean Expression Rules Evaluation rules for Boolean expressions follow a similar pattern. Boolean Constants htrue;ffi!true (L1.t) (L1.f) hfalse;ffi!false Boolean constants evaluate to themselves without reference to the state. 4

Atomic Propositions ha 0 = a 1 ;ffi!true where n 0 = n 1 (L1.eqt) ha 0 = a 1 ;ffi!false ha 0» a 1 ;ffi!true where n 0 6= n 1 where n 0» n 1 (L1.eqf) (L1.leqt) ha 0» a 1 ;ffi!false where n 0 >n 1 (L1.leqf) These rules are all straightforward. Note the conclusions are Boolean expression reduction relations, while the hypotheses require proving arithmetic expression reduction relations. Unary Operations hb; ffi!true h:b; ffi!false (L1.nott) hb; ffi!false h:b; ffi!true (L1.notf) These are the obvious rules relating the value of a unary Boolean expression to the value of its negation. Binary Operations hb 0 ;ffi!t 0 hb 1 ;ffi!t 1 hb 0 ^ b 1 ;ffi!t where t 0 ^ t 1 t (L1.and) hb 0 ;ffi!t 0 hb 1 ;ffi!t 1 hb 0 _ b 1 ;ffi!t where t 0 _ t 1 t (L1.or) These rules relate the value of a binary Boolean expression to the values of its subexpressions. Note most real" programming languages would not evaluate both subexpressions unnecessarily that is, if the left operand of an _ reduced to true or the left operand of an ^ reduced to false, then the right operand would not be evaluated at all. This technique is sometimes called short-circuit evaluation," as opposed to the strict evaluation" performed by our inference rules. In the current version of IMP this optimization has no effect on the result of program execution. The value of a Boolean expression, and the result of executing a program containing Boolean expressions, is the same whether 5

strict or short-circuit evaluation is used. Later we will discuss variants of IMP in which expression evaluation can modify the store or can fail to terminate. In such variants, the distinction between strict and non-strict evaluation will be significant. 2.3 Command Execution Rules Before we present rules for command execution, we need some additional notation. Recall the state is a function mapping locations of program variables to their values. Intuitively, assigning a new value to a program variable should have the effect of changing the state function at a single argument point(the location of the affected variable). We express this by the following: ff[m=x](y )= ρ m ff(y ) if Y = X o.w. That is, ff[m=x] is a new state function that is identical to ff except on the argument X, where it returns m rather than ff(x). Null Commands hskip;ffi!ff The skip command has no effect on the state. (L1.skip) Assignments ha; ffi!n hx ψ a; ffi!ff[n=x] (L1.asgn) An assignment updates the state at the specified location to the value of the right-hand-side expression. Sequential Composition hc 0 ;ffi!ff 00 hc 1 ;ff 00 i!ff 0 hc 0 ; c 1 ;ffi!ff 0 (L1.seq) The execution of the sequential composition c 0 ; c 1 can be understood as the consecutive execution of c 0 and c 1 where the intermediate state ff 00 (the result of executing c 0 ) appears explicitly in the instantiated rule. 6

Conditionals hb; ffi!true hc 0 ;ffi!ff 0 hif b then c 0 elsec 1 ;ffi!ff 0 (L1.ift) Execution of a conditional whose test evaluates to true is equivalent to execution of the then clause, c 0. hb; ffi!false hc 1 ;ffi!ff 0 hif b then c 0 else c 1 ;ffi!ff 0 (L1.iff) Execution of a conditional whose test evaluates to false is equivalent to execution of the else clause, c 1. Loops The loop rules are intuitively straightforward. hb; ffi!false hwhile b do c; ffi!ff (L1.whf) A loop whose condition evaluates to false terminates immediately with no effect on the store. hb; ffi!true hc; ffi!ff 00 hwhile b do c; ff 00 i!ff 0 hwhile b do c; ffi!ff 0 (L1.wht) Execution of a loop whose condition evaluates to true is equivalent to unrolling" the loop once that is, executing the loop body c and then re-executing the entire loop starting from the new state ff 00 in which the first execution of c terminated. One aspect of this rule is new, and has important consequences. Note that the entire while- loop while b do c from the rule's conclusion appears in the third hypothesis. You should verify that this is new behavior in the following sense: in every other rule, the program pieces appearing in hypothesis configurations are subparts of the program piece in the conclusion configuration, and thus are strictly smaller. Without while loops and the while rule, you could exploit this diminishingsize property to compute an upper bound on the size of any possible proof of a given execution relation. That is, you could come up with an easy-to-compute function f such that no proof tree for hc; ffi!ff 0 could possibly require more than f(jcj) rule instances. In principle, you could write a computer program to enumerate all proof trees up to that size and check each tree against the desired conclusion hc; ffi!ff 0. Such a program would check termination, which isnot possible for a Turing-complete language. Of course, without while commands 7

IMP is not Turing-complete, and all loop-free IMP do terminate, so there is no contradiction in any of this. With while loops, there is no recursive bound on the size of the proof tree required to prove a terminating program execution. Any algorithm to construct a proof tree would be guaranteed, on at least some nonterminating programs and inputs, to loop forever, trying to construct an infinite proof tree." 8