Type Inference. For the Simply-Typed Lambda Calculus. Peter Thiemann, Manuel Geffken. Albert-Ludwigs-Universität Freiburg. University of Freiburg

Similar documents
Simply Typed Lambda Calculus

Introduction to lambda calculus Part 6

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

A proof of correctness for the Hindley-Milner type inference algorithm

Rank-2 intersection for recursive definitions

Subtyping and Intersection Types Revisited

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

Safety Analysis versus Type Inference

Review. Principles of Programming Languages. Equality. The Diamond Property. The Church-Rosser Theorem. Corollaries. CSE 230: Winter 2007

Beyond First-Order Logic

Type Systems Winter Semester 2006

Programming Languages

Static Program Analysis

Midterm Exam Types and Programming Languages Frank Pfenning. October 18, 2018

BRICS. A Computational Formalization for Partial Evaluation. (Extended Version) Basic Research in Computer Science

Information Flow Inference for ML

Element x is R-minimal in X if y X. R(y, x).

Typing λ-terms. Types. Typed λ-terms. Base Types. The Typing Relation. Advanced Formal Methods. Lecture 3: Simply Typed Lambda calculus

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

Models of computation

CSCI 490 problem set 6

Nunchaku: Flexible Model Finding for Higher-Order Logic

Inductive Definitions with Inference Rules 1 / 27

The Lambda Calculus. Stephen A. Edwards. Fall Columbia University

Induction on Failing Derivations

An extension of HM(X) with bounded existential and universal data-types

On Typability for Rank-2 Intersection Types with Polymorphic Recursion

Lecture Notes on Data Abstraction

Reasoning with Higher-Order Abstract Syntax and Contexts: A Comparison

Lambda-Calculus (I) 2nd Asian-Pacific Summer School on Formal Methods Tsinghua University, August 23, 2010

Mechanizing a Decision Procedure for Coproduct Equality in Twelf

Predicate Logic. Xinyu Feng 09/26/2011. University of Science and Technology of China (USTC)

Functional Database Query Languages as. Typed Lambda Calculi of Fixed Order. Gerd G. Hillebrand and Paris C. Kanellakis

Typed Arithmetic Expressions

Simply Typed Lambda-Calculi (II)

Foundations of Programming Languages. Paul Downen

Introduction to Intuitionistic Logic

An Introduction to Logical Relations Proving Program Properties Using Logical Relations

Principal types Robustness to program transformations Practice. Type constraints for simple types Type constraints for ML Type inference in ML F

A Generalized Let-Polymorphic Type Inference Algorithm

1. Object Calculus. Object calculus is to OO languages what lambda calculus is to functional languages

07 Equational Logic and Algebraic Reasoning

Recitation 2: Binding, Semantics, and Safety : Foundations of Programming Languages

