A Polymorphic Type System for Multi-Staged Languages

Similar documents
Typing Multi-Staged Programs and Beyond

Typing & Static Analysis of Multi-Staged Programs

Collage of Static Analysis

Kripke-Style Contextual Modal Type Theory

Meta-programming with Names and Necessity p.1

On the Logical Foundations of Staged Computation

MetaML: structural reflection for multi-stage programming (CalcagnoC, MoggiE, in collaboration with TahaW)

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

Tracing Ambiguity in GADT Type Inference

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

Gradual Typing with Inference

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

Where is ML type inference headed?

Types and Type Inference

CSE-321 Programming Languages 2014 Final

CLF: A logical framework for concurrent systems

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

Simple Unification-based Type Inference for GADTs

Closed Types for a Safe Imperative MetaML

CS558 Programming Languages

MPRI course 2-4 Functional programming languages Exercises

Programming Languages

CSE-321 Programming Languages 2011 Final

Types and Type Inference

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

The Polymorphic Blame Calculus and Parametricity

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

Programming Languages Assignment #7

Programming Languages Lecture 15: Recursive Types & Subtyping

Last class. CS Principles of Programming Languages. Introduction. Outline

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

Polymorphic Delimited Continuations

Type Checking and Type Inference

Lecture #23: Conversion and Type Inference

좋은 발표란 무엇인가? 정영범 서울대학교 5th ROSAEC Workshop 2011년 1월 6일 목요일

Let Arguments Go First

Topic 16: Issues in compiling functional and object-oriented languages

How to reify fresh type variables?

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Theoretical Corner: The Non-Interference Property

Programming Languages and Compilers (CS 421)

CSE-321 Programming Languages 2010 Final

Verifying Program Invariants with Refinement Types

Types, Type Inference and Unification

CMSC 330: Organization of Programming Languages. Lambda Calculus

COS 320. Compiling Techniques

CS558 Programming Languages

Flexible Types. Robust type inference for first-class polymorphism. Daan Leijen. Abstract. 1. Introduction. Microsoft Research

4.5 Pure Linear Functional Programming

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

Lecture 13: Subtyping

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

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

Part VI. Imperative Functional Programming

A New Foundation for Generic Programming

Generalised Recursion in ML and Mixins

Liquid Types. Patrick M. Rondon Ming W. Kawaguchi Ranjit Jhala. Abstract. 1. Introduction

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

Polymorphic Delimited Continuations

Type Inference with Inequalities

GADTs meet Subtyping

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Type Inference; Parametric Polymorphism; Records and Subtyping Section and Practice Problems Mar 20-23, 2018

ML Type Inference and Unification. Arlen Cox

Programming Languages Lecture 14: Sum, Product, Recursive Types

CMSC 330: Organization of Programming Languages

Contextual Effects for Version-Consistent Dynamic Software Updating and Safe Concurrent Programming

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

Mutable References. Chapter 1

Topic 9: Type Checking

Topic 9: Type Checking

Constrained Types and their Expressiveness

A Sound Polymorphic Type System for a Dialect of C

1.3. Conditional expressions To express case distinctions like

Untangling Typechecking of Intersections and Unions

Combining Programming with Theorem Proving

Type Inference for ML

Supplementary Notes on Exceptions

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

Principal Type Schemes for Gradual Programs

Region Analysis for Imperative Languages

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

An Effect System Combining Alias and Liveness for Explicit Memory Reuse (preliminary report)

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

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

CS558 Programming Languages

Let Arguments Go First

A Type System for Functional Traversal-Based Aspects

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Type Reconstruction for General Refinement Types

λ calculus is inconsistent

ML F Raising ML to the Power of System F

Checking Type Safety of Foreign Function Calls

Semantics with Applications 3. More on Operational Semantics

The Substitution Model

Generalised recursion and type inference for intersection types p.1

Transcription:

A Polymorphic Modal Type System for Lisp-like Multi-Staged Languages Ik-Soon Kim 1 Kwangkeun Yi 1 Cristiano Calcagno 2 1 Seoul National University 2 Imperial College POPL 06, 1/12/2006 @ Charleston

Outline 1. Introduction and Challenge 2. Contribution and Ideas 3. Simple Type System 4. Polymorphic Type System 5. Conclusion

