• Aucun résultat trouvé

Operational Semantics

N/A
N/A
Protected

Academic year: 2022

Partager "Operational Semantics"

Copied!
38
0
0

Texte intégral

(1)

Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner

Operational Semantics

(2)

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

(3)

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

(4)

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

(5)

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

(6)

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

(7)

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

(8)

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”.

(9)

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”.

(10)

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.

(11)

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

t1vV1 t2vV2

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

(12)

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

(13)

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.

t0t1vλx.hx,t1xi 1⇓v1 h1,t11i⇓vh1,1i t0t11⇓vh1,1i

t0v λf.λx.hx,f xi t1v λy.y λx.hx,f xi{f/t1} ⇓vλx.hx,t1xi t0t1vλx.hx,t1xi

1⇓v1 t1vλy.y 1⇓v1 y{y/1} ⇓v1 t11⇓v1

h1,t11i ⇓vh1,1i

(14)

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

(15)

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

(16)

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 t1nP

tu⇓nP

t⇓nsnd u⇓nht1,t2i t2nP tu⇓nP

t⇓nifthenelse u⇓nhu1,u2i u1ntrue u2nhm1,m2i m1nP tu⇓n P

t⇓nifthenelse u⇓nhu1,u2i u1nfalse u2nhm1,m2i m2nP tu⇓n P

(17)

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

(18)

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

(19)

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

(20)

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.

(21)

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.

(22)

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.

(23)

Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner

Call-by-Need

(24)

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).

(25)

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.

(26)

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

(27)

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-constructors

Semantics:

Neededness

Evaluation strategy defined semantically, using the residual theoryofλ-calculus.

(28)

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

(29)

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] ⇓

R

if 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.

(30)

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.

(31)

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.

(32)

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 ]

(33)

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→

dB

t[ x \ u ] [x

1

\u

1

] . . . [x

n

\u

n

]

(34)

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??

(35)

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]

(36)

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→

lsv

N[[V ]][ x /V ] [x

1

\ u

1

] . . . [x

n

\ u

n

]

(37)

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) →

dB

id(x id)[ x / λy.λz.id y ] →

dB

w[ w / x id ][x/λy.λz.id y] →

lsv

w[w/(λy.λz.id y)id][x/λy.λz.id y] →

dB

w[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]

(38)

Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner

Different but equivalent weak head normal forms

Related weak head normal forms for t = (λx.id (x id))(λy.λz.id y)

t →

cbname

λz.id id

value

t →

cbneed

(λz.id y) [w/λz.id y] [y/id] [x/λy.λz.id y]

answer

Références

Documents relatifs

(We don’t consider shallow approximations in this paper since they are a restricted case of growing approximations without resulting in any obvious lower complexity.) It is known

Event Ontology Extension propagates the most relevant subevent categories (to the input photo stream) with the information discovered from Data Sources, then extends the event

Keywords: Strategy Ontology requirements, Strategy levels, Strategy taxonomy, Strategy semantics, Strategy Lifecycle, Strategy artefacts, Strategy decomposition and

Although this example is quite simple, it reveals the necessary usage of dependent types for an appropriate specification of categorical constructions as well as the requirement

In big-step semantics errors occur deeply inside derivation trees.. The order of evaluation is explicit in small-step semantics but implicit in

A normal form is 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

It is a weak form of r\ " 1 and associâtes to each constant expression (including expressions which contain applications of •, Pr and constructor inverses) a finite structure

Be careful that x b is actually a local variable of the thread and should actually be renamed in each copy.. This id is sent back on a