Docsity

Lambda Calculus - Lecture Slides | CMSC 330, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 330: Organization of
Programming Languages
Lambda Calculus
CMSC 330 2
Programming Language Features
Many features exist simply for convenience
•Multi-argument functions foo ( a, b, c )
¾Use currying or tuples
•Loops while (a < b) …
¾Use recursion
•Side effects a := 1
¾Use functional programming
So what language features are really needed?
CMSC 330 3
Turing Completeness
Computational system that can
•Simulate a Turing machine
•Compute every Turing-computable function
A programming language is Turing complete if
•It can map every Turing machine to a program
•A program can be written to emulate a Turing machine
•It is a superset of a known Turing-complete language
Most powerful programming language possible
•Since Turing machine is most powerful automaton
CMSC 330 4
Programming Language Theory
Come up with a “core” language
•That’s as small as possible
•But still Turing complete
Helps illustrate important
•Language features
•Algorithms
One solution
•Lambda calculus
CMSC 330 5
Lambda Calculus (λ-calculus)
Proposed in 1930s by
•Alonzo Church
•Stephen Cole Kleene
Formal system
•Designed to investigate functions & recursion
•For exploration of foundations of mathematics
Now used as
•Tool for investigating computability
•Basis of functional programming languages
¾Lisp, Scheme, ML, OCaml, Haskell…
CMSC 330 6
Lambda Expressions
A lambda calculus expression is defined as
e ::= x variable
| λx.e function
| e e function application
λx.e is like (fun x -> e) in OCaml
That’s it! Nothing but higher-order functions
pf3
pf4
pf5

Partial preview of the text

Download Lambda Calculus - Lecture Slides | CMSC 330 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Lambda Calculus

CMSC 330 2

Programming Language Features

Many features exist simply for convenience

  • Multi-argument functions foo ( a, b, c ) ¾ Use currying or tuples
  • Loops while (a < b) … ¾ Use recursion
  • Side effects a := 1 ¾ Use functional programming

So what language features are really needed?

CMSC 330 3

Turing Completeness

Computational system that can

  • Simulate a Turing machine
  • Compute every Turing-computable function

A programming language is Turing complete if

  • It can map every Turing machine to a program
  • A program can be written to emulate a Turing machine
  • It is a superset of a known Turing-complete language

Most powerful programming language possible

  • Since Turing machine is most powerful automaton

CMSC 330 4

Programming Language Theory

Come up with a “core” language

  • That’s as small as possible
  • But still Turing complete

Helps illustrate important

  • Language features
  • Algorithms

One solution

  • Lambda calculus

CMSC 330 5

Lambda Calculus (λ-calculus)

Proposed in 1930s by

  • Alonzo Church
  • Stephen Cole Kleene

Formal system

  • Designed to investigate functions & recursion
  • For exploration of foundations of mathematics

Now used as

  • Tool for investigating computability
  • Basis of functional programming languages ¾ Lisp, Scheme, ML, OCaml, Haskell…

CMSC 330 6

Lambda Expressions

A lambda calculus expression is defined as

e ::= x variable

| λx.e function

| e e function application

λx.e is like (fun x -> e) in OCaml

That’s it! Nothing but higher-order functions

CMSC 330 7

Three Conveniences

Syntactic sugar for local declarations

  • let x = e1 in e2 is short for (λx.e2) e

Scope of λ extends as far right as possible

  • Subject to scope delimited by parentheses
  • λx. λy.x y is same as λx.(λy.(x y))

Function application is left-associative

  • x y z is (x y) z
  • Same rule as OCaml CMSC 330 8

Lambda Calculus Semantics

All we’ve got are functions

  • So all we can do is call them

To evaluate (λx.e1) e

  • Evaluate e1 with x bound to e

This application is called beta-reduction

  • (λx.e1) e2 → e1[x/e2] ¾ e1[x/e2] is e1 where occurrences of x are replaced by e ¾ Slightly different than the environments we saw for Ocaml - Do substitutions to replace formals with actuals - Instead of using environment to map formals to actuals
  • We allow reductions to occur anywhere in a term

CMSC 330 9

Beta Reduction Example

(λx.λz.x z) y

→ (λx.(λz.(x z))) y // since λ extends to right

→ (λx.(λz.(x z))) y // apply (λx.e1) e2 → e1[x/e2] // where e1 = λz.(x z), e2 = y

→ λz.(y z) // final result

Equivalent OCaml code

  • (fun x -> (fun z -> (x z))) y → fun z -> (y z)

Parameters

  • Formal
  • Actual

CMSC 330 10

Lambda Calculus Examples

(λx.x) z →

(λx.y) z →

(λx.x y) z →

  • A function that applies its argument to y

z

y

z y

CMSC 330 11

Lambda Calculus Examples (cont.)

(λx.x y) (λz.z) →

(λx.λy.x y) z →

  • A curried function of two arguments
  • Applies its first argument to its second

(λx.λy.x y) (λz.zz) x →

(λz.z) y → y

λy.z y

λy.((λz.zz)y)x → (λz.zz)x → xx

CMSC 330 12

Static Scoping & Alpha Conversion

Lambda calculus uses static scoping

Consider the following

  • (λx.x (λx.x)) z →? ¾ The rightmost “x” refers to the second binding
  • This is a function that ¾ Takes its argument and applies it to the identity function

