chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Even more format weirdness


From: felix winkelmann
Subject: Re: [Chicken-users] Even more format weirdness
Date: Mon, 5 Sep 2005 08:15:02 +0200

On 9/4/05, Alejandro Forero Cuervo <address@hidden> wrote:
> 
> I was thinking about an optimization where one can call
> "make-format-function" with the format string and it will return a
> function receiving the port and optional arguments.  With that one
> could define format as follows:
> 
>   (define (format port str . rest)
>     (apply (make-format-function str) port rest))
> 
> (Actually, one uses another function to build "make-format-function"
> from some parameters specifing its behaviour.)
> 
> This would have the advantage, though, that when the same format
> string is used multiple times, one can make things run faster, as in:
> 
>   (let ((func (make-format-function "Times: ~A: ~:{ ~A=~A~^,~}~%")))
>     (for-each (cut apply func #t <>) *all-objects*))
> 

Very nice.

> Since most of the time this string will be known in advance, I'd like
> this expansion to be performed at compile time (or at least once at
> startup).  Basically, I want to do something like this:
> 
>   (define-macro (format p fmt . args)
>     (if (string? fmt)
>       (compile-time-expand p fmt args)
>       `((make-format-function fmt) p ,@args)))
> 
> However, I would save a lot of time if I could define
> compile-time-expand as a call to make-format-function so I wouldn't
> have to duplicate functionality.  Any thoughts on how I should do
> this?
> 

That's even nicer. We then also have argument-count checking at
compile-time (I forget argument to format all the time - if I use it at all,
which is mostly for complex format-strings).

Now, what you need is to load the format library code also at compile-time
(by installing with the '((syntax) (require-at-runtime ...))' extension info).
Making format a macro is probably not the best idea. Instead I would recommend
to add an optimizer to the compiler. `format' will still be a macro, but
the compiler will perform compile-time format-string expansion and
rewrite the call to something different. You also get some basic value-tracking
for free (like in `(let ((a "...")) (format #t a ...))').
Unfortunately that involves
mucking around with the CPS node-tree. I will provide some code that
does this.


cheers,
felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]