Review: Church Encodings true = \x.\y.x; // Booleans false = - - PowerPoint PPT Presentation

review church encodings
SMART_READER_LITE
LIVE PREVIEW

Review: Church Encodings true = \x.\y.x; // Booleans false = - - PowerPoint PPT Presentation

Review: Church Encodings true = \x.\y.x; // Booleans false = \x.\y.y; pair = \x.\y.\f.f x y; // pairs fst = \p.p (\x.\y.x); snd = \p.p (\x.\y.y); noreduce bot = (\x.x x)(\x.x x); // divergence // S-expressions nil = \n.\c.n; cons


slide-1
SLIDE 1

Review: Church Encodings

true = \x.\y.x; // Booleans false = \x.\y.y; pair = \x.\y.\f.f x y; // pairs fst = \p.p (\x.\y.x); snd = \p.p (\x.\y.y); noreduce bot = (\x.x x)(\x.x x); // divergence // S-expressions nil = \n.\c.n; cons = \y.\ys.\n.\c.c y ys; null? = \xs.xs true (\y.\ys.false); noreduce car = \xs.xs bot (\y.\ys.y); noreduce cdr = \xs.xs bot (\y.\ys.ys);

slide-2
SLIDE 2

Review: Church Numerals

zero = \f.\x.x; succ = \n.\f.\x.f (n f x); plus = \n.\m.n succ m; times = \n.\m.n (plus m) zero; ...

  • > four;

\f.\x.f (f (f (f x)))

  • > three;

\f.\x.f (f (f x))

  • > times four three;

\f.\x.f (f (f (f (f (f (f (f (f (f (f (f x)))))))))))

slide-3
SLIDE 3

Reduction rules

Central rule based on substitution

(x :M )N
  • ! M
