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