Operational Semantics
Dening an Operational Semantics
Granurality
Order of evaluation
Delia Kesner (Université Paris Diderot) Typage 2018 2 / 34
Big-step Semantics
Each rule completely 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
Delia Kesner (Université Paris Diderot) Typage 2018 4 / 34
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+a20, σ0i
hX, σi hσ(X), σi
n is n1 plus n2 hn1+n2, σi hn, σi
Properties
Less abstract
Specication of order of evaluation Control of errors : n2 6=0
n1/n2 n , where n is n1 divided by n2. Interleaving : hc1, σi hc10, σ0i
hc1||c2, σi hc10||c2, σ0i
Delia Kesner (Université Paris Diderot) Typage 2018 6 / 34
From Small-step to Multi-step Semantics
The multi-step semantics is given by the relation t ∗ t0 which is the reexive and transitive closure of t t0.
(P1) t ∗ t for every t (P2) t t0 implies t ∗ t0
(P3) t ∗ t0 and t0 ∗t00 implies t ∗ t00
Normal Forms
Anormal formis a term that cannot be evaluated any further : is a state where the abstract machine is halted (result of the evaluation).
Delia Kesner (Université Paris Diderot) Typage 2018 8 / 34
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.
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".
Delia Kesner (Université Paris Diderot) Typage 2018 10 / 34
A functional langage
M,N ::= x (variable) |
ct (constant) |
hM,Ni (pair) | M N (application) | λx.M (abstraction) | let x =M in N (let)
Some constant function symbols : fst, snd, x, 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 E then M else N ≡ ifthenelsehE,hM,Nii
Delia Kesner (Université Paris Diderot) Typage 2018 12 / 34
Free variables
FV(x) = {x}
FV(ct) = ∅
FV(hM,Ni) = FV(M)∪FV(N) FV(M N) = FV(M)∪FV(N) FV(λx.M) = FV(M)\ {x}
FV(let x =M in N) = FV(M)∪FV(N)\ {x}
A term M isclosedi it has no free variable, i.e. FV(M) =∅. For example, λz.((λx.x z)(λy.y))is closed but (λx.x z)(λy.y) is not.
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(let x =M in N) = BV(M)∪BV(N)∪ {x} A variable may be free and bound : x (λx.x).
Delia Kesner (Université Paris Diderot) Typage 2018 14 / 34
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 let x =x0 in x y =α let z=x0 in z y.
Théorème : For every term t there is a term t0 such that
1 t =α t0
2 Barendregt's Convention :
I FV(t0)∩BV(t0) =∅.
I All the bound variables of t0 are distinct.
Substitution
The application of a substitution σ={x1/t1, . . . ,xn/tn} to a term M is dened by induction as follows :
σxi = ti If i∈ {1, . . . ,n}
σy = y If y ∈ {/ x1, . . . ,xn}
σct = ct
σhM,Ni = hσM, σNi
σ(M N) = σM σN
σ(λx.M) = λx.σM If no capture of variables σ(let x =M in N) = let x =σM inσN If no capture of variables
Delia Kesner (Université Paris Diderot) Typage 2018 16 / 34
Reduction Rules
(λx.M)N → M{x/N} let x =N in M → M{x/N}
x M → M (x M)
fsthM,Ni → M
sndhM,Ni → N
if true then M else N → M if false then M else N → N if 0 then M else N → M
if n then M else N → N, n 6=0
Call-by-value lambda-calculus (big-step semantics)
(Values) V ::=ct | hV,Vi |λx.M |x M
Meaningless expressions such as (h1,1i 3) or(true 3) are not 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
N ⇓v V L{x/V} ⇓v W let x =N in L⇓v W
Delia Kesner (Université Paris Diderot) Typage 2018 18 / 34
M ⇓v x L N ⇓v W (L(x 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 then N else L⇓v V
M ⇓v false L⇓v V if M then N else L⇓v V M ⇓v 0 N⇓v V
if M then N else L⇓v V
M ⇓v n n 6=0 L⇓v V if M then N else L⇓v V
Particular case : closed pure lambda-terms
(Values) V ::=λx.M
V ⇓v V
M ⇓v λx.L N ⇓v W L{x/W} ⇓v V M N ⇓v V
Delia Kesner (Université Paris Diderot) Typage 2018 20 / 34
An example
M =λf.λx.hx,f xi and 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 ⇓v N 1⇓v 1 y{y/1} ⇓v 1 N 1⇓v 1
Call-by-value lambda calculus (small-step semantics)
M v M0 M N v M0 N
N v N0 V N v V N0
(λx.M)V v M{x/V} (x M)V v (M (x M))V N v N0
let x =N in L v let x =N0 in L let x =V in L 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
Delia Kesner (Université Paris Diderot) Typage 2018 22 / 34
M v M0
if M then N else L v if M0then N else L
if true then N else L v N if false then N else L v L
if 0 then N else L v N
n6=0
if n then N else L v L
The same example
M =λf.λx.hx,f xi and N =λy.y.
M N 1 v
(λx.hx,N xi) 1 v h1,N 1i v h1,1i
Delia Kesner (Université Paris Diderot) Typage 2018 24 / 34
Call-by-name lambda-calculus (big-step semantics)
(Lazy Forms)P ::=ct | hM,Ni |λx.M |x M M ⇓nλx.L L{x/N} ⇓nP
M N ⇓nP
P is a lazy form P ⇓n P L{x/N} ⇓nP
let x =N in L⇓nP
M ⇓nx L (L(x L))N ⇓nP M N ⇓nP
M⇓n hM1,M2i M1 ⇓nP1 fst M ⇓nP1
M ⇓nhM1,M2i M2⇓nP2 snd M ⇓nP2
M ⇓ntrue N ⇓nP M ⇓nfalse L⇓nP
Particular case : closed pure lambda-terms
(Lazy Forms)P ::=λx.M
P ⇓n P
M ⇓nλx.L L{x/N} ⇓nP M N ⇓nP
Delia Kesner (Université Paris Diderot) Typage 2018 26 / 34
An example
Let M =λf.λx.hx,(f x)i
x M ⇓nx M M (x M)1⇓nh1,x M 1i x M 1⇓nh1,x M 1i
Let Mf =x M.
M ⇓n M (λx.hx,f xi){f/Mf} ⇓nλx.hx,Mf xi
M Mf ⇓nλx.hx,Mf xi hx,Mf xi{x/1} ⇓nh1,Mf 1i M(Mf)1⇓nh1,Mf 1i
Exercice
Try to compute x M 1⇓v?
Delia Kesner (Université Paris Diderot) Typage 2018 28 / 34
Call-by-name lambda calculus (small-step semantics)
M n M0 M N n M0 N
(λx.M)N nM{x/N} (x M)N n(M (x M))N
let x =M in L nL{x/M} M nM0
fst M nfst M0 fst hM,Ni nM
M n M0
if M then N else L n if M0 then N else L
if true then N else L nN if false then N else L nL
if 0 then N else L nN
n 6=0
if n then N else L nL
Delia Kesner (Université Paris Diderot) Typage 2018 30 / 34
The same example
M =λf.λx.hx,(f x)i.
x M 1 n
M (x M)1 n
(λx.hx,(x M x)i) 1 n
h1,(x M 1)i
Coherence of results
If M ⇓v N, then N is a value.
If M ⇓nN, then N is a lazy form.
Delia Kesner (Université Paris Diderot) Typage 2018 32 / 34
Deterministic properties
If M ⇓v V and M ⇓v V0, then V =V0. If M ⇓nP and M ⇓nP0, then P=P0. If M v N and M v N0, then N =N0. If M nN and M nN0, then N=N0.
Relating big and small-steps semantics
If M ⇓v V , then M ∗v V . If M ⇓nP, 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 ⇓nN.
Delia Kesner (Université Paris Diderot) Typage 2018 34 / 34