Docsity

Representing Data Structures and Functions using Church Numerals and Combinators in CS 421, Study notes of Computer Science

Toggle sidebar
bg1
Programming Languages and
Compilers (CS 421)
Elsa L Gunter
2112 SC, UIUC
http://www.cs.uiuc.edu/class
/sp07/cs421/
Based in part on slides by Mattox Beckman, as updated
by Vikram Adve and Gul Agha
Elsa L. Gunter
How to Represent (Free) Data
Structures (First Pass)
•Suppose τ is a type with n constructors:
C1,…,Cn (no arguments)
•Represent each term as an abstraction:
•Ci
→
λ x1. … λxn. xi
•Think: you give me what to return in
each case (think match statement) and
I’ll apply the correct case for the i'th
constructor
Elsa L. Gunter
How to Represent Booleans
•bool = True | False
•True -> λ x1 x2. x1 ≡α λ x. λ y. x
•False -> λ x1 x2. x2 ≡α λ x. λ y. y
•Notation
–Will write
λ x1 … xn. e for λ x1. … λxn. E
e1 e2 … en for (…(e1 e2 )… en )
Elsa L. Gunter
How to Write Functions over
Data Structures
•Write a “match” or “case” function
• match c with C1 -> x1
| …
| Cn -> xn
→
λ x1 … xn c. c x1…xn
•Think: give me what to do in each case
and give me a case, and I’ll apply that
case
Elsa L. Gunter
How to Write Functions over
Booleans
•If b then c else d
• if_then_else b x1 x2 = b x1 x2
• if_then_else ≡ λ b x1 x2 . b x1 x2
Alternately,
if_then_else b x1 x2 =
match b with True -> x1 | False -> x2 =
(λ x1 x2 b . b x1 x2 ) x1 x2 b
if_then_else = λ b x1 x2 (matchbool x1 x2 b)
Elsa L. Gunter
Example:
not b = if b then False else True
= if_then_else b False True
= (λ b c d. b c d) b (λ x y. y)(λ x y. x)
= b (λ x y. y)(λ x y. x)
•not ≡ λ b. b (λ x y. y)(λ x y. x)
• Try and, or
pf3
pf4

Partial preview of the text

Download Representing Data Structures and Functions using Church Numerals and Combinators in CS 421 and more Study notes Computer Science in PDF only on Docsity!

Programming Languages and

Compilers (CS 421)

Elsa L Gunter

2112 SC, UIUC

http://www.cs.uiuc.edu/class

/sp07/cs421/

Based in part on slides by Mattox Beckman, as updated

by Vikram Adve and Gul Agha

Elsa L. Gunter

How to Represent (Free) Data

Structures (First Pass)

  • Suppose τ is a type with n constructors:

C

1

,…, C

n

(no arguments)

  • Represent each term as an abstraction:

• C

i

→λ x 1

. … λx n . x i

  • Think: you give me what to return in

each case (think match statement) and

I’ll apply the correct case for the i' th

constructor

Elsa L. Gunter

How to Represent Booleans

  • bool = True | False
  • True -> λ x 1

x 2

. x 1

α

λ x. λ y. x

  • False -> λ x 1

x 2

. x 2

α

λ x. λ y. y

  • Notation
    • Will write

λ x 1

… x n

. e for λ x 1 . … λx n

. E

