Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Operational Semantics
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Defining an Operational Semantics
Granurality
Small-step semantics, big-step semantics
Order of evaluation: call-by-name, call-by-value, call-by-need, perpetual strategy Abstract machines
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Big-step Semantics
Consider a very elemental calculator handling expressions belonging to the following grammare::=n|x|e+e.
Each rulecompletelyevaluates an expression under asubstitutionto a”value”.
hn, σi ⇓n hx, σi ⇓σ(x) ha1, σi ⇓n1 ha2, σi ⇓n2 nis ”n1plusn2”
ha1+a2, σi ⇓n
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Properties
Abstract
Allows to avoid details
No specification of evaluation order (e.g.(1+3)+(5−3)) No specification of control of errors
No specification of interleaving
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Small-step Semantics
Evaluation of an expression under a substitution is given by a sequence ofstate changes 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
nis ”n1plusn2” hn1+n2, σi{hn, σi
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Properties
Less abstract
Specification of order of evaluation Control of errors: n2,0
n1/n2{n , wherenis ”n1divided byn2”.
Interleaving: hc1, σi{hc01, σ0i hc1||c2, σi{hc01||c2, σ0i
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
From Small-step to Multi-step Semantics
The multi-step semantics is given by the relationt{∗t0which is the reflexive and transitive closure oft{t0.
(P1) t{∗tfor everyt (P2) t{t0impliest{∗t0
(P3) t{∗t0andt0{∗t00impliest{∗t00
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Properties of the small and big step semantics
The relation{is deterministic.
The relation⇓is deterministic.
t⇓vifft{∗v, wherevis a ”value”.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
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 isexplicitin small-step semantics butimplicitin big-step semantics.
Big-step semantics is more abstract, but less precise.
Small-step semantics allows to make difference between non-termination and
”getting stuck”.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
A functional langage
t,u::= x (variable) |
c (constant) |
ht,ui (pair) |
t u (application) |
λx.t (abstraction) |
letx=tinu (let)
Some constant function symbols:fst,snd,ifthenelse,+,∗. . . Some constants:true,false,0,1,2,3, . . .
Thus e.g.iftthenuelsevcan be defined asifthenelseht,hu,vii. Aprogramis a closed expression belonging to the previous grammar.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-value lambda-calculus (big-step semantics)
(CBV Normal Forms)V,W::=c| hV,Vi |λx.t
Meaningless expressions such as(h1,1i3)or(true3)arenotconsidered as CBV Normal Forms.
Vis a CBV Normal Form V ⇓v V
t1⇓vV1 t2⇓vV2
ht1,t2i ⇓vhV1,V2i
u⇓vV r{x/V} ⇓vW letx=uinr⇓vW t⇓vλx.r u⇓vW r{x/W} ⇓vV
t u⇓vV t⇓vfst u⇓vhV1,V2i
t u⇓vV1
t⇓vsnd u⇓vhV1,V2i t u⇓vV2
t⇓vifthenelse u⇓vhtrue,hV1,V2ii tu⇓vV1
t⇓vifthenelse u⇓vhfalse,hV1,V2ii tu⇓vV2
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Particular case: closed pure lambda-terms
(CBV Normal Forms)V::=λx.t
V ⇓v V
t⇓vλx.r u⇓vW r{x/W} ⇓vV t u⇓vV
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
An example
t0=λf.λx.hx,f xiandt1=λy.y.
t0t1⇓vλx.hx,t1xi 1⇓v1 h1,t11i⇓vh1,1i t0t11⇓vh1,1i
t0 ⇓v λf.λx.hx,f xi t1 ⇓v λy.y λx.hx,f xi{f/t1} ⇓vλx.hx,t1xi t0t1⇓vλx.hx,t1xi
1⇓v1 t1 ⇓vλy.y 1⇓v1 y{y/1} ⇓v1 t11⇓v1
h1,t11i ⇓vh1,1i
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-value lambda calculus (small-step semantics)
t{vt0 ht,ui{vht0,ui
u{vu0 hV,ui{vhV,u0i u{vu0
letx=uinr{vletx=u0inr letx=Vinr{vr{x/V} t{vt0
t u{vt0u
u{vu0 V u{vV u0
(λx.t)V{vt{x\V}
fsthV1,V2i{vV1 sndhV1,V2i{vV2
ifthenelsehtrue,hV1,V2ii{vV1 ifthenelsehfalse,hV1,V2ii{vV2
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
The same example
t0=λf.λx.hx,f xiandt1=λy.y.
t0t11 {v
(λx.hx,t1 xi) 1 {v
h1,t11i {v
h1,1i
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-name lambda-calculus (big-step semantics)
(CBN Normal Forms)P::=c| ht,ui |λx.t Pis a CBN normal-form
P ⇓n P
r{x/u} ⇓nP letx=uinr⇓nP t⇓nλx.r r{x/u} ⇓nP
t u⇓n P t⇓nfst u⇓nht1,t2i t1⇓nP
tu⇓nP
t⇓nsnd u⇓nht1,t2i t2⇓nP tu⇓nP
t⇓nifthenelse u⇓nhu1,u2i u1⇓ntrue u2⇓nhm1,m2i m1⇓nP tu⇓n P
t⇓nifthenelse u⇓nhu1,u2i u1⇓nfalse u2⇓nhm1,m2i m2⇓nP tu⇓n P
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Particular case: closed pure lambda-terms
(CBN Normal Forms)P::=λx.t
P ⇓n P
t⇓nλx.r r{x/u} ⇓nP t u⇓n P
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-name lambda calculus (small-step semantics)
letx=tinr{nr{x/t} t{nt0
t u{nt0u (λx.t)u{nt{x\u} t{nt0
fstt{nfstt0 fstht,ui{nt t{nt0
sndt{nsndt0 sndht,ui{nu
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
t{nt0
ifthenelset{nifthenelset0
t{nt0
ifthenelseht,ui{nifthenelseht0,ui t∈ {true,false} u{nu0
ifthenelseht,ui{nifthenelseht,u0i t{nt0
ifthenelsehtrue,ht,uii{nifthenelsehtrue,ht0,uii u{nu0
ifthenelsehfalse,ht,uii{nifthenelsehfalse,ht,u0ii
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Coherence of results
Ift⇓vu, thenuis a CBV normal-form.
Ift⇓nu, thenuis a CBN normal-form.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Deterministic properties
Ift⇓vVandt⇓vV0, thenV=V0. Ift⇓nPandt⇓nP0, thenP=P0. Ift{vuandt{vu0, thenu=u0. Ift{nuandt{nu0, thenu=u0.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Relating big and small-steps semantics
Ift⇓vV, thent{∗vV. Ift⇓nP, thent{∗nP.
Ift{∗vuanduis a CBV normal-form, thent⇓vu. Ift{∗nuanduis a CBN normal-form, thent⇓nu.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-Need
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Lazy Evaluation and Call-by-Need
Lazy evaluation(Wadsworth’71)
Based ondemand-drivencomputation
Implementsmemoization(the first demand-driven function call transforms the argument into avalue)
May manipulate potentiallyinfinite data
The best ofcall-by-nameand the best ofcall-by-value Modeled bycall-by-needcalculi (Ariola-Felleisen).
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-need different from call-by-name
Call-by-needis different fromcall-by-name: first evaluates arguments (like call-by-value).
Twice(4+3)→cbname (4+3)+(4+3)→cbname 7+(4+3)→cbname 7+7→cbname 14 Twice(4+3)→cbneed Twice7→cbneed 7+7→cbneed 14
whereTwice=λx.x+x.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-need different from call-by-value
Call-by-needis different fromcall-by-value: values are only consumed when required (λx.8)(4+3)→cbvalue (λx.8)7→cbvalue 8
(λx.8)(4+3)→cbneed 8 In particular
(λx.8)Ω((→cbvalue(( (λx.8)Ω→cbneed 8
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
(Syntactical) call-by-need different from (semantical) neededness
Syntax:
Call-by-need
Evaluation strategy defined syntactically, using a notion ofneed contextandlet-constructorsSemantics:
Neededness
Evaluation strategy defined semantically, using the residual theoryofλ-calculus.Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
But....
?
call-by-name call-by-value
call-by-need neededness
full evaluation
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Observational equivalence, formally
LetRbe a reduction relation with an associated notion of result (e.g. value). We write t⇓Rif the termtconverges to some result.
Two termstanduare said to beobservationally equivalentforR, writtentRu, if for every contextC,
C[t] ⇓
Rif and only if C[u] ⇓
R.
Terms that cannot be distinguished by any context.
Often used to compare two different implementations or protocols.
Difficult to reason because of the universal quantification of contexts.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-need observationally equivalent to call-by-name
Theorem
Given a programt, thecall-by-nameinterpreter ontstops in avalueif and only if the call-by-needinterpreter ontstops in ananswer.
Said differently, Theorem
Giventanduwe have thatt
cbname uif and only ift
cbneed u.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Call-by-Need
Syntaxuseslet constructors/explicit substitutions letx=uintis writtent[x\u]
Grammar of terms:
t,u::= x (variable) |
t u (application) |
λx.t (abstraction)
t[x\u] (explicit substitutions)
Reduction computesweak head normal formswith let constructors, so that reduction does not take place inside abstractions:
t→ t0does not implyλx.t→ λx.t0.
Memoization⇒notion ofvalue(terms of the formV::=λx.t).
Demand-driven computations⇒notion ofneed context.
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Defining weak call-by-need
Explicit binding of arguments
( λx. id) (λy.y)
→
let x = (λy.y) in id (also written id[ x \ (λy.y) ] )
First Reduction Rule:
(λ x .t) u 7→ dB t[ x \ u ]
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Defining weak call-by-need
Explicit binding of arguments again
( λx. id) [y\ Ω ] (λy.y)
→
id[ x \ (λy.y) ] [y\ Ω ]
First (Revised) Reduction Rule:
(λ x .t) [ x
1\u
1] . . . [x
n\u
n] u 7→
dBt[ x \ u ] [x
1\u
1] . . . [x
n\u
n]
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Defining weak call-by-need
Substituting values when needed
x [ x \λy.y]
(λy.y)[x\λy.y] →
Second Reduction Rule:
N[[ x ]][ x /V ] 7→ lsv N[[V ]][ x /V ]
In the example N = , but what is N in general??
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Defining weak call-by-need
Substituting values when needed
Left of an application Left of a let Inside a needed let ( x id)[x\(λy.y)] x[y\id][x\(λy.y)] z[z\x][x\λy.y]
(N0[[x]]t)[x\V] N0[[x]][y\t][x\V] N0[[z]][z\M[[x]]][x\V]
Second Reduction Rule:
N[[ x ]][ x /V ] 7→ lsv N[[V ]][ x /V ]
where (need contexts) M, N :: = | Nt | N[y\t] | N[[z]][z\M]
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
Defining weak call-by-need
Sharing while Substituting
x [ x \(λy.y) [z\id] ]
→ x [x\λy.y] [z\id]
(λy.y)[x\(λy.y)] [z\id] →
Second (Revised) Reduction Rule:
N[[ x ]][ x /V [x
1\u
1] . . . [x
n\u
n] ] 7→
lsvN[[V ]][ x /V ] [x
1\ u
1] . . . [x
n\ u
n]
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner
A Call-by-Need Calculus (Small-Step Semantics)
Reduction Rules:
(Distant Beta reduction)
(λx.t) [x1\u1]. . .[xn\un] u 7→dBt[x\u] [x1\u1]. . .[xn\un] (Linear substitution of values)
N[[x]][x/V [x1\u1]. . .[xn\un] ]7→lsvN[[V]][x/V] [x1\u1]. . .[xn\un] Closed byneedcontextsN
A full example:
(λ x .id ( x id)) (λy.λz.id y) →
dBid(x id)[ x / λy.λz.id y ] →
dBw[ w / x id ][x/λy.λz.id y] →
lsvw[w/(λy.λz.id y)id][x/λy.λz.id y] →
dBw[w/(λz.id y)[ y / id ]][x/λy.λz.id y] →
lsv(λz.id y)[w/λz.id y] [y/id] [x/λy.λz.id y]
Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner