eval.scm 23 KB

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