Types, Polymorphism, and Type Reconstruction



Similar documents
Types and Programming Languages

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

Introduction to type systems

CIS 500 Software Foundations Midterm I, Review Questions

CMCS 312: Programming Languages Lecture 3: Lambda Calculus (Syntax, Substitution, Beta Reduction) Acar & Ahmed 17 January 2008

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

If n is odd, then 3n + 7 is even.

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Well-typed programs can t be blamed

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

OPERATIONAL TYPE THEORY by Adam Petcher Prepared under the direction of Professor Aaron Stump A thesis presented to the School of Engineering of

Deterministic Discrete Modeling

NOTES ON TYPES AND PROGRAMMING LANGUAGES

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

Midterm Practice Problems

Notes from February 11

The countdown problem

Find-The-Number. 1 Find-The-Number With Comps

LS.6 Solution Matrices

Summary Last Lecture. Automated Reasoning. Outline of the Lecture. Definition sequent calculus. Theorem (Normalisation and Strong Normalisation)

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

each college c i C has a capacity q i - the maximum number of students it will admit

Indiana State Core Curriculum Standards updated 2009 Algebra I

Notes from Week 1: Algorithms for sequential prediction

6.2 Permutations continued

Some Polynomial Theorems. John Kennedy Mathematics Department Santa Monica College 1900 Pico Blvd. Santa Monica, CA

THE DIMENSION OF A VECTOR SPACE

GROUPS ACTING ON A SET

Boolean Algebra Part 1

INCIDENCE-BETWEENNESS GEOMETRY

CHAPTER 7 GENERAL PROOF SYSTEMS

POLYNOMIAL RINGS AND UNIQUE FACTORIZATION DOMAINS

DEGREES OF ORDERS ON TORSION-FREE ABELIAN GROUPS

How To Know If A Domain Is Unique In An Octempo (Euclidean) Or Not (Ecl)

Full and Complete Binary Trees

Mathematical Induction

Hybrid Type Checking

Bindings, mobility of bindings, and the -quantifier

A SOUND TYPE SYSTEM FOR SECURE FLOW ANALYSIS

1 Homework 1. [p 0 q i+j p i 1 q j+1 ] + [p i q j ] + [p i+1 q j p i+j q 0 ]

WHAT ARE MATHEMATICAL PROOFS AND WHY THEY ARE IMPORTANT?

Chapter 7: Products and quotients

Observational Program Calculi and the Correctness of Translations

NIM with Cash. Abstract. loses. This game has been well studied. For example, it is known that for NIM(1, 2, 3; n)

6.042/18.062J Mathematics for Computer Science. Expected Value I

TAKE-AWAY GAMES. ALLEN J. SCHWENK California Institute of Technology, Pasadena, California INTRODUCTION

CS422 - Programming Language Design

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

3. Mathematical Induction

Type Systems. Luca Cardelli. Microsoft Research

Lecture 19: Introduction to NP-Completeness Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Andrew Pitts chapter for D. Sangorgi and J. Rutten (eds), Advanced Topics in Bisimulation and Coinduction, Cambridge Tracts in Theoretical Computer

G = G 0 > G 1 > > G k = {e}

On strong fairness in UNITY

GREATEST COMMON DIVISOR

Adding GADTs to OCaml the direct approach

Programming Languages Featherweight Java David Walker

Likewise, we have contradictions: formulas that can only be false, e.g. (p p).

RINGS WITH A POLYNOMIAL IDENTITY

MATH10040 Chapter 2: Prime and relatively prime numbers

Journal of Functional Programming, volume 3 number 4, Dynamics in ML

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

Lectures notes on orthogonal matrices (with exercises) Linear Algebra II - Spring 2004 by D. Klain

A Propositional Dynamic Logic for CCS Programs

Towards a Type System for Security APIs

On the generation of elliptic curves with 16 rational torsion points by Pythagorean triples

Every tree contains a large induced subgraph with all degrees odd

System Description: Delphin A Functional Programming Language for Deductive Systems

The Design and Implementation of Typed Scheme

Functional Programming

DEDUCTIVE & INDUCTIVE REASONING

PYTHAGOREAN TRIPLES KEITH CONRAD

11 Ideals Revisiting Z

Mathematical Induction

Translating Generalized Algebraic Data Types to System F

Cosmological Arguments for the Existence of God S. Clarke

= = 3 4, Now assume that P (k) is true for some fixed k 2. This means that

Authenticity by Typing for Security Protocols

THE FUNDAMENTAL THEOREM OF ARBITRAGE PRICING

Jieh Hsiang Department of Computer Science State University of New York Stony brook, NY 11794

Basic Proof Techniques

