Debian Codegraph @

benchmarks.larceny/src/peval.scm

raw
1 ;;; PEVAL -- A simple partial evaluator for Scheme, written by Marc Feeley.
2
3 ;------------------------------------------------------------------------------
4
5 ; Utilities
6
7 (define (every? pred? l)
8 (let loop ((l l))
9 (or (null? l) (and (pred? (car l)) (loop (cdr l))))))
10
11 (define (some? pred? l)
12 (let loop ((l l))
13 (if (null? l) #f (or (pred? (car l)) (loop (cdr l))))))
14
15 (define (map2 f l1 l2)
16 (let loop ((l1 l1) (l2 l2))
17 (if (pair? l1)
18 (cons (f (car l1) (car l2)) (loop (cdr l1) (cdr l2)))
19 '())))
20
21 (define (get-last-pair l)
22 (let loop ((l l))
23 (let ((x (cdr l))) (if (pair? x) (loop x) l))))
24
25 ;------------------------------------------------------------------------------
26 ;
27 ; The partial evaluator.
28
29 (define (partial-evaluate proc args)
30 (peval (alphatize proc '()) args))
31
32 (define (alphatize exp env) ; return a copy of 'exp' where each bound var has
33 (define (alpha exp) ; been renamed (to prevent aliasing problems)
34 (cond ((const-expr? exp)
35 (quot (const-value exp)))
36 ((symbol? exp)
37 (let ((x (assq exp env))) (if x (cdr x) exp)))
38 ((or (eq? (car exp) 'if) (eq? (car exp) 'begin))
39 (cons (car exp) (map alpha (cdr exp))))
40 ((or (eq? (car exp) 'let) (eq? (car exp) 'letrec))
41 (let ((new-env (new-variables (map car (cadr exp)) env)))
42 (list (car exp)
43 (map (lambda (x)
44 (list (cdr (assq (car x) new-env))
45 (if (eq? (car exp) 'let)
46 (alpha (cadr x))
47 (alphatize (cadr x) new-env))))
48 (cadr exp))
49 (alphatize (caddr exp) new-env))))
50 ((eq? (car exp) 'lambda)
51 (let ((new-env (new-variables (cadr exp) env)))
52 (list 'lambda
53 (map (lambda (x) (cdr (assq x new-env))) (cadr exp))
54 (alphatize (caddr exp) new-env))))
55 (else
56 (map alpha exp))))
57 (alpha exp))
58
59 (define (const-expr? expr) ; is 'expr' a constant expression?
60 (and (not (symbol? expr))
61 (or (not (pair? expr))
62 (eq? (car expr) 'quote))))
63
64 (define (const-value expr) ; return the value of a constant expression
65 (if (pair? expr) ; then it must be a quoted constant
66 (cadr expr)
67 expr))
68
69 (define (quot val) ; make a quoted constant whose value is 'val'
70 (list 'quote val))
71
72 (define (new-variables parms env)
73 (append (map (lambda (x) (cons x (new-variable x))) parms) env))
74
75 (define *current-num* 0)
76
77 (define (new-variable name)
78 (set! *current-num* (+ *current-num* 1))
79 (string->symbol
80 (string-append (symbol->string name)
81 "_"
82 (number->string *current-num*))))
83
84 ;------------------------------------------------------------------------------
85 ;
86 ; (peval proc args) will transform a procedure that is known to be called
87 ; with constants as some of its arguments into a specialized procedure that
88 ; is 'equivalent' but accepts only the non-constant parameters. 'proc' is the
89 ; list representation of a lambda-expression and 'args' is a list of values,
90 ; one for each parameter of the lambda-expression. A special value (i.e.
91 ; 'not-constant') is used to indicate an argument that is not a constant.
92 ; The returned procedure is one that has as parameters the parameters of the
93 ; original procedure which are NOT passed constants. Constants will have been
94 ; substituted for the constant parameters that are referenced in the body
95 ; of the procedure.
96 ;
97 ; For example:
98 ;
99 ; (peval
100 ; '(lambda (x y z) (f z x y)) ; the procedure
101 ; (list 1 not-constant #t)) ; the knowledge about x, y and z
102 ;
103 ; will return: (lambda (y) (f '#t '1 y))
104
105 (define (peval proc args)
106 (simplify!
107 (let ((parms (cadr proc)) ; get the parameter list
108 (body (caddr proc))) ; get the body of the procedure
109 (list 'lambda
110 (remove-constant parms args) ; remove the constant parameters
111 (beta-subst ; in the body, replace variable refs to the constant
112 body ; parameters by the corresponding constant
113 (map2 (lambda (x y) (if (not-constant? y) '(()) (cons x (quot y))))
114 parms
115 args))))))
116
117 (define not-constant (list '?)) ; special value indicating non-constant parms.
118
119 (define (not-constant? x) (eq? x not-constant))
120
121 (define (remove-constant l a) ; remove from list 'l' all elements whose
122 (cond ((null? l) ; corresponding element in 'a' is a constant
123 '())
124 ((not-constant? (car a))
125 (cons (car l) (remove-constant (cdr l) (cdr a))))
126 (else
127 (remove-constant (cdr l) (cdr a)))))
128
129 (define (extract-constant l a) ; extract from list 'l' all elements whose
130 (cond ((null? l) ; corresponding element in 'a' is a constant
131 '())
132 ((not-constant? (car a))
133 (extract-constant (cdr l) (cdr a)))
134 (else
135 (cons (car l) (extract-constant (cdr l) (cdr a))))))
136
137 (define (beta-subst exp env) ; return a modified 'exp' where each var named in
138 (define (bs exp) ; 'env' is replaced by the corresponding expr (it
139 (cond ((const-expr? exp) ; is assumed that the code has been alphatized)
140 (quot (const-value exp)))
141 ((symbol? exp)
142 (let ((x (assq exp env)))
143 (if x (cdr x) exp)))
144 ((or (eq? (car exp) 'if) (eq? (car exp) 'begin))
145 (cons (car exp) (map bs (cdr exp))))
146 ((or (eq? (car exp) 'let) (eq? (car exp) 'letrec))
147 (list (car exp)
148 (map (lambda (x) (list (car x) (bs (cadr x)))) (cadr exp))
149 (bs (caddr exp))))
150 ((eq? (car exp) 'lambda)
151 (list 'lambda
152 (cadr exp)
153 (bs (caddr exp))))
154 (else
155 (map bs exp))))
156 (bs exp))
157
158 ;------------------------------------------------------------------------------
159 ;
160 ; The expression simplifier.
161
162 (define (simplify! exp) ; simplify the expression 'exp' destructively (it
163 ; is assumed that the code has been alphatized)
164 (define (simp! where env)
165
166 (define (s! where)
167 (let ((exp (car where)))
168
169 (cond ((const-expr? exp)) ; leave constants the way they are
170
171 ((symbol? exp)) ; leave variable references the way they are
172
173 ((eq? (car exp) 'if) ; dead code removal for conditionals
174 (s! (cdr exp)) ; simplify the predicate
175 (if (const-expr? (cadr exp)) ; is the predicate a constant?
176 (begin
177 (set-car! where
178 (if (memq (const-value (cadr exp)) '(#f ())) ; false?
179 (if (= (length exp) 3) ''() (cadddr exp))
180 (caddr exp)))
181 (s! where))
182 (for-each! s! (cddr exp)))) ; simplify consequent and alt.
183
184 ((eq? (car exp) 'begin)
185 (for-each! s! (cdr exp))
186 (let loop ((exps exp)) ; remove all useless expressions
187 (if (not (null? (cddr exps))) ; not last expression?
188 (let ((x (cadr exps)))
189 (loop (if (or (const-expr? x)
190 (symbol? x)
191 (and (pair? x) (eq? (car x) 'lambda)))
192 (begin (set-cdr! exps (cddr exps)) exps)
193 (cdr exps))))))
194 (if (null? (cddr exp)) ; only one expression in the begin?
195 (set-car! where (cadr exp))))
196
197 ((or (eq? (car exp) 'let) (eq? (car exp) 'letrec))
198 (let ((new-env (cons exp env)))
199 (define (keep i)
200 (if (>= i (length (cadar where)))
201 '()
202 (let* ((var (car (list-ref (cadar where) i)))
203 (val (cadr (assq var (cadar where))))
204 (refs (ref-count (car where) var))
205 (self-refs (ref-count val var))
206 (total-refs (- (car refs) (car self-refs)))
207 (oper-refs (- (cadr refs) (cadr self-refs))))
208 (cond ((= total-refs 0)
209 (keep (+ i 1)))
210 ((or (const-expr? val)
211 (symbol? val)
212 (and (pair? val)
213 (eq? (car val) 'lambda)
214 (= total-refs 1)
215 (= oper-refs 1)
216 (= (car self-refs) 0))
217 (and (caddr refs)
218 (= total-refs 1)))
219 (set-car! where
220 (beta-subst (car where)
221 (list (cons var val))))
222 (keep (+ i 1)))
223 (else
224 (cons var (keep (+ i 1))))))))
225 (simp! (cddr exp) new-env)
226 (for-each! (lambda (x) (simp! (cdar x) new-env)) (cadr exp))
227 (let ((to-keep (keep 0)))
228 (if (< (length to-keep) (length (cadar where)))
229 (begin
230 (if (null? to-keep)
231 (set-car! where (caddar where))
232 (set-car! (cdar where)
233 (map (lambda (v) (assq v (cadar where))) to-keep)))
234 (s! where))
235 (if (null? to-keep)
236 (set-car! where (caddar where)))))))
237
238 ((eq? (car exp) 'lambda)
239 (simp! (cddr exp) (cons exp env)))
240
241 (else
242 (for-each! s! exp)
243 (cond ((symbol? (car exp)) ; is the operator position a var ref?
244 (let ((frame (binding-frame (car exp) env)))
245 (if frame ; is it a bound variable?
246 (let ((proc (bound-expr (car exp) frame)))
247 (if (and (pair? proc)
248 (eq? (car proc) 'lambda)
249 (some? const-expr? (cdr exp)))
250 (let* ((args (arg-pattern (cdr exp)))
251 (new-proc (peval proc args))
252 (new-args (remove-constant (cdr exp) args)))
253 (set-car! where
254 (cons (add-binding new-proc frame (car exp))
255 new-args)))))
256 (set-car! where
257 (constant-fold-global (car exp) (cdr exp))))))
258 ((not (pair? (car exp))))
259 ((eq? (caar exp) 'lambda)
260 (set-car! where
261 (list 'let
262 (map2 list (cadar exp) (cdr exp))
263 (caddar exp)))
264 (s! where)))))))
265
266 (s! where))
267
268 (define (remove-empty-calls! where env)
269
270 (define (rec! where)
271 (let ((exp (car where)))
272
273 (cond ((const-expr? exp))
274 ((symbol? exp))
275 ((eq? (car exp) 'if)
276 (rec! (cdr exp))
277 (rec! (cddr exp))
278 (rec! (cdddr exp)))
279 ((eq? (car exp) 'begin)
280 (for-each! rec! (cdr exp)))
281 ((or (eq? (car exp) 'let) (eq? (car exp) 'letrec))
282 (let ((new-env (cons exp env)))
283 (remove-empty-calls! (cddr exp) new-env)
284 (for-each! (lambda (x) (remove-empty-calls! (cdar x) new-env))
285 (cadr exp))))
286 ((eq? (car exp) 'lambda)
287 (rec! (cddr exp)))
288 (else
289 (for-each! rec! (cdr exp))
290 (if (and (null? (cdr exp)) (symbol? (car exp)))
291 (let ((frame (binding-frame (car exp) env)))
292 (if frame ; is it a bound variable?
293 (let ((proc (bound-expr (car exp) frame)))
294 (if (and (pair? proc)
295 (eq? (car proc) 'lambda))
296 (begin
297 (set! changed? #t)
298 (set-car! where (caddr proc))))))))))))
299
300 (rec! where))
301
302 (define changed? #f)
303
304 (let ((x (list exp)))
305 (let loop ()
306 (set! changed? #f)
307 (simp! x '())
308 (remove-empty-calls! x '())
309 (if changed? (loop) (car x)))))
310
311 (define (ref-count exp var) ; compute how many references to variable 'var'
312 (let ((total 0) ; are contained in 'exp'
313 (oper 0)
314 (always-evaled #t))
315 (define (rc exp ae)
316 (cond ((const-expr? exp))
317 ((symbol? exp)
318 (if (eq? exp var)
319 (begin
320 (set! total (+ total 1))
321 (set! always-evaled (and ae always-evaled)))))
322 ((eq? (car exp) 'if)
323 (rc (cadr exp) ae)
324 (for-each (lambda (x) (rc x #f)) (cddr exp)))
325 ((eq? (car exp) 'begin)
326 (for-each (lambda (x) (rc x ae)) (cdr exp)))
327 ((or (eq? (car exp) 'let) (eq? (car exp) 'letrec))
328 (for-each (lambda (x) (rc (cadr x) ae)) (cadr exp))
329 (rc (caddr exp) ae))
330 ((eq? (car exp) 'lambda)
331 (rc (caddr exp) #f))
332 (else
333 (for-each (lambda (x) (rc x ae)) exp)
334 (if (symbol? (car exp))
335 (if (eq? (car exp) var) (set! oper (+ oper 1)))))))
336 (rc exp #t)
337 (list total oper always-evaled)))
338
339 (define (binding-frame var env)
340 (cond ((null? env) #f)
341 ((or (eq? (caar env) 'let) (eq? (caar env) 'letrec))
342 (if (assq var (cadar env)) (car env) (binding-frame var (cdr env))))
343 ((eq? (caar env) 'lambda)
344 (if (memq var (cadar env)) (car env) (binding-frame var (cdr env))))
345 (else
346 (fatal-error "ill-formed environment"))))
347
348 (define (bound-expr var frame)
349 (cond ((or (eq? (car frame) 'let) (eq? (car frame) 'letrec))
350 (cadr (assq var (cadr frame))))
351 ((eq? (car frame) 'lambda)
352 not-constant)
353 (else
354 (fatal-error "ill-formed frame"))))
355
356 (define (add-binding val frame name)
357 (define (find-val val bindings)
358 (cond ((null? bindings) #f)
359 ((equal? val (cadar bindings)) ; *kludge* equal? is not exactly what
360 (caar bindings)) ; we want...
361 (else
362 (find-val val (cdr bindings)))))
363 (or (find-val val (cadr frame))
364 (let ((var (new-variable name)))
365 (set-cdr! (get-last-pair (cadr frame)) (list (list var val)))
366 var)))
367
368 (define (for-each! proc! l) ; call proc! on each CONS CELL in the list 'l'
369 (if (not (null? l))
370 (begin (proc! l) (for-each! proc! (cdr l)))))
371
372 (define (arg-pattern exps) ; return the argument pattern (i.e. the list of
373 (if (null? exps) ; constants in 'exps' but with the not-constant
374 '() ; value wherever the corresponding expression in
375 (cons (if (const-expr? (car exps)) ; 'exps' is not a constant)
376 (const-value (car exps))
377 not-constant)
378 (arg-pattern (cdr exps)))))
379
380 ;------------------------------------------------------------------------------
381 ;
382 ; Knowledge about primitive procedures.
383
384 (define *primitives*
385 (list
386 (cons 'car (lambda (args)
387 (and (= (length args) 1)
388 (pair? (car args))
389 (quot (car (car args))))))
390 (cons 'cdr (lambda (args)
391 (and (= (length args) 1)
392 (pair? (car args))
393 (quot (cdr (car args))))))
394 (cons '+ (lambda (args)
395 (and (every? number? args)
396 (quot (sum args 0)))))
397 (cons '* (lambda (args)
398 (and (every? number? args)
399 (quot (product args 1)))))
400 (cons '- (lambda (args)
401 (and (> (length args) 0)
402 (every? number? args)
403 (quot (if (null? (cdr args))
404 (- (car args))
405 (- (car args) (sum (cdr args) 0)))))))
406 (cons '/ (lambda (args)
407 (and (> (length args) 1)
408 (every? number? args)
409 (quot (if (null? (cdr args))
410 (/ (car args))
411 (/ (car args) (product (cdr args) 1)))))))
412 (cons '< (lambda (args)
413 (and (= (length args) 2)
414 (every? number? args)
415 (quot (< (car args) (cadr args))))))
416 (cons '= (lambda (args)
417 (and (= (length args) 2)
418 (every? number? args)
419 (quot (= (car args) (cadr args))))))
420 (cons '> (lambda (args)
421 (and (= (length args) 2)
422 (every? number? args)
423 (quot (> (car args) (cadr args))))))
424 (cons 'eq? (lambda (args)
425 (and (= (length args) 2)
426 (quot (eq? (car args) (cadr args))))))
427 (cons 'not (lambda (args)
428 (and (= (length args) 1)
429 (quot (not (car args))))))
430 (cons 'null? (lambda (args)
431 (and (= (length args) 1)
432 (quot (null? (car args))))))
433 (cons 'pair? (lambda (args)
434 (and (= (length args) 1)
435 (quot (pair? (car args))))))
436 (cons 'symbol? (lambda (args)
437 (and (= (length args) 1)
438 (quot (symbol? (car args))))))
439 )
440 )
441
442 (define (sum lst n)
443 (if (null? lst)
444 n
445 (sum (cdr lst) (+ n (car lst)))))
446
447 (define (product lst n)
448 (if (null? lst)
449 n
450 (product (cdr lst) (* n (car lst)))))
451
452 (define (reduce-global name args)
453 (let ((x (assq name *primitives*)))
454 (and x ((cdr x) args))))
455
456 (define (constant-fold-global name exprs)
457
458 (define (flatten args op)
459 (cond ((null? args)
460 '())
461 ((and (pair? (car args)) (eq? (caar args) op))
462 (append (flatten (cdar args) op) (flatten (cdr args) op)))
463 (else
464 (cons (car args) (flatten (cdr args) op)))))
465
466 (let ((args (if (or (eq? name '+) (eq? name '*)) ; associative ops
467 (flatten exprs name)
468 exprs)))
469 (or (and (every? const-expr? args)
470 (reduce-global name (map const-value args)))
471 (let ((pattern (arg-pattern args)))
472 (let ((non-const (remove-constant args pattern))
473 (const (map const-value (extract-constant args pattern))))
474 (cond ((eq? name '+) ; + is commutative
475 (let ((x (reduce-global '+ const)))
476 (if x
477 (let ((y (const-value x)))
478 (cons '+
479 (if (= y 0) non-const (cons x non-const))))
480 (cons name args))))
481 ((eq? name '*) ; * is commutative
482 (let ((x (reduce-global '* const)))
483 (if x
484 (let ((y (const-value x)))
485 (cons '*
486 (if (= y 1) non-const (cons x non-const))))
487 (cons name args))))
488 ((eq? name 'cons)
489 (cond ((and (const-expr? (cadr args))
490 (null? (const-value (cadr args))))
491 (list 'list (car args)))
492 ((and (pair? (cadr args))
493 (eq? (car (cadr args)) 'list))
494 (cons 'list (cons (car args) (cdr (cadr args)))))
495 (else
496 (cons name args))))
497 (else
498 (cons name args))))))))
499
500 ;------------------------------------------------------------------------------
501 ;
502 ; Examples:
503
504 (define (try-peval proc args)
505 (partial-evaluate proc args))
506
507 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
508
509 (define example1
510 '(lambda (a b c)
511 (if (null? a) b (+ (car a) c))))
512
513 ;(try-peval example1 (list '(10 11) not-constant '1))
514
515 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
516
517 (define example2
518 '(lambda (x y)
519 (let ((q (lambda (a b) (if (< a 0) b (- 10 b)))))
520 (if (< x 0) (q (- y) (- x)) (q y x)))))
521
522 ;(try-peval example2 (list not-constant '1))
523
524 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
525
526 (define example3
527 '(lambda (l n)
528 (letrec ((add-list
529 (lambda (l n)
530 (if (null? l)
531 '()
532 (cons (+ (car l) n) (add-list (cdr l) n))))))
533 (add-list l n))))
534
535 ;(try-peval example3 (list not-constant '1))
536
537 ;(try-peval example3 (list '(1 2 3) not-constant))
538
539 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
540
541 (define example4
542 '(lambda (exp env)
543 (letrec ((eval
544 (lambda (exp env)
545 (letrec ((eval-list
546 (lambda (l env)
547 (if (null? l)
548 '()
549 (cons (eval (car l) env)
550 (eval-list (cdr l) env))))))
551 (if (symbol? exp) (lookup exp env)
552 (if (not (pair? exp)) exp
553 (if (eq? (car exp) 'quote) (car (cdr exp))
554 (apply (eval (car exp) env)
555 (eval-list (cdr exp) env)))))))))
556 (eval exp env))))
557
558 ;(try-peval example4 (list 'x not-constant))
559
560 ;(try-peval example4 (list '(f 1 2 3) not-constant))
561
562 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
563
564 (define example5
565 '(lambda (a b)
566 (letrec ((funct
567 (lambda (x)
568 (+ x b (if (< x 1) 0 (funct (- x 1)))))))
569 (funct a))))
570
571 ;(try-peval example5 (list '5 not-constant))
572
573 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
574
575 (define example6
576 '(lambda ()
577 (letrec ((fib
578 (lambda (x)
579 (if (< x 2) x (+ (fib (- x 1)) (fib (- x 2)))))))
580 (fib 10))))
581
582 ;(try-peval example6 '())
583
584 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
585
586 (define example7
587 '(lambda (input)
588 (letrec ((copy (lambda (in)
589 (if (pair? in)
590 (cons (copy (car in))
591 (copy (cdr in)))
592 in))))
593 (copy input))))
594
595 ;(try-peval example7 (list '(a b c d e f g h i j k l m n o p q r s t u v w x y z)))
596
597 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
598
599 (define example8
600 '(lambda (input)
601 (letrec ((reverse (lambda (in result)
602 (if (pair? in)
603 (reverse (cdr in) (cons (car in) result))
604 result))))
605 (reverse input '()))))
606
607 ;(try-peval example8 (list '(a b c d e f g h i j k l m n o p q r s t u v w x y z)))
608
609 ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
610
611 (define (test)
612 (set! *current-num* 0)
613 (list (try-peval example1 (list '(10 11) not-constant '1))
614 (try-peval example2 (list not-constant '1))
615 (try-peval example3 (list not-constant '1))
616 (try-peval example3 (list '(1 2 3) not-constant))
617 (try-peval example4 (list 'x not-constant))
618 (try-peval example4 (list '(f 1 2 3) not-constant))
619 (try-peval example5 (list '5 not-constant))
620 (try-peval example6 '())
621 (try-peval
622 example7
623 (list '(a b c d e f g h i j k l m n o p q r s t u v w x y z)))
624 (try-peval
625 example8
626 (list '(a b c d e f g h i j k l m n o p q r s t u v w x y z)))))
627
628 (define (main . args)
629 (run-benchmark
630 "peval"
631 peval-iters
632 (lambda (result)
633 (and (list? result)
634 (= (length result) 10)
635 (equal? (list-ref result 9)
636 '(lambda ()
637 (list 'z 'y 'x 'w 'v 'u 't 's 'r 'q 'p 'o 'n
638 'm 'l 'k 'j 'i 'h 'g 'f 'e 'd 'c 'b 'a)))))
639 (lambda () (lambda () (test)))))