reading, writing and relations towards extensional semantics for effect analyses andrew kennedy...

25
Reading, Writing and Reading, Writing and Relations Relations Towards Extensional Towards Extensional Semantics for Effect Semantics for Effect Analyses Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton, Martin Hofmann & Lennart Beringer) Published in APLAS’06

Post on 18-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Reading, Writing and Reading, Writing and RelationsRelationsTowards Extensional Towards Extensional Semantics for Effect AnalysesSemantics for Effect Analyses

Andrew KennedyMicrosoft Research Cambridge

(joint work with Nick Benton, Martin Hofmann & Lennart Beringer)

Published in APLAS’06

Page 2: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Reading and writingReading and writing When is this equivalence valid?

C ; if B then C1 else C2

if B then { C;C1 } else { C; C2 }

Answer: when B’s reads are disjoint from C’s writes.

We’re interested in effect systems that can validate equations such as this. Specifically, we want to study their semantics.

Page 3: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Effect systemsEffect systems Effect systems (Gifford & Lucassen)

associate to every term an approximation of its effect, such as◦ Reading from a location in the store◦ Writing to a location in the store◦ Throwing an exception◦ Performing I/O◦ Allocating an object or ref cell◦ Diverging

Effect information can be used to justify transformations e.g. in optimizing compilers.

Page 4: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Syntactic approach to effect Syntactic approach to effect soundnesssoundness

Typically, effect systems are formalized in the style of a type system

Usually, only a syntactic soundness result is shown. To do this, it is often necessary to instrument the standard semantics e.g. with traces of read/write operations.

This “intensional” semantics is very close to the analysis, so it’s easy to prove a soundness result. But:◦ It doesn’t capture what types mean – it’s purely

syntactic◦ It’s a cheat: the original semantics must be modified◦ It confuses the inference system with the semantics

of information that it produces◦ It’s very hard to use it to prove equivalences

Page 5: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Intensional vs extensional Intensional vs extensional effectseffects

Code Intensional

effect

Extensional

(observable)

effect

x := x + 1; x := x – 1 read x, write

x

none

if x=3 then y := x else y :=

3

read x, write

y

write y

if x = 2 then y := y+1 else y

:= y-1;

if x2 then y := y+1 else

y := y-1

read x, read

y, write y

none

Page 6: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Extensional interpretation of Extensional interpretation of effectseffects

We prefer the extensional approach: what does it actually

mean to “read” or “write”? Start simple: a store consisting

of just 2 integer locations. Let f be the denotation of a

command i.e. f 2 Z£ZZ£Z.

Suppose C does not write to the first location.

Extensionally: there is some g : Z£ZZ such that f(x,y) =

(x,g(x,y))

Suppose C does not read or write the first location.

Extensionally: there is some g : ZZ such that f(x,y) =

(x,g(y))

Suppose C does not read from the first location. Extensionally: there is some h : ZB, g1,g2 : ZZ such that

f(x,y) = (h(y) ? x : g1(y), g2(y))

Page 7: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

RelationsRelations The “does not read” effect, in particular, was a little intricate

to capture. But move to a relational interpretation, and we

have a much slicker characterization of reading and writing.

= diagonal relation , £ and usual constructions on

relations

f : R shorthand for (f,f) 2 R

C does not write to the first location:

8 Rµ. f : R£ R£.

C does not read or write the first location:

8 R. f : R£ R£.

C does not read from the first location:

8 R¶. f : R£ R£.

Page 8: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

FrameworkFramework

` M : A « ` M : A ¬

` M : X

Base type system

Refined type system(effect annotation, subtyping)

« ` M : X ¬

“erases to”

Base semantics(sets and functions)

Refined semantics(partial equivalence relations over base semantics)

Page 9: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Base languageBase language

A;B ::= uni t j A £ B j A ! TBA £ B

A;B := uni t j i nt j bool j A £ B j A ! TB

¡ := x1 : A1; : : : ;xn : An

V;W := () j n j bj (V;W) j ¸X : A:M j V + W j ¼i V j :::

