CMSC 330: Organization of Programming Languages Lambda Calculus and Types.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
The lambda calculus David Walker CS 441. the (untyped) lambda calculus a language of functions e ::= x | \x.e | e1 e2 v ::= \x.e there are several different.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Type checking © Marcelo d’Amorim 2010.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Foundations of Programming Languages: Introduction to Lambda Calculus
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
MinML: an idealized programming language CS 510 David Walker.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
Imperative Programming
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
1 440 Midterm 2012 fall. Given the following two classes: public class A { void doSomething () { System.out.println("A"); } public class B extends A{
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Fixed Point Illustrations. Simple Examples f : int -> int f(n) = 5 n = 5 is a unique fixed point. f(n) = n^2 – 2 n = 2 and n = -1 are both fixed points.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
CS 363 Comparative Programming Languages Semantics.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Chapter 3 Part II Describing Syntax and Semantics.
CMSC 330: Organization of Programming Languages
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CMSC 330: Organization of Programming Languages Operational Semantics.
CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
Dr. Philip Cannata 1 Programming Languages Chapters 24 – 29 Types, Type Judgments, Type Systems and Type Safety - Chapters Explicit Polymorphism.
The Church-Turing Thesis Chapter Are We Done? FSM  PDA  Turing machine Is this the end of the line? There are still problems we cannot solve:
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
CS5205: Foundation in Programming Languages Type Reconstruction
CS5205: Foundations in Programming Languages
Functional Programming
Programming Languages and Compilers (CS 421)
CS 550 Programming Languages Jeremy Johnson
Lambda Calculus CSE 340 – Principles of Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
The Metacircular Evaluator
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
Programming Languages Dan Grossman 2013
Presentation transcript:

CMSC 330: Organization of Programming Languages Lambda Calculus and Types

CMSC 3302 Lambda Calculus A lambda calculus expression is defined as e ::= xvariable | λx.efunction | e efunction application λx.e is like (fun x -> e) in OCaml That’s it! Only higher-order functions

CMSC 3303 Beta-Reduction, Again Whenever we do a step of beta reduction... –(λx.e1) e2 → e1[x/e2] –...alpha-convert variables as necessary Examples: –(λx.x (λx.x)) z = (λx.x (λy.y)) z → z (λy.y)‏ –(λx.λy.x y) y = (λx.λz.x z) y → λz.y z

CMSC 3304 Encodings It turns out that this language is Turing complete That means we can encode any computation we want in it –...if we’re sufficiently clever...

CMSC 3305 Booleans true = λx.λy.x false = λx.λy.y if a then b else c is defined to be the λ expression: a b c Examples: –if true then b else c → (λx.λy.x) b c → (λy.b) c → b –if false then b else c → (λx.λy.y) b c → (λy.y) c → c

CMSC 3306 Booleans (continued)‏ Other Boolean operations: not = λx.((x false) true)‏ not true → λx.((x false) true) true → ((true false) true) → false and = λx.λy.((x y) false)‏ or = λx.λy.((x true) y)‏ Given these operations, can build up a logical inference system Exercise: Show that not, and and or have the desired properties

CMSC 3307 Pairs (a,b) = λx.if x then a else b fst = λf.f true snd = λf.f false Examples: –fst (a,b) = (λf.f true) (λx.if x then a else b) → (λx.if x then a else b) true → if true then a else b → a –snd (a,b) = (λf.f false) (λx.if x then a else b) → (λx.if x then a else b) false → if false then a else b → b

CMSC 3308 Natural Numbers (Church*)‏ 0 = λf.λy.y 1 = λf.λy.f y 2 = λf.λy.f (f y)‏ 3 = λf.λy.f (f (f y))‏ i.e., n = λf.λy. succ = λz.λf.λy.f (z f y)‏ iszero = λg.g (λy.false) true –Recall that this is equivalent to λg.((g (λy.false)) true)‏ *(Named after Alonzo Church, developer of lambda calculus)‏

CMSC 3309 Natural Numbers (cont’d)‏ Examples: succ 0 = (λz.λf.λy.f (z f y)) (λf.λy.y) → λf.λy.f ((λf.λy.y) f y) → λf.λy.f y = 1 iszero 0 = (λz.z (λy.false) true) (λf.λy.y) → (λf.λy.y) (λy.false) true → (λy.y) true → true

CMSC Arithmetic defined Addition, if M and N are integers (as λ expressions): M + N = λx.λy.(M x)((N x) y)‏ Equivalently: + = λM.λN.λx.λy.(M x)((N x) y)‏ Multiplication: M * N = λx.(M (N x))‏ Prove 1+1 = = λx.λy.(1 x)((1 x) y) → λx.λy.((λx.λy.x y) x)(((λx.λy.x y) x) y) → λx.λy.(λy.x y)(((λx.λy.x y) x) y) → λx.λy.(λy.x y)((λy.x y) y) → λx.λy.x ((λy.x y) y) → λx.λy.x (x y) = 2 With these definitions, can build a theory of integer arithmetic.

CMSC Looping Define D = λx.x x Then –D D = (λx.x x) (λx.x x) → (λx.x x) (λx.x x) = D D So D D is an infinite loop –In general, self application is how we get looping

CMSC The “Paradoxical” Combinator Y = λf.(λx.f (x x)) (λx.f (x x))‏ Then Y F = (λf.(λx.f (x x)) (λx.f (x x))) F → (λx.F (x x)) (λx.F (x x)) → F ((λx.F (x x)) (λx.F (x x)))‏ = F (Y F)‏ Thus Y F = F (Y F) = F (F (Y F)) =...

CMSC Example fact = λf. λn.if n = 0 then 1 else n * (f (n-1))‏ –The second argument to fact is the integer –The first argument is the function to call in the body We’ll use Y to make this recursively call fact (Y fact) 1 = (fact (Y fact)) 1 → if 1 = 0 then 1 else 1 * ((Y fact) 0)‏ → 1 * ((Y fact) 0)‏ → 1 * (fact (Y fact) 0)‏ → 1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1))‏ → 1 * 1 → 1

