Improve the usage of variable names in Scheme docstrings.
[bpt/guile.git] / module / ice-9 / r4rs.scm
index de2aeb2..072c8c6 100644 (file)
@@ -1,12 +1,12 @@
 ;;;; r4rs.scm --- definitions needed for libguile to be R4RS compliant
 ;;;; Jim Blandy <jimb@cyclic.com> --- October 1996
 
-;;;;   Copyright (C) 1996, 1997, 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
+;;;;   Copyright (C) 1996, 1997, 1998, 2000, 2001, 2006, 2010, 2011 Free Software Foundation, Inc.
 ;;;; 
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
 ;;;; License as published by the Free Software Foundation; either
-;;;; version 2.1 of the License, or (at your option) any later version.
+;;;; version 3 of the License, or (at your option) any later version.
 ;;;; 
 ;;;; This library is distributed in the hope that it will be useful,
 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;;;; License along with this library; if not, write to the Free Software
 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
+(eval-when (compile)
+  (set-current-module (resolve-module '(guile))))
+
 \f
 ;;;; apply and call-with-current-continuation
 
-;;; We want these to be tail-recursive, so instead of using primitive
-;;; procedures, we define them as closures in terms of the primitive
-;;; macros @apply and @call-with-current-continuation.
-(set! apply (lambda (fun . args) (@apply fun (apply:nconc2last args))))
-(set-procedure-property! apply 'name 'apply)
+;;; The deal with these is that they are the procedural wrappers around the
+;;; primitives of Guile's language. There are about 20 different kinds of
+;;; expression in Guile, and e.g. @apply is one of them. (It has to be that way
+;;; to preserve tail recursion.)
+;;;
+;;; Usually we recognize (apply foo bar) to be an instance of @apply, but in the
+;;; case that apply is passed to apply, or we're bootstrapping, we need a
+;;; trampoline -- and here they are.
+(define (apply fun . args)
+  (@apply fun (apply:nconc2last args)))
 (define (call-with-current-continuation proc)
   (@call-with-current-continuation proc))
 (define (call-with-values producer consumer)
   (@call-with-values producer consumer))
+(define (dynamic-wind in thunk out)
+  "All three arguments must be 0-argument procedures.
+Guard @var{in} is called, then @var{thunk}, then
+guard @var{out}.
+
+If, any time during the execution of @var{thunk}, the
+continuation of the @code{dynamic_wind} expression is escaped
+non-locally, @var{out} is called.  If the continuation of
+the dynamic-wind is re-entered, @var{in} is called.  Thus
+@var{in} and @var{out} may be called any number of
+times.
+@lisp
+ (define x 'normal-binding)
+@result{} x
+ (define a-cont
+   (call-with-current-continuation
+     (lambda (escape)
+       (let ((old-x x))
+         (dynamic-wind
+           ;; in-guard:
+           ;;
+           (lambda () (set! x 'special-binding))
+
+           ;; thunk
+           ;;
+           (lambda () (display x) (newline)
+                   (call-with-current-continuation escape)
+                   (display x) (newline)
+                   x)
+
+           ;; out-guard:
+           ;;
+           (lambda () (set! x old-x)))))))
+
+;; Prints:
+special-binding
+;; Evaluates to:
+@result{} a-cont
+x
+@result{} normal-binding
+ (a-cont #f)
+;; Prints:
+special-binding
+;; Evaluates to:
+@result{} a-cont  ;; the value of the (define a-cont...)
+x
+@result{} normal-binding
+a-cont
+@result{} special-binding
+@end lisp"
+  (@dynamic-wind in (thunk) out))
 
 \f
 ;;;; Basic Port Code
@@ -81,14 +140,16 @@ already exist. These procedures call PROC
 with one argument: the port obtained by opening the named file for
 input or output.  If the file cannot be opened, an error is
 signalled.  If the procedure returns, then the port is closed
-automatically and the value yielded by the procedure is returned.
+automatically and the values yielded by the procedure are returned.
 If the procedure does not return, then the port will not be closed
 automatically unless it is possible to prove that the port will
 never again be used for a read or write operation."
-  (let* ((file (open-input-file str))
-        (ans (proc file)))
-    (close-input-port file)
-    ans))
+  (let ((p (open-input-file str)))
+    (call-with-values
+      (lambda () (proc p))
+      (lambda vals
+        (close-input-port p)
+        (apply values vals)))))
 
 (define (call-with-output-file str proc)
   "PROC should be a procedure of one argument, and STR should be a
@@ -97,14 +158,16 @@ already exists. These procedures call PROC
 with one argument: the port obtained by opening the named file for
 input or output.  If the file cannot be opened, an error is
 signalled.  If the procedure returns, then the port is closed
-automatically and the value yielded by the procedure is returned.
+automatically and the values yielded by the procedure are returned.
 If the procedure does not return, then the port will not be closed
 automatically unless it is possible to prove that the port will
 never again be used for a read or write operation."
-  (let* ((file (open-output-file str))
-        (ans (proc file)))
-    (close-output-port file)
-    ans))
+  (let ((p (open-output-file str)))
+    (call-with-values
+      (lambda () (proc p))
+      (lambda vals
+        (close-output-port p)
+        (apply values vals)))))
 
 (define (with-input-from-port port thunk)
   (let* ((swaports (lambda () (set! port (set-current-input-port port)))))
@@ -125,13 +188,11 @@ input, an input port connected to it is made
 the default value returned by `current-input-port', 
 and the THUNK is called with no arguments.
 When the THUNK returns, the port is closed and the previous
-default is restored.  Returns the value yielded by THUNK.  If an
+default is restored.  Returns the values yielded by THUNK.  If an
 escape procedure is used to escape from the continuation of these
 procedures, their behavior is implementation dependent."
-  (let* ((nport (open-input-file file))
-        (ans (with-input-from-port nport thunk)))
-    (close-port nport)
-    ans))
+  (call-with-input-file file
+   (lambda (p) (with-input-from-port p thunk))))
 
 (define (with-output-to-file file thunk)
   "THUNK must be a procedure of no arguments, and FILE must be a
@@ -140,13 +201,11 @@ The file is opened for output, an output port connected to it is made
 the default value returned by `current-output-port', 
 and the THUNK is called with no arguments.
 When the THUNK returns, the port is closed and the previous
-default is restored.  Returns the value yielded by THUNK.  If an
+default is restored.  Returns the values yielded by THUNK.  If an
 escape procedure is used to escape from the continuation of these
 procedures, their behavior is implementation dependent."
-  (let* ((nport (open-output-file file))
-        (ans (with-output-to-port nport thunk)))
-    (close-port nport)
-    ans))
+  (call-with-output-file file
+   (lambda (p) (with-output-to-port p thunk))))
 
 (define (with-error-to-file file thunk)
   "THUNK must be a procedure of no arguments, and FILE must be a
@@ -155,13 +214,11 @@ The file is opened for output, an output port connected to it is made
 the default value returned by `current-error-port', 
 and the THUNK is called with no arguments.
 When the THUNK returns, the port is closed and the previous
-default is restored.  Returns the value yielded by THUNK.  If an
+default is restored.  Returns the values yielded by THUNK.  If an
 escape procedure is used to escape from the continuation of these
 procedures, their behavior is implementation dependent."
-  (let* ((nport (open-output-file file))
-        (ans (with-error-to-port nport thunk)))
-    (close-port nport)
-    ans))
+  (call-with-output-file file
+   (lambda (p) (with-error-to-port p thunk))))
 
 (define (with-input-from-string string thunk)
   "THUNK must be a procedure of no arguments.
@@ -169,7 +226,7 @@ The test of STRING  is opened for
 input, an input port connected to it is made, 
 and the THUNK is called with no arguments.
 When the THUNK returns, the port is closed.
-Returns the value yielded by THUNK.  If an
+Returns the values yielded by THUNK.  If an
 escape procedure is used to escape from the continuation of these
 procedures, their behavior is implementation dependent."
   (call-with-input-string string
@@ -186,28 +243,3 @@ procedures, their behavior is implementation dependent."
    (lambda (p) (with-error-to-port p thunk))))
 
 (define the-eof-object (call-with-input-string "" (lambda (p) (read-char p))))
-
-\f
-;;;; Loading
-
-(if (not (defined? '%load-verbosely))
-    (define %load-verbosely #f))
-(define (assert-load-verbosity v) (set! %load-verbosely v))
-
-(define (%load-announce file)
-  (if %load-verbosely
-      (with-output-to-port (current-error-port)
-       (lambda ()
-         (display ";;; ")
-         (display "loading ")
-         (display file)
-         (newline)
-         (force-output)))))
-
-(set! %load-hook %load-announce)
-
-(define (load name . reader)
-  (with-fluid* current-reader (and (pair? reader) (car reader))
-    (lambda ()
-      (start-stack 'load-stack
-                  (primitive-load name)))))