Search code examples
fileioschemeread-writemit-scheme

Scheme: using open-i/o-file function


I'm using MIT Scheme 10.1.5 and am curious why the following code using open-i/o-file is not working I expected. Does anyone know what the issue is?

(define l "~/tmp0")
(define x ''(a b (c d) e f))

(let ((p (open-i/o-file l)))
  (begin (write x p)
     (flush-output p)
     (let ((r (read p)))
       (close-port p)
       r)))
;Value: #!eof

when I was expecting:

;Value: (quote (a b (c d) e f))

When using open-input-file or open-output-file the results are expected:

(let ((p (open-output-file l)))
  (write x p)
  (close-port p))

(let ((p (open-input-file l)))
  (let ((r (read p)))
    (close-port p)
    r))
;Value: (quote (a b (c d) e f))

Solution

  • When you read from or write to a port, the port position is advanced. After writing to an i/o port, you need to reset the position of the port if you want to read what was written.

    This does not seem to be very well documented in the MIT/GNU Scheme Reference Manual, but you can use textual-port-operation to perform manipulations on textual ports. Now, this is a bit more convoluted than you might like for a couple of reasons.

    First, textual-port-operation takes a port and a symbol as its arguments and returns a procedure that accomplishes the operation, i.e., textual-port-operation does not do the operations itself.

    Second, the symbol argument to textual-port-operation indicates the operation, but a complete list of these symbols is not present in the reference manual (as far as I can tell). You will need to call textual-port-operation-names on the port to find out which operations the port supports.

    Here is a rewritten version of OP code that behaves as expected:

    (define l "./test-file.dat")
    (define x ''(a b (c d) e f))
    
    ;; (define (display-port-ops p)
    ;;   (newline)
    ;;   (display (textual-port-operation-names p)) (newline)
    ;;   (newline))
    
    (let* ((p (open-i/o-file l))
           (start ((textual-port-operation p 'position) p)))
      ;; (display-port-ops p)
      (write x p)
      (flush-output p)
      ((textual-port-operation p 'set-position!) p start)
      (let ((r (read p)))
        (close-port p) r))
    

    Here, the display-port-ops procedure was used to interrogate the port and discover which operations are supported and what symbols represent those operations. This had to be called with a port argument, so it was placed in the let* form. I called this, uncommented, before adding either of the calls to textual-port-operation so that I could see which operations were available. The position and set-position! operations looked promising, so they were incorporated into the program. Note that let* had to replace let so that the value of p could be used in an expression calculating the start position.

    The call to display-port-ops, which just wraps a call to textual-port-operation-names, showed this result:

    (length pathname position set-position! truename write-self close-input eof?
     input-line input-open? input-channel buffered-output-bytes bytes-written
     close-output output-column output-open? output-channel synchronize-output
     char-set close coding known-coding? known-codings known-line-ending?
     known-line-endings line-ending open? set-coding set-line-ending
     supports-coding? char-ready? peek-char read-char read-substring unread-char
     flush-output write-char write-substring)
    

    Armed with these procedures, here is what happens:

    Immediately after creating the port p, (textual-port-operation p 'position) is called, yielding a procedure that will return a port position; this procedure is called on the port p, and start is bound to the result, saving the initial position of the port.

    After writing to the port, (textual-port-operation p 'set-position!) is called, yielding a procedure that will mutate the state of a port, setting a new position; this procedure is called on the port p and the desired position start.

    Now, when read is called on p, reading commences from the port's initial position.

    When the program is loaded, the test-file.dat is created as expected, and the result of reading the file back is displayed in the REPL:

    1 (user) => (load "file-io.scm")
    
    ;Loading "file-io.scm"... done
    ;Value: (quote (a b (c d) e f))