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.

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

3.7 Denotational Semantics

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

Induction and Semantics in Dafny

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

Application: Programming Language Semantics

A CRASH COURSE IN SEMANTICS

In Our Last Exciting Episode

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

1 Introduction. 3 Syntax

CS422 - Programming Language Design

Programming Languages Third Edition

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

Contents. Chapter 1 SPECIFYING SYNTAX 1

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

2 Introduction to operational semantics

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

CS422 - Programming Language Design

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

CSC 7101: Programming Language Structures 1. Operational Semantics. Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6. Operational vs.

3.4 Denotational Semantics

An Annotated Language

Denotational Semantics

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

Fundamental Concepts. Chapter 1

3.3 Small-Step Structural Operational Semantics (Small-Step SOS)

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

Static Program Analysis

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

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Design

Programming Languages

Static Program Analysis

PROGRAM ANALYSIS & SYNTHESIS

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

CSc 520 Principles of Programming Languages

CS2104 Prog. Lang. Concepts

Semantics and Concurrence Module on Semantic of Programming Languages

Formal Semantics of Programming Languages

Syntax vs. semantics. Detour: Natural deduction. Syntax vs. semantics (cont d) Syntax = grammatical structure Semantics = underlying meaning

Program Syntax; Operational Semantics

Semantics and Concurrence Module on Semantic of Programming Languages

Formal Semantics of Programming Languages

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

Chapter 3. Describing Syntax and Semantics

Chapter 3. The While programming language

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

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

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

Introduction to Axiomatic Semantics

Propositional Logic Formal Syntax and Semantics. Computability and Logic

Semantics with Applications 3. More on Operational Semantics

Formal Systems and their Applications

The Substitution Model

Lecture 9: Typed Lambda Calculus

Types, Expressions, and States

Handout 9: Imperative Programs and State

Exercises on Semantics of Programming Languages

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Operational Semantics 1 / 13

CSE505, Fall 2012, Midterm Examination October 30, 2012

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Lectures 20, 21: Axiomatic Semantics

Semantics of programming languages

Introduction to Denotational Semantics (1/2)

Lecture 3: Recursion; Structural Induction

Programming Languages Fall 2013

4.5 Pure Linear Functional Programming

Last class. CS Principles of Programming Languages. Introduction. Outline

Program Analysis: Lecture 02 Page 1 of 32

Abstract Interpretation

3.1 IMP: A Simple Imperative Language

Introduction to Denotational Semantics

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

Shared Variables and Interference

Semantics of programming languages

Chapter 3 (part 3) Describing Syntax and Semantics

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

CSE 505: Concepts of Programming Languages

Defining syntax using CFGs

Denotational Semantics

Operational Semantics. One-Slide Summary. Lecture Outline

CMSC 330: Organization of Programming Languages

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

A Rewriting Logic Approach to Operational Semantics

CS 242. Fundamentals. Reading: See last slide

Formal Semantics of Programming Languages

CMSC 330: Organization of Programming Languages. Operational Semantics

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

CS 6110 S14 Lecture 1 Introduction 24 January 2014

02157 Functional Programming Interpreters for two simple languages including exercises

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

Compilers and computer architecture From strings to ASTs (2): context free grammars

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

Denotational Semantics. Domain Theory

Transcription:

CS 6110 S18 Lecture 8 Structural Operational Semantics and IMP Today we introduce a very simple imperative language, IMP, along with two systems of rules for evaluation called small-step and big-step semantics. These both fall under the general style called structural operational semantics (SOS). We will also discuss why both the big-step and small-step approaches can be useful. 1 The IMP Language 1.1 Syntax of IMP There are three distinct types of expressions in IMP: arithmetic expressions AExp with elements denoted by a, a 0, a 1,... Boolean expressions BExp with elements denoted by b, b 0, b 1,... commands Com with elements denoted by c, c 0, c 1,... A program in the IMP language is a command in Com. Let n, n 0, n 1,... denote integers (elements of Z = {..., 2, 1, 0, 1, 2,...}). We will view n both as a number (a semantic object) and as a numeral (a syntactic object) representing the number n. This little bit of ambiguity should not cause any confusion; the numbers and the numerals are in one-to-one correspondence, so there is really no need to distinguish them. Similarly, there is no need to distinguish between the Boolean constants true and false (syntactic objects) and the Boolean values true, false 2 (semantic objects). Let Var be a countable set of variables ranging over Z. Elements of Var are denoted x, x 0, x 1.... The BNF grammar for IMP is AExp : a ::= n x a 0 + a 1 a 0 a 1 a 0 a 1 BExp : b ::= true false a 0 = a 1 a 0 a 1 b 0 b 1 b 0 b 1 b Com : c ::= skip x := a c 0 ; c 1 if b then c 1 else c 2 while b do c 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. 1.2 Environments and Configurations An environment (also known as a valuation or a store) is a function σ : Var Z that assigns an integer to each variable. Environments are denoted σ, σ 1, τ,... and the set of all environments is denoted Env. In IMP semantics, environments are always total functions. A configuration is a pair c, σ, where c Com is a command and σ is an environment. Intuitively, the configuration c, σ represents an instantaneous snapshot of reality during a computation, in which σ represents the current values of the variables and c represents the next command to be executed. 1

