Programming Language Concepts: Lecture 19

Similar documents
Logic Programming: Lecture 1

The Typed λ Calculus and Type Inferencing in ML

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

Lecture slides & distribution files:

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Lambda Calculus. Variables and Functions. cs3723 1

λ calculus is inconsistent

Lexicografie computationala Feb., 2012

Programming Language Concepts: Lecture 22

Introduction to the Lambda Calculus. Chris Lomont

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

Programming Language Concepts: Lecture 14

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

Programming Languages

Lambda Calculus. Lecture 4 CS /26/10

Introduction to the Lambda Calculus

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

Less naive type theory

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

Recursive Definitions, Fixed Points and the Combinator

CMSC 330: Organization of Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Lecture 5: The Untyped λ-calculus

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

CMSC 330: Organization of Programming Languages

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

Simply-Typed Lambda Calculus

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

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

The Untyped Lambda Calculus

More Lambda Calculus and Intro to Type Systems

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Lambda Calculus and Type Inference

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

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Formal Systems and their Applications

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

An Introduction to the Lambda Calculus

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

Introduction to the λ-calculus

VU Semantik von Programmiersprachen

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

CITS3211 FUNCTIONAL PROGRAMMING

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types

What does my program mean?

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Lambda Calculus. Concepts in Programming Languages Recitation 6:

CS 11 Haskell track: lecture 1

Harvard School of Engineering and Applied Sciences Computer Science 152

Grades. 10% - participation & exercises. 90% - term paper

Polymorphism and System-F (OV)

Lecture 3: Typed Lambda Calculus and Curry-Howard

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

Type Systems Winter Semester 2006

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Lecture Notes on Data Representation

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

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

Functional Programming and Modeling

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

dynamically typed dynamically scoped

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Lecture #23: Conversion and Type Inference

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

Theory and Implementation of a Functional Programming Language

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Type Inference. COS 326 David Walker Princeton University

Functional Languages and Higher-Order Functions

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

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

3.1 λ-calculus: Syntax

1 Introduction. 3 Syntax

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

The Untyped Lambda Calculus

CPS Conversion. Di Zhao.

Polymorphism and Type Inference

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Functional Programming

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

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

An Introduction to Polymorphic Lambda Calculus with Subtyping.

CSE-321 Programming Languages 2011 Final

Untyped Lambda Calculus

1.3. Conditional expressions To express case distinctions like

Exercise 1 ( = 22 points)

CS558 Programming Languages

Lecture Notes on Program Equivalence

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

The Untyped Lambda Calculus

10.6 Theoretical Foundations

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and recursion.

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Transcription:

Programming Language Concepts: Lecture 19 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 19, 01 April 2009

Adding types to λ-calculus The basic λ-calculus is untyped The first functional programming language, LISP, was also untyped Modern languages such as Haskell, ML,...are strongly typed What is the theoretical foundation for such languages?

Types in functional programming The structure of types in Haskell Basic types Int, Bool, Float, Char

Types in functional programming The structure of types in Haskell Basic types Int, Bool, Float, Char Structured types [Lists] If a is a type, so is [a] [Tuples] If a1, a2,..., ak are types, so is (a1,a2,...,ak)

Types in functional programming The structure of types in Haskell Basic types Int, Bool, Float, Char Structured types [Lists] If a is a type, so is [a] [Tuples] If a1, a2,..., ak are types, so is (a1,a2,...,ak) Function types If a, b are types, so is a -> b Function with input a, output b

Types in functional programming The structure of types in Haskell Basic types Int, Bool, Float, Char Structured types [Lists] If a is a type, so is [a] [Tuples] If a1, a2,..., ak are types, so is (a1,a2,...,ak) Function types If a, b are types, so is a -> b Function with input a, output b User defined types Data day = Sun Mon Tue Wed Thu Fri Sat Data BTree a = Nil Node (BTree a) a (Btree a)

Adding types to λ-calculus... Set Λ of untyped lambda expressions is given by Λ = x λx.m MM where x Var, M,M Λ.

Adding types to λ-calculus... Set Λ of untyped lambda expressions is given by Λ = x λx.m MM where x Var, M,M Λ. Add a syntax for basic types When constructing expressions, build up the type from the types of the parts

Adding types to λ-calculus... Restrict our language to have just one basic type, written as τ