The Relative Worst Order Ratio for On-Line Algorithms

Automated Theorem Proving - summary of lecture 1

Five High Order Thinking Skills

Modal Proofs as Distributed Programs (Extended Abstract)

Sample Induction Proofs

Rigorous Software Engineering Hoare Logic and Design by Contracts

Blame for All. Jeremy G. Siek. Amal Ahmed. Robert Bruce Findler. Philip Wadler. Abstract. 1. Introduction

Solutions to TOPICS IN ALGEBRA I.N. HERSTEIN. Part II: Group Theory

Degrees of Truth: the formal logic of classical and quantum probabilities as well as fuzzy sets.

Lemma 5.2. Let S be a set. (1) Let f and g be two permutations of S. Then the composition of f and g is a permutation of S.

OutsideIn(X) Modular type inference with local assumptions

Online Convex Optimization

Object-Oriented Type Inference

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

Transcription:

Types, Polymorphism, and Type Reconstruction

Sources This material is based on the following sources: Pierce, B.C., Types and Programming Languages. MIT Press, 2002. Kanellakis, P.C., Mairson, H.G. and Mitchell, J.C., Unification and ML type reconstruction. In Computational Logic: Essays in Honor of Alan Robinson, ed. J.-L. Lassez and G.D. Plotkin, MIT Press, 1991, pages 444 478. Damas L., Milner, R., Principal type-schemes for functional programs. Proceedings of the 9th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, ACM, 1982, pages 207 212. Recommended further reading: Pierce, B.C., Turner D.N., Local type inference. ACM Transactions on Programming Languages and Systems, Volume 22 Issue 1, Jan. 2000, pages 1 44.

The Simply Typed Lambda-Calculus

The simply typed lambda-calculus The system we are about to define is commonly called the simply typed lambda-calculus, or λ for short. Unlike the untyped lambda-calculus, the pure form of λ (with no primitive values or operations) is not very interesting; to talk about λ, we always begin with some set of base types. So, strictly speaking, there are many variants of λ, depending on the choice of base types. For now, we ll work with a variant constructed over the booleans.

Untyped lambda-calculus with booleans t ::= terms x variable λx.t abstraction t t application true constant true false constant false if t then t else t conditional v ::= values λx.t abstraction value true true value false false value

Simple Types T ::= types Bool type of booleans T T types of functions What are some examples?

Type Annotations We now have a choice to make. Do we... annotate lambda-abstractions with the expected type of the argument λx:t 1. t 2 (as in most mainstream programming languages), or continue to write lambda-abstractions as before λx. t 2 and ask the typing rules to guess an appropriate annotation (as in OCaml)? Both are reasonable choices, but the first makes the job of defining the typing rules simpler. Let s take this choice for now.

Typing rules true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-True) (T-False) (T-If)

Typing rules true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T??? λx:t 1.t 2 : T 1 T 2 (T-True) (T-False) (T-If) (T-Abs)

Typing rules true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-True) (T-False) (T-If) Γ, x:t 1 t 2 : T 2 Γ λx:t 1.t 2 : T 1 T 2 x:t Γ Γ x : T (T-Abs) (T-Var)

Typing rules Γ true : Bool Γ false : Bool (T-True) (T-False) Γ t 1 : Bool Γ t 2 : T Γ t 3 : T Γ if t 1 then t 2 else t 3 : T (T-If) Γ, x:t 1 t 2 : T 2 Γ λx:t 1.t 2 : T 1 T 2 x:t Γ Γ x : T (T-Abs) (T-Var) Γ t 1 : T 11 T 12 Γ t 2 : T 11 Γ t 1 t 2 : T 12 (T-App)

Typing Derivations What derivations justify the following typing statements? (λx:bool.x) true : Bool f:bool Bool f (if false then true else false) : Bool f:bool Bool λx:bool. f (if x then false else x) : Bool Bool

Properties of λ The fundamental property of the type system we have just defined is soundness with respect to the operational semantics. 1. Progress: A closed, well-typed term is not stuck If t : T, then either t is a value or else t t for some t. 2. Preservation: Types are preserved by one-step evaluation If Γ t : T and t t, then Γ t : T.

Proving progress inversion lemma for typing relation canonical forms lemma progress theorem

Inversion Lemma: 1. If Γ true : R, then R = Bool. 2. If Γ false : R, then R = Bool. 3. If Γ if t 1 then t 2 else t 3 : R, then Γ t 1 : Bool and Γ t 2, t 3 : R.

Inversion Lemma: 1. If Γ true : R, then R = Bool. 2. If Γ false : R, then R = Bool. 3. If Γ if t 1 then t 2 else t 3 : R, then Γ t 1 : Bool and Γ t 2, t 3 : R. 4. If Γ x : R, then

