Lambda Calculus and Type Inference

Similar documents
Lambda Calculus and Type Inference

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Lambda Calculus and Extensions as Foundation of Functional Programming

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

CMSC 330: Organization of Programming Languages

Lecture 5: The Untyped λ-calculus

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

dynamically typed dynamically scoped

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Programming Languages Lecture 14: Sum, Product, Recursive Types

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

(Refer Slide Time: 4:00)

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

λ calculus is inconsistent

Lambda Calculus. Lecture 4 CS /26/10

Formal Systems and their Applications

The Typed λ Calculus and Type Inferencing in ML

Functional Programming

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

Introduction to the λ-calculus

Recursive Definitions, Fixed Points and the Combinator

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

Fundamentals and lambda calculus

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

Lambda Calculus. Variables and Functions. cs3723 1

VU Semantik von Programmiersprachen

CITS3211 FUNCTIONAL PROGRAMMING

Chapter 5: The Untyped Lambda Calculus

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

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

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

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

Lambda Calculus-2. Church Rosser Property

Introduction to the Lambda Calculus. Chris Lomont

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

CSCI.4430/6969 Programming Languages Lecture Notes

Theory and Implementation of a Functional Programming Language

Programming Languages Lecture 15: Recursive Types & Subtyping

The Untyped Lambda Calculus

Lambda Calculus. Concepts in Programming Languages Recitation 6:

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

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

From the λ-calculus to Functional Programming Drew McDermott Posted

Functional Programming. Overview. Topics. Recall λ-terms. Examples

CSE-321 Programming Languages 2011 Final

Pure Lambda Calculus. Lecture 17

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

Chapter 5: The Untyped Lambda Calculus

Type Checking and Type Inference

Where is ML type inference headed?

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

10.6 Theoretical Foundations

Untyped Lambda Calculus

Arbitrary-rank polymorphism in (GHC) Haskell

List Functions, and Higher-Order Functions

Lambda Calculus. Lambda Calculus

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE505, Fall 2012, Midterm Examination October 30, 2012

Programming Language Concepts: Lecture 19

Lecture #23: Conversion and Type Inference

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

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

1 Scope, Bound and Free Occurrences, Closed Terms

Programming Languages

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

International School on Rewriting (ISR 2015) Rewriting Techniques for Correctness of Program Transformations

(Refer Slide Time: 01:01)

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

Costly software bugs that could have been averted with type checking

Simple Unification-based Type Inference for GADTs

Type Systems COMP 311 Rice University Houston, Texas

Introduction to the Lambda Calculus

Self-applicable Partial Evaluation for Pure Lambda Calculus

Typed Lambda Calculus for Syntacticians

Type Systems Winter Semester 2006

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

Type Processing by Constraint Reasoning

CS 242. Fundamentals. Reading: See last slide

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CIS 500 Software Foundations Fall October 2

Theoretical Corner: The Non-Interference Property

An Introduction to the Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Introductory Example

Transcription:

Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type Inference (revised 2007-08-17)

The Topics Lambda Calculus: a formal calculus for functions and how to compute with them Type Inference: how to find the possible type(s) of expressions, without explicit typing Lambda Calculus and Type Inference (revised 2007-08-17) 1

Lambda Calculus Formal calculus Invented by logicians around 1930 Formal syntax for functions, and function application Gives a certain computational meaning to function application Theorems about reduction order (which possible subcomputation to execute first) This is related to call-by-value/call-by-need Several variations of the calculus Lambda Calculus and Type Inference (revised 2007-08-17) 2

The Simple Untyped Lambda Calculus A term in this calculus is: a variable x, a lambda-abstraction λx.e, or an application e 1 e 2 Some examples: x x y x x λx.(x y) (λx.x) y λx.λy.λx.x Any term can be applied to any term, no concept of (function) types Syntax: function application binds strongest, λx.x y = λx.(x y) (λx.x) y Lambda Calculus and Type Inference (revised 2007-08-17) 3

Untyped Lambda Calculus with Constants We can extend the syntax with constants, for instance: 1, 17, +, [], : We can then write terms closer to Haskell, like 17 + x λx.(x + y) λl.λx.(l : x) Every Haskell program can be translated into an intermediate form, which essentially is a lambda calculus with constants Some constants (like +) can be given a computational meaning, more on this later Lambda Calculus and Type Inference (revised 2007-08-17) 4

Equivalences Some lambda-expressions are considered equivalent (e 1 e 2 ) Rule 1: change of name of bound variable gives an equivalent expression (alpha-conversion) So λx.(x x) λy.(y y) Quite natural, right? However, beware of variable capture: λx.λy.x λy.λy.y Renaming must avoid name clashes with locally bound variables Note that (λx.λy.x) 17=λy.17, whereas (λy.λy.y) 17=λy.y. Different! Lambda Calculus and Type Inference (revised 2007-08-17) 5

