Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Programming Chapter 14. History of Functional Languages Lisp is the second oldest language Motivated by a need to do symbolic, rather than.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Functional Programming in Scheme
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
ISBN Chapter 15 Functional Programming Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Functional Programming Languages
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
6.001 SICP Variations on a Scheme
Abstraction and Repetition
Functional Programming: Lisp
15.2 Mathematical Functions
Chapter 15 Functional Programming 6/1/2019.
Lisp.
Presentation transcript:

Lambda Calculus and Lisp PZ03J

Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative programming. The basic lambda calculus has just 3 constructs: variables, function definition (creation), and function application.

Grammar The terminals are variables x, y, z, … and also lambda, period, parentheses and numbers. M -> x | (M M) | x.M If F and A are both expressions then so is (F A) and indicates the application of the function F with A as its parameter. If F is a expression then so is x.F This is a function definition and is also called an ABSTRACTION because it generalizes the expression F for any value substituted for x.

Example of Function Definition x.x is a way of expressing the identity function f(x) = x without having to give it a name. x.2x represents the function f(x) = 2x

Bound and Free Variables A bound variable is a variable that is in the scope of a declaration (lambda binding) for that variable. A variable that is not bound is free. In the expression: x. y.(x z) x is bound and z is free (the y-binding has no effect as there is no y in the body of the expression) Examples: x. y.(yz) x.(x b)

Bound Variables Any bound variable may have its name changed without altering the meaning of the expression. Just change all occurrences of the variable in that scope. x.x is the same as y.y However, x. x.x is the same as x. y.y (scope rules)

Scope of Lambda Bindings In the expression x.F, the scope of the declaration (or lambda binding) x is limited to F.

Frequently the parentheses are left off when it will not cause confusion. In these cases, remember: –Application is left associative: a b c = ((a b) c). –There is only one lambda expression in the body of an abstraction. ( x.x y)

Reduction The Reduction Operation (also called application or evaluation) In (F A), the function F is applied with the parameter A. ( x.x b) => b ( x.xy b) => by

Constants and functions We can add constants (both for variables and functions), like 0, 1, 2 …, plus ( x.x 4) => 4 (plus 5 4) => 9 Here “plus” stands for x.( y.x+y)

