Lambda Calculus, Booleans, Natural Numbers - Notes | CMSC 330, Study notes of Programming Languages

Material Type: Notes; Class: ORGNZTN PROGM LANG; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-jil
koofers-user-jil 🇺🇸

4

(1)

10 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 330: Organization of
Programming Languages
Lambda Calculus and Types
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Lambda Calculus, Booleans, Natural Numbers - Notes | CMSC 330 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Lambda Calculus and Types

Lambda Calculus

• 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! Only higher-order functions

Encodings

• It turns out that this language is Turing complete

• That means we can encode any computation we

want in it

  • (^) ...if we’re sufficiently clever...

Booleans

true = λx.λy.x false = λx.λy.y

if a then b else c is defined to be the λ expression: a b c

  • (^) Examples:
    • (^) if true then b else c → (λx.λy.x) b c → (λy.b) c → b
    • (^) if false then b else c → (λx.λy.y) b c → (λy.y) c → c

Pairs

(a,b) = λx.if x then a else b

fst = λf.f true

snd = λf.f false

• Examples:

  • (^) fst (a,b) = (λf.f true) (λx.if x then a else b) → (λx.if x then a else b) true → if true then a else b → a
  • (^) snd (a,b) = (λf.f false) (λx.if x then a else b) → (λx.if x then a else b) false → if false then a else b → b

Natural Numbers (Church*)

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.

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

iszero = λg.g (λy.false) true

  • (^) Recall that this is equivalent to λg.((g (λy.false)) true) *(Named after Alonzo Church, developer of lambda calculus)

Arithmetic defined

  • (^) Addition, if M and N are integers (as λ expressions): M + N = λx.λy.(M x)((N x) y) Equivalently: + = λM.λN.λx.λy.(M x)((N x) y)
  • (^) Multiplication: M * N = λx.(M (N x))
  • (^) 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 integer arithmetic.

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

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

Discussion

• Using encodings we can represent pretty much

anything we have in a “real” language

  • (^) But programs would be pretty slow if we really implemented things this way
  • (^) In practice, we use richer languages that include built- in primitives

• Lambda calculus shows all the issues with

scoping and higher-order functions

• It's useful for understanding how languages work

What is a Type System?

• A type system is some mechanism for

distinguishing good programs from bad

  • (^) Good = well typed
  • (^) Bad = ill typed or not typable; has a type error

• Examples

  • (^) 0 + 1 // well typed
  • (^) false 0 // ill-typed; can’t apply a boolean

Static versus Dynamic Typing

• In a static type system , we guarantee at compile

time that all program executions will be free of

type errors

  • (^) OCaml and C have static type systems

• In a dynamic type system , we wait until runtime,

and halt a program (or raise an exception) if we

detect a type error

  • (^) Ruby has a dynamic type system

• Java, C++ have a combination of the two

Type Judgments

• We will construct a type system that proves

judgments of the form

A ⊢ e : t

  • (^) “In type environment A, expression e has type t”

• If for a program e we can prove that it has some

type, then the program type checks

  • (^) Otherwise the program has a type error, and we’ll reject the program as bad

Type Environments

• A type environment is a map from variables

names to their types

• • is the empty type environment

• A, x:t is just like A, except x now has type t

• When we see a variable in the program, we’ll

look up its type in the environment