Formal Methods Lecture 8. (B. Pierce's slides for the book Types and Programming Languages )

Categories, Proofs and Programs

On Typability for Rank-2 Intersection Types with Polymorphic Recursion

Lecture Notes on Certifying Theorem Provers

Existence and Consistency in Bounded Arithmetic

CIS 500 Software Foundations Midterm II Answer key November 17, 2004

CS 611 Advanced Programming Languages. Andrew Myers Cornell University. Lecture 26 Type reconstruction. 1 Nov 04. Type reconstruction

Type Systems. Lecture 2 Oct. 27th, 2004 Sebastian Maneth.

Typing-by-encoding. A reductionistic approach to building type systems. François Pottier.

3.2 Reduction 29. Truth. The constructor just forms the unit element,. Since there is no destructor, there is no reduction rule.

CMSC 336: Type Systems for Programming Languages Lecture 10: Polymorphism Acar & Ahmed 19 February 2008

Consequence Relations and Natural Deduction

Safety Analysis versus Type Inference for Partial Types

Simply Typed λ-calculus

COMP6463: λ-calculus

HORSes: format, termination and confluence

Lecture 2: Self-interpretation in the Lambda-calculus

Lambda Calculus. Andrés Sicard-Ramírez. Semester Universidad EAFIT

Towards Correctness of Program Transformations Through Unification and Critical Pair Computation

Introduction to Logic in Computer Science: Autumn 2006

Type Systems. Today. 1. What is the Lambda Calculus. 1. What is the Lambda Calculus. Lecture 2 Oct. 27th, 2004 Sebastian Maneth

arxiv: v3 [cs.pl] 15 May 2011

Lecture Notes: Axiomatic Semantics and Hoare-style Verification

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

Chapter 2. Assertions. An Introduction to Separation Logic c 2011 John C. Reynolds February 3, 2011

Meta-reasoning in the concurrent logical framework CLF

Foundations of Artificial Intelligence

Lambda-Calculus (cont): Fixpoints, Naming. Lecture 10 CS 565 2/10/08

Foundations of Artificial Intelligence

Meta-Reasoning in a Concurrent Logical Framework

Predicate Logic. Xinyu Feng 11/20/2013. University of Science and Technology of China (USTC)

Type Inference Algorithms

Principles of Program Analysis: A Sampler of Approaches

A New Look at Generalized Rewriting in Type Theory

Consequence Relations and Natural Deduction

Code Generation for a Simple First-Order Prover

Principles of Program Analysis: Control Flow Analysis

Type inference in context

Propositional Logic: Part II - Syntax & Proofs 0-0

Call-by-value non-determinism in a linear logic type discipline

Information Flow Inference for ML

On the Semantics of Parsing Actions

Formalisation in Constructive Type Theory of Barendregt s Variable Convention for Generic Structures with Binders

Applied Logic. Lecture 1 - Propositional logic. Marcin Szczuka. Institute of Informatics, The University of Warsaw

CS522 - Programming Language Semantics

Chapter 1 A computational interpretation of forcing in Type Theory

Introduction to λ-calculus

Programming Language Concepts: Lecture 16

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

The Locally Nameless Representation

A Call-by-Name CPS Hierarchy

Context Free Grammars

Limitations of OCAML records

Programming Languages and Types

III.2 (b) Higher level programming concepts for URMs Anton Setzer

Semantics of Higher-Order Functional Programming

Polymorphism, Subtyping, and Type Inference in MLsub

Transcription:

Type Inference For the Simply-Typed Lambda Calculus Albert-Ludwigs-Universität Freiburg Peter Thiemann, Manuel Geffken University of Freiburg 24. Januar 2013

Outline 1 Introduction 2 Applied Lambda Calculus 3 Simple Types for the Lambda Calculus 4 Type Inference for the Simply-Typed Lambda Calculus Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 2 / 25

Foundation: Type Systems Static type systems are the world s most successful application of formal methods (Simon Peyton Jones) Formally, a type system defines a relation between a set of executable syntax and a set of types To express properties of the execution, the typing relation must be compatible with execution Type soundness A type system for analysis must be able to construct a typing from executable syntax Type inference Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 3 / 25

Foundation: Type Inference Type inference is also referred to as type reconstruction. Included in ML, OCaml, Haskell, Scala,...... and to some extend in C#, C++11 We can distinguish different kinds of type inference Global type inference: ML, OCaml, Haskell Infers the most general type Difficult to combine with subtyping Local type inference: Scala, C#,... Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 4 / 25

Example: Global and Local Type Inference Global: No type annotations needed (OCaml) 1 let addtosecond (tuple, i) = 2 let x = snd tuple + i in 3 x;; 4 addtosecond (( a, 1), 1);; Local: Type annotations at function boundaries required (Scala) 1 def addtosecond(tuple:(string,int), i:int) = { 2 var x = tuple. 2 + i; 3 x; 4 } 5 addtosecond(( a, 1), 1); Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 5 / 25

Outline 1 Introduction 2 Applied Lambda Calculus 3 Simple Types for the Lambda Calculus 4 Type Inference for the Simply-Typed Lambda Calculus Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 6 / 25

Applied Lambda Calculus Syntax of Applied Lambda Calculus Let x Var, a countable set of variables, and n N. Exp e ::= x λx.e e e n succ e A term is either a variable, an abstraction (with body e), an application, a numeric constant, or a primitive operation. Conventions Applications associate to the left. The body of an abstraction extends as far right as possible. λxy.e stands for λx.λy.e (and so on). Abstraction and constant are introduction forms, application and primitive operation are elimination forms. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 7 / 25

Applied Lambda Calculus Values of Applied Lambda Calculus Val v ::= λx.e n A value is either an abstraction or a numeric constant. Each value is an expression: Val Exp. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 8 / 25

Variable Occurrences Free and Bound Variables The functions FV( ), BV( ) : Exp P(Var) return the set of free and bound variables of a lambda term, respectively. e FV(e) BV(e) x {x} λx.e FV(e) \ {x} BV(e) {x} e 0 e 1 FV(e 0 ) FV(e 1 ) BV(e 0 ) BV(e 1 ) n succ e FV(e) BV(e) Var(e) := FV(e) BV(e) is the set of variables of e. A lambda term e is closed (e is a combinator) iff FV(e) =. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 9 / 25

Computation in Applied Lambda Calculus Computation defined by term rewriting / reduction Three reduction relations Alpha reduction (alpha conversion) Beta reduction Delta reduction Each relates a family of redexes to a family of contracta. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 10 / 25

Reduction Rules of Lambda Calculus Alpha Conversion Renaming of bound variables λx.e α λy.e[x y] y FV(e) Alpha conversion is often applied tacitly and implicitly. Beta Reduction Only computation step Intuition: Function call (λx.e) f β e[x f ] Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 11 / 25

Reduction Rules, cont d Delta Reduction Operations on built-in types succ n δ n + 1 Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 12 / 25

Reduction Rules, cont d Delta Reduction Operations on built-in types succ n δ n + 1 Reduction in Context In Lambda Calculus, the reduction rules may be applied anywhere in a term. Execution in a programming language is more restrictive. It is usually reduces according to a reduction strategy: call-by-name or call-by-value Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 12 / 25

Reduction Rules, cont d Call-by-Name Reduction Beta e β e AppL f n f SuccL e n e Delta e δ e e n e f e n f e succ e n succ e e n e Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 13 / 25

Reduction Rules, cont d Call-by-Value Reduction Beta-V (λx.e) v v e[x v] AppL f v f f e v f e VAppR e v e v e v v e SuccL e v e succ e v succ e Delta e δ e e v e Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 14 / 25

Computation in Lambda Calculus Computation = Iterated Reduction Let x {n, v}. e x e e x e e x e e x e Outcomes of Computation Starting a computation at e may lead to Nontermination: e, e x e exists e such that e x e Termination: e, e x e such that for all e, e x e e is irreducible. If e is a value, then it is the result of the computation. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 15 / 25

Examples of Irreducible Forms 1 42 2 λfxy.f x y 3 1 λx.x 4 1 2 5 succ λx.x Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 16 / 25

Examples of Irreducible Forms 1 42 2 λfxy.f x y 3 1 λx.x 4 1 2 5 succ λx.x Expected Benefits of a Type System 1 2 are values 3 5 contain elimination forms that try to eliminate non-variables without a corresponding rule (run-time errors) should be ruled out by a type system Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 16 / 25

Outline 1 Introduction 2 Applied Lambda Calculus 3 Simple Types for the Lambda Calculus 4 Type Inference for the Simply-Typed Lambda Calculus Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 17 / 25

Simple Types for the Lambda Calculus Language of types τ ::= α Nat τ τ Typing environment (function from variables to types) Γ ::= Γ, x : τ Typing judgment (relation between terms and types): In typing environment Γ, e has type τ Γ e : τ Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 18 / 25

Inference Rules for STLC Var Γ x : Γ(x) Lam Γ, x : τ e : τ Γ λx.e : τ τ App Γ e 0 : τ τ Γ e 1 : τ Γ e 0 e 1 : τ Num Γ n : Nat Succ Γ e : Nat Γ succ e : Nat Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 19 / 25

Example Inference Tree... f : α α... x : α... f : α α... f x : α f : α α, x : α f (f x) : α f : α α λx.f (f x) : α α λf.λx.f (f x) : (α α) α α Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 20 / 25

Type Soundness Type Preservation If Γ e : τ and e x e, then Γ e : τ. Proof by induction on e e Progress If e : τ, then either e is a value or there exists e such that e x e. Proof by induction on Γ e : τ Type Soundness If e : τ, then either 1 exists v such that e x v or 2 for each e, such that e x e there exists e such that e x e. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 21 / 25

Outline 1 Introduction 2 Applied Lambda Calculus 3 Simple Types for the Lambda Calculus 4 Type Inference for the Simply-Typed Lambda Calculus Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 22 / 25

Type Inference for the Simply-Typed Lambda Calculus (STLC) Typing Problems Type checking: Given environment Γ, a term e and a type τ, is Γ e : τ derivable? Type inference: Given a term e, are there Γ and τ such that Γ e : τ is derivable? Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 23 / 25

Type Inference for the Simply-Typed Lambda Calculus (STLC) Typing Problems Type checking: Given environment Γ, a term e and a type τ, is Γ e : τ derivable? Type inference: Given a term e, are there Γ and τ such that Γ e : τ is derivable? Typing Problems for STLC Type checking and type inference are decidable for STLC Moreover, for each typable e there is a principal typing Γ e : τ such that any other typing is a substitution instance of the principal typing. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 23 / 25

Prerequisites for Type Inference for STLC Unification Let E be a set of equations on types. Unifiers and Most General Unifiers A substitution S is a unifier of E if, for each τ. = τ E, it holds that Sτ = Sτ. A substitution S is a most general unifier of E if S is a unifier of E and for every other unifier S of E, there is a substitution T such that S = T S. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 24 / 25

Prerequisites for Type Inference for STLC Unification Let E be a set of equations on types. Unifiers and Most General Unifiers A substitution S is a unifier of E if, for each τ. = τ E, it holds that Sτ = Sτ. A substitution S is a most general unifier of E if S is a unifier of E and for every other unifier S of E, there is a substitution T such that S = T S. Unification There is an algorithm U that, on input of E, either returns a most general unifier of E or fails if none exists. Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 24 / 25

Principal Type Inference for STLC The algorithm transforms a term into a principal typing judgment for the term (or fails if no typing exists). P(x) = return x : α x : α P(λx.e) = let Γ e : τ P(e) in if x : τ x Γ then return Γ x λx.e : τ x τ else choose α / Var(Γ, τ) in return Γ λx.e : α τ P(e 0 e 1 ) = let Γ 0 e 0 : τ 0 P(e 0 ) in let Γ 1 e 1 : τ 1 P(e 1 ) in with disjoint type variables in (Γ 0, τ 0 ) and (Γ 1, τ 1 ) choose α / Var(Γ 0, Γ 1, τ 0, τ 1 ) in.. let S U(Γ 0 = Γ1, τ 0 = τ1 α) in return SΓ 0 SΓ 1 e 0 e 1 : Sα P( n ) = return n : Nat P(succ e) = let Γ e : τ P(e) in let S U(τ =. Nat) in return Γ succ e : Nat Peter Thiemann, Manuel Geffken Type Inference 24. Januar 2013 25 / 25