There is sometimes a choice of order of reduction. When the reduction of a lambda expression terminates (it doesn't always), the order of evaluation makes no difference. Evaluate: ( x.(xxx) ( x.x a)) one way => ( x.(xxx) (a)) => (aaa) other way => ( x.x a) ( x.x a) ( x.x a) => aaa

Some expressions do not terminate ( x.(xx)) ( x.(xx)) and some get more complicated (can you find an example??

Modeling Logic Lambda expressions can be used to model arithmetic (plus 4 5) How can we define lambda expression to give us arithmetic and logic? Define TRUE and FALSE so that we can use them in an conditional statement A B C where if A reduces to True then the result is B, if A reduces to False then the result is C (like C expression z = (a>b)? a: b; //z gets max(a,b)

Defining True and False Define T == x. y. x and F == x. y. y T P Q => ( x. y. x) P Q => ( y.P) Q => P F P Q => ( x. y. y) P Q => ( y.y) Q => Q

Other Logic Operators Similarly define NOT == x.((x F)T) AND == x. y.((x y)F) OR == x. y.((x T)y)

Lambda Calculus Models Functional Programming Languages Imperative languages are abstractions of the Von Neumann architecture; a computation is done by performing a sequence of operations that changes the values of memory locations through assignments. Functional languages do computations by defining functions and evaluating expressions.

Style Comparison Imperative: sequence, iteration, conditional Functional: functional composition, recursion, conditional Imperative: change the value of existing object with assignment Functional: compute a new object (garbage collection issues) Imperative: side-effects can cause errors Functional: no side effects (or limited to I/O) leads to referential transparency – if you call a function with the same parameters, you always get the same result.

Structure of Functional Programs Functional languages (FLs) have primitive functions and a mechanism for defining new functions and an expression evaluator that will evaluate an expression using the primitive and newly defined functions. A functional program usually consists of a series of function definitions followed by an expression using those functions.

Functional Composition FLs rely on functional composition instead of sequence. f(x) = x + 1 g(x) = x * 2 Instead of (imperative style ) x = x + 1; x = x * 2; we say g(f(x))

First Class Values Functions as first class values: functions can be passed as parameters and returned. A function that returns a function as its result is a "higher order" function.

Lisp and Scheme Lisp was the first FL and is the one most people think of. It has a simple syntax using prefix notation and parentheses. Scheme is a dialect of Lisp. It has static scope rather than dynamic, uses meaningful identifiers, true and false are #T and #F, predicates end in ? ( so (atom? (x) ) returns #F because x is not an atom (it is a list). Also uses prefix notation.

Lists: the Built-in Data Structure Lists consist of elements (atoms (symbolic and numeric) and lists) separated by spaces and enclosed in parentheses. The basis data structure is a 'cons' cell. Ex. three lists each with 3 elements (ALPHA BETA GAMMA) ( ) ( (A B) (HELLO THERE) 94)

Internal Representation (ALPHA BETA GAMMA) ( )

Representation of Lists ( (A B) (HELLO THERE) 94)

Exercises Diagram : ( (0 2) (1 5) (2 3) ) Identify the following as atoms, lists, or neither. If a list, how many elements are in the (top level) list? ATOM (this is an atom) ( ( (3) ) ) ( ( ) ((a b) 3 (c d e)) (* (+ 3 1) (+ 2 1) )

Working with Lists First and Rest takes lists apart (CAR and CDR, Head and Tail) First ( ) is 12 Rest ( ) is (14 16) First ( Rest ( ) ) is? NOTE: Rest always returns a list! The empty list is written () or nil. So, rest of (12) is (). First and rest only work on lists

Set and Quote set listA = ' ( (A B) (HELLO THERE) 94) Note, set makes listA into a constant. It is "assigned" this string literal, but it cannot be changed. Also, the single quote means to take what follows as a literal and not try to evaluate it. What do the following expressions evaluate to? rest (first listA) first (first (rest listA ) )

Making Lists with CONS CONS takes an expression (atom or list) and a list, puts them together and returns a new list whose first element is the expression and remaining elements are those of the old list. cons a '(b c)constructs the list (a b c) (cons (first listA) (rest listA) ) puts together a list just like listA (first (cons a x) ) = a (rest (cons a x) ) = x

Doing Arithmetic The operators + - * / do what you think they would: (+ 3 4)evaluates to7 (- 3 1)evaluates to 2 (/ 3 1)evaluates to 3 (- 24 (* 4 3))evaluates to 12 Note: on most systems, (/ 1 3) will evaluate to 1/3. Equality operators: = works on numeric symbols and numbers; EQUAL? works on any atom; equality for lists?

Defining Functions (DEFINE (name para) (body) ) Examples: (DEFINE (square a) (* a a) ) (DEFINE (second list) (First (Rest list)))

Conditionals (IF (condition) (then_part) (else_part) ) (COND (p_1 expression-or-expr-list) (p_2 expression-or-expr-list) … (p_n expr) (ELSE expr);; ELSE is #T );; exp associated with first true p_x is executed

Example: Fibonacci Numbers F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) Number sequence is: … (define (fib n) (if (or (= n 0) (= n 1) ) 1 (+ (fib (- n 1) ) (fib (- n 2) ) )

Higher Order Functions: map The function map takes a function and a list as parameters and returns a list with the function applied to each element of the original list. (define map (f x) (cond ( (nil x) nil ) (else (cons (f (first x)) (map(f (rest x)) ) ) ) )

Example with map (define TimesTen x (* 10 x) ) Then (map TimesTen '( ) ) returns the list ??

CAR and CDR CAR and CDR instead of FIRST and REST: Both LISP and Scheme allow the CAR and CDR functions to be combined in a composite form. That is, assuming x is a list, (CAR ( CDR x) ) can be written (CADR x)

More CAR and CDR The general form is: CxxxR where you can have any number of x's (at least one) and each x is A or D signifying CAR or CDR. The order of the A's and D's indicates the order of CAR's and CDR's in the expression. Thus, (CDAR x)  (CDR( CAR x)) (CADDR x)  (CAR( CDR( CDR x)) ) What is (CADADR '( (a b) (c d) (e f) ) )

Predicates in Scheme NULL? is this an empty list? EQ? are two atoms equal? LIST? is parameter a list? ZERO? is a numeric zero?

Equality for Simple Lists (DEFINE (eqsimple L1 L2) (COND ((NULL? L1) (NULL? L2)) ((NULL? L2) ‘() ) ((EQ? (CAR L1) (CAR L2)) (eqsimple (CDR L1) (CDR L2) ) ) (ELSE ‘() ) ) )

Lambda Calculus and Lisp PZ03J Coen256/NotesCh4.doc Coen171/FunctionalProgramming.doc