Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 1
Informal Analysis
Goal: prove the semantics is deterministic
Stages:• Values --- we know they have no transitions• Minimal non-values --- consider the axioms• General composite expressions --- consider the
propagation rules
d
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 2
“Minimal” non-value expressions
Reducible expression (redex): • provided it has a transition (condition?)
• (always has a transition) by (op-apply): 3+4 7 by (if_) : if true 3 4 3 Both are reductions
Claim: there is a a unique transition from a redex. (both )
Q: Why “minimal”?
2 3f i b e eo v
, d
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 3
Intuition: The rules of define paths in a composite expression E; each descends to a “minimal” non-value sub-expression F
A path defined by the rules: a selection path
The sub-expression at its end: a selected sub-expression
If F is a redex, has a transition F F’, E has a transition E {F’/F}E (replacement)
Arbitrary composite expressions
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 4
Examples
(3 5)*(4 5) applic
* tuple
applic applic
+ tuple
3 5
4 5
tuple
(eval-rand)
(eval-tuple) (2,1)
(op-apply)
8*(4 5)
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 5
(3 5)*(4 5) applic
* tuple
applic applic
+ tuple
3 5
4 5
tuple
(eval-rand)
(eval-tuple) (2,2)
(op-apply)
(3 5)*( 1)
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 6
For , an expression may have several selected sub-expressions, several transitions
But, for d, there is a unique selected sub-expression, hence at most one transition
How do argue this is the case?
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 7
Consider application E1 E2• E1 is not a value:
– (op-apply), (eval-rand) cannot be used.
– (eval-rator) can be used,
if E1 has a transition
• E1 is a value, E2 is not:– (op-apply), (eval-rator) cannot be used (why?)
– (eval-rand) can be used,
if E2 has a transition
• Both E1 E2 are values: – (eval-rator),(eval-rand) cannot be used,
– Possibly (op-apply) can be used
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 8
This “shows” that for d :For each composite expression E, there is at most one transition
The binary relation d is functional (a partial function)
This transition semantics is deterministic
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 9
Natural (evaluation) Semantics
Transition semantics deals with one step
Natural semantics deals with a complete computation
E V
transition
Natural: E V
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 10
Natural semantics rules
(nat-int) n n (nat-bool) b b (nat-op) o o
(nat-applic-op) 1 2
1 2 ( )o
e o e v
e e f v
(nat-tuple)1 1
1
...
( ,..., ) ( ,..., )n n
n n n
e v e v
e e v v
(nat-if-true)1 2
1 2 3
true e
if
e v
e e e v
1 3
1 2 3
false e
if
e v
e e e v
(nat-if-false)
[ ]S EL
if defined
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 11
A proof tree in natural semantics
Q: What is the difference in shape of trees between transition and natural?
(if 6<2 + )(3,4) 1
6 2 false(if 6<2 + )
3 3 4 4
(3,4) (3,4)
6 6 2 2(6,2) (6,2)
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 12
A (faulty) proof tree in natural semantics:(what is missing?)
3 3 5 5
(3,5) (3,5)
3 5 8 4 2 2
4 4 2 2
(4,2) (4,2)
(3 5)*(4 2) 16 * *
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 13
Properties of natural semantics
• For each value V,
• For each E, if then E’ is a value (The value variables in the rules are not needed!)
• For each E, there is at most one proof tree, hence
at most one value(main idea of proof: For each expression category, except if, a unique rule.)
Natural semantics is a functional relation --- it defines a partial function
V V
E E
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 14
On run-time errors
High level pl’s do not allow run-time errors to occur w/o taking notice:
prevent, detect and treat
A semantics should specify explicitly both
• what are errors,
• error handling
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 15
A computation from E may get stuck in a
non-value final expression
3/0, true + 2, if 3 4 5, 2 3
We shall:• Determine the forms of error expressions• Introduce terminology• Add rules that specify error treatment to all specs
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 16
Candidate for a reduction: a expression of the forms• •
For both, proper rules certainly do not apply
If there is no transition: a run-time error
4/0, 4 2, false + 3, + (2, 3, 4)(All, except the first, are run-time type errors)
Candidate for a reduction:
either a redex or a run-time error Claim: there is a at most one transition from a
candidate for a reduction (for both , d).
1 2v v
2 3f i v e e
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 17
The run-time errors:
3/0, 2+false, + (2,3,4)
2 4
if 5 4 6
1 2 1 , where op v v v
2 2 o , where ( ) is undefinedov f v
if 2 3 where boolv e e v
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 18
For each composite expression:
each selected sub-expression
is a candidate for a reduction
Def:• E contains an error:
a selected sub-expression is a run-time error• E is an error: ( a stuck expression)
all its selected sub-expressions are run-time errors
Discuss differences between , d on these concepts
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 19
Error treatment --- error rules
We add a special “constant” ER (not a regular expression/value)
We add error rules to specify error handling
An expression may transform in one step to ER iff • It is a run-time error (error axioms)• It contains an error (error propagation rules)
Def: Exval - extended values: Val {ER}
extended expressions: Exp {ER}
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 20
Need to change assumption about built-in:
For each built-in operation o, is a total function from Val to Exval (returns a value or ER)
Takes care of
of
where ( ) is undefinedoo v f v
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 21
Some error rules:
For other constructs –left to you
For d -- left for you
The new set of rules
Is there a change in selected sub-expressions?
Can rules be improved upon by using v instead of e?
1
1 2
(apply-error) v op
v e
ER
1
1 2
(rator-error) e
e e
ER
ER
2
1 2
(rand-error) e
e e
ER
ER
,ER[ ] ( [ ] )S EL S EL
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 22
Example:
ER
(3 5)*(4 5)
applic
* tuple
applic applic
3 5
4 5
seq
(error-rand)
(error-tuple) (2,1)
(apply- error)
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 23
Rules for natural semantics:
The other constructs – left to you
The new set:
1
1 2
(nat-applic-er1) v op
v e
ER
1
1 2
(nat-applic-er2) e
e e
ER
ER
2
1 2
(nat-applic-er3) e
e e
ER
ER
,ER[ ] ( [ ] )S EL S EL
Winter 2004/5 Pls – expresssion-lang – Catriel Beeri 24
Additional properties for semantics with error rules:• Soundness: an expression cannot evaluate to both a
value and ER• Completeness/Progress: If E is not in Exval, then E
evaluates to some XV in Exval (progress --- evaluation does not get stuck)
Determinism implies soundness(we argue that is deterministic, as before)
Completeness: Argue that a candidate for a reduction is never stuck, extend to all expressions
, ,ER[ ] dS EL
Top Related