[x 7! N ℄

(BETA)

Structural rules: Beta-reduce anywhere, any time N

  • ! N′

MN

  • ! MN′

M

  • ! M′

MN

  • ! M′N

M

  • ! M′
x :M
  • !
x :M′
slide-4
SLIDE 4

Free variables

x is free in x x is free in M

_ x is free in N

x is free in MN x is free in M x

6= x′

x is free in

x′ :M
slide-5
SLIDE 5

Your turn! Free Variables

What are the free variables in each expression? \x.\y. y z \x.x (\y.x) \x.\y.\x.x y \x.\y.x (\z.y w) y (\x.z) (\x.\y.x y) y

slide-6
SLIDE 6

Your turn! Free Variables

What are the free variables in each expression? \x.\y. y z

  • z

\x.x (\y.x)

  • nothing

\x.\y.\x.x y

  • nothing

\x.\y.x (\z.y w)

  • w

y (\x.z)

  • y z

(\x.\y.x y) y

  • y
slide-7
SLIDE 7

Capture-avoiding substitution

x [x

7! M ℄ =

M y

[x 7! M ℄ =

y

(YZ )[x 7! M ℄ = (Y [x 7! M ℄)(Z [x 7! M ℄) (x :Y )[x 7! M ℄ = x :Y (y :Z )[x 7! M ℄ = y :Z [x 7! M ℄

if x not free in Z or y not free in M

(y :Z )[x 7! M ℄ = w :(Z [y 7! w ℄)[x 7! M ℄

where w not free in Z or M Last transformation is renaming of bound variables

slide-8
SLIDE 8

Renaming of bound variables

So important it has its own Greek letter: w not free in Z

y :Z
  • !
w :(Z [y 7! w ℄)

(ALPHA) Also has structural rules

slide-9
SLIDE 9

Conversion and reduction

Alpha-conversion (rename bound variable) y not free in Z

x :Z
  • !
y :Z [x 7! y ℄

Beta-reduction (the serious evaluation rule)

(x :M )N
  • ! M
[x 7! N ℄

Eta-reduction: x not free in M

x :Mx
  • ! M

All structural: Convert/reduce whole term or subterm

slide-10
SLIDE 10

Church-Rosser Theorem

Equivalence of convertible terms: if A

! B and A ! C

there exists D s.t. B

!∗ D and C !∗ D
slide-11
SLIDE 11

Idea: normal form

A term is a normal form if It cannot be reduced What do you suppose it means to say

  • A term has no normal form?
  • A term has a normal form?
slide-12
SLIDE 12

Idea: normal form

A term is a normal form if It cannot be reduced A term has a normal form if There exists a sequence of reductions that terminates (in a normal form) A term has no normal form if It always reduces forever (This term diverges)

slide-13
SLIDE 13

Normal forms code for values

Corollary of Church-Rosser: if A

!∗ B, B in normal form, and

A

!∗ C, C in normal form

then B and C are identical (up to renaming of bound variables)

slide-14
SLIDE 14

Normal-order reduction

(If a normal form exists, find it!) Application offers up to three choices:

#1

(x :M )N
  • ! M
[x 7! N ℄

(BETA) #3

N

  • ! N′

MN

  • ! MN′

#2

M

  • ! M′

MN

  • ! M′N

M

  • ! M′
x :M
  • !
x :M′

Slogan: “leftmost, outermost redex”

slide-15
SLIDE 15

Normal-order illustration

Not every term has a normal form:

(x :xx )(x :xx )
  • !
(x :xx )(x :xx )

But

(x :y :y )((x :xx )(x :xx ))
  • !
y :y

Think “bodies before arguments” Applicative order does not terminate!

slide-16
SLIDE 16

What solves this equation?

Equation: fact

= n :if n = 0 then 1 else n fact (n 1 )?

The factorial function!

slide-17
SLIDE 17

Review

What solves this Equation? fact

= n :if n = 0 then 1 else n fact (n 1 )?

The factorial function!

slide-18
SLIDE 18

Your turn: Recursion equations

Is there a solution? Is it unique? If so, what is it? f1 = \n.\m.(eq? n m) n (plus n (f1 (succ n) m)); f2 = \n.f2 (isZero? n 100 (pred n)); f3 = \xs.xs nil (\z.\zs.cons 0 (f3 zs)); f4 = \xs.\ys.f4 ys xs;

slide-19
SLIDE 19

Wait for it...

slide-20
SLIDE 20

Your turn: Recursion equations

f1 = \n.\m.(eq? n m) n (plus n (f1 (succ n) m)); ; sigma (sum from n to m) f2 = \n.f2 (isZero? n 100 (pred n)); ; no unique solution (any constant f2) f3 = \xs.xs nil (\z.\zs.cons 0 (f3 zs)); ; map (const 0) f4 = \xs.\ys. f4 xs ys; ; not unique: constant functions, commutative

slide-21
SLIDE 21

Recursion = Fixed point

Equation: fact

= n :if n = 0 then 1 else n fact (n 1 )?

Definition such that fact

= g fact:

g

= f :n :if n = 0 then 1 else n f (n 1 )
slide-22
SLIDE 22

Conversion to fixed point

length = \xs.null? xs 0 (+ 1 (length (cdr xs))) lg = \lf.\xs.null? xs 0 (+ 1 (lf (cdr xs)))

slide-23
SLIDE 23

One startling idea

You can define a fixed-point operator

  • More than one!
slide-24
SLIDE 24

A simple algebraic law

If fix g = g (fix g) then fix g can define recursive functions! The only recursion equation you’ll ever need

slide-25
SLIDE 25

Y combinator can implement fix

Define Y such that, for any g, Y g

= g (Y g ):

Y

= f :(x :f (xx ))(x :f (xx ))

Y g

= (x :g (xx ))(x :g (xx ))

and by beta-conversion Y g

=

g

((x :g (xx ))(x :g (xx )))

Y g

=

g

(Y g )

so Y g is a fixed point of g Does Y g have a normal form?