Beta-reduction A lambda abstraction applied to an expression can be beta-reduced: (λx.x + x) 9 β 9+9 Beta-reduction means substitute actual argument for symbolic parameter in function body Works also with symbolic arguments: (λx.x + x) (λx.y z) β (λx.y z)+(λx.y z) However, beware of variable capture: (λx.λy.(x + y)) y β λy.(y + y) The fix is to first rename the bound variable y: (λx.λy.(x + y)) y (λx.λz.(x + z)) y β λz.(y + z) Lambda Calculus and Type Inference (revised 2007-08-17) 6

Extending the Equivalence We consider expressions equal if there is a way to convert them into each other, through beta-reductions or inverse beta-reductions For instance, (λx.x) 17 (λy.17) z since (λx.x) 17 β 17 β (λy.17) z Lambda Calculus and Type Inference (revised 2007-08-17) 7

Some Encodings Many mathematical concepts can be encoded in the lambda-calculus That is, they can be translated into the calculus For instance, we can encode the boolean constants, and a conditional (functional if-then-else): T RUE = λx.λy.x F ALSE = λx.λy.y C OND = λp.λq.λr.(p qr) Lambda Calculus and Type Inference (revised 2007-08-17) 8

An example of how C OND works: C OND T RUE A B β (λp.λq.λr.(p qr)) (λx.λy.x) AB β β β β β (λq.λr.((λx.λy.x) qr)) AB (λr.((λx.λy.x) Ar)) B (λx.λy.x) AB λy.a B A Try evaluating C OND F ALSE A B yourself! Lambda Calculus and Type Inference (revised 2007-08-17) 9

Boolean connectives (and, or) can also be encoded As well as lists, integers,... Actually anything you can do in a functional language! Lambda Calculus and Type Inference (revised 2007-08-17) 10

Nontermination Consider this expression: (λx.x x) (λx.x x) What if we beta-reduce it? (λx.x x) (λx.x x) β (λx.x x) (λx.x x) Whoa, we got back the same! Scary... Clearly, we can reduce ad infinitum The lambda-calculus thus contains nonterminating reductions Lambda Calculus and Type Inference (revised 2007-08-17) 11

Recursion Now consider this expression: λh.(λx.h (xx)) (λx.h (x x)) Let s call it Y What if we apply it to a function f? Yf = λh.(λx.h (x x)) (λx.h (x x)) f β β = f (Y f) Hmm, we got back f applied to Y f (λx.f(x x)) (λx.f(x x)) f ((λx.f (x x)) (λx.f (x x))) Lambda Calculus and Type Inference (revised 2007-08-17) 12

Y is called fixed-point combinator It encodes recursion To see why, consider the recursive definition x = fx The solution is x = fff Likewise, Y f = f (Y f)=ff(y f)=fff Thus, x = Y f! Note that all recursive definitions can be written on the form x = fx Lambda Calculus and Type Inference (revised 2007-08-17) 13