Multi-Staged Programming (1/2) program texts (code) as first class objects meta programming A general concept that subsumes macros Lisp/Scheme s quasi-quotation partial evaluation runtime code generation

Multi-Staged Programming (2/2) divides a computation into stages program at stage 0: conventional program program at stage n + 1: code as data at stage n Stage Computation Value 0 usual + code + eval usual + code > 0 code substitution code

Multi-Staged Programming Examples (1/2) In examples, we will use Lisp-style staging constructs + only 2 stages e ::= e code as data, e code substitution eval e execute code

Multi-Staged Programming Examples (1/2) In examples, we will use Lisp-style staging constructs + only 2 stages e ::= e code as data, e code substitution eval e execute code Code as data let NULL = 0 let body = (if e =,NULL then abort()...) in eval body

Multi-Staged Programming Examples (2/2) Specializer/Partial evaluator power(x,n) = if n=0 then 1 else x * power(x,n-1) v.s. power(x,3) = x*x*x prepared as let spower(n) = if n=0 then 1 else (x*,(spower (n-1))) let fastpower10 = eval (λx.,(spower 10)) in fastpower10 2

Review: Practice of Multi-Staged Programming Features of Lisp/Scheme s quasi-quotation system

Review: Practice of Multi-Staged Programming open code (x+1) Features of Lisp/Scheme s quasi-quotation system

Review: Practice of Multi-Staged Programming open code (x+1) intentional variable-capturing substitution at stages > 0 (λx.,(spower 10)) Features of Lisp/Scheme s quasi-quotation system

Review: Practice of Multi-Staged Programming open code (x+1) intentional variable-capturing substitution at stages > 0 (λx.,(spower 10)) capture-avoiding substitution (λ x.,(spower 10) + x) Features of Lisp/Scheme s quasi-quotation system

Review: Practice of Multi-Staged Programming open code (x+1) intentional variable-capturing substitution at stages > 0 (λx.,(spower 10)) capture-avoiding substitution (λ x.,(spower 10) + x) imperative operations with open code cell := (x+1); cell := (y 1); Features of Lisp/Scheme s quasi-quotation system

Challenge A static type system that supports the practice. Should allow programmers both type safety and the expressivenss of Lisp/Scheme s quasi-quote operators Existing type systems support only part of the practice.

Our Contribution A type system for ML + Lisp s quasi-quote system supports multi-staged programming practice open code: (x+1) unrestricted imperative operations with open code intentional var-capturing substitution at stages > 0 capture-avoiding substitution at stages > 0 conservative extension of ML s let-polymorphism principal type inference algorithm

Comparison (1) closed code and eval (2) open code (3) imperative operations (4) type inference (5) var-capturing subst. (6) capture-avoiding subst. (7) polymorphism Our system +1 +2 +3 +4 +5 +6 +7 [Rhiger 2005] +1 +2 +3 4 +5 6 7 [Calcagno et al. 2004] +1 +2 3 +4 5 +6 +7 [Ancona & Moggi 2004] +1 +2 +3 4 5 +6 7 [Taha & Nielson 2003] +1 +2 3 4 5 +6 +7 [Chen & Xi 2003] +1 +2 +3 4 +5 6 +7 [Nanevsky & Pfenning 2002] +1 +2 +3 4 5 +6 7 MetaML/Ocaml[2000,2001] +1 +2 3 +4 5 +6 +7 [Davies 1996] 1 +2 3 4 5 +6 7 [Davies & Pfenning 1996,2001] +1 2 +3 +4 5 +6 7

Ideas code s type: parameterized by its expected context (Γ int) view the type environment Γ as a record type Γ = {x : int, y : int int, } stages by the stack of type environments (modal logic S4) Γ 0 Γ n e : A with due restrictions let-polymorphism for syntactic values monomorphic Γ in code type (Γ int) monomorphic store types Natural ideas worked.

Multi-Staged Language e ::= c x λx.e e e box e code as data e unbox k e code substitution,...,e eval e execute code λ x.e gensym Evaluation where E e n r E: value environment n: a stage number r: a value or err

Operational Semantics (stage n 0) at stage 0: normal evaluation + code + eval at stage > 0: code substitution (EBOX) (EUNBOX) (EEVAL) E e n+1 v E box e n box v E e 0 box v k > 0 E unbox k e k v E e 0 box v E v 0 v E eval e 0 v

