• Keine Ergebnisse gefunden

Programming Language Semantics

N/A
N/A
Protected

Academic year: 2022

Aktie "Programming Language Semantics"

Copied!
29
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

7. Programming Language Semantics 7.0

Chapter 7

Programming Language Semantics

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 331

7. Programming Language Semantics 7.0

Overview of Chapter

7. Programming Language Semantics 7.1 Introduction

7.2 Big-step semantics

Basic concepts of big-step semantics Formalization of big-step semantics

7.3 Small-step semantics

Small-step semantics of IMP Proving properties of the semantics Extensions of IMP

7.4 Denotational semantics

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 332

7. Programming Language Semantics 7.1 Introduction

Section 7.1

Introduction

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 333

7. Programming Language Semantics 7.1 Introduction

Motivation

Why studying language semantics?

Understanding the details and construction of programming and modeling languages

Foundation for language processing tools (compilers, optimizers, interpreters,...)

Verifying type systems

Development of new language abstractions and software concepts

Reasoning about software

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 334

(2)

7. Programming Language Semantics 7.1 Introduction

Material

Literature

Glynn Winskel:

The Formal Semantics of Programming Languages: An Introduction

Benjamin C. Pierce et al.:

Software Foundations (www.cis.upenn.edu/~bcpierce/sf/)

Acknowledgement

Thanks to Prof. Peter Müller for the slides.

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 335

7. Programming Language Semantics 7.1 Introduction

General aspects

Degree of formalization:

Informal semantics in language reports

Formalization to support proof tools and as quality control

Goals here:

Learn distinction between operational and denotational semantics

Learn about the formalization of operational semantics

Understand the relationship between programming language semantics and transition systems

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 336

7. Programming Language Semantics 7.1 Introduction

Why Formal Semantics?

Programming language design

- Formal verification of language properties - Reveal ambiguities

- Support for standardization

Implementation of programming languages - Compilers

- Interpreters - Portability

Reasoning about programs

- Formal verification of program properties - Extended static checking

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.7

©Peter Müller 337

7. Programming Language Semantics 7.1 Introduction

Language Properties

Type safety:

In each execution state, a variable of type T holds a value of T or a subtype of T

Very important question for language designers Example:

If String is a subtype of Object, shouldString[] be a subtype ofObject[]?

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.8

©Peter Müller 338

(3)

7. Programming Language Semantics 7.1 Introduction

Language Properties

Type safety:

In each execution state, a variable of type T holds a value of T or a subtype of T

Very important question for language designers Example:

If String is a subtype of Object, shouldString[] be a subtype ofObject[]?

void m(Object[] oa) { oa[0]=new Integer(5);

}

String[] sa=new String[10];

m(sa);

String s = sa[0];

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.8

©Peter Müller 339

7. Programming Language Semantics 7.1 Introduction

Language Definition

Dynamic Semantics

Static Semantics

Syntax

State of a program execution Transformation of states

Type rules

Name resolution

Syntax rules, defined by grammar

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.12

©Peter Müller 340

7. Programming Language Semantics 7.1 Introduction

Compilation and Execution

Execution Semantic Analysis,

Type Checking Scanning, Parsing

Abstract Syntax Tree

Annotated Abstract Syntax Tree

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.13

©Peter Müller 341

7. Programming Language Semantics 7.1 Introduction

Three Kinds of Semantics

Operational semantics

- Describes execution on an abstract machine - Describes howthe effect is achieved

Denotational semantics

- Programs are regarded as functionsin a mathematical domain

- Describes only the effect, not how it is obtained Axiomatic semantics

- Specifies propertiesof the effect of executing a program are expressed

- Some aspects of the computation may be ignored

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.14

©Peter Müller 342

(4)

7. Programming Language Semantics 7.1 Introduction

Operational Semantics

y := 1;

while not(x=1) do ( y := x*y; x := x-1 )

“First we assign 1 to y, then we test whetherxis 1 or not. If it is then we stop and otherwise we updatey to be the product of xand the previous value ofy and then we decrement xby 1. Now we test whether the new value ofx is 1 or not. . . ”

Two kinds of operational semantics - Natural Semantics

- Structural Operational Semantics

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.15

©Peter Müller 343

7. Programming Language Semantics 7.1 Introduction

Denotational Semantics

y := 1;

while not(x=1) do ( y := x*y; x := x-1 )

“The program computes a partial function from states to states: the final state will be equal to the initial state except that the value ofx will be 1 and the value ofywill be equal to the factorial of the value of xin the initial state”

Two kinds of denotational semantics - Direct Style Semantics

- Continuation Style Semantics

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.16

©Peter Müller 344

7. Programming Language Semantics 7.1 Introduction

Axiomatic Semantics

y := 1;

while not(x=1) do ( y := x*y; x := x-1 )

“Ifx=nholds before the program is executed then y=n! will hold when the execution terminates (if it terminates)”

Two kinds of axiomatic semantics - Partial correctness

- Total correctness

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.17

©Peter Müller 345

7. Programming Language Semantics 7.1 Introduction

Abstraction

Concrete language implementation Operational semantics

Denotational semantics Axiomatic semantics

Abstract descrption

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.18

©Peter Müller 346

(5)

7. Programming Language Semantics 7.1 Introduction

Selection Criteria

Constructs of the

programming language - Imperative

- Functional - Concurrent - Object-oriented - Non-deterministic - Etc.

Application of the semantics

- Understanding the language

- Program verification - Prototyping

- Compiler construction - Program analysis - Etc.

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.19

©Peter Müller 347

7. Programming Language Semantics 7.1 Introduction

The Language IMP

Expressions

- Boolean and arithmetic expressions - No side-effects in expressions

Variables

- All variables range over integers - All variables are initialized - No global variables

IMP does not include

- Heap allocation and pointers - Variable declarations

- Procedures - Concurrency

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.30

©Peter Müller 348

7. Programming Language Semantics 7.1 Introduction

Syntax of IMP: Characters and Tokens

Characters

Letter = ’A’ . . . ’Z’ | ’a’ . . . ’z’

Digit = ’0’ | ’1’ | ’2’ | ’3’ | ’4’ | ’5’ | ’6’ | ’7’ | ’8’ | ’9’ Tokens

Ident = Letter { Letter | Digit } Integer = Digit { Digit }

Var = Ident

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.31

©Peter Müller 349

7. Programming Language Semantics 7.1 Introduction

Syntax of IMP: Expressions

Arithmetic expressions

Aexp = Aexp Op Aexp | Var | Integer Op = ’+’ | ’-’ | ’*’ | ’/’ | ’mod’

Boolean expressions

Bexp = Bexp ’or’ Bexp | Bexp ’and’ Bexp

| ’not’ Bexp | Aexp RelOp Aexp RelOp = ’=’ | ’#’ | ’<’ | ’<=’ | ’>’ | ’>=’

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.32

©Peter Müller 350

(6)

7. Programming Language Semantics 7.1 Introduction

Syntax of IMP: Statemens

Stm = ’skip’

| Var ’:=’ Aexp

| Stm ’;’ Stm

| ’if’ Bexp ’then’ Stm ’else’ Stm ’end’

| ’while’ Bexp ’do’ Stm ’end’

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.33

©Peter Müller 351

7. Programming Language Semantics 7.1 Introduction

Notation

Meta-variables (written in italic font) x, y, z for variables (Var)

e, e0, e1, e2 for arithmetic expressions (Aexp) b, b1, b2 for boolean expressions (Bexp) s, s0, s1, s2 for statements (Stm)

Keywords are written in typewriterfont

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.34

©Peter Müller 352

7. Programming Language Semantics 7.1 Introduction

Syntax of IMP: Example

res := 1;

while n > 1 do res := res * n;

n := n - 1 end

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.35

©Peter Müller 353

7. Programming Language Semantics 7.1 Introduction

Semantic Categories

Syntactic category: Integer Semantic category: Val= Z

101 - 5

101 - 101

Semantic functions map elements of syntactic categories to elements of semantic categories To define the semantics of IMP, we need semantic functions for

- Arithmetic expressions (syntactic category Aexp) - Boolean expressions (syntactic category Bexp) - Statements (syntactic category Stm)

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.37

©Peter Müller 354

(7)

7. Programming Language Semantics 7.1 Introduction

States

x+1 - ??

The meaning of an expression depends on the values bound to the variables that occur in it A state associates a value to each variable

State: VarVal

We represent a stateσ as a finite function σ ={x1 7→ v1, x2 7→ v2, . . . , xn 7→ vn}

wherex1, x2, . . . , xn are different elements of Var and v1, v2, . . . , vn are elements ofVal.

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.38

©Peter Müller 355

7. Programming Language Semantics 7.1 Introduction

Semantics of Arithmetic Expressions

The semantic function

A : AexpStateVal

maps an arithmetic expressione and a stateσ to a value A[[e]]σ

A[[x]]σ = σ(x)

A[[i]]σ = i fori ∈Z

A[[e1 op e2]]σ =A[[e1]]σ op A[[e2]]σ for op∈ Op opis the operation Val×ValVal corresponding toop

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.39

©Peter Müller 356

7. Programming Language Semantics 7.1 Introduction

Semantics of Boolean Expressions

The semantic function

B : BexpStateBool

maps a boolean expressionb and a state σ to a truth valueB[[b]]σ

B[[e1 op e2]]σ =

( tt ifA[[e1]]σ op A[[e2]]σ

ff otherwise

op∈RelOp andopis the relation Val×Val corresponding to op

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.40

©Peter Müller 357

7. Programming Language Semantics 7.1 Introduction

Boolean Expressions (cont’d)

B[[b1 or b2]]σ =

( tt ifB[[b1]]σ =tt or B[[b2]]σ =tt

ff otherwise

B[[b1 and b2]]σ =

( tt ifB[[b1]]σ =tt and B[[b2]]σ =tt

ff otherwise

B[[not b]]σ =

( tt ifB[[b]]σ =ff

ff otherwise

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.41

©Peter Müller 358

(8)

7. Programming Language Semantics 7.1 Introduction

Operational Semantics of Statements

Evaluation of an expression in a state yields a value

x + 2 * y A:AexpStateVal

Execution of a statement modifies the state

x := 2 * y

Operational semantics describe howthe state is modified during the execution of a statement

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.57

©Peter Müller 359

7. Programming Language Semantics 7.1 Introduction

Big-Step and Small-Step Semantics

Big-step semantics describe how theoverall results of the executions are obtained

- Natural semantics

Small-step semantics describe how the individual stepsof the computations take place

- Structural operational semantics - Abstract state machines

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.58

©Peter Müller 360

7. Programming Language Semantics 7.1 Introduction

Transition Systems

A transition system is a tuple (Γ, T,B)

- Γ: a set ofconfigurations

- T: a set ofterminal configurations,T Γ - B: atransition relation,BΓ×Γ

Example: Finite automaton

Γ ={hw, Si |w∈ {a, b, c}, S ∈ {1,2,3,4}}

T ={h, Si |S ∈ {1,2,3,4}}

B ={(haw,1i → hw,2i),(haw,1i → hw,3i), (hbw,2i → hw,4i),(hcw,3i → hw,4i)}

a b

c 2

3

4 1

a

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.60

©Peter Müller 361

7. Programming Language Semantics 7.2 Big-step semantics

Section 7.2

Big-step semantics

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 362

(9)

7. Programming Language Semantics 7.2 Big-step semantics

Big-step semantics

Introductory remarks:

Big-step semantics relatesprestatesof statement executions to poststates

Often callednatural semantics, because it abstracts from intermediate states

Overview:

Basic concepts of big-step semantics

Formalization of big-step semantics in Isabelle/HOL

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 363

7. Programming Language Semantics 7.2 Big-step semantics

Subsection 7.2.1

Basic concepts of big-step semantics

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 364

7. Programming Language Semantics 7.2 Big-step semantics

Transitions in Natural Semantics

Two types of configurations for operational semantics

1. hs, σi, which represents that the statementsis to be executed in stateσ

2. σ, which represents a terminal state

The transition relation→ describes how executions take place

- Typical transition: hs, σi → σ0 - Example: hskip, σi →σ

Γ ={hs, σi | s ∈ Stm, σ ∈State} ∪State

T =State

→⊆ {hs, σi | s ∈Stm, σ ∈State} ×State

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.61

©Peter Müller 365

7. Programming Language Semantics 7.2 Big-step semantics

Rules

Transition relation is specified by rules ϕ1, . . . , ϕn

ψ ifCondition whereϕ1, . . . , ϕn andψ are transitions Meaning of the rule

If Condition andϕ1, . . . , ϕn then ψ Terminology

- ϕ1, . . . , ϕn are calledpremises - ψis calledconclusion

- A rule without premises is calledaxiom

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.62

©Peter Müller 366

(10)

7. Programming Language Semantics 7.2 Big-step semantics

Notation

Updating States: σ[y 7→ v] is the function that

- overrides the association ofyinσ byy 7→v or - adds the new associationy7→vtoσ

(σ[y 7→ v])(x) =

( v if x =y σ(x) if x 6=y

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.63

©Peter Müller 367

7. Programming Language Semantics 7.2 Big-step semantics

Natural Semantics of IMP

skipdoes not modify the state hskip, σi →σ

x:=e assigns the value ofe to variablee hx:=e, σi →σ[x 7→ A[[e]]σ]

Sequential compositions1;s2

- First,s1is executed in stateσ, leading toσ0 - Thens2is executed in stateσ0

hs1, σi →σ0,hs2, σ0i →σ00 hs1;s2, σi → σ00

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.64

©Peter Müller 368

7. Programming Language Semantics 7.2 Big-step semantics

Natural Semantics of IMP (cont’d)

Conditional statement ifb then s1 elses2 end

- If bholds,s1is executed

- If bdoes not hold,s2 is executed

hs1, σi →σ0

hif b thens1 elses2 end, σi →σ0 ifB[[b]]σ =tt hs2, σi →σ0

hif b thens1 elses2 end, σi →σ0 ifB[[b]]σ =ff

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.65

©Peter Müller 369

7. Programming Language Semantics 7.2 Big-step semantics

Natural Semantics of IMP (cont’d)

Loop statementwhile b do s end

- Ifbholds,sis executed once, leading to stateσ0 - Then the whole while-statement is executed againσ0

hs, σi → σ0,hwhile b do s end, σ0i →σ00

hwhile b do s end, σi →σ00 if B[[b]]σ = tt

- Ifbdoes not hold, the while-statement does not modify the state

hwhile b dos end, σi → σ ifB[[b]]σ =ff

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.66

©Peter Müller 370

(11)

7. Programming Language Semantics 7.2 Big-step semantics

Rule Instantiations

Rules are actually rule schemes

- Meta-variables stand for arbitrary variables, expressions, statements, states, etc.

- To apply rules, they have to be instantiatedby selecting particular variables, expressions, statements, states, etc.

Assignment rule scheme

hx:=e, σi → σ[x7→ A[[e]]σ]

Assignment rule instance

hv:=v+1,{v 7→ 3}i → {v7→ 4}

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.67

©Peter Müller 371

7. Programming Language Semantics 7.2 Big-step semantics

Derivations: Example

What is the final state if statement

z:=x; x:=y; y:=z

is executed in state{x7→ 5,y 7→ 7,z 7→ 0}

(abbreviated by[5,7,0])?

hz:=x,[5,7,0]i →[5,7,5],hx:=y,[5,7,5]i →[7,7,5]

hz:=x; x:=y,[5,7,0]i →[7,7,5] ,

hy:=z,[7,7,5]i →[7,5,5]

hz:=x; x:=y; y:=z,[5,7,0]i →[7,5,5]

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.68

©Peter Müller 372

7. Programming Language Semantics 7.2 Big-step semantics

Derivation Trees

Rule instances can be combined to derive a transitionhs, σi →σ0

The result is a derivation tree

- The root is the transition hs, σi →σ0 - The leaves are axiom instances

- The internal nodes are conclusions of rule instances and have the corresponding premises as immediate children

The conditions of all instantiated rules must be satisfied

There can be several derivations for one transition (non-deterministic semantics)

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.69

©Peter Müller 373

7. Programming Language Semantics 7.2 Big-step semantics

Termination

The execution of a statements in stateσ

- terminatesiff there is a stateσ0 such thaths, σi →σ0 - loopsiff there is no stateσ0 such thaths, σi →σ0

A statements

- always terminatesif the execution in a stateσ terminates for all choices ofσ

- always loopsif the execution in a stateσ loops for all choices ofσ

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.70

©Peter Müller 374

(12)

7. Programming Language Semantics 7.2 Big-step semantics

Semantic Equivalence

Definition

Two statements s1 and s2 are semantically equivalent (denoted by s1 ≡ s2) if the follow- ing property holds for all states σ, σ0:

hs1, σi → σ0 ⇔ hs2, σi → σ0 Example

while b do s end

if b then s; while b do s end

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.72

©Peter Müller 375

7. Programming Language Semantics 7.2 Big-step semantics

Subsection 7.2.2

Formalization of big-step semantics

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 376

7. Programming Language Semantics 7.2 Big-step semantics

Big-step semantics in Isabelle/HOL

Approach

Formalize the abstract syntax of the language by a recursive datatype

Formalize the big-step semantics as an inductive predicate

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 377

7. Programming Language Semantics 7.2 Big-step semantics

Arithmetic expressions

datatype var = V nat (".v_" 90)

datatype aexp = Plus aexp aexp (infixr ".+." 30)

| Minus aexp aexp (infixr ".-." 30)

| Mult aexp aexp (infixr ".*." 50)

| Div aexp aexp (infixr "./." 50)

| Mod aexp aexp (infixr ".%." 50)

| Var var ("0_" 90)

| Const int ("0_" 90)

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 378

(13)

7. Programming Language Semantics 7.2 Big-step semantics

Boolean expressions

datatype bexp = Or bexp bexp (infixr ".|." 20)

| And bexp bexp (infixr ".&." 20)

| Not bexp (".!_" 80)

| Eq aexp aexp (infixr ".=." 10)

| Less aexp aexp (infixr ".<." 10)

| Leq aexp aexp (infixr ".<=." 10)

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 379

7. Programming Language Semantics 7.2 Big-step semantics

commands/statements

datatype com =

Skip ("SKIP")

| Assign var aexp ("_:==_" [60, 60] 20)

| Semi com com ("_; _" [60, 60] 10)

| Cond bexp com com ("IF _ THEN _ ELSE _ END" 60)

| While bexp com ("WHILE _ DO _ END" 60)

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 380

7. Programming Language Semantics 7.2 Big-step semantics

Evaluation of arithmetic expressions

type_synonym state = "var int"

primrec aeval :: "aexp state int" where

"aeval (Plus la ra) s = (( aeval la s) + (aeval ra s))"

| "aeval (Minus la ra) s = (( aeval la s) - (aeval ra s))"

| "aeval (Mult la ra) s = (( aeval la s) * (aeval ra s))"

| "aeval (Div la ra) s = (( aeval la s)div(aeval ra s))"

| "aeval (Mod la ra) s = (( aeval la s)mod(aeval ra s))"

| "aeval (Var v) s = (s v)"

| "aeval (Const i) s = i"

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 381

7. Programming Language Semantics 7.2 Big-step semantics

Evaluation of boolean expressions

primrec beval :: "bexp state bool" where

"beval (Or lb rb) s = (( beval lb s) (beval rb s))"

| "beval (And lb rb) s = (( beval lb s) (beval rb s))"

| "beval (Not be) s = (¬ (beval be s))"

| "beval (Eq la ra) s = (( aeval la s) = (aeval ra s))"

| "beval (Less la ra) s = (( aeval la s) < (aeval ra s))"

| "beval (Leq la ra) s = (( aeval la s) (aeval ra s))"

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 382

(14)

7. Programming Language Semantics 7.2 Big-step semantics

Operational semantics

inductive exec :: "state com state bool"

("_/ -_/ _" [50 ,0 ,50] 50) where Skip: "s -SKIP s"

| Assign: "s -(v:== ae) s(v:= aeval ae s)"

| Semi: "~ s0 -c1 s1; s1 -c2 s2 

= s0 -c1;c2 s2"

| IfT: "~ beval b s ; s -c1 t 

= s -IF b THEN c1 ELSE c2 END t"

| IfF: "~ ¬(beval b s); s -c2 t 

= s -IF b THEN c1 ELSE c2 END t"

| WhileF: "¬(beval b s) = s -WHILE b DO c END s"

| WhileT: "~ beval b s; s-ct; t -WHILE b DO c END u 

= s -WHILE b DO c END u"

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 383

7. Programming Language Semantics 7.2 Big-step semantics

Some properties

lemma [iff]: "(s -c;d u) = (t. s -c t t -d u)"

lemma [iff]: "(s -IF be THEN c ELSE d END t) = (s -if beval be s then c else d t)"

lemma unfold_while:

"(s -WHILE b DO c END u) =

(s -IF b THEN c; WHILE b DO c END ELSE SKIP END u)"

lemma while_rule:

"~ s -WHILE b DO c END t; P s;

s s ’. P s (beval b s) s -c s’ −→ P s’

= P t ∧ ¬(beval b t)"

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 384

7. Programming Language Semantics 7.2 Big-step semantics

Formalization of semantics and verification

Remarks

Inductive definition of the “semantics judgement” leads to a semantic predicate satisfying the least fixpoint of the semantical rules

The operational semantics can be directly used for program

verification (Why do we need a programming logic? (cf. Chapter 8))

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 385

7. Programming Language Semantics 7.3 Small-step semantics

Section 7.3

Small-step semantics

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 386

(15)

7. Programming Language Semantics 7.3 Small-step semantics

Overview

7.3.1 Small-step semantics of IMP 7.3.2 Proving properties of the semantics 7.3.3 Extensions of IMP

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 387

7. Programming Language Semantics 7.3 Small-step semantics

Subsection 7.3.1

Small-step semantics of IMP

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 388

7. Programming Language Semantics 7.3 Small-step semantics

Structural Operational Semantics

The emphasis is on the individual steps of the execution

- Execution of assignments - Execution of tests

Describing small steps of the execution allows one to express the order of execution of individual steps

- Interleaving computations

- Evaluation order for expressions (not shown in the course)

Describing always the next small step allows one to expressproperties of looping programs

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.100

©Peter Müller 389

7. Programming Language Semantics 7.3 Small-step semantics

Transitions in SOS

The configurations are the same as for natural semantics

The transition relation→1 can have two forms hs, σi →1 hs0, σ0i: the execution ofs from σ is not completedand the remaining computation is expressed by the intermediate configurationhs0, σ0i hs, σi →1 σ0: the execution of s from σ has

terminatedand the final state isσ0

A transitionhs, σi →1 γ describes the first stepof the execution ofs from σ

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.101

©Peter Müller 390

(16)

7. Programming Language Semantics 7.3 Small-step semantics

Transition System

Γ ={hs, σi | s ∈Stm, σ ∈ State} ∪State T =State

1⊆ {hs, σi | s ∈Stm, σ ∈State} ×Γ

We say that hs, σiis stuck if there is noγ such that hs, σi →1 γ

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.102

©Peter Müller 391

7. Programming Language Semantics 7.3 Small-step semantics

SOS of IMP

skipdoes not modify the state hskip, σi →1 σ

x:=e assigns the value ofe to variablex hx:=e, σi →1 σ[x 7→ A[[e]]σ]

skipand assignment require only one step Rules are analogous to natural semantics

hskip, σi →σ

hx:=e, σi →σ[x 7→ A[[e]]σ]

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.103

©Peter Müller 392

7. Programming Language Semantics 7.3 Small-step semantics

SOS of IMP: Sequential Composition

Sequential composition s1;s2

First step of executing s1;s2 is the first step of executing s1

s1 is executed in one step

hs1, σi →1 σ0 hs1;s2, σi →1 hs2, σ0i s1 is executed in several steps

hs1, σi →1 hs01, σ0i hs1;s2, σi →1 hs01;s2, σ0i

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.104

©Peter Müller 393

7. Programming Language Semantics 7.3 Small-step semantics

SOS of IMP: Conditional Statement

The first step of executingif b thens1 elses2 end is to determine the outcome of the test and thereby which branch to select

hif b thens1 elses2 end, σi →1 hs1, σi ifB[[b]]σ =tt hif b thens1 elses2 end, σi →1 hs2, σi ifB[[b]]σ = ff

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.105

©Peter Müller 394

(17)

7. Programming Language Semantics 7.3 Small-step semantics

Alternative for Conditional Statement

The first step of executingif b then s1 elses2 end is the first step of the branch determined by the outcome of the test

hs1, σi →1 σ0

hif b thens1 elses2 end, σi →1 σ0 if B[[b]]σ =tt hs1, σi →1 hs01, σ0i

hif b thens1 elses2 end, σi →1 hs01, σ0i if B[[b]]σ =tt

and two similar rules forB[[b]]σ =ff

Alternatives are equivalent for IMP

Choice is important for languages with parallel execution

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.106

©Peter Müller 395

7. Programming Language Semantics 7.3 Small-step semantics

SOS of IMP: Loop Statement

The first step is to unrole the loop hwhile b do s end, σi →1

hif b thens;whileb do s end else skip end, σi

Recall thatwhile b do s end and

ifb then s;while b dos end else skip end are semantically equivalent in the natural semantics

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.107

©Peter Müller 396

7. Programming Language Semantics 7.3 Small-step semantics

Alternatives for Loop Statement

The first step is to decide the outcome of the test and thereby whether to unrole the body of the loop or to terminate

hwhile b do s end, σi →1 hs;while b do s end, σi

if B[[b]]σ =tt

hwhile b do s end, σi →1 σ if B[[b]]σ =ff Or combine with the alternative semantics of the conditional statement

Alternatives are equivalent for IMP

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.108

©Peter Müller 397

7. Programming Language Semantics 7.3 Small-step semantics

Derivation Sequences

Aderivation sequence of a statements starting in stateσ is a sequence γ0, γ1, γ2, . . . , where

- γ0 =hs, σi

- γi 1γi+1 for0i

A derivation sequence is eitherfinite orinfinite

- Finite derivation sequences end with a configuration that is either a terminal configuration or a stuck configuration

Notation

- γ0 i1γiindicates that there areisteps in the execution fromγ0toγi

- γ0 1γiindicates that there is afinite number of stepsin the execution fromγ0toγi

- γ0 i1γiandγ01 γi neednotbe derivation sequences

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.109

©Peter Müller 398

(18)

7. Programming Language Semantics 7.3 Small-step semantics

Derivation Sequences: Example

What is the final state if statement

z:=x; x:=y; y:=z

is executed in state{x7→ 5,y7→ 7,z7→ 0}?

hz:=x; x:=y; y:=z,{x7→5,y7→7,z7→0}i

1hx:=y; y:=z,{x7→5,y7→7,z7→5}i

1hy:=z,{x7→7,y7→7,z7→5}i

1{x7→7,y7→5,z7→5}

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.110

©Peter Müller 399

7. Programming Language Semantics 7.3 Small-step semantics

Derivation Trees

Derivation trees explain why transitions take place For the first step

hz:=x; x:=y; y:=z, σi →1hx:=y; y:=z, σ[z7→5]i

the derivation tree is

hz:=x, σi →1 σ[z7→5]

hz:=x; x:=y, σi →1hx:=y, σ[z7→5]i

hz:=x; x:=y; y:=z, σi →1hx:=y; y:=z, σ[z7→5]i

z:=x; ( x:=y; y:=z )would lead to a simpler tree with only one rule application

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.111

©Peter Müller 400

7. Programming Language Semantics 7.3 Small-step semantics

Derivation Sequences and Trees

Natural (big-step) semantics

- The execution of a statement (sequence) is described by one big transition

- The big transition can be seen as trivial derivation sequence with exactly one transition

- The derivation tree explains why this transition takes place

Structural operational (small-step) semantics

- The execution of a statement (sequence) is described by one or more transitions

- Derivation sequences are important

- Derivation trees justify each individual step in a derivation sequence

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.112

©Peter Müller 401

7. Programming Language Semantics 7.3 Small-step semantics

Termination

The execution of a statements in stateσ

- terminatesiff there is a finite derivation sequence starting withhs, σi

- loopsiff there is an infinite derivation sequence starting withhs, σi

The execution of a statements in stateσ

- terminates successfullyif hs, σi →1σ0

- In IMP, an execution terminates successfully iff it terminates (no stuck configurations)

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.113

©Peter Müller 402

(19)

7. Programming Language Semantics 7.3 Small-step semantics

Subsection 7.3.2

Proving properties of the semantics

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 403

7. Programming Language Semantics 7.3 Small-step semantics

Induction on Derivations

Induction on the length of derivation sequences

1. Induction base: Prove that the property holds for all derivation sequences of length0

2. Induction step: Prove that the property holds for all other derivation sequences:

Induction hypothesis: Assume that the property holds for all derivation sequences of length at mostk

Prove that it also holds for derivation sequences of length k+ 1

Induction on the length of derivation sequences is an ap- plication of strong mathematical induction.

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.115

©Peter Müller 404

7. Programming Language Semantics 7.3 Small-step semantics

Using Induction on Derivations

The induction step is often done by inspecting either

- the structure of the syntactic element or

- the derivation tree validating the first transition of the derivation sequence

Lemma

hs1;s2, σi →k1 σ00

∃σ0, k1, k2 : hs1, σi →k11 σ0∧ hs2, σ0i →k12 σ00∧ k1 +k2 =k

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.116

©Peter Müller 405

7. Programming Language Semantics 7.3 Small-step semantics

Proof

Proof by induction onk, that is, by induction on the length of the derivation sequence for

hs1;s2, σi →k1 σ00

Induction base: k = 0: There is no derivation sequence of length0for hs1;s2, σi →k1 σ00 Induction step

- We assume that the lemma holds forkm - We prove that the lemma holds form+ 1 - The derivation sequence

hs1;s2, σi →m+11 σ00can be written as

hs1;s2, σi →1 γm1 σ00for some configurationγ

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.117

©Peter Müller 406

(20)

7. Programming Language Semantics 7.3 Small-step semantics

Induction Step

hs1;s2, σi →1 γ →m1 σ00

Consider the two rules that could lead to the transitionhs1;s2, σi →1 γ

Case 1

hs1, σi →1 σ0 hs1;s2, σi →1 hs2, σ0i Case 2

hs1, σi →1 hs01, σ0i hs1;s2, σi →1 hs01;s2, σ0i

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.118

©Peter Müller 407

7. Programming Language Semantics 7.3 Small-step semantics

Induction Step: Case 1

From

hs1;s2, σi →1 γ →m1 σ00 andhs1;s2, σi →1 hs2, σ0i we concludehs2, σ0i →m1 σ00

The required result follows by choosingk1 = 1 and k2 =m

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.119

©Peter Müller 408

7. Programming Language Semantics 7.3 Small-step semantics

Induction Step: Case 2

From

hs1;s2, σi →1 γ →m1 σ00and hs1;s2, σi →1 hs01;s2, σ0i we concludehs01;s2, σ0i →m1 σ00

By applying the induction hypothesis, we get

∃σ0, l1, l2 : hs01, σ0i →l11 σ0∧hs2, σ0i →l12 σ00∧l1+l2 =m From

hs1, σi →1 hs01, σ0iand hs01, σ0i →l11 σ0

we get hs1, σi →l11+1 σ0 By

hs2, σ0i →l12 σ00 and(l1+ 1) +l2 =m+ 1 we have proved the required result

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.120

©Peter Müller 409

7. Programming Language Semantics 7.3 Small-step semantics

Semantic Equivalence

Two statementss1 ands2 aresemantically equivalent if for all statesσ:

hs1, σi →1 γ iff hs2, σi →1 γ, wheneverγ is a configuration that is either stuck or terminal, and there is an infinite derivation sequence starting in hs1, σiiff there is one starting inhs2, σi

Note: In the first case, the length of the two derivation sequences may be different

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.121

©Peter Müller 410

(21)

7. Programming Language Semantics 7.3 Small-step semantics

Determinism

Lemma: The structural operational semantics of IMP is deterministic. That is, for alls,σ,γ, and γ0 we have that

hs, σi →1 γ ∧ hs, σi →1 γ0 ⇒ γ =γ0 The proof runs by induction on the shape of the derivation tree for the transition hs, σi →1 γ

Corollary: There is exactly one derivation sequence starting in configurationhs, σi

The proof runs by induction on the length of the derivation sequence

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.122

©Peter Müller 411

7. Programming Language Semantics 7.3 Small-step semantics

Subsection 7.3.3

Extensions of IMP

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 412

7. Programming Language Semantics 7.3 Small-step semantics

Extensions of IMP

Local variable declarations

Statement “abort”

Non-determinism

Parallelism

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 413

7. Programming Language Semantics 7.3 Small-step semantics

Local Variable Declarations

Local variable declarationvar x:=e in s end The small steps are

1. Assignetox 2. Executes

3. Restore the initial value ofx

(necessary ifxexists in the enclosing scope)

Problem: There is no history of states that could be used to restore the value ofx

Idea: Represent states as execution stacks

Peter M ¨uller—Semantics of Programming Languages, SS04 – p.124

©Peter Müller 414

Referenzen

ÄHNLICHE DOKUMENTE

The Formal Semantics of Programming Languages: An Introduction.. •

In this exercise we return to our small While-language based on the language IMP introduced in the lec- ture. In particular, we want to create and show properties about an

I Axiomatic semantics tries to give a meaning of a programming construct by giving proof rules. This means we have to put them into relation with each other, and show that

The Tape Control presents the data to the One Buffer of the Data Control for transfer to computer core memory through the High Speed Channel Control.. Data transfers

Komp lexi thats

cjioöoßa, ist aber vor 8 Jahren auch bebaut worden, wobei die Grabsteine verschleppt wurden. Hier war auch Pastor Glück, Pflegevater Katharinas I., bestattet. In

a) Client/server communication can cause problems if messages are lost during transmission between client and server... The handling of such errors can be different, resulting

This way, Clara allows researchers from two communities to integrate their approaches with each other.We have pre- sented the syntax and semantics of Dependency State Machines and