Reduction Strategies Any application of a lambda-abstraction in an expression can be beta-reduced Each such position is called a redex An expression can contain several redexes Can you find all redexes in this expression? (λx.((λy.y) x) ((λy.y) x) Try reduce them in different orders! Lambda Calculus and Type Inference (revised 2007-08-17) 14

Does the order of reducing redexes matter? Well, yes and no: Theorem: if two different reduction orders of the same expression end in expressions that cannot be further reduced, then these expressions must be the same However, we can have potentially infinite reductions: (λx.y) ((λx.x x) (λx.x x)) Reducing the outermost redex yields y But the innermost redex can be reduced infinitely many times nontermination! So the order does matter, as regards termination anyway! Lambda Calculus and Type Inference (revised 2007-08-17) 15

Normal Order Reduction Consider this slight adaptation of the previous example: (λx.λx.y) (λx.x) ((λx.x x) (λx.x x)) Let us draw it as a tree: @ @ @ \x > \x > y \x > x \x > x x \x > x x Reducing the leftmost-outermost redex twice yields y Lambda Calculus and Type Inference (revised 2007-08-17) 16

Some other reduction orders do not terminate This is not a coincidence! To reduce the leftmost-outermost redex in each step is called normal order reduction Theorem: if there is a reduction order that terminates, then normal order reduction terminates Normal order reduction corresponds to call-by-need in functional languages Lambda Calculus and Type Inference (revised 2007-08-17) 17

Type Inference You have seen that Haskell systems can find types for expressions: y f = f (y f) y :: (a -> a) -> a As we have mentioned, the most general type is always found Lambda Calculus and Type Inference (revised 2007-08-17) 18

There is an interesting theory behind Haskell-style type inference To infer means to prove, or to deduce A type system is a logic, whose statements are of form expression e has type τ To infer a type means to prove a statement like above A type inference algorithm finds a type if it exists: it is thus a proof search algorithm Such an algorithm exists for Haskell s type system Lambda Calculus and Type Inference (revised 2007-08-17) 19

Hindley-Milner s Type System Haskell s type system extends a simpler type system known as Hindley-Milner s type system (HM) HM does not have type classes, and it is defined over a simple lambda-calculus language Here, we will briefly describe a subset of HM: Language is lambda-calculus with constants (also known functions, like +) Constants have typing given in advance Recursion can be encoded through constant Y (fixed-point combinator) Lambda Calculus and Type Inference (revised 2007-08-17) 20

A language of types: type constants (like I nt) type variables α function types τ τ ( is a type constructor) type schemes α.τ Other type constructors (for list types etc) can easily be added Type schemes like α.α α correspond to polymorphic Haskell types like a -> a Lambda Calculus and Type Inference (revised 2007-08-17) 21

Statements Statements of form A e : τ, where e is expression and τ is a type A is a set of assumptions, of form x : τ (read: variable x has type τ ) A e : τ is read under the assumptions on typings of variables in A, the expression e can have type τ So in order to prove such a statement, we must guess some typings of variables in e and then check that we can give e a consistent type with these assumptions The key in type inference is to make these guesses systematically Lambda Calculus and Type Inference (revised 2007-08-17) 22

Inference Rules Axioms and allowed proof steps are given as a set of inference rules Each inference rule has a number of premises and a conclusion Often written on the form premise 1 premise n conclusion Example (modus ponens in propositional logic): P P = Q Q Axioms are inference rules without premises Lambda Calculus and Type Inference (revised 2007-08-17) 23

Hindley-Milner Inference Rules A selection of rules from the HM inference system: A {x : τ} x : τ A {x : σ} e : τ A λx.e : σ τ A e : σ τ A e : σ A ee : τ A e : α.τ A e : τ[σ/α] [V AR] [ABS] [APP] [SPEC] (τ[σ/α] stands for τ with σ replacing every occurrence of α) Lambda Calculus and Type Inference (revised 2007-08-17) 24

An Example Best way to understand inference rules is to see a derivation Let s infer a type for (λy.(tail y)) nil Extend language of types with list types [τ] Assume given typings for constants: A = {nil : α.[α], tail : α.[α] [α]} Lambda Calculus and Type Inference (revised 2007-08-17) 25

Derivation T A nil : α.[α] A λy.(tail y) :[γ] [γ] A nil :[γ] A (λy.(tail y)) nil :[γ] where T = A {y :[γ]} tail : α.[α] [α] A {y :[γ]} y :[γ] A {y :[γ]} tail :[γ] [γ] A {y :[γ]} tail y :[γ] Lambda Calculus and Type Inference (revised 2007-08-17) 26

Inference Algorithm There is a classical algorithm for type inference in the HM system Called algorithm W Basically a systematic and efficient way to infer types like we did in the example The algorithm uses unification, remember this when you learn logic programming! It has been proved that algorithm W always yields a most general type for any typable expression Most general means that any other possible type for the expression can be obtained from the most general type by instantiating its type variables Lambda Calculus and Type Inference (revised 2007-08-17) 27

A More Practical Type Inference Example Define length [] = 0 length (x:xs) = 1 + length xs Derive the most general type for length! For simplicity, assume that 0 :: Int, 1 :: Int, and (+) :: Int -> Int -> Int See next four slides for how to do it... Lambda Calculus and Type Inference (revised 2007-08-17) 28

Type inference can be seen as equation solving: every declaration gives rise to a type equation constraining the types for the untyped identifiers These equations can be solved to find the types In our example, we already know: 0 :: Int 1 :: Int (+) :: Int -> Int -> Int [] :: [a] (:) :: b -> [b] -> [b] Note different type variable names in types of [] and (:), to make sure they re not mixed up Lambda Calculus and Type Inference (revised 2007-08-17) 29

Solving the equation for the first declaration: length [] = 0 length :: c -> d (since it is applied to one argument) c = [a] (from what we know about the type of []) d = Int (since length [] :: d, 0 :: Int, and both sides of the declaration must have the same type) Thus, length :: [a] -> Int Is this consistent with the second case in declaration of length? Lambda Calculus and Type Inference (revised 2007-08-17) 30

Second declaration length (x:xs) = 1 + length xs Must first find possible types for x, xs, x:xs Assume x :: e, xs :: f e = b, f = [b] (or else x:xs is not well-typed), then x:xs :: [b] Left-hand side: OK if [b] = [a] (so xs :: [a] and x:xs :: [a]), and then length (x:xs) :: Int Right-hand side: xs :: [b], length :: [a] -> Int and xs :: [a] gives length xs :: Int 1 :: Int, length xs :: Int, (+) :: Int -> Int -> Int gives 1 + length xs :: Int Lambda Calculus and Type Inference (revised 2007-08-17) 31

Thus type of LHS = type of RHS! We re done. Result: length :: [a] -> Int Must be a most general type since we were careful not to make any stronger assumptions than necessary about any types (In the formal type system, we would obtain length : α.[α] Int) Lambda Calculus and Type Inference (revised 2007-08-17) 32

Applications of Type Inference HM-style type systems used in some advanced functional languages, most notably ML and Haskell Similar type inference systems can be used for program analysis Types (not the usual ones) can stand for properties e : τ then means expression (program) e has property τ Can be used in optimizing compilers Another application: dimensional analysis, to find whether equations are sound w.r.t. physical dimensions Lambda Calculus and Type Inference (revised 2007-08-17) 33