Title: Operational Semantics
1Operational Semantics
- References Kurtz (Ch. 8.4, 8.5, 8.6)
- Many ways to define o.s. of a language L
- Define an interpreter for L
- Define a compiler for L, plus an interpreter for
the assembly language used - Specify how the state changes as various commands
execute - We will now consider the third approach
2Oper. Sem. (contd)
- Will define a relation ltc, ?gt ? ?' to mean
execution of c starting in ? terminates in ?' - E.g. ltskip , ?gt ? ?
- Also need to define ltae, ?gt ? n if value of ae in
? is n - And define ltbe, ?gt ? bv if value of be in ? is
bv
3Oper. Sem. (contd)
- What if ae or be are not well defined in ??
- Then there will be no n such that ltae, ?gt ?
nSimilarly for be. - What if c doesn't terminate or aborts (when
started in ?)? - Then there will be no ?' such that ltc, ?gt ? ?'
- What if ae (or be) have side-effects?
- Will have to consider ltae, ?gt ? ltn, ?'gt
4Oper. Sem. of Arith. Exps.
- a n X a0 a1 a0 ? a1 a0 ? a1
- ltn, ?gt ? n
- ltX, ?gt ? ?(X)
- lt a0, ?gt ? n0 , lt a1, ?gt ? n1 ----------------
-------------------------- - lt a0 a1 , ?gt ? nwhere n is the sum of n0
and n1 - lt a0, ?gt ? n0 , lt a1, ?gt ? n1 ----------------
-------------------------- - lt a0 ? a1 , ?gt ? nwhere n is the product of
n0 and n1 Etc. - Using these, we can derive, for e.g., ltXY, ?gt ?
10 if ?(X) is 4 and ?(Y) is 6
Look like axiomatic inf. rules but are not. No
assertions here
5Oper. Sem. of Bool. Exps.
- b true false a0 a1 a0 ? a1 ?b b0
? b1 b0 ? b1 - lttrue, ?gt ? true
- lt a0, ?gt ? n0 , lt a1, ?gt ? n1 ----------------
------------------------- if n0 equal to n1 - lt a0 a1 , ?gt ? true
- lt a0, ?gt ? n0 , lt a1, ?gt ? n1 ----------------
------------------------- if n0 not equal to
n1 - lt a0 a1 , ?gt ? false
- Similarly for a0 ? a1
ltfalse, ?gt ? false
6Oper. Sem. of Bool. Exps. (contd)
- lt b, ?gt ? true ------------------------
- lt ?b, ?gt ? false
- ltb0, ?gt ? t0 , lt b1, ?gt ? t1 -------------------
------------------ t true if t0 and t0 are
both - lt b0 ? b1 , ?gt ? t true and false
otherwise - ltb0, ?gt ? false ltb0, ?gt ? true, ltb1, ?gt ?
false -------------------------------- --------
--------------------------------- - lt b0 ? b1 , ?gt ? false lt b0 ? b1 , ?gt ?
false - ltb0, ?gt ? true, ltb1, ?gt ? true
----------------------------------------- - lt b0 ? b1 , ?gt ? true
lt b, ?gt ? false ------------------------ lt ?b,
?gt ? true
b0 ? b1 is similar
7Oper. Sem. of Commands
- Notation ?X/m is the same as ? except value of
X is m i.e. ?X/m(Y) m if Y ? X and
?(Y) otherwise - ?X/m is also written ?X?m
-
- ltskip, ?gt ? ?
- lt a, ?gt ? m ------------------------------
- lt X a, ?gt ? ?X/m
- lt c0, ?gt ? ?', ltc1, ?'gt ? ?'' ------------------
--------------------- - lt c0 c1 , ?gt ? ?''
Need to define sem. of read and write
8Oper. Sem. of Commands
- lt b, ?gt ? true, lt c0, ?gt ? ?' ------------------
----------------------- - lt if b then c0 else c1, ?gt ? ?'
- lt b, ?gt ? false, lt c1, ?gt ? ?' -----------------
------------------------ - lt if b then c0 else c1, ?gt ? ?'
- lt b, ?gt ? false ------------------------------
- lt while b do c, ?gt ? ?
- lt b, ?gt ? true, lt c, ?gt ? ?', ltwhile b do c, ?'gt
? ?'' ------------------------------------------
----------------------------- - lt while b do c, ?gt ? ?''
-
This semantics is coarse or big-step details of
individual steps are lost. Kurtz/Slonnegar focus
on small-step sem.
9Small-step O.S.
- Example ltX 5 Y1, ?gt ? ltY1, ?X/5 gt ?
?X/5Y/1 - lt c, ?gt ? ltc', ?'gt -----------------------------
---- - lt c c1 , ?gt ? ltc' c1 ,?'gt
- K/S destroy small step OS with the following
rule ltc, ?gt ? ?', ltc1 ,?'gt ? ?'' --------------
--------------------- - ltc c1 , ?gt ? ?''This removes distinction with
big-step o.s., so makes no sense - Problem What is a single step? Consider
evaluation of right side of X5 as a step?
lt c, ?gt ? ?'----------------------------- lt c
c1 , ?gt ? ltc1 ,?'gt
10Denotational Semantics
- References Kurtz (Ch. 9, 10(?)) Pagan (Ch. 4.2)
- Idea The d.s. of each construct is the function
computed by it - Important Compositionality Semantics of a
construct should be obtained by appropriately
composing the semantics of its components (and
should not depend on the details of the
components)
11Notation Sem. of Expressions
- Notation C?c? denotes the den. sem. of c
- C?c?(?) ?' means executing c starting in ?
will lead to ?' - E?ae?(?) n means value of ae in state ? is n
- B?be?(?) bv means value of be in state ? is
bv - "?...?" means whatever is enclosed in ?? is a
syntactic entity - B?? and E?? are easy to define
- E?n?(?) n
- E?X?(?) ?(X)
- E?ae1 ae2?(?) E?ae1?(?) E?ae2?(?) but!
"" vs. "" - ...
- B?true?(?) true B?false?(?) false
"true" vs. "true" - B?ae1ae2?(?) E?ae1?(?) E?ae2?(?) ""
vs. ""
12Semantics of Commands
- C?skip?(?) ?
- C?x ae?(?) ? x ? E?ae?(?) (or ?
x/E?ae?(?) - C?write ae?(?) ? out ? ?(out)E?ae?(?)
- C?read x?(?) ? in ? tail(?(in)), x ?
head(?(in)) - C?c1 c2?(?) C?c2? (C?c1?(?))
- C?if be then c1 else c2?(?)
- C?c1?(?) if B?be?(?) true
- C?c2?(?) if B?be?(?) false
- C?while be do c?(?)
- ? if B?be?(?) false
- C?while be do c?(C?c?(?)) if B?be?(?)
true
13Comparison with Oper. Sem.
- Den. sem. looks like oper. sem. But
- No such thing as small-step den. sem.
- The den. sem. of the loop has a subtle and
important problem
14Nested Blocks
- Consider nested blocks (but no procedures/function
s) - begin int x x 0
- begin int x x 1 end
- write x
- end
- should output 0, not 1
- But when we reach end of block, we can't just go
back to the state we had prior to entering the
block - begin int x int y x 0 y 0
- begin int x x 1 y 1 end
- write x write y
- end
- should output 0, 1 not 0, 0
15Semantics of Nested Blocks
- Solution Split the state, ?, into two
components - ? (environment) maps each program variable to
its current address or location - ? (store) maps each address to the value at
that location - D?d?(?) ?' sem. of a decl. d maps the env. ?
to a new ?' - C?c?(?, ?) ?' sem. of a command gives a new
store (same env) - C?skip?(?, ?) ?
- C?x 5?(?, ?) ??x ? 5
- D?int x?(?) ?x ? new(?)
- C?begin ds cs end?(?, ?) C?cs?(D?ds?(?), ?)
- C?c1 c2?(?, ?) C?c2?(?, C?c1?(?, ?))
- This restores, at start of c2, env. to what it
was at start of c1
16Semantics of Nested Blocks (contd)
- Work out semantics of begin int x int y x
0 y 0 - begin int x x 1 y 1 end
- write x write y
- end
- You would not want to implement using this
approach - What if sem. of decl. depended on the store?
17Den. Sem. of Loops (revisited)
- Our definition of den. sem. of while loops
- C?while be do c?(?)
- ? if B?be?(?) false
- C?while be do c?(C?c?(?)) if B?be?(?)
true - Let ww be "while true do skip"
- C?ww?(?) ? if B?true?(?) false
- C?ww?(C?skip?(?)) if B?true?(?) true
- Reduces to
- C?ww?(?) C?ww?( ? ) (???)
- Any function would satisfy that "definition"!