27
Introduction to Structural Operational Semantics Patricia H ILL School of Computing University of Leeds, United Kingdom Roberto B AGNARA Department of Mathematics University of Parma, Italy Copyright c 2004 Patricia Hill. Distributed under the terms of the GNU Free Documentation License 1.2 1

Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

  • Upload
    others

  • View
    30

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

Introduction to StructuralOperational Semantics

Patricia HILL

School of ComputingUniversity of Leeds, United Kingdom

Roberto BAGNARA

Department of MathematicsUniversity of Parma, Italy

Copyright c� 2004 Patricia Hill. Distributed under the terms of the GNU Free Documentation License 1.2 1

Page 2: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

SYNTAX AND SEMANTICS

� Syntax is concerned with the form of expressions that areallowed in a language.

� Semantics describes what should be the result of executing orevaluating the program.

� Static semantics describes what kinds of syntactically correctexpressions are executable.

� Dynamic semantics describes the run-time behaviour includingthe expected results.

SYNTAX AND SEMANTICS 2

Page 3: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

APPROACHES TO SEMANTICS

� Operational Semantics: The meaning of a program in terms ofhow it executes on an abstract machine. This course will followthe approach called “Structural Operational Semantics” (SOS),proposed by G. Plotkin.

Useful for modelling the execution behaviour of a program.

� Denotational Semantics: The mathematical meaning of aprogram and models the expected result rather than how theresult might be obtained. This approach was first proposed byC. Strachey and later formalised by D. Scott.

Useful for understanding the internal logic of a program.

APPROACHES TO SEMANTICS 3

Page 4: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

APPROACHES TO SEMANTICS, CONT.

� Axiomatic Semantics: Provides correctness assertions for eachprogram construct. The approach was developed by R.W. Floydand C.A.R. Hoare.

Useful for verifying that a program’s computed results are correctwith respect to the specification.

APPROACHES TO SEMANTICS, CONT. 4

Page 5: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

IMP: AN IMPERATIVE LANGUAGE

Imp is a simple imperative language for elementary arithmetic withjust a conditional command and a while loop command (see Chapter2 of Winskel). We present here:

� the abstract syntax (i.e., the sentences to which we will associatea semantics);

� the operational semantics of expressions, i.e., the rulesgoverning their evaluation;

� the operational semantics of commands, i.e., the rules governingtheir evaluation (execution).

IMP: AN IMPERATIVE LANGUAGE 5

Page 6: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

IMP: THE ABSTRACT SYNTAX

The basic syntactic sets are:

Integers: � �� � ��� � �� ��� � �� � ��� � � � � � � ;

Booleans: � �� � �� � � � � � ��� � ;

Variables: � �� � � � � � ! � � " � � ;

Arithmetic expressions: # �$ %& ' ;

Boolean expressions: ( �� %& ' ;

Commands: ) �* �+ .

We shall regard the variables � ! , � " , . . . as locations.

The � , � , � , # , ( and ) are called syntactic metavariables: theydenote a generic element of the respective syntactic category.

IMP: THE ABSTRACT SYNTAX 6

Page 7: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

IMP: THE LANGUAGE FORMATION RULES

$ %& ' :

# , , � � - � - # . / # ! - # . � # ! - # . 0 # !

� %& ' :

( , , � � - # . � # ! - # . 1 # ! - ( . � � 2 ( ! - ( . � ( ! - � � � ( !

* �+ :

) , , � skip - � , � # - ) . 3 ) ! - if ( then ) ! else ) " - while ( do )

IMP: THE LANGUAGE FORMATION RULES 7

Page 8: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

IMP: EXAMPLES OF SYNTACTICALLY CORRECT SENTENCES

$ %& ' ,465 / � 7 � 8 / 4 � 0 9 7

� %& ' ,� : �

465 / � 7 � 8 1<;

4 � � � � � 5 7 � 9 � �

* �+ ,� , � � 3 9 , � �

while � 1 � � � do � , � � / �if � 1 9 skip else 9 , � �

IMP: EXAMPLES OF SYNTACTICALLY CORRECT SENTENCES 8

Page 9: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EXECUTION STORES

A store = maps each variable (location) to a value.

In the language Imp, these values must be numbers. Let> � � %? � � @ = , A B � � �DCC A E � � F

Example: Let= � @ 4 � � 5 7 � 4 9 � � 7 F �

so that= 4 � 7 � 5

= 4 9 7 � �

The store = can (and will) be more compactly denoted by

� � � 5 � 9 � � �

EXECUTION STORES 9

Page 10: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

CONFIGURATIONS

A non-terminal configuration is a pair of the form G # � = H , G ( � = H , or

G ) � = H .

