gradual typing with union and intersection types · gradual typing with union and intersection...
Post on 24-Mar-2020
11 Views
Preview:
TRANSCRIPT
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Gradual Typing with Union and Intersection Types
Giuseppe Castagna, Victor Lanvin
ICFP ’17
September 6, 2017
1 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Outline
1 Motivating Example
2 Types and Subtyping
3 Function Types and Operators
4 Conclusion
1 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Motivating Example (1/3)
[Siek and Vachharajani, 2008]
let succ : Int -> Int = ...let not : Bool -> Bool = ...
let f (condition : Bool) (x :
?
) :
?
=if condition then
succ xelse
not x
→ Cannot be typed with simple types, but valid with gradual types.→ What if we apply it to a string?
2 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Motivating Example (1/3)
[Siek and Vachharajani, 2008]
let succ : Int -> Int = ...let not : Bool -> Bool = ...
let f (condition : Bool) (x : ?) : ? =if condition then
succ xelse
not x
→ Cannot be typed with simple types, but valid with gradual types.
→ What if we apply it to a string?
2 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Motivating Example (1/3)
[Siek and Vachharajani, 2008]
let succ : Int -> Int = ...let not : Bool -> Bool = ...
let f (condition : Bool) (x : ?) : ? =if condition then
succ xelse
not x
→ Cannot be typed with simple types, but valid with gradual types.→ What if we apply it to a string?
2 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Motivating Example (2/3)
Set-theoretic version:
let f (condition : Bool) (x : (Int | Bool)): (Int | Bool) =
if condition thenif x ∈ Int then succ x else assert false
elseif x ∈ Bool then not x else assert false
→ Syntactically heavy, but safe
3 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Motivating Example (2/3)
Set-theoretic version:
let f (condition : Bool) (x : (Int | Bool)): (Int | Bool) =
if condition thenif x ∈ Int then succ x else assert false
elseif x ∈ Bool then not x else assert false
→ Syntactically heavy, but safe
3 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Motivating Example (3/3)
Mixing the two:
let f (condition : Bool) (x : (Int | Bool) & ?): (Int | Bool) =
if condition thensucc x
elsenot x
→ Cannot be applied to something else than an integer or aboolean, and has a precise return type→ Syntactically straightforward
4 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Motivating Example (3/3)
Mixing the two:
let f (condition : Bool) (x : (Int | Bool) & ?): (Int | Bool) =
if condition thensucc x
elsenot x
→ Cannot be applied to something else than an integer or aboolean, and has a precise return type→ Syntactically straightforward
4 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Summary of the Motivations
– Gradualization of single expressions is sometimes too coarse
– Set-theoretic types are powerful but syntactically heavy (noreconstruction)
Mixing the two would:
– Make the transition between dynamic types and static typessmoother
– Reduce the syntactic overhead of set-theoretic types
5 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Summary of the Motivations
– Gradualization of single expressions is sometimes too coarse
– Set-theoretic types are powerful but syntactically heavy (noreconstruction)
Mixing the two would:
– Make the transition between dynamic types and static typessmoother
– Reduce the syntactic overhead of set-theoretic types
5 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Outline
1 Motivating Example
2 Types and Subtyping
3 Function Types and Operators
4 Conclusion
5 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Syntax
t ∈ STypes ::= b | t → t | t ∨ t | t ∧ t | ¬t | Empty | Any
τ ∈ GTypes ::= ? | b | τ → τ | τ ∨ τ | τ ∧ τ | ¬t | Empty | Any
Note: ? 6= Any
Any = unknown type, explicitly deconstructed? = unknown type, implicitly deconstructed
6 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Syntax
t ∈ STypes ::= b | t → t | t ∨ t | t ∧ t | ¬t | Empty | Any
τ ∈ GTypes ::= ? | b | τ → τ | τ ∨ τ | τ ∧ τ | ¬t | Empty | Any
Note: ? 6= Any
Any = unknown type, explicitly deconstructed? = unknown type, implicitly deconstructed
6 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Concretization
First idea: apply AGT [Garcia et al., 2016]
We define a concretization function γ : GTypes→ P(STypes).
γ(?) = STypesγ(τ1 ∨ τ2) = {t1 ∨ t2 | ti ∈ γ(τi )}γ(τ1 → τ2) = {t1 → t2 | ti ∈ γ(τi )}
γ(b) = {b}etc...
For example,
γ((?→ Int)∧ ?) = {(t → Int) ∧ t ′ | (t, t ′) ∈ STypes2}
7 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Concretization
First idea: apply AGT [Garcia et al., 2016]
We define a concretization function γ : GTypes→ P(STypes).
γ(?) = STypesγ(τ1 ∨ τ2) = {t1 ∨ t2 | ti ∈ γ(τi )}γ(τ1 → τ2) = {t1 → t2 | ti ∈ γ(τi )}
γ(b) = {b}etc...
For example,
γ((?→ Int)∧ ?) = {(t → Int) ∧ t ′ | (t, t ′) ∈ STypes2}
7 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Concretization
First idea: apply AGT [Garcia et al., 2016]
We define a concretization function γ : GTypes→ P(STypes).
γ(?) = STypesγ(τ1 ∨ τ2) = {t1 ∨ t2 | ti ∈ γ(τi )}γ(τ1 → τ2) = {t1 → t2 | ti ∈ γ(τi )}
γ(b) = {b}etc...
For example,
γ((?→ Int)∧ ?) = {(t → Int) ∧ t ′ | (t, t ′) ∈ STypes2}
7 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Subtyping (1/2)
Consistent subtyping [Garcia et al., 2016]
σ ≤̃ τ ⇐⇒ ∃(s, t) ∈ γ(σ)× γ(τ), s ≤ t
However, we can show the existence of “extremal” concretizations:
∀t ∈ γ(τ), τ⇓ ≤ t ≤ τ⇑
(?→?)⇓ = (Any→ Empty) (?→?)⇑ = (Empty→ Any)
8 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Subtyping (1/2)
Consistent subtyping [Garcia et al., 2016]
σ ≤̃ τ ⇐⇒ ∃(s, t) ∈ γ(σ)× γ(τ), s ≤ t
However, we can show the existence of “extremal” concretizations:
∀t ∈ γ(τ), τ⇓ ≤ t ≤ τ⇑
(?→?)⇓ = (Any→ Empty) (?→?)⇑ = (Empty→ Any)
8 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Subtyping (1/2)
Consistent subtyping [Garcia et al., 2016]
σ ≤̃ τ ⇐⇒ ∃(s, t) ∈ γ(σ)× γ(τ), s ≤ t
However, we can show the existence of “extremal” concretizations:
∀t ∈ γ(τ), τ⇓ ≤ t ≤ τ⇑
(?→?)⇓ = (Any→ Empty) (?→?)⇑ = (Empty→ Any)
8 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Subtyping (2/2)
Consistent subtyping
σ ≤̃ τ ⇐⇒ σ⇓ ≤ τ⇑
=⇒ Consistent subtyping reduces in linear time to semanticsubtyping!
Note: emphasizes the fact that consistent subtyping is nottransitive.
9 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Subtyping (2/2)
Consistent subtyping
σ ≤̃ τ ⇐⇒ σ⇓ ≤ τ⇑
=⇒ Consistent subtyping reduces in linear time to semanticsubtyping!
Note: emphasizes the fact that consistent subtyping is nottransitive.
9 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Subtyping (2/2)
Consistent subtyping
σ ≤̃ τ ⇐⇒ σ⇓ ≤ τ⇑
=⇒ Consistent subtyping reduces in linear time to semanticsubtyping!
Note: emphasizes the fact that consistent subtyping is nottransitive.
9 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨
?
α({Int ∨ Bool, Bool ∨ Int}) =
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨
?
α({Int ∨ Bool, Bool ∨ Int}) =
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty}) =
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨
?
α({Int ∨ Bool, Bool ∨ Int}) =
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty}) =
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨
?
α({Int ∨ Bool, Bool ∨ Int}) =
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})
= Int ∨
?
α({Int ∨ Bool, Bool ∨ Int}) =
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨
?
α({Int ∨ Bool, Bool ∨ Int}) =
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨ ?
α({Int ∨ Bool, Bool ∨ Int}) =
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨ ?
α({Int ∨ Bool, Bool ∨ Int}) = ???
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨ ?
α({Int ∨ Bool, Bool ∨ Int}) = ? ∨ ?
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Type Semantics: Abstraction?
What about the abstraction function?
Problem: ∧ and ∨ are connectives, not constructors.
α({Int∨Bool,Int∨Int, Int∨Empty})
=
= α({Int, Int, Int}) ∨ α({Bool, Int, Empty})= Int ∨ ?
α({Int ∨ Bool, Bool ∨ Int}) = Int ∨ Bool
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Outline
1 Motivating Example
2 Types and Subtyping
3 Function Types and Operators
4 Conclusion
10 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens
We start from the usual rule:
Γ ` e1 : σ1 → τ1 Γ ` e2 : σ2 σ2 ≤̃ σ1
Γ ` e1e2 : τ1
Three components:
– Domain– Subtyping check– Result
11 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens
We start from the usual rule:
Γ ` e1 : σ1 → τ1 Γ ` e2 : σ2 σ2 ≤̃ σ1
Γ ` e1e2 : τ1
Three components:– Domain
– Subtyping check– Result
11 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens
We start from the usual rule:
Γ ` e1 : σ1 → τ1 Γ ` e2 : σ2 σ2 ≤̃ σ1
Γ ` e1e2 : τ1
Three components:– Domain– Subtyping check
– Result
11 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens
We start from the usual rule:
Γ ` e1 : σ1 → τ1 Γ ` e2 : σ2 σ2 ≤̃ σ1
Γ ` e1e2 : τ1
Three components:– Domain– Subtyping check– Result
11 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens Revisited
Problem: what is the domain of((Int→ Bool) ∧ ¬Int) ∨ (¬(Bool→ Int) ∧ (Int→ Int))?
Int
What is the result of((Int→ Bool) ∧ (Bool→ Int)) ∨ (Nat→ Nat) applied to Nat?
Nat ∨ Bool
Γ ` e1 : σ Γ ` e2 : τ τ ≤̃ d̃om(σ)
Γ ` e1e2 : σ ◦̃ τ
12 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens Revisited
Problem: what is the domain of((Int→ Bool) ∧ ¬Int) ∨ (¬(Bool→ Int) ∧ (Int→ Int))?
Int
What is the result of((Int→ Bool) ∧ (Bool→ Int)) ∨ (Nat→ Nat) applied to Nat?
Nat ∨ Bool
Γ ` e1 : σ Γ ` e2 : τ τ ≤̃ d̃om(σ)
Γ ` e1e2 : σ ◦̃ τ
12 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens Revisited
Problem: what is the domain of((Int→ Bool) ∧ ¬Int) ∨ (¬(Bool→ Int) ∧ (Int→ Int))?
Int
What is the result of((Int→ Bool) ∧ (Bool→ Int)) ∨ (Nat→ Nat) applied to Nat?
Nat ∨ Bool
Γ ` e1 : σ Γ ` e2 : τ τ ≤̃ d̃om(σ)
Γ ` e1e2 : σ ◦̃ τ
12 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Typing Applications: Modus Ponens Revisited
Problem: what is the domain of((Int→ Bool) ∧ ¬Int) ∨ (¬(Bool→ Int) ∧ (Int→ Int))?
Int
What is the result of((Int→ Bool) ∧ (Bool→ Int)) ∨ (Nat→ Nat) applied to Nat?
Nat ∨ Bool
Γ ` e1 : σ Γ ` e2 : τ τ ≤̃ d̃om(σ)
Γ ` e1e2 : σ ◦̃ τ
12 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Function Operators: Examples
d̃om((Int→ Int) ∧ (?→?)) =
Any since it can accept any value
d̃om((Int→ Int) ∨ (?→?)) =
Int
((?→ Int) ∧ (?→ Bool))◦̃Int =
Int ∨ Bool
13 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Function Operators: Examples
d̃om((Int→ Int) ∧ (?→?)) = Any since it can accept any value
d̃om((Int→ Int) ∨ (?→?)) =
Int
((?→ Int) ∧ (?→ Bool))◦̃Int =
Int ∨ Bool
13 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Function Operators: Examples
d̃om((Int→ Int) ∧ (?→?)) = Any since it can accept any value
d̃om((Int→ Int) ∨ (?→?)) =
Int
((?→ Int) ∧ (?→ Bool))◦̃Int =
Int ∨ Bool
13 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Function Operators: Examples
d̃om((Int→ Int) ∧ (?→?)) = Any since it can accept any value
d̃om((Int→ Int) ∨ (?→?)) = Int
((?→ Int) ∧ (?→ Bool))◦̃Int =
Int ∨ Bool
13 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Function Operators: Examples
d̃om((Int→ Int) ∧ (?→?)) = Any since it can accept any value
d̃om((Int→ Int) ∨ (?→?)) = Int
((?→ Int) ∧ (?→ Bool))◦̃Int =
Int ∨ Bool
13 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Function Operators: Examples
d̃om((Int→ Int) ∧ (?→?)) = Any since it can accept any value
d̃om((Int→ Int) ∨ (?→?)) = Int
((?→ Int) ∧ (?→ Bool))◦̃Int = Int ∨ Bool
13 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Outline
1 Motivating Example
2 Types and Subtyping
3 Function Types and Operators
4 Conclusion
13 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Conclusion and Future Work
Current results:+ Efficient characterization of consistent subtyping+ Sound system+ Supports polymorphism (W.I.P.)– No blame theorem, and no gradual guarantee yet
Future work:– Provide a statically-typed cast-calculus and prove the gradual
guarantee– Study blame
14 / 14
Motivating Example Types and Subtyping Function Types and Operators Conclusion
Conclusion and Future Work
Current results:+ Efficient characterization of consistent subtyping+ Sound system+ Supports polymorphism (W.I.P.)– No blame theorem, and no gradual guarantee yet
Future work:– Provide a statically-typed cast-calculus and prove the gradual
guarantee– Study blame
14 / 14
top related