Trace Semantics for Polymorphic References Guilhem Jaber & Nikos Tzevelekos PPS, IRIF, Université Paris Diderot Queen Mary University of London GaLoP 16 April 3rd 2016 1 / 19
Goals Build an intensional model for a call-by-value language with Church-style polymorphism and higher-order (nominal) references. No bad variable problems. Fully abstract for a big fragment of the language. 2 / 19
Goals Build an intensional model for a call-by-value language with Church-style polymorphism and higher-order (nominal) references. No bad variable problems. Fully abstract for a big fragment of the language. Denotations of terms as set of traces Generated operationally using an LTS. 2 / 19
Goals Build an intensional model for a call-by-value language with Church-style polymorphism and higher-order (nominal) references. No bad variable problems. Fully abstract for a big fragment of the language. Denotations of terms as set of traces Generated operationally using an LTS. Using names to represent functional and poylmorphic values Operational Nominal Game Semantics Refresh of names to control the observational power of Opponent. 2 / 19
Goals Build an intensional model for a call-by-value language with Church-style polymorphism and higher-order (nominal) references. No bad variable problems. Fully abstract for a big fragment of the language. Denotations of terms as set of traces Generated operationally using an LTS. Using names to represent functional and poylmorphic values Operational Nominal Game Semantics Refresh of names to control the observational power of Opponent. What kind of free theorems can we get for programs with polymorphic references? Some surprise appears: possibility to disclose abstract types, Strachey parametricity seems broken! 2 / 19
System ReF v def = x l () n λx.m Λα.M v 1, v 2 pack θ, v... (x Var, α TVar, l Loc, n Z) M, N def = v MN Mθ ref M!M M := N M == N M 1, M 2 π i M pack θ, M unpack M as α, x in N θ, θ def = Unit Int θ θ ref θ θ θ θ + θ α.θ α.θ S def = Loc Val Given a functional type θ and some u, we let the argument and return type of θ be: arg(θ θ) = θ arg( α.θ) = U ret u (θ θ) = θ ret u ( α.θ) = θ{u/α} 3 / 19
Operational semantics ((λx.m)v, S) (M{v/x}, S) (ref v, S) (l, S [l v]) (!l, S) (S(l), S) (l := v, S) ((), S[l v]) (l == l, S) (true, S) (l == l, S) (false, S) (π i M 1, M 2, S) (M i, S) ((Λα.M)θ, S) (M{θ/α}, S) (unpack pack θ, v as α, x in N, S) (N{θ/α}{v/x}, S) 4 / 19
Type System (Polymorphic Part), α; Σ, Γ M : θ ; Σ, Γ Λα.M : α.θ ; Σ, Γ M : α.θ ; Σ, Γ Mθ : θ{θ /α} ; Σ, Γ M : θ{θ /α} ; Σ, Γ pack θ, M : α.θ ; Σ, Γ M : α.θ, α; Γ, x : θ N : θ ; Σ, Γ unpack M as α, x in N : θ 5 / 19
Type System (Reference part) ; Σ, Γ M : θ ; Σ, Γ ref M : ref θ (l, θ) Σ ; Σ, Γ l : ref θ ; Σ, Γ M : ref θ ; Σ, Γ! M : θ ; Σ, Γ M : ref θ ; Σ, Γ N : θ ; Σ, Γ M := N : Unit ; Σ, Γ M : refθ ; Σ, Γ M : refθ ; Σ, Γ M == N : Bool 6 / 19
Example I : Fake Identity Is the identity function the only program of type α.α α up to contextual equivalence? 7 / 19
Example I : Fake Identity Is the identity function the only program of type α.α α up to contextual equivalence? No: Λα.let count = ref0 in let z = refλ.ω α in λx:α.(if!count = 0 then z := λ.x else ()); count++;!z() 7 / 19
Example II : Type Disclosure Interaction between references and polymorphism is complex. Example: α. (refα refint) α 8 / 19
Example II : Type Disclosure Interaction between references and polymorphism is complex. Example: α. (refα refint) α What happen when the same location is given as type refα and refint? Λα.λ x, y refα refint. y := 42;!x 8 / 19
Example II : Type Disclosure Interaction between references and polymorphism is complex. Example: α. (refα refint) α What happen when the same location is given as type refα and refint? Λα.λ x, y refα refint. y := 42;!x More complex example: Λα.λ x, y refα refint. let x = ref!x, y = ref!y in y++; x := x ; if y =!y then (y := 42;!x) else!x 8 / 19
Abstract values and Semantic Types AValues v, u def = () n l f p α u, v where n Z, l Loc, f Fun, p Pol and α TVar. Nominal set A def = Loc Pol Fun TVar 9 / 19
Abstract values and Semantic Types AValues v, u def = () n l f p α u, v where n Z, l Loc, f Fun, p Pol and α TVar. Nominal set A def = Loc Pol Fun TVar θ : P(AValues (Loc P(Types))) Unit = {((), ε)} Int = {(n, ε) n Z} refθ = {(l, {(l, refθ)}) l Loc} α = {(p, ε) p Pol α } θ θ = {(f, ε) f Fun θ θ } α.θ = {(f, ε) f Fun α.θ } α.θ = {( α, v, φ) (v, φ) θ{α /α} } θ 1 θ 2 = {( v 1, v 2, φ 1 φ 2 ) (v i, φ i ) θ i } 9 / 19
From Values to Abstract Values AVal(v, θ) : P(AValues ((Fun Pol) Values) (Loc P(Types))) AVal(u, ι) def = {(u, ε, )} for ι = Unit or Int and u ι AVal(l, refθ) def = {(l, ε, {(l, refθ)} l Loc} AVal(u, α) def = {(p, [p u], ) p Pol α } {(u, ε, ) u Pol α } AVal(u, θ) def = {(f, [f u], ) f Fun θ } for θ functional AVal( u 1, u 2, θ 1 θ 2 ) def = {( v 1, v 2, γ 1 γ 2, φ 1 φ 2 ) AVal( θ, u, α.θ) def = {( α, v, γ [α θ ], φ) (v i, γ i, φ i ) AVal(u i, θ i )} (v, γ, φ) AVal(u, θ{α /α})} 10 / 19
Traces Full moves: triple (m, S, ρ) where m is a move f u, f u, ū or u (f Fun, u AValues), S is a closed abstract store, ρ is a finite map Pol AValues Used to disclose polymorphic values whose type have been disclosed. 11 / 19
Traces Full moves: triple (m, S, ρ) where m is a move f u, f u, ū or u (f Fun, u AValues), S is a closed abstract store, ρ is a finite map Pol AValues Used to disclose polymorphic values whose type have been disclosed. Traces: finite sequences of full moves. 11 / 19
Configurations A configuration is a tuple E, γ, φ, S, λ with: 12 / 19
Configurations A configuration is a tuple E, γ, φ, S, λ with: an evaluation stack E, 12 / 19
Configurations A configuration is a tuple E, γ, φ, S, λ with: an evaluation stack E, a typing function φ for locations, 12 / 19
Configurations A configuration is a tuple E, γ, φ, S, λ with: an evaluation stack E, a typing function φ for locations, a closed store S, 12 / 19
Configurations A configuration is a tuple E, γ, φ, S, λ with: an evaluation stack E, a typing function φ for locations, a closed store S, an environment γ mapping names to values, 12 / 19
Configurations A configuration is a tuple E, γ, φ, S, λ with: an evaluation stack E, a typing function φ for locations, a closed store S, an environment γ mapping names to values, an ownership function λ (A {O, P}). 12 / 19
Configurations A configuration is a tuple E, γ, φ, S, λ with: an evaluation stack E, a typing function φ for locations, a closed store S, an environment γ mapping names to values, an ownership function λ (A {O, P}). They satisfy: dom(γ) = {a Pol Fun TVar λ(a) = P} dom(φ) = {l Loc dom(λ)} dom(s) for all a ν(e, codom(s), codom(γ))\loc, λ(a) = O 12 / 19
LTS generating Traces (Int) (M, θ) :: E, γ, φ, S, λ (M, θ) :: E, γ, φ, S, λ given (M, S) (M, S ). 13 / 19
LTS generating Traces (Int) (M, θ) :: E, γ, φ, S, λ (M, θ) :: E, γ, φ, S, λ given (M, S) (M, S ). (PA) (u, θ) :: E, γ, φ, S, λ v,s, ρ E, γ γ, φ φ, S, λ λ given (v, γ v, φ v ) AVal(u, θ) 13 / 19
LTS generating Traces (Int) (M, θ) :: E, γ, φ, S, λ (M, θ) :: E, γ, φ, S, λ given (M, S) (M, S ). (PA) (PQ) (u, θ) :: E, γ, φ, S, λ v,s, ρ E, γ γ, φ φ, S, λ λ given (v, γ v, φ v ) AVal(u, θ) (E[f u], θ) :: E, γ, φ, S, λ f v,s, ρ (E, θ θ) :: E, γ γ, φ φ, S, λ λ (v, γ v, φ v ) AVal(u, arg(θ f )), θ = ret v (θ f ) 13 / 19
LTS generating Traces (Int) (M, θ) :: E, γ, φ, S, λ (M, θ) :: E, γ, φ, S, λ given (M, S) (M, S ). (PA) (PQ) (OA) (u, θ) :: E, γ, φ, S, λ v,s, ρ E, γ γ, φ φ, S, λ λ given (v, γ v, φ v ) AVal(u, θ) (E[f u], θ) :: E, γ, φ, S, λ f v,s, ρ (E, θ θ) :: E, γ γ, φ φ, S, λ λ (v, γ v, φ v ) AVal(u, arg(θ f )), θ = ret v (θ f ) (E, θ θ) :: E, γ, φ, S, λ v,s, ρ (Ẽ[ṽ], θ) :: Ẽ, γ, φ φ, S[ S ], λ λ given (v, φ v ) θ 13 / 19
LTS generating Traces (Int) (M, θ) :: E, γ, φ, S, λ (M, θ) :: E, γ, φ, S, λ given (M, S) (M, S ). (PA) (PQ) (OA) (OQ) (u, θ) :: E, γ, φ, S, λ v,s, ρ E, γ γ, φ φ, S, λ λ given (v, γ v, φ v ) AVal(u, θ) (E[f u], θ) :: E, γ, φ, S, λ f v,s, ρ (E, θ θ) :: E, γ γ, φ φ, S, λ λ (v, γ v, φ v ) AVal(u, arg(θ f )), θ = ret v (θ f ) (E, θ θ) :: E, γ, φ, S, λ v,s, ρ (Ẽ[ṽ], θ) :: Ẽ, γ, φ φ, S[ S ], λ λ given (v, φ v ) θ E, γ, φ, S, λ f v,s, ρ (ũ ṽ, θ) :: Ẽ, γ, φ φ, S[ S ], λ λ (v, φ v ) arg(θ), θ = ret v (θ ) and γ(f ) = u 13 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) 14 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) g α {}}{ (v α, α α α ), γ 1,, ε, λ 1 (α, O) λ 2 14 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) { }} { g α (v α, α α α ), γ 1,, ε, λ 1 (α, O) (λx. π 1 (x), α α α ), γ 1,, ε, λ 2 λ 2 14 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) { }} { g α (v α, α α α ), γ 1,, ε, λ 1 (α, O) (λx. π 1 (x), α α α ), γ 1,, ε, λ 2 f γ 2 {}}{{}}{, γ 1 [f λx. π 1 x],, ε, λ 2 (f, P) λ 3 λ 2 14 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) { }} { g α (v α, α α α ), γ 1,, ε, λ 1 (α, O) (λx. π 1 (x), α α α ), γ 1,, ε, λ 2 f γ 2 {}}{{}}{, γ 1 [f λx. π 1 x],, ε, λ 2 (f, P) f p 1,p 2 {}}{ ((λx. π 1 x) p 1, p 2, α ), γ 2,, ε, λ 3 (p 1, O) (p 2, O) λ 3 λ 2 λ 4 14 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) { }} { g α (v α, α α α ), γ 1,, ε, λ 1 (α, O) (λx. π 1 (x), α α α ), γ 1,, ε, λ 2 f γ 2 {}}{{}}{, γ 1 [f λx. π 1 x],, ε, λ 2 (f, P) f p 1,p 2 {}}{ ((λx. π 1 x) p 1, p 2, α ), γ 2,, ε, λ 3 (p 1, O) (p 2, O) (p 1, α ), γ 2,, ε, λ 4 λ 3 λ 2 λ 4 14 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) { }} { g α (v α, α α α ), γ 1,, ε, λ 1 (α, O) (λx. π 1 (x), α α α ), γ 1,, ε, λ 2 f γ 2 {}}{{}}{, γ 1 [f λx. π 1 x],, ε, λ 2 (f, P) f p 1,p 2 {}}{ ((λx. π 1 x) p 1, p 2, α ), γ 2,, ε, λ 3 (p 1, O) (p 2, O) (p 1, α ), γ 2,, ε, λ 4 λ 3 λ 2 λ 4 p 1, γ 2,, ε, λ 4 14 / 19
A Simple Example: Polymorphic projection Consider the term v Λα.λx : α α. π 1 x of type θ = α. α α α. (v, θ), ε,, ε, ε ḡ γ 1 λ 1 {}}{{}}{, [g v],, ε, (g, P) { }} { g α (v α, α α α ), γ 1,, ε, λ 1 (α, O) (λx. π 1 (x), α α α ), γ 1,, ε, λ 2 f γ 2 {}}{{}}{, γ 1 [f λx. π 1 x],, ε, λ 2 (f, P) f p 1,p 2 {}}{ ((λx. π 1 x) p 1, p 2, α ), γ 2,, ε, λ 3 (p 1, O) (p 2, O) (p 1, α ), γ 2,, ε, λ 4 λ 3 λ 2 λ 4 p 1, γ 2,, ε, λ 4 Trace? ḡ g α f f p 1, p 2 p 1 14 / 19
Computing Type Disclosure cast relations κ: relations over types. (θ, θ ) κ: we can cast values of type θ to θ. 15 / 19
Computing Type Disclosure cast relations κ: relations over types. (θ, θ ) κ: we can cast values of type θ to θ. Atomic casts from typing function of locations φ: {(θ, θ ) l Loc.(refθ, refθ ) φ(l)} cast term: y θ : refθ, y θ : refθ λx θ.let z =!y θ in y θ := x; let z =!y θ in y θ := z; z 15 / 19
Computing Type Disclosure cast relations κ: relations over types. (θ, θ ) κ: we can cast values of type θ to θ. Atomic casts from typing function of locations φ: {(θ, θ ) l Loc.(refθ, refθ ) φ(l)} cast term: y θ : refθ, y θ : refθ λx θ.let z =!y θ in y θ := x; let z =!y θ in y θ := z; z Take the closure of κ: to represent all the cast than can be build from these atomic casts Problem on ref types! No global substitution possible. 15 / 19
Computing Type Disclosure cast relations κ: relations over types. (θ, θ ) κ: we can cast values of type θ to θ. Atomic casts from typing function of locations φ: {(θ, θ ) l Loc.(refθ, refθ ) φ(l)} cast term: y θ : refθ, y θ : refθ λx θ.let z =!y θ in y θ := x; let z =!y θ in y θ := z; z Take the closure of κ: to represent all the cast than can be build from these atomic casts Problem on ref types! No global substitution possible. Quiz: Suppose f : (refint refint) Unit, g : α. refα refα and? let x, y = ref0 in f (x, y); let u = g Int x in if (u = y) then 1 else 2 =? let x, y = ref0 in f (x, y); let u = g Int x in if (u = y) then 3 else 2 15 / 19
An Example with Type Disclosure θ def v def = refα refint α = Λα.λ x, y, z θ. if x = y then (y := 42;!x) else z }{{} M 16 / 19
An Example with Type Disclosure θ def = refα refint α v def = Λα.λ x, y, z θ. if x = y then (y := 42;!x) else z }{{} M λ f {}} 1 { (v, α.θ α), ε,, ε, ε, [f v],, ε, (f, P) 16 / 19
An Example with Type Disclosure θ def = refα refint α v def = Λα.λ x, y, z θ. if x = y then (y := 42;!x) else z }{{} M λ f {}} 1 { (v, α.θ α), ε,, ε, ε, [f v],, ε, (f, P) f α {}}{ (vα, θ α), γ 1,, ε, λ 1 (α, O) λ 2 16 / 19
An Example with Type Disclosure θ def = refα refint α v def = Λα.λ x, y, z θ. if x = y then (y := 42;!x) else z }{{} M λ f {}} 1 { (v, α.θ α), ε,, ε, ε, [f v],, ε, (f, P) f α {}}{ (vα, θ α), γ 1,, ε, λ 1 (α, O) γ {}} 2 { ḡ, γ 1 [g λz θ.m],, ε, λ 2 g l,l,p,s,ρ {}}{{}}{ (M{l/x, y}{p/z}, α), γ 2, {(l, Int),(l, α)}, S, λ 2 (l, O) (p, O)) λ 2 φ 1 λ 3 16 / 19
An Example with Type Disclosure θ def = refα refint α v def = Λα.λ x, y, z θ. if x = y then (y := 42;!x) else z }{{} M λ f {}} 1 { (v, α.θ α), ε,, ε, ε, [f v],, ε, (f, P) f α {}}{ (vα, θ α), γ 1,, ε, λ 1 (α, O) γ {}} 2 { ḡ, γ 1 [g λz θ.m],, ε, λ 2 g l,l,p,s,ρ {}}{{}}{ (M{l/x, y}{p/z}, α), γ 2, {(l, Int),(l, α)}, S, λ 2 (l, O) (p, O)) 42,S, γ 1, φ 1, S, λ 3 e.g. for S =[l 9], ρ = [p 7]. λ 2 φ 1 λ 3 16 / 19
Soundness M : Set of complete traces generated by the initial configurations of M. 17 / 19
Soundness M : Set of complete traces generated by the initial configurations of M. : effective equivalent relation on (set of) traces which substitute disclosed polymorphic names by their concrete values given by the ρ s. 17 / 19
Soundness M : Set of complete traces generated by the initial configurations of M. : effective equivalent relation on (set of) traces which substitute disclosed polymorphic names by their concrete values given by the ρ s. Theorem (Soundness) For all terms ; Σ, Γ M 1, M 2 : θ, M 1 M 2 implies M 1 ctx M 2. 17 / 19
Full Abstraction SystemReF*: Forbid binders, to appear in the scope of a ref, any type variable α inside a refθ must be reachably inhabited: an abstract value of type α must have been played before. 18 / 19
Full Abstraction SystemReF*: Forbid binders, to appear in the scope of a ref, any type variable α inside a refθ must be reachably inhabited: an abstract value of type α must have been played before. Theorem (Completeness) Given SystemReF* terms ; Σ, Γ M 1, M 2 : θ, if M 1 ctx M 2 then M 1 M 2. 18 / 19
Full Abstraction SystemReF*: Forbid binders, to appear in the scope of a ref, any type variable α inside a refθ must be reachably inhabited: an abstract value of type α must have been played before. Theorem (Completeness) Given SystemReF* terms ; Σ, Γ M 1, M 2 : θ, if M 1 ctx M 2 then M 1 M 2. A type outside SystemReF* : h : α, α.(ref(α α) ref(α Int) α) α 18 / 19
Full Abstraction SystemReF*: Forbid binders, to appear in the scope of a ref, any type variable α inside a refθ must be reachably inhabited: an abstract value of type α must have been played before. Theorem (Completeness) Given SystemReF* terms ; Σ, Γ M 1, M 2 : θ, if M 1 ctx M 2 then M 1 M 2. A type outside SystemReF* : h : α, α.(ref(α α) ref(α Int) α) α let x = ref(λy.1) in let u = h Int Int (x, x, 0) in if u then 1 else 2 =? let x = ref(λy.1) in let u = h Int Int (x, x, 0) in if u then 3 else 2 18 / 19
Conclusion On the fragment without refθ: an operational variant of Laird s CBV game model of polymorphism. Link with Levy s Ultimate Patterns and Operational Bisimulations? Next Step: Hindley-Milner (Curry-style) Polymorphism Towards fully abstract model of ML! Deduce reasoning techniques for proof equivalences Examples that cannot be handle by Kripke Logical Relations and Environmental Bisimulations! 19 / 19