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