Lambda Calculus. Variables and Functions. cs3723 1

Similar documents
COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

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

The Untyped Lambda Calculus

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

Pure Lambda Calculus. Lecture 17

1 Scope, Bound and Free Occurrences, Closed Terms

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

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

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

Lambda Calculus. Lambda Calculus

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

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Introduction to the Lambda Calculus

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

CS 242. Fundamentals. Reading: See last slide

Lambda Calculus. Lecture 4 CS /26/10

Type Systems Winter Semester 2006

Functional Programming

The Untyped Lambda Calculus

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

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

Concepts of programming languages

VU Semantik von Programmiersprachen

CIS 500 Software Foundations Fall September 25

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

CITS3211 FUNCTIONAL PROGRAMMING

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

Week 4: Functional Programming

Introduction to the Lambda Calculus. Chris Lomont

The Untyped Lambda Calculus

Fundamentals and lambda calculus

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

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

Formal Systems and their Applications

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

CMSC 330: Organization of Programming Languages

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

Lecture 5: The Untyped λ-calculus

Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

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

Recursive Definitions, Fixed Points and the Combinator

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lexicografie computationala Feb., 2012

Introduction to lambda calculus Part 3

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

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

λ calculus is inconsistent

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

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

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

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

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

Functional Programming

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

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

Chapter 5: The Untyped Lambda Calculus

Chapter 11 :: Functional Languages

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 Extensions as Foundation of Functional Programming

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

dynamically typed dynamically scoped

Programming Language Concepts: Lecture 19

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference

LECTURE 16. Functional Programming

Chapter 5: The Untyped Lambda Calculus

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Untyped Lambda Calculus

Part I. Historical Origins

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

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

Lecture 2: Big-Step Semantics

CSE 505: Concepts of Programming Languages

Introduction to the λ-calculus

3.1 λ-calculus: Syntax

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.

1.3. Conditional expressions To express case distinctions like

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lambda Calculus. Concepts in Programming Languages Recitation 6:

4/19/2018. Chapter 11 :: Functional Languages

Functional Programming. Big Picture. Design of Programming Languages

CSCI.4430/6969 Programming Languages Lecture Notes

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Functional Languages. Hwansoo Han

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

Programming Language Pragmatics

Functional Languages and Higher-Order Functions

Transcription:

Lambda Calculus Variables and Functions cs3723 1

Lambda Calculus Mathematical system for functions Computation with functions Captures essence of variable binding Function parameters and substitution Can be extended with types, expressions, memory stores and side-effects Introduced by Church in 1930s Notation for function expressions Proof system for equality of expressions Calculation rules for function application (invocation) cs3723 2

Pure Lambda Calculus Abstract syntax: M ::= x λx.m M M x represents variable names λx.m is equivalent to (lambda (x) M) in Lisp/Scheme M M is equivalent to (M M) in Lisp/Scheme Each expression is called a lambda term or a lambda expression Concrete syntax: add parentheses to resolve ambiguity (M M) has higher precedence than λx.m; i.e. λx.m N => λx. (M N) M M is left associative; i.e. x y z => (x y) z Compare: concrete syntax in Lisp/Scheme M ::= x (lambda (x) M) (M M) cs3723 3

The Applied Lambda Calculus Can pure lambda calculi express all computation? Yes, it is Turing complete. Other values/operations can be represented as function abstractions. For example, boolean values can be expressed as True = λ t. (λ f. t) False = λ t. (λ f. f) But we are not going to be extreme. The applied lambda calculus M ::= e x λ x.m M M e represents all regular arithmetic expressions Examples of applied lambda calculus Expressions: x+y, x+2*y+z Function abstraction/definition: λ x.(x+y), λz.(x+2*y+z) Function application (invocation): (λ x.(x+y)) 3 cs3723 4

Lambda Calculus In Real Languages Lisp Many different dialects Lisp 1.5, Maclisp,, Scheme,...CommonLisp, This class uses Scheme Function abstraction (allow multiple parameters) λ x. M => (lambda (x) M) λx. λ y. λ z. M => (lambda (x y z) M) Function application M1 M2 => (M1 M2) (M1 M2) M3 => (M1 M2 M3) C (each function must have a name) λ x. λ y. λ z. M => int f(int x,int y,int z) { return M; } (M1 M2) M3 => M1(M2, M3) cs3723 5

Example Lambda Terms Nested function abstractions (definitions) λ s. λ z. z λ s. λ z. s (s z) λ s. λ z. s (s (s z))) Nested function applications (invocations) x y z (λ s. λ z. z) y z (λ s. λ z. s (s z)) ((λ s. λ z. z) y z) cs3723 6

Semantics of Lambda Calculus The lambda calculus language Pure lambda calculus supports only a single type: function Applied lambda calculus supports additional types of values such as int, char, float etc. Evaluation of lambda calculus involves a single operation: function application (invocation) Provide theoretical foundation for reasoning about semantics of functions in Programming Languages Functions are used both as parameters and return values Support higher-order functions; functions are first-class objects. Semantic definitions How to bind variables to values (substitute parameters with values)? How do we know whether two lambda terms are equal? (evaluation) cs3723 7

