CMSC 330: Organization of Programming Languages

Similar documents
CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

CMSC330. Objects, Functional Programming, and lambda calculus

Fundamentals and lambda calculus

CMSC 330: Organization of Programming Languages

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

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

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

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

Lecture 5: The Untyped λ-calculus

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

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

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

Lambda Calculus. Lambda Calculus

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

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

CITS3211 FUNCTIONAL PROGRAMMING

Type Systems Winter Semester 2006

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Pure Lambda Calculus. Lecture 17

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

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.

Lambda Calculus and Type Inference

dynamically typed dynamically scoped

Lambda Calculus. Lecture 4 CS /26/10

CMSC 330: Organization of Programming Languages. Operational Semantics

Introduction to the Lambda Calculus

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

Introduction to the λ-calculus

Lambda Calculus and Extensions as Foundation of Functional Programming

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

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

Chapter 5: The Untyped Lambda Calculus

Introductory Example

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

CSCI.4430/6969 Programming Languages Lecture Notes

1 Scope, Bound and Free Occurrences, Closed Terms

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

Concepts of programming languages

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

CIS 500 Software Foundations Fall September 25

Lambda Calculus. Variables and Functions. cs3723 1

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

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

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

Lecture 13 CIS 341: COMPILERS

CMSC 330: Organization of Programming Languages

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

CSE 505: Concepts of Programming Languages

Functional Programming

Recursive Definitions, Fixed Points and the Combinator

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Formal Systems and their Applications

Introduction to the Lambda Calculus. Chris Lomont

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

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

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

The Untyped Lambda Calculus

Elixir, functional programming and the Lambda calculus.

Untyped Lambda Calculus

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Lecture 2: Big-Step Semantics

A Functional Evaluation Model

Untyped Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lambda Calculus-2. Church Rosser Property

Lambda Calculus. Gunnar Gotshalks LC-1

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Functional Programming

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

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

VU Semantik von Programmiersprachen

Lambda Calculus.

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

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

CMSC330 Spring 2017 Midterm 2

Implementing functional languages with abstract machines

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Programming Languages. Dr. Philip Cannata 1

CSCI-GA Scripting Languages

Variables. Substitution

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Transcription:

CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 Summer 2017 1

100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of Moving Bodies CMSC 330 Summer 2017 2

Prioritätsstreit, priority dispute General Theory of Relativity Einstein's field equations presented in Berlin: Nov 25, 1915 Published: Dec 2,1915 CMSC 330 Summer 2017 3

Prioritätsstreit, priority dispute General Theory of Relativity Einstein's field equations presented in Berlin: Nov 25, 1915 Published: Dec 2,1915 David Hilbert's equations presented in Gottingen: Nov 20, 1915 Published: March 6, 1916 CMSC 330 Summer 2017 4

Entscheidungsproblem decision problem Is there an algorithm to determine if a statement is true in all models of a theory? CMSC 330 Summer 2017 5

Entscheidungsproblem decision problem CMSC 330 Summer 2017 6

Turing Machine CMSC 330 Summer 2017 7

Turing Completeness Turing machines are the most powerful description of computation possible They define the Turing-computable functions A programming language is Turing complete if It can map every Turing machine to a program A program can be written to emulate a Turing machine It is a superset of a known Turing-complete language Most powerful programming language possible Since Turing machine is most powerful automaton CMSC 330 Summer 2017 8

Programming Language Expressiveness So what language features are needed to express all computable functions? What s a minimal language that is Turing Complete? Observe: some features exist just for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying or tuples Loops Ø Use recursion Side effects a := 1 while (a < b) Ø Use functional programming pass heap as an argument to each function, return it when with function s result) CMSC 330 Summer 2017 9

Mini C You only have: If statement Plus 1 Minus 1 functions Sum n = 1+2+3+4+5 n in Mini C int add1(int n){return n+1;} int sub1(int n){return n-1;} int add(int a,int b){ } if(b == 0) return a; else return add( add1(a),sub1(b)); int sum(int n){ } if(n == 1) return 1; else return add(n, sum(sub1(n))); int main(){ printf("%d\n",sum(5)); } CMSC 330 Summer 2017 10

Lambda Calculus (λ-calculus) Proposed in 1930s by Alonzo Church (born in Washingon DC!) Formal system Designed to investigate functions & recursion For exploration of foundations of mathematics Now used as Tool for investigating computability Basis of functional programming languages Ø Lisp, Scheme, ML, OCaml, Haskell CMSC 330 Summer 2017 11

Lambda Calculus Syntax A lambda calculus expression is defined as e ::= x variable λx.e abstraction (fun def) e e application (fun call) Ø This grammar describes ASTs; not for parsing (ambiguous!) Ø Lambda expressions also known as lambda terms λx.e is like (fun x -> e) in OCaml That s it! Nothing but higher-order functions CMSC 330 Summer 2017 12

