An Introduction to Type Inference

Similar documents
One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Lambda Calculus and Type Inference

CITS3211 FUNCTIONAL PROGRAMMING

Types and Type Inference

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

Fundamentals and lambda calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

CMSC 330: Organization of Programming Languages

Constrained Types and their Expressiveness

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

Types, Type Inference and Unification

CMSC 330: Organization of Programming Languages. Lambda Calculus

Polymorphism and Type Inference

To What Extend Is Type Inference for Parameteric Polymorphism Possible in the Presence of Ad-Hoc and Subtype Polymorphism

Conclusions and further reading

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

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Lecture 5: The Untyped λ-calculus

Types and Type Inference

CMSC330. Objects, Functional Programming, and lambda calculus

Lambda Calculus. Variables and Functions. cs3723 1

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

Harvard School of Engineering and Applied Sciences Computer Science 152

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

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

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

> module Lambda where > import Data.List -- I need some standard list functions

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

Why Types Matter. Zheng Gao CREST, UCL

Lambda Calculus and Type Inference

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Introduction to the Lambda Calculus. Chris Lomont

Lambda Calculus and Extensions as Foundation of Functional Programming

Advances in Programming Languages

CS 242. Fundamentals. Reading: See last slide

Lambda Calculus LC-1

Introduction to the λ-calculus

Recursive Definitions, Fixed Points and the Combinator

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

λ calculus is inconsistent

Type Checking and Type Inference

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

Introductory Example

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

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Overloading is NP-complete

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

A system of constructor classes: overloading and implicit higher-order polymorphism

Algorithm W Step by Step

Functional Programming. Big Picture. Design of Programming Languages

Dimensions of Precision in Reference Analysis of Object-oriented Programming Languages. Outline

Programming Languages 2nd edition Tucker and Noonan"

Polymorphism and Type Inference

Type Processing by Constraint Reasoning

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Part I. Historical Origins

Lambda Calculus. Lambda Calculus

Programming language design and analysis

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Lambda Calculus. Gunnar Gotshalks LC-1

dynamically typed dynamically scoped

Overloading, Type Classes, and Algebraic Datatypes

Principles of Programming Languages

Single-pass Static Semantic Check for Efficient Translation in YAPL

Strategies for solving constraints in program analysis

Introduction. chapter Functions

To What Extent Is Type Inference for Parameteric Polymorphism Possible in the Presence of Ad-Hoc and Subtype Polymorphism

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

Uniqueness Typing Redefined

Lambda Calculus see notes on Lambda Calculus

Pure Lambda Calculus. Lecture 17

Balancing Cost and Precision of Approximate Type Inference in Python

Linear lambda calculus and PTIME-completeness

Type Inference with Inequalities

Chapter 11 :: Functional Languages

Concepts of programming languages

JOURNAL OF OBJECT TECHNOLOGY

Programming with Types

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

CSCI-GA Scripting Languages

Where is ML type inference headed?

Programming Language Pragmatics

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Self-applicable Partial Evaluation for Pure Lambda Calculus

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

ML Has Principal Typings

COSE212: Programming Languages. Lecture 0 Course Overview

A Prolog-based Proof Tool for Type Theory TA λ and Implicational Intuitionistic-Logic

Functional Logic Programming. Kristjan Vedel

Fundamentals of type inference systems

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

Presented By : Abhinav Aggarwal CSI-IDD, V th yr Indian Institute of Technology Roorkee. Joint work with: Prof. Padam Kumar

Heuristics for type error discovery and recovery

Transcription:

Sebastian Zimmeck An Introduction to Type Inference Professor Alfred V. Aho - COMS E6998-2 Advanced Topics in Programming Languages and Compilers November 29, 2011

Presentation Overview 1. Introduction 2. Lambda Calculus 3. Hindley-Milner Type Inference 4. Object-oriented Type Inference 5. Concluding Thoughts

1. Introduction Type Inference / Type Reconstruction: Determining the Type of an Expression in a Programming Language Duck Typing: Determining the Type of an Expression from the Way it is used For Type-checked Languages that do not require Declaration of Names Object-oriented Languages: E.g., C# (version 3.0), C++ (C++11) Functional Languages: E.g., ML, OCaml, Haskell

2. Lambda Calculus Lambda Calculus: Small Turing complete Programming Language [1] x = Variable EE = Application λx.e = Lambda Abstraction Grammar: E ::= x EE λx.e λ = Binding Operator (In λx.xy, Variable x is bound, Variable y is free)