This function is “the same” as (λx.x (λy.y))

  • Renaming bound variables consistently is allowed ¾ This is called alpha-renaming or alpha conversion
  • Ex. λx.x = λy.y = λz.z λy.λx.y = λz.λx.z

CMSC 330 19

Natural Numbers (Church* Numerals)

Encoding of non-negative integers

  • 0 = λf.λy.y
  • 1 = λf.λy.f y
  • 2 = λf.λy.f (f y)
  • 3 = λf.λy.f (f (f y)) i.e., n = λf.λy.

*(Alonzo Church, of course) CMSC 330 20

Operations On Church Numerals

Successor

  • succ = λz.λf.λy.f (z f y)

Example

  • succ 0 = (λz.λf.λy.f (z f y)) (λf.λy.y) → λf.λy.f ((λf.λy.y) f y) → λf.λy.f ((λy.y) y) → λf.λy.f y = 1

Since (λx.y) z → y

  • 0 = λf.λy.y
  • 1 = λf.λy.f y

CMSC 330 21

Operations On Church Numerals (cont.)

IsZero?

  • iszero = λz.z (λy.false) true This is equivalent to λz.((z (λy.false)) true)

Example

  • iszero 0 = (λz.z (λy.false) true) (λf.λy.y) → (λf.λy.y) (λy.false) true → (λy.y) true → true - 0 = λf.λy.y

Since (λx.y) z → y

CMSC 330 22

Arithmetic Using Church Numerals

If M and N are numbers (as λ expressions)

  • Can also encode various arithmetic operations

Addition

  • M + N = λx.λy.(M x)((N x) y) Equivalently: + = λM.λN.λx.λy.(M x)((N x) y) ¾ In prefix notation (+ M N)

Multiplication

  • M * N = λx.(M (N x)) Equivalently: * = λM.λN.λx.(M (N x)) ¾ In prefix notation (* M N)

CMSC 330 23

Arithmetic (cont.)

Prove 1+1 = 2

  • 1+1 = λx.λy.(1 x)((1 x) y) =
  • λx.λy.((λx.λy.x y) x)(((λx.λy.x y) x) y) →
  • λx.λy.(λy.x y)(((λx.λy.x y) x) y) →
  • λx.λy.(λy.x y)((λy.x y) y) →
  • λx.λy.x ((λy.x y) y) →
  • λx.λy.x (x y) = 2

With these definitions

  • Can build a theory of arithmetic
    • 1 = λf.λy.f y
    • 2 = λf.λy.f (f y)

Many implicit alpha conversions

CMSC 330 24

Looping

Define D = λx.x x, then

  • D D = (λx.x x) (λx.x x) → (λx.x x) (λx.x x) = D D

So D D is an infinite loop

  • In general, self application is how we get looping

CMSC 330 25

The “Paradoxical” Combinator

Y = λf.(λx.f (x x)) (λx.f (x x))

Then

Y F =

(λf.(λx.f (x x)) (λx.f (x x))) F → (λx.F (x x)) (λx.F (x x)) → F ((λx.F (x x)) (λx.F (x x))) = F (Y F)

Thus Y F = F (Y F) = F (F (Y F)) = ...

  • We can use Y to achieve recursion for F

CMSC 330 26

Example

fact = λf. λn.if n = 0 then 1 else n * (f (n-1))

  • The second argument to fact is the integer
  • The first argument is the function to call in the body ¾ We’ll use Y to make this recursively call fact

(Y fact) 1 = (fact (Y fact)) 1

→ if 1 = 0 then 1 else 1 * ((Y fact) 0)

→ 1 * ((Y fact) 0)

→ 1 * (fact (Y fact) 0)

→ 1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1))

CMSC 330 27

Discussion

Lambda calculus is Turing-complete

  • Most powerful language possible
  • Can represent pretty much anything in “real” language ¾ Using clever encodings

But programs would be

  • Pretty slow (10000 + 1 → thousands of function calls)
  • Pretty large (10000 + 1 → hundreds of lines of code)
  • Pretty hard to understand (recognize 10000 vs. 9999)

In practice

  • We use richer, more expressive languages
  • That include built-in primitives CMSC 330 28

The Need For Types

Consider the untyped lambda calculus

  • false = λx.λy.y
  • 0 = λx.λy.y

Since everything is encoded as a function...

  • We can easily misuse terms… ¾ false 0 → λy.y ¾ if 0 then ... …because everything evaluates to some function

The same thing happens in assembly language

  • Everything is a machine word (a bunch of bits)
  • All operations take machine words to machine words

CMSC 330 29

Simply-Typed Lambda Calculus

e ::= n | x | λx:t.e | e e

  • Added integers n as primitives ¾ Need at least two distinct types (integer & function)… ¾ …to have type errors
  • Functions now include the type of their argument

CMSC 330 30

Simply-Typed Lambda Calculus (cont.)

t ::= int | t → t

  • int is the type of integers
  • t1 → t2 is the type of a function ¾ That takes arguments of type t1 and returns result of type t
  • t1 is the domain and t2 is the range
  • Notice this is a recursive definition ¾ So we can give types to higher-order functions

Will show how to compute types later

  • Example of operational semantics