Lambda Calculus LC-1

Similar documents
Lambda Calculus. Gunnar Gotshalks LC-1

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Lambda Calculus see notes on Lambda Calculus

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Languages

Functional Programming

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

FP Foundations, Scheme

Functional programming with Common Lisp

Chapter 1. Fundamentals of Higher Order Programming

Introductory Example

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

CS 314 Principles of Programming Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

15 Unification and Embedded Languages in Lisp

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

CSCE 314 Programming Languages

1.3. Conditional expressions To express case distinctions like

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Imperative, OO and Functional Languages A C program is

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Introduction to the Lambda Calculus

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

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

Functional Languages. Hwansoo Han

CS 242. Fundamentals. Reading: See last slide

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

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

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming Languages

Functional Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Formal Systems and their Applications

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Lambda Calculus. Variables and Functions. cs3723 1

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

CSCC24 Functional Programming Scheme Part 2

Control Flow February 9, Lecture 7

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

CMSC 330: Organization of Programming Languages

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

CMSC 330: Organization of Programming Languages. Lambda Calculus

Z Notation. June 21, 2018

Nano-Lisp The Tutorial Handbook

CS115 - Module 9 - filter, map, and friends

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

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Functional Programming Languages (FPL)

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

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

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Lambda Calculus. Lambda Calculus

Lecture 5: The Halting Problem. Michael Beeson

Evaluating Scheme Expressions

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

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

Scheme Datatypes. expressions. pairs. atoms. lists. symbols. booleans

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

From the λ-calculus to Functional Programming Drew McDermott Posted

CMSC 330: Organization of Programming Languages

CS 314 Principles of Programming Languages

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

Project 2: Scheme Interpreter

CSE 341: Programming Languages

Lisp Basic Example Test Questions

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

SOFTWARE ARCHITECTURE 6. LISP

CS 360 Programming Languages Interpreters

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Week 4: Functional Programming

Recursive Functions of Symbolic Expressions and Their Application, Part I

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

CMSC 330: Organization of Programming Languages

Introduction to lambda calculus Part 3

A Small Interpreted Language

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

Chapter 15. Functional Programming Languages

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

Recursion Chapter 4 Self-Reference. Recursive Definitions Inductive Proofs Implementing Recursion

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

An introduction to Scheme

Comp 311: Sample Midterm Examination

Programming Language Pragmatics

CSc 520 Principles of Programming Languages

Lecture08: Scope and Lexical Address

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Chapter 7 Control I Expressions and Statements

CMSC330. Objects, Functional Programming, and lambda calculus

Transcription:

Lambda Calculus LC-1

λ- Calculus History-1 Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of λ-calculus is equivalent in power to a Turing machine Turing machines and λ-calculus are alternate descriptions of our understanding of what is computable In the mid to late 1950 s, John McCarthy developed Lisp A programming language based on λ-calculus Implementation includes syntactic sugar functions and forms that do not add to the power of what we can compute but make programs simpler and easier to understand LC-2

λ- Calculus Basis Mathematical theory for anonymous functions functions that have not been bound to names Present a subset of full definition to present the flavour Notation and interpretation scheme identifies functions and their application to operands argument-paramter binding Clearly indicates which variables are free and which are bound LC-3

Bound and Free Variables Bound variables are similar to local variables in Java (or any procedural language) function Changing the name of a bound variable (consistently) does not change the meaning (semantics) of a function Free variables are similar to global variables in Java (or any procedural language) function Changing the name of a free variable normally changes the meaning (semantics) of a function. LC-4

λ-functions 1 Consider following expression (u + 1)(u - 1) is u bound or free? Disambiguate the expression with the following λ-function ( λ u. (u + 1)(u - 1) ) bound variables defining form Clearly indicates that u is a bound variable Note the parallel with programming language functions void functionname( arguments) { function definition } It seems obvious now but that is because programming languages developed out of these mathematical notions LC-5