2 Small-Step Semantics Small-step semantics specifies the operation of a program one step at a time. There is a set of rules that we continue to apply to configurations until reaching a final configuration skip, σ (if ever). We write c, σ c, σ to indicate that the configuration c, σ reduces to c, σ in one step, and we write c, σ c, σ to indicate that c, σ reduces to c, σ in zero or more steps. Thus c, σ c, σ iff there exists k 0 and configurations c 0, σ 0,..., c k, σ k such that c, σ = c 0, σ 0, c, σ = c k, σ k, and c i, σ i c i+1, σ i+1 for 0 i k 1. To be completely proper, we will define auxiliary small-step operators a and b for arithmetic and Boolean expressions, respectively, as well as for commands. 1 These operators are partial functions: a : (AExp Env) AExp b : (BExp Env) BExp : (Com Env) (Com Env) Intuitively, a, σ a n if the expression a evaluates to the constant n in environment σ. We now present the small-step rules for evaluation in IMP. Just as with the λ-calculus, evaluation is defined by a set of inference rules that inductively define relations consisting of acceptable computation steps. 2.1 Arithmetic and Boolean Expressions Variables: Arithmetic: x, σ a σ(x) n 1 + n 2, σ a n 3 (where n 3 is the sum of n 1 and n 2 ) a 1, σ a a 1 a 1 + a 2, σ a a 1 + a 2 a 2, σ a a 2 n 1 + a 2, σ a n 1 + a 2 and similar rules for and. These rules say: If the reduction above the line can be performed, then the reduction below the line can be performed. The rules are thus inductive on the structure of the expression to be evaluated. Note that there is no rule that applies in the case n, σ. This configuration is irreducible. In all other cases, there is exactly one rule that applies. The rules for Booleans and comparison operators are similar. We leave them as exercises. 2.2 Commands Let us denote by σ[n/x] the environment that is identical to σ except possibly for the value of x, which is n. That is, { σ(y) if y x, σ[n/x](y) n if y = x. 1 Winskel [1] uses 1 instead of. 2

The construct [n/x] is called a rebinding operator. It is a meta-operator that is used to rebind a variable to a different value in the environment. Note that in the definition of σ[n/x], the condition y x does not mean that y and x are bound to different values, but that they are syntactically different variables. Assignment: x := n, σ skip, σ[n/x] a, σ a a x := a, σ x := a, σ Sequence: c 0, σ c 0, σ c 0 ; c 1, σ c 0 ; c 1, σ skip ; c 1, σ c 1, σ Conditional: b, σ b b if b then c 0 else c 1, σ if b then c 0 else c 1, σ While statement: if true then c 0 else c 1, σ c 0, σ if false then c 0 else c 1, σ c 1, σ while b do c, σ if b then (c ; while b do c) else skip, σ There is no rule for skip; the configuration skip, σ is irreducible. In all other cases, there is exactly one rule that applies. These rules tell us all we need to know to run IMP programs. 3 Big-Step Semantics As an alternative to small-step structural operational semantics, which specifies the operation of the program one step at a time, we now consider big-step operational semantics, in which we specify the entire transition from a configuration (an expression, environment pair) to a final value. This relation is denoted. For arithmetic expressions, the final value is an integer n Z; for Boolean expressions, it is a Boolean truth value true, false 2; and for commands, it is an environment σ : Var Z. Thus a : (AExp Env) Z b : (BExp Env) 2 : (Com Env) Env Here 2 represents the two-element Boolean algebra consisting of the two truth values {true, false} with the usual Boolean operations. Then c, σ σ says that σ is the environment of the final configuration, starting in configuration c, σ ; a, σ a n says that n Z is the integer value of arithmetic expression a evaluated in environment σ; and b, σ b t says that t 2 is the truth value of Boolean expression b evaluated in environment σ. 3

3.1 Arithmetic and Boolean Expressions The big-step rules for arithmetic and Boolean expressions are straightforward. The key when writing big-step rules is to think about how a recursive interpreter would evaluate the expression in question. The rules for arithmetic expressions are: Constants: Variables: Operations: and similarly for and. n, σ a n x, σ a σ(x) a 0, σ a n 0 a 1, σ a n 1 (where n2 is the sum of n 0 and n 1 ) a 0 + a 1, σ a n 2 The rules for evaluating Boolean expressions and comparison operators are similar. 3.2 Commands Skip: Assignments: Sequences: Conditionals: skip, σ σ a, σ a n x := a, σ σ[n/x] c 0, σ σ c 1, σ σ c 0 ; c 1, σ σ b, σ b true c 0, σ σ b, σ b false c 1, σ σ if b then c 0 else c 1, σ σ if b then c 0 else c 1, σ σ While statements: b, σ b false while b do c, σ σ b, σ b true c, σ σ while b do c, σ σ while b do c, σ σ 4 Big-Step vs. Small-Step SOS If the big-step and small-step semantics both describe the same language, we would expect them to agree. In particular, the relations and both capture the idea of a complete evaluation. We would expect that if c, σ is a configuration that evaluates in the small-step semantics to skip, σ, then σ should also be the result of the big-step evaluation, and vice-versa. Formally, Theorem 1. For all commands c Com and environments σ, τ Env, c, σ skip, τ c, σ τ. 4

One can prove this theorem by induction. Note that this statement about the agreement of big-step and small-step semantics has nothing to say about the agreement of nonterminating computations. This is because big-step semantics cannot talk directly about nontermination. If c, σ does not terminate, then there is no τ such that c, σ τ. Small-step semantics can model more complex features such as nonterminating programs and concurrency. However, in many cases it involves unnecessary extra work. If we do not care about modeling nonterminating computations, it is often easier to reason in terms of bigstep semantics. Moreover, big-step semantics more closely models an actual recursive interpreter. However, because evaluation skips over intermediate steps, all programs without final configurations are indistinguishable. References [1] Glynn Winskel. The Formal Semantics of Programming Languages. MIT Press, 1993. 5