Custom record printers

27 views
Skip to first unread message

Marc Nieper-Wißkirchen

unread,
Sep 24, 2020, 12:33:48 PM9/24/20
to chibi-scheme
I am not sure whether I have asked this before, but is it possible to customize the result of writing or displaying a record type through Scheme code in Chibi?

John Cowan

unread,
Sep 24, 2020, 2:21:54 PM9/24/20
to chibi-...@googlegroups.com
I've been thinking about that for a long time.  It has two problems: we don't have generic functions and Scheme has no way in general to deal with plug-ins (i.e., even if we had a generic record-write function, we have no way to retroactively make the (scheme write) functions use it.  

Probably the best solution is a registration system plus one of the various implementations of write, write-shared, and write-simple; that would make it portable.  I'm uncertain whether display should be included or not.

On Thu, Sep 24, 2020 at 12:33 PM Marc Nieper-Wißkirchen <marc....@gmail.com> wrote:
I am not sure whether I have asked this before, but is it possible to customize the result of writing or displaying a record type through Scheme code in Chibi?

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chibi-scheme/3a734940-34aa-4c4f-a111-0b32e9eb085dn%40googlegroups.com.

Marc Nieper-Wißkirchen

unread,
Sep 24, 2020, 2:36:45 PM9/24/20
to chibi-scheme
This question of mine was meant Chibi-specific.

Nevertheless... as for a general solution (which is yet to be standardized): If we just want to customize the printing of records, the procedure should become an optional part of the record-type definition as the record-type can function as its own registry.

I don't understand your comment about a generic record-write function and applying it retroactively.

John Cowan

unread,
Sep 24, 2020, 2:56:59 PM9/24/20
to chibi-...@googlegroups.com
On Thu, Sep 24, 2020 at 2:36 PM Marc Nieper-Wißkirchen <marc....@gmail.com> wrote:

This question of mine was meant Chibi-specific.

I realized that, but I decided to broaden (or hijack) the discussion.
 
Nevertheless... as for a general solution (which is yet to be standardized): If we just want to customize the printing of records, the procedure should become an optional part of the record-type definition as the record-type can function as its own registry.

That's a possible, but not a necessary, approach.  SRFI 9 define-record-type format is rather inelastic: its only virtue is its wide support.

 
I don't understand your comment about a generic record-write function and applying it retroactively.

In other words, you can provide `write-record`, which will write a record using its associated procedure.   But you cannot portably integrate that into existing implementations of write, because write doesn't have any such hook.  Few if any Scheme things do, and this is a weakness in the language's general design.

What I think we need is a SRFI that provides an implementation of (scheme write) but also has an API for registering printing procedures.  Bootstrapping from Chibi or some other Scheme with an MIT-compatible license might be reasonable.



that would make it portable.  I'm uncertain whether display should be included or not.

On Thu, Sep 24, 2020 at 12:33 PM Marc Nieper-Wißkirchen <marc....@gmail.com> wrote:
I am not sure whether I have asked this before, but is it possible to customize the result of writing or displaying a record type through Scheme code in Chibi?

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chibi-scheme/3a734940-34aa-4c4f-a111-0b32e9eb085dn%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.

Marc Nieper-Wißkirchen

unread,
Sep 24, 2020, 3:46:24 PM9/24/20
to chibi-...@googlegroups.com
Am Do., 24. Sept. 2020 um 20:57 Uhr schrieb John Cowan <co...@ccil.org>:

>> This question of mine was meant Chibi-specific.

> I realized that, but I decided to broaden (or hijack) the discussion.

No problem, unless the original question gets lost. :)

>> Nevertheless... as for a general solution (which is yet to be standardized): If we just want to customize the printing of records, the procedure should become an optional part of the record-type definition as the record-type can function as its own registry.

> That's a possible, but not a necessary, approach. SRFI 9 define-record-type format is rather inelastic: its only virtue is its wide support.

I don't think that this is a major obstacle. We can easily invent
`define-record-type/writer' or, for further future extensions,
`define-record-type/options'. This doesn't touch the code where
records without printers are defined.

