semantics with applications mooly sagiv msagiv@post schrirber 317 03-640-7606...
Post on 21-Dec-2015
221 views
TRANSCRIPT
![Page 1: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/1.jpg)
Semantics with Applications Mooly Sagivmsagiv@postSchrirber 317 03-640-7606
html://www.cs.tau.ac.il/~msagiv/courses/sem08.html
Textbooks:Winskel The Formal Semantics of Programming Languages Types and Programming Languages Benjamin C. Pierce
![Page 2: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/2.jpg)
Outline• Course requirements• What is semantics• Who needs semantics• Forms of semantics• Tentative Plan• Trace semantics• Introduction to operational semantics
![Page 3: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/3.jpg)
Course Requirements• Prerequisites
– Compiler Course– Basic set theory and logic
• A theoretical course– Forms of induction– Domain theory– No algorithms
• Grade– Course Notes 10%– Assignments 60%
• Mostly theoretical with some programming
– Home exam 30%
![Page 4: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/4.jpg)
Modern Programming Languages• Imperative
– PL/1– Pascal– C
• Object Oriented– C++– Java– C#
• Functional– Scheme– ML– Ocaml– F#– Haskel
• Logic– Prolog
![Page 5: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/5.jpg)
Programming Languages• Syntax
– Which string is a legal program?
– Usually defined using context free grammar+ contextual constraints
• Semantics– What does a program mean?
– What is the output of the program on a given run?
– When does a runtime error occur?
– A formal definition
![Page 6: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/6.jpg)
Benefits of Formal Semantics• Programming language design
– hard-to-define= hard-to-implement=hard-to-use– Avoid design mistakes
• Programming language implementation– Compiler Correctness
• Correctness of program optimizations• Design of Static Analysis
• Programming language understanding• Program correctness
– Type checking
• Program equivalence• Automatic generation of interpreter• Techniques used in software engineering
![Page 7: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/7.jpg)
Desired Features of PL Semantics
• Tractable– as simple as possible without losing the ability to express
behavior accurately
• Abstract– uncluttered by irrelevant detail
• Computational– an accurate abstraction from runtime behavior
• Compositional– The meaning of compound language construct is defined using
the meaning of subconstructs– Supports modular reasoning
![Page 8: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/8.jpg)
Alternative Formal Semantics• Operational Semantics [Plotkin, Kahn]
– The meaning of the program is described “operationally”
– Trace based Semantics
– Structural Operational Semantics
– Natural Semantics
• Denotational Semantics [Strachey, Scott]– The meaning of the program is an input/output relation
• Axiomatic Semantics [Floyd, Hoare]– The meaning of the program is observed properties
– Proof rules to show that the program is correct
• Complement each other
![Page 9: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/9.jpg)
Tentative Plan• A simple programming language IMP
– Natural Semantics of IMP– Structural operational Semantics of IMP– Denotational Semantics of IMP
• Axiomatic Semantics– IMP– Non-Determinism and Parallelism– Rely Guarantee Axiomatic Semantics– Separation Logic
• Type inference/checking
![Page 10: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/10.jpg)
IMP: A Simple Imperative Language
• numbers N– Positive and negative numbers– n, m N
• truth values T={true, false}• locations Loc
– X, Y Loc• arithmetic Aexp
– a Aexp• boolean expressions Bexp
– b Bexp• commands Com
– c Com
![Page 11: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/11.jpg)
(3+5 ) 3 + 5
3 + 5 5+ 3
Abstract Syntax for IMP• Aexp
– a ::= n | X | a0 + a1 | a0 – a1 | a0 a1
• Bexp– b ::= true | false | a0 = a1 | a0 a1 | b | b0 b1
| b0 b1
• Com– c ::= skip | X := a | c0 ; c1 | if b then c0 else c1
| while b do c
2+34-5
(2+(34))-5 ((2+3)4))-5
![Page 12: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/12.jpg)
Example Program
Y := 1;
while (X=1) do
Y := Y * X;
X := X - 1
![Page 13: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/13.jpg)
But what about semantics
![Page 14: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/14.jpg)
Trace Based Semantics
• For every program P define a set potential states (P)
• Let be the set of finite and infinite traces over = (P)* (P)
• The meaning of P is a set of maximal traces P
![Page 15: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/15.jpg)
Example Program
1: while 2:(X>0) do
3:X := X – 1
4:
[pc1, x 2]
[pc2, x 2]
[pc3, x 2]
[pc2, x 1]
[pc3, x 1]
[pc2, x 0]
[pc4, x 0]
[pc1, x -7]
[pc2, x -7]
[pc4, x -7] ..
![Page 16: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/16.jpg)
Example Program
1: while 2:(true) do
3: skip
4:
[pc1, x 2]
[pc2, x 2]
[pc3, x 2][pc2, x 2][pc3, x 2][pc2, x 2][pc3, x 2]
..
![Page 17: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/17.jpg)
Limitations of trace based semantics
• The program counter is an implementation detail
• Equivalent programs do not necessarily have the same set of traces
• Hard to define semantics by induction on the syntax
• Hard to prove properties of the programming language
![Page 18: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/18.jpg)
Chapter 2
Introduction to
Operational Semantics
![Page 19: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/19.jpg)
Expression Evaluation • States
– Mapping locations to values - The set of states
: Loc N(X)= X=value of X in = [ X 5, Y 7]
– The value of X is 5– The value of Y is 7– The value of Z is undefined
– For a Exp, , n N,• <a, > n
– a is evaluated in to n
![Page 20: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/20.jpg)
Evaluating (a0 + a1) at • Evaluate a0 to get a number n0 at
• Evaluate a1 to get a number n1 at
• Add n0 and n1
![Page 21: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/21.jpg)
Expression Evaluation Rules
• Numbers– <n, > n
• Locations– <X, > (X)
• Sums
• Subtractions
• Products
10,10
1,1,0,0nnnwhere
naa
nana
10,10
1,1,0,0nnnwhere
naa
nana
10,10
1,1,0,0nnnwhere
naa
nana
Axiom
s
![Page 22: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/22.jpg)
Derivations• A rule instance
– Instantiating meta variables with corresponding values
632
3322
,
0,,0,
1232
4332
,
0,,0,
![Page 23: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/23.jpg)
Derivation (Tree)
• Axioms in the leafs• Rule instances at
internal nodes
0Init 0 , 55 0 , 77 0 , 99 0 ,
55)Init( 0 , 1697 0 ,
219)(75)Init( 0 ,
![Page 24: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/24.jpg)
Computing a derivation
• We write <a, > n when there exists a derivation tree whose root is <a, > n
• Can be computed in a top-down manner• At every node try all derivations “in parallel”
0Init 0 , 55 0 , 77 0 , 99 0 ,
?5)Init( 0 , ?97 0 ,
?9)(75)Init( 0 ,
5 16
21
![Page 25: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/25.jpg)
Recap
• Operational Semantics– The rules can be implemented easily– Define interpreter
• Natural semantics
![Page 26: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/26.jpg)
Equivalence of IMP expressions
a0 a1 iff
nanaNn ,,. 10
![Page 27: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/27.jpg)
Boolean Expression Evaluation Rules• <true, > true• <false, > false
•
•
•
•
mnifaa
mana
true
,10
,1,,0
mnifaa
mana
false
,10
,1,,0
mnifaa
mana
true,10
,1,,0
mnnotifaa
mana
false
,10
,1,,0
![Page 28: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/28.jpg)
Boolean Expression Evaluation Rules(cont)
•
•
•
•
otherwisetand
whentwhere 10
,10
,1,0,0 1
false
truetrue
tt
tbb
tbtb
false
true
,
,
b
b
true
false
,
,
b
b
otherwisetand
whentwhere 10
,10
,1,0,0 1
true
falsefalse
tt
tbb
tbtb
![Page 29: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/29.jpg)
Equivalence of Boolean expressions
b0 b1 iff
tbtbTt ,,. 10
![Page 30: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/30.jpg)
Extensions
• Shortcut evaluation of Boolean expressions
• “Parallel” evaluation of Boolean expressions
• Other data types
![Page 31: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/31.jpg)
The execution of commands• <c, > ’
– c terminates on in a final state ’
• Initial state 0
0(X)=0 for all X
• Handling assignments <X:=5, > ’•
XY
XYnYXn
if(Y)
if{)](/[
•<X:=5, > [5/X]
![Page 32: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/32.jpg)
Rules for commands
• <skip, >
•
• Sequencing:
• Conditionals:
]/[: ,
,
XmaX
ma
'
'
,10
,0,
ccb
cb
elsethenif
true
Atom
ic
'
'
,10
,1,
ccb
cb
elsethenif
false
'
'''''
,10
,1,0
cc
cc
;
![Page 33: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/33.jpg)
Rules for commands (while)
,
,
cb
b
dowhile
false
'
'''
,
,,
cb
cbb
dowhile
dowhile '' c, true
![Page 34: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/34.jpg)
Example Program
Y := 1;
while (X=1) do
Y := Y * X;
X := X - 1
![Page 35: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/35.jpg)
Equivalence of commands
c0 c1 iff
',',.', 10 cc
![Page 36: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/36.jpg)
Proposition 2.8
while b do c if b then (c; while b do c) else skip
![Page 37: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/37.jpg)
Small Step Operational Semantics
• The natural semantics defines evaluation in large steps– Abstracts “computation time”
• It is possible to define a small step operational semantics– <a, > 1 <a’, ’>
• “one” step of executing a in a state yields a’ in a state ’
![Page 38: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/38.jpg)
SOS for Additions
,101,10
,01,0
'
'
aaaa
aa
,11,1
,11,1
'
'
anan
aa
mnpwherepmn
,1,
![Page 39: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/39.jpg)
SOS Rules for commands
• <skip, > 1
•
• Sequencing:
]/[1:
1
,
,
XmaX
na
Atom
ic
',;'1
','1
10,10
0,0
cccc
cc
;
',1
'11
1,10
,
ccc
c
;
![Page 40: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/40.jpg)
SOS Rules for commands
• Conditionals:
<if b then c1 else c2, > 1 <c1, >
<b, > 1 true
<if b then c1 else c2, > 1 <c2, >
<b, > 1 false
![Page 41: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/41.jpg)
SOS rules for while
<while b do c, > 1 < if b then (c; while b do c) else skip, >
![Page 42: Semantics with Applications Mooly Sagiv msagiv@post Schrirber 317 03-640-7606 html://msagiv/courses/sem08.html Textbooks:Winskel The](https://reader035.vdocuments.mx/reader035/viewer/2022062516/56649d625503460f94a45058/html5/thumbnails/42.jpg)
Summary• Operational semantics enables to naturally
express program behavior• Can handle
– Non determinism– Concurrency– Procedures– Object oriented– Pointers and dynamically allocated structures
• But remains very closed to the implementation– Two programs which compute the same
functions are not necessarily equivalent