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

Similar documents
Lambda Calculus. Gunnar Gotshalks LC-1

Lambda Calculus LC-1

Lambda Calculus see notes on Lambda Calculus

Functional programming with Common Lisp

Lisp Basic Example Test Questions

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

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

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

Functional Programming. Pure Functional Languages

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Pure Functional Languages

Review of Functional Programming

Chapter 1. Fundamentals of Higher Order Programming

Functional Programming. Pure Functional Programming

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

Functional Programming

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

Imperative, OO and Functional Languages A C program is

Functions, Conditionals & Predicates

Lecture Notes on Lisp A Brief Introduction

Organization of Programming Languages CS3200/5200N. Lecture 11

FP Foundations, Scheme

An introduction to Scheme

Functional Programming. Contents

CSCI337 Organisation of Programming Languages LISP

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Functional Programming Languages (FPL)

1.3. Conditional expressions To express case distinctions like

Midterm Examination (Sample Solutions), Cmput 325

CS 314 Principles of Programming Languages

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

MIDTERM EXAMINATION - CS130 - Spring 2005

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Basics of Using Lisp! Gunnar Gotshalks! BLU-1

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Documentation for LISP in BASIC

UMBC CMSC 331 Final Exam

Programming Languages

John McCarthy IBM 704

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

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

Chapter 15. Functional Programming Languages

Principles of Programming Languages 2017W, Functional Programming

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Recursion & Iteration

Imperative languages

15 Unification and Embedded Languages in Lisp

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

INF4820. Common Lisp: Closures and Macros

SCHEME AND CALCULATOR 5b

Department of Computer and information Science Norwegian University of Science and Technology

Principles of Programming Languages

CSCC24 Functional Programming Scheme Part 2

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

19 Machine Learning in Lisp

Common LISP-Introduction

CS 360 Programming Languages Interpreters

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

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

A Quick Introduction to Common Lisp

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Common Lisp. Blake McBride

Introduction to Functional Programming and basic Lisp

Putting the fun in functional programming

UMBC CMSC 331 Final Exam

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

Functional Languages. Hwansoo Han

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Func%onal Programming in Scheme and Lisp

LECTURE 17. Expressions and Assignment

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

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

Robot Programming with Lisp

Func%onal Programming in Scheme and Lisp

Introduction to Scheme

CS115 - Module 9 - filter, map, and friends

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

Recap: Functions as first-class values

LECTURE 16. Functional Programming

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Functional Programming Languages (FPL)

Lecture08: Scope and Lexical Address

Concepts of programming languages

SOFTWARE ARCHITECTURE 6. LISP

Announcements. Today s Menu

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

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

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

Project 2: Scheme Interpreter

Evaluating Scheme Expressions

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

MetacircularScheme! (a variant of lisp)

Transcription:

λ Calculus Basis Lambda Calculus and Lambda notation in Lisp II Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky 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 LC2-1 LC2-2 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. { λ u, w. ( u + 1 ) ( v 1 ) ( w + 2 ) } bound variables In the above λ expression:» u and w are bound» v is free λ functions defining form LC2-3 LC2-4

Work from the inside out Function application Work right to left so arguments are evaluated first λ function example { λ u, v. ( u * v) / ( u + 6 ) } [ { λ a. a ( a + 1 ) } [ 3 ], { λ b. ( b + 1 )**2 } [ 2 ] ] Build up longer definitions with auxiliary definitions { λ u, v. ( u * v) / ( u + 6 ) } [ 12, { λ b. ( b + 1 )**2 } [ 2 ] ] { λ u, v. ( u * v) / ( u + 6 ) } [ 12, 9 ] 108/18 6 LC2-5 LC2-6 λ function example { λ f. f * (f + 1) } [ { λ a, b. a ( b + 1 ) } [ x + 1, x - 1 ] ] { λ f. f * (f + 1)} [ (x+1) * x ] λ function example - 1 Example» Define f(u(a)) = u(a)**2+1 where u(a) = a ( a + 1 ) where a = z+1 where z = 1 (x+1)*x*((x+1)*x+1) x * (x+1) * (x**2+x+1) LC2-7 LC2-8

λ function example - 2 Example» Define f(u(a)) = u(a)**2+1 where u(a) = a ( a + 1 ) where a = z+1 where z = 1 λ function example - 3 Example» Define f(u(a)) = u(a)**2+1 where u(a) = a ( a + 1 ) where a = z+1 where z = 1 { λ z. z + 1 } [ 1 ] { λ a. a ( a + 1 ) } [ { λ z. z + 1 } [ 1 ] ] LC2-9 LC2-10 λ function example - 4 Example» Define f(u(a)) = u(a)**2+1 where u(a) = a ( a + 1 ) where a = z+1 where z = 1 λ function example - 5 Example» Define f(u(a)) = u(a)**2+1 where u(a) = a ( a + 1 ) where a = z+1 where z = 1 { λ f. f ** 2 + 1 } [ { λ a. a ( a + 1 ) } [ { λ z. z + 1 } [ 1 ] ] ] { λ f. f ** 2 + 1 } [ { λ a. a ( a + 1 ) } [ { λ z. z + 1 } [ 1 ] ] ] { λ f. f ** 2 + 1 } [ { λ a. a ( a + 1 ) } [ 2 ] ] { λ f. f ** 2 + 1 } [ 6 ] 37 LC2-11 LC2-12

