Lambda Calculus. Gunnar Gotshalks LC-1

Similar documents
Lambda Calculus 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 Languages

Functional Programming. Pure Functional Programming

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

FP Foundations, Scheme

Functional programming with Common Lisp

CS 314 Principles of Programming Languages

15 Unification and Embedded Languages in Lisp

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

Chapter 1. Fundamentals of Higher Order Programming

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

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

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

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

Functional Programming

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

CSCE 314 Programming Languages

Functional Programming Languages (FPL)

Functional Languages. Hwansoo Han

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

Introductory Example

Imperative, OO and Functional Languages A C program is

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

CSCC24 Functional Programming Scheme Part 2

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

1.3. Conditional expressions To express case distinctions like

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

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

Evaluating Scheme Expressions

CMSC 330: Organization of Programming Languages

Project 2: Scheme Interpreter

Control Flow February 9, Lecture 7

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lisp Basic Example Test Questions

Documentation for LISP in BASIC

CSE 341: Programming Languages

Chapter 7 Control I Expressions and Statements

Introduction to lambda calculus Part 3

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

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

A Small Interpreted Language

CS 242. Fundamentals. Reading: See last slide

Formal Systems and their Applications

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

Chapter 15. Functional Programming Languages

CSc 520 Principles of Programming Languages

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

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

CS 314 Principles of Programming Languages

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

Lecture08: Scope and Lexical Address

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CS 360 Programming Languages Interpreters

CMSC 330: Organization of Programming Languages

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

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

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

Programming Languages

Lambda Calculus. Variables and Functions. cs3723 1

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

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

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Programming Systems in Artificial Intelligence Functional Programming

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

Functional Programming

Principles of Programming Languages 2017W, Functional Programming

Comp 311: Sample Midterm Examination

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Week 4: Functional Programming

Programming Language Pragmatics

LECTURE 16. Functional Programming

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

Z Notation. June 21, 2018

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

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

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

CMSC 330: Organization of Programming Languages

Chapter 11 :: Functional Languages

Introduction. chapter Functions

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

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Functions, Conditionals & Predicates

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CS115 - Module 9 - filter, map, and friends

Denotational Semantics. Domain Theory

Introduction to the Lambda Calculus

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

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

Transcription:

Lambda Calculus LC-1

l- Calculus History Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of l-calculus is equivalent in power to a Turing machine» Turing machines and l-calculus are alternate descriptions of our understanding of what is computable LC-2

l- Calculus History 2 In the mid to late 1950 s, John McCarthy developed Lisp» A programming language based on l-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-3

l- 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-parameter binding» Clearly indicates which variables are free and which are bound LC-4

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

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

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

Function application Functions are applied to arguments in a list immediately following the l-function» { l u. ( u + 1 ) ( u + 2 ) } [ 3 ] > 3 ==> u then ==> (3 + 1) (3 + 2) ==> 20» { l u. ( u + a ) ( u + b ) } [ 7 1 ] > 7 1 ==> u then ==> ( 6 + a ) ( 6 + b ) and no further in this context» {l u, v. ( u v ) ( u + v ) } [ 2p + q, 2p - q ] > ==> ( (2p+q) (2p - q) ) ( (2p + q) + (2p q) ) > Can pass expressions to a variable Can use different bracketing symbols for visual clarity; they all mean the same thing. LC-8

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

Functions are Variables Define f ( 3 ) + f ( 5 ) where f ( x ) = a x ( a + x ) where a = 4 { l f. f (3) + f (5) } [ { l a. { l x. a x ( a + x ) } } [ 4 ] ] Arguments must be evaluated first ==> { l f. f (3) + f (5) } [ { l x. 4 x ( 4 + x ) } ] ==> { l x. 4 x (4 + x ) } (3) + { l x. 4 x (4 + x ) } (5) ==> 4 * 3 ( 4 + 3 ) + 4 * 5 ( 4 + 5 ) ==> 264 LC-10

Lamba notation in Lisp Lambda expressions are a direct analogue of l-calculus expressions» They are the basis of Lisp functions 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 ) ) { l x. ( x + x ) } > Note the similar syntax with l-calculus and the change to prefix, from infix, to permit a uniform syntax for functions of any number of arguments LC-11

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-12

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-13

Recursion Recursion with lambda functions uses labels to temporarily name a function The following is a general l-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-14

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

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