Unless absolutely necessary, we shouldn't use a registry for printing
procedures for records. Registries are fragile (because a record-type
may be registered twice), unknown to the optimizer, and the lookup can
be O(n) (at least in a simple implementation), while it is O(1) when
tied to the record. Finally, R7RS's record types are generative and
subject to garbage-collection, for which a registry is not a very good
match.

> What I think we need is a SRFI that provides an implementation of (scheme write) but also has an API for registering printing procedures. Bootstrapping from Chibi or some other Scheme with an MIT-compatible license might be reasonable.

I don't understand what you mean by "bootstrapping", but providing a
sample implementation is something I can do along with a
define-record-type extension.

John Cowan

unread,
Sep 24, 2020, 6:26:50 PM9/24/20
to chibi-...@googlegroups.com
By "bootstrapping" I mean "starting with an existing implementation of (scheme write)":

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.

Alex Shinn

unread,
Sep 24, 2020, 10:17:07 PM9/24/20
to chibi-...@googlegroups.com
$ chibi -mchibi.ast -msrfi.{125,128} -R
> (define x (hash-table (make-default-comparator) 'foo 1 'bar 2))
> x
{Hash-Table #41 #(((bar . 2) (foo . 1)) () () () () () () () () () () () () () () () () () () () () () ()) 2 #<procedure default-hash> #<procedure #f>}
> (define (write-hash-table ht wr out)
  (write-string "{Hash-Table " out)
  (wr (hash-table->alist ht))
  (write-string "}" out))
> (type-printer-set! (type-of x) write-hash-table)
> x
{Hash-Table ((foo . 1) (bar . 2))}
>

--
Alex

On Fri, Sep 25, 2020 at 1:33 AM Marc Nieper-Wißkirchen <marc....@gmail.com> wrote:
I am not sure whether I have asked this before, but is it possible to customize the result of writing or displaying a record type through Scheme code in Chibi?

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.

Marc Nieper-Wißkirchen

unread,
Sep 25, 2020, 1:38:10 AM9/25/20
to chibi-...@googlegroups.com
Am Fr., 25. Sept. 2020 um 04:17 Uhr schrieb Alex Shinn <alex...@gmail.com>:
>
> $ chibi -mchibi.ast -msrfi.{125,128} -R
> > (define x (hash-table (make-default-comparator) 'foo 1 'bar 2))
> > x
> {Hash-Table #41 #(((bar . 2) (foo . 1)) () () () () () () () () () () () () () () () () () () () () () ()) 2 #<procedure default-hash> #<procedure #f>}
> > (define (write-hash-table ht wr out)
> (write-string "{Hash-Table " out)
> (wr (hash-table->alist ht))
> (write-string "}" out))
> > (type-printer-set! (type-of x) write-hash-table)
> > x
> {Hash-Table ((foo . 1) (bar . 2))}
> >

Thank you very much, Alex. This is what I was looking for in my
initial question.

Marc Nieper-Wißkirchen

unread,
Sep 25, 2020, 2:38:26 PM9/25/20
to chibi-scheme
Marc Nieper-Wißkirchen schrieb am Donnerstag, 24. September 2020 um 21:46:24 UTC+2:

>> Nevertheless... as for a general solution (which is yet to be standardized): If we just want to customize the printing of records, the procedure should become an optional part of the record-type definition as the record-type can function as its own registry.

> That's a possible, but not a necessary, approach. SRFI 9 define-record-type format is rather inelastic: its only virtue is its wide support.

I don't think that this is a major obstacle. We can easily invent
`define-record-type/writer' or, for further future extensions,
`define-record-type/options'. This doesn't touch the code where
records without printers are defined.

By the way, this is a good example of why we may want to reconsider Kawa/Racket/Guile-style keywords and/or SRFI 88 keywords. We had some discussion about the different merits on the SRFI 177 (keyword arguments) mailing list. Kawa/Racket-style keywords are syntactic (and are not evaluated as SRFI 88 keyword objects) and maybe more helpful when adding optional arguments to macro forms. For example, we could have (modulo pairs of parentheses or ordering)

(define-record-type <record>
  (make-record ...)
  record?
  (#:printer ...)
  <field spec> ...)
 
Reply all
Reply to author
Forward
0 new messages