Lambda functions in Lisp Lambda functions in Lisp are anonymous functions or functions without a name. Eliminate the need to allot a function storage space Use lambda functions when you don t expect to reuse the function Lambda functions can be found wherever Lisp expects to find a function All Lisp functions are stored as lambda functions Lamba notation in Lisp Lambda expressions are a direct analogue of λ-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 ) ) { λ 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 LC2-13 LC2-14 Lambda expression - 1 ( lambda ( list of symbols ) list of forms ) list of symbols: list of formal parameters Lambda expression - 2 We are used to seeing functions at the beginning of a list followed by arguments. Lambda functions can be used the same way. ( (lambda ( list of symbols ) list of forms ) arguments ) list of forms: body of the function list of symbols: list of formal parameters list of forms: body of the function arguments: arguments supplied to lambda function LC2-15 LC2-16

Lambda examples > ( ( lambda ( x ) (cons x nil ) ) y) (Y) > ( apply ( lambda ( x y ) ( list y x ) ) ( 1 2 ) ) ( 2 1 ) > ( mapcar ( lambda ( x ) ( / 1 ( sqrt x ) ) ) ( 1 2 3 4 5 ) ) ( 1.0 0.707106 0.577350 0.5 0.447213 ) > ( ( lambda ( x ) ( setq x 5 ) ) ( setq x 4 ) ) 5 > x 4 More lambda examples > ( apply ( lambda ( x y ) ( list y x ) ) ( ( list 1 2 3 ) car ) ) ( CAR ( LIST 1 2 3 )) > ( funcall ( lambda ( x y ) ( list y x ) ) ( list 1 2 3 ) car ) ( CAR ( LIST 1 2 3 )) > ( eval ( ( lambda ( x y ) ( list y x ) ) (list 1 2 3 ) car ) ) 1 > ( eval ( funcall ( lambda ( x y ) ( list y x ) ) ( list 1 2 3 ) car ) ) 1 LC2-17 LC2-18 Even more lambda examples > ( mapcar ( lambda ( x ) ( list a x ) ) ( 1 2 3 ) ) ( ( A 1 ) ( A 2 ) ( A 3 ) ) > ( mapc ( lambda ( x ) ( + x x ) ) ( 1 2 3 ) ) ( 1 2 3 ) > ( mapcar ( lambda ( x ) ( or ( atom x ) ( equal ( a b ) x ) ) ) ( a b ( a b ) ( ) x ( ( x ) ) ) ) Remember sumint sumint (defun sumint (func n) (cond ((equal n 0) 0) ( t ( + (funcall func n) (sumint func (1- n)))))) ( T T T T T NIL ) > ( maplist ( lambda (x) ( cons (car x) ( b ) ) ) (1 2 3 ) ) ( ( 1 B ) ( 2 B ) ( 3 B ) ) LC2-19 LC2-20

Anonymous functions - 1 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) Then we called sumint with the desired function as an argument (sumint double 10 ) -- > 110 (sumint square 2 ) -- > 5 (sumint identity 5 ) -- > 15 Anonymous functions - 2 This adds additional 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) LC2-21 LC2-22 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. Another example - 1 Suppose we have a two step process we want to apply when iterating down a list. For example: we first want to take the absolute value of each number and then we want to take the square root. Applying these operations to the list (1-4 16-25) would produce (1 2 4 5). Whenever a function is to be quoted use # in place of LC2-23 LC2-24

Another example - 2 We could accomplish this by using functionals: Another example - 3 We could accomplish this by using functionals: (???? # sqrt (???? # abs ( -9 16 1024-1 ) ) ) (???? # sqrt (mapcar # abs ( -9 16 1024-1 ) ) ) LC2-25 LC2-26 Another example - 4 We could accomplish this by using functionals: Another example - 5 A different way to accomplish the same thing: ( mapcar # sqrt (mapcar # abs ( -9 16 1024-1 ) ) ) ( defun compose ( f g ) # ( lambda ( x ) ( funcall f (funcall g x ) ) ) ) NOTE: instead of passing a function into a function, here you are passing a function out of a function LC2-27 LC2-28

Another example - 6 A different way to accomplish the same thing: > ( mapcar # sqrt (mapcar # abs ( -9 16 1024-1 ) ) ) ( 3.0 4.0 32.0 1.0 ) > ( funcall (compose # sqrt # abs ) -9 ) 3.0 > ( mapcar (compose # sqrt # abs ) ( -9 16 1024-1 ) ) ( 3.0 4.0 32.0 1.0 ) Note: this gets rid of the nested mapcars 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 ) LC2-29 LC2-30 Example 1 of recursion A recursive multiply that uses only addition. > The temporary function is called mult (labels ((mult (k n) (cond ((zerop n) 0) (t (+ k (mult k (1- n)))) ))) (mult 2 3) ) Example 2 of recursion rectimes computes k * n by supplying the parameters to a unary function that is a variation of example 1. (defun rectimes (k n) )) (labels (( temp (n) ))) (temp n) (cond ((zerop n) 0) ( t (+ k (temp (1- n)))) LC2-31 LC2-32

A Lisp hackers puzzle A Lisp hackers puzzle Self replicating So t No good must be non-atomic Generate a self-reproducing non-atomic Lisp form??? It is considered cheating if your form involves any permanent side-effects. How about creating a function foo that returns foo Also no good - creating a function involves a permanent sideeffect. All known solutions involve using lambda! LC2-33 LC2-34 A Lisp hackers puzzle TEST 1 A solution: ( ( lambda ( x ) ( list ( list lambda ( x ) x ) ( list quote x ) ) ) ( list ( list lambda ( x ) x ) ( list quote x ) ) ) ) TEST 1 WILL COVER ALL MATERIAL UNTIL HERE!! LC2-35 LC2-36

TEST 1 What is on the test?» All my slides» Wilensky chapters 1, 2, 3, 15, 4, 6, 7, 8, 9 (not 9.6)» Notes on symbols, notes on functionals (no Backus notation), notes on lambda calculus > Notes are on the web site under resources LC2-37