Evaluating Lambda Calculus What happens in evaluation (λ y. y + 1) x = x + 1 (λ f. λ x. f (f x)) g = λ x. g (g x) (λ f. λ x. f (f x)) (λ y. y+1) = λ x. (λ y. y+1) ((λ y. y+1) x) = λ x. (λ y. y+1) (x+1) = λ x. (x+1)+1 Lambda term evaluation => substitute variables (parameters) with values Each variable is a name (or memory store) that can be given different values When variables are used in expressions, need find the binding location/declaration and get the value cs3723 8

Variable Binding Bound and Free variables Each λ x.m declares a new local variable x x is bound (local) in λ x.m The binding scope of x is M => the occurrences of x in M refers to the λ x declaration Each variable x in a expression M is free (global) if there is no λx in the expression M, or x appears outside all λx declarations in M The binding scope of x is somewhere outside of M Example: λ x. λ y. (z1*x+z2 *y) Bound variables: x, y; free variables: z1, z2 Binding scopes λ x => λ y. (z1*x+z2 *y) λ y => (z1*x+z2 *y) Do variable names matter? λ x. (x+y) = λ z. (z+y) Bound (local) variables: no; Free (global) variables: yes Example: y is both free and bound in λ x. ((λ y. y+2) x) + y cs3723 9

Equality of Lambda Terms α-axiom λx. M = λy. [y/x]m [y/x]m: substitutes y for free occurrences of x in M y cannot already appear in M Example λ x. (x + y) = λ z. (z + y) But λ x. (x + y) λ y. (y + y) β-axiom (λ x. M) N = [N/x] M [N/x]M: substitutes N for free occurrences of x in M Free variables in N cannot be bound in M Example (λ x. λ y. (x + y)) z1 = λ y. (z1+y) But (λ x. λ y. (x + y)) y λ y. (y + y) cs3723 10

Evaluation of Lambda-terms β-reduction (λ x. t1) t2 => [t2/x]t1 where [t2/x]t1 involves renaming as needed Rename bound variables in t1 if they appear free in t2 α-conversion: λ x. M => λ y. [y/x]m (y is not free in M) Replaces all free occurrences of x in t1 with t2 Reduction Repeatedly apply β-reduction to each subexpression Each reducible expression is called a redex The order of applying β-reductions does not matter cs3723 11

Example: Variable Substitution (λ f. λ x. f (f x)) (λ y. y+x) apply twice add x to argument Substitute variables blindly λ x. [(λ y. y+x) ((λ y. y+x) x)] => λ x. x+x+x Rename bound variables (λ f. λ z. f (f z)) (λ y. y+x) => λ z. [(λ y. y+x) ((λ y. y+x) z))] => λ z. z+x+x Easy rule: always rename variables to be distinct cs3723 12

Examples Reduce Lambda Terms (λ x. (x+y)) 3 (λ f. λ x. f (f x)) (λ y. y+1) λ x. (λ y. y x) (λ z. x z) (λ x. (λ y. y x) (λ z. x z) ) (λ y. y y) cs3723 13

Solutions Reduce Lambda Terms (λ x. (x+y)) 3 => 3 + y (λ f. λ x. f (f x)) (λ y. y+1) => λ x. (λ y. y+1) ((λ y. y+1) x) => λ x. (λ y. y+1) (x+1) => λ x. (x+1)+1 λ x. (λ y. y x) (λ z. x z) => λ x. (λ z. x z) x => λ x. x x (λ x. (λ y. y x) (λ z. x z) ) (λ y. y y) => (λ x. x x) (λ y. y y) => (λ y. y y) (λ y. y y) => (λ y. y y) (λ y. y y) cs3723 14

Confluence of Reduction Reduction Repeatedly apply β-reduction to each subexpression Each reducible expression is called a redex Normal form A lambda expression that cannot be further reduced The order of applying β-reductions does not matter Confluence If a lambda expression can be reduced to a normal form, the final result is uniquely determined Ordering of applying reductions does not matter cs3723 15

Termination of Reduction Can all lambda terms be reduced to normal form? No. Some lambda terms do not have a normal form (i.e., their reduction does not terminate) Example non-terminating reductions (λ x. x x) (λ x. x x) =>(λ y. y y) (λ x. x x) =>(λ x. x x) (λ x. x x) Combinators Pure lambda terms without free variables Fixed-point combinator A combinator Y such that given a function f, Y f => f (Y f) Example: Y = λ f. (λ x. f (x x)) (λ x. f (x x)) Yf = (λ 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 (Yf) cs3723 16

Recursion and Fixed Points Recursive functions The body of a function invokes the function Factorial: f(n) = if n=0 then 1 else n*f(n-1) Is it possible to write recursive functions in Lambda Calculus? Yes, using fixed-point combinator More advanced topics (not required) cs3723 17