Gradual Typing with Union and Intersection Types

Similar documents
Gradual Typing with Union and Intersection Types

Consistent Subtyping for All

The Polymorphic Blame Calculus and Parametricity

Introduction to System F. Lecture 18 CS 565 4/20/09

1 Introduction. 3 Syntax

Programming Languages Lecture 14: Sum, Product, Recursive Types

Polymorphism. Lecture 19 CS 565 4/17/08

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Consistent Subtyping for All

Intermediate Code Generation Part II

A CRASH COURSE IN SEMANTICS

CSE-321 Programming Languages 2010 Final

Type Checking and Type Inference

Gradual Parametricity, Revisited

Programming Languages Lecture 15: Recursive Types & Subtyping

Part III. Chapter 15: Subtyping

A First-Order Logic with First-Class Types

3.7 Denotational Semantics

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Second-Order Type Systems

Subtyping. Lecture 13 CS 565 3/27/06

Subsumption. Principle of safe substitution

Programming Languages

Part III Chapter 15: Subtyping

The gradual typing approach to mixing static and dynamic typing

At build time, not application time. Types serve as statically checkable invariants.

CS558 Programming Languages

Gradual Typing with Inference

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CSCI-GA Scripting Languages

Assignment 4: Semantics

GADTs meet Subtyping

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Programming Languages. Lecture 5. Extensions of simple types

Lecture 6. Abstract Interpretation

Combining Programming with Theorem Proving

COS 320. Compiling Techniques

Typed Lambda Calculus and Exception Handling

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

We defined congruence rules that determine the order of evaluation, using the following evaluation

Structural polymorphism in Generic Haskell

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Lambda Calculi With Polymorphism

Static Checking and Type Systems

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Combining Static and Dynamic Contract Checking for Curry

Where is ML type inference headed?

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

A Brief Introduction to Standard ML

Motivation At the very begining Ni won t be complete until it has static typing 1 Maintenance needs nipkgs: 1M sloc Errors hard to spot 1 Eelco Dolstr

Dynamic Logic with Non-rigid Functions

Lecture Notes on Program Equivalence

CSE-321 Programming Languages 2011 Final

Lecture Notes on Aggregate Data Structures

CS422 - Programming Language Design

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

Fundamental Concepts. Chapter 1

Simple Unification-based Type Inference for GADTs

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CSE-321 Programming Languages 2014 Final

Exploring the Design Space of Higher-Order Casts ; CU-CS

λ calculus is inconsistent

Type Systems Winter Semester 2006

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Lambda Calculus and Type Inference

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Recursive Types and Subtyping

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

Logic - CM0845 Introduction to Haskell

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

Outline Mousavi: ADT

CMSC 631 Program Analysis and Understanding. Dynamic Typing, Contracts, and Gradual Typing

Pierce Ch. 3, 8, 11, 15. Type Systems

2 Blending static and dynamic typing

CS 320: Concepts of Programming Languages

Type-indexed functions in Generic Haskell

Semantic Subtyping. Alain Frisch (ENS Paris) Giuseppe Castagna (ENS Paris) Véronique Benzaken (LRI U Paris Sud)

Type-directed Syntax Transformations for ATS-style Programs with Proofs

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Programming Languages Fall 2014

Syntax and Grammars 1 / 21

Type Checking. Error Checking

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Negations in Refinement Type Systems

Recursive Types and Subtyping

Simply-Typed Lambda Calculus

7. Introduction to Denotational Semantics. Oscar Nierstrasz

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Supplementary Notes on Exceptions

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Programming Languages Assignment #7

Denotational Semantics. Domain Theory

Lecture Notes on Data Representation

Exercise 1 (2+2+2 points)

Transcription:

Gradual Typing with Union and Intersection Types Giuseppe Castagna, Victor Lanvin ICFP 17 September 6, 2017 1 / 14

Outline 1 Motivating Example 2 Types and Subtyping 3 Function Types and Operators 4 Conclusion 1 / 14

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 x else not x 2 / 14

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 x else not x Cannot be typed with simple types, but valid with gradual types. 2 / 14

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 x else 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 (2/3) Set-theoretic version: let f (condition : Bool) (x : (Int Bool)) : (Int Bool) = if condition then if x Int then succ x else assert false else if x Bool then not x else assert false 3 / 14

Motivating Example (2/3) Set-theoretic version: let f (condition : Bool) (x : (Int Bool)) : (Int Bool) = if condition then if x Int then succ x else assert false else if x Bool then not x else assert false Syntactically heavy, but safe 3 / 14

Motivating Example (3/3) Mixing the two: let f (condition : Bool) (x : (Int Bool) &?) : (Int Bool) = if condition then succ x else not x 4 / 14

Motivating Example (3/3) Mixing the two: let f (condition : Bool) (x : (Int Bool) &?) : (Int Bool) = if condition then succ x else not x Cannot be applied to something else than an integer or a boolean, and has a precise return type Syntactically straightforward 4 / 14

Summary of the Motivations Gradualization of single expressions is sometimes too coarse Set-theoretic types are powerful but syntactically heavy (no reconstruction) 5 / 14

Summary of the Motivations Gradualization of single expressions is sometimes too coarse Set-theoretic types are powerful but syntactically heavy (no reconstruction) Mixing the two would: Make the transition between dynamic types and static types smoother Reduce the syntactic overhead of set-theoretic types 5 / 14

Outline 1 Motivating Example 2 Types and Subtyping 3 Function Types and Operators 4 Conclusion 5 / 14

