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