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