2. Lambda Calculus Axioms of Lambda Calculus α-equivalence: Change of bound Variable Name, e.g., λx.e = λy.e[y/x] β-equivalence: Application of Function to Arguments, e.g., (λx.e)y = E[y/x] η-equivalence: Elimination of Redundant Lambda Abstractions, e.g., if x is bound in E, λx.(ex) = E Substitution Process of replacing all free Occurrences of a Variable by an Expression

3. Hindley-Milner Type Inference Hindley-Milner Type System [2,3,4] is based on Lambda Calculus Extension: Let-clause (only syntactic sugar) let x = E in E (let x = E) in E (λx.e )E Hindley-Milner Type Inference = Type Inference Rules + Unification

3. Hindley-Milner Type Inference Hindley-Milner Type Inference Rules [5] 1. For Variables 2. For Applications

3. Hindley-Milner Type Inference Hindley-Milner Type Inference Rules [5] 3. For Lambda Abstractions 4. For Let-Clauses

3. Hindley-Milner Type Inference Unification can be used for Equalizing Type Expressions (or find that they cannot be equalized) [2] Examples of Unification [6] U(Knows(John,x), Knows(John, Jane)) = {x/jane} U(Knows(John,x), Knows(y, Mother(y))) = {y/john, x/mother(john)} U(Knows(John,x), Knows(x, Elizabeth)) = fail

4. Object-oriented Type Inference Polymorphism: Polymorphic code Fragments can be executed with Arguments of different Types Basic Type Inference Algorithm: Early Type Inference Algorithm for Object-oriented Languages [7] Cartesian Product Algorithm: Improves Precision and Efficiency over Previous Algorithms and deals directly with Inheritance [8]

4. Object-oriented Type Inference Basic Type Inference Algorithm [7] Builds a directed Graph with Nodes representing Type Variables and Edges representing Constraints Whenever an Edge is added to the Graph, Type Information is propagated to the next higher Program Level

4. Object-oriented Type Inference Basic Type Inference Algorithm [7] Example x := y As it holds that type(y) type (x), it is ensured that only valid Types are inferred

4. Object-oriented Type Inference Cartesian Product Algorithm [8] Example R = type(rcvr) A = type(arg) rcvr max: arg R = {r_1, r_2,, r_s} A = {a_1, a_2,, a_t}

4. Object-oriented Type Inference Cartesian Product Algorithm [8] 1. The Cartesian Product Algorithm computes the Cartesian Product of the Types R * A = {(r_1,a_1),, (r_1,a_t),, (r_s,a_1),, (r_s,a_t)} 2. Then the Algorithm propagates each (r_i,a_j) into a separate max-template (if such already exists for a given pair, it is reused) 3. All Templates are unioned

4. Object-oriented Type Inference Cartesian Product Algorithm [8]

5. Concluding Thoughts Lambda Calculus provides a fundamental Type Inference Environment, particularly, for Functional Languages Popularity of object-oriented Programming Languages gradually lead to new Type Inference Algorithms and Environments Type Inference is at the Core of Programming Language Design, making it desirable to fully work it out in the early Stages of Language Development Parallelism between Type Checking and Type Inference may provide further Insights

References [1] A. Church. A set of postulates for the foundation of logic. Annals of Mathematics, 33:346-366, 1932. [2] J. R. Hindley. The principal type-scheme of an object in combinatory logic. Transactions of the American Mathematical Society, 146:29-60, 1969. [3] R. Milner. A theory of type polymorphism in programming. Journal of Computer and System Science, 17:348-374, 1978. [4] L. Damas and R. Milner. Principal type-schemes for functional programs. In POPL '82: Proceedings of the 9th ACM SIGAPLAN-SIGACT Symposium on Principles of Programming Languages, pages 207-212. ACM, 1982.

References [5] B. Heeren, J. Hage, and D. Swierstra. Generalizing hindleymilner type inference algorithms. Technical Report UU-CS- 2001-031, Institute of Information and Computing Sciences, Utrecht University, 2002. [6] S. J. Russell and P. Norvig. Artificial Intelligence: A modern Approach. Prentice Hall Series in Artificial Intelligence. Pearson Education, 2nd edition, 2003. [7] J. Palsberg and M. I. Schwartzbach. Objected-oriented type inference. In Proceedings of OOPSLA'91, ACM SIGPLAN Sixth Annual Conference on Object-Oriented Programming Systems, Languages and Applications, pages 146-161, October 1991. [8] O. Agesen. The cartesian product algorithm: Simple and precise type inference of parametric polymorphism. In ECOOP '95 Conference Proceedings, Aarhus, Denmark, August 1995.

Thank You Very Much!