Inversion Lemma: 1. If Γ true : R, then R = Bool. 2. If Γ false : R, then R = Bool. 3. If Γ if t 1 then t 2 else t 3 : R, then Γ t 1 : Bool and Γ t 2, t 3 : R. 4. If Γ x : R, then x:r Γ.

Inversion Lemma: 1. If Γ true : R, then R = Bool. 2. If Γ false : R, then R = Bool. 3. If Γ if t 1 then t 2 else t 3 : R, then Γ t 1 : Bool and Γ t 2, t 3 : R. 4. If Γ x : R, then x:r Γ. 5. If Γ λx:t 1.t 2 : R, then

Inversion Lemma: 1. If Γ true : R, then R = Bool. 2. If Γ false : R, then R = Bool. 3. If Γ if t 1 then t 2 else t 3 : R, then Γ t 1 : Bool and Γ t 2, t 3 : R. 4. If Γ x : R, then x:r Γ. 5. If Γ λx:t 1.t 2 : R, then R = T 1 R 2 for some R 2 with Γ, x:t 1 t 2 : R 2.

Inversion Lemma: 1. If Γ true : R, then R = Bool. 2. If Γ false : R, then R = Bool. 3. If Γ if t 1 then t 2 else t 3 : R, then Γ t 1 : Bool and Γ t 2, t 3 : R. 4. If Γ x : R, then x:r Γ. 5. If Γ λx:t 1.t 2 : R, then R = T 1 R 2 for some R 2 with Γ, x:t 1 t 2 : R 2. 6. If Γ t 1 t 2 : R, then

Inversion Lemma: 1. If Γ true : R, then R = Bool. 2. If Γ false : R, then R = Bool. 3. If Γ if t 1 then t 2 else t 3 : R, then Γ t 1 : Bool and Γ t 2, t 3 : R. 4. If Γ x : R, then x:r Γ. 5. If Γ λx:t 1.t 2 : R, then R = T 1 R 2 for some R 2 with Γ, x:t 1 t 2 : R 2. 6. If Γ t 1 t 2 : R, then there is some type T 11 such that Γ t 1 : T 11 R and Γ t 2 : T 11.

Canonical Forms Lemma:

Canonical Forms Lemma: 1. If v is a value of type Bool, then

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false.

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type T 1 T 2, then

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type T 1 T 2, then v has the form λx:t 1.t 2.

Progress Theorem: Suppose t is a closed, well-typed term (that is, t : T for some T). Then either t is a value or else there is some t with t t. Proof: By induction

Progress Theorem: Suppose t is a closed, well-typed term (that is, t : T for some T). Then either t is a value or else there is some t with t t. Proof: By induction on typing derivations.

Progress Theorem: Suppose t is a closed, well-typed term (that is, t : T for some T). Then either t is a value or else there is some t with t t. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values.

Progress Theorem: Suppose t is a closed, well-typed term (that is, t : T for some T). Then either t is a value or else there is some t with t t. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values. Consider the case for application, where t = t 1 t 2 with t 1 : T 11 T 12 and t 2 : T 11.

Progress Theorem: Suppose t is a closed, well-typed term (that is, t : T for some T). Then either t is a value or else there is some t with t t. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values. Consider the case for application, where t = t 1 t 2 with t 1 : T 11 T 12 and t 2 : T 11. By the induction hypothesis, either t 1 is a value or else it can make a step of evaluation, and likewise t 2.

Progress Theorem: Suppose t is a closed, well-typed term (that is, t : T for some T). Then either t is a value or else there is some t with t t. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values. Consider the case for application, where t = t 1 t 2 with t 1 : T 11 T 12 and t 2 : T 11. By the induction hypothesis, either t 1 is a value or else it can make a step of evaluation, and likewise t 2. If t 1 can take a step, then rule E-App1 applies to t. If t 1 is a value and t 2 can take a step, then rule E-App2 applies. Finally, if both t 1 and t 2 are values, then the canonical forms lemma tells us that t 1 has the form λx:t 11.t 12, and so rule E-AppAbs applies to t.

Preservation for STLC

Preservation for STLC Theorem: If Γ t : T and t t, then Γ t : T. Proof: By induction

Preservation for STLC Theorem: If Γ t : T and t t, then Γ t : T. Proof: By induction on typing derivations. Which case is the hard one??

Preservation for STLC Theorem: If Γ t : T and t t, then Γ t : T. Proof: By induction on typing derivations. Case T-App: Given t = t 1 t 2 Γ t 1 : T 11 T 12 Γ t 2 : T 11 T = T 12 Show Γ t : T 12

