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