Type assignment for intersections and unions in call-by-value languages

Similar documents
Type Assignment for Intersections and Unions in Call-by-Value Languages

Refined typechecking with Stardust

Tridirectional Typechecking

Verifying Program Invariants with Refinement Types

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

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Programming Languages Lecture 15: Recursive Types & Subtyping

Intersections and Unions of Session Types

λ calculus is inconsistent

Harvard School of Engineering and Applied Sciences Computer Science 152

Programming Languages Lecture 14: Sum, Product, Recursive Types

Untangling Typechecking of Intersections and Unions

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

` e : T. Gradual Typing. ` e X. Ronald Garcia University of British Columbia

Generic polymorphism on steroids

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

CSE-321 Programming Languages 2010 Final

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

Practical Refinement-Type Checking

A Unified System of Type Refinements

CLF: A logical framework for concurrent systems

CSE-321 Programming Languages 2012 Midterm

Lecture Notes on Aggregate Data Structures

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

1 Introduction. 3 Syntax

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

CSCI-GA Scripting Languages

A Practical Optional Type System for Clojure. Ambrose Bonnaire-Sergeant

CSE-321 Programming Languages 2011 Final

Part III Chapter 15: Subtyping

Type Checking and Type Inference

On the Logical Foundations of Staged Computation

Combining Programming with Theorem Proving

Subsumption. Principle of safe substitution

Part III. Chapter 15: Subtyping

Subtyping and Objects

This section is primarily based on Principles of Type Refinement, Noam Zeilberger,

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

Kripke-Style Contextual Modal Type Theory

Typing & Static Analysis of Multi-Staged Programs

A Substructural Type System for Delimited Continuations

Tracing Ambiguity in GADT Type Inference

The Logical Basis of Evaluation Order & Pattern-Matching

Subtyping. Lecture 13 CS 565 3/27/06

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

Lambda Calculi With Polymorphism

Where is ML type inference headed?

Polymorphism and System-F (OV)

Lists. Michael P. Fourman. February 2, 2010

A Polymorphic Type System for Multi-Staged Languages

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

Programming Languages Assignment #7

Lecture Notes on Data Representation

Hiding local state in direct style: a higher-order anti-frame rule

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

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

Constrained Types and their Expressiveness

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

HOL DEFINING HIGHER ORDER LOGIC LAST TIME ON HOL CONTENT. Slide 3. Slide 1. Slide 4. Slide 2 WHAT IS HIGHER ORDER LOGIC? 2 LAST TIME ON HOL 1

CS4215 Programming Language Implementation. Martin Henz

Variables. Substitution

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

Lambda Calculi With Polymorphism

Type Reconstruction for General Refinement Types

Simple Unification-based Type Inference for GADTs

Java Wildcards Meet Definition-Site Variance

Recency Types for Dynamically-Typed, Object-Based Languages

CS558 Programming Languages

Type Inference with Inequalities

Programming Languages

The Typed λ Calculus and Type Inferencing in ML

Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Sets Fall / 31

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

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

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

Collage of Static Analysis

Calculus of Inductive Constructions

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Modal Logic: Implications for Design of a Language for Distributed Computation p.1/53

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

CSE-321 Programming Languages 2010 Midterm

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Sémantique des Langages de Programmation (SemLP) DM : Region Types

Let Arguments Go First

Supplementary Notes on Recursive Types

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

3 Type Inference and Optimization

Dependent types and program equivalence. Stephanie Weirich, University of Pennsylvania with Limin Jia, Jianzhou Zhao, and Vilhelm Sjöberg

Dependent types and program equivalence. Stephanie Weirich, University of Pennsylvania with Limin Jia, Jianzhou Zhao, and Vilhelm Sjöberg

Subject Reduction vs Intersection / Union Types in λµ µ

Gradual Typing with Union and Intersection Types

ATS: a language to make typeful programming real and fun

GADTs meet Subtyping

Programming Language Concepts: Lecture 19

Flang typechecker Due: February 27, 2015

Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description)

Consistent Subtyping for All

CIS 500 Software Foundations Fall December 6

Transcription:

Type assignment for intersections and unions in call-by-value languages Joshua Dunfield and Frank Pfenning Triple Project Carnegie Mellon University 8 April 2003 FOSSACS 03, Warsaw, Poland Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.1/42

Outline Motivation Language Datasort refinements Index refinements Intersections Indefinite types Related work Conclusion & future work Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.2/42

Motivation Conventional type systems: ML, Java,... Tractable and easy to use Limited expressive power Refined type systems: Tractable and easy to use Better expressive power Fully dependent type systems: Nuprl,... Undecidable Extremely expressive Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.3/42

Push the envelope The goal of the Triple Project: create refined type systems. The present work: Persistent refinements of algebraic datatypes Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.4/42

