Operational Semantics
Dening an Operational Semantics Granurality
Order of evaluation
2
Big-step Semantics
Each rulecompletely evaluates the expression to avalue.
hn, σi ⇓n hX, σi ⇓σ(X)
ha1, σi ⇓n1 ha2, σi ⇓n2 n is n1 plus n2 ha1+a2, σi ⇓n
Properties Abstract
Allows to avoid details
No specication of evaluation order (e.g. (1 + 3) + (5−3)) No specication of control of errors
No specication of interleaving
Small-step Semantics
Evaluation is given by a sequence of state changes of an abstract machine which terminates when the state cannot be reduced further.
ha1, σi;ha01, σ0i ha1+a2, σi;ha01+a2, σ0i
ha2, σi;ha02, σ0i hn1+a2, σi;hn1+a02, σ0i
hX, σi;hσ(X), σi
n is n1 plus n2 hn1+n2, σi;hn, σi
5
Properties Less abstract
Specication of order of evaluation Control of errors : n2 6= 0
n1/n2;n , where n is n1 divided byn2. Interleaving : hc1, σi;hc01, σ0i
hc1||c2, σi;hc01||c2, σ0i
6
From Small-step to Multi-step Semantics
The multi-step semantics is given by the relationt;∗t0 which is the reexive and transitive closure oft;t0.
(P1) t;∗t for everyt (P2) t;t0 impliest;∗t0
(P3) t;∗t0 and t0 ;∗ t00 implies t;∗t00
Normal Forms
A normal formis a term that cannot be evaluated any further : is a state where the abstract machine is halted (result of the evaluation).
Properties of the small and big step semantics The relation; is deterministic.
The relation⇓ is deterministic.
t⇓v i t;∗v, where v is a value.
9
Big-step versus small-step semantics
In small-step semantics evaluation stops at errors. In big-step semantics errors occur deeply inside derivation trees.
The order of evaluation is explicit in small-step semantics but implicit in big-step semantics.
Big-step semantics is more abstract, but less precise.
Small-step semantics allows to make dierence between non-termination and "getting stuck".
10
A functional langage
M, N ::= x (variable) |
ct (constant) |
hM, Ni (pair) |
M N (application) |
λx.M (abstraction) | letx=M inN (let)
Some constant function symbols : fst,snd, fix, ifthenelse,+,∗ . . .
Some constants :true, false,0,1,2, 3, . . .
Notations
M1M2. . . Mn ≡ (. . .((M1M2)M3). . . Mn−1)Mn
N ~M ≡ (. . .(((N M1)M2)M3). . . Mn−1)Mn
M +N ≡ +hM, Ni
if EthenM elseN ≡ ifthenelsehE,hM, Nii
Free variables
F V(x) = {x}
F V(ct) = ∅
F V(hM, Ni) = F V(M)∪F V(N) F V(M N) = F V(M)∪F V(N) F V(λx.M) = F V(M)\ {x}
F V(letx=M inN) = F V(M)∪F V(N)\ {x}
A term M is closedi it has no free variable, i.e.F V(M) =∅. For example, λz.((λx.x z)(λy.y)) is closed but(λx.x z)(λy.y)is not.
13
Bound variables
BV(x) = ∅
BV(ct) = ∅
BV(hM, Ni) = BV(M)∪BV(N) BV(M N) = BV(M)∪BV(N) BV(λx.M) = BV(M)∪ {x}
BV(letx=M inN) = BV(M)∪BV(N)∪ {x}
A variable may be free and bound : x(λx.x).
14
Alpha-conversion
Alpha-conversion is the operation which consists in renaming some bound variables.
Thus for example x(λx.x y) =α x(λz.z y)and letx=x0inx y=αletz=x0inz y.
Théorème : For every term tthere is a termt0 such that 1. t=α t0
2. Barendregt's Convention : F V(t0)∩BV(t0) =∅.
All the bound variables oft0 are distinct.
Substitution
The application of a substitution σ={x1/t1, . . . , xn/tn} to a term M is dened by induction as follows :
σxi = ti Ifi∈ {1, . . . , n}
σy = y Ify /∈ {x1, . . . , xn}
σct = ct
σhM, Ni = hσM, σNi
σ(M N) = σM σN
σ(λx.M) = λx.σM If no capture of variables σ(letx=M inN) = letx=σM inσN If no capture of variables
Règles de réduction
(λx.M)N → M{x/N}
letx=N inM → M{x/N}
fix M → M (fix M)
fsthM, Ni → M
sndhM, Ni → N
if truethenM elseN → M if falsethenM elseN → N if 0thenM elseN → M
if nthenM elseN → N, n6= 0
17
Call-by-value lambda-calculus (big-step semantics)
(Values)V ::=ct| hV, Vi |λx.M |fix M Meaningless expressions such as (h1,1i3) or(true3) arenot considered as values.
V is a value V ⇓v V
M1⇓v V1 M2⇓v V2
hM1, M2i ⇓v hV1, V2i M ⇓v λx.L N ⇓v W L{x/W} ⇓v V
M N ⇓v V
18
N ⇓v V L{x/V} ⇓v W letx=N inL⇓v W
M ⇓v fix L N ⇓v W (L(fix L))W ⇓v V M N ⇓v V
M ⇓v fst N ⇓v hV1, V2i M N ⇓v V1
M ⇓v snd N ⇓v hV1, V2i M N ⇓v V2
M ⇓v true N ⇓v V if M thenN elseL⇓v V
M ⇓v false L⇓v V if M thenN elseL⇓v V
M ⇓v 0 N ⇓v V if M thenN elseL⇓v V
M ⇓v n n6= 0 L⇓v V if M thenN elseL⇓v V
Particular case : closed pure terms
(Values)V ::=λx.M |fix M
V ⇓v V
M ⇓v λx.L N ⇓v W L{x/W} ⇓v V M N ⇓v V
N ⇓v V L{x/V} ⇓v W letx=N inL⇓v W
M ⇓v fix L (L(fix L))N ⇓v V M N ⇓v V
21
An example
Let M =λf.λx.hx, f xiand N =λy.y
M N ⇓v λx.hx, N xi 1⇓v 1 h1, N 1i⇓v h1,1i M N 1⇓vh1,1i
M ⇓v M N ⇓v N λx.hx, f xi{f/N} ⇓vλx.hx, N xi M N ⇓v λx.hx, N xi
1⇓v 1 N ⇓vN 1⇓v 1 y{y/1} ⇓v1 N 1⇓v1
h1, N 1i ⇓v h1,1i
22
Call-by-value lambda calculus (small-step semantics) M ;v M0
M N ;v M0N
N ;v N0 V N ;v V N0
(λx.M)V ;v M{x/V} (fix M)V ;v (M (fix M))V N ;v N0
letx=N inL;v letx=N0inL letx=V inL;v L{x/V} M ;v M0
hM, Ni;v hM0, Ni
N ;v N0 hV, Ni;v hV, N0i
fst hV1, V2i;v V1 snd hV1, V2i;v V2
M ;v M0
if M thenN elseL;v if M0 thenN elseL
if truethenN elseL;v N if falsethenN elseL;v L
if 0thenN elseL;v N
n6= 0
if nthenN elseL;v L
The same example
LetM =λf.λx.hx, f xiand N =λy.y
M N 1 ;v
(λx.hx, N xi) 1 ;v
h1, N 1i ;v
h1,1i
25
Call-by-name lambda-calculus (big-step semantics)
(Lazy Forms) P ::=ct| hM, Ni |λx.M |fix M
M ⇓nλx.L L{x/N} ⇓n P M N ⇓nP
P is a lazy form P ⇓n P L{x/N} ⇓n P
letx=N inL⇓n P
M ⇓n fix L (L(fix L))N ⇓n P M N ⇓nP
M ⇓n hM1, M2i M1 ⇓nP1
fst M ⇓n P1
M ⇓nhM1, M2i M2⇓nP2
snd M ⇓n P2
26
M ⇓n true N ⇓n P if M thenN elseL⇓nP
M ⇓nfalse L⇓n P if M thenN elseL⇓nP M ⇓n0 N ⇓n P
if M thenN elseL⇓n P
M ⇓n n n6= 0 L⇓nP if M thenN elseL⇓nP
Particular case : closed pure terms
(Lazy Forms)P ::=λx.M |fix M
M ⇓n λx.L L{x/N} ⇓n P
M N ⇓n P P ⇓n P L{x/N} ⇓n P
letx=N inL⇓n P
M ⇓n fix L (L(fix L))N ⇓n P M N ⇓nP
An example
LetM =λf.λx.hx,(f x)i
fix M ⇓nfix M M (fix M) 1⇓nh1, fix M 1i fix M 1⇓nh1, fix M 1i
LetMf =fix M.
M ⇓n M (λx.hx, f xi){f/Mf} ⇓nλx.hx, Mf xi
M Mf⇓nλx.hx, Mf xi hx, Mfxi{x/1} ⇓nh1, Mf 1i M(Mf) 1⇓nh1, Mf 1i
29
Exercice
Try to compute fix M 1⇓v?
30
Call-by-name lambda calculus (small-step semantics) M ;nM0
M N ;nM0N
(λx.M)N ;nM{x/N} (fix M)N ;n(M (fix M))N
letx=M inL;n L{x/M}
M ;n M0
fst M ;n fst M0 fsthM, Ni;nM
M ;nM0
snd M ;nsnd M0 snd hM, Ni;nN M ;n M0
if M thenN elseL;n if M0thenN elseL
if truethenN elseL;n N if falsethenN elseL;n L
if 0thenN elseL;n N
n6= 0
if nthenN elseL;nL
The same example LetM =λf.λx.hx,(f x)i
fix M 1 ;n
M (fix M) 1 ;n
(λx.hx,(fix M x)i) 1 ;n
h1,(fix M 1)i
33
Coherence of results
If M ⇓v N, then N is a value.
If M ⇓n N, then N is a lazy form.
34
Deterministic properties
IfM ⇓v V and M ⇓v V0, then V =V0. IfM ⇓nP and M ⇓n P0, then P =P0. IfM ;v N and M ;v N0, then N =N0. IfM ;n N and M ;nN0, then N =N0.
Relating big and small-steps semantics If M ⇓v V, then M ;∗v V.
If M ⇓n P, then M ;∗nP.
If M ;∗v N and N is a value, then M ⇓v N.
If M ;∗nN and N is a lazy form, then M ⇓n N.