Adding types to λ-calculus... Restrict our language to have just one basic type, written as τ No structured types (lists, tuples,...)

Adding types to λ-calculus... Restrict our language to have just one basic type, written as τ No structured types (lists, tuples,...) Function types arise naturally (τ τ, (τ τ) τ τ,...

Simply typed λ-calculus A separate set of variables Var s for each type s

Simply typed λ-calculus A separate set of variables Var s for each type s Define Λ s, expressions of type s, by mutual recursion

Simply typed λ-calculus A separate set of variables Var s for each type s Define Λ s, expressions of type s, by mutual recursion For each type s, every variable x Var s is in Λ s

Simply typed λ-calculus A separate set of variables Var s for each type s Define Λ s, expressions of type s, by mutual recursion For each type s, every variable x Var s is in Λ s If M Λ t and x Var s then (λx.m) Λ s t.

Simply typed λ-calculus A separate set of variables Var s for each type s Define Λ s, expressions of type s, by mutual recursion For each type s, every variable x Var s is in Λ s If M Λ t and x Var s then (λx.m) Λ s t. If M Λ s t and N Λ s then (MN) Λ t. Note that application must be well typed

Simply typed λ-calculus A separate set of variables Var s for each type s Define Λ s, expressions of type s, by mutual recursion For each type s, every variable x Var s is in Λ s If M Λ t and x Var s then (λx.m) Λ s t. If M Λ s t and N Λ s then (MN) Λ t. Note that application must be well typed β rule as usual (λx.m)n β M{x N}

Simply typed λ-calculus A separate set of variables Var s for each type s Define Λ s, expressions of type s, by mutual recursion For each type s, every variable x Var s is in Λ s If M Λ t and x Var s then (λx.m) Λ s t. If M Λ s t and N Λ s then (MN) Λ t. Note that application must be well typed β rule as usual (λx.m)n β M{x N} We must have λx.m Λ s t and N Λ s for some types s,t

Simply typed λ-calculus A separate set of variables Var s for each type s Define Λ s, expressions of type s, by mutual recursion For each type s, every variable x Var s is in Λ s If M Λ t and x Var s then (λx.m) Λ s t. If M Λ s t and N Λ s then (MN) Λ t. Note that application must be well typed β rule as usual (λx.m)n β M{x N} We must have λx.m Λ s t and N Λ s for some types s,t Moreover, if λx.m Λ s t, then x Var s, so x and N are compatible

Simply typed λ-calculus... Extend β to one-step reduction, as usual

Simply typed λ-calculus... Extend β to one-step reduction, as usual The reduction relation is Church-Rosser

Simply typed λ-calculus... Extend β to one-step reduction, as usual The reduction relation is Church-Rosser In fact, satisifies a much strong property

Strong normalization A λ-expression is normalizing if it has a normal form.

Strong normalization A λ-expression is normalizing if it has a normal form. strongly normalizing if every reduction sequence leads to a normal form

Strong normalization A λ-expression is normalizing if it has a normal form. strongly normalizing if every reduction sequence leads to a normal form Examples (λx.xx)(λx.xx) is not normalizing

Strong normalization A λ-expression is normalizing if it has a normal form. strongly normalizing if every reduction sequence leads to a normal form Examples (λx.xx)(λx.xx) is not normalizing (λyz.z)((λx.xx)(λx.xx)) is not strongly normalizing.

Strong normalization... A λ-calculus is strongly normalizing if every term in the calculus is strongly normalizing

Strong normalization... A λ-calculus is strongly normalizing if every term in the calculus is strongly normalizing Theorem The simply typed λ-calculus is strongly normalizing

Strong normalization... A λ-calculus is strongly normalizing if every term in the calculus is strongly normalizing Theorem The simply typed λ-calculus is strongly normalizing Proof intuition Each β-reduction reduces the type complexity of the term Cannot have an infinite sequence of reductions

Type checking Syntax of simply typed λ-calculus permits only well-typed terms

Type checking Syntax of simply typed λ-calculus permits only well-typed terms Converse question; Given an arbitrary term, is it well-typed?

Type checking Syntax of simply typed λ-calculus permits only well-typed terms Converse question; Given an arbitrary term, is it well-typed? For instance, we cannot assign a valid type to f f...... so f f is not a valid expression in this calculus

Type checking Syntax of simply typed λ-calculus permits only well-typed terms Converse question; Given an arbitrary term, is it well-typed? For instance, we cannot assign a valid type to f f...... so f f is not a valid expression in this calculus Theorem The type-checking problem for the simply typed λ-calculus is decidable

Type checking... A term may admit multiple types λx.x can be of type τ τ, (τ τ) (τ τ),...

Type checking... A term may admit multiple types λx.x can be of type τ τ, (τ τ) (τ τ),... Principal type scheme of a term M unique type s such that every other valid type is an instance of s Uniformly replace τ s by another type

Type checking... A term may admit multiple types λx.x can be of type τ τ, (τ τ) (τ τ),... Principal type scheme of a term M unique type s such that every other valid type is an instance of s Uniformly replace τ s by another type τ τ is principal type scheme of λx.x

Type checking... A term may admit multiple types λx.x can be of type τ τ, (τ τ) (τ τ),... Principal type scheme of a term M unique type s such that every other valid type is an instance of s Uniformly replace τ s by another type τ τ is principal type scheme of λx.x Theorem We can always compute the principal type scheme for any well-typed term in the simply typed λ-calculus.

Computability with simple types Church numerals are well typed

Computability with simple types Church numerals are well typed Translations of basic recursive functions (zero, successor, projection) are well-typed

Computability with simple types Church numerals are well typed Translations of basic recursive functions (zero, successor, projection) are well-typed Translation of function composition is well typed

Computability with simple types Church numerals are well typed Translations of basic recursive functions (zero, successor, projection) are well-typed Translation of function composition is well typed Translation of primitive recursion is well typed

Computability with simple types Church numerals are well typed Translations of basic recursive functions (zero, successor, projection) are well-typed Translation of function composition is well typed Translation of primitive recursion is well typed Translation of minimalization requires elimination of recursive definitions Uses untypable expressions of the form f f

Computability with simple types Church numerals are well typed Translations of basic recursive functions (zero, successor, projection) are well-typed Translation of function composition is well typed Translation of primitive recursion is well typed Translation of minimalization requires elimination of recursive definitions Uses untypable expressions of the form f f Minimalization introduces non terminating computations, but we have strong normalization!

Computability with simple types Church numerals are well typed Translations of basic recursive functions (zero, successor, projection) are well-typed Translation of function composition is well typed Translation of primitive recursion is well typed Translation of minimalization requires elimination of recursive definitions Uses untypable expressions of the form f f Minimalization introduces non terminating computations, but we have strong normalization! However, there do exist total recursive functions that are not primitive recursive e.g. Ackermann s function

Polymorphism Simply typed λ-calculus has explicit types

Polymorphism Simply typed λ-calculus has explicit types Languages like Haskell have polymorphic types Compare id :: a -> a with λx.x : τ τ

Polymorphism Simply typed λ-calculus has explicit types Languages like Haskell have polymorphic types Compare id :: a -> a with λx.x : τ τ Second-order polymorhpic typed lambda calculus (System F) Jean-Yves Girard John Reynolds

System F Add type variables, a, b,...

System F Add type variables, a, b,... Use i, j,...to denote concrete types

System F Add type variables, a, b,... Use i, j,...to denote concrete types Type schemes s ::= a i s s a.s

System F Syntax of second order polymorphic lambda calculus Every variable and (type) constant is a term.

System F Syntax of second order polymorphic lambda calculus Every variable and (type) constant is a term. If M is a term, x is a variable and s is a type scheme, then (λx s.m) is a term.

System F Syntax of second order polymorphic lambda calculus Every variable and (type) constant is a term. If M is a term, x is a variable and s is a type scheme, then (λx s.m) is a term. If M and N are terms, so is (MN). Function application does not enforce type check

System F Syntax of second order polymorphic lambda calculus Every variable and (type) constant is a term. If M is a term, x is a variable and s is a type scheme, then (λx s.m) is a term. If M and N are terms, so is (MN). Function application does not enforce type check If M is a term and a is a type variable, then (Λa.M) is a term. Type abstraction

System F Syntax of second order polymorphic lambda calculus Every variable and (type) constant is a term. If M is a term, x is a variable and s is a type scheme, then (λx s.m) is a term. If M and N are terms, so is (MN). Function application does not enforce type check If M is a term and a is a type variable, then (Λa.M) is a term. Type abstraction If M is a term and s is a type scheme, (Ms) is a term. Type application

System F Example A polymorphic identity function Λa.λx a.x

System F Example A polymorphic identity function Λa.λx a.x Two β rules, for two types of abstraction

System F Example A polymorphic identity function Λa.λx a.x Two β rules, for two types of abstraction (λx s.m)n β M{x N}

System F Example A polymorphic identity function Λa.λx a.x Two β rules, for two types of abstraction (λx s.m)n β M{x N} (Λa.M)s β M{a s}

System F System F is also strongly normalizing

System F System F is also strongly normalizing...but type inference is undecidable! Given an arbitrary term, can it be assigned a sensible type?

Type inference in System F Type of a complex expression can be deduced from types assigned to its parts

Type inference in System F Type of a complex expression can be deduced from types assigned to its parts To formalize this, define a relation A M : s A is list {xi : t i } of type assumptions for variables Under the assumptions in A, the expression M has type s.

Type inference in System F Type of a complex expression can be deduced from types assigned to its parts To formalize this, define a relation A M : s A is list {xi : t i } of type assumptions for variables Under the assumptions in A, the expression M has type s. Inference rules to derive type judgments of the form A M : s

Type inference in System F Notation If A is a list of assumptions, A + {x : s} is the list where Assumption for x in A (if any) is overridden by the new assumption x : s. For any variable y x, assumption does not change

Type inference in System F Notation If A is a list of assumptions, A + {x : s} is the list where Assumption for x in A (if any) is overridden by the new assumption x : s. For any variable y x, assumption does not change A + {x : s} M : t A (λx s.m) : s t

Type inference in System F Notation If A is a list of assumptions, A + {x : s} is the list where Assumption for x in A (if any) is overridden by the new assumption x : s. For any variable y x, assumption does not change A + {x : s} M : t A (λx s.m) : s t A M : s t, A N : s A (MN) : t

Type inference in System F Notation If A is a list of assumptions, A + {x : s} is the list where Assumption for x in A (if any) is overridden by the new assumption x : s. For any variable y x, assumption does not change A + {x : s} M : t A (λx s.m) : s t A M : s t, A N : s A (MN) : t A M : s A (Λa.M) : a.s

Type inference in System F Notation If A is a list of assumptions, A + {x : s} is the list where Assumption for x in A (if any) is overridden by the new assumption x : s. For any variable y x, assumption does not change A + {x : s} M : t A (λx s.m) : s t A M : s t, A N : s A (MN) : t A M : s A (Λa.M) : a.s A M : a.s A Mt : s{a t}

Type inference in System F Example Deriving the type of polymorphic identity function Λa.λx a.x

Type inference in System F Example Deriving the type of polymorphic identity function Λa.λx a.x x : a x : a

Type inference in System F Example Deriving the type of polymorphic identity function Λa.λx a.x x : a x : a (λx a.x) : a a

Type inference in System F Example Deriving the type of polymorphic identity function Λa.λx a.x x : a x : a (λx a.x) : a a (Λa.λx a.x) : a.a a

Type inference in System F Type inference is undecidable for System F

Type inference in System F Type inference is undecidable for System F... but we have type-checking algorithms for Haskell, ML,...!

Type inference in System F Type inference is undecidable for System F... but we have type-checking algorithms for Haskell, ML,...! Haskell etc use a restricted version of polymorphic types All types are universally quantified at the top level

Type inference in System F Type inference is undecidable for System F... but we have type-checking algorithms for Haskell, ML,...! Haskell etc use a restricted version of polymorphic types All types are universally quantified at the top level When we write map :: (a -> b) -> [a] -> [b], we mean that the type is map :: a,b. (a b) [a] [b]

Type inference in System F Type inference is undecidable for System F... but we have type-checking algorithms for Haskell, ML,...! Haskell etc use a restricted version of polymorphic types All types are universally quantified at the top level When we write map :: (a -> b) -> [a] -> [b], we mean that the type is map :: a,b. (a b) [a] [b] Also called shallow typing

Type inference in System F Type inference is undecidable for System F... but we have type-checking algorithms for Haskell, ML,...! Haskell etc use a restricted version of polymorphic types All types are universally quantified at the top level When we write map :: (a -> b) -> [a] -> [b], we mean that the type is map :: a,b. (a b) [a] [b] Also called shallow typing System F permits deep typing a. [( b. a b) a a]