Type Assignment A type assignment system a la Curry Terms do not contain types Undecidable A bidirectional system Terms contain some type annotations Two judgments: inference e A, checking e A. This paper: Type assignment only Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.5/42

Property Types Some types build values Example: tuples Others talk about values Example: parametric polymorphism α. α α describing behavior of the identity fn We call the latter property types Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.6/42

Property Types In our system: δ(i): Datasort and index refinement : Intersection: v : A B means v has type A and type B : Greatest type (0-ary ) Π: Universal quantifier over indices (infinitary ) : Union: v : A B means v has type A or type B : Empty type (0-ary ) Σ: Existential quantifier over indices (infinitary ) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.7/42

Language Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.8/42

Language + Initial Typing A, B, C, D ::= 1 A B e ::= x f () λx. e e 1 (e 2 ) fix f. e B 1 A 1 A 2 B 2 A 1 A 2 B 1 B 2 ( ) 1 1 (1) Γ(x) = A Γ x : A (var) Γ(f) = A Γ f : A (fixvar) Γ e : A A B Γ e : B (sub) Γ e 1 : A B Γ e 2 : A Γ e 1 (e 2 ) : B ( E) Γ, x:a e : B Γ λx. e : A B ( I) Γ, f:a e : A Γ fix f. e : A (fix) Γ () : 1 (1I) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.9/42

cbv Semantics Values v ::= x () λx. e Evaluation contexts E ::= [] E(e) v(e) e R e E[e ] E[e ] (λx. e) v R fix f. e R [v/x] e [fix f. e / f] e Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.10/42

Key Properties A practical type system should satisfy Preservation: If e : A and e e then e : A. Progress: If e : A then e is a value or e. e e. Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.11/42

Datasort Refinements a.k.a. refinement types (Freeman, Pfenning, Davies) Refine an algebraic datatype by a datasort δ Example: Lists of integers Nil : 1 list Cons : int list list Intersections essential Nil : 1 even Cons : (int odd even) δ : (int even odd) (int list list) list odd 6 even Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.12/42

Index Refinements a.k.a. dependent types restricted to a decidable constraint domain (Xi & Pfenning) Refine an algebraic datatype by an index Indices drawn from any decidable constraint domain, here N Example: Lists indexed by their length Nil : 1 list Cons : int list list Nil : 1 list(0) Cons : Πa:N. int list(a) list(a + 1) Example: append : Πa:N. Πb:N. list(a) list(b) list(a+b) Universal quantifier Π essential Existential quantifier Σ also essential Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.13/42

Intersection Types Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.14/42

Typing Intersections Typing: Γ e : A 1 A 2 Γ e : A 1 ( E 1 ) Γ e : A 1 A 2 Γ e : A 2 ( E 2 ) Γ e : A 1 Γ e : A 2 Γ e : A 1 A 2 ( I) ( I) unsound with mutable references. Γ v : A 1 Γ v : A 2 Γ v : A 1 A 2 ( I) The fix: a value restriction [Davies & Pfenning 00]. Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.15/42

Indefinite Types Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.16/42

Indefinite Types: Motivation filter : (int bool) list list filter : Πa:N. (int bool) list(a) list( ) filter : Πa:N. (int bool) list(a) (Σb:N. list(b)) Σb:N. B quantifies existentially over b in B [Xi & Pfenning] Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.17/42

Indefinite Types: Motivation Definitely need Σ The binary analogue of Σ is the union type Need? Possibly just a convenience Issues similar for both, but easier to talk about Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.18/42

Union Types Introduction rules straightforward: Γ e : A Γ e : A B ( I 1) Elimination rule? Γ e : B Γ e : A B ( I 2) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.19/42

Rough Road Recall: for, introduction rule ( I) was tricky Duality suggests ( E) will be interesting too Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.20/42

Elimination Want to reason by cases: For x : A B, show for x : A and for x : B. Substitution approach (e.g. [MacQueen et al. 86]), reminiscent of natural deduction: Γ, x:a e : C Γ e : A B Γ, x:b e : C Γ [e /x] e : C Suppose Γ h : (A C) (B C), Γ v : A B Γ v : A B Γ, x:a h x : C Γ, x:b h x : C Γ h v : C Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.21/42

Elimination Γ e : A B Γ [e /x] e : C Γ, x:a e : C Γ, x:b e : C Consider a nondeterministic choice e 1 e 2 : e 1 e 2 e 1 e 1 e 2 e 2 Suppose Γ g : (A A C) (B B C), Γ y : A, Γ z : B. Γ y z : A B Γ, x:a g x x : C Γ, x:b g x x : C Γ g (y z) (y z) : C g (y z) (y z) g y (y z) g y z Unsound whenever a term evaluates to more than one value Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.22/42

Elimination Γ e : A B Γ [e /x] e : C Γ, x:a e : C Γ, x:b e : C Restrict to exactly one occurrence of e? Can fail if e inside a λ (see paper) Restrict e to a value? [van Bakel 99] Sound but still must guess occurrences of e Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.23/42

