;; Caltech CS 1 Fall 2007
;; Scheme code used in Lecture 19 (12/03/07)
;; mvanier@cs.caltech.edu
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (h x) (if (= x 0) 0 (+ 1 (h (+ x 1)))))
;; Lambda wrapper for h:
(define hh 
  (lambda (x) 
    (define (hhh x) 
      (if (= x 0) 0 (+ 1 (hhh (+ x 1)))))
    (hhh x)))
;; Quoted lambda wrapper for h:
(define qh 
  '(lambda (x) 
     (define (hhh x) 
       (if (= x 0) 0 (+ 1 (hhh (+ x 1)))))
     (hhh x)))
;; Using the "eval" special form to evaluate
;; quoted lambda versions of functions:
(eval expr env)
((eval qh (list)) -3)
; => 3
((eval qh (list)) 3)
; => Aborting! Maximum recursion depth exceeded

;; Question: Can we define a function T that 
;; -- takes a quoted procedure as its argument, 
;; -- returns #t if that procedure applied to itself terminates,
;; -- otherwise returns #f?
(define (T x) ...) ;; hypothetical function

(define K 
  '(lambda (x)
     (define (T x) ...)   ;; assumed
     (define (loop-forever) (loop-forever)) 
     (if (T x)                ;; if x terminates
         (loop-forever)  ;; loop forever
         0)))                  ;; else terminate

;; Question: What does K return when given K itself as its input?
((eval K (list)) K)
;; Leads to a contradiction
;; Conclusion: We cannot compute the function T.
