6666f80b4c87a9bda2ecccaeffa7f50da068e3fa
[bpt/guile.git] / module / ice-9 / boot-9.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2009
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 2.1 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 ;;; This file is the first thing loaded into Guile. It adds many mundane
26 ;;; definitions and a few that are interesting.
27 ;;;
28 ;;; The module system (hence the hierarchical namespace) are defined in this
29 ;;; file.
30 ;;;
31
32 ;;; Code:
33
34 \f
35
36 ;; Before compiling, make sure any symbols are resolved in the (guile)
37 ;; module, the primary location of those symbols, rather than in
38 ;; (guile-user), the default module that we compile in.
39
40 (eval-when (compile)
41 (set-current-module (resolve-module '(guile))))
42
43 ;;; {R4RS compliance}
44 ;;;
45
46 (primitive-load-path "ice-9/r4rs")
47
48 \f
49
50 ;;; {Simple Debugging Tools}
51 ;;;
52
53 ;; peek takes any number of arguments, writes them to the
54 ;; current ouput port, and returns the last argument.
55 ;; It is handy to wrap around an expression to look at
56 ;; a value each time is evaluated, e.g.:
57 ;;
58 ;; (+ 10 (troublesome-fn))
59 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
60 ;;
61
62 (define (peek . stuff)
63 (newline)
64 (display ";;; ")
65 (write stuff)
66 (newline)
67 (car (last-pair stuff)))
68
69 (define pk peek)
70
71 (define (warn . stuff)
72 (with-output-to-port (current-error-port)
73 (lambda ()
74 (newline)
75 (display ";;; WARNING ")
76 (display stuff)
77 (newline)
78 (car (last-pair stuff)))))
79
80 \f
81
82 ;;; {Features}
83 ;;;
84
85 (define (provide sym)
86 (if (not (memq sym *features*))
87 (set! *features* (cons sym *features*))))
88
89 ;; Return #t iff FEATURE is available to this Guile interpreter. In SLIB,
90 ;; provided? also checks to see if the module is available. We should do that
91 ;; too, but don't.
92
93 (define (provided? feature)
94 (and (memq feature *features*) #t))
95
96 \f
97
98 ;;; {and-map and or-map}
99 ;;;
100 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
101 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
102 ;;;
103
104 ;; and-map f l
105 ;;
106 ;; Apply f to successive elements of l until exhaustion or f returns #f.
107 ;; If returning early, return #f. Otherwise, return the last value returned
108 ;; by f. If f has never been called because l is empty, return #t.
109 ;;
110 (define (and-map f lst)
111 (let loop ((result #t)
112 (l lst))
113 (and result
114 (or (and (null? l)
115 result)
116 (loop (f (car l)) (cdr l))))))
117
118 ;; or-map f l
119 ;;
120 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
121 ;; If returning early, return the return value of f.
122 ;;
123 (define (or-map f lst)
124 (let loop ((result #f)
125 (l lst))
126 (or result
127 (and (not (null? l))
128 (loop (f (car l)) (cdr l))))))
129
130 \f
131
132 ;; let format alias simple-format until the more complete version is loaded
133
134 (define format simple-format)
135
136 ;; this is scheme wrapping the C code so the final pred call is a tail call,
137 ;; per SRFI-13 spec
138 (define (string-any char_pred s . rest)
139 (let ((start (if (null? rest)
140 0 (car rest)))
141 (end (if (or (null? rest) (null? (cdr rest)))
142 (string-length s) (cadr rest))))
143 (if (and (procedure? char_pred)
144 (> end start)
145 (<= end (string-length s))) ;; let c-code handle range error
146 (or (string-any-c-code char_pred s start (1- end))
147 (char_pred (string-ref s (1- end))))
148 (string-any-c-code char_pred s start end))))
149
150 ;; this is scheme wrapping the C code so the final pred call is a tail call,
151 ;; per SRFI-13 spec
152 (define (string-every char_pred s . rest)
153 (let ((start (if (null? rest)
154 0 (car rest)))
155 (end (if (or (null? rest) (null? (cdr rest)))
156 (string-length s) (cadr rest))))
157 (if (and (procedure? char_pred)
158 (> end start)
159 (<= end (string-length s))) ;; let c-code handle range error
160 (and (string-every-c-code char_pred s start (1- end))
161 (char_pred (string-ref s (1- end))))
162 (string-every-c-code char_pred s start end))))
163
164 ;; A variant of string-fill! that we keep for compatability
165 ;;
166 (define (substring-fill! str start end fill)
167 (string-fill! str fill start end))
168
169 \f
170
171 ;; Define a minimal stub of the module API for psyntax, before modules
172 ;; have booted.
173 (define (module-name x)
174 '(guile))
175 (define (module-define! module sym val)
176 (let ((v (hashq-ref (%get-pre-modules-obarray) sym)))
177 (if v
178 (variable-set! v val)
179 (hashq-set! (%get-pre-modules-obarray) sym
180 (make-variable val)))))
181 (define (module-ref module sym)
182 (let ((v (module-variable module sym)))
183 (if v (variable-ref v) (error "badness!" (pk module) (pk sym)))))
184 (define (resolve-module . args)
185 #f)
186
187 ;; Input hook to syncase -- so that we might be able to pass annotated
188 ;; expressions in. Currently disabled. Maybe we should just use
189 ;; source-properties directly.
190 (define (annotation? x) #f)
191
192 ;; API provided by psyntax
193 (define syntax-violation #f)
194 (define datum->syntax #f)
195 (define syntax->datum #f)
196 (define identifier? #f)
197 (define generate-temporaries #f)
198 (define bound-identifier=? #f)
199 (define free-identifier=? #f)
200 (define sc-expand #f)
201
202 ;; $sc-expand is an implementation detail of psyntax. It is used by
203 ;; expanded macros, to dispatch an input against a set of patterns.
204 (define $sc-dispatch #f)
205
206 ;; Load it up!
207 (primitive-load-path "ice-9/psyntax-pp")
208
209 ;; %pre-modules-transformer is the Scheme expander from now until the
210 ;; module system has booted up.
211 (define %pre-modules-transformer sc-expand)
212
213 (define-syntax and
214 (syntax-rules ()
215 ((_) #t)
216 ((_ x) x)
217 ((_ x y ...) (if x (and y ...) #f))))
218
219 (define-syntax or
220 (syntax-rules ()
221 ((_) #f)
222 ((_ x) x)
223 ((_ x y ...) (let ((t x)) (if t t (or y ...))))))
224
225 (define-syntax cond
226 (syntax-rules (else =>)
227 ((cond (else result1 result2 ...))
228 (begin result1 result2 ...))
229 ((cond (test => result))
230 (let ((temp test))
231 (if temp (result temp))))
232 ((cond (test => result) clause1 clause2 ...)
233 (let ((temp test))
234 (if temp
235 (result temp)
236 (cond clause1 clause2 ...))))
237 ((cond (test)) test)
238 ((cond (test) clause1 clause2 ...)
239 (let ((temp test))
240 (if temp
241 temp
242 (cond clause1 clause2 ...))))
243 ((cond (test result1 result2 ...))
244 (if test (begin result1 result2 ...)))
245 ((cond (test result1 result2 ...)
246 clause1 clause2 ...)
247 (if test
248 (begin result1 result2 ...)
249 (cond clause1 clause2 ...)))))
250
251 (define-syntax case
252 (syntax-rules (else)
253 ((case (key ...)
254 clauses ...)
255 (let ((atom-key (key ...)))
256 (case atom-key clauses ...)))
257 ((case key
258 (else result1 result2 ...))
259 (begin result1 result2 ...))
260 ((case key
261 ((atoms ...) result1 result2 ...))
262 (if (memv key '(atoms ...))
263 (begin result1 result2 ...)))
264 ((case key
265 ((atoms ...) result1 result2 ...)
266 clause clauses ...)
267 (if (memv key '(atoms ...))
268 (begin result1 result2 ...)
269 (case key clause clauses ...)))))
270
271 (define-syntax do
272 (syntax-rules ()
273 ((do ((var init step ...) ...)
274 (test expr ...)
275 command ...)
276 (letrec
277 ((loop
278 (lambda (var ...)
279 (if test
280 (begin
281 (if #f #f)
282 expr ...)
283 (begin
284 command
285 ...
286 (loop (do "step" var step ...)
287 ...))))))
288 (loop init ...)))
289 ((do "step" x)
290 x)
291 ((do "step" x y)
292 y)))
293
294 (define-syntax delay
295 (syntax-rules ()
296 ((_ exp) (make-promise (lambda () exp)))))
297
298 \f
299
300 ;;; {Defmacros}
301 ;;;
302
303 (define-syntax define-macro
304 (lambda (x)
305 "Define a defmacro."
306 (syntax-case x ()
307 ((_ (macro . args) doc body1 body ...)
308 (string? (syntax->datum (syntax doc)))
309 (syntax (define-macro macro doc (lambda args body1 body ...))))
310 ((_ (macro . args) body ...)
311 (syntax (define-macro macro #f (lambda args body ...))))
312 ((_ macro doc transformer)
313 (or (string? (syntax->datum (syntax doc)))
314 (not (syntax->datum (syntax doc))))
315 (syntax
316 (define-syntax macro
317 (lambda (y)
318 doc
319 (syntax-case y ()
320 ((_ . args)
321 (let ((v (syntax->datum (syntax args))))
322 (datum->syntax y (apply transformer v))))))))))))
323
324 (define-syntax defmacro
325 (lambda (x)
326 "Define a defmacro, with the old lispy defun syntax."
327 (syntax-case x ()
328 ((_ macro args doc body1 body ...)
329 (string? (syntax->datum (syntax doc)))
330 (syntax (define-macro macro doc (lambda args body1 body ...))))
331 ((_ macro args body ...)
332 (syntax (define-macro macro #f (lambda args body ...)))))))
333
334 (provide 'defmacro)
335
336 \f
337
338 ;;; {Deprecation}
339 ;;;
340 ;;; Depends on: defmacro
341 ;;;
342
343 (defmacro begin-deprecated forms
344 (if (include-deprecated-features)
345 `(begin ,@forms)
346 `(begin)))
347
348 \f
349
350 ;;; {Trivial Functions}
351 ;;;
352
353 (define (identity x) x)
354 (define (and=> value procedure) (and value (procedure value)))
355 (define call/cc call-with-current-continuation)
356
357 ;;; apply-to-args is functionally redundant with apply and, worse,
358 ;;; is less general than apply since it only takes two arguments.
359 ;;;
360 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
361 ;;; perform binding in many circumstances when the "let" family of
362 ;;; of forms don't cut it. E.g.:
363 ;;;
364 ;;; (apply-to-args (return-3d-mouse-coords)
365 ;;; (lambda (x y z)
366 ;;; ...))
367 ;;;
368
369 (define (apply-to-args args fn) (apply fn args))
370
371 (defmacro false-if-exception (expr)
372 `(catch #t (lambda () ,expr)
373 (lambda args #f)))
374
375 \f
376
377 ;;; {General Properties}
378 ;;;
379
380 ;; This is a more modern interface to properties. It will replace all
381 ;; other property-like things eventually.
382
383 (define (make-object-property)
384 (let ((prop (primitive-make-property #f)))
385 (make-procedure-with-setter
386 (lambda (obj) (primitive-property-ref prop obj))
387 (lambda (obj val) (primitive-property-set! prop obj val)))))
388
389 \f
390
391 ;;; {Symbol Properties}
392 ;;;
393
394 (define (symbol-property sym prop)
395 (let ((pair (assoc prop (symbol-pref sym))))
396 (and pair (cdr pair))))
397
398 (define (set-symbol-property! sym prop val)
399 (let ((pair (assoc prop (symbol-pref sym))))
400 (if pair
401 (set-cdr! pair val)
402 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
403
404 (define (symbol-property-remove! sym prop)
405 (let ((pair (assoc prop (symbol-pref sym))))
406 (if pair
407 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
408
409 \f
410
411 ;;; {Arrays}
412 ;;;
413
414 (define (array-shape a)
415 (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
416 (array-dimensions a)))
417
418 \f
419
420 ;;; {Keywords}
421 ;;;
422
423 (define (kw-arg-ref args kw)
424 (let ((rem (member kw args)))
425 (and rem (pair? (cdr rem)) (cadr rem))))
426
427 \f
428
429 ;;; {Structs}
430 ;;;
431
432 (define (struct-layout s)
433 (struct-ref (struct-vtable s) vtable-index-layout))
434
435 \f
436
437 ;;; {Records}
438 ;;;
439
440 ;; Printing records: by default, records are printed as
441 ;;
442 ;; #<type-name field1: val1 field2: val2 ...>
443 ;;
444 ;; You can change that by giving a custom printing function to
445 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
446 ;; will be called like
447 ;;
448 ;; (<printer> object port)
449 ;;
450 ;; It should print OBJECT to PORT.
451
452 (define (inherit-print-state old-port new-port)
453 (if (get-print-state old-port)
454 (port-with-print-state new-port (get-print-state old-port))
455 new-port))
456
457 ;; 0: type-name, 1: fields
458 (define record-type-vtable
459 (make-vtable-vtable "prpr" 0
460 (lambda (s p)
461 (cond ((eq? s record-type-vtable)
462 (display "#<record-type-vtable>" p))
463 (else
464 (display "#<record-type " p)
465 (display (record-type-name s) p)
466 (display ">" p))))))
467
468 (define (record-type? obj)
469 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
470
471 (define (make-record-type type-name fields . opt)
472 (let ((printer-fn (and (pair? opt) (car opt))))
473 (let ((struct (make-struct record-type-vtable 0
474 (make-struct-layout
475 (apply string-append
476 (map (lambda (f) "pw") fields)))
477 (or printer-fn
478 (lambda (s p)
479 (display "#<" p)
480 (display type-name p)
481 (let loop ((fields fields)
482 (off 0))
483 (cond
484 ((not (null? fields))
485 (display " " p)
486 (display (car fields) p)
487 (display ": " p)
488 (display (struct-ref s off) p)
489 (loop (cdr fields) (+ 1 off)))))
490 (display ">" p)))
491 type-name
492 (copy-tree fields))))
493 ;; Temporary solution: Associate a name to the record type descriptor
494 ;; so that the object system can create a wrapper class for it.
495 (set-struct-vtable-name! struct (if (symbol? type-name)
496 type-name
497 (string->symbol type-name)))
498 struct)))
499
500 (define (record-type-name obj)
501 (if (record-type? obj)
502 (struct-ref obj vtable-offset-user)
503 (error 'not-a-record-type obj)))
504
505 (define (record-type-fields obj)
506 (if (record-type? obj)
507 (struct-ref obj (+ 1 vtable-offset-user))
508 (error 'not-a-record-type obj)))
509
510 (define (record-constructor rtd . opt)
511 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
512 (primitive-eval
513 `(lambda ,field-names
514 (make-struct ',rtd 0 ,@(map (lambda (f)
515 (if (memq f field-names)
516 f
517 #f))
518 (record-type-fields rtd)))))))
519
520 (define (record-predicate rtd)
521 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
522
523 (define (%record-type-error rtd obj) ;; private helper
524 (or (eq? rtd (record-type-descriptor obj))
525 (scm-error 'wrong-type-arg "%record-type-check"
526 "Wrong type record (want `~S'): ~S"
527 (list (record-type-name rtd) obj)
528 #f)))
529
530 (define (record-accessor rtd field-name)
531 (let ((pos (list-index (record-type-fields rtd) field-name)))
532 (if (not pos)
533 (error 'no-such-field field-name))
534 (lambda (obj)
535 (if (eq? (struct-vtable obj) rtd)
536 (struct-ref obj pos)
537 (%record-type-error rtd obj)))))
538
539 (define (record-modifier rtd field-name)
540 (let ((pos (list-index (record-type-fields rtd) field-name)))
541 (if (not pos)
542 (error 'no-such-field field-name))
543 (lambda (obj val)
544 (if (eq? (struct-vtable obj) rtd)
545 (struct-set! obj pos val)
546 (%record-type-error rtd obj)))))
547
548 (define (record? obj)
549 (and (struct? obj) (record-type? (struct-vtable obj))))
550
551 (define (record-type-descriptor obj)
552 (if (struct? obj)
553 (struct-vtable obj)
554 (error 'not-a-record obj)))
555
556 (provide 'record)
557
558 \f
559
560 ;;; {Booleans}
561 ;;;
562
563 (define (->bool x) (not (not x)))
564
565 \f
566
567 ;;; {Symbols}
568 ;;;
569
570 (define (symbol-append . args)
571 (string->symbol (apply string-append (map symbol->string args))))
572
573 (define (list->symbol . args)
574 (string->symbol (apply list->string args)))
575
576 (define (symbol . args)
577 (string->symbol (apply string args)))
578
579 \f
580
581 ;;; {Lists}
582 ;;;
583
584 (define (list-index l k)
585 (let loop ((n 0)
586 (l l))
587 (and (not (null? l))
588 (if (eq? (car l) k)
589 n
590 (loop (+ n 1) (cdr l))))))
591
592 \f
593
594 (if (provided? 'posix)
595 (primitive-load-path "ice-9/posix"))
596
597 (if (provided? 'socket)
598 (primitive-load-path "ice-9/networking"))
599
600 ;; For reference, Emacs file-exists-p uses stat in this same way.
601 ;; ENHANCE-ME: Catching an exception from stat is a bit wasteful, do this in
602 ;; C where all that's needed is to inspect the return from stat().
603 (define file-exists?
604 (if (provided? 'posix)
605 (lambda (str)
606 (->bool (false-if-exception (stat str))))
607 (lambda (str)
608 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
609 (lambda args #f))))
610 (if port (begin (close-port port) #t)
611 #f)))))
612
613 (define file-is-directory?
614 (if (provided? 'posix)
615 (lambda (str)
616 (eq? (stat:type (stat str)) 'directory))
617 (lambda (str)
618 (let ((port (catch 'system-error
619 (lambda () (open-file (string-append str "/.")
620 OPEN_READ))
621 (lambda args #f))))
622 (if port (begin (close-port port) #t)
623 #f)))))
624
625 (define (has-suffix? str suffix)
626 (string-suffix? suffix str))
627
628 (define (system-error-errno args)
629 (if (eq? (car args) 'system-error)
630 (car (list-ref args 4))
631 #f))
632
633 \f
634
635 ;;; {Error Handling}
636 ;;;
637
638 (define (error . args)
639 (save-stack)
640 (if (null? args)
641 (scm-error 'misc-error #f "?" #f #f)
642 (let loop ((msg "~A")
643 (rest (cdr args)))
644 (if (not (null? rest))
645 (loop (string-append msg " ~S")
646 (cdr rest))
647 (scm-error 'misc-error #f msg args #f)))))
648
649 ;; bad-throw is the hook that is called upon a throw to a an unhandled
650 ;; key (unless the throw has four arguments, in which case
651 ;; it's usually interpreted as an error throw.)
652 ;; If the key has a default handler (a throw-handler-default property),
653 ;; it is applied to the throw.
654 ;;
655 (define (bad-throw key . args)
656 (let ((default (symbol-property key 'throw-handler-default)))
657 (or (and default (apply default key args))
658 (apply error "unhandled-exception:" key args))))
659
660 \f
661
662 (define (tm:sec obj) (vector-ref obj 0))
663 (define (tm:min obj) (vector-ref obj 1))
664 (define (tm:hour obj) (vector-ref obj 2))
665 (define (tm:mday obj) (vector-ref obj 3))
666 (define (tm:mon obj) (vector-ref obj 4))
667 (define (tm:year obj) (vector-ref obj 5))
668 (define (tm:wday obj) (vector-ref obj 6))
669 (define (tm:yday obj) (vector-ref obj 7))
670 (define (tm:isdst obj) (vector-ref obj 8))
671 (define (tm:gmtoff obj) (vector-ref obj 9))
672 (define (tm:zone obj) (vector-ref obj 10))
673
674 (define (set-tm:sec obj val) (vector-set! obj 0 val))
675 (define (set-tm:min obj val) (vector-set! obj 1 val))
676 (define (set-tm:hour obj val) (vector-set! obj 2 val))
677 (define (set-tm:mday obj val) (vector-set! obj 3 val))
678 (define (set-tm:mon obj val) (vector-set! obj 4 val))
679 (define (set-tm:year obj val) (vector-set! obj 5 val))
680 (define (set-tm:wday obj val) (vector-set! obj 6 val))
681 (define (set-tm:yday obj val) (vector-set! obj 7 val))
682 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
683 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
684 (define (set-tm:zone obj val) (vector-set! obj 10 val))
685
686 (define (tms:clock obj) (vector-ref obj 0))
687 (define (tms:utime obj) (vector-ref obj 1))
688 (define (tms:stime obj) (vector-ref obj 2))
689 (define (tms:cutime obj) (vector-ref obj 3))
690 (define (tms:cstime obj) (vector-ref obj 4))
691
692 (define file-position ftell)
693 (define (file-set-position port offset . whence)
694 (let ((whence (if (eq? whence '()) SEEK_SET (car whence))))
695 (seek port offset whence)))
696
697 (define (move->fdes fd/port fd)
698 (cond ((integer? fd/port)
699 (dup->fdes fd/port fd)
700 (close fd/port)
701 fd)
702 (else
703 (primitive-move->fdes fd/port fd)
704 (set-port-revealed! fd/port 1)
705 fd/port)))
706
707 (define (release-port-handle port)
708 (let ((revealed (port-revealed port)))
709 (if (> revealed 0)
710 (set-port-revealed! port (- revealed 1)))))
711
712 (define (dup->port port/fd mode . maybe-fd)
713 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
714 mode)))
715 (if (pair? maybe-fd)
716 (set-port-revealed! port 1))
717 port))
718
719 (define (dup->inport port/fd . maybe-fd)
720 (apply dup->port port/fd "r" maybe-fd))
721
722 (define (dup->outport port/fd . maybe-fd)
723 (apply dup->port port/fd "w" maybe-fd))
724
725 (define (dup port/fd . maybe-fd)
726 (if (integer? port/fd)
727 (apply dup->fdes port/fd maybe-fd)
728 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
729
730 (define (duplicate-port port modes)
731 (dup->port port modes))
732
733 (define (fdes->inport fdes)
734 (let loop ((rest-ports (fdes->ports fdes)))
735 (cond ((null? rest-ports)
736 (let ((result (fdopen fdes "r")))
737 (set-port-revealed! result 1)
738 result))
739 ((input-port? (car rest-ports))
740 (set-port-revealed! (car rest-ports)
741 (+ (port-revealed (car rest-ports)) 1))
742 (car rest-ports))
743 (else
744 (loop (cdr rest-ports))))))
745
746 (define (fdes->outport fdes)
747 (let loop ((rest-ports (fdes->ports fdes)))
748 (cond ((null? rest-ports)
749 (let ((result (fdopen fdes "w")))
750 (set-port-revealed! result 1)
751 result))
752 ((output-port? (car rest-ports))
753 (set-port-revealed! (car rest-ports)
754 (+ (port-revealed (car rest-ports)) 1))
755 (car rest-ports))
756 (else
757 (loop (cdr rest-ports))))))
758
759 (define (port->fdes port)
760 (set-port-revealed! port (+ (port-revealed port) 1))
761 (fileno port))
762
763 (define (setenv name value)
764 (if value
765 (putenv (string-append name "=" value))
766 (putenv name)))
767
768 (define (unsetenv name)
769 "Remove the entry for NAME from the environment."
770 (putenv name))
771
772 \f
773
774 ;;; {Load Paths}
775 ;;;
776
777 ;;; Here for backward compatability
778 ;;
779 (define scheme-file-suffix (lambda () ".scm"))
780
781 (define (in-vicinity vicinity file)
782 (let ((tail (let ((len (string-length vicinity)))
783 (if (zero? len)
784 #f
785 (string-ref vicinity (- len 1))))))
786 (string-append vicinity
787 (if (or (not tail)
788 (eq? tail #\/))
789 ""
790 "/")
791 file)))
792
793 \f
794
795 ;;; {Help for scm_shell}
796 ;;;
797 ;;; The argument-processing code used by Guile-based shells generates
798 ;;; Scheme code based on the argument list. This page contains help
799 ;;; functions for the code it generates.
800 ;;;
801
802 (define (command-line) (program-arguments))
803
804 ;; This is mostly for the internal use of the code generated by
805 ;; scm_compile_shell_switches.
806
807 (define (turn-on-debugging)
808 (debug-enable 'debug)
809 (debug-enable 'backtrace)
810 (read-enable 'positions))
811
812 (define (load-user-init)
813 (let* ((home (or (getenv "HOME")
814 (false-if-exception (passwd:dir (getpwuid (getuid))))
815 "/")) ;; fallback for cygwin etc.
816 (init-file (in-vicinity home ".guile")))
817 (if (file-exists? init-file)
818 (primitive-load init-file))))
819
820 \f
821
822 ;;; {The interpreter stack}
823 ;;;
824
825 (defmacro start-stack (tag exp)
826 `(%start-stack ,tag (lambda () ,exp)))
827
828 \f
829
830 ;;; {Loading by paths}
831 ;;;
832
833 ;;; Load a Scheme source file named NAME, searching for it in the
834 ;;; directories listed in %load-path, and applying each of the file
835 ;;; name extensions listed in %load-extensions.
836 (define (load-from-path name)
837 (start-stack 'load-stack
838 (primitive-load-path name)))
839
840 (define %load-verbosely #f)
841 (define (assert-load-verbosity v) (set! %load-verbosely v))
842
843 (define (%load-announce file)
844 (if %load-verbosely
845 (with-output-to-port (current-error-port)
846 (lambda ()
847 (display ";;; ")
848 (display "loading ")
849 (display file)
850 (newline)
851 (force-output)))))
852
853 (set! %load-hook %load-announce)
854
855 (define (load name . reader)
856 (with-fluid* current-reader (and (pair? reader) (car reader))
857 (lambda ()
858 (start-stack 'load-stack
859 (primitive-load name)))))
860
861 \f
862
863 ;;; {Transcendental Functions}
864 ;;;
865 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
866 ;;; Written by Jerry D. Hedden, (C) FSF.
867 ;;; See the file `COPYING' for terms applying to this program.
868 ;;;
869
870 (define expt
871 (let ((integer-expt integer-expt))
872 (lambda (z1 z2)
873 (cond ((and (exact? z2) (integer? z2))
874 (integer-expt z1 z2))
875 ((and (real? z2) (real? z1) (>= z1 0))
876 ($expt z1 z2))
877 (else
878 (exp (* z2 (log z1))))))))
879
880 (define (sinh z)
881 (if (real? z) ($sinh z)
882 (let ((x (real-part z)) (y (imag-part z)))
883 (make-rectangular (* ($sinh x) ($cos y))
884 (* ($cosh x) ($sin y))))))
885 (define (cosh z)
886 (if (real? z) ($cosh z)
887 (let ((x (real-part z)) (y (imag-part z)))
888 (make-rectangular (* ($cosh x) ($cos y))
889 (* ($sinh x) ($sin y))))))
890 (define (tanh z)
891 (if (real? z) ($tanh z)
892 (let* ((x (* 2 (real-part z)))
893 (y (* 2 (imag-part z)))
894 (w (+ ($cosh x) ($cos y))))
895 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
896
897 (define (asinh z)
898 (if (real? z) ($asinh z)
899 (log (+ z (sqrt (+ (* z z) 1))))))
900
901 (define (acosh z)
902 (if (and (real? z) (>= z 1))
903 ($acosh z)
904 (log (+ z (sqrt (- (* z z) 1))))))
905
906 (define (atanh z)
907 (if (and (real? z) (> z -1) (< z 1))
908 ($atanh z)
909 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
910
911 (define (sin z)
912 (if (real? z) ($sin z)
913 (let ((x (real-part z)) (y (imag-part z)))
914 (make-rectangular (* ($sin x) ($cosh y))
915 (* ($cos x) ($sinh y))))))
916 (define (cos z)
917 (if (real? z) ($cos z)
918 (let ((x (real-part z)) (y (imag-part z)))
919 (make-rectangular (* ($cos x) ($cosh y))
920 (- (* ($sin x) ($sinh y)))))))
921 (define (tan z)
922 (if (real? z) ($tan z)
923 (let* ((x (* 2 (real-part z)))
924 (y (* 2 (imag-part z)))
925 (w (+ ($cos x) ($cosh y))))
926 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
927
928 (define (asin z)
929 (if (and (real? z) (>= z -1) (<= z 1))
930 ($asin z)
931 (* -i (asinh (* +i z)))))
932
933 (define (acos z)
934 (if (and (real? z) (>= z -1) (<= z 1))
935 ($acos z)
936 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
937
938 (define (atan z . y)
939 (if (null? y)
940 (if (real? z) ($atan z)
941 (/ (log (/ (- +i z) (+ +i z))) +2i))
942 ($atan2 z (car y))))
943
944 \f
945
946 ;;; {Reader Extensions}
947 ;;;
948 ;;; Reader code for various "#c" forms.
949 ;;;
950
951 (read-hash-extend #\' (lambda (c port)
952 (read port)))
953
954 (define read-eval? (make-fluid))
955 (fluid-set! read-eval? #f)
956 (read-hash-extend #\.
957 (lambda (c port)
958 (if (fluid-ref read-eval?)
959 (eval (read port) (interaction-environment))
960 (error
961 "#. read expansion found and read-eval? is #f."))))
962
963 \f
964
965 ;;; {Command Line Options}
966 ;;;
967
968 (define (get-option argv kw-opts kw-args return)
969 (cond
970 ((null? argv)
971 (return #f #f argv))
972
973 ((or (not (eq? #\- (string-ref (car argv) 0)))
974 (eq? (string-length (car argv)) 1))
975 (return 'normal-arg (car argv) (cdr argv)))
976
977 ((eq? #\- (string-ref (car argv) 1))
978 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
979 (string-length (car argv))))
980 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
981 (kw-opt? (member kw kw-opts))
982 (kw-arg? (member kw kw-args))
983 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
984 (substring (car argv)
985 (+ kw-arg-pos 1)
986 (string-length (car argv))))
987 (and kw-arg?
988 (begin (set! argv (cdr argv)) (car argv))))))
989 (if (or kw-opt? kw-arg?)
990 (return kw arg (cdr argv))
991 (return 'usage-error kw (cdr argv)))))
992
993 (else
994 (let* ((char (substring (car argv) 1 2))
995 (kw (symbol->keyword char)))
996 (cond
997
998 ((member kw kw-opts)
999 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1000 (new-argv (if (= 0 (string-length rest-car))
1001 (cdr argv)
1002 (cons (string-append "-" rest-car) (cdr argv)))))
1003 (return kw #f new-argv)))
1004
1005 ((member kw kw-args)
1006 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1007 (arg (if (= 0 (string-length rest-car))
1008 (cadr argv)
1009 rest-car))
1010 (new-argv (if (= 0 (string-length rest-car))
1011 (cddr argv)
1012 (cdr argv))))
1013 (return kw arg new-argv)))
1014
1015 (else (return 'usage-error kw argv)))))))
1016
1017 (define (for-next-option proc argv kw-opts kw-args)
1018 (let loop ((argv argv))
1019 (get-option argv kw-opts kw-args
1020 (lambda (opt opt-arg argv)
1021 (and opt (proc opt opt-arg argv loop))))))
1022
1023 (define (display-usage-report kw-desc)
1024 (for-each
1025 (lambda (kw)
1026 (or (eq? (car kw) #t)
1027 (eq? (car kw) 'else)
1028 (let* ((opt-desc kw)
1029 (help (cadr opt-desc))
1030 (opts (car opt-desc))
1031 (opts-proper (if (string? (car opts)) (cdr opts) opts))
1032 (arg-name (if (string? (car opts))
1033 (string-append "<" (car opts) ">")
1034 ""))
1035 (left-part (string-append
1036 (with-output-to-string
1037 (lambda ()
1038 (map (lambda (x) (display (keyword->symbol x)) (display " "))
1039 opts-proper)))
1040 arg-name))
1041 (middle-part (if (and (< (string-length left-part) 30)
1042 (< (string-length help) 40))
1043 (make-string (- 30 (string-length left-part)) #\ )
1044 "\n\t")))
1045 (display left-part)
1046 (display middle-part)
1047 (display help)
1048 (newline))))
1049 kw-desc))
1050
1051
1052
1053 (define (transform-usage-lambda cases)
1054 (let* ((raw-usage (delq! 'else (map car cases)))
1055 (usage-sans-specials (map (lambda (x)
1056 (or (and (not (list? x)) x)
1057 (and (symbol? (car x)) #t)
1058 (and (boolean? (car x)) #t)
1059 x))
1060 raw-usage))
1061 (usage-desc (delq! #t usage-sans-specials))
1062 (kw-desc (map car usage-desc))
1063 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1064 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1065 (transmogrified-cases (map (lambda (case)
1066 (cons (let ((opts (car case)))
1067 (if (or (boolean? opts) (eq? 'else opts))
1068 opts
1069 (cond
1070 ((symbol? (car opts)) opts)
1071 ((boolean? (car opts)) opts)
1072 ((string? (caar opts)) (cdar opts))
1073 (else (car opts)))))
1074 (cdr case)))
1075 cases)))
1076 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1077 (lambda (%argv)
1078 (let %next-arg ((%argv %argv))
1079 (get-option %argv
1080 ',kw-opts
1081 ',kw-args
1082 (lambda (%opt %arg %new-argv)
1083 (case %opt
1084 ,@ transmogrified-cases))))))))
1085
1086
1087 \f
1088
1089 ;;; {Low Level Modules}
1090 ;;;
1091 ;;; These are the low level data structures for modules.
1092 ;;;
1093 ;;; Every module object is of the type 'module-type', which is a record
1094 ;;; consisting of the following members:
1095 ;;;
1096 ;;; - eval-closure: the function that defines for its module the strategy that
1097 ;;; shall be followed when looking up symbols in the module.
1098 ;;;
1099 ;;; An eval-closure is a function taking two arguments: the symbol to be
1100 ;;; looked up and a boolean value telling whether a binding for the symbol
1101 ;;; should be created if it does not exist yet. If the symbol lookup
1102 ;;; succeeded (either because an existing binding was found or because a new
1103 ;;; binding was created), a variable object representing the binding is
1104 ;;; returned. Otherwise, the value #f is returned. Note that the eval
1105 ;;; closure does not take the module to be searched as an argument: During
1106 ;;; construction of the eval-closure, the eval-closure has to store the
1107 ;;; module it belongs to in its environment. This means, that any
1108 ;;; eval-closure can belong to only one module.
1109 ;;;
1110 ;;; The eval-closure of a module can be defined arbitrarily. However, three
1111 ;;; special cases of eval-closures are to be distinguished: During startup
1112 ;;; the module system is not yet activated. In this phase, no modules are
1113 ;;; defined and all bindings are automatically stored by the system in the
1114 ;;; pre-modules-obarray. Since no eval-closures exist at this time, the
1115 ;;; functions which require an eval-closure as their argument need to be
1116 ;;; passed the value #f.
1117 ;;;
1118 ;;; The other two special cases of eval-closures are the
1119 ;;; standard-eval-closure and the standard-interface-eval-closure. Both
1120 ;;; behave equally for the case that no new binding is to be created. The
1121 ;;; difference between the two comes in, when the boolean argument to the
1122 ;;; eval-closure indicates that a new binding shall be created if it is not
1123 ;;; found.
1124 ;;;
1125 ;;; Given that no new binding shall be created, both standard eval-closures
1126 ;;; define the following standard strategy of searching bindings in the
1127 ;;; module: First, the module's obarray is searched for the symbol. Second,
1128 ;;; if no binding for the symbol was found in the module's obarray, the
1129 ;;; module's binder procedure is exececuted. If this procedure did not
1130 ;;; return a binding for the symbol, the modules referenced in the module's
1131 ;;; uses list are recursively searched for a binding of the symbol. If the
1132 ;;; binding can not be found in these modules also, the symbol lookup has
1133 ;;; failed.
1134 ;;;
1135 ;;; If a new binding shall be created, the standard-interface-eval-closure
1136 ;;; immediately returns indicating failure. That is, it does not even try
1137 ;;; to look up the symbol. In contrast, the standard-eval-closure would
1138 ;;; first search the obarray, and if no binding was found there, would
1139 ;;; create a new binding in the obarray, therefore not calling the binder
1140 ;;; procedure or searching the modules in the uses list.
1141 ;;;
1142 ;;; The explanation of the following members obarray, binder and uses
1143 ;;; assumes that the symbol lookup follows the strategy that is defined in
1144 ;;; the standard-eval-closure and the standard-interface-eval-closure.
1145 ;;;
1146 ;;; - obarray: a hash table that maps symbols to variable objects. In this
1147 ;;; hash table, the definitions are found that are local to the module (that
1148 ;;; is, not imported from other modules). When looking up bindings in the
1149 ;;; module, this hash table is searched first.
1150 ;;;
1151 ;;; - binder: either #f or a function taking a module and a symbol argument.
1152 ;;; If it is a function it is called after the obarray has been
1153 ;;; unsuccessfully searched for a binding. It then can provide bindings
1154 ;;; that would otherwise not be found locally in the module.
1155 ;;;
1156 ;;; - uses: a list of modules from which non-local bindings can be inherited.
1157 ;;; These modules are the third place queried for bindings after the obarray
1158 ;;; has been unsuccessfully searched and the binder function did not deliver
1159 ;;; a result either.
1160 ;;;
1161 ;;; - transformer: either #f or a function taking a scheme expression as
1162 ;;; delivered by read. If it is a function, it will be called to perform
1163 ;;; syntax transformations (e. g. makro expansion) on the given scheme
1164 ;;; expression. The output of the transformer function will then be passed
1165 ;;; to Guile's internal memoizer. This means that the output must be valid
1166 ;;; scheme code. The only exception is, that the output may make use of the
1167 ;;; syntax extensions provided to identify the modules that a binding
1168 ;;; belongs to.
1169 ;;;
1170 ;;; - name: the name of the module. This is used for all kinds of printing
1171 ;;; outputs. In certain places the module name also serves as a way of
1172 ;;; identification. When adding a module to the uses list of another
1173 ;;; module, it is made sure that the new uses list will not contain two
1174 ;;; modules of the same name.
1175 ;;;
1176 ;;; - kind: classification of the kind of module. The value is (currently?)
1177 ;;; only used for printing. It has no influence on how a module is treated.
1178 ;;; Currently the following values are used when setting the module kind:
1179 ;;; 'module, 'directory, 'interface, 'custom-interface. If no explicit kind
1180 ;;; is set, it defaults to 'module.
1181 ;;;
1182 ;;; - duplicates-handlers: a list of procedures that get called to make a
1183 ;;; choice between two duplicate bindings when name clashes occur. See the
1184 ;;; `duplicate-handlers' global variable below.
1185 ;;;
1186 ;;; - observers: a list of procedures that get called when the module is
1187 ;;; modified.
1188 ;;;
1189 ;;; - weak-observers: a weak-key hash table of procedures that get called
1190 ;;; when the module is modified. See `module-observe-weak' for details.
1191 ;;;
1192 ;;; In addition, the module may (must?) contain a binding for
1193 ;;; `%module-public-interface'. This variable should be bound to a module
1194 ;;; representing the exported interface of a module. See the
1195 ;;; `module-public-interface' and `module-export!' procedures.
1196 ;;;
1197 ;;; !!! warning: The interface to lazy binder procedures is going
1198 ;;; to be changed in an incompatible way to permit all the basic
1199 ;;; module ops to be virtualized.
1200 ;;;
1201 ;;; (make-module size use-list lazy-binding-proc) => module
1202 ;;; module-{obarray,uses,binder}[|-set!]
1203 ;;; (module? obj) => [#t|#f]
1204 ;;; (module-locally-bound? module symbol) => [#t|#f]
1205 ;;; (module-bound? module symbol) => [#t|#f]
1206 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1207 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1208 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1209 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1210 ;;; (module-symbol-binding module symbol opt-value)
1211 ;;; => [ <obj> | opt-value | an error occurs ]
1212 ;;; (module-make-local-var! module symbol) => #<variable...>
1213 ;;; (module-add! module symbol var) => unspecified
1214 ;;; (module-remove! module symbol) => unspecified
1215 ;;; (module-for-each proc module) => unspecified
1216 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1217 ;;; (set-current-module module) => unspecified
1218 ;;; (current-module) => #<module...>
1219 ;;;
1220 ;;;
1221
1222 \f
1223
1224 ;;; {Printing Modules}
1225 ;;;
1226
1227 ;; This is how modules are printed. You can re-define it.
1228 ;; (Redefining is actually more complicated than simply redefining
1229 ;; %print-module because that would only change the binding and not
1230 ;; the value stored in the vtable that determines how record are
1231 ;; printed. Sigh.)
1232
1233 (define (%print-module mod port) ; unused args: depth length style table)
1234 (display "#<" port)
1235 (display (or (module-kind mod) "module") port)
1236 (let ((name (module-name mod)))
1237 (if name
1238 (begin
1239 (display " " port)
1240 (display name port))))
1241 (display " " port)
1242 (display (number->string (object-address mod) 16) port)
1243 (display ">" port))
1244
1245 ;; module-type
1246 ;;
1247 ;; A module is characterized by an obarray in which local symbols
1248 ;; are interned, a list of modules, "uses", from which non-local
1249 ;; bindings can be inherited, and an optional lazy-binder which
1250 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1251 ;; bindings that would otherwise not be found locally in the module.
1252 ;;
1253 ;; NOTE: If you change anything here, you also need to change
1254 ;; libguile/modules.h.
1255 ;;
1256 (define module-type
1257 (make-record-type 'module
1258 '(obarray uses binder eval-closure transformer name kind
1259 duplicates-handlers import-obarray
1260 observers weak-observers)
1261 %print-module))
1262
1263 ;; make-module &opt size uses binder
1264 ;;
1265 ;; Create a new module, perhaps with a particular size of obarray,
1266 ;; initial uses list, or binding procedure.
1267 ;;
1268 (define make-module
1269 (lambda args
1270
1271 (define (parse-arg index default)
1272 (if (> (length args) index)
1273 (list-ref args index)
1274 default))
1275
1276 (define %default-import-size
1277 ;; Typical number of imported bindings actually used by a module.
1278 600)
1279
1280 (if (> (length args) 3)
1281 (error "Too many args to make-module." args))
1282
1283 (let ((size (parse-arg 0 31))
1284 (uses (parse-arg 1 '()))
1285 (binder (parse-arg 2 #f)))
1286
1287 (if (not (integer? size))
1288 (error "Illegal size to make-module." size))
1289 (if (not (and (list? uses)
1290 (and-map module? uses)))
1291 (error "Incorrect use list." uses))
1292 (if (and binder (not (procedure? binder)))
1293 (error
1294 "Lazy-binder expected to be a procedure or #f." binder))
1295
1296 (let ((module (module-constructor (make-hash-table size)
1297 uses binder #f %pre-modules-transformer
1298 #f #f #f
1299 (make-hash-table %default-import-size)
1300 '()
1301 (make-weak-key-hash-table 31))))
1302
1303 ;; We can't pass this as an argument to module-constructor,
1304 ;; because we need it to close over a pointer to the module
1305 ;; itself.
1306 (set-module-eval-closure! module (standard-eval-closure module))
1307
1308 module))))
1309
1310 (define module-constructor (record-constructor module-type))
1311 (define module-obarray (record-accessor module-type 'obarray))
1312 (define set-module-obarray! (record-modifier module-type 'obarray))
1313 (define module-uses (record-accessor module-type 'uses))
1314 (define set-module-uses! (record-modifier module-type 'uses))
1315 (define module-binder (record-accessor module-type 'binder))
1316 (define set-module-binder! (record-modifier module-type 'binder))
1317
1318 ;; NOTE: This binding is used in libguile/modules.c.
1319 (define module-eval-closure (record-accessor module-type 'eval-closure))
1320
1321 (define module-transformer (record-accessor module-type 'transformer))
1322 (define set-module-transformer! (record-modifier module-type 'transformer))
1323 ;; (define module-name (record-accessor module-type 'name)) wait until mods are booted
1324 (define set-module-name! (record-modifier module-type 'name))
1325 (define module-kind (record-accessor module-type 'kind))
1326 (define set-module-kind! (record-modifier module-type 'kind))
1327 (define module-duplicates-handlers
1328 (record-accessor module-type 'duplicates-handlers))
1329 (define set-module-duplicates-handlers!
1330 (record-modifier module-type 'duplicates-handlers))
1331 (define module-observers (record-accessor module-type 'observers))
1332 (define set-module-observers! (record-modifier module-type 'observers))
1333 (define module-weak-observers (record-accessor module-type 'weak-observers))
1334 (define module? (record-predicate module-type))
1335
1336 (define module-import-obarray (record-accessor module-type 'import-obarray))
1337
1338 (define set-module-eval-closure!
1339 (let ((setter (record-modifier module-type 'eval-closure)))
1340 (lambda (module closure)
1341 (setter module closure)
1342 ;; Make it possible to lookup the module from the environment.
1343 ;; This implementation is correct since an eval closure can belong
1344 ;; to maximally one module.
1345 (set-procedure-property! closure 'module module))))
1346
1347 \f
1348
1349 ;;; {Observer protocol}
1350 ;;;
1351
1352 (define (module-observe module proc)
1353 (set-module-observers! module (cons proc (module-observers module)))
1354 (cons module proc))
1355
1356 (define (module-observe-weak module observer-id . proc)
1357 ;; Register PROC as an observer of MODULE under name OBSERVER-ID (which can
1358 ;; be any Scheme object). PROC is invoked and passed MODULE any time
1359 ;; MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd
1360 ;; (thus, it is never unregistered if OBSERVER-ID is an immediate value,
1361 ;; for instance).
1362
1363 ;; The two-argument version is kept for backward compatibility: when called
1364 ;; with two arguments, the observer gets unregistered when closure PROC
1365 ;; gets GC'd (making it impossible to use an anonymous lambda for PROC).
1366
1367 (let ((proc (if (null? proc) observer-id (car proc))))
1368 (hashq-set! (module-weak-observers module) observer-id proc)))
1369
1370 (define (module-unobserve token)
1371 (let ((module (car token))
1372 (id (cdr token)))
1373 (if (integer? id)
1374 (hash-remove! (module-weak-observers module) id)
1375 (set-module-observers! module (delq1! id (module-observers module)))))
1376 *unspecified*)
1377
1378 (define module-defer-observers #f)
1379 (define module-defer-observers-mutex (make-mutex 'recursive))
1380 (define module-defer-observers-table (make-hash-table))
1381
1382 (define (module-modified m)
1383 (if module-defer-observers
1384 (hash-set! module-defer-observers-table m #t)
1385 (module-call-observers m)))
1386
1387 ;;; This function can be used to delay calls to observers so that they
1388 ;;; can be called once only in the face of massive updating of modules.
1389 ;;;
1390 (define (call-with-deferred-observers thunk)
1391 (dynamic-wind
1392 (lambda ()
1393 (lock-mutex module-defer-observers-mutex)
1394 (set! module-defer-observers #t))
1395 thunk
1396 (lambda ()
1397 (set! module-defer-observers #f)
1398 (hash-for-each (lambda (m dummy)
1399 (module-call-observers m))
1400 module-defer-observers-table)
1401 (hash-clear! module-defer-observers-table)
1402 (unlock-mutex module-defer-observers-mutex))))
1403
1404 (define (module-call-observers m)
1405 (for-each (lambda (proc) (proc m)) (module-observers m))
1406
1407 ;; We assume that weak observers don't (un)register themselves as they are
1408 ;; called since this would preclude proper iteration over the hash table
1409 ;; elements.
1410 (hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m)))
1411
1412 \f
1413
1414 ;;; {Module Searching in General}
1415 ;;;
1416 ;;; We sometimes want to look for properties of a symbol
1417 ;;; just within the obarray of one module. If the property
1418 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1419 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1420 ;;;
1421 ;;;
1422 ;;; Other times, we want to test for a symbol property in the obarray
1423 ;;; of M and, if it is not found there, try each of the modules in the
1424 ;;; uses list of M. This is the normal way of testing for some
1425 ;;; property, so we state these properties without qualification as
1426 ;;; in: ``The symbol 'fnord is interned in module M because it is
1427 ;;; interned locally in module M2 which is a member of the uses list
1428 ;;; of M.''
1429 ;;;
1430
1431 ;; module-search fn m
1432 ;;
1433 ;; return the first non-#f result of FN applied to M and then to
1434 ;; the modules in the uses of m, and so on recursively. If all applications
1435 ;; return #f, then so does this function.
1436 ;;
1437 (define (module-search fn m v)
1438 (define (loop pos)
1439 (and (pair? pos)
1440 (or (module-search fn (car pos) v)
1441 (loop (cdr pos)))))
1442 (or (fn m v)
1443 (loop (module-uses m))))
1444
1445
1446 ;;; {Is a symbol bound in a module?}
1447 ;;;
1448 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1449 ;;; of S in M has been set to some well-defined value.
1450 ;;;
1451
1452 ;; module-locally-bound? module symbol
1453 ;;
1454 ;; Is a symbol bound (interned and defined) locally in a given module?
1455 ;;
1456 (define (module-locally-bound? m v)
1457 (let ((var (module-local-variable m v)))
1458 (and var
1459 (variable-bound? var))))
1460
1461 ;; module-bound? module symbol
1462 ;;
1463 ;; Is a symbol bound (interned and defined) anywhere in a given module
1464 ;; or its uses?
1465 ;;
1466 (define (module-bound? m v)
1467 (let ((var (module-variable m v)))
1468 (and var
1469 (variable-bound? var))))
1470
1471 ;;; {Is a symbol interned in a module?}
1472 ;;;
1473 ;;; Symbol S in Module M is interned if S occurs in
1474 ;;; of S in M has been set to some well-defined value.
1475 ;;;
1476 ;;; It is possible to intern a symbol in a module without providing
1477 ;;; an initial binding for the corresponding variable. This is done
1478 ;;; with:
1479 ;;; (module-add! module symbol (make-undefined-variable))
1480 ;;;
1481 ;;; In that case, the symbol is interned in the module, but not
1482 ;;; bound there. The unbound symbol shadows any binding for that
1483 ;;; symbol that might otherwise be inherited from a member of the uses list.
1484 ;;;
1485
1486 (define (module-obarray-get-handle ob key)
1487 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1488
1489 (define (module-obarray-ref ob key)
1490 ((if (symbol? key) hashq-ref hash-ref) ob key))
1491
1492 (define (module-obarray-set! ob key val)
1493 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1494
1495 (define (module-obarray-remove! ob key)
1496 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1497
1498 ;; module-symbol-locally-interned? module symbol
1499 ;;
1500 ;; is a symbol interned (not neccessarily defined) locally in a given module
1501 ;; or its uses? Interned symbols shadow inherited bindings even if
1502 ;; they are not themselves bound to a defined value.
1503 ;;
1504 (define (module-symbol-locally-interned? m v)
1505 (not (not (module-obarray-get-handle (module-obarray m) v))))
1506
1507 ;; module-symbol-interned? module symbol
1508 ;;
1509 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1510 ;; or its uses? Interned symbols shadow inherited bindings even if
1511 ;; they are not themselves bound to a defined value.
1512 ;;
1513 (define (module-symbol-interned? m v)
1514 (module-search module-symbol-locally-interned? m v))
1515
1516
1517 ;;; {Mapping modules x symbols --> variables}
1518 ;;;
1519
1520 ;; module-local-variable module symbol
1521 ;; return the local variable associated with a MODULE and SYMBOL.
1522 ;;
1523 ;;; This function is very important. It is the only function that can
1524 ;;; return a variable from a module other than the mutators that store
1525 ;;; new variables in modules. Therefore, this function is the location
1526 ;;; of the "lazy binder" hack.
1527 ;;;
1528 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1529 ;;; to a variable, return that variable object.
1530 ;;;
1531 ;;; If the symbols is not found at first, but the module has a lazy binder,
1532 ;;; then try the binder.
1533 ;;;
1534 ;;; If the symbol is not found at all, return #f.
1535 ;;;
1536 ;;; (This is now written in C, see `modules.c'.)
1537 ;;;
1538
1539 ;;; {Mapping modules x symbols --> bindings}
1540 ;;;
1541 ;;; These are similar to the mapping to variables, except that the
1542 ;;; variable is dereferenced.
1543 ;;;
1544
1545 ;; module-symbol-binding module symbol opt-value
1546 ;;
1547 ;; return the binding of a variable specified by name within
1548 ;; a given module, signalling an error if the variable is unbound.
1549 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1550 ;; return OPT-VALUE.
1551 ;;
1552 (define (module-symbol-local-binding m v . opt-val)
1553 (let ((var (module-local-variable m v)))
1554 (if (and var (variable-bound? var))
1555 (variable-ref var)
1556 (if (not (null? opt-val))
1557 (car opt-val)
1558 (error "Locally unbound variable." v)))))
1559
1560 ;; module-symbol-binding module symbol opt-value
1561 ;;
1562 ;; return the binding of a variable specified by name within
1563 ;; a given module, signalling an error if the variable is unbound.
1564 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1565 ;; return OPT-VALUE.
1566 ;;
1567 (define (module-symbol-binding m v . opt-val)
1568 (let ((var (module-variable m v)))
1569 (if (and var (variable-bound? var))
1570 (variable-ref var)
1571 (if (not (null? opt-val))
1572 (car opt-val)
1573 (error "Unbound variable." v)))))
1574
1575
1576 \f
1577
1578 ;;; {Adding Variables to Modules}
1579 ;;;
1580
1581 ;; module-make-local-var! module symbol
1582 ;;
1583 ;; ensure a variable for V in the local namespace of M.
1584 ;; If no variable was already there, then create a new and uninitialzied
1585 ;; variable.
1586 ;;
1587 ;; This function is used in modules.c.
1588 ;;
1589 (define (module-make-local-var! m v)
1590 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1591 (and (variable? b)
1592 (begin
1593 ;; Mark as modified since this function is called when
1594 ;; the standard eval closure defines a binding
1595 (module-modified m)
1596 b)))
1597
1598 ;; Create a new local variable.
1599 (let ((local-var (make-undefined-variable)))
1600 (module-add! m v local-var)
1601 local-var)))
1602
1603 ;; module-ensure-local-variable! module symbol
1604 ;;
1605 ;; Ensure that there is a local variable in MODULE for SYMBOL. If
1606 ;; there is no binding for SYMBOL, create a new uninitialized
1607 ;; variable. Return the local variable.
1608 ;;
1609 (define (module-ensure-local-variable! module symbol)
1610 (or (module-local-variable module symbol)
1611 (let ((var (make-undefined-variable)))
1612 (module-add! module symbol var)
1613 var)))
1614
1615 ;; module-add! module symbol var
1616 ;;
1617 ;; ensure a particular variable for V in the local namespace of M.
1618 ;;
1619 (define (module-add! m v var)
1620 (if (not (variable? var))
1621 (error "Bad variable to module-add!" var))
1622 (module-obarray-set! (module-obarray m) v var)
1623 (module-modified m))
1624
1625 ;; module-remove!
1626 ;;
1627 ;; make sure that a symbol is undefined in the local namespace of M.
1628 ;;
1629 (define (module-remove! m v)
1630 (module-obarray-remove! (module-obarray m) v)
1631 (module-modified m))
1632
1633 (define (module-clear! m)
1634 (hash-clear! (module-obarray m))
1635 (module-modified m))
1636
1637 ;; MODULE-FOR-EACH -- exported
1638 ;;
1639 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1640 ;;
1641 (define (module-for-each proc module)
1642 (hash-for-each proc (module-obarray module)))
1643
1644 (define (module-map proc module)
1645 (hash-map->list proc (module-obarray module)))
1646
1647 \f
1648
1649 ;;; {Low Level Bootstrapping}
1650 ;;;
1651
1652 ;; make-root-module
1653
1654 ;; A root module uses the pre-modules-obarray as its obarray. This
1655 ;; special obarray accumulates all bindings that have been established
1656 ;; before the module system is fully booted.
1657 ;;
1658 ;; (The obarray continues to be used by code that has been closed over
1659 ;; before the module system has been booted.)
1660
1661 (define (make-root-module)
1662 (let ((m (make-module 0)))
1663 (set-module-obarray! m (%get-pre-modules-obarray))
1664 m))
1665
1666 ;; make-scm-module
1667
1668 ;; The root interface is a module that uses the same obarray as the
1669 ;; root module. It does not allow new definitions, tho.
1670
1671 (define (make-scm-module)
1672 (let ((m (make-module 0)))
1673 (set-module-obarray! m (%get-pre-modules-obarray))
1674 (set-module-eval-closure! m (standard-interface-eval-closure m))
1675 m))
1676
1677
1678 \f
1679
1680 ;;; {Module-based Loading}
1681 ;;;
1682
1683 (define (save-module-excursion thunk)
1684 (let ((inner-module (current-module))
1685 (outer-module #f))
1686 (dynamic-wind (lambda ()
1687 (set! outer-module (current-module))
1688 (set-current-module inner-module)
1689 (set! inner-module #f))
1690 thunk
1691 (lambda ()
1692 (set! inner-module (current-module))
1693 (set-current-module outer-module)
1694 (set! outer-module #f)))))
1695
1696 (define basic-load load)
1697
1698 (define (load-module filename . reader)
1699 (save-module-excursion
1700 (lambda ()
1701 (let ((oldname (and (current-load-port)
1702 (port-filename (current-load-port)))))
1703 (apply basic-load
1704 (if (and oldname
1705 (> (string-length filename) 0)
1706 (not (char=? (string-ref filename 0) #\/))
1707 (not (string=? (dirname oldname) ".")))
1708 (string-append (dirname oldname) "/" filename)
1709 filename)
1710 reader)))))
1711
1712
1713 \f
1714
1715 ;;; {MODULE-REF -- exported}
1716 ;;;
1717
1718 ;; Returns the value of a variable called NAME in MODULE or any of its
1719 ;; used modules. If there is no such variable, then if the optional third
1720 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1721 ;;
1722 (define (module-ref module name . rest)
1723 (let ((variable (module-variable module name)))
1724 (if (and variable (variable-bound? variable))
1725 (variable-ref variable)
1726 (if (null? rest)
1727 (error "No variable named" name 'in module)
1728 (car rest) ; default value
1729 ))))
1730
1731 ;; MODULE-SET! -- exported
1732 ;;
1733 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1734 ;; to VALUE; if there is no such variable, an error is signaled.
1735 ;;
1736 (define (module-set! module name value)
1737 (let ((variable (module-variable module name)))
1738 (if variable
1739 (variable-set! variable value)
1740 (error "No variable named" name 'in module))))
1741
1742 ;; MODULE-DEFINE! -- exported
1743 ;;
1744 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1745 ;; variable, it is added first.
1746 ;;
1747 (define (module-define! module name value)
1748 (let ((variable (module-local-variable module name)))
1749 (if variable
1750 (begin
1751 (variable-set! variable value)
1752 (module-modified module))
1753 (let ((variable (make-variable value)))
1754 (module-add! module name variable)))))
1755
1756 ;; MODULE-DEFINED? -- exported
1757 ;;
1758 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1759 ;; uses)
1760 ;;
1761 (define (module-defined? module name)
1762 (let ((variable (module-variable module name)))
1763 (and variable (variable-bound? variable))))
1764
1765 ;; MODULE-USE! module interface
1766 ;;
1767 ;; Add INTERFACE to the list of interfaces used by MODULE.
1768 ;;
1769 (define (module-use! module interface)
1770 (if (not (or (eq? module interface)
1771 (memq interface (module-uses module))))
1772 (begin
1773 ;; Newly used modules must be appended rather than consed, so that
1774 ;; `module-variable' traverses the use list starting from the first
1775 ;; used module.
1776 (set-module-uses! module
1777 (append (filter (lambda (m)
1778 (not
1779 (equal? (module-name m)
1780 (module-name interface))))
1781 (module-uses module))
1782 (list interface)))
1783
1784 (module-modified module))))
1785
1786 ;; MODULE-USE-INTERFACES! module interfaces
1787 ;;
1788 ;; Same as MODULE-USE! but add multiple interfaces and check for duplicates
1789 ;;
1790 (define (module-use-interfaces! module interfaces)
1791 (set-module-uses! module
1792 (append (module-uses module) interfaces))
1793 (module-modified module))
1794
1795 \f
1796
1797 ;;; {Recursive Namespaces}
1798 ;;;
1799 ;;; A hierarchical namespace emerges if we consider some module to be
1800 ;;; root, and variables bound to modules as nested namespaces.
1801 ;;;
1802 ;;; The routines in this file manage variable names in hierarchical namespace.
1803 ;;; Each variable name is a list of elements, looked up in successively nested
1804 ;;; modules.
1805 ;;;
1806 ;;; (nested-ref some-root-module '(foo bar baz))
1807 ;;; => <value of a variable named baz in the module bound to bar in
1808 ;;; the module bound to foo in some-root-module>
1809 ;;;
1810 ;;;
1811 ;;; There are:
1812 ;;;
1813 ;;; ;; a-root is a module
1814 ;;; ;; name is a list of symbols
1815 ;;;
1816 ;;; nested-ref a-root name
1817 ;;; nested-set! a-root name val
1818 ;;; nested-define! a-root name val
1819 ;;; nested-remove! a-root name
1820 ;;;
1821 ;;;
1822 ;;; (current-module) is a natural choice for a-root so for convenience there are
1823 ;;; also:
1824 ;;;
1825 ;;; local-ref name == nested-ref (current-module) name
1826 ;;; local-set! name val == nested-set! (current-module) name val
1827 ;;; local-define! name val == nested-define! (current-module) name val
1828 ;;; local-remove! name == nested-remove! (current-module) name
1829 ;;;
1830
1831
1832 (define (nested-ref root names)
1833 (let loop ((cur root)
1834 (elts names))
1835 (cond
1836 ((null? elts) cur)
1837 ((not (module? cur)) #f)
1838 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1839
1840 (define (nested-set! root names val)
1841 (let loop ((cur root)
1842 (elts names))
1843 (if (null? (cdr elts))
1844 (module-set! cur (car elts) val)
1845 (loop (module-ref cur (car elts)) (cdr elts)))))
1846
1847 (define (nested-define! root names val)
1848 (let loop ((cur root)
1849 (elts names))
1850 (if (null? (cdr elts))
1851 (module-define! cur (car elts) val)
1852 (loop (module-ref cur (car elts)) (cdr elts)))))
1853
1854 (define (nested-remove! root names)
1855 (let loop ((cur root)
1856 (elts names))
1857 (if (null? (cdr elts))
1858 (module-remove! cur (car elts))
1859 (loop (module-ref cur (car elts)) (cdr elts)))))
1860
1861 (define (local-ref names) (nested-ref (current-module) names))
1862 (define (local-set! names val) (nested-set! (current-module) names val))
1863 (define (local-define names val) (nested-define! (current-module) names val))
1864 (define (local-remove names) (nested-remove! (current-module) names))
1865
1866
1867 \f
1868
1869 ;;; {The (%app) module}
1870 ;;;
1871 ;;; The root of conventionally named objects not directly in the top level.
1872 ;;;
1873 ;;; (%app modules)
1874 ;;; (%app modules guile)
1875 ;;;
1876 ;;; The directory of all modules and the standard root module.
1877 ;;;
1878
1879 ;; module-public-interface is defined in C.
1880 (define (set-module-public-interface! m i)
1881 (module-define! m '%module-public-interface i))
1882 (define (set-system-module! m s)
1883 (set-procedure-property! (module-eval-closure m) 'system-module s))
1884 (define the-root-module (make-root-module))
1885 (define the-scm-module (make-scm-module))
1886 (set-module-public-interface! the-root-module the-scm-module)
1887 (set-module-name! the-root-module '(guile))
1888 (set-module-name! the-scm-module '(guile))
1889 (set-module-kind! the-scm-module 'interface)
1890 (set-system-module! the-root-module #t)
1891 (set-system-module! the-scm-module #t)
1892
1893 ;; NOTE: This binding is used in libguile/modules.c.
1894 ;;
1895 (define (make-modules-in module name)
1896 (if (null? name)
1897 module
1898 (make-modules-in
1899 (let* ((var (module-local-variable module (car name)))
1900 (val (and var (variable-bound? var) (variable-ref var))))
1901 (if (module? val)
1902 val
1903 (let ((m (make-module 31)))
1904 (set-module-kind! m 'directory)
1905 (set-module-name! m (append (or (module-name module) '())
1906 (list (car name))))
1907 (module-define! module (car name) m)
1908 m)))
1909 (cdr name))))
1910
1911 (define (beautify-user-module! module)
1912 (let ((interface (module-public-interface module)))
1913 (if (or (not interface)
1914 (eq? interface module))
1915 (let ((interface (make-module 31)))
1916 (set-module-name! interface (module-name module))
1917 (set-module-kind! interface 'interface)
1918 (set-module-public-interface! module interface))))
1919 (if (and (not (memq the-scm-module (module-uses module)))
1920 (not (eq? module the-root-module)))
1921 ;; Import the default set of bindings (from the SCM module) in MODULE.
1922 (module-use! module the-scm-module)))
1923
1924 ;; NOTE: This binding is used in libguile/modules.c.
1925 ;;
1926 (define resolve-module
1927 (let ((the-root-module the-root-module))
1928 (lambda (name . maybe-autoload)
1929 (if (equal? name '(guile))
1930 the-root-module
1931 (let ((full-name (append '(%app modules) name)))
1932 (let ((already (nested-ref the-root-module full-name))
1933 (autoload (or (null? maybe-autoload) (car maybe-autoload))))
1934 (cond
1935 ((and already (module? already)
1936 (or (not autoload) (module-public-interface already)))
1937 ;; A hit, a palpable hit.
1938 already)
1939 (autoload
1940 ;; Try to autoload the module, and recurse.
1941 (try-load-module name)
1942 (resolve-module name #f))
1943 (else
1944 ;; A module is not bound (but maybe something else is),
1945 ;; we're not autoloading -- here's the weird semantics,
1946 ;; we create an empty module.
1947 (make-modules-in the-root-module full-name)))))))))
1948
1949 ;; Cheat. These bindings are needed by modules.c, but we don't want
1950 ;; to move their real definition here because that would be unnatural.
1951 ;;
1952 (define try-module-autoload #f)
1953 (define process-define-module #f)
1954 (define process-use-modules #f)
1955 (define module-export! #f)
1956 (define default-duplicate-binding-procedures #f)
1957
1958 (define %app (make-module 31))
1959 (define app %app) ;; for backwards compatability
1960
1961 (local-define '(%app modules) (make-module 31))
1962 (local-define '(%app modules guile) the-root-module)
1963
1964 ;; This boots the module system. All bindings needed by modules.c
1965 ;; must have been defined by now.
1966 ;;
1967 (set-current-module the-root-module)
1968 ;; definition deferred for syncase's benefit
1969 (define module-name (record-accessor module-type 'name))
1970
1971 ;; (define-special-value '(%app modules new-ws) (lambda () (make-scm-module)))
1972
1973 (define (try-load-module name)
1974 (try-module-autoload name))
1975
1976 (define (purify-module! module)
1977 "Removes bindings in MODULE which are inherited from the (guile) module."
1978 (let ((use-list (module-uses module)))
1979 (if (and (pair? use-list)
1980 (eq? (car (last-pair use-list)) the-scm-module))
1981 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
1982
1983 ;; Return a module that is an interface to the module designated by
1984 ;; NAME.
1985 ;;
1986 ;; `resolve-interface' takes four keyword arguments:
1987 ;;
1988 ;; #:select SELECTION
1989 ;;
1990 ;; SELECTION is a list of binding-specs to be imported; A binding-spec
1991 ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG
1992 ;; is the name in the used module and SEEN is the name in the using
1993 ;; module. Note that SEEN is also passed through RENAMER, below. The
1994 ;; default is to select all bindings. If you specify no selection but
1995 ;; a renamer, only the bindings that already exist in the used module
1996 ;; are made available in the interface. Bindings that are added later
1997 ;; are not picked up.
1998 ;;
1999 ;; #:hide BINDINGS
2000 ;;
2001 ;; BINDINGS is a list of bindings which should not be imported.
2002 ;;
2003 ;; #:prefix PREFIX
2004 ;;
2005 ;; PREFIX is a symbol that will be appended to each exported name.
2006 ;; The default is to not perform any renaming.
2007 ;;
2008 ;; #:renamer RENAMER
2009 ;;
2010 ;; RENAMER is a procedure that takes a symbol and returns its new
2011 ;; name. The default is not perform any renaming.
2012 ;;
2013 ;; Signal "no code for module" error if module name is not resolvable
2014 ;; or its public interface is not available. Signal "no binding"
2015 ;; error if selected binding does not exist in the used module.
2016 ;;
2017 (define (resolve-interface name . args)
2018
2019 (define (get-keyword-arg args kw def)
2020 (cond ((memq kw args)
2021 => (lambda (kw-arg)
2022 (if (null? (cdr kw-arg))
2023 (error "keyword without value: " kw))
2024 (cadr kw-arg)))
2025 (else
2026 def)))
2027
2028 (let* ((select (get-keyword-arg args #:select #f))
2029 (hide (get-keyword-arg args #:hide '()))
2030 (renamer (or (get-keyword-arg args #:renamer #f)
2031 (let ((prefix (get-keyword-arg args #:prefix #f)))
2032 (and prefix (symbol-prefix-proc prefix)))
2033 identity))
2034 (module (resolve-module name))
2035 (public-i (and module (module-public-interface module))))
2036 (and (or (not module) (not public-i))
2037 (error "no code for module" name))
2038 (if (and (not select) (null? hide) (eq? renamer identity))
2039 public-i
2040 (let ((selection (or select (module-map (lambda (sym var) sym)
2041 public-i)))
2042 (custom-i (make-module 31)))
2043 (set-module-kind! custom-i 'custom-interface)
2044 (set-module-name! custom-i name)
2045 ;; XXX - should use a lazy binder so that changes to the
2046 ;; used module are picked up automatically.
2047 (for-each (lambda (bspec)
2048 (let* ((direct? (symbol? bspec))
2049 (orig (if direct? bspec (car bspec)))
2050 (seen (if direct? bspec (cdr bspec)))
2051 (var (or (module-local-variable public-i orig)
2052 (module-local-variable module orig)
2053 (error
2054 ;; fixme: format manually for now
2055 (simple-format
2056 #f "no binding `~A' in module ~A"
2057 orig name)))))
2058 (if (memq orig hide)
2059 (set! hide (delq! orig hide))
2060 (module-add! custom-i
2061 (renamer seen)
2062 var))))
2063 selection)
2064 ;; Check that we are not hiding bindings which don't exist
2065 (for-each (lambda (binding)
2066 (if (not (module-local-variable public-i binding))
2067 (error
2068 (simple-format
2069 #f "no binding `~A' to hide in module ~A"
2070 binding name))))
2071 hide)
2072 custom-i))))
2073
2074 (define (symbol-prefix-proc prefix)
2075 (lambda (symbol)
2076 (symbol-append prefix symbol)))
2077
2078 ;; This function is called from "modules.c". If you change it, be
2079 ;; sure to update "modules.c" as well.
2080
2081 (define (process-define-module args)
2082 (let* ((module-id (car args))
2083 (module (resolve-module module-id #f))
2084 (kws (cdr args))
2085 (unrecognized (lambda (arg)
2086 (error "unrecognized define-module argument" arg))))
2087 (beautify-user-module! module)
2088 (let loop ((kws kws)
2089 (reversed-interfaces '())
2090 (exports '())
2091 (re-exports '())
2092 (replacements '())
2093 (autoloads '()))
2094
2095 (if (null? kws)
2096 (call-with-deferred-observers
2097 (lambda ()
2098 (module-use-interfaces! module (reverse reversed-interfaces))
2099 (module-export! module exports)
2100 (module-replace! module replacements)
2101 (module-re-export! module re-exports)
2102 (if (not (null? autoloads))
2103 (apply module-autoload! module autoloads))))
2104 (case (car kws)
2105 ((#:use-module #:use-syntax)
2106 (or (pair? (cdr kws))
2107 (unrecognized kws))
2108 (cond
2109 ((equal? (caadr kws) '(ice-9 syncase))
2110 (issue-deprecation-warning
2111 "(ice-9 syncase) is deprecated. Support for syntax-case is now in Guile core.")
2112 (loop (cddr kws)
2113 reversed-interfaces
2114 exports
2115 re-exports
2116 replacements
2117 autoloads))
2118 (else
2119 (let* ((interface-args (cadr kws))
2120 (interface (apply resolve-interface interface-args)))
2121 (and (eq? (car kws) #:use-syntax)
2122 (or (symbol? (caar interface-args))
2123 (error "invalid module name for use-syntax"
2124 (car interface-args)))
2125 (set-module-transformer!
2126 module
2127 (module-ref interface
2128 (car (last-pair (car interface-args)))
2129 #f)))
2130 (loop (cddr kws)
2131 (cons interface reversed-interfaces)
2132 exports
2133 re-exports
2134 replacements
2135 autoloads)))))
2136 ((#:autoload)
2137 (or (and (pair? (cdr kws)) (pair? (cddr kws)))
2138 (unrecognized kws))
2139 (loop (cdddr kws)
2140 reversed-interfaces
2141 exports
2142 re-exports
2143 replacements
2144 (let ((name (cadr kws))
2145 (bindings (caddr kws)))
2146 (cons* name bindings autoloads))))
2147 ((#:no-backtrace)
2148 (set-system-module! module #t)
2149 (loop (cdr kws) reversed-interfaces exports re-exports
2150 replacements autoloads))
2151 ((#:pure)
2152 (purify-module! module)
2153 (loop (cdr kws) reversed-interfaces exports re-exports
2154 replacements autoloads))
2155 ((#:duplicates)
2156 (if (not (pair? (cdr kws)))
2157 (unrecognized kws))
2158 (set-module-duplicates-handlers!
2159 module
2160 (lookup-duplicates-handlers (cadr kws)))
2161 (loop (cddr kws) reversed-interfaces exports re-exports
2162 replacements autoloads))
2163 ((#:export #:export-syntax)
2164 (or (pair? (cdr kws))
2165 (unrecognized kws))
2166 (loop (cddr kws)
2167 reversed-interfaces
2168 (append (cadr kws) exports)
2169 re-exports
2170 replacements
2171 autoloads))
2172 ((#:re-export #:re-export-syntax)
2173 (or (pair? (cdr kws))
2174 (unrecognized kws))
2175 (loop (cddr kws)
2176 reversed-interfaces
2177 exports
2178 (append (cadr kws) re-exports)
2179 replacements
2180 autoloads))
2181 ((#:replace #:replace-syntax)
2182 (or (pair? (cdr kws))
2183 (unrecognized kws))
2184 (loop (cddr kws)
2185 reversed-interfaces
2186 exports
2187 re-exports
2188 (append (cadr kws) replacements)
2189 autoloads))
2190 (else
2191 (unrecognized kws)))))
2192 (run-hook module-defined-hook module)
2193 module))
2194
2195 ;; `module-defined-hook' is a hook that is run whenever a new module
2196 ;; is defined. Its members are called with one argument, the new
2197 ;; module.
2198 (define module-defined-hook (make-hook 1))
2199
2200 \f
2201
2202 ;;; {Autoload}
2203 ;;;
2204
2205 (define (make-autoload-interface module name bindings)
2206 (let ((b (lambda (a sym definep)
2207 (and (memq sym bindings)
2208 (let ((i (module-public-interface (resolve-module name))))
2209 (if (not i)
2210 (error "missing interface for module" name))
2211 (let ((autoload (memq a (module-uses module))))
2212 ;; Replace autoload-interface with actual interface if
2213 ;; that has not happened yet.
2214 (if (pair? autoload)
2215 (set-car! autoload i)))
2216 (module-local-variable i sym))))))
2217 (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f
2218 (make-hash-table 0) '() (make-weak-value-hash-table 31))))
2219
2220 (define (module-autoload! module . args)
2221 "Have @var{module} automatically load the module named @var{name} when one
2222 of the symbols listed in @var{bindings} is looked up. @var{args} should be a
2223 list of module-name/binding-list pairs, e.g., as in @code{(module-autoload!
2224 module '(ice-9 q) '(make-q q-length))}."
2225 (let loop ((args args))
2226 (cond ((null? args)
2227 #t)
2228 ((null? (cdr args))
2229 (error "invalid name+binding autoload list" args))
2230 (else
2231 (let ((name (car args))
2232 (bindings (cadr args)))
2233 (module-use! module (make-autoload-interface module
2234 name bindings))
2235 (loop (cddr args)))))))
2236
2237
2238 \f
2239
2240 ;;; {Autoloading modules}
2241 ;;;
2242
2243 (define autoloads-in-progress '())
2244
2245 ;; This function is called from "modules.c". If you change it, be
2246 ;; sure to update "modules.c" as well.
2247
2248 (define (try-module-autoload module-name)
2249 (let* ((reverse-name (reverse module-name))
2250 (name (symbol->string (car reverse-name)))
2251 (dir-hint-module-name (reverse (cdr reverse-name)))
2252 (dir-hint (apply string-append
2253 (map (lambda (elt)
2254 (string-append (symbol->string elt) "/"))
2255 dir-hint-module-name))))
2256 (resolve-module dir-hint-module-name #f)
2257 (and (not (autoload-done-or-in-progress? dir-hint name))
2258 (let ((didit #f))
2259 (define (load-file proc file)
2260 (save-module-excursion (lambda () (proc file)))
2261 (set! didit #t))
2262 (dynamic-wind
2263 (lambda () (autoload-in-progress! dir-hint name))
2264 (lambda ()
2265 (let ((file (in-vicinity dir-hint name)))
2266 (let ((compiled (and load-compiled
2267 (%search-load-path
2268 (string-append file ".go"))))
2269 (source (%search-load-path file)))
2270 (cond ((and source
2271 (or (not compiled)
2272 (< (stat:mtime (stat compiled))
2273 (stat:mtime (stat source)))))
2274 (if compiled
2275 (warn "source file" source "newer than" compiled))
2276 (with-fluid* current-reader #f
2277 (lambda () (load-file primitive-load source))))
2278 (compiled
2279 (load-file load-compiled compiled))))))
2280 (lambda () (set-autoloaded! dir-hint name didit)))
2281 didit))))
2282
2283 \f
2284
2285 ;;; {Dynamic linking of modules}
2286 ;;;
2287
2288 (define autoloads-done '((guile . guile)))
2289
2290 (define (autoload-done-or-in-progress? p m)
2291 (let ((n (cons p m)))
2292 (->bool (or (member n autoloads-done)
2293 (member n autoloads-in-progress)))))
2294
2295 (define (autoload-done! p m)
2296 (let ((n (cons p m)))
2297 (set! autoloads-in-progress
2298 (delete! n autoloads-in-progress))
2299 (or (member n autoloads-done)
2300 (set! autoloads-done (cons n autoloads-done)))))
2301
2302 (define (autoload-in-progress! p m)
2303 (let ((n (cons p m)))
2304 (set! autoloads-done
2305 (delete! n autoloads-done))
2306 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2307
2308 (define (set-autoloaded! p m done?)
2309 (if done?
2310 (autoload-done! p m)
2311 (let ((n (cons p m)))
2312 (set! autoloads-done (delete! n autoloads-done))
2313 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2314
2315 \f
2316
2317 ;;; {Run-time options}
2318 ;;;
2319
2320 (defmacro define-option-interface (option-group)
2321 (let* ((option-name car)
2322 (option-value cadr)
2323 (option-documentation caddr)
2324
2325 ;; Below follow the macros defining the run-time option interfaces.
2326
2327 (make-options (lambda (interface)
2328 `(lambda args
2329 (cond ((null? args) (,interface))
2330 ((list? (car args))
2331 (,interface (car args)) (,interface))
2332 (else (for-each
2333 (lambda (option)
2334 (display (option-name option))
2335 (if (< (string-length
2336 (symbol->string (option-name option)))
2337 8)
2338 (display #\tab))
2339 (display #\tab)
2340 (display (option-value option))
2341 (display #\tab)
2342 (display (option-documentation option))
2343 (newline))
2344 (,interface #t)))))))
2345
2346 (make-enable (lambda (interface)
2347 `(lambda flags
2348 (,interface (append flags (,interface)))
2349 (,interface))))
2350
2351 (make-disable (lambda (interface)
2352 `(lambda flags
2353 (let ((options (,interface)))
2354 (for-each (lambda (flag)
2355 (set! options (delq! flag options)))
2356 flags)
2357 (,interface options)
2358 (,interface))))))
2359 (let* ((interface (car option-group))
2360 (options/enable/disable (cadr option-group)))
2361 `(begin
2362 (define ,(car options/enable/disable)
2363 ,(make-options interface))
2364 (define ,(cadr options/enable/disable)
2365 ,(make-enable interface))
2366 (define ,(caddr options/enable/disable)
2367 ,(make-disable interface))
2368 (defmacro ,(caaddr option-group) (opt val)
2369 `(,',(car options/enable/disable)
2370 (append (,',(car options/enable/disable))
2371 (list ',opt ,val))))))))
2372
2373 (define-option-interface
2374 (eval-options-interface
2375 (eval-options eval-enable eval-disable)
2376 (eval-set!)))
2377
2378 (define-option-interface
2379 (debug-options-interface
2380 (debug-options debug-enable debug-disable)
2381 (debug-set!)))
2382
2383 (define-option-interface
2384 (evaluator-traps-interface
2385 (traps trap-enable trap-disable)
2386 (trap-set!)))
2387
2388 (define-option-interface
2389 (read-options-interface
2390 (read-options read-enable read-disable)
2391 (read-set!)))
2392
2393 (define-option-interface
2394 (print-options-interface
2395 (print-options print-enable print-disable)
2396 (print-set!)))
2397
2398 \f
2399
2400 ;;; {Running Repls}
2401 ;;;
2402
2403 (define (repl read evaler print)
2404 (let loop ((source (read (current-input-port))))
2405 (print (evaler source))
2406 (loop (read (current-input-port)))))
2407
2408 ;; A provisional repl that acts like the SCM repl:
2409 ;;
2410 (define scm-repl-silent #f)
2411 (define (assert-repl-silence v) (set! scm-repl-silent v))
2412
2413 (define *unspecified* (if #f #f))
2414 (define (unspecified? v) (eq? v *unspecified*))
2415
2416 (define scm-repl-print-unspecified #f)
2417 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2418
2419 (define scm-repl-verbose #f)
2420 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2421
2422 (define scm-repl-prompt "guile> ")
2423
2424 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2425
2426 (define (default-pre-unwind-handler key . args)
2427 (save-stack pre-unwind-handler-dispatch)
2428 (apply throw key args))
2429
2430 (define (pre-unwind-handler-dispatch key . args)
2431 (apply default-pre-unwind-handler key args))
2432
2433 (define abort-hook (make-hook))
2434
2435 ;; these definitions are used if running a script.
2436 ;; otherwise redefined in error-catching-loop.
2437 (define (set-batch-mode?! arg) #t)
2438 (define (batch-mode?) #t)
2439
2440 (define (error-catching-loop thunk)
2441 (let ((status #f)
2442 (interactive #t))
2443 (define (loop first)
2444 (let ((next
2445 (catch #t
2446
2447 (lambda ()
2448 (call-with-unblocked-asyncs
2449 (lambda ()
2450 (with-traps
2451 (lambda ()
2452 (first)
2453
2454 ;; This line is needed because mark
2455 ;; doesn't do closures quite right.
2456 ;; Unreferenced locals should be
2457 ;; collected.
2458 (set! first #f)
2459 (let loop ((v (thunk)))
2460 (loop (thunk)))
2461 #f)))))
2462
2463 (lambda (key . args)
2464 (case key
2465 ((quit)
2466 (set! status args)
2467 #f)
2468
2469 ((switch-repl)
2470 (apply throw 'switch-repl args))
2471
2472 ((abort)
2473 ;; This is one of the closures that require
2474 ;; (set! first #f) above
2475 ;;
2476 (lambda ()
2477 (run-hook abort-hook)
2478 (force-output (current-output-port))
2479 (display "ABORT: " (current-error-port))
2480 (write args (current-error-port))
2481 (newline (current-error-port))
2482 (if interactive
2483 (begin
2484 (if (and
2485 (not has-shown-debugger-hint?)
2486 (not (memq 'backtrace
2487 (debug-options-interface)))
2488 (stack? (fluid-ref the-last-stack)))
2489 (begin
2490 (newline (current-error-port))
2491 (display
2492 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2493 (current-error-port))
2494 (set! has-shown-debugger-hint? #t)))
2495 (force-output (current-error-port)))
2496 (begin
2497 (primitive-exit 1)))
2498 (set! stack-saved? #f)))
2499
2500 (else
2501 ;; This is the other cons-leak closure...
2502 (lambda ()
2503 (cond ((= (length args) 4)
2504 (apply handle-system-error key args))
2505 (else
2506 (apply bad-throw key args)))))))
2507
2508 ;; Note that having just `pre-unwind-handler-dispatch'
2509 ;; here is connected with the mechanism that
2510 ;; produces a nice backtrace upon error. If, for
2511 ;; example, this is replaced with (lambda args
2512 ;; (apply pre-unwind-handler-dispatch args)), the stack
2513 ;; cutting (in save-stack) goes wrong and ends up
2514 ;; saving no stack at all, so there is no
2515 ;; backtrace.
2516 pre-unwind-handler-dispatch)))
2517
2518 (if next (loop next) status)))
2519 (set! set-batch-mode?! (lambda (arg)
2520 (cond (arg
2521 (set! interactive #f)
2522 (restore-signals))
2523 (#t
2524 (error "sorry, not implemented")))))
2525 (set! batch-mode? (lambda () (not interactive)))
2526 (call-with-blocked-asyncs
2527 (lambda () (loop (lambda () #t))))))
2528
2529 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2530 (define before-signal-stack (make-fluid))
2531 (define stack-saved? #f)
2532
2533 (define (save-stack . narrowing)
2534 (or stack-saved?
2535 (cond ((not (memq 'debug (debug-options-interface)))
2536 (fluid-set! the-last-stack #f)
2537 (set! stack-saved? #t))
2538 (else
2539 (fluid-set!
2540 the-last-stack
2541 (case (stack-id #t)
2542 ((repl-stack)
2543 (apply make-stack #t save-stack primitive-eval #t 0 narrowing))
2544 ((load-stack)
2545 (apply make-stack #t save-stack 0 #t 0 narrowing))
2546 ((tk-stack)
2547 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2548 ((#t)
2549 (apply make-stack #t save-stack 0 1 narrowing))
2550 (else
2551 (let ((id (stack-id #t)))
2552 (and (procedure? id)
2553 (apply make-stack #t save-stack id #t 0 narrowing))))))
2554 (set! stack-saved? #t)))))
2555
2556 (define before-error-hook (make-hook))
2557 (define after-error-hook (make-hook))
2558 (define before-backtrace-hook (make-hook))
2559 (define after-backtrace-hook (make-hook))
2560
2561 (define has-shown-debugger-hint? #f)
2562
2563 (define (handle-system-error key . args)
2564 (let ((cep (current-error-port)))
2565 (cond ((not (stack? (fluid-ref the-last-stack))))
2566 ((memq 'backtrace (debug-options-interface))
2567 (let ((highlights (if (or (eq? key 'wrong-type-arg)
2568 (eq? key 'out-of-range))
2569 (list-ref args 3)
2570 '())))
2571 (run-hook before-backtrace-hook)
2572 (newline cep)
2573 (display "Backtrace:\n")
2574 (display-backtrace (fluid-ref the-last-stack) cep
2575 #f #f highlights)
2576 (newline cep)
2577 (run-hook after-backtrace-hook))))
2578 (run-hook before-error-hook)
2579 (apply display-error (fluid-ref the-last-stack) cep args)
2580 (run-hook after-error-hook)
2581 (force-output cep)
2582 (throw 'abort key)))
2583
2584 (define (quit . args)
2585 (apply throw 'quit args))
2586
2587 (define exit quit)
2588
2589 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2590
2591 ;; Replaced by C code:
2592 ;;(define (backtrace)
2593 ;; (if (fluid-ref the-last-stack)
2594 ;; (begin
2595 ;; (newline)
2596 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2597 ;; (newline)
2598 ;; (if (and (not has-shown-backtrace-hint?)
2599 ;; (not (memq 'backtrace (debug-options-interface))))
2600 ;; (begin
2601 ;; (display
2602 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2603 ;;automatically if an error occurs in the future.\n")
2604 ;; (set! has-shown-backtrace-hint? #t))))
2605 ;; (display "No backtrace available.\n")))
2606
2607 (define (error-catching-repl r e p)
2608 (error-catching-loop
2609 (lambda ()
2610 (call-with-values (lambda () (e (r)))
2611 (lambda the-values (for-each p the-values))))))
2612
2613 (define (gc-run-time)
2614 (cdr (assq 'gc-time-taken (gc-stats))))
2615
2616 (define before-read-hook (make-hook))
2617 (define after-read-hook (make-hook))
2618 (define before-eval-hook (make-hook 1))
2619 (define after-eval-hook (make-hook 1))
2620 (define before-print-hook (make-hook 1))
2621 (define after-print-hook (make-hook 1))
2622
2623 ;;; The default repl-reader function. We may override this if we've
2624 ;;; the readline library.
2625 (define repl-reader
2626 (lambda (prompt)
2627 (display (if (string? prompt) prompt (prompt)))
2628 (force-output)
2629 (run-hook before-read-hook)
2630 ((or (fluid-ref current-reader) read) (current-input-port))))
2631
2632 (define (scm-style-repl)
2633
2634 (letrec (
2635 (start-gc-rt #f)
2636 (start-rt #f)
2637 (repl-report-start-timing (lambda ()
2638 (set! start-gc-rt (gc-run-time))
2639 (set! start-rt (get-internal-run-time))))
2640 (repl-report (lambda ()
2641 (display ";;; ")
2642 (display (inexact->exact
2643 (* 1000 (/ (- (get-internal-run-time) start-rt)
2644 internal-time-units-per-second))))
2645 (display " msec (")
2646 (display (inexact->exact
2647 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2648 internal-time-units-per-second))))
2649 (display " msec in gc)\n")))
2650
2651 (consume-trailing-whitespace
2652 (lambda ()
2653 (let ((ch (peek-char)))
2654 (cond
2655 ((eof-object? ch))
2656 ((or (char=? ch #\space) (char=? ch #\tab))
2657 (read-char)
2658 (consume-trailing-whitespace))
2659 ((char=? ch #\newline)
2660 (read-char))))))
2661 (-read (lambda ()
2662 (let ((val
2663 (let ((prompt (cond ((string? scm-repl-prompt)
2664 scm-repl-prompt)
2665 ((thunk? scm-repl-prompt)
2666 (scm-repl-prompt))
2667 (scm-repl-prompt "> ")
2668 (else ""))))
2669 (repl-reader prompt))))
2670
2671 ;; As described in R4RS, the READ procedure updates the
2672 ;; port to point to the first character past the end of
2673 ;; the external representation of the object. This
2674 ;; means that it doesn't consume the newline typically
2675 ;; found after an expression. This means that, when
2676 ;; debugging Guile with GDB, GDB gets the newline, which
2677 ;; it often interprets as a "continue" command, making
2678 ;; breakpoints kind of useless. So, consume any
2679 ;; trailing newline here, as well as any whitespace
2680 ;; before it.
2681 ;; But not if EOF, for control-D.
2682 (if (not (eof-object? val))
2683 (consume-trailing-whitespace))
2684 (run-hook after-read-hook)
2685 (if (eof-object? val)
2686 (begin
2687 (repl-report-start-timing)
2688 (if scm-repl-verbose
2689 (begin
2690 (newline)
2691 (display ";;; EOF -- quitting")
2692 (newline)))
2693 (quit 0)))
2694 val)))
2695
2696 (-eval (lambda (sourc)
2697 (repl-report-start-timing)
2698 (run-hook before-eval-hook sourc)
2699 (let ((val (start-stack 'repl-stack
2700 ;; If you change this procedure
2701 ;; (primitive-eval), please also
2702 ;; modify the repl-stack case in
2703 ;; save-stack so that stack cutting
2704 ;; continues to work.
2705 (primitive-eval sourc))))
2706 (run-hook after-eval-hook sourc)
2707 val)))
2708
2709
2710 (-print (let ((maybe-print (lambda (result)
2711 (if (or scm-repl-print-unspecified
2712 (not (unspecified? result)))
2713 (begin
2714 (write result)
2715 (newline))))))
2716 (lambda (result)
2717 (if (not scm-repl-silent)
2718 (begin
2719 (run-hook before-print-hook result)
2720 (maybe-print result)
2721 (run-hook after-print-hook result)
2722 (if scm-repl-verbose
2723 (repl-report))
2724 (force-output))))))
2725
2726 (-quit (lambda (args)
2727 (if scm-repl-verbose
2728 (begin
2729 (display ";;; QUIT executed, repl exitting")
2730 (newline)
2731 (repl-report)))
2732 args))
2733
2734 (-abort (lambda ()
2735 (if scm-repl-verbose
2736 (begin
2737 (display ";;; ABORT executed.")
2738 (newline)
2739 (repl-report)))
2740 (repl -read -eval -print))))
2741
2742 (let ((status (error-catching-repl -read
2743 -eval
2744 -print)))
2745 (-quit status))))
2746
2747
2748 \f
2749
2750 ;;; {IOTA functions: generating lists of numbers}
2751 ;;;
2752
2753 (define (iota n)
2754 (let loop ((count (1- n)) (result '()))
2755 (if (< count 0) result
2756 (loop (1- count) (cons count result)))))
2757
2758 \f
2759
2760 ;;; {collect}
2761 ;;;
2762 ;;; Similar to `begin' but returns a list of the results of all constituent
2763 ;;; forms instead of the result of the last form.
2764 ;;; (The definition relies on the current left-to-right
2765 ;;; order of evaluation of operands in applications.)
2766 ;;;
2767
2768 (defmacro collect forms
2769 (cons 'list forms))
2770
2771 \f
2772
2773 ;;; {with-fluids}
2774 ;;;
2775
2776 ;; with-fluids is a convenience wrapper for the builtin procedure
2777 ;; `with-fluids*'. The syntax is just like `let':
2778 ;;
2779 ;; (with-fluids ((fluid val)
2780 ;; ...)
2781 ;; body)
2782
2783 (defmacro with-fluids (bindings . body)
2784 (let ((fluids (map car bindings))
2785 (values (map cadr bindings)))
2786 (if (and (= (length fluids) 1) (= (length values) 1))
2787 `(with-fluid* ,(car fluids) ,(car values) (lambda () ,@body))
2788 `(with-fluids* (list ,@fluids) (list ,@values)
2789 (lambda () ,@body)))))
2790
2791 ;;; {While}
2792 ;;;
2793 ;;; with `continue' and `break'.
2794 ;;;
2795
2796 ;; The inner `do' loop avoids re-establishing a catch every iteration,
2797 ;; that's only necessary if continue is actually used. A new key is
2798 ;; generated every time, so break and continue apply to their originating
2799 ;; `while' even when recursing.
2800 ;;
2801 ;; FIXME: This macro is unintentionally unhygienic with respect to let,
2802 ;; make-symbol, do, throw, catch, lambda, and not.
2803 ;;
2804 (define-macro (while cond . body)
2805 (let ((keyvar (make-symbol "while-keyvar")))
2806 `(let ((,keyvar (make-symbol "while-key")))
2807 (do ()
2808 ((catch ,keyvar
2809 (lambda ()
2810 (let ((break (lambda () (throw ,keyvar #t)))
2811 (continue (lambda () (throw ,keyvar #f))))
2812 (do ()
2813 ((not ,cond))
2814 ,@body)
2815 #t))
2816 (lambda (key arg)
2817 arg)))))))
2818
2819
2820 \f
2821
2822 ;;; {Module System Macros}
2823 ;;;
2824
2825 ;; Return a list of expressions that evaluate to the appropriate
2826 ;; arguments for resolve-interface according to SPEC.
2827
2828 (eval-when
2829 (compile)
2830 (if (memq 'prefix (read-options))
2831 (error "boot-9 must be compiled with #:kw, not :kw")))
2832
2833 (define (compile-interface-spec spec)
2834 (define (make-keyarg sym key quote?)
2835 (cond ((or (memq sym spec)
2836 (memq key spec))
2837 => (lambda (rest)
2838 (if quote?
2839 (list key (list 'quote (cadr rest)))
2840 (list key (cadr rest)))))
2841 (else
2842 '())))
2843 (define (map-apply func list)
2844 (map (lambda (args) (apply func args)) list))
2845 (define keys
2846 ;; sym key quote?
2847 '((:select #:select #t)
2848 (:hide #:hide #t)
2849 (:prefix #:prefix #t)
2850 (:renamer #:renamer #f)))
2851 (if (not (pair? (car spec)))
2852 `(',spec)
2853 `(',(car spec)
2854 ,@(apply append (map-apply make-keyarg keys)))))
2855
2856 (define (keyword-like-symbol->keyword sym)
2857 (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
2858
2859 (define (compile-define-module-args args)
2860 ;; Just quote everything except #:use-module and #:use-syntax. We
2861 ;; need to know about all arguments regardless since we want to turn
2862 ;; symbols that look like keywords into real keywords, and the
2863 ;; keyword args in a define-module form are not regular
2864 ;; (i.e. no-backtrace doesn't take a value).
2865 (let loop ((compiled-args `((quote ,(car args))))
2866 (args (cdr args)))
2867 (cond ((null? args)
2868 (reverse! compiled-args))
2869 ;; symbol in keyword position
2870 ((symbol? (car args))
2871 (loop compiled-args
2872 (cons (keyword-like-symbol->keyword (car args)) (cdr args))))
2873 ((memq (car args) '(#:no-backtrace #:pure))
2874 (loop (cons (car args) compiled-args)
2875 (cdr args)))
2876 ((null? (cdr args))
2877 (error "keyword without value:" (car args)))
2878 ((memq (car args) '(#:use-module #:use-syntax))
2879 (loop (cons* `(list ,@(compile-interface-spec (cadr args)))
2880 (car args)
2881 compiled-args)
2882 (cddr args)))
2883 ((eq? (car args) #:autoload)
2884 (loop (cons* `(quote ,(caddr args))
2885 `(quote ,(cadr args))
2886 (car args)
2887 compiled-args)
2888 (cdddr args)))
2889 (else
2890 (loop (cons* `(quote ,(cadr args))
2891 (car args)
2892 compiled-args)
2893 (cddr args))))))
2894
2895 (defmacro define-module args
2896 `(eval-when
2897 (eval load compile)
2898 (let ((m (process-define-module
2899 (list ,@(compile-define-module-args args)))))
2900 (set-current-module m)
2901 m)))
2902
2903 ;; The guts of the use-modules macro. Add the interfaces of the named
2904 ;; modules to the use-list of the current module, in order.
2905
2906 ;; This function is called by "modules.c". If you change it, be sure
2907 ;; to change scm_c_use_module as well.
2908
2909 (define (process-use-modules module-interface-args)
2910 (let ((interfaces (map (lambda (mif-args)
2911 (or (apply resolve-interface mif-args)
2912 (error "no such module" mif-args)))
2913 module-interface-args)))
2914 (call-with-deferred-observers
2915 (lambda ()
2916 (module-use-interfaces! (current-module) interfaces)))))
2917
2918 (defmacro use-modules modules
2919 `(eval-when
2920 (eval load compile)
2921 (process-use-modules
2922 (list ,@(map (lambda (m)
2923 `(list ,@(compile-interface-spec m)))
2924 modules)))
2925 *unspecified*))
2926
2927 (defmacro use-syntax (spec)
2928 `(eval-when
2929 (eval load compile)
2930 (issue-deprecation-warning
2931 "`use-syntax' is deprecated. Please contact guile-devel for more info.")
2932 (process-use-modules (list (list ,@(compile-interface-spec spec))))
2933 *unspecified*))
2934
2935 ;; Dirk:FIXME:: This incorrect (according to R5RS) syntax needs to be changed
2936 ;; as soon as guile supports hygienic macros.
2937 (define-syntax define-private
2938 (syntax-rules ()
2939 ((_ foo bar)
2940 (define foo bar))))
2941
2942 (define-syntax define-public
2943 (syntax-rules ()
2944 ((_ (name . args) . body)
2945 (define-public name (lambda args . body)))
2946 ((_ name val)
2947 (begin
2948 (define name val)
2949 (export name)))))
2950
2951 (define-syntax defmacro-public
2952 (syntax-rules ()
2953 ((_ name args . body)
2954 (begin
2955 (defmacro name args . body)
2956 (export-syntax name)))))
2957
2958 ;; Export a local variable
2959
2960 ;; This function is called from "modules.c". If you change it, be
2961 ;; sure to update "modules.c" as well.
2962
2963 (define (module-export! m names)
2964 (let ((public-i (module-public-interface m)))
2965 (for-each (lambda (name)
2966 (let ((var (module-ensure-local-variable! m name)))
2967 (module-add! public-i name var)))
2968 names)))
2969
2970 (define (module-replace! m names)
2971 (let ((public-i (module-public-interface m)))
2972 (for-each (lambda (name)
2973 (let ((var (module-ensure-local-variable! m name)))
2974 (set-object-property! var 'replace #t)
2975 (module-add! public-i name var)))
2976 names)))
2977
2978 ;; Re-export a imported variable
2979 ;;
2980 (define (module-re-export! m names)
2981 (let ((public-i (module-public-interface m)))
2982 (for-each (lambda (name)
2983 (let ((var (module-variable m name)))
2984 (cond ((not var)
2985 (error "Undefined variable:" name))
2986 ((eq? var (module-local-variable m name))
2987 (error "re-exporting local variable:" name))
2988 (else
2989 (module-add! public-i name var)))))
2990 names)))
2991
2992 (defmacro export names
2993 `(call-with-deferred-observers
2994 (lambda ()
2995 (module-export! (current-module) ',names))))
2996
2997 (defmacro re-export names
2998 `(call-with-deferred-observers
2999 (lambda ()
3000 (module-re-export! (current-module) ',names))))
3001
3002 (defmacro export-syntax names
3003 `(export ,@names))
3004
3005 (defmacro re-export-syntax names
3006 `(re-export ,@names))
3007
3008 (define load load-module)
3009
3010 \f
3011
3012 ;;; {Parameters}
3013 ;;;
3014
3015 (define make-mutable-parameter
3016 (let ((make (lambda (fluid converter)
3017 (lambda args
3018 (if (null? args)
3019 (fluid-ref fluid)
3020 (fluid-set! fluid (converter (car args))))))))
3021 (lambda (init . converter)
3022 (let ((fluid (make-fluid))
3023 (converter (if (null? converter)
3024 identity
3025 (car converter))))
3026 (fluid-set! fluid (converter init))
3027 (make fluid converter)))))
3028
3029 \f
3030
3031 ;;; {Handling of duplicate imported bindings}
3032 ;;;
3033
3034 ;; Duplicate handlers take the following arguments:
3035 ;;
3036 ;; module importing module
3037 ;; name conflicting name
3038 ;; int1 old interface where name occurs
3039 ;; val1 value of binding in old interface
3040 ;; int2 new interface where name occurs
3041 ;; val2 value of binding in new interface
3042 ;; var previous resolution or #f
3043 ;; val value of previous resolution
3044 ;;
3045 ;; A duplicate handler can take three alternative actions:
3046 ;;
3047 ;; 1. return #f => leave responsibility to next handler
3048 ;; 2. exit with an error
3049 ;; 3. return a variable resolving the conflict
3050 ;;
3051
3052 (define duplicate-handlers
3053 (let ((m (make-module 7)))
3054
3055 (define (check module name int1 val1 int2 val2 var val)
3056 (scm-error 'misc-error
3057 #f
3058 "~A: `~A' imported from both ~A and ~A"
3059 (list (module-name module)
3060 name
3061 (module-name int1)
3062 (module-name int2))
3063 #f))
3064
3065 (define (warn module name int1 val1 int2 val2 var val)
3066 (format (current-error-port)
3067 "WARNING: ~A: `~A' imported from both ~A and ~A\n"
3068 (module-name module)
3069 name
3070 (module-name int1)
3071 (module-name int2))
3072 #f)
3073
3074 (define (replace module name int1 val1 int2 val2 var val)
3075 (let ((old (or (and var (object-property var 'replace) var)
3076 (module-variable int1 name)))
3077 (new (module-variable int2 name)))
3078 (if (object-property old 'replace)
3079 (and (or (eq? old new)
3080 (not (object-property new 'replace)))
3081 old)
3082 (and (object-property new 'replace)
3083 new))))
3084
3085 (define (warn-override-core module name int1 val1 int2 val2 var val)
3086 (and (eq? int1 the-scm-module)
3087 (begin
3088 (format (current-error-port)
3089 "WARNING: ~A: imported module ~A overrides core binding `~A'\n"
3090 (module-name module)
3091 (module-name int2)
3092 name)
3093 (module-local-variable int2 name))))
3094
3095 (define (first module name int1 val1 int2 val2 var val)
3096 (or var (module-local-variable int1 name)))
3097
3098 (define (last module name int1 val1 int2 val2 var val)
3099 (module-local-variable int2 name))
3100
3101 (define (noop module name int1 val1 int2 val2 var val)
3102 #f)
3103
3104 (set-module-name! m 'duplicate-handlers)
3105 (set-module-kind! m 'interface)
3106 (module-define! m 'check check)
3107 (module-define! m 'warn warn)
3108 (module-define! m 'replace replace)
3109 (module-define! m 'warn-override-core warn-override-core)
3110 (module-define! m 'first first)
3111 (module-define! m 'last last)
3112 (module-define! m 'merge-generics noop)
3113 (module-define! m 'merge-accessors noop)
3114 m))
3115
3116 (define (lookup-duplicates-handlers handler-names)
3117 (and handler-names
3118 (map (lambda (handler-name)
3119 (or (module-symbol-local-binding
3120 duplicate-handlers handler-name #f)
3121 (error "invalid duplicate handler name:"
3122 handler-name)))
3123 (if (list? handler-names)
3124 handler-names
3125 (list handler-names)))))
3126
3127 (define default-duplicate-binding-procedures
3128 (make-mutable-parameter #f))
3129
3130 (define default-duplicate-binding-handler
3131 (make-mutable-parameter '(replace warn-override-core warn last)
3132 (lambda (handler-names)
3133 (default-duplicate-binding-procedures
3134 (lookup-duplicates-handlers handler-names))
3135 handler-names)))
3136
3137 \f
3138
3139 ;;; {`cond-expand' for SRFI-0 support.}
3140 ;;;
3141 ;;; This syntactic form expands into different commands or
3142 ;;; definitions, depending on the features provided by the Scheme
3143 ;;; implementation.
3144 ;;;
3145 ;;; Syntax:
3146 ;;;
3147 ;;; <cond-expand>
3148 ;;; --> (cond-expand <cond-expand-clause>+)
3149 ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
3150 ;;; <cond-expand-clause>
3151 ;;; --> (<feature-requirement> <command-or-definition>*)
3152 ;;; <feature-requirement>
3153 ;;; --> <feature-identifier>
3154 ;;; | (and <feature-requirement>*)
3155 ;;; | (or <feature-requirement>*)
3156 ;;; | (not <feature-requirement>)
3157 ;;; <feature-identifier>
3158 ;;; --> <a symbol which is the name or alias of a SRFI>
3159 ;;;
3160 ;;; Additionally, this implementation provides the
3161 ;;; <feature-identifier>s `guile' and `r5rs', so that programs can
3162 ;;; determine the implementation type and the supported standard.
3163 ;;;
3164 ;;; Currently, the following feature identifiers are supported:
3165 ;;;
3166 ;;; guile r5rs srfi-0 srfi-4 srfi-6 srfi-13 srfi-14 srfi-55 srfi-61
3167 ;;;
3168 ;;; Remember to update the features list when adding more SRFIs.
3169 ;;;
3170
3171 (define %cond-expand-features
3172 ;; Adjust the above comment when changing this.
3173 '(guile
3174 r5rs
3175 srfi-0 ;; cond-expand itself
3176 srfi-4 ;; homogenous numeric vectors
3177 srfi-6 ;; open-input-string etc, in the guile core
3178 srfi-13 ;; string library
3179 srfi-14 ;; character sets
3180 srfi-55 ;; require-extension
3181 srfi-61 ;; general cond clause
3182 ))
3183
3184 ;; This table maps module public interfaces to the list of features.
3185 ;;
3186 (define %cond-expand-table (make-hash-table 31))
3187
3188 ;; Add one or more features to the `cond-expand' feature list of the
3189 ;; module `module'.
3190 ;;
3191 (define (cond-expand-provide module features)
3192 (let ((mod (module-public-interface module)))
3193 (and mod
3194 (hashq-set! %cond-expand-table mod
3195 (append (hashq-ref %cond-expand-table mod '())
3196 features)))))
3197
3198 (define cond-expand
3199 (procedure->memoizing-macro
3200 (lambda (exp env)
3201 (let ((clauses (cdr exp))
3202 (syntax-error (lambda (cl)
3203 (error "invalid clause in `cond-expand'" cl))))
3204 (letrec
3205 ((test-clause
3206 (lambda (clause)
3207 (cond
3208 ((symbol? clause)
3209 (or (memq clause %cond-expand-features)
3210 (let lp ((uses (module-uses (env-module env))))
3211 (if (pair? uses)
3212 (or (memq clause
3213 (hashq-ref %cond-expand-table
3214 (car uses) '()))
3215 (lp (cdr uses)))
3216 #f))))
3217 ((pair? clause)
3218 (cond
3219 ((eq? 'and (car clause))
3220 (let lp ((l (cdr clause)))
3221 (cond ((null? l)
3222 #t)
3223 ((pair? l)
3224 (and (test-clause (car l)) (lp (cdr l))))
3225 (else
3226 (syntax-error clause)))))
3227 ((eq? 'or (car clause))
3228 (let lp ((l (cdr clause)))
3229 (cond ((null? l)
3230 #f)
3231 ((pair? l)
3232 (or (test-clause (car l)) (lp (cdr l))))
3233 (else
3234 (syntax-error clause)))))
3235 ((eq? 'not (car clause))
3236 (cond ((not (pair? (cdr clause)))
3237 (syntax-error clause))
3238 ((pair? (cddr clause))
3239 ((syntax-error clause))))
3240 (not (test-clause (cadr clause))))
3241 (else
3242 (syntax-error clause))))
3243 (else
3244 (syntax-error clause))))))
3245 (let lp ((c clauses))
3246 (cond
3247 ((null? c)
3248 (error "Unfulfilled `cond-expand'"))
3249 ((not (pair? c))
3250 (syntax-error c))
3251 ((not (pair? (car c)))
3252 (syntax-error (car c)))
3253 ((test-clause (caar c))
3254 `(begin ,@(cdar c)))
3255 ((eq? (caar c) 'else)
3256 (if (pair? (cdr c))
3257 (syntax-error c))
3258 `(begin ,@(cdar c)))
3259 (else
3260 (lp (cdr c))))))))))
3261
3262 ;; This procedure gets called from the startup code with a list of
3263 ;; numbers, which are the numbers of the SRFIs to be loaded on startup.
3264 ;;
3265 (define (use-srfis srfis)
3266 (process-use-modules
3267 (map (lambda (num)
3268 (list (list 'srfi (string->symbol
3269 (string-append "srfi-" (number->string num))))))
3270 srfis)))
3271
3272 \f
3273
3274 ;;; srfi-55: require-extension
3275 ;;;
3276
3277 (define-macro (require-extension extension-spec)
3278 ;; This macro only handles the srfi extension, which, at present, is
3279 ;; the only one defined by the standard.
3280 (if (not (pair? extension-spec))
3281 (scm-error 'wrong-type-arg "require-extension"
3282 "Not an extension: ~S" (list extension-spec) #f))
3283 (let ((extension (car extension-spec))
3284 (extension-args (cdr extension-spec)))
3285 (case extension
3286 ((srfi)
3287 (let ((use-list '()))
3288 (for-each
3289 (lambda (i)
3290 (if (not (integer? i))
3291 (scm-error 'wrong-type-arg "require-extension"
3292 "Invalid srfi name: ~S" (list i) #f))
3293 (let ((srfi-sym (string->symbol
3294 (string-append "srfi-" (number->string i)))))
3295 (if (not (memq srfi-sym %cond-expand-features))
3296 (set! use-list (cons `(use-modules (srfi ,srfi-sym))
3297 use-list)))))
3298 extension-args)
3299 (if (pair? use-list)
3300 ;; i.e. (begin (use-modules x) (use-modules y) (use-modules z))
3301 `(begin ,@(reverse! use-list)))))
3302 (else
3303 (scm-error
3304 'wrong-type-arg "require-extension"
3305 "Not a recognized extension type: ~S" (list extension) #f)))))
3306
3307 \f
3308
3309 ;;; {Load emacs interface support if emacs option is given.}
3310 ;;;
3311
3312 (define (named-module-use! user usee)
3313 (module-use! (resolve-module user) (resolve-interface usee)))
3314
3315 (define (load-emacs-interface)
3316 (and (provided? 'debug-extensions)
3317 (debug-enable 'backtrace))
3318 (named-module-use! '(guile-user) '(ice-9 emacs)))
3319
3320 \f
3321
3322 (define using-readline?
3323 (let ((using-readline? (make-fluid)))
3324 (make-procedure-with-setter
3325 (lambda () (fluid-ref using-readline?))
3326 (lambda (v) (fluid-set! using-readline? v)))))
3327
3328 (define (top-repl)
3329 (let ((guile-user-module (resolve-module '(guile-user))))
3330
3331 ;; Load emacs interface support if emacs option is given.
3332 (if (and (module-defined? guile-user-module 'use-emacs-interface)
3333 (module-ref guile-user-module 'use-emacs-interface))
3334 (load-emacs-interface))
3335
3336 ;; Use some convenient modules (in reverse order)
3337
3338 (set-current-module guile-user-module)
3339 (process-use-modules
3340 (append
3341 '(((ice-9 r5rs))
3342 ((ice-9 session))
3343 ((ice-9 debug)))
3344 (if (provided? 'regex)
3345 '(((ice-9 regex)))
3346 '())
3347 (if (provided? 'threads)
3348 '(((ice-9 threads)))
3349 '())))
3350 ;; load debugger on demand
3351 (module-autoload! guile-user-module '(ice-9 debugger) '(debug))
3352
3353 ;; Note: SIGFPE, SIGSEGV and SIGBUS are actually "query-only" (see
3354 ;; scmsigs.c scm_sigaction_for_thread), so the handlers setup here have
3355 ;; no effect.
3356 (let ((old-handlers #f)
3357 (start-repl (module-ref (resolve-interface '(system repl repl))
3358 'start-repl))
3359 (signals (if (provided? 'posix)
3360 `((,SIGINT . "User interrupt")
3361 (,SIGFPE . "Arithmetic error")
3362 (,SIGSEGV
3363 . "Bad memory access (Segmentation violation)"))
3364 '())))
3365 ;; no SIGBUS on mingw
3366 (if (defined? 'SIGBUS)
3367 (set! signals (acons SIGBUS "Bad memory access (bus error)"
3368 signals)))
3369
3370 (dynamic-wind
3371
3372 ;; call at entry
3373 (lambda ()
3374 (let ((make-handler (lambda (msg)
3375 (lambda (sig)
3376 ;; Make a backup copy of the stack
3377 (fluid-set! before-signal-stack
3378 (fluid-ref the-last-stack))
3379 (save-stack 2)
3380 (scm-error 'signal
3381 #f
3382 msg
3383 #f
3384 (list sig))))))
3385 (set! old-handlers
3386 (map (lambda (sig-msg)
3387 (sigaction (car sig-msg)
3388 (make-handler (cdr sig-msg))))
3389 signals))))
3390
3391 ;; the protected thunk.
3392 (lambda ()
3393 (let ((status (start-repl 'scheme)))
3394 (run-hook exit-hook)
3395 status))
3396
3397 ;; call at exit.
3398 (lambda ()
3399 (map (lambda (sig-msg old-handler)
3400 (if (not (car old-handler))
3401 ;; restore original C handler.
3402 (sigaction (car sig-msg) #f)
3403 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
3404 (sigaction (car sig-msg)
3405 (car old-handler)
3406 (cdr old-handler))))
3407 signals old-handlers))))))
3408
3409 ;;; This hook is run at the very end of an interactive session.
3410 ;;;
3411 (define exit-hook (make-hook))
3412
3413 \f
3414
3415 ;;; {Deprecated stuff}
3416 ;;;
3417
3418 (begin-deprecated
3419 (define (feature? sym)
3420 (issue-deprecation-warning
3421 "`feature?' is deprecated. Use `provided?' instead.")
3422 (provided? sym)))
3423
3424 (begin-deprecated
3425 (primitive-load-path "ice-9/deprecated"))
3426
3427 \f
3428
3429 ;;; Place the user in the guile-user module.
3430 ;;;
3431
3432 ;;; FIXME: annotate ?
3433 ;; (define (syncase exp)
3434 ;; (with-fluids ((expansion-eval-closure
3435 ;; (module-eval-closure (current-module))))
3436 ;; (deannotate/source-properties (sc-expand (annotate exp)))))
3437
3438 (define-module (guile-user)
3439 #:autoload (system base compile) (compile))
3440
3441 ;;; boot-9.scm ends here