Type Syntax t STypes ::= b t t t t t t t Empty Any τ GTypes ::=? b τ τ τ τ τ τ t Empty Any 6 / 14

Type Syntax t STypes ::= b t t t t t t t Empty Any τ GTypes ::=? b τ τ τ τ τ τ t Empty Any Note:? Any Any = unknown type, explicitly deconstructed? = unknown type, implicitly deconstructed 6 / 14

Type Semantics: Concretization First idea: apply AGT [Garcia et al., 2016] 7 / 14

Type Semantics: Concretization First idea: apply AGT [Garcia et al., 2016] We define a concretization function γ : GTypes P(STypes). γ(?) = STypes γ(τ 1 τ 2 ) = {t 1 t 2 t i γ(τ i )} γ(τ 1 τ 2 ) = {t 1 t 2 t i γ(τ i )} γ(b) = {b} etc... 7 / 14

Type Semantics: Concretization First idea: apply AGT [Garcia et al., 2016] We define a concretization function γ : GTypes P(STypes). γ(?) = STypes γ(τ 1 τ 2 ) = {t 1 t 2 t i γ(τ i )} γ(τ 1 τ 2 ) = {t 1 t 2 t i γ(τ i )} γ(b) = {b} etc... For example, γ((? Int)?) = {(t Int) t (t, t ) STypes 2 } 7 / 14

Type Semantics: Subtyping (1/2) Consistent subtyping [Garcia et al., 2016] σ τ (s, t) γ(σ) γ(τ), s t 8 / 14

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 τ 8 / 14

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

Type Semantics: Subtyping (2/2) Consistent subtyping σ τ σ τ 9 / 14

Type Semantics: Subtyping (2/2) Consistent subtyping σ τ σ τ = Consistent subtyping reduces in linear time to semantic subtyping! 9 / 14

Type Semantics: Subtyping (2/2) Consistent subtyping σ τ σ τ = Consistent subtyping reduces in linear time to semantic subtyping! Note: emphasizes the fact that consistent subtyping is not transitive. 9 / 14

Type Semantics: Abstraction? What about the abstraction function? 10 / 14

Type Semantics: Abstraction? What about the abstraction function? Problem: and are connectives, not constructors. 10 / 14

Type Semantics: Abstraction? What about the abstraction function? Problem: and are connectives, not constructors. α({int Bool,Int Int, Int Empty}) = 10 / 14

Type Semantics: Abstraction? What about the abstraction function? Problem: and are connectives, not constructors. α({int Bool,Int Int, Int Empty}) = 10 / 14

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}) 10 / 14

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 10 / 14

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? 10 / 14

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

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

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

Outline 1 Motivating Example 2 Types and Subtyping 3 Function Types and Operators 4 Conclusion 10 / 14

Typing Applications: Modus Ponens We start from the usual rule: Γ e 1 : σ 1 τ 1 Γ e 2 : σ 2 σ 2 σ 1 Γ e 1 e 2 : τ 1 Three components: 11 / 14

Typing Applications: Modus Ponens We start from the usual rule: Γ e 1 : σ 1 τ 1 Γ e 2 : σ 2 σ 2 σ 1 Γ e 1 e 2 : τ 1 Three components: Domain 11 / 14

Typing Applications: Modus Ponens We start from the usual rule: Γ e 1 : σ 1 τ 1 Γ e 2 : σ 2 σ 2 σ 1 Γ e 1 e 2 : τ 1 Three components: Domain Subtyping check 11 / 14

Typing Applications: Modus Ponens We start from the usual rule: Γ e 1 : σ 1 τ 1 Γ e 2 : σ 2 σ 2 σ 1 Γ e 1 e 2 : τ 1 Three components: Domain Subtyping check Result 11 / 14

Typing Applications: Modus Ponens Revisited Problem: what is the domain of ((Int Bool) Int) ( (Bool Int) (Int Int))? 12 / 14

Typing Applications: Modus Ponens Revisited Problem: what is the domain of ((Int Bool) Int) ( (Bool Int) (Int Int))? What is the result of ((Int Bool) (Bool Int)) (Nat Nat) applied to Nat? 12 / 14

Typing Applications: Modus Ponens Revisited Problem: what is the domain of ((Int Bool) Int) ( (Bool Int) (Int Int))? What is the result of ((Int Bool) (Bool Int)) (Nat Nat) applied to Nat? Γ e 1 : σ Γ e 2 : τ τ dom(σ) Γ e 1 e 2 : σ τ 12 / 14

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 Γ e 1 : σ Γ e 2 : τ τ dom(σ) Γ e 1 e 2 : σ τ 12 / 14

Function Operators: Examples dom((int Int) (??)) = 13 / 14

Function Operators: Examples dom((int Int) (??)) = Any since it can accept any value 13 / 14

Function Operators: Examples dom((int Int) (??)) = Any since it can accept any value dom((int Int) (??)) = 13 / 14

Function Operators: Examples dom((int Int) (??)) = Any since it can accept any value dom((int Int) (??)) = Int 13 / 14

Function Operators: Examples dom((int Int) (??)) = Any since it can accept any value dom((int Int) (??)) = Int ((? Int) (? Bool)) Int = 13 / 14

Function Operators: Examples dom((int Int) (??)) = Any since it can accept any value dom((int Int) (??)) = Int ((? Int) (? Bool)) Int = Int Bool 13 / 14

Outline 1 Motivating Example 2 Types and Subtyping 3 Function Types and Operators 4 Conclusion 13 / 14

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 14 / 14

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