e 1 e 2 … e n for (…(e 1 e 2 )… e n

Elsa L. Gunter

How to Write Functions over

Data Structures

  • Write a “match” or “case” function
  • match c with C 1 -> x 1

| C

n -> x n

→ λ x 1

… x n

c. c x 1

…x n

  • Think: give me what to do in each case

and give me a case, and I’ll apply that

case

How to Write Functions over

Booleans

  • If b then c else d
  • if_then_else b x 1 x 2 = b x 1 x 2
  • if_then_else ≡ λ b x 1

x 2

. b x 1

x 2

Alternately,

if_then_else b x 1 x 2

match b with True -> x 1 | False -> x 2

(λ x 1

x 2

b. b x 1

x 2

) x 1

x 2

b

if_then_else = λ b x 1 x 2 (match bool x 1 x 2 b)

Example:

not b = if b then False else True

= if_then_else b False True

= (λ b c d. b c d) b (λ x y. y)(λ x y. x)

= b (λ x y. y)(λ x y. x)

  • not ≡ λ b. b (λ x y. y)(λ x y. x)
  • Try and, or

Elsa L. Gunter Elsa L. Gunter

How to Represent (Free) Data

Structures (Second Pass)

  • Suppose τ is a type with n constructors:

C

1

t 11

… t 1k,

…, C

n

t n

… t nm,

  • Represent each term as an abstraction:

• C

i

t i

… t ij,

→λ x 1

… x n

. x i

t i

… t ij,

• C

i →λ t i … t ij, x 1 … x n

. x i t i … t ij,

  • Think: you need to give each

constructor its arguments fisrt

Elsa L. Gunter

How to Represent Pairs

  • Pair has one constructor (comma) that

takes two arguments

  • (a , b) --> λ x. x a b
  • (_ , _) --> λ a b x. x a b

Elsa L. Gunter

Functions over Pairs

  • fst ≡ λ p. p (λ x y. x)

fst (u , v) -->

(λ p. p (λ x y. x))((λ a b x. x a b) u v) -->

(λ p. p (λ x y. x))(λ x. x u v) -->

(λ x. x u v) (λ x y. x) -->

(λ x y. x) u v --> (λ y. u) v --> u

  • snd ≡ λ p. p (λ x y. y)

How to Represent (Free) Data

Structures (Third Pass)

  • Suppose τ is a type with n constructors:

C

1

t 11

… t 1k,

…, C

n

t n

… t nm,

  • Suppose t ih

; τ (ie. is recursive)

  • In place of a value t ih

have a function to

compute the recursive value t ih x 1 … x n

• C

i

λ t i

… t ij,

x 1

… x n

. x i

t i

… (t ih

x 1

… x n

) … t ij,

How to Represent Natural

Numbers

  • nat = Suc nat | 0
  • 0 ≡ λ f x. x
  • Suc n = λ f x. f (n f x)
  • Suc ≡ λ n f x. f (n f x)
  • Such re presentation called

Church Numerals

Elsa L. Gunter

Recursion

  • Want a λ-term Y such that for all term R

we have

• Y R = R (Y R)

  • Y needs to have replication to

“remember” a copy of R

  • Y = λ y. (λ x. y(x x)) (λ x. y(x x))
  • Y R = (λ x. R(x x)) (λ x. R(x x))
  • = R ((λ x. R(x x)) (λ x. R(x x)))
  • Notice: Requires lazy evaluation

Elsa L. Gunter

Factorial

  • Let F = λ f n. if n = 0 then 1 else n * f(n-1)
  • Y F 3 = F (Y F) 3
  • = if 3 = 0 then 1 else 3 * ((Y F)(3 -1))
  • = 3 * (Y F) 2 = 3 * (F(Y F) 2)
  • = 3 * (if 2 = 0 then 1 else 2 * (Y F)(2-1))
  • = 3 * (2 * (Y F)(1)) = 3 * (2 * (F(Y F) 1)) =…
  • = 3 * 2 * 1 * (if 0 = 0 then 1 else 0*(Y F)(0 -1))
  • = 3 * 2 * 1 * 1 = 6

Elsa L. Gunter

Y in OCaml

let rec y f = f (y f);;

val y : ('a -> 'a) -> 'a =

let mk_fact =

fun f n -> if n = 0 then 1 else n * f(n-1);;

val mk_fact : (int -> int) -> int -> int =

y mk_fact;;

Stack overflow during evaluation (looping

recursion?).

Elsa L. Gunter

Eager Eval Y in Ocaml

let rec y f x = f (y f) x;;

val y : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b =

y mk_fact;;

  • : int -> int =

y mk_fact 5;;

  • : int = 120
  • Used recursion to get recursion

Elsa L. Gunter

Some Other Combinators

  • For your general exposure
  • I = λ x. x
  • K = λ x. λ y. x

• K

= λ x. λ y. y

  • S = λ x. λ y. λ z. x z (y z)