λ-functions 2 Consider the following expression (u + a)(u + b) Can have any of the following functions, depending on what you mean (λ u. (u + a)(u + b) ) u is bound, a and b are free (defined in the enclosing context) (λ u,b. (u + a)(u + b) ) u and b are bound, a is free (λ u,a,b. (u + a)(u + b) ) u, a and b are all bound, no free variables in the expression LC-6

Function application Functions are applied to arguments in a list immediately following the λ-function {λ u. (u + 1) (u + 2)} [3] ==> (3 + 1) (3 + 2) ==> 20 {λ u. (u + a) (u + b)} [7-1] ==> (6 + a) (6 + b) and no further in this context {λ u, v. (u - v) (u + v)} [2p + q, 2p - q] ==> ((2p+q) - (2p - q)) ((2p + q) + (2p - q)) Can pass expressions to a variable Different types of parenthesis can be used to make the expressions more readable Other than visual impact it doesn t matter which type of parenthesis is used as long as they are correctly matched. LC-7

Using auxiliary definitions Build up longer definitions with auxiliary definitions Define u/(u+5) where u = a(a+1) where a = 7-3 { λ u. u/(u+5) } [ { λ a. a(a+1) } [ 7-3 ] ] Note the nested function definition and argument application ==> { λ u. u/(u+5) } [ 4 (4+1) ] ==> { 20 / (20+5) } ==> 0.8 LC-8

Functions are Variables Define f(3) + f(5) where f(x) = ax(a + x) where a = 7-3 { λ f. f(3) + f(5) } [ { λ a. { λ x. a x (a + x) } } [ 7-3 ] ] Working from the inside out and right to left as arguments must be evaluated first ==> { λ f. f(3) + f(5) } [ { λ x. 4 x (4 + x) } ] ==> { λ x. 4 x (4 + x) } (3) + { λ x. 4 x (4 + x) } (5) ==> 4*3(4+3) + 4*5(4 + 5) ==> 364 LC-9

Lamba notation in Lisp Lambda λ expressions are a direct analogue of λ-calculus expressions They are the basis of Lisp functions, just a modified syntax to simplify the interpreter For example (defun double (x) (+ x x)) is the named version of the following unnamed lambda expression (lambda (x) (+ x x)) Note the similar syntax with λ-calculus and the change to prefix, from infix, to permit a uniform syntax for functions of any number of arguments { λ x. (x + x) } LC-10

Anonymous functions Recall in the abstraction for sumint we defined support functions to handle each case (defun double (int) (+ int int)) (defun square (int) (* int int)) (defun identity (int) int) This adds additonal symbols we may not want, especially if the function is to be used only once. Using lambda we get the same effect without adding symbols (sumint # (lambda (int) (+ int int)) 10) (sumint # (lambda (int) (* int int)) 10) (sumint # (lambda (int) int) 10) LC-11

The function function What is the meaning of # in the following (sumint # (lambda (int) (+ int int)) 10) It is a short hand # (...) ==> (function (...)) One of its attributes is it works like quote, in that its argument is not evaluated, thus, in this simple context the following will also work (sumint (lambda (int) (+ int int)) 10) Later we will see another attribute of function that makes it different from quote. Whenever a function is to be quoted use # in place of LC-12

Recursion Recursion with lambda functions uses labels to temporarily name a function The following is a general λ-calculus template. The name is in scope within the entire body but is out of scope outside of the lambda expression. {label name ( lambda arguments. body_references_name ) } In Lisp can use labels to define a mutually recursive set of functions ( labels (list of named lambda expressions) ) sequence of forms using the temporarily named functions LC-13

Example 1 of recursion A recursive multiply that uses only addition. The temporary function is called mult Use quote not function (eval '(labels ((mult (k n) (cond ((zerop n) 0) (t (+ k (mult k (1- n)))) ))) (mult 2 3) ) ) LC-14

Example 2 of recursion rectimes computes k*n by supplying the paramters to a unary function that is a variation of example 1. Note the form (temp n) is within the labels section. (defun rectimes (k n) )) (labels ((temp (n) (temp n) ))) (cond ((zerop n) 0) (t (+ k (temp (1- n)))) LC-15