Why Study Lambda Calculus? It is a core language Very small but still Turing complete But with it can explore general ideas Language features, semantics, proof systems, algorithms, Plus, higher-order, anonymous functions (aka lambdas) are now very popular! C++ (C++11), PHP (PHP 5.3.0), C# (C# v2.0), Delphi (since 2009), Objective C, Java 8, Swift, (and functional languages like OCaml, Haskell, F#, ) CMSC 330 Summer 2017 13

Three Conventions Scope of λ extends as far right as possible Subject to scope delimited by parentheses λx. λy.x y is same as λx.(λy.(x y)) Function application is left-associative x y z is (x y) z Same rule as OCaml As a convenience, we use the following syntactic sugar for local declarations let x = e1 in e2 is short for (λx.e2) e1 CMSC 330 Summer 2017 14

OCaml Lambda Calc Interpreter e ::= x λx.e e e type id = string type exp = Var of id Lam of id * exp App of exp * exp y λx.x λx.λy.x y (λx.λy.x y) λx.x x Var y Lam ( x, Var x ) Lam ( x, (Lam ( y, App (Var x, Var y ) App (Lam( x,lam( y,app(var x,var y ))), Lam ( x, App (Var x, Var x ))) CMSC 330 Summer 2017 15

Quiz #1 (λx.y) z and λx.y z are equivalent A. True B. False CMSC 330 Summer 2017 16

Quiz #1 (λx.y) z and λx.y z are equivalent A. True B.False CMSC 330 Summer 2017 17

Quiz #2 What is this term s AST? λx.x x type id = string type exp = Var of id Lam of id * exp App of exp * exp A. App (Lam ( x, Var x ), Var x ) B. Lam ( x, App (Var x,var x )) C. Lam (Var x, Var x, Var x ) D. App (Lam ( x, App ( x, x ))) CMSC 330 Summer 2017 18

Quiz #2 What is this term s AST? λx.x x type id = string type exp = Var of id Lam of id * exp App of exp * exp A. App (Lam ( x, Var x ), Var x ) B. Lam ( x, App (Var x,var x )) C. Lam (Var x, Var x, Var x ) D. App (Lam ( x, App ( x, x ))) CMSC 330 Summer 2017 19

Quiz #3 This term is equivalent to which of the following? λx.x a b A. (λx.x) (a b) B. (((λx.x) a) b) C. λx.(x (a b)) D. (λx.((x a) b)) CMSC 330 Summer 2017 20

Quiz #3 This term is equivalent to which of the following? λx.x a b A. (λx.x) (a b) B. (((λx.x) a) b) C. λx.(x (a b)) D. (λx.((x a) b)) CMSC 330 Summer 2017 21

Lambda Calculus Semantics All we ve got are functions So all we can do is call them To evaluate (λx.e1) e2 Evaluate e1 with x replaced by e2 This application is called beta-reduction (λx.e1) e2 e1[x:=e2] Ø e1[x:=e2] is e1 with occurrences of x replaced by e2 Ø This operation is called substitution Replace formals with actuals Instead of using environment to map formals to actuals We allow reductions to occur anywhere in a term Ø Order reductions are applied does not affect final value! CMSC 330 Summer 2017 22

Beta Reduction Example (λx.λz.x z) y (λx.(λz.(x z))) y // since λ extends to right (λx.(λz.(x z))) y // apply (λx.e1) e2 e1[x:=e2] // where e1 = λz.(x z), e2 = y λz.(y z) // final result Parameters Formal Equivalent OCaml code (fun x -> (fun z -> (x z))) y Actual fun z -> (y z) CMSC 330 Summer 2017 23

Beta Reduction Examples (λx.x) z (λx.y) z z y (λx.x y) z z y A function that applies its argument to y CMSC 330 Summer 2017 24

Beta Reduction Examples (cont.) (λx.x y) (λz.z) (λz.z) y y (λx.λy.x y) z λy.z y A curried function of two arguments Applies its first argument to its second (λx.λy.x y) (λz.zz) x (λy.(λz.zz)y)x (λz.zz)x xx CMSC 330 Summer 2017 25

Beta Reduction Examples (cont.) (λx.x (λy.y)) (u r) (λx.(λw. x w)) (y z) CMSC 330 Summer 2017 26

Beta Reduction Examples (cont.) (λx.x (λy.y)) (u r) (u r) (λy.y) (λx.(λw. x w)) (y z) (λw. (y z) w) CMSC 330 Summer 2017 27

Quiz #4 λx.y z can be beta-reduced to A. y B. y z C.z D. cannot be reduced CMSC 330 Summer 2017 28

Quiz #4 λx.y z can be beta-reduced to A. y B. y z C.z D. cannot be reduced CMSC 330 Summer 2017 29

Quiz #5 Which of the following reduces to λz. z? a) (λy. λz. x) z b) (λz. λx. z) y c) (λy. y) (λx. λz. z) w d) (λy. λx. z) z (λz. z) CMSC 330 Summer 2017 30

Quiz #5 Which of the following reduces to λz. z? a) (λy. λz. x) z b) (λz. λx. z) y c) (λy. y) (λx. λz. z) w d) (λy. λx. z) z (λz. z) CMSC 330 Summer 2017 31

Static Scoping & Alpha Conversion Lambda calculus uses static scoping Consider the following (λx.x (λx.x)) z? Ø The rightmost x refers to the second binding This is a function that Ø Takes its argument and applies it to the identity function This function is the same as (λx.x (λy.y)) Renaming bound variables consistently preserves meaning Ø This is called alpha-renaming or alpha conversion Ex. λx.x = λy.y = λz.z λy.λx.y = λz.λx.z CMSC 330 Summer 2017 32

Quiz #6 Which of the following expressions is alpha equivalent to (alpha-converts from) (λx. λy. x y) y a) λy. y y b) λz. y z c) (λx. λz. x z) y d) (λx. λy. x y) z CMSC 330 Summer 2017 33

Quiz #6 Which of the following expressions is alpha equivalent to (alpha-converts from) (λx. λy. x y) y a) λy. y y b) λz. y z c) (λx. λz. x z) y d) (λx. λy. x y) z CMSC 330 Summer 2017 34

Defining Substitution Use recursion on structure of terms x[x:=e] = e y[x:=e] = y // Replace x by e // y is different than x, so no effect (e1 e2)[x:=e] = (e1[x:=e]) (e2[x:=e]) // Substitute both parts of application (λx.e )[x:=e] = λx.e Ø In λx.e, the x is a parameter, and thus a local variable that is different from other x s. Implements static scoping. Ø So the substitution has no effect in this case, since the x being substituted for is different from the parameter x that is in e (λy.e )[x:=e] =? Ø The parameter y does not share the same name as x, the variable being substituted for Ø Is λy.(e [x:=e]) correct? No CMSC 330 Summer 2017 35

Variable capture How about the following? (λx.λy.x y) y? When we replace y inside, we don t want it to be captured by the inner binding of y, as this violates static scoping I.e., (λx.λy.x y) y λy.y y Solution (λx.λy.x y) is the same as (λx.λz.x z) Ø Due to alpha conversion So alpha-convert (λx.λy.x y) y to (λx.λz.x z) y first Ø Now (λx.λz.x z) y λz.y z CMSC 330 Summer 2017 36

Completing the Definition of Substitution Recall: we need to define (λy.e )[x:=e] We want to avoid capturing (free) occurrences of y in e Solution: alpha-conversion! Ø Change y to a variable w that does not appear in e or e (Such a w is called fresh) Ø Replace all occurrences of y in e by w. Ø Then replace all occurrences of x in e by e! Formally: (λy.e )[x:=e] = λw.((e [y:=w]) [x:=e]) (w is fresh) CMSC 330 Summer 2017 37

Beta-Reduction, Again Whenever we do a step of beta reduction (λx.e1) e2 e1[x:=e2] We must alpha-convert variables as necessary Sometimes performed implicitly (w/o showing conversion) Examples (λx.λy.x y) y = (λx.λz.x z) y λz.y z // y z (λx.x (λx.x)) z = (λy.y (λx.x)) z z (λx.x) // x y CMSC 330 Summer 2017 38

OCaml Implementation: Substitution m[y:=e] (* substitute e for y in m-- *) let rec subst m y e = match m with Var x -> if y = x then e (* substitute *) else m (* don t subst *) App (e1,e2) -> App (subst e1 y e, subst e2 y e) Lam (x,e0) -> CMSC 330 Summer 2017 39

OCaml Impl: Substitution (cont d) (* substitute e for y in m *) let rec subst m y e = match m with Lam (x,e0) -> if y = x then m else if not (List.mem x (fvs e)) then Lam (x, subst e0 y e) else Might capture; need to α-convert let z = newvar() in (* fresh *) let e0' = subst e0 x (Var z) in Lam (z,subst e0' y e) Shadowing blocks substitution Safe: no capture possible CMSC 330 Summer 2017 40

OCaml Impl: Reduction let rec reduce e = match e with App (Lam (x,e), e2) -> subst e x e2 App (e1,e2) -> let e1' = reduce e1 in if e1'!= e1 then App(e1',e2) else App (e1,reduce e2) Lam (x,e) -> Lam (x, reduce e) _ -> e nothing to do Straight β rule Reduce lhs of app Reduce rhs of app Reduce function body CMSC 330 Summer 2017 41

Quiz #7 Beta-reducing the following term produces what result? (λx.x λy.y x) y A. y (λz.z y) B. z (λy.y z) C. y (λy.y y) D. y y CMSC 330 Summer 2017 42

Quiz #7 Beta-reducing the following term produces what result? (λx.x λy.y x) y A. y (λz.z y) B. z (λy.y z) C. y (λy.y y) D. y y CMSC 330 Summer 2017 43

Quiz #8 Beta reducing the following term produces what result? λx.(λy. y y) w z a) λx. w w z b) λx. w z c) w z d) Does not reduce CMSC 330 Summer 2017 44

Quiz #8 Beta reducing the following term produces what result? λx.(λy. y y) w z a) λx. w w z b) λx. w z c) w z d) Does not reduce CMSC 330 Summer 2017 45