Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus...

28
LC-1 © Gunnar Gotshalks Lambda Calculus

Transcript of Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus...

Page 1: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-1 © Gunnar Gotshalks!

Lambda Calculus!

Page 2: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-2 © Gunnar Gotshalks!

λ- Calculus History!

◊  Developed by Alonzo Church during mid 1930’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!

Page 3: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-3 © Gunnar Gotshalks!

λ- Calculus History – 2!

◊  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!

Page 4: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-4 © Gunnar Gotshalks!

λ- 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!

Page 5: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-5 © Gunnar Gotshalks!

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.!

Page 6: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-6 © Gunnar Gotshalks!

◊  Consider following expression!»  ( u + 1 ) ( u – 1 )!»  is u bound or free?!

◊  Disambiguate the expression with the following λ-function!»  ( λ u . ( u + 1 ) ( u – 1 ) )!

» 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

λ-functions – 1!

bound variables! defining form!

Page 7: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-7 © Gunnar Gotshalks!

λ-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!

Page 8: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-8 © Gunnar Gotshalks!

Function application!

◊  Functions are applied to arguments in a list immediately following the l-function!»  { λ u . ( u + 1 ) ( u + 2 ) } [ 3 ]!

>  3 ==> u then ==> (3 + 1) (3 + 2) ==> 20!»  { λ u . ( u + a ) ( u + b ) } [ 7 – 1 ]!

>  7–1 ==> u then ==> ( 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!

◊  Can use different bracketing symbols for visual clarity; they all mean the same thing.!

Page 9: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-9 © Gunnar Gotshalks!

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!

Page 10: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-10 © Gunnar Gotshalks!

Functions are Variables!

◊  Define f ( 3 ) + f ( 5 ) where f ( x ) = a x ( a + x ) where a = 4!   { λ f . f (3) + f (5) } [ { λ a . { λ x . a x ( a + x ) } } [ 4 ] ]!

◊  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 ) ==> 264!

Page 11: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-11 © Gunnar Gotshalks!

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!

Page 12: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-12 © Gunnar Gotshalks!

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 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)!

Page 13: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-13 © Gunnar Gotshalks!

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 ’ !

Page 14: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-14 © Gunnar Gotshalks!

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 )!

Page 15: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-15 © Gunnar Gotshalks!

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)!   )!  )!

Page 16: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-16 © Gunnar Gotshalks!

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)!  ))!

Page 17: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-17 © Gunnar Gotshalks!

Churchʼs Lambda Calculus!

◊  The preceding description is not exactly what Church developed!

◊  He worked with one parameter !

◊  Result of a function is a function!»  In lambda calculus numbers are functions!

Page 18: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-18 © Gunnar Gotshalks!

Function notation!

◊  Do not need parenthesis for one argument functions.!»  f ( x ) = f x!» g ( f ( x ) ) = g (f x)!

> Cannot have g f x as that could mean (g f) x!> Could have g f x!

Page 19: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-19 © Gunnar Gotshalks!

Function notation – 2!

◊  For multiple argument functions convert to single argument functions!»  f ( x , y ) = ( f x ) y!

>  (f x) returns a one parameter function that is applied to y!

»  f ( x , y , z ) = ( ( f x ) y ) z!» …!

Page 20: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-20 © Gunnar Gotshalks!

Function definition examples!

◊  Use λ to denote the parameters of a function!»  { λ u . u * v + 2*a }!»  { λ u v . u * v + 2*a }

= { λ u . [ λ v . u * v + 2*a ] }!> Can see how multiple parameters are equivalent

to nested one parameter functions!> At this level of definition, we simplify!

»  { λ v a . u * v + 2*a }!»  { λ u v a . u * v + 2*a }!»  { λ x . f x }!!!

Page 21: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-21 © Gunnar Gotshalks!

Function application!

◊  Consider the following!» g = { λ x . (1 / 6) x^3 } !» g 3 = (1 / 6) 3^3 = 9 / 6!» g (a + 1)

