Lecture 9: Crazy Eddie and the Fixed Points M.C. Escher, Ascending and Descending (also known as f.

Download Report

Transcript Lecture 9: Crazy Eddie and the Fixed Points M.C. Escher, Ascending and Descending (also known as f.

Lecture 9:
Crazy Eddie and
the Fixed Points
M.C. Escher,
Ascending and
Descending (also
known as
f. (( x.f (xx))
( x. f (xx)))
CS655: Programming Languages
David Evans
University of Virginia
http://www.cs.virginia.edu/~evans
Computer Science
-term Evaluation
Y   f. (( x.f (xx)) ( x. f (xx)))
What is (Y I)?
Recall I = z.z
Evaluation rule:
-reduction
(substitution)
(x. M)N   M [ x | N]
Substitute N for x in M.
6 November 2015
University of Virginia CS 655
2
Alyssa P. Hacker’s Answer
( f. (( x.f (xx)) ( x. f (xx)))) (z.z)
  (x.(z.z)(xx)) ( x. (z.z)(xx))
  (z.z) ( x.(z.z)(xx)) ( x.(z.z)(xx))
  (x.(z.z)(xx)) ( x.(z.z)(xx))
  (z.z) ( x.(z.z)(xx)) ( x.(z.z)(xx))
  (x.(z.z)(xx)) ( x.(z.z)(xx))
  ...
6 November 2015
University of Virginia CS 655
3
Ben Bitdiddle’s Answer
( f. (( x.f (xx)) ( x. f (xx)))) (z.z)
  (x.(z.z)(xx)) ( x. (z.z)(xx))
  (x.xx) (x.(z.z)(xx))
  (x.xx) (x.xx)
  (x.xx) (x.xx)
  ...
6 November 2015
University of Virginia CS 655
4
Recursive Definitions
Which of these make you uncomfortable?
x=1+x
x=4–x
x=9/x
x=x
x = 1 / (16x3)
6 November 2015
No solutions over integers
1 integer solution, x = 2
2 integer solutions, x = 3, x = -3
Infinitely many integer solutions
No integer solutions,
two rational solutions (1/2 and –1/2),
four complex solutions (1/2, -1/2, i/2, -i/2)
University of Virginia CS 655
5
Equations  Functions
x=4–x
f  z. 4 – z
Find an x such that (f x) = f.
Same as solve for x.
6 November 2015
University of Virginia CS 655
6
What’s a function?
f  z. 4 – z
f: Nat  Nat
{ <0, 4>, <1, 3>, <2, 2>,
<3, 1>, <4, 0>,
<5, ???>, ... }
6 November 2015
University of Virginia CS 655
7
Domains
• Set: unordered collection of values
Nat = { 0, 2, 1, 4, 3, 6, 5, 8, 7, ... }
• Domains: like a set, but has structure
Nat = { 0, 1, 2, 3, 4, 5, 6, 7, 8, ... }
where 0 is the least element, and
there is an order of the elements.
6 November 2015
University of Virginia CS 655
8
Making Domains
• Primitive domains can be combined to
make new domains
• Product domain: D1 x D2
– Pairs of values
Nat x Nat =
{ (tupleNat,Nat 0 0), (tupleNat,Nat 0 1),
(tupleNat,Nat 1 0), (tupleNat,Nat 1 1),
(tupleNat,Nat 0 2), ... }
6 November 2015
University of Virginia CS 655
9
Product Domains
Nat x Nat = { <0, 0>, <0, 1>, <1, 0>, <1, 1>,
<0, 2>, ... }
Watch out: remember the order matters!
(Later today: what is the “right” order for
the elements of Nat x Nat? The order
shown is “wrong”.)
6 November 2015
University of Virginia CS 655
10
Projections
• Product domains come with projection
functions:
ProjiD1,D2 ,... ,Dn : D1 x D2 x ... x Dn  Di
Proj1Nat,Nat = Nat x Nat  Nat
6 November 2015
University of Virginia CS 655
11
Function Domains
• Function domain: D1  D2
– An infix mapping from D1 to D2
z. z +Int 1: Int  Int
z. z >Int 0: Int  Bool
6 November 2015
University of Virginia CS 655
12
Functions
• A set of input-output pairs
• The inputs (Di) and outputs (Do) are
elements of a domain
• The function is an element of the domain
Di  Do.
• Its a (completely defined) function if and
only if for every element d  Di, the set
of input-output pairs has one member
whose first member matches d.
6 November 2015
University of Virginia CS 655
13
Functions?
f: Nat  Nat  z. 4 – z
f: Nat  Int  z. 4 – z
f: Nat  Nat  z. z + 1
f: ?  ? 
z. if (z = 0) then 0
else (2 + f (z – 1))
6 November 2015
University of Virginia CS 655
14
Functions
f: (Nat  Nat) =  n. (1 + ( f n))
No solutions (over natural numbers) –
would require x = 1 + x.
f: (Nat  Nat) =  n. ( f (1 + n))
Infinitely many solutions – e.g., f(x)
= 3.
{ <0, 3>, <1, 3>, <2, 3>, ... }
6 November 2015
University of Virginia CS 655
15
Fixed Points
• A fixed point of a function f: (D  D)
is an element d D such that (f d) =
d.
• Examples:
f: Nat  Int  z. 4 – z
f: Nat  Nat  z. 2 * z
f: Nat  Nat  z.z
6 November 2015
University of Virginia CS 655
2
0
infinitely many
16
Fixed Points
Sequence type – like a list
map: Nat* x (Nat  Nat)  Nat* 
lf. if (null? l) l
(cons (f (car l))
(map (cdr l) f)))
Some fixed points:
<Null, any function>, <any list, x.x>, ...
6 November 2015
University of Virginia CS 655
17
Fixed Points Demo
Ryan Persaud’s swarm simulation
6 November 2015
University of Virginia CS 655
18
Generating Functions
• Any recursive definition can be encoded
with a (non-recursive) generating
function
f: (Nat  Nat) =  n. (1 + ( f n))
(Nat  Nat)  (Nat  Nat) =
 f.  n. (1 + ( f n))
 g:
• Solution to a recursive definition is a
fixed point of its associated generating
function.
6 November 2015
University of Virginia CS 655
19
Example
fact: (Nat  Nat) =
 n. if (n = 0) then 1
else (n * ( fact (n - 1)))
gfact: (Nat  Nat)  (Nat  Nat) =
 f. n. if (n = 0) then 1
else (n * ( f (n - 1)))
6 November 2015
University of Virginia CS 655
20
(gfact I)
(gfact (z.z)) =
n. if (n = 0) then 1
else (n * ((z.z) (n - 1)))
Function that returns 1 for 0, 0 for
1, 2 for 2, ..., n * n-1 for n ~= 0.
6 November 2015
University of Virginia CS 655
21
(gfact factorial)
(gfact factorial) =
n. if (n = 0) then 1
else (n * (factorial (n - 1)))
= factorial
factorial is a fixed point of gfact
6 November 2015
University of Virginia CS 655
22
Unsettling Questions
• Is a factorial function the only fixed point
of gfact?
• Given an arbitrary function, how does
one find a fixed point?
• If there is more than one fixed point,
how do you know which is the right
one?
6 November 2015
University of Virginia CS 655
23
Iterative Fixed Point Technique
• Start with some element d D
• Calculate g(d), g(g(d)), g(g(g(d))), ... until you
get g(g(v)) = v then v is a fixed point.
• If you start with   D you get the least fixed
point (which is the “best” one)
•  (pronounced “bottom”) is the element of D
such that for any element d D,  d.
• means “has less information than” or “is
weaker than”
• Not all domains have a .
6 November 2015
University of Virginia CS 655
24
Partial Order
A partial order is a pair (D, ) of a
domain D and a relation that is
for all a, b, c  D
a a
reflexive
transitive
a b and b c imply a c
and anti-symmetric
a b and b a imply a = b
6 November 2015
University of Virginia CS 655
25
Partial Orders?
• (Nat, <=)
• (Nat, =)
• ({ burger, fries, coke }, yummier)
where burger yummier fries, burger yummier coke,
fries yummier coke
• ({ burger, fries, beer }, yummier)
where burger yummier fries, fries yummier beer,
beer yummier burger
6 November 2015
University of Virginia CS 655
26
Pointed Partial Order
• A partial order (D, ) is pointed if it has
a bottom element u  D such that u d
for all elements d  D.
• Bottom of (Nat, <=)? 0
• Bottom of (Nat, =)? Not a pointed partial order
• Bottom of (Int, <=)? Not a pointed partial order
• Bottom of ({Nat}, <=)? {}
6 November 2015
University of Virginia CS 655
27
Partial Order of Functions
• ((D, D)  (E, E),
order using:
f
D E
DE)
is a partial
g if for all d  D, (f d)
E
(g d).
double: Nat  Nat =
n. if (n = 0) then 0 else (2 + (double (n – 1))
Is double fact under (Nat, <=)?
No, (double 1) is not <= (fact 1)
6 November 2015
University of Virginia CS 655
28
Getting to the  of things
• Think of bottom as the element with the least
information, or the “worst” possible
approximation.
• Bottom of Nat  Nat is a function that is
undefined for all inputs. That is, the function
with the graph {}.
• To find the least fixed point in a function
domain, start with the function whose graph is
{} and iterate.
6 November 2015
University of Virginia CS 655
29
Least Fixed Point of gfact
gfact: (Nat  Nat)  (Nat  Nat) =
 f. n. if (n = 0) then 1
else (n * ( f (n - 1)))
(gfactn (function with graph {})) = fact
as n approaches infinity.
6 November 2015
University of Virginia CS 655
30
Fixed Point Theorem
• Do all -calculus terms have a
fixed point?
• (Smullyan: Is there a Sage
bird?)
6 November 2015
University of Virginia CS 655
31
Finding the Sage Bird
•  F ,  X  such that FX = X
• Proof:
Let W =  x.F(xx) and X = WW.
X = WW = ( x.F(xx))W
  F (WW) = FX
6 November 2015
University of Virginia CS 655
32
Why of Y?
• Y is  f. WW:
Y   f. (( x.f (xx)) ( x. f (xx)))
• Y calculates a fixed point (but not
necessarily the least fixed point) of
any lambda term!
• If you’re not convinced, try
calculating ((Y fact) n). (PS1, 1e)
6 November 2015
University of Virginia CS 655
33
Still Uncomfortable?
6 November 2015
University of Virginia CS 655
34
Remember the problem
reducing Y
• Different reduction orders produce different
results
• Reduction may never terminate
• Normal Form means no more ß-reductions
can be done
– There are no subterms of the form (x. M)N
• Not all -terms can be written in normal form
6 November 2015
University of Virginia CS 655
35
Church-Rosser Theorem
• If a -term has a normal form, any reduction
sequence that produces a normal form
always the same normal form
– Computation is deterministic
– Some orders of evaluation might not terminate
though
• Evaluating leftmost first finds the normal form
if there is one.
• Proof by trust the theory people, but don’t
become one.
6 November 2015
University of Virginia CS 655
36
Charge
• PS2 is due next Thursday
– Answers should be short
• Domains are like types in programming
languages, we will see them again
soon...
• Lots of readings out today, but check
manifest carefully to see what you
should read!
6 November 2015
University of Virginia CS 655
37