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