Some users of Gambit have experienced problems using the REPL to debug programs which create circular structures because the external representation used by default by "write", "pretty-print", and "pp" does not handle circular structures or deeply nested structures very well (i.e. infinite loop).
This issue can be addressed by changing the readtable associated with the REPL's output port. This can be done with code such as:
(define (repl-ctrl sharing-allowed? max-write-level max-write-length) (let ((port (repl-output-port))) (output-port-readtable-set! port (let* ((rt (output-port-readtable port)) (rt (readtable-sharing-allowed?-set rt sharing-allowed?)) (rt (readtable-max-write-level-set rt max-write-level)) (rt (readtable-max-write-length-set rt max-write-length))) rt))))
(repl-ctrl #t 2 5)
It is convenient to place this code in the ~/gambcini.scm file so that it does not have to be entered each time gsi is invoked.
The above code will use the SRFI 38 syntax for shared/circular structures. It will also stop printing parts of a structure that are beyond a nesting level of 2, or beyond a length of 5 (using an elipsis instead).
Marc
Afficher les réponses par date
The readtables doc is a bit over my head. Do readtables allow me to define how the system will display a type I create? For example, something like:
(set 1 2 'pi)
{1 2 pi}
(dict 'grass 'green 'sky 'blue 'v8 'red)
[grass => green sky => blue v8 => red]
If not, is such a thing possible through some other means, in gambit?
On 8/6/07, Marc Feeley feeley@iro.umontreal.ca wrote:
Some users of Gambit have experienced problems using the REPL to debug programs which create circular structures because the external representation used by default by "write", "pretty-print", and "pp" does not handle circular structures or deeply nested structures very well (i.e. infinite loop).
This issue can be addressed by changing the readtable associated with the REPL's output port. This can be done with code such as:
(define (repl-ctrl sharing-allowed? max-write-level max-write-length) (let ((port (repl-output-port))) (output-port-readtable-set! port (let* ((rt (output-port-readtable port)) (rt (readtable-sharing-allowed?-set rt sharing-allowed?)) (rt (readtable-max-write-level-set rt max-write-level)) (rt (readtable-max-write-length-set rt max-write-length))) rt))))
(repl-ctrl #t 2 5)
It is convenient to place this code in the ~/gambcini.scm file so that it does not have to be entered each time gsi is invoked.
The above code will use the SRFI 38 syntax for shared/circular structures. It will also stop printing parts of a structure that are beyond a nesting level of 2, or beyond a length of 5 (using an elipsis instead).
Marc
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
On 26-Aug-07, at 11:29 PM, |/|/ Bendick wrote:
The readtables doc is a bit over my head. Do readtables allow me to define how the system will display a type I create? For example, something like:
(set 1 2 'pi)
{1 2 pi}
(dict 'grass 'green 'sky 'blue 'v8 'red)
[grass => green sky => blue v8 => red]
If not, is such a thing possible through some other means, in gambit?
No readtables do not support this currently. I think they should but it is not clear to me how this should be done.
Currently the only hook for the "printer" is the procedure ##wr which can be redefined. ##wr receives two parameters: we (the write environment) and obj (the object to write). The write environment is a structure containing a flag indicating the writing style (display, write, pretty-print), the output port, the readtable, etc. The procedure ##wr-str can be used to send strings to the write environment. Here's an example:
(define-type point x y) (define-type rect p1 p2)
(set! ##wr (let ((old-wr ##wr)) (lambda (we obj) (cond ((point? obj) (##wr-str we "<") (##wr we (point-x obj)) (##wr-str we " ") (##wr we (point-y obj)) (##wr-str we ">")) ((rect? obj) (##wr-str we "{") (##wr we (rect-p1 obj)) (##wr-str we " ") (##wr we (rect-p2 obj)) (##wr-str we "}")) (else (old-wr we obj))))))
;; this prints: {<1 2> <3 4>}
(pretty-print (make-rect (make-point 1 2) (make-point 3 4)))
Marc