= (1 / 6) (a + 1)^3 = (1 / 6) a^3 + (1 / 2) a ^ 2 + (1 / 2) a + (1 / 6)!

Page 22: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-22 © Gunnar Gotshalks!

Function application – 2!

◊  Functions can be parameters!»  { λ f . f x } g f g

= g x!

◊  Can abstract out the x!»  { λ f . [ λ x . f x ] } g

= { λ f x . f x } g !Simplify = { λ x . g x }!

◊  Apply the function!»  { λ x . g x } a

= g a! As a consequence g = { λ x . g x } !!

Page 23: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-23 © Gunnar Gotshalks!

Function application – 3!

◊  A more complex example!»  { λ f x . f (f x) } g

= { λ x . g (g x) } = g (g x)!

◊  Church associated this function with the number 2!> Numbers are functions!

» 2 = { λ f x . f (f x) }!» 2 g = g (g x) !g applied twice

Page 24: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-24 © Gunnar Gotshalks!

Definition of Natural Numbers!

◊  The following shows how the natural numbers can be defined!» 0 = { λ f x . x } !» 1 = { λ f x . f x }!» 2 = { λ f x . f (f x) }!» 3 = { λ f x . f (f (f x)) }!» 4 = { λ f x . f (f (f (f x))) }!» …!

Page 25: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-25 © Gunnar Gotshalks!

Add 1 to a number!

◊  Define add_1 as follows!» add_1 = { λ a b c . b ( (a b) c ) }!

◊  Apply add_1 to the number (i.e. function) ! !3 = { λ f x . f (f (f x) ) }!» add_1 3

= { λ b c . b ( (3 b) c ) } = { λ b c . b ( { λ x . b (b (b x) ) } c ) } = { λ b c . b ( b (b (b c) ) ) } = 4!

Page 26: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-26 © Gunnar Gotshalks!

Multiply a number by two!

◊  Define mult_2 as follows!» mult_2 = { λ a b c . (a b) ( (a b) c ) }!

◊  Apply multi_2 to the number (i.e. function) ! !3 = { λ f x . f (f (f x) ) }!» mult_2 3

= { λ b c . (3 b) ( (3 b) c ) } = { λ b c . (3 b) ({ λ x . b (b (b x) ) } c ) } = { λ b c . (3 b) ( b (b (b c) ) ) } = { λ b c . { λ x . b (b (b x) ) } ( b (b (b c) ) ) } = { λ b c . b (b (b (b (b (b c) ) ) ) ) } = 6!

Page 27: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-27 © Gunnar Gotshalks!

Basic arithmetic operator definitions!

◊  Addition operator – p + q!» add = { λ p q x y . ( (p x) (q x) ) y ) }!

> Exercise show that (add 2) 3 = 2 + 3 = 5

◊  Multiplication operator – p × q!» mult = { λ p q x . p (q x) }!

> Exercise show that (mult 2) 3 = 2 × 3 = 6!

◊  Power operator – p q!

» pow = { λ p q . p q }!

Page 28: Lambda Calculus - York University · Lambda expressions are a direct analogue of λ-calculus expressions! » They are the basis of Lisp functions – a modified syntax to simplify

LC-28 © Gunnar Gotshalks!

(pow 2) 3 = 9!

»  (pow 2) 3= ({ λ p q . p q } 2) 3= { λ q . 2 q } 3 = { λ q . { λ f x . f (f x) } g } 3 = { λ q x . q (q x) } 3 = { λ x . 3 (3 x) } = { λ x . { λ f y . f (f (f y) ) } (3 x) } = { λ x . { λ y . (3 x) ((3 x) ((3 x) y) ) } }= { λ x y . (3 x) ((3 x) (x (x (x y) ) ) ) } }= { λ x y . (3 x) (x ( x (x (x (x (x y) ) ) ) ) ) }= { λ x y . x (x (x (x ( x (x (x (x (x y) ) ) ) ) ) ) ) } = 9 = 32