View
215
Download
0
Embed Size (px)
Citation preview
CLAUS BRABRAND © SEMANTICS (Q1,’05) SEP 1, 2005
CLAUS BRABRAND
© 2005, University of Aarhus
[ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ]
SEMANTICS (Q1,’05)
WEEK 1
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 2 ]
SEP 1, 2005
Week 1 - Outline
Introduction Welcome Course Presentation [ homepage ]
Prerequisitional (discrete) Mathematics Relations Inference Systems Transition Systems The Language “L” Virtual Machine Semantics
CLAUS BRABRAND © SEMANTICS (Q1,’05) SEP 1, 2005
INFERENCE SYSTEMS
Keywords:
relations, axioms, rules, (co-)inductive interpretation, fixed-points
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 4 ]
SEP 1, 2005
Relations
Example1: “even” relation: Written as: as a short-hand for:
… and as: as a short-hand for:
Example2: “equals” relation: Written as: as a short-hand for:
… and as: as a short-hand for:
Example3: “DFA transition” relation: Written as: as a short-hand for:
… and as: as a short-hand for:
|_even Z
|_even 4
|_even 5
4 |_even
5 |_even
2 3 (2,3) ‘=’
‘=’ Z Z
(2,2) ‘=’2 = 2
‘’ Q Q
q q’ (q, , q’) ‘’
(p, , p’) ‘’p p’
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 5 ]
SEP 1, 2005
Inference System
Inference System: Inductive (recursive) specification of relations Consists of axioms and rules
Example: Axiom:
“0 (zero) is even”!
Rule: “If n is even, then m is even (where m = n+2)”
|_even 0
|_even n
|_even m
m = n+2
|_even Z
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 6 ]
SEP 1, 2005
Terminology
Meaning: “If n is even, then m is even (provided m = n+2)”; or “m is even, if n is even (provided m = n+2)”
|_even n
|_even m
m = n+2
premise(s)
conclusion
side-condition(s)
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 7 ]
SEP 1, 2005
Abbreviation
Often, rules are abbreviated:
Rule: “If n is even, then m is even (provided m = n+2)”; or “m is even, if n is even (provided m = n+2)”
Abbreviated rule: “If n is even, then n+2 is even”; or “n+2 is even, if n is even”
|_even n
|_even n+2
|_even n
|_even m
m = n+2
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 8 ]
SEP 1, 2005
Even Example (cont’d)
Axiom: “0 (zero) is even”!
Rule: “If n is even, then n+2 is even”
Is 6 even?!?
The inference tree proves that:
|_even 0
|_even 2
|_even 4
|_even 6
|_even 0
|_even n
|_even n+2
[rule1]
[rule1]
[rule1][axiom1]
inference tree
|_even 6
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 9 ]
SEP 1, 2005
Relation Definition (Interpretation)
Actually, an inference system:
…is a demand specification for a relation:
The three relations: R = {0, 2, 4, 6, …} (aka., 2N) R’ = {0, 2, 4, 5, 6, 7, 8, …} R’’ = {…, -2, -1, 0, 1, 2, …} (aka., Z)
…all satisfy the (above) specification!
|_even 0|_even n
|_even n+2
[rule1][axiom1]
|_even Z
(0 ‘|_even’) ( n ‘|_even’ n+2 ‘|_even’)
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 10 ]
SEP 1, 2005
Inductive Interpretation
A relation:
…induces a function:
Definition: ‘lfp’ (least fixed point) ~ least solution:
|_even 0|_even n
|_even n+2
[rule1][axiom1]
F: P(Z) P(Z)
|_even Z
F(R) = {0} { n+2 | n R }
F(Ø) = {0} F2(Ø) = F({0}) = {0,2} F3(Ø) = F2({0}) = F({0,2}) = {0,2,4} …
|_even := lfp(F) = Fn(Ø)n
|_even P(Z)
From rel. to rel.
2N =
Fn(Ø) ~ “Anything that can be proved in ‘n’ steps”
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 11 ]
SEP 1, 2005
Co-inductive Interpretation ()
A relation:
…induces a function:
Definition: ‘gfp’ (greatest fixed point) ~ greatest solution:
|_even 0|_even n
|_even n+2
[rule1][axiom1]
F: P(Z) P(Z)
|_even Z
F(R) = {0} { n+2 | n R }
F(Z) = Z
|_even := gfp(F) = Fn(Z)n
|_even P(Z)
From rel. to rel.
F2(Z) = F(Z) = Z F3(Z) = F2(Z) = F(Z) = Z … Z =
Fn(Z) ~ “Anything that cannot be disproved in ‘n’ steps”
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 12 ]
SEP 1, 2005
Example: “less-than-or-equal-to”
Relation:
Is ”1 2” ?!? Yes, because there exists an inference tree:
In fact, it has two inference trees:
0 0 n mn m+1
[rule1][axiom1]
‘’ N N
n mn+1 m+1
[rule2]
0 00 11 2
[rule2]
[rule1][axiom1] 0 0
1 11 2
[rule1]
[rule2][axiom1]
CLAUS BRABRAND © SEMANTICS (Q1,’05) SEP 1, 2005
TRANSITION SYSTEMS
Keywords:
Configuration, Final Configuration, Transition, Action / label
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 14 ]
SEP 1, 2005
Definition: Transition System
A Transition System is (just) a structure:
is a set of configurations is a binary relation
(called the transition relation)
We will write instead of
Other times we use the following notation:
,
’ (,’)
’ |_ ’ ’ …
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 15 ]
SEP 1, 2005
The transition may be illustrated as:
We will (often) be using:
For instance:
A Transition
’
’
= system configuration = < program , data >
< “x:=y ; y:=0” , [x=3,y=7] > < “y:=0” , [x=7,y=7] >
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 16 ]
SEP 1, 2005
Def: Terminal Transition System
A Terminal Transition System is a structure:
is the set of configurations is the transition relation T is a set of final configurations
…satisfying:
i.e. “all configurations in ‘T’ really are terminal”.
, , T
T : ’ : ’
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 17 ]
SEP 1, 2005
Example: Finite Automaton
A Finite Automaton (FA) is a quintuplet:
Q finite set of states finite set of input symbols q0 Q initial state F Q set of acceptance states : Q P(Q) state transition relation
M = Q , , q0 , F ,
toss
toss
heads
tails
0 1 2
Q = { 0,1,2 } = { = { toss,heads,tails } (0,toss) {1,2} ,q0 = 0 (1,heads) {0} ,F = { 0 } (2,tails) {0} }
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 18 ]
SEP 1, 2005
Example: FA Transition System (1)
Define (FA) Trans. Sys. by:
Configurations:
Transition relation:
i.e., we have whenever
Final Configurations:
M := Q *
M , M , TM
TM := { <q,> | q F }
M := { ( <q,aw> , <q’,w> ) | q,q’Q, a, w*, q’(q,a) }
“State component”“Data component”
q’ (q,a)<q,aw> M <q’,w>
Recall:
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 19 ]
SEP 1, 2005
Example: FA Trans. Sys. (Cont’d)
Behavior:
toss
toss
heads
tails
0 1 2
<0, toss;heads;toss;tails> M <1, heads;toss;tails> M
<0, toss;tails> M
<2, tails> M
<0, > TM
L(M) := { w * | T : <q0,w> * }
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 20 ]
SEP 1, 2005
Def: Labelled Transition System
A Labelled Transition System is a structure:
is the set of configurations A is the set of actions (=
labels) A is the transition relation
Note: we will write instead of
, A ,
’a (, a, ’) ‘’
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 21 ]
SEP 1, 2005
The labelled transition may be illustrated as:
The labels(/actions) add the opportunity of describing transitions:
Internally (e.g., information about what went on internally) Externally (e.g., information about communication /w env) …or both.
A Labelled Transition
’
’a
a
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 22 ]
SEP 1, 2005
Example: FA Transition System (2)
Given Finite Automaton:
Define Labelled Terminal Transition System:
Configurations:
Labels:
Transition relation:
Final configurations:
M := Q
M , AM , M , TM
TM := F
q M q’ := q’(q,a)
AM := a
M = Q , , q0 , F ,
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 23 ]
SEP 1, 2005
Example: FA Trans. Sys. (Cont’d)
Behavior:
Define ”*” as the transitive closure of ””on sequences of labels:
toss
toss
heads
tails
0 1 2
0 M 1 M 0 M 2 M 0 TM
L(M) := { w A* | qT : q0 * qw
toss
heads
toss
tails
L(M) := { a a’ … a’’ A* | qT : q0 q’ … qa a’ a’’
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 24 ]
SEP 1, 2005
FA: Machine 1 vs. Machine 2
The two transition systems are very different:
Machine 1: ”I transitioned from <q,aw> to <q’,w>”
Implicit: ”…by consuming part of (internal) data component”
Machine 2: ”I transitioned from q to q’
…by inputing an ’a’ symbol from the (external) environment!”
<q,aw> M <q’,w>
q M q’a
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 25 ]
SEP 1, 2005
More Examples…
More Examples in [Plotkin, p. 6 – 13]: Three Counter Machine (***) Context-Free Grammars (**) Petri Nets (*)
They illustrate expressive power of transition systems no new points
...except formalizing input/output behavior (also later here…)
Read these yourself…
CLAUS BRABRAND © SEMANTICS (Q1,’05) SEP 1, 2005
THE LANGUAGE “L”
Keywords:
Expressions, Boolean Expressions, Commands, Abstract Syntax, Concrete Syntax
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 27 ]
SEP 1, 2005
The Language ”L” Basic Syntactic Sets:
Truthvalues: Set ranged over by: t , t’, t0, …
Numbers: Set ranged over by: m, n, …
Variables: Set ranged over by: v, v’,
…
T = {tt, ff}
N = {0, 1, 2, …}
VAR = {a, b, c, …, z}
Meta-variables
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 28 ]
SEP 1, 2005
The Language ”L” Derived Syntactic Sets:
Arithmetic Expressions (e Exp):
Boolean Expressions (b BExp):
Commands (c Com):
e ::= n | v | e + e’ | e – e’ | e e’
b ::= t | e = e’ | b or b’ | ~ b
c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 29 ]
SEP 1, 2005
Consider program: It can be (ambiguously) understood:
Note:
Abstract- vs. Concrete Syntax
while b do c ; c’
while b do c ; c’ while b do c ; c’
…either as: …or as:
“Concrete syntax”
while
b c
;
c’
while
b
c
;
c’
“Abstract syntax”
Parsing: “Concrete syntax” “Abstract syntax”
CLAUS BRABRAND © SEMANTICS (Q1,’05) SEP 1, 2005
VIRTUAL MACHINE SEMANTICS
Keywords:
The “SMC” Machine, Virtual Machine Semantics, Machine Code Semantics
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 31 ]
SEP 1, 2005
Value Stack: Set ranged over by: S
Memories: Functions ranged over by: M
Control Stack: Set ranged over by: C
SMC Machine (Trans. Sys. Semantics): Configurations:
Virtual Machines (SMC Expressions)
= Value Stack Memories Control Stack
( T N )*
VAR N
( Exp { +, –, } )*
Initially Commands, and later bits of commands
For accumulating partial results
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 32 ]
SEP 1, 2005
Configurations:
Notation:
i.e., “Memory Update” More common syntax for this:
Memory Update
= ( T N VAR )* VAR N ( Exp { +, –, } )*
m , if v = v’M’(v’) = M(v’) , otherwise
M[m/v]
M[m/v] = M’ where
M[mv]
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 33 ]
SEP 1, 2005
Virtual Machine Transitions
Configurations:
Transitions: Defined by case according to top of control stack:
= ( T N VAR )* VAR N ( Exp { +, –, } )*
[case n]: < S , M , n C > < n S , M , C >
[case v]: < S , M , v C > < M(v) S , M , C >
[case ee’]: < S , M , e e’ C > < S , M , e e’ C >
[case +/-/]: < m’ m S , M , C > < n S , M , C > …where n = m m’
Syntactic ‘+’
Semantic ‘+’
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 34 ]
SEP 1, 2005
Virtual Machine: Example
Given program: and memory:(((x + 1) – y) 7)
< , M, (((x + 1) – y) 7) >
< , M, (x + 1) ; y ; – ; 7 ; >
< , M, ((x + 1) – y) ; 7 ; >
M = [x=9,y=4]
< , M, x ; 1 ; + ; y ; – ; 7 ; >
< , M, ((x + 1) – y) ; 7 ; > < , M, (x + 1) ; y ; – ; 7 ; >
< , M, x ; 1 ; + ; y ; – ; 7 ; >
< 9, M, 1 ; + ; y ; – ; 7 ; >
< 9, M, 1 ; + ; y ; – ; 7 ; > < 1 ; 9, M, + ; y ; – ; 7 ; >
< 9 ; 1, M, + ; y ; – ; 7 ; > < 10, M, y ; – ; 7 ; >
< 10, M, y ; – ; 7 ; > < 4 ; 10, M, – ; 7 ; >
< 4 ; 10, M, – ; 7 ; > < 6, M, 7 ; >
< 6, M, 7 ; > < 7 ; 6, M, >
< 7 ; 6, M, > < 42, M, >
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 35 ]
SEP 1, 2005
VM Semantics: Major Drawbacks!
Advantage: Easy to implement (and efficient)
Drawbacks:
Non-intuitive Too concrete (e.g., stack) Indirect semantics (not syntax directed) Computational step?
“High-level language understood in terms of low-level machine code”
“Many other machine along these lines […]. They all have a tendency to pull the syntax into pieces or at any rate to wander around the syntax creating various complex symbolic structures which do not seem particularly forced by the demands of the language itself”
- Gordon Plotkin, ‘81
CLAUS BRABRAND © SEMANTICS (Q1,’05) SEP 1, 2005
NEXT WEEK…
Semantics of…:
“Simple Commands and Expressions”
CLAUS BRABRAND SEMANTICS (Q1,’05)[ 37 ]
SEP 1, 2005
Next week
Use everything…: Inference Systems, Transition Systems, Syntax, …
…to: describe explain analyse compare
…semantics of Expressions: