Docsity

Introduction to Lambda Calculus - Lecture Slides | ECS 240, Study notes of Programming Languages

Toggle sidebar
bg1
1
ECS 240 Lecture 18-19 1
Introduction to Lambda Calculus
Lecture 18-19
ECS 240
ECS 240 Lecture 18-19 2
Plan
โ€ข๎˜Introduce lambda calculus
โ€“๎˜Syntax and operational semantics
โ€“๎˜Properties
โ€ข๎˜Relationship to programming languages (later)
โ€ข
๎˜
Study of types and type systems (even later)
ECS 240 Lecture 18-19 3
Background
โ€ข๎˜Developed in 1930โ€™s by Alonzo Church
โ€ข๎˜Subsequently studied by many people
โ€ข๎˜โ€œTestbedโ€ for procedural and functional languages
โ€“๎˜Simple
โ€“๎˜Powerful
โ€“๎˜Easy to extend with features of interest
โ€“๎˜Plays similar role for PL research as Turing machines for
computability
โ€œ
Whatever the next 700 languages turn out to be,
they will surely be variants of lambda calculus.
โ€
(Landin โ€™66)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Introduction to Lambda Calculus - Lecture Slides | ECS 240 and more Study notes Programming Languages in PDF only on Docsity!

ECS 240 Lecture 18-19 1

Introduction to Lambda Calculus

Lecture 18- ECS 240

ECS 240 Lecture 18-19 2

Plan

  •  Introduce lambda calculus
    •  Syntax and operational semantics
    •  Properties
  •  Relationship to programming languages (later)
  •  Study of types and type systems (even later)

ECS 240 Lecture 18-19 3

Background

  •  Developed in 1930โ€™s by Alonzo Church
  •  Subsequently studied by many people
  •  โ€œTestbedโ€ for procedural and functional languages
    •  Simple
    •  Powerful
    •  Easy to extend with features of interest
    •  Plays similar role for PL research as Turing machines for computability

โ€œ Whatever the next 700 languages turn out to be,

they will surely be variants of lambda calculus.โ€

(Landin โ€™66)

ECS 240 Lecture 18-19 4

Syntax

  •  The -calculus has three kinds of expressions (terms) e ::= x Variables | x.e Functions (abstraction) | e 1 e 2 Application
  •  x.e is a one-argument function with body e
  •  e 1 e 2 is a function application
  •  Application associates to the left x y z means (x y) z
  •  Abstraction extends to the right as far as possible x.xy.x y z means x.(x (y. ((x y) z)))

ECS 240 Lecture 18-19 5

Examples of Lambda Expressions

  •  The identity function:  I =def x. x
  •  A function that given an argument y discards it and yields the identity function:  y. (x. x)
  •  A function that given a function f invokes it on the identity function f. f (x. x)

ECS 240 Lecture 18-19 6

Scope of Variables

  •  As in all languages with variables it is important to discuss the notion of scope -  Recall: the scope of an identifier is the portion of a program where the identifier is accessible
  •  An abstraction x. E binds variable x in E
    •  x is the newly introduced variable
    •  E is the scope of x
    •  We say x is bound in x. E
    •  Just like formal function arguments are bound in the function body

ECS 240 Lecture 18-19 10

Renaming Bound Variables (Cont.)

  •  Convention: we will always try to rename bound variables so that they are all unique -  e.g., write x. x (y.y) x instead of x. x (x.x) x
  •  This makes it easy to see the scope of bindings
  •  And also prevents confusion!

ECS 240 Lecture 18-19 11

Substitution

  •  The substitution of Eโ€™ for x in E (written [Eโ€™/x]E )
    •  Step 1. Rename bound variables in E and Eโ€™ so they are unique
    •  Step 2. Perform the textual substitution of Eโ€™ for x in E
  •  Example: [y (x. x) / x] y. (x. x) y x
    •  After renaming: [y (v. v)/x] z. (u. u) z x
    •  After substitution: z. (u. u) z (y (v. v))
  •  If we are not careful with scopes might get: y. (x. x) y (y (x. x))

ECS 240 Lecture 18-19 12

Informal Semantics

  •  We consider only closed terms
  •  The evaluation of (x. e) eโ€™ 1. Binds x to eโ€™ 2. Evaluates e with the new binding 3. Yields the result of this evaluation
  •  Like a function call, or like โ€œlet x = eโ€™ in eโ€
  •  Example: (f. f (f e)) g evaluates to g (g e)

ECS 240 Lecture 18-19 13

Operational Semantics

  •  There exist many operational semantics for the - calculus
  •  All are based on the equation

(x. e) eโ€™ = [eโ€™/x]e usually oriented from left to right

  •  This is called the -rule and the evaluation step a - reduction
  •  The subterm (x. e) eโ€™ is a -redex
  •  e  eโ€™: e -reduces to eโ€™ in one step
  •  e *^ eโ€™: e -reduces to eโ€™ in 0 or more steps

ECS 240 Lecture 18-19 14

Examples of Evaluation

  •  The identity function: (x. x) E  [E / x] x = E
  •  Another example with the identity: (f. f (x. x)) (x. x)  [x. x / f] f (x. x)) = [(x. x) / f] f (y. y)) = (x. x) (y. y)  [y. y /x] x = y. y
  •  A non-terminating evaluation: (x. xx)(y. yy)  [y. yy / x]xx = (y. yy)(y. yy)  โ€ฆ

ECS 240 Lecture 18-19 15

Evaluation and the Static Scope

  •  The definition of substitution guarantees that evaluation respects static scoping: ( x. (y. y x)) (y (x. x))  (^)  z. z (y (v. v))

(y remains free, i.e., defined externally)

  •  If we forget to rename the bound y: ( x. (y. y x)) (y (x. x))  y. y (y (v. v))

(y was free before but is bound now)

ECS 240 Lecture 18-19 19

The Order of Evaluation

  •  A -term can have more than one instances of (x. E) Eโ€™ (y. (x. x) y) E
  •  A choice: reduce the inner or the outer 
  •  Which one should we pick?

(y. (x. x) y) E

(y. [y/x] x) E = (y. y) E [E/y] (x. x) y =(x. x) E

E

inner outer

ECS 240 Lecture 18-19 20

The Diamond Property

  •  A relation R has the diamond property iff
    •  e R e 1 and e R e 2 implies there exists eโ€™ with e 1 R eโ€™ and e 2 R eโ€™
  •   does not have the diamond property
  •  *^ has the diamond property
  •  The simplest known proof is quite technical

e

e 1 e 2

eโ€™

R R

R R

ECS 240 Lecture 18-19 21

The Diamond Property

  •  Languages defined by non-deterministic sets of rules are common -  Logic programming languages -  Expert systems -  Constraint satisfaction systems
  •  It is useful to know whether such systems have the diamond property

ECS 240 Lecture 18-19 22

Equality

  •  Let = be the reflexive, transitive and symmetric closure of  = is (  ) *
  •  In another words, e 1 = e 2 if e 1 converts to e 2 via a sequence of forward and backward 

e 1 e 2

ECS 240 Lecture 18-19 23

The Church-Rosser Theorem

  •  If e 1 = e 2 then there exists eโ€™ such that e 1 ^ eโ€™ and e 2 ^ eโ€™
  •  Proof (informal): apply the diamond property as many times as necessary

e 1 e 2

eโ€™

ECS 240 Lecture 18-19 24

Corollaries

  •  If e 1 = e 2 and e 1 and e 2 are normal forms then e 1 is identical to e 2 -  From CR we have eโ€™. e 1  eโ€™ and e 2  eโ€™ -  Since e 1 and e 2 are normal forms they are identical to eโ€™
  •  If e  e 1 and e  e 2 and e 1 and e 2 are normal forms then e 1 is identical to e 2 -  All terms have a unique normal form

ECS 240 Lecture 18-19 28

Call-by-Name

  •  Donโ€™t reduce under 
  •  Donโ€™t evaluate the argument to a function call
  •  A value is an abstraction
  •  Call-by-name is demand-driven: an expression is not evaluated unless needed
  •  It is normalizing: converges whenever normal order converges
  •  Call-by-name does not necessarily evaluate to a normal form. Example: D D = (x. x x) (x. x x)

e 1 n^ x. e 1 โ€™ [e 2 /x]e 1 โ€™ n^ e x. en^ x. e e 1 e 2 n^ e

ECS 240 Lecture 18-19 29

Call by Name

  •  Example:

(y. (x. x) y) ((u. u) (v. v))  (^) n

(x. x) ((u. u) (v. v))  (^) n

(u. u) (v. v)  (^) n

v. v

ECS 240 Lecture 18-19 30

Call-by-Value Evaluation

  •  Donโ€™t reduce under lambda
  •  Do evaluate the arguments to a function call
  •  A value is an abstraction
  •  Most languages are primarily call-by-value
  •  But CBV is not normalizing: (x. I) (D D)
  •  CBV diverges more often than normal order and CBN

e 1 v^ x. e 1 โ€™ e 2 v^ e 2 โ€™ [eโ€™ 2 /x]e 1 โ€™ v^ e x. ev^ x. e e 1 e 2 v*^ e

ECS 240 Lecture 18-19 31

Call by Value

  •  Example: (y. (x. x) y) ((u. u) (v. v))  (^) v

(y. (x. x) y) (v. v)  (^) v

(x. x) (v. v)  (^) v

v. v

ECS 240 Lecture 18-19 32

Considerations

  •  Call-by-value:
    •  easy to implement
    •  well-behaved (predictable) with respect to side-effects
  •  Call-by-name:
    •  More difficult to implement (must pass unevaluated expressions)
    •  The order of evaluation is harder to predict (e.g., difficulty with side-effects)
    •  Has a simpler theory than call-by-value
    •  Allows the natural expression of infinite data structures (e.g. streams)
    •  Terminates more often than call-by-value
  •  Various other (not as common) evaluation strategies

ECS 240 Lecture 18-19 33

Functional Programming

  •  The -calculus is a prototypical functional language with: -  no side effects -  several evaluation strategies -  lots of functions -  nothing but functions (pure -calculus does not have any other data type)
  •  How can we program with functions?
  •  How can we program with only functions?

ECS 240 Lecture 18-19 37

Expressiveness of  -Calculus

  •  The -calculus is a minimal system but can express
    •  data types (integers, booleans, lists, trees, etc.)
    •  branching
    •  recursion
  •  This is enough to encode Turing machines
  •  Corollary: e = eโ€™ is undecidable
  •  Still, how do we encode all these constructs using only functions?
  •  Idea: encode the โ€œbehaviorโ€ of values and not their structure

ECS 240 Lecture 18-19 38

Encoding Booleans in Lambda Calculus

  •  What can we do with a boolean?
    •  we can make a binary choice
  •  A boolean is a function that given two choices selects one of them -  true = (^) def x. y. x -  false =def x. y. y -  if E 1 then E 2 else E 3 =def E 1 E 2 E 3
  •  Example: โ€œif true then u else vโ€ is (x. y. x) u v  (^)  (y. u) v  (^)  u

ECS 240 Lecture 18-19 39

Encoding Pairs in Lambda Calculus

  •  What can we do with a pair?
    •  we can select one of its elements
  •  A pair is a function that given a boolean returns the left or the right element mkpair x y =def b. b x y fst p =def p true snd p =def p false
  •  Example: fst (mkpair x y)  (mkpair x y) true  true x y  x

ECS 240 Lecture 18-19 40

Encoding Natural Numbers in Lambda Calculus

  •  What can we do with a natural number?
    •  we can iterate a number of times over some function
  •  A natural number is a function that given an operation f and a starting value s, applies f a number of times to s: 0 = (^) def f. s. s 1 = (^) def f. s. f s 2 = (^) def f. s. f (f s) and so on
  •  These are numerals in unary representation
    •  Also called Church numerals

ECS 240 Lecture 18-19 41

Computing with Natural Numbers

  •  The successor function succ n =def f. s. f (n f s) or succ n = (^) def f.s.n f (f s)
  •  Addition

add n 1 n 2 =def n 1 succ n 2

  •  Multiplication

mult n 1 n 2 =def n 1 (add n 2 ) 0

  •  Testing equality with 0

iszero n =def n (b. false) true

ECS 240 Lecture 18-19 42

Computing with Natural Numbers. Example

mult 2 2  2 (add 2) 0  (add 2) ((add 2) 0)  2 succ (add 2 0)  2 succ (2 succ 0)  succ (succ (succ (succ 0)))  succ (succ (succ (f. s. f (0 f s))))  succ (succ (succ (f. s. f s)))  succ (succ (g. y. g ((f. s. f s) g y))) succ (succ (g. y. g (g y))) *^ g. y. g (g (g (g y))) = 4

succ n =  f.  s. f (n f s) add n1 n2 = n1 succ n mult n1 n2 = n1 (add n2) 0

ECS 240 Lecture 18-19 46

Expressiveness of Lambda Calculus

  •  Encodings are fun
  •  But programming in pure -calculus is painful
  •  We will add constants (0, 1, 2, โ€ฆ, true, false, if-then- else, etc.)
  •  And we will add types (next time!)