primitive support for 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.
222 (define (make-general-closure env body nreq rest? nopt kw inits alt)
223 (lambda args
224 (let lp ((env env)
225 (nreq nreq)
226 (args args))
227 (if (> nreq 0)
228 ;; First, bind required arguments.
229 (if (null? args)
230 (scm-error 'wrong-number-of-args
231 "eval" "Wrong number of arguments"
232 '() #f)
233 (lp (cons (car args) env)
234 (1- nreq)
235 (cdr args)))
236 ;; Move on to optional arguments.
237 (if (not kw)
238 ;; Without keywords, bind optionals from arguments.
239 (let lp ((env env)
240 (nopt nopt)
241 (args args)
242 (inits inits))
243 (if (zero? nopt)
244 (if rest?
245 (eval body (cons args env))
246 (if (null? args)
247 (eval body env)
248 (scm-error 'wrong-number-of-args
249 "eval" "Wrong number of arguments"
250 '() #f)))
251 (if (null? args)
252 (lp (cons (eval (car inits) env) env)
253 (1- nopt) args (cdr inits))
254 (lp (cons (car args) env)
255 (1- nopt) (cdr args) (cdr inits)))))
256 ;; With keywords, we stop binding optionals at the first
257 ;; keyword.
258 (let lp ((env env)
259 (nopt* nopt)
260 (args args)
261 (inits inits))
262 (if (> nopt* 0)
263 (if (or (null? args) (keyword? (car args)))
264 (lp (cons (eval (car inits) env) env)
265 (1- nopt*) args (cdr inits))
266 (lp (cons (car args) env)
267 (1- nopt*) (cdr args) (cdr inits)))
268 ;; Finished with optionals.
269 (let* ((aok (car kw))
270 (kw (cdr kw))
271 (kw-base (+ nopt nreq (if rest? 1 0)))
272 (imax (let lp ((imax (1- kw-base)) (kw kw))
273 (if (null? kw)
274 imax
275 (lp (max (cdar kw) imax)
276 (cdr kw)))))
277 ;; Fill in kwargs with "undefined" vals.
278 (env (let lp ((i kw-base)
279 ;; Also, here we bind the rest
280 ;; arg, if any.
281 (env (if rest? (cons args env) env)))
282 (if (<= i imax)
283 (lp (1+ i) (cons unbound-arg env))
284 env))))
285 ;; Now scan args for keywords.
286 (let lp ((args args))
287 (if (and (pair? args) (pair? (cdr args))
288 (keyword? (car args)))
289 (let ((kw-pair (assq (car args) kw))
290 (v (cadr args)))
291 (if kw-pair
292 ;; Found a known keyword; set its value.
293 (list-set! env (- imax (cdr kw-pair)) v)
294 ;; Unknown keyword.
295 (if (not aok)
296 (scm-error 'keyword-argument-error
297 "eval" "Unrecognized keyword"
298 '() #f)))
299 (lp (cddr args)))
300 (if (pair? args)
301 (if rest?
302 ;; Be lenient parsing rest args.
303 (lp (cdr args))
304 (scm-error 'keyword-argument-error
305 "eval" "Invalid keyword"
306 '() #f))
307 ;; Finished parsing keywords. Fill in
308 ;; uninitialized kwargs by evalling init
309 ;; expressions in their appropriate
310 ;; environment.
311 (let lp ((i (- imax kw-base))
312 (inits inits))
313 (if (pair? inits)
314 (let ((tail (list-tail env i)))
315 (if (eq? (car tail) unbound-arg)
316 (set-car! tail
317 (eval (car inits)
318 (cdr tail))))
319 (lp (1- i) (cdr inits)))
320 ;; Finally, eval the body.
321 (eval body env))))))))))))))
322
323 ;; The "engine". EXP is a memoized expression.
324 (define (eval exp env)
325 (memoized-expression-case exp
326 (('lexical-ref n)
327 (let lp ((n n) (env env))
328 (if (zero? n)
329 (car env)
330 (lp (1- n) (cdr env)))))
331
332 (('call (f nargs . args))
333 (let ((proc (eval f env)))
334 (call eval proc nargs args env)))
335
336 (('toplevel-ref var-or-sym)
337 (variable-ref
338 (if (variable? var-or-sym)
339 var-or-sym
340 (let lp ((env env))
341 (if (pair? env)
342 (lp (cdr env))
343 (memoize-variable-access! exp (capture-env env)))))))
344
345 (('if (test consequent . alternate))
346 (if (eval test env)
347 (eval consequent env)
348 (eval alternate env)))
349
350 (('quote x)
351 x)
352
353 (('let (inits . body))
354 (let lp ((inits inits) (new-env (capture-env env)))
355 (if (null? inits)
356 (eval body new-env)
357 (lp (cdr inits)
358 (cons (eval (car inits) env) new-env)))))
359
360 (('lambda (body nreq . tail))
361 (if (null? tail)
362 (make-fixed-closure eval nreq body (capture-env env))
363 (if (null? (cdr tail))
364 (make-general-closure (capture-env env) body nreq (car tail)
365 0 #f '() #f)
366 (apply make-general-closure (capture-env env) body nreq tail))))
367
368 (('begin (first . rest))
369 (let lp ((first first) (rest rest))
370 (if (null? rest)
371 (eval first env)
372 (begin
373 (eval first env)
374 (lp (car rest) (cdr rest))))))
375
376 (('lexical-set! (n . x))
377 (let ((val (eval x env)))
378 (let lp ((n n) (env env))
379 (if (zero? n)
380 (set-car! env val)
381 (lp (1- n) (cdr env))))))
382
383 (('call-with-values (producer . consumer))
384 (call-with-values (eval producer env)
385 (eval consumer env)))
386
387 (('apply (f args))
388 (apply (eval f env) (eval args env)))
389
390 (('module-ref var-or-spec)
391 (variable-ref
392 (if (variable? var-or-spec)
393 var-or-spec
394 (memoize-variable-access! exp #f))))
395
396 (('define (name . x))
397 (define! name (eval x env)))
398
399 (('toplevel-set! (var-or-sym . x))
400 (variable-set!
401 (if (variable? var-or-sym)
402 var-or-sym
403 (let lp ((env env))
404 (if (pair? env)
405 (lp (cdr env))
406 (memoize-variable-access! exp (capture-env env)))))
407 (eval x env)))
408
409 (('dynwind (in exp . out))
410 (dynamic-wind (eval in env)
411 (lambda () (eval exp env))
412 (eval out env)))
413
414 (('with-fluids (fluids vals . exp))
415 (let* ((fluids (map (lambda (x) (eval x env)) fluids))
416 (vals (map (lambda (x) (eval x env)) vals)))
417 (let lp ((fluids fluids) (vals vals))
418 (if (null? fluids)
419 (eval exp env)
420 (with-fluids (((car fluids) (car vals)))
421 (lp (cdr fluids) (cdr vals)))))))
422
423 (('prompt (tag exp . handler))
424 (@prompt (eval tag env)
425 (eval exp env)
426 (eval handler env)))
427
428 (('call/cc proc)
429 (call/cc (eval proc env)))
430
431 (('module-set! (x . var-or-spec))
432 (variable-set!
433 (if (variable? var-or-spec)
434 var-or-spec
435 (memoize-variable-access! exp #f))
436 (eval x env)))))
437
438 ;; primitive-eval
439 (lambda (exp)
440 "Evaluate @var{exp} in the current module."
441 (eval
442 (if (memoized? exp)
443 exp
444 ((module-transformer (current-module)) exp))
445 '()))))