Preservation for STLC Theorem: If Γ t : T and t t, then Γ t : T. Proof: By induction on typing derivations. Case T-App: Given t = t 1 t 2 Γ t 1 : T 11 T 12 Γ t 2 : T 11 T = T 12 Show Γ t : T 12 By the inversion lemma for evaluation, there are three subcases...

The Substitution Lemma Lemma: Types are preserved under substitition. That is, if Γ, x:s t : T and Γ s : S, then Γ [x s]t : T.

The Substitution Lemma Lemma: Types are preserved under substitition. That is, if Γ, x:s t : T and Γ s : S, then Γ [x s]t : T. Proof:...

Weakening and Permutation Two other lemmas will be useful. Weakening tells us that we can add assumptions to the context without losing any true typing statements. Lemma: If Γ t : T and x / dom(γ), then Γ, x:s t : T.

Weakening and Permutation Two other lemmas will be useful. Weakening tells us that we can add assumptions to the context without losing any true typing statements. Lemma: If Γ t : T and x / dom(γ), then Γ, x:s t : T. Permutation tells us that the order of assumptions in (the list) Γ does not matter. Lemma: If Γ t : T and is a permutation of Γ, then t : T.

Weakening and Permutation Two other lemmas will be useful. Weakening tells us that we can add assumptions to the context without losing any true typing statements. Lemma: If Γ t : T and x / dom(γ), then Γ, x:s t : T. Moreover, the latter derivation has the same depth as the former. Permutation tells us that the order of assumptions in (the list) Γ does not matter. Lemma: If Γ t : T and is a permutation of Γ, then t : T. Moreover, the latter derivation has the same depth as the former.

The Substitution Lemma Lemma: If Γ, x:s t : T and Γ s : S, then Γ [x s]t : T. I.e., Types are preserved under substitition.

The Substitution Lemma Lemma: If Γ, x:s t : T and Γ s : S, then Γ [x s]t : T. Proof: By induction on the derivation of Γ, x:s t : T. Proceed by cases on the final typing rule used in the derivation.

The Substitution Lemma Lemma: If Γ, x:s t : T and Γ s : S, then Γ [x s]t : T. Proof: By induction on the derivation of Γ, x:s t : T. Proceed by cases on the final typing rule used in the derivation.

The Substitution Lemma Lemma: If Γ, x:s t : T and Γ s : S, then Γ [x s]t : T. Proof: By induction on the derivation of Γ, x:s t : T. Proceed by cases on the final typing rule used in the derivation. Case T-App: t = t 1 t 2 Γ, x:s t 1 : T 2 T 1 Γ, x:s t 2 : T 2 T = T 1 By the induction hypothesis, Γ [x s]t 1 : T 2 T 1 and Γ [x s]t 2 : T 2. By T-App, Γ [x s]t 1 [x s]t 2 : T, i.e., Γ [x s](t 1 t 2 ) : T.

The Substitution Lemma Lemma: If Γ, x:s t : T and Γ s : S, then Γ [x s]t : T. Proof: By induction on the derivation of Γ, x:s t : T. Proceed by cases on the final typing rule used in the derivation. Case T-Var: t = z with z:t (Γ, x:s) There are two sub-cases to consider, depending on whether z is x or another variable. If z = x, then [x s]z = s. The required result is then Γ s : S, which is among the assumptions of the lemma. Otherwise, [x s]z = z, and the desired result is immediate.

The Substitution Lemma Lemma: If Γ, x:s t : T and Γ s : S, then Γ [x s]t : T. Proof: By induction on the derivation of Γ, x:s t : T. Proceed by cases on the final typing rule used in the derivation. Case T-Abs: t = λy:t 2.t 1 T = T 2 T 1 Γ, x:s, y:t 2 t 1 : T 1 By our conventions on choice of bound variable names, we may assume x y and y / FV(s). Using permutation on the given subderivation, we obtain Γ, y:t 2, x:s t 1 : T 1. Using weakening on the other given derivation (Γ s : S), we obtain Γ, y:t 2 s : S. Now, by the induction hypothesis, Γ, y:t 2 [x s]t 1 : T 1. By T-Abs, Γ λy:t 2. [x s]t 1 : T 2 T 1, i.e. (by the definition of substitution), Γ [x s]λy:t 2. t 1 : T 2 T 1.

Summary: Preservation Theorem: If Γ t : T and t t, then Γ t : T. Lemmas to prove: Weakening Permutation Substitution preserves types Reduction preserves types (i.e., preservation)

Review: Type Systems To define and verify a type system, you must: 1. Define types 2. Specify typing rules 3. Prove soundness: progress and preservation