Debian Codegraph @

benchmarks/rnrs-benchmarks/peval.ss

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