Elimination: Our Solution Γ e : A B Γ E[e ] : C Γ, x:a E[x] : C Γ, x:b E[x] : C ( E) Exactly one e, in evaluation position So e is the next thing to be evaluated Remainder of computation (E) on the result of e Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.24/42

The Empty Type is the empty or void type A ( L) Elimination rule: by analogy with ( E) Γ e : Γ E[e ] : C ( E) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.25/42

A Generalization? Must we restrict the rules to an evaluation context? Or could we also allow cases where e must be evaluated exactly once but might not be in evaluation position? Γ e : e eval d exactly once in e Γ e : C... let ω = (fix f. f). Then ω :. Γ ω : Γ (()())ω : C But ()() is not a value and does not step Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.26/42

Type Safety Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.27/42

Type Safety Γ e : A B Γ E[e ] : C Γ, x:a E[x] : C Γ, x:b E[x] : C ( E) Preservation and progress hold with δ, i,,, Π,,, Σ Lemma: Values always have definite types. v : If v : A B then v : A or v : B If v : Σa:N. A then v : [i/a] A for some i Above: a special case of the actual lemma (Fully written out for δ, i,, Π in [Dunfield 02]) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.28/42

Closely Related Work Davies 97, 00* δ, ; Dunfield 02 δ, i,, Π; Xi 98, 99*, 00 i, Π, Σ; * & Pfenning This paper* δ, i,, Π,,, Σ, ; : Ongoing work δ, i,, Π,,, Σ, ; Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.29/42

Related Work [Coppo et al. 81]: can characterize normal forms (termination); hence undecidable [Reynolds 96]: FORSYTHE with (& type annotations) [Pierce 91]: Language with,, syntactic markers [Barbanera et al. 95]: With unrestricted -elim rule, types not preserved under β-reduction [Wells et al. 02], [Palsberg & Pavlopoulou 01]:, for control flow analysis Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.30/42

Conclusion Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.31/42

Summary Goal: express more invariants Two especially useful refinements already developed: Datasort refinements Index refinements Refinements motivate,, Π, Σ ( I): Value restriction ( E): Eval. context restriction Type safety The present system: Undecidable, hence impractical Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.32/42

Future Work Bidirectional formulation Let-normal form Mutable references Implementation Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.33/42

Acknowledgments National Science Foundation: CCR-0204248: Type Refinements; 0121633: ITR/SY+SI: Language Technology for Trustless Software Dissemination; Graduate Research Fellowship Feedback on the paper: Brigitte Pientka and the anonymous referees Feedback on the talk: Aleksey Kliger, Stephen Magill, Jonathan Moody, Frank Pfenning : Henrik Christian Grove, available at a CTAN mirror near you Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.34/42

The End Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.35/42

Intersections: Subtyping Subtyping: A B 1 A B 2 A B 1 B 2 A 1 B A 1 A 2 B ( L 1) Distributivity? ( R) A 2 B A 1 A 2 B ( L 2) (A B) (A B ) A (B B ) Again unsound with mutable references. [Davies & Pfenning 00] Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.36/42

Unions: Subtyping Subtyping for just the dual of : A 1 B A 2 B A 1 A 2 B ( L) A B 1 A B 1 B 2 ( R 1 ) A B 2 A B 1 B 2 ( R 2 ) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.37/42

Formulating Refinements P ::= i. = j... Γ ::= Γ, x:a Γ, a:γ Γ, P e ::=... c(e) case e of ms v ::=... c(v) E ::=... c(e) case E of ms Atomic subtyping δ 1 δ 2 Γ i =. j Γ δ 1 (i) δ 2 (j) (δ) Γ c : A δ(i) Γ e : A Γ c(e) : δ(i) (δi) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.38/42

Formulating Refinements Atomic subtyping δ 1 δ 2 Γ i =. j Γ δ 1 (i) δ 2 (j) (δ) Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.39/42

Evaluation Order Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.40/42

Evaluation Order Binary and 0-ary indefinite type elim rules: Γ e : A B Γ E[e ] : C Γ, x:a E[x] : C Γ, x:b E[x] : C A corresponding unary rule? ( E) Γ e : A Γ, x:a E[x] : C Γ E[e ] : C Γ e : Γ E[e ] : C ( E) (direct) Actually a derivable rule: ( E) with A A But not derivable in a bidirectional system Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.41/42

Evaluation Order Γ e : A Γ, x:a E[x] : C Γ E[e ] : C (direct) Rule needed so that Π s can be instantiated with existential Σ index variables Problem first recognized by Xi Formulated a translation to let-normal form Added good programs, but removed them too Current work: a sound, complete let-normal translation Type assignment for intersections and unions in call-by-value languages FOSSACS 03 p.42/42