Documente Academic
Documente Profesional
Documente Cultură
6.001 SICP
• Elements of a Language (or Engineering Design)
Computability
• Primitives, means of combination, means of abstraction
• What we've seen...
• Procedural Abstraction:
• Deep question #1: • Lambda – captures common patterns and
"how to" knowledge
• Does every expression stand for a value?
• Functional programming & substitution model
• Deep question #2:
• Are there things we can't compute?
• Conventional interfaces:
• list-oriented programming
• Deep question #3:
• higher order procedures
• Where does our computational power
(of recursion) come from?
1 2
(2) Data, State and Objects (3) Language Design and Implementation
• Data Abstraction • Evaluation – meta-circular evaluator
• Primitive, Compound, & Symbolic Data • eval & apply
• Contracts, Abstract Data Types
• Selectors, constructors, operators, ... • Language extensions & design
• lazy evaluation
• Mutation: need for environment model • dynamic scoping
3 4
1
Deep Question #2 Mysteries of Infinity: Countability
• Two sets of numbers (or other objects) are said to have the
same cardinality (or size) if there is a one-to-one mapping
between them. This means each element in the first set
Are there well-defined things that matches to exactly one element in the second set, and vice
cannot be computed? versa.
• Any set of same cardinality as the integers is called
countable.
• {integers} same size as {even integers}: n à 2n
• {integers} same size as {squares}: n à n2
• {integers} same size as {rational numbers}
7 8
è
• Use Cantor Diagonalization again! Define a new predicate • halts? is well specified – has a clear value for its inputs
è
function by complementing the diagonals. By construction (halts? return-seven) #t
this predicate cannot be in the list (of all integers, of all (halts? loop-forever) #f
programs). Thus there are more predicate functions than
there are procedures.
11 Halperin, Kaiser, and Knight, "Concrete Abstractions," p. 114, ITP 1999. 12
2
The Halting Theorem: Deep Question #3
Procedure halts? cannot exist. Too bad!
• Proof (informal): Assume halts? exists as specified.
(define (contradict-halts)
Where does the power of recursion come from?
(if (halts? contradict-halts)
(loop-forever)
#t))
(contradict-halts)
Þ ??????
15 16
17 18
3
Harnessing our anonymous recursion How do we stop the recursion?
λ (h) (h h)) ; our anonymous infinite loop
((λ • We need to subdue the infinite recursion – how to
λ (h) (h h)))
(λ prevent (Q Q) from spinning out of control?
• We'd like to do something each time we recurse: ((λλ(h) (λ
λ(x) ((f (h h)) x)))
Q (shorthand)
(λλ(h) (λ
λ(x) ((f (h h)) x))))
λ (h) (f (h h)))
((λ
λ (h) (f (h h)))) = (D D)
Þ
(λ
λ (x) ((f (D D)) x))
(λ
Þ
= (Q Q)
Þ (f (Q Q))
Þ (f (f (Q Q)))
p: x
Þ
b: ((f (D D)) x)
(f (f (f ... (f (Q Q))..)
• So our first step in harnessing recursion results in infinite • So (D D) results in something very finite – a procedure!
recursion... but at least it generates the "stack up" of f as • That procedure object has the germ or seed (D D) inside
we expect in recursion it – the potential for further recursion!
19 20
4
Implication of 2: F Can End the Recursion Example: An F That Terminates a Recursion
Þ ((F ) #) λ(proc)
F = (λ
p: x (λλ(n) (if (= n 0) 1 ...)))
b: ((F (D D)) x)
λ(proc)
F = (λ So
(λλ(n) ((F ) 0)
...)) p: x
b: ((F (D D)) x)
• Can use this to complete a computation,
depending on value of n: Þ λ(n) (if (= n 0) 1 ...))
((λ 0)
λ(proc)
F = (λ Þ 1
(λλ(n)
(if (= n 0) • If we write F to bottom out for some values of n,
1 we can implement a base case!
...))) Let's try it!
25 26
Implication of 1: F Should have Proc as Arg Implication of 1: F Should have Proc as Arg
• The more complicated (confusing) issue is how to arrange • Question is: how do we appropriately use proc inside F?
for F to take a proc of the form we need: • Well, when we use proc, what happens?
( #)
We need F to conform to: p: x
((F ) 0) b: ((F (D D)) x)
p: x
Þ ((F (D D)) #)
b: ((F (D D)) x)
5
Putting it all together Y Combinator: The Essence of Recursion
( (Y F) 10) = ((Y F) x) = ((D D) x) = ((F (Y F)) x)
λ(f) ((λ
( ((λ λ(h) (λ
λ(x) ((f (h h)) x)))
λ(h) (λ
(λ λ(x) ((f (h h)) x))))) The power of controlled recursion!
λ(fact)
(λ
λ(n)
(λ No define – only
(if (= n 0) lambda and the power
of Y combinator!
1
(* n (fact (- n 1)))))))
10) λ(f) ((λ
(λ λ(h) (λ
λ(x) ((f (h h)) x)))
Þ (* 10 (* ... (* 3 (* 2 (* 1 1)))
λ(h) (λ
(λ λ(x) ((f (h h)) x)))))
Þ 3628800
31 32
33 34