dynamic-wind in terms of wind and unwind; remove <dynwind>, @dynamic-wind
[bpt/guile.git] / module / ice-9 / eval.scm
1 ;;; -*- mode: scheme; coding: utf-8; -*-
2
3 ;;;; Copyright (C) 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ;;;;
19
20 \f
21
22 ;;; Commentary:
23
24 ;;; Scheme eval, written in Scheme.
25 ;;;
26 ;;; Expressions are first expanded, by the syntax expander (i.e.
27 ;;; psyntax), then memoized into internal forms. The evaluator itself
28 ;;; only operates on the internal forms ("memoized expressions").
29 ;;;
30 ;;; Environments are represented as linked lists of the form (VAL ... .
31 ;;; MOD). If MOD is #f, it means the environment was captured before
32 ;;; modules were booted. If MOD is the literal value '(), we are
33 ;;; evaluating at the top level, and so should track changes to the
34 ;;; current module.
35 ;;;
36 ;;; Evaluate this in Emacs to make code indentation work right:
37 ;;;
38 ;;; (put 'memoized-expression-case 'scheme-indent-function 1)
39 ;;;
40
41 ;;; Code:
42
43 \f
44
45 (eval-when (compile)
46 (define-syntax capture-env
47 (syntax-rules ()
48 ((_ (exp ...))
49 (let ((env (exp ...)))
50 (capture-env env)))
51 ((_ env)
52 (if (null? env)
53 (current-module)
54 (if (not env)
55 ;; the and current-module checks that modules are booted,
56 ;; and thus the-root-module is defined
57 (and (current-module) the-root-module)
58 env)))))
59
60 ;; Fast case for procedures with fixed arities.
61 (define-syntax make-fixed-closure
62 (lambda (x)
63 (define *max-static-argument-count* 8)
64 (define (make-formals n)
65 (map (lambda (i)
66 (datum->syntax
67 x
68 (string->symbol
69 (string (integer->char (+ (char->integer #\a) i))))))
70 (iota n)))
71 (syntax-case x ()
72 ((_ eval nreq body env) (not (identifier? #'env))
73 #'(let ((e env))
74 (make-fixed-closure eval nreq body e)))
75 ((_ eval nreq body env)
76 #`(case nreq
77 #,@(map (lambda (nreq)
78 (let ((formals (make-formals nreq)))
79 #`((#,nreq)
80 (lambda (#,@formals)
81 (eval body
82 (cons* #,@(reverse formals) env))))))
83 (iota *max-static-argument-count*))
84 (else
85 #,(let ((formals (make-formals *max-static-argument-count*)))
86 #`(lambda (#,@formals . more)
87 (let lp ((new-env (cons* #,@(reverse formals) env))
88 (nreq (- nreq #,*max-static-argument-count*))
89 (args more))
90 (if (zero? nreq)
91 (eval body
92 (if (null? args)
93 new-env
94 (scm-error 'wrong-number-of-args
95 "eval" "Wrong number of arguments"
96 '() #f)))
97 (if (null? args)
98 (scm-error 'wrong-number-of-args
99 "eval" "Wrong number of arguments"
100 '() #f)
101 (lp (cons (car args) new-env)
102 (1- nreq)
103 (cdr args)))))))))))))
104
105 (define-syntax call
106 (lambda (x)
107 (define *max-static-call-count* 4)
108 (syntax-case x ()
109 ((_ eval proc nargs args env) (identifier? #'env)
110 #`(case nargs
111 #,@(map (lambda (nargs)
112 #`((#,nargs)
113 (proc
114 #,@(map
115 (lambda (n)
116 (let lp ((n n) (args #'args))
117 (if (zero? n)
118 #`(eval (car #,args) env)
119 (lp (1- n) #`(cdr #,args)))))
120 (iota nargs)))))
121 (iota *max-static-call-count*))
122 (else
123 (apply proc
124 #,@(map
125 (lambda (n)
126 (let lp ((n n) (args #'args))
127 (if (zero? n)
128 #`(eval (car #,args) env)
129 (lp (1- n) #`(cdr #,args)))))
130 (iota *max-static-call-count*))
131 (let lp ((exps #,(let lp ((n *max-static-call-count*)
132 (args #'args))
133 (if (zero? n)
134 args
135 (lp (1- n) #`(cdr #,args)))))
136 (args '()))
137 (if (null? exps)
138 (reverse args)
139 (lp (cdr exps)
140 (cons (eval (car exps) env) args)))))))))))
141
142 ;; This macro could be more straightforward if the compiler had better
143 ;; copy propagation. As it is we do some copy propagation by hand.
144 (define-syntax mx-bind
145 (lambda (x)
146 (syntax-case x ()
147 ((_ data () body)
148 #'body)
149 ((_ data (a . b) body) (and (identifier? #'a) (identifier? #'b))
150 #'(let ((a (car data))
151 (b (cdr data)))
152 body))
153 ((_ data (a . b) body) (identifier? #'a)
154 #'(let ((a (car data))
155 (xb (cdr data)))
156 (mx-bind xb b body)))
157 ((_ data (a . b) body)
158 #'(let ((xa (car data))
159 (xb (cdr data)))
160 (mx-bind xa a (mx-bind xb b body))))
161 ((_ data v body) (identifier? #'v)
162 #'(let ((v data))
163 body)))))
164
165 ;; The resulting nested if statements will be an O(n) dispatch. Once
166 ;; we compile `case' effectively, this situation will improve.
167 (define-syntax mx-match
168 (lambda (x)
169 (syntax-case x (quote)
170 ((_ mx data tag)
171 #'(error "what" mx))
172 ((_ mx data tag (('type pat) body) c* ...)
173 #`(if (eqv? tag #,(or (memoized-typecode (syntax->datum #'type))
174 (error "not a typecode" #'type)))
175 (mx-bind data pat body)
176 (mx-match mx data tag c* ...))))))
177
178 (define-syntax memoized-expression-case
179 (lambda (x)
180 (syntax-case x ()
181 ((_ mx c ...)
182 #'(let ((tag (memoized-expression-typecode mx))
183 (data (memoized-expression-data mx)))
184 (mx-match mx data tag c ...)))))))
185
186
187 ;;;
188 ;;; On 18 Feb 2010, I did a profile of how often the various memoized expression
189 ;;; types occur when getting to a prompt on a fresh build. Here are the numbers
190 ;;; I got:
191 ;;;
192 ;;; lexical-ref: 32933054
193 ;;; call: 20281547
194 ;;; toplevel-ref: 13228724
195 ;;; if: 9156156
196 ;;; quote: 6610137
197 ;;; let: 2619707
198 ;;; lambda: 1010921
199 ;;; begin: 948945
200 ;;; lexical-set: 509862
201 ;;; call-with-values: 139668
202 ;;; apply: 49402
203 ;;; module-ref: 14468
204 ;;; define: 1259
205 ;;; toplevel-set: 328
206 ;;; with-fluids: 0
207 ;;; call/cc: 0
208 ;;; module-set: 0
209 ;;;
210 ;;; So until we compile `case' into a computed goto, we'll order the clauses in
211 ;;; `eval' in this order, to put the most frequent cases first.
212 ;;;
213
214 (define primitive-eval
215 (let ()
216 ;; We pre-generate procedures with fixed arities, up to some number of
217 ;; arguments; see make-fixed-closure above.
218
219 ;; A unique marker for unbound keywords.
220 (define unbound-arg (list 'unbound-arg))
221
222 ;; Procedures with rest, optional, or keyword arguments, potentially with
223 ;; multiple arities, as with case-lambda.
224 (define (make-general-closure env body nreq rest? nopt kw inits alt)
225 (define alt-proc
226 (and alt ; (body docstring nreq ...)
227 (let* ((body (car alt))
228 (spec (cddr alt))
229 (nreq (car spec))
230 (rest (if (null? (cdr spec)) #f (cadr spec)))
231 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
232 (nopt (if tail (car tail) 0))
233 (kw (and tail (cadr tail)))
234 (inits (if tail (caddr tail) '()))
235 (alt (and tail (cadddr tail))))
236 (make-general-closure env body nreq rest nopt kw inits alt))))
237 (define (set-procedure-arity! proc)
238 (let lp ((alt alt) (nreq nreq) (nopt nopt) (rest? rest?))
239 (if (not alt)
240 (begin
241 (set-procedure-property! proc 'arglist
242 (list nreq
243 nopt
244 (if kw (cdr kw) '())
245 (and kw (car kw))
246 (and rest? '_)))
247 (set-procedure-minimum-arity! proc nreq nopt rest?))
248 (let* ((spec (cddr alt))
249 (nreq* (car spec))
250 (rest?* (if (null? (cdr spec)) #f (cadr spec)))
251 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
252 (nopt* (if tail (car tail) 0))
253 (alt* (and tail (cadddr tail))))
254 (if (or (< nreq* nreq)
255 (and (= nreq* nreq)
256 (if rest?
257 (and rest?* (> nopt* nopt))
258 (or rest?* (> nopt* nopt)))))
259 (lp alt* nreq* nopt* rest?*)
260 (lp alt* nreq nopt rest?)))))
261 proc)
262 (set-procedure-arity!
263 (lambda %args
264 (let lp ((env env)
265 (nreq* nreq)
266 (args %args))
267 (if (> nreq* 0)
268 ;; First, bind required arguments.
269 (if (null? args)
270 (if alt
271 (apply alt-proc %args)
272 (scm-error 'wrong-number-of-args
273 "eval" "Wrong number of arguments"
274 '() #f))
275 (lp (cons (car args) env)
276 (1- nreq*)
277 (cdr args)))
278 ;; Move on to optional arguments.
279 (if (not kw)
280 ;; Without keywords, bind optionals from arguments.
281 (let lp ((env env)
282 (nopt nopt)
283 (args args)
284 (inits inits))
285 (if (zero? nopt)
286 (if rest?
287 (eval body (cons args env))
288 (if (null? args)
289 (eval body env)
290 (if alt
291 (apply alt-proc %args)
292 (scm-error 'wrong-number-of-args
293 "eval" "Wrong number of arguments"
294 '() #f))))
295 (if (null? args)
296 (lp (cons (eval (car inits) env) env)
297 (1- nopt) args (cdr inits))
298 (lp (cons (car args) env)
299 (1- nopt) (cdr args) (cdr inits)))))
300 (let lp ((env env)
301 (nopt* nopt)
302 (args args)
303 (inits inits))
304 (cond
305 ;; With keywords, we stop binding optionals at the
306 ;; first keyword.
307 ((> nopt* 0)
308 (if (or (null? args) (keyword? (car args)))
309 (lp (cons (eval (car inits) env) env)
310 (1- nopt*) args (cdr inits))
311 (lp (cons (car args) env)
312 (1- nopt*) (cdr args) (cdr inits))))
313 ;; Finished with optionals.
314 ((and alt (pair? args) (not (keyword? (car args)))
315 (not rest?))
316 ;; Too many positional args, no #:rest arg,
317 ;; and we have an alternate.
318 (apply alt-proc %args))
319 (else
320 (let* ((aok (car kw))
321 (kw (cdr kw))
322 (kw-base (+ nopt nreq (if rest? 1 0)))
323 (imax (let lp ((imax (1- kw-base)) (kw kw))
324 (if (null? kw)
325 imax
326 (lp (max (cdar kw) imax)
327 (cdr kw)))))
328 ;; Fill in kwargs with "undefined" vals.
329 (env (let lp ((i kw-base)
330 ;; Also, here we bind the rest
331 ;; arg, if any.
332 (env (if rest?
333 (cons args env)
334 env)))
335 (if (<= i imax)
336 (lp (1+ i) (cons unbound-arg env))
337 env))))
338 ;; Now scan args for keywords.
339 (let lp ((args args))
340 (if (and (pair? args) (pair? (cdr args))
341 (keyword? (car args)))
342 (let ((kw-pair (assq (car args) kw))
343 (v (cadr args)))
344 (if kw-pair
345 ;; Found a known keyword; set its value.
346 (list-set! env
347 (- imax (cdr kw-pair)) v)
348 ;; Unknown keyword.
349 (if (not aok)
350 (scm-error
351 'keyword-argument-error
352 "eval" "Unrecognized keyword"
353 '() #f)))
354 (lp (cddr args)))
355 (if (pair? args)
356 (if rest?
357 ;; Be lenient parsing rest args.
358 (lp (cdr args))
359 (scm-error 'keyword-argument-error
360 "eval" "Invalid keyword"
361 '() #f))
362 ;; Finished parsing keywords. Fill in
363 ;; uninitialized kwargs by evalling init
364 ;; expressions in their appropriate
365 ;; environment.
366 (let lp ((i (- imax kw-base))
367 (inits inits))
368 (if (pair? inits)
369 (let ((tail (list-tail env i)))
370 (if (eq? (car tail) unbound-arg)
371 (set-car! tail
372 (eval (car inits)
373 (cdr tail))))
374 (lp (1- i) (cdr inits)))
375 ;; Finally, eval the body.
376 (eval body env))))))))))))))))
377
378 ;; The "engine". EXP is a memoized expression.
379 (define (eval exp env)
380 (memoized-expression-case exp
381 (('lexical-ref n)
382 (list-ref env n))
383
384 (('call (f nargs . args))
385 (let ((proc (eval f env)))
386 (call eval proc nargs args env)))
387
388 (('toplevel-ref var-or-sym)
389 (variable-ref
390 (if (variable? var-or-sym)
391 var-or-sym
392 (memoize-variable-access! exp
393 (capture-env (if (pair? env)
394 (cdr (last-pair env))
395 env))))))
396
397 (('if (test consequent . alternate))
398 (if (eval test env)
399 (eval consequent env)
400 (eval alternate env)))
401
402 (('quote x)
403 x)
404
405 (('let (inits . body))
406 (let lp ((inits inits) (new-env (capture-env env)))
407 (if (null? inits)
408 (eval body new-env)
409 (lp (cdr inits)
410 (cons (eval (car inits) env) new-env)))))
411
412 (('lambda (body docstring nreq . tail))
413 (let ((proc
414 (if (null? tail)
415 (make-fixed-closure eval nreq body (capture-env env))
416 (if (null? (cdr tail))
417 (make-general-closure (capture-env env) body
418 nreq (car tail)
419 0 #f '() #f)
420 (apply make-general-closure (capture-env env)
421 body nreq tail)))))
422 (when docstring
423 (set-procedure-property! proc 'documentation docstring))
424 proc))
425
426 (('seq (head . tail))
427 (begin
428 (eval head env)
429 (eval tail env)))
430
431 (('lexical-set! (n . x))
432 (let ((val (eval x env)))
433 (list-set! env n val)))
434
435 (('call-with-values (producer . consumer))
436 (call-with-values (eval producer env)
437 (eval consumer env)))
438
439 (('apply (f args))
440 (apply (eval f env) (eval args env)))
441
442 (('module-ref var-or-spec)
443 (variable-ref
444 (if (variable? var-or-spec)
445 var-or-spec
446 (memoize-variable-access! exp #f))))
447
448 (('define (name . x))
449 (let ((x (eval x env)))
450 (if (and (procedure? x) (not (procedure-property x 'name)))
451 (set-procedure-property! x 'name name))
452 (define! name x)
453 (if #f #f)))
454
455 (('toplevel-set! (var-or-sym . x))
456 (variable-set!
457 (if (variable? var-or-sym)
458 var-or-sym
459 (memoize-variable-access! exp
460 (capture-env (if (pair? env)
461 (cdr (last-pair env))
462 env))))
463 (eval x env)))
464
465 (('with-fluids (fluids vals . exp))
466 (let* ((fluids (map (lambda (x) (eval x env)) fluids))
467 (vals (map (lambda (x) (eval x env)) vals)))
468 (let lp ((fluids fluids) (vals vals))
469 (if (null? fluids)
470 (eval exp env)
471 (with-fluids (((car fluids) (car vals)))
472 (lp (cdr fluids) (cdr vals)))))))
473
474 (('call-with-prompt (tag thunk . handler))
475 (call-with-prompt
476 (eval tag env)
477 (eval thunk env)
478 (eval handler env)))
479
480 (('call/cc proc)
481 (call/cc (eval proc env)))
482
483 (('module-set! (x . var-or-spec))
484 (variable-set!
485 (if (variable? var-or-spec)
486 var-or-spec
487 (memoize-variable-access! exp #f))
488 (eval x env)))))
489
490 ;; primitive-eval
491 (lambda (exp)
492 "Evaluate @var{exp} in the current module."
493 (eval
494 (memoize-expression
495 (if (macroexpanded? exp)
496 exp
497 ((module-transformer (current-module)) exp)))
498 '()))))