head	1.2;
access;
symbols;
locks
	leavens:1.2; strict;
comment	@# @;


1.2
date	93.03.31.05.47.18;	author leavens;	state Exp;
branches;
next	1.1;

1.1
date	93.03.31.05.18.27;	author leavens;	state Exp;
branches;
next	;


desc
@deep recursion.
@


1.2
log
@modernized types and variable names.
@
text
@;; AUTHOR: Gary T. Leavens

(define deep-recur-abs
  ; TYPE: all S . all T . T, (S,T->T), (T,T ->T), (datum -> boolean)
  ;                         -> ((tree of S) -> T)
  (lambda (seed flat-proc tree-proc leaf?)
    ; REQUIRES: (leaf? x) = (x has type S)
    ; ENSURES: (result '()) = seed,
    ;          (result (cons x t))  = (flat-proc x (result t)),
    ;          (result (cons t1 t2)) = (tree-proc (result t1) (result t2))
    ;          where (leaf? x) holds, but not (leaf? t1) and (leaf? t2).
    (letrec
      ((helper  ; TYPE: all S. all T . (tree of S) -> T
         (lambda (tr)
           (if (null? tr)
               seed
               (let ((a (car tr)))
                 (if (leaf? a)
                     (flat-proc a (helper (cdr tr)))
                     (tree-proc (helper a) (helper (cdr tr)))))))))
      helper)))


;; The following allows easy creation of deep recursions schemes over
;; "trees of S", by using a leaf? predicate that tests for type S.

(define deep-recur-abs-m
  ; TYPE: all S . all T . (datum -> boolean)
  ;                       -> (T, (S,T->T), (T,T ->T) -> ((tree of S) -> T))
  (lambda (leaf?)
    (lambda (seed flat-proc tree-proc)
      (deep-recur-abs seed flat-proc tree-proc leaf?))))


;; The following is the case of deep recursion over trees of atomic-items.
;; Compare with program 7.28

(define deep-recur
  ; TYPE: all S . all T . T, (S,T->T), (T,T ->T) -> ((tree of S) -> T)
  ; REQUIRES: if x has type S, then (atomic-item? x) is true
  (let ((atomic-item?  ; TYPE: datum -> boolean
	     (lambda (x)
	       (not (or (pair? x)
			(null? x))))))
    (deep-recur-abs-m atomic-item?)))
@


1.1
log
@Initial revision
@
text
@d1 1
a1 1
;; compare Program 7.28, pg. 227
d4 4
a7 4
  ; TYPE: b, (a, b -> b), (b, b -> b), (datum -> boolean)
  ;                 -> (tree of a -> b)
  (lambda (seed item-proc list-proc leaf?)
    ; REQUIRES: (leaf? x) is true iff x has type a
d9 3
a11 3
    ;          (result (cons x t)) = (item-proc x (result t)),
    ;          (result (cons t1 t2)) = (list-proc (result t1) (result t2))
    ;  where (leaf? x) holds, but not (leaf? t1) and (leaf? t2).
d13 3
a15 3
      ((helper  ; TYPE: tree of a -> b
         (lambda (ls)
           (if (null? ls)
d17 1
a17 1
               (let ((a (car ls)))
d19 2
a20 3
                     (item-proc a (helper (cdr ls)))
                     (list-proc (helper a) (helper (cdr ls)))))
	       ))))
d22 24
@