Simple Type System (1/2) Type A, B ::= ι A B (Γ A) code type (x+1): ({x : int, } int) typing judgment Γ 0 Γ n e : A

Simple Type System (1/2) Type A, B ::= ι A B (Γ A) code type (x+1): ({x : int, } int) typing judgment Γ 0 Γ n e : A (TSBOX) (TSUNBOX) (TSEVAL) Γ 0 Γ n Γ e : A Γ 0 Γ n box e : (Γ A) Γ 0 Γ n e : (Γ n+k A) Γ 0 Γ n Γ n+k unbox k e : A Γ 0 Γ n e : ( A) Γ 0 Γ n eval e : A (for alpha-equiv. at stage 0)

Simple Type System (2/2) (TSCON) (TSVAR) (TSABS) Γ 0 Γ n c : ι Γ n (x) = A Γ 0 Γ n x : A Γ 0 (Γ n + x : A) e : B Γ 0 Γ n λx.e : A B (TSGENS) (TSAPP) Γ 0 (Γ n + w : A) [x n w] e : B Γ 0 Γ n λ x.e : A B n fresh w Γ 0 Γ n e 1 : A B Γ 0 Γ n e 2 : A Γ 0 Γ n e 1 e 2 : B

Polymorphic Type System (1/4) A combination of ML s let-polymorphism syntactic value restriction + multi-staged expansive n (e) expansive n (e) = False = e never expands the store during its eval. at stages n e.g.) (λx.,e) : can be expansive (λx.eval y) : unexpansive Rémy s record types [Rémy 1993] type environments as record types with field addition record subtyping + record polymorphism

Polymorphic Type System (2/4) if e then (x+1) else 1: then-branch: ({x : int}ρ int) else-branch: (ρ int) ({x : int}ρ int) let x = y in (,x + w); ((,x 1) + z) x: α ρ. ({y : α}ρ α) first x: ({y : int, w : int}ρ int) second x: ({y : int int, z : int}ρ int int)

Polymorphic Type System (3/4) typing judgment (TBOX) 0 n e : A 0 n Γ e : A 0 n box e : (Γ A) (TUNBOX) 0 n e : (Γ A) n+k Γ k > 0 0 n n+k unbox k e : A (TEVAL) 0 n e : ( A) 0 n eval e : A

Polymorphic Type System (4/4) (TVAR) (TABS) (TAPP) (TLETIMP) n (x) A 0 n x : A 0 ( n + x : A) e : B 0 n λx.e : A B 0 n e 1 : A B 0 n e 2 : A 0 n e 1 e 2 : B expansive n (e 1 ) 0 n e 1 : A 0 n + x : A e 2 : B 0 n let (x e 1 ) e 2 : B (TLETAPP) expansive n (e 1 ) 0 n e 1 : A 0 n + x : GEN A ( 0 n ) e 2 : B 0 n let (x e 1 ) e 2 : B

Type Inference Algorithm Unification: Rémy s unification for record type Γ usual unification for new type terms such as (Γ A) and A ref Type inference algorithm: the same structure as top-down version M [Lee and Yi 1998] of the W usual on-the-fly instantiation and unification

Type Inference Algorithm Unification: Rémy s unification for record type Γ usual unification for new type terms such as (Γ A) and A ref Type inference algorithm: the same structure as top-down version M [Lee and Yi 1998] of the W usual on-the-fly instantiation and unification Sound If infer(, e, α) = S then ; e : Sα. Complete If ; e : Rα then infer(, e, α) = S and R = T S for some T.

Conclusion A type system for ML + Lisp s quasi-quote system supports multi-staged programming practice conservative extension to ML s let-polymorphism principal type inference algorithm Exact details, lemmas, proof sketchs, and embedding relations in the paper; full proofs in the technical report.

Conclusion A type system for ML + Lisp s quasi-quote system supports multi-staged programming practice conservative extension to ML s let-polymorphism principal type inference algorithm Exact details, lemmas, proof sketchs, and embedding relations in the paper; full proofs in the technical report. Staged programming practice has a sound static type system.

Conclusion A type system for ML + Lisp s quasi-quote system supports multi-staged programming practice conservative extension to ML s let-polymorphism principal type inference algorithm Exact details, lemmas, proof sketchs, and embedding relations in the paper; full proofs in the technical report. Staged programming practice has a sound static type system. Thank you.