[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PRINT methods

Contents:  (a) a new kind of PRINT operation.
           (b) a convention for using PRINT, PRETTY-PRINT, and the new operation.


I would like to suggest a new kind of PRINT operation, in addition to the
extant PRINT and PRETTY-PRINT operations, and a convention for using these
operations.  Consider the following use of PRINT and PP.  (I use the symbol
==> to indicate the output from a PRINT method).

    (define (foo x y)
       (list x y) )
    ==>  #{Procedure 46 FOO}

    (PRINT foo (terminal-output))
    ==>  #{Procedure 45 FOO}

    (PP foo)
    ==>  (LAMBDA (X Y)
            (LIST X Y) )

Thus I think of PRINT as being a brief printout of the object, giving only
the minimal details which would suffice in identifying the object without
great difficulty, and PRETTY-PRINT as being the detailed printout, giving all
the gory details of the object.  With this in mind, I would like to suggest
that PRETTY-PRINT behave thus:

    (PRETTY-PRINT foo (terminal-output))
    ==>  (LAMBDA (X Y)
            (LIST X Y) )

rather than thus:

    (PRETTY-PRINT foo (terminal-output))
    ==>  #{Procedure 45 FOO}

since PP is just a nice user-interface to PRETTY-PRINT.

Now for the third PRINT method.  Let me call that PRINT-READABLE for now.
Notice that neither PRINT nor PRETTY-PRINT prints the object in a format
which would restore the object on being re-read.  I would therefore like
PRINT-READABLE to do the following:

    (PRINT-READABLE foo (terminal-output))
    ==>  (DEFINE (FOO X Y)
            (LIST X Y) )

The utility of this is apparent when you consider that you could do
(PRINT-READABLE <some-object> <file>) and PRINT that object to the file
in a format which would allow the file to be LOADed later.  I find this
useful when I write programs that add to their own knowledge bases.

Consider another common example.  We all use and love STRUCTURE-TYPEs and
their kin.  I like to use the following convention for defining PRINT methods
for STRUCTUREs.  (For those who use the MSG macro, this convention fits in
well since you can PRINT stuff by referring to it normally, and PRETTY-PRINT
its details by using the PP form).

    (define-cstructure-type foo foo-x foo-y)
    ==>  #{Structure-type FOO}

    (set zap (foo:new 'a 'b))
    ==>  #{Structure 46 FOO}

    (PRINT zap (terminal-output))
    ==>  #{Structure 46 FOO}              ; the short form

    (PRETTY-PRINT zap (terminal-output))
    ==>  #{Structure 46 FOO
                        [FOO-X A]
                        [FOO-Y B]}        ; the details

The new PRINT-READABLE would actually print out the form needed to reconstruct
the cstructure, which can then be, for example, reloaded from a file:

    (PRINT-READABLE zap (terminal-output))
    ==>  (SET ZAP (FOO:NEW 'A 'B))

Of course, (PRINT-READABLE <symbol>) would just print out '<symbol>, which is
where the 'A and 'B came from.  This has the obvious extension to STRUCTUREs,
and in fact to OBJECTs and all other beasties.

To summarize, I would like to suggest:
    (a) a new print operation called PRINT-READABLE (or whatever you like; I'm
        not fussy!) that behaves as described above.
    (b) a convention for using PRINT, PRETTY-PRINT and PRINT-READABLE as
        described above.

Ashwin Ram.