CMSC Discussion Using encodings we can represent pretty much anything we have in a “real” language –But programs would be pretty slow if we really implemented things this way –In practice, we use richer languages that include built- in primitives Lambda calculus shows all the issues with scoping and higher-order functions It's useful for understanding how languages work

CMSC The Need for Types Consider the untyped lambda calculus –false = λx.λy.y –0 = λx.λy.y Since everything is encoded as a function... –We can easily misuse terms false 0 → λy.y if 0 then... Everything evaluates to some function The same thing happens in assembly language –Everything is a machine word (a bunch of bits)‏ –All operations take machine words to machine words

CMSC What is a Type System? A type system is some mechanism for distinguishing good programs from bad –Good = well typed –Bad = ill typed or not typable; has a type error Examples –0 + 1// well typed –false 0// ill-typed; can’t apply a boolean

CMSC Static versus Dynamic Typing In a static type system, we guarantee at compile time that all program executions will be free of type errors –OCaml and C have static type systems In a dynamic type system, we wait until runtime, and halt a program (or raise an exception) if we detect a type error –Ruby has a dynamic type system Java, C++ have a combination of the two

CMSC Simply-Typed Lambda Calculus e ::= n | x | λx:t.e | e e –We’ve added integers n as primitives Without at least two distinct types (integer and function), can’t have any type errors –Functions now include the type of their argument t ::= int | t → t –int is the type of integers –t1 → t2 is the type of a function that takes arguments of type t1 and returns a result of type t2 –t1 is the domain and t2 is the range –Notice this is a recursive definition, so that we can give types to higher-order functions

CMSC Type Judgments We will construct a type system that proves judgments of the form A ⊢ e : t –“In type environment A, expression e has type t” If for a program e we can prove that it has some type, then the program type checks –Otherwise the program has a type error, and we’ll reject the program as bad

CMSC Type Environments A type environment is a map from variables names to their types is the empty type environment A, x:t is just like A, except x now has type t When we see a variable in the program, we’ll look up its type in the environment

CMSC Type Rules A ⊢ n : int e ::= n | x | λx:t.e | e e A ⊢ x : t‏ x:t ∊ A A ⊢ λx:t.e : t → t' A, x : t ⊢ e : t' A ⊢ e e' : t' A ⊢ e : t → t'A ⊢ e' : t TInt TFun TVar TApp

CMSC Example A ⊢ (λx:int.+ x 3) 4 : int A = { + : int → int → int } A ⊢ (λx:int.+ x 3) : int → intA ⊢ 4 : int B ⊢ + x 3 : int B ⊢ 3 : int B ⊢ + : i→i→i B ⊢ + x : int → int B ⊢ x : int B = A, x : int

CMSC Discussion The type rules provide a way to reason about programs (ie. a formal logic)‏ –The tree of judgments we just saw is a kind of proof in this logic that the program has a valid type So the type checking problem is like solving a jigsaw puzzle –Can we apply the rules to a program in such a way as to produce a typing proof? –We can do this automatically

CMSC An Algorithm for Type Checking (Write this in OCaml!)‏ TypeCheck : type env × expression → type TypeCheck(A, n) = int TypeCheck(A, x) = if x in A then A(x) else fail TypeCheck(A, λx:t.e) = let t' = TypeCheck((A, x:t), e) in t → t' TypeCheck(A, e1 e2) = let t1 = TypeCheck(A, e1) in let t2 = TypeCheck(A, e2) in if dom(t1) = t2 then range(t1) else fail

CMSC Type Inference We could extend the rules to allow the type checker to deduce the types of every expression in a program even without the annotations –This is called type inference –Not covered in this class

CMSC Summary Lambda calculus shows all the issues with scoping and higher-order functions It's useful for understanding how languages work

CMSC Practice Reduce the following: –(λx.λy.x y y) (λa.a) b –(or true) (and true false)‏ –(* 1 2) (* m n = λM.λN.λx.(M (N x)) )‏ Derive and prove the type of: –A ⊢ (λf:int->int.λn:int.f n) (λx:int. + 3 x) 6 –λx:int->int->int. λy:int->int. λz:int.x z (y z)‏ A = { + : int → int → int }