Download presentation
Presentation is loading. Please wait.
Published byJeffrey Lee Modified over 8 years ago
1
Data Structures in Scheme Building data structures: Java class Circle { private double center_x, center_y, radius; Circle(double x, double y, double r) { this.center_x = x; this.center_y = y; this.radius = r; } }; (define make-circle (lambda (center-x center-y radius) (list center-x center-y radius)))
2
Data Structures in Scheme Building data structures: Scheme - use list in a constructor function: (define make-circle (lambda (center-x center-y radius) (list center-x center-y radius)))
3
Data Structures in Scheme Accessing fields: use list-ref in a selector function: double getRadius () {// Java return radius; } (define get-radius; Scheme (lambda (circle) (list-ref circle 2)))
4
Local declaration/scope via let double area() { double pi = 3.14159, r = radius; return (pi * r * r); } (define area (lambda (circle) (let ((pi 3.14159) (r (get-radius circle))) (* pi r r))))
5
Local declaration/scope via let (let ( (var1 val1) (var2 val2).. (varN valN) ) result )
6
Shortcut with letrec > (letrec ((a 3) (b (* 2 a)) (c (+ b 1))) c) 7 > (let ((a 3)) ; long version (let ((b (* 2 a))) (let ((c (+ b 1))) c))) 7
7
Sequential execution via begin (define (prompt-for-command-char prompt) (begin (write prompt) (read)))
8
The Main Course: Essentials of Programming Languages The Big Idea: The interpreter for a computer language is just another program. Think of quotes: The claim “Nerds are cool” is very true. This simple fact has profound implications. Brings us to the Halting Problem (Turing), and related diagonalization proofs (Cantor, Gödel)
9
The Halting Problem Formulated by Hilbert as the Entscheidungsproblem (lit., “Decision Problem”, 1928) Is there a general method that will tell us whether or not a given statement is true? Gödel: NO! (For arithmetic at least, 1930's) “Cocktail party” proof: There is no proof for this statement.
10
The Halting Problem Is there a general program that will tell us whether or not a given program halts on a given input? Turing: NO! (1937) “Cocktail party” proof:
11
(define run-forever (lambda () (run-forever))) (define invert (lambda (program) (if (halts? program program) (run-forever) 'halted))) The Halting Problem (à la Abelson & Sussman) Can we write the (general) halts? predicate? function input
12
The Halting Problem (define halts? (lambda (program input)... ) (define invert (lambda (program) (if (halts? program program) (run-forever) 'halted))) (invert invert) ; uh-oh!
13
The Halting Problem If invert halts on itself, then it runs forever. If it runs forever, then it halts: CONTRADICTION This means that our assumption (the existence of halts? ) must have been wrong, Q.E.D. Reductio ad absurdum by diagonalization:
14
Diagonalization #1: Halting Problem length 37 9... 52 exit (exits) (exits)... (exits)... invert (loops) (loops)... ????? input program length exit... invert...
15
Diagonalization #2: Cantor's Proof Can the real numbers be counted (matched 1:1 with the positive integers?) Cantor (~1897): NO! Proof: If reals (-∞... +∞) can be counted, then certainly reals in (0... 1) can be counted. So let's try:
16
Diagonalization: Cantor's Proof 1.1082984911293... 2.8304302821712... 3.0024826578924.... So, now we've enumerated every real in (0,1), right?
17
Diagonalization: Cantor's Proof 1.8082984911293... 2.8604302821712... 3.0074826578924.... Lets invert every number on the diagonal, by subtracting it from 9: Now look at the number on the diagonal:.867...
18
This diagonal number can't be in the first row, because it differs in the first digit from the number in that row. It can't be in the second row, because it differs in the second digit. It can't be in the Nth row, because it differs in the Nth digit. Therefore it can't be in the table. Therefore we haven't enumerated the reals in (0,1). Therefore we can't enumerate the reals, Q.E.D.
19
Diagonalization #3: Do Mathematicians Work Harder than Computer Scientists? Are there some functions that cannot be computed – ie., for which we cannot write a computer program? Consider a program to be a finite sequence of bits (certainly valid!), so we can count programs. Consider just the class of functions that input a natural number and output true or false (e.g., test for even, prime, perfect) Assume there is a program that computes each such function….
20
even? T F T F T F T F T F … prime? F F T T F T F T F F … perfect?T F F F F F T F F F … p-o-2?F T T F T F F F T F … input program 0 1 2 3 4 5 6 7 8 9 …
21
input program 0 1 2 3 4 5 6 7 8 9 … Invert diagonal: even? F F T F T F T F T F … prime? F T T T F T F T F F … perfect?T F T F F F T F F F … p-o-2?F T T T T F F F T F …
22
The diagonal is now A valid function (assigns a unique value to each number) Not in any row of the table So there is at least one function that cannot be computed!
23
Diagonalization: What to Do? Go insane! (Cantor, Gödel, Turing) Use type systems (Russell, Wittgenstein, Church, Curry) e.g., square is undefined (type error) on strings. Denial: Die ganzen Zahlen hat der liebe Gott gemacht, alles andere ist Menschenwerk (Kronecker 1886) Surrealism / Postmodernism:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.