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