Introduction to Structural Operational Semantics Patricia ILL School of Computing University of Leeds United Kingdom Roberto ANARA Department of Mathematics University of Parma Italy Copyright c 200 Patricia ill Distributed under the terms of the NU Free Documentation License 12 1
SYNTAX AND SEMANTICS Syntax is concerned with the form of expressions that are allowed in a language Semantics describes what should be the result of executing or evaluating the program Static semantics describes what kinds of syntactically correct expressions are executable Dynamic semantics describes the run-time behaviour including the expected results YNTAX AND SEMANTICS 2
APPROACES TO SEMANTICS Operational Semantics: The meaning of a program in terms of how it executes on an abstract machine This course will follow the approach called Structural Operational Semantics SOS) proposed by Plotkin Useful for modelling the execution behaviour of a program Denotational Semantics: The mathematical meaning of a program and models the expected result rather than how the result might be obtained This approach was first proposed by C Strachey and later formalised by D Scott Useful for understanding the internal logic of a program PPROACES TO SEMANTICS 3
APPROACES TO SEMANTICS CONT Axiomatic Semantics: Provides correctness assertions for each program construct The approach was developed by RW Floyd and CAR oare Useful for verying that a program s computed results are correct with respect to the specication PPROACES TO SEMANTICS CONT
IMP: AN IMPERATIVE LANUAE Imp is a simple imperative language for elementary arithmetic with just a conditional command and a while loop command see Chapter 2 of Winskel) We present here: the abstract syntax ie the sentences to which we will associate a semantics) the operational semantics of expressions ie the rules governing their evaluation the operational semantics of commands ie the rules governing their evaluation execution) P: AN IMPERATIVE LANUAE
) * %& ' %& ' $ " " The basic syntactic sets are: IMP: TE ASTRACT SYNTAX Integers: ooleans: Variables: Arithmetic expressions: oolean expressions: Commands: + We shall regard the variables as locations The )and are called syntactic metavariables: they denote a generic element of the respective syntactic category P: TE ASTRACT SYNTAX 6
IMP: TE LANUAE FORMATION RULES $ %& ' : - - - - 0 %& ' : - - 1-2 - - * +: ) skip - - ) 3 ) - )then else ) " - while )do P: TE LANUAE FORMATION RULES
1 1 * + : %& ' %& ' $ 3 6 8 1< 6 8 0 IMP: EXAMPLES OF SYNTACTICALLY CORRECT SENTENCES while do skip else P: EXAMPLES OF SYNTACTICALLY CORRECT SENTENCES 8
@ >?% A EXECUTION STORES A store maps each variable location) to a value In the language Imp these values must be numbers Let DC F @ C A E Example: Let F so that The store can and will) be more compactly denoted by XECUTION STORES
I I J 0 ) CONFIURATIONS A non-terminal configuration is a pair of the form or boolean expression denote the situation of an arithmetic expression waiting for evaluation using the store or a : ) J I using the store : denotes the situation of a command ) waiting for execution I J skip J ONFIURATIONS 10
Kin a store TRANSITION RULES Structural operational semantics provides transition rules for the evaluation of expressions and execution of commands A transition for an arithmetic expression forms: has one of the K K KML L A transition rule is written as premise conclusion RANSITION RULES 11
EVALUATION OF ARITMETIC EXPRESSIONS 3 3 3 0 VALUATION OF ARITMETIC EXPRESSIONS 12
N EVALUATION OF ARITMETIC EXPRS: EXAMPLES Let Then and Using the rules we write: and then VALUATION OF ARITMETIC EXPRS: EXAMPLES 13
0 EVALUATION OF ARITMETIC EXPRS: EXAMPLES CONT The rules for evaluating arithmetic expressions can be combined to form a derivation tree For example consider the expression with We obtain the derivation tree 0 O 0 VALUATION OF ARITMETIC EXPRS: EXAMPLES CONT 1
EVALUATION OF OOLEAN EXPRESSIONS I Truth values: Equality: 3 P VALUATION OF OOLEAN EXPRESSIONS I 1
EVALUATION OF OOLEAN EXPRESSIONS II Inequality: 1 1 3 RQ 1 VALUATION OF OOLEAN EXPRESSIONS II 16
EVALUATION OF OOLEAN EXPRESSIONS III Negation: 3 Conjunction: TS 2 Disjunction: VU VALUATION OF OOLEAN EXPRESSIONS III 1
EVALUATION OF EXPRESSIONS: EXAMPLES The rules for evaluating arithmetic expressions can be combined For example consider the expression 2 with 2 VALUATION OF EXPRESSIONS: EXAMPLES 18
ZY \ ^ ZY \ 3 [ \ >?% ZY ^ XW []\ >?% ZY XW EQUIVALENCE OF EXPRESSIONS Two expressions are equivalent they evaluate to the same value in any given store: QUIVALENCE OF EXPRESSIONS 1
2 SORT-CIRCUIT EVALUATION OF OOLEAN EXPRESSIONS Ever heard of short-circuit evaluation? It is a mechanism used by C C ++ and many other programming languages but not by IMP whereby the evaluation of boolean expressions is stopped as soon as the overall value of the expression is known If is true is always true independently from Short-circuit evaluation means that in this case is not evaluated The case for is dual: is false evaluating avoided can be Exercise: Define a variation of IMP that uses short-circuit evaluation of boolean expressions Exercise: Can something similar be done for arithmetic expressions? ow? ORT-CIRCUIT EVALUATION OF OOLEAN EXPRESSIONS 20
except for the location 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 the operational semantics of a program Example: L where is the same as the store has the value L which XECUTION OF COMMANDS: INTRODUCTION 21
d c ` a b a EXECUTION OF COMMANDS: NOTATION Let each : `_be a store Then the store is defined as follows for _ P Then we can write L _ a ` It is assumed that in the initial state of the store all the variables have a value Suppose is the initial store then L L XECUTION OF COMMANDS: NOTATION 22
3 ) ` ) L ) L L ) L L L _ a skip EVALUATION OF COMMANDS: RULES I Skip operation: Assignment: Command sequence: VALUATION OF COMMANDS: RULES I 23
)then )then )do )do )do 3 ) ) 3 while ) while L L L L L while ) L ) L EVALUATION OF COMMANDS: RULES II Conditional: While-loop: VALUATION OF COMMANDS: RULES II 2 L else L else L
EVALUATION OF COMMANDS: AN EXAMPLE For example with consider the command while do _ ` a VALUATION OF COMMANDS: AN EXAMPLE 2
AN EXAMPLE CONT Let So we can continue the derivation: while do e gf h i kj while do N EXAMPLE CONT 26
) EQUIVALENCE OF COMMANDS Two commands are equivalent when executed starting from the same store they evaluate to the same store W [ \ L ) L ) ^ L ) ) ZY ) ml ) ml >?% ZY Exercise: Let while while Kdo Kdo n Is XW )? QUIVALENCE OF COMMANDS 2