M ;N := val V j l et x( M i nN j V W

j i f V then M el se N j read` j wri te( ;V)

Computation type

Trivial computation

Sequencing of computations

Read from a global integer-valued location

Write to a global location

Value typesCall by value

Page 10: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Selected typing rulesSelected typing rules¡ ` V1 : A ¡ ` V2 : B

¡ ` (V1;V2) : A £ B

¡ ` V : A1 £ A2

¡ ` ¼i V : A i

¡ ;x : A ` M : TB

¡ ` ¸x : A:M : A ! TB

¡ ` V1 : A ! TB ¡ ` V2 : A

¡ ` V1 V2 : TB

¡ ` V : A

¡ ` val V : TA

¡ ` M : TA ¡ ;x : A ` N : TB

¡ ` l et x( M i nN : TB

¡ ` V : bool ¡ ` M : TA ¡ ` N : TA

¡ ` i f V then M el se N : TA

¡ ` read( ) : Ti nt

¡ ` V : i nt

¡ ` wri te( ;V) : Tuni t

Page 11: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Base semanticsBase semantics Our language is simple: no recursion, no recursive

types, global store of integer locations Obvious semantics in sets and functions:

S = Locs ! Z

[[uni t ]] = 1

[[i nt ]] = Z

[[bool ]] = B

[[A £ B]] = [[A]]£ [[B]]

[[A ! TB]] = [[A]] ! [[TB]]

[[TA]] = S ! S £ [[A]]

Page 12: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Refined types and subtypingRefined types and subtyping

Subtyping

Computation type is annotated with its effect

X · XX · Y Y · Z

X · Z

X · X 0 Y · Y 0

X £ Y · X 0£ Y 0

X 0 · X T"Y · T"0Y 0

(X ! T"Y ) · (X 0! T"0Y 0)

" µ "0 X · X 0

T"X · T"0X 0

Types

X ;Y := uni t j i nt j bool j X £ Y j X ! T"Y

£ := x1 : X 1; : : : ;xn : X n

" µ[

`2L

f r `;wg

An effect consists of a set of read and write effects to

particular locations

Set inclusion on effects induces a subtyping relation

Page 13: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Selected typing rules for Selected typing rules for refined typesrefined types£;x : X ` M : T"Y

£ ` ¸x : U(X ):M : X ! T"Y

£ ` V1 : X ! T"Y £ ` V2 : X

£ ` V1 V2 : T"Y

£ ` V : X

£ ` val V : T; X

£ ` M : T"X £;x : X ` N : T"0Y

£ ` l et x( M i nN : T"[ "0Y

£ ` V : bool £ ` M : T"X £ ` N : T"X

£ ` i f V then M el se N : T"X

£ ` read( ) : Tf r ` g(i nt )£ ` V : i nt

£ ` wri te( ;V) : Tf w g(uni t )

£ ` V : X X · X 0

£ ` V : X 0

£ ` M : T"X T"X · T"0X 0

£ ` M : T"0X 0

Effects are merged in letTrivial computations have no effect

Reads have “read” effect Writes have “write” effect

“Latent” effects are observed on function application

Page 14: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

ErasureErasure Define a map U from refined type to

underlying base type.

U(i nt ) = i ntU(bool ) = boolU(uni t ) = uni t

U(X £ Y ) = U(X ) £ U(Y )U(X ! T"Y ) = U(X ) ! U(T"Y )

U(T"X ) = T(U(X ))

Easy results:

If X · Y then U(X ) = U(Y )If £ ` V : X then U(£) ` V : U(X )

Page 15: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Embedding base into Embedding base into refinedrefined

Define map G from base types to refined types that adds the “top” annotation to computation types:

G(i nt ) = i nt

G(bool ) = bool

G(uni t ) = uni t

G(A £ B) = G(A) £ G(B)

G(A ! TB) = G(A) ! Tf r ;wgG(B)

Easy result:If ¡ ` V : A then G(¡ ) ` V : G(A)

Page 16: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

GoalGoal Use the semantics to validate equivalences at particular types. E.g. effect-

independent:

£ ` M : T"1 Y £;y : Y ` N : T"2 X £;x : X ` P : T"3 Z

£ `l et x( (l et y( M i nN ) i nP= l et y( M i nl et x( N i nP

: T"1[ "2[ "3 Z

E.g. effect-dependent:

£ ` M : T"X £;x : X ;y : X ` N : T"0Yrds(") \ wrs(") = ;

£ `l et x( M i nl et y( M i nN

= l et x( M i nN [x=y]: T"[ "0Y

Page 17: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Relational semantics of Relational semantics of effectseffects

For a simple type A, the meaning «A¬ was a set.

For a refined type X, the meaning «X¬ is a subset of the underlying set «U(X)¬ together with a coarser notion of equality.

That is: a partial equivalence relation on «U(X)¬ (a symmetric, transitive relation).

Roughly speaking:

◦ (x,x) in «X¬ = “semantically, x behaves as type X”

◦ (x,y) in «X¬ = “semantically, x is equivalent to y at

type X”

Page 18: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Semantics of refined types, on one Semantics of refined types, on one slideslide

[[X ]] µ [[U(X )]]£ [[U(X )]]

[[i nt ]] = ¢ Z

[[bool ]] = ¢ B

[[uni t ]] = ¢ 1

[[X £ Y ]] = [[X ]]£ [[Y ]]

[[X ! T"Y ]] = [[X ]] ! [[T"Y ]]

[[T"X ]] =\

R 2R "

R ! R £ [[X ]]

Values of base type are related just to themselves (diagonal relation)

Functions are related in the usual “logical” fashion:

related arguments related results

Computations are related if they preserve all state relations that

respect the effect

R " ;R e µ P(S £ S)

R " =\

e2"

R e

R r ` = fR j 8(s;s0) 2 R; s` = s0`g

R w = fR j 8(s;s0) 2 R; 8n 2 Z:(s[ 7! n];s0[ 7! n]) 2 Rg

Page 19: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Effect-respecting relationsEffect-respecting relations

Computations are related if they produce related results and preserve all state relations R that respect .”

We say R respects reads from l if

We say R respects writes to l if

(s;s0) 2 R ) s(l) = s0(l)

(s;s0) 2 R ) 8x 2 Z:(s[ := x];s0[ := x]) 2 R We say R respects if it respects each effect e in

[[T"X ]] =\

R 2R "

R ! R £ [[X ]]

R 2 R r `

R 2 R w

R 2 R " =\

e2"

R e

Page 20: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

ResultsResults Soundness of subtyping:If X · Y then [[X ]] µ [[Y ]].

Fundamental theorem:If £ ` V : X ;(½;½0) 2 [[£ ]]then ([[U(£ ) ` V : U(X )]]½; [[U(£) ` V : U(X )]]½0) 2 [[X ]]:

Meaning of top effect: Equivalences

◦ Effect-independent: congruence rules, , rules, commuting conversions

◦ Effect-dependent: dead computation, duplicated computation, commuting computations, pure lambda hoist

◦ Reasoning is quite intricate, involving construction of specific effect-respecting relations. See paper!

[[G(A)]] = ¢ [[A ]].

Page 21: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Effect-dependent Effect-dependent equivalences (1)equivalences (1)

Dead Computation:

£ ` M : T"X £ ` N : T"0Yx 62 £;wrs(") = ;

£ ` l et x( M i nN = N : T"0Y

Duplicated Computation:

£ ` M : T"X £;x : X ;y : X ` N : T"0Yrds(") \ wrs(") = ;

£ `l et x( M i nl et y( M i nN

= l et x( M i nN [x=y]: T"[ "0Y

Page 22: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Effect-dependent Effect-dependent equivalences (2)equivalences (2)

Commuting Computations:

£ ` M1 : T"1 X 1 £ ` M2 : T"2X 2 £;x1 : X 1;x2 : X 2 ` N : T"0Y rds("1) \ wrs("2) = ;wrs("1) \ rds("2) = ;wrs("1) \ wrs("2) = ;£ `

l et x1( M1 i n l et x2( M2 i nN= l et x2( M2 i n l et x1( M1 i nN

: T"1[ "2[ "0Y

Pure Lambda Hoist:

£ ` M : Tf gZ £;x : X ;y : Z ` N : T"Y

£ `val (¸x : U(X ):l et y( M i nN )

= l et y( M i nval (¸x : U(X ):N ): Tf g(X ! T"Y )

Page 23: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Type Type isomorphismsisomorphisms

This “currying” isomorphism follows from lambda-hoist and - rules.

X £ Y ! T"Z »= X ! T; (Y ! T"Z)

(1 ! Twx bool ) ! Tf gbool »= 1! Tf gbool

Interestingly, this does not hold in our model due to the presence of “snapback” functions. Here:

¸g: s: let (s0;b) = g() s in (s;b)

Page 24: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Other applicationsOther applications Our general approach – of defining semantics of refined types by

relations over semantics of base types – transfers to other effects.

◦ “Semantics of an Effect Analysis for Exceptions”, Benton & Buchlovsky,

TLDI’07.

◦ Here, the effect is directly observable and the definition of relations is

obvious. Nevertheless, they’re necessary: a predicate-based semantics

is unable to validate non-trivial equivalences.

[[T A]] = ([[A]]+ E)?

[[¢ ` T "X C TA]] =f (dinl me;dinl m0e) j (m;m0) 2 [[¢ ` X C A]] g[ f (dinrE e;dinrE e) j E 2 [[¢ ` "]] g[ f (? ;? ) j ? 2 [[¢ ` "]] g

Page 25: Reading, Writing and Relations Towards Extensional Semantics for Effect Analyses Andrew Kennedy Microsoft Research Cambridge (joint work with Nick Benton,

Future directionsFuture directions Recursion

◦ Relations over domains instead of sets, unproblematic in absence of dynamic allocation

Higher-typed store◦ Much more challenging

Dynamic allocation and regions◦ Our current focus: runST-style encapsulation + its

semantic analogue (e.g. purity of memoize function)