G # � = H , G ( � = H denote the situation of an arithmetic expression # , or aboolean expression ( “waiting for evaluation” using the store = :

I � / � 0 9 � � � � 5 � 9 � ; � J �

I � � 9 / � � � � � 5 � 9 � ; � J

G ) � = H denotes the situation of a command ) “waiting for execution”using the store = :

I � , � � / � � � � � 5 � 9 � ; � J �

I skip � � � � 5 � 9 � ; � J

CONFIGURATIONS 10

Page 11: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

TRANSITION RULES

Structural operational semantics provides transition rules for theevaluation of expressions and execution of commands.

A transition for an arithmetic expression K in a store = has one of theforms:

G K � = H B G KML � = L H �

G K � = H B � A transition rule is written as

premise

conclusion

TRANSITION RULES 11

Page 12: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF ARITHMETIC EXPRESSIONS

� �� � � ;

G � � = H B �

= 4 � 7 � � � �� �

G � � = H B � 3

G # . � = H B � . G # ! � = H B � !

if � � � . / � !

G # . / # ! � = H B � 3

G # . � = H B � . G # ! � = H B � !if � � � . � � !

G # . � # ! � = H B � 3

G # . � = H B � . G # ! � = H B � !

if � � � . � !

G # . 0 # ! � = H B � EVALUATION OF ARITHMETIC EXPRESSIONS 12

Page 13: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF ARITHMETIC EXPRS: EXAMPLES

Let

= � � � � 5 � 9 � ; �

Then = 4 � 7 � 5 and = 4 9 7 � ; .

Using the rules we write:

= 4 � 7 � 5

G � � = H B 5

= 4 9 7 � ;

G 9 � = H B ;

and then

G � � = H B 5 G 9 � = H B ;

G � / 9 � = H B N

EVALUATION OF ARITHMETIC EXPRS: EXAMPLES 13

Page 14: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF ARITHMETIC EXPRS: EXAMPLES, CONT.

The rules for evaluating arithmetic expressions can be combined toform a derivation tree. For example, consider the expression

� / � 0 9

with= � � � � 5 � 9 � ; ��

We obtain the derivation tree

= 4 � 7 � 5

G � � = H B 5

G � � = H B �= 4 9 7 � ;

G 9 � = H B ;

G � 0 9 � = H B O

G � / � 0 9 � = H B � �

EVALUATION OF ARITHMETIC EXPRS: EXAMPLES, CONT. 14

Page 15: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF BOOLEAN EXPRESSIONS I

Truth values:

G � � � = H B � � G� � = H B �

Equality:

G # . � = H B � . G # ! � = H B � !

if � . � � !

G # . � # ! � = H B � � 3

G # . � = H B � . G # ! � = H B � !if � . P� � !

G # . � # ! � = H B � EVALUATION OF BOOLEAN EXPRESSIONS I 15

Page 16: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF BOOLEAN EXPRESSIONS II

Inequality:

G # . � = H B � . G # ! � = H B � !

if � . 1 � !

G # . 1 # ! � = H B � � 3

G # . � = H B � . G # ! � = H B � !

if � .RQ � !

G # . 1 # ! � = H B � EVALUATION OF BOOLEAN EXPRESSIONS II 16

Page 17: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF BOOLEAN EXPRESSIONS III

Negation:

G ( � = H B � �

G � � � ( � = H B � 3 G ( � = H B �

G � � � ( � = H B � �

Conjunction:

G ( . � = H B � . G ( ! � = H B � !

if � � � .TS � !

G ( . � � 2 ( ! � = H B �

Disjunction:

G ( . � = H B � . G ( ! � = H B � !

if � � � .VU � !

G ( . � ( ! � = H B � EVALUATION OF BOOLEAN EXPRESSIONS III 17

Page 18: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF EXPRESSIONS: EXAMPLES

The rules for evaluating arithmetic expressions can be combined.For example, consider the expression

� � 5 � � 2 � � � 9 � �

with= � � � � 5 � 9 � ; ��

= 4 � 7 � 5

G � � = H B 5 G5 � = H B 5

G � � 5 � = H B � �

= 4 9 7 � ;G 9 � = H B ; G � � = H B �

G 9 � � � = H B �

G � � � 9 � � � = H B � �

G � � 5 � � 2 � � � 9 � � � = H B � �

EVALUATION OF EXPRESSIONS: EXAMPLES 18

Page 19: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EQUIVALENCE OF EXPRESSIONS

Two expressions are equivalent if they evaluate to the same value inany given store:

# .XW # !� YZ []\ � � � � � , \ = � > � � %? , G # . � = H B � Y Z G # ! � = H B � ^ 3

( .XW ( !� YZ [ \ � �� � �� , \ = � > � � %? , G ( . � = H B � Y Z G ( ! � = H B � ^

EQUIVALENCE OF EXPRESSIONS 19

Page 20: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

SHORT-CIRCUIT EVALUATION OF BOOLEAN EXPRESSIONS

Ever heard of short-circuit evaluation?

It is a mechanism, used by C, C++ and many other programminglanguages —but not by IMP— whereby the evaluation of booleanexpressions is stopped as soon as the overall value of theexpression is known.

If ( . is true, ( . � ( ! is always true, independently from ( ! .Short-circuit evaluation means that, in this case, ( ! is not evaluated.The case for ( . � � 2 ( ! is dual: if ( . is false, evaluating ( ! can beavoided.

Exercise: Define a variation of IMP that uses short-circuit evaluationof boolean expressions.

Exercise: Can something similar be done for arithmeticexpressions? How?

SHORT-CIRCUIT EVALUATION OF BOOLEAN EXPRESSIONS 20

Page 21: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EXECUTION OF COMMANDS: INTRODUCTION

The execution of a command may change the values of the store.Transition relations and transition systems are used to define theoperational semantics of a program.

Example:

G � , � � � � = H B = Lwhere = L is the same as the store = except for the location � whichhas the value � � .

EXECUTION OF COMMANDS: INTRODUCTION 21

Page 22: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EXECUTION OF COMMANDS: NOTATION

Let = be a store. Then the store = _ � ` � a is defined as follows, foreach 9 �� � :

= _ � ` � a 4 9 7� �b

c d� � if 9 � � ;

= 4 9 7 � if 9 P� � .

Then we can write

G � , � � � � = H B = L � = _ � � ` � a

It is assumed that in the initial state of the store, all the variableshave a value� .

Suppose = is the initial store, then

= 4 � 7 � � � = 4 9 7 � � �

= L 4 � 7 � � � � = L 4 9 7 � �

EXECUTION OF COMMANDS: NOTATION 22

Page 23: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF COMMANDS: RULES I

Skip operation:

G skip � = H B =

Assignment:

G # � = H B �

G � , � # � = H B = _ � ` � a

Command sequence:

G ) . � = H B = L L G ) ! � = L L H B = L

G ) . 3 ) ! � = H B = L

EVALUATION OF COMMANDS: RULES I 23

Page 24: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF COMMANDS: RULES II

Conditional:

G ( � = H B � � G ) . � = H B = L

G if ( then ) . else ) ! � = H B = L 3

G ( � = H B � G ) ! � = H B = L

G if ( then ) . else ) ! � = H B = L

While-loop:G ( � = H B �

G while ( do ) � = H B = 3

G ( � = H B � � G ) � = H B = L L G while ( do ) � = L L H B = L

G while ( do ) � = H B = L EVALUATION OF COMMANDS: RULES II 24

Page 25: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EVALUATION OF COMMANDS: AN EXAMPLE

For example, with = . � � � � � � , consider the command

while � � � 4 � � � 7 do 4 � , � � / � 7

= . 4 � 7 � �

G � � = . H B � G � � = . H B �

G � � � � = . H B �

G � � � 4 � � � 7 � = . H B � �

= . 4 � 7 � �

G � � = . H B � G � � = . H B �

G � / � � = . H B �

G � , � � / � � = . H B = . _ � ` � a � � � � � �

EVALUATION OF COMMANDS: AN EXAMPLE 25

Page 26: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

AN EXAMPLE, CONT.

Let = ! � � � � � � . So we can continue the derivation:

= ! 4 � 7 � �

G � � = ! H B = ! 4 � 7 � � G � � = ! H B �

G � � � � = ! H B � �

G � � � 4 � � � 7 � = ! H B �

G while � � � 4 � � � 7 do 4 � , � � / � 7e fg hi

� = ! � H B = !

G � � � 4 � � � 7 � = . H B � � G � , � � / � � = . H B = ! Gkj � = ! � H B = !

G while � � � 4 � � � 7 do 4 � , � � / � 7 � = . � H B = !AN EXAMPLE, CONT. 26

Page 27: Introduction to Structural Operational Semantics · APPROACHES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine. This

EQUIVALENCE OF COMMANDS

Two commands are equivalent if, when executed starting from thesame store, they evaluate to the same store.

) . W ) ! � YZ [ \ = � = L � > � � % ? , G ) . � = H B = L Y Z G ) ! � = H B = L ^

Exercise: Let

) . � while �lm K do � , � � �

) ! � while �lm K do � , � n Is ) .XW ) ! ?

EQUIVALENCE OF COMMANDS 27