[racket] Compile-time evaluation
I don't think read-time computation is the right approach for Racket.
Here's a compile/expand-time alternative that lets you use unsyntax as a
compile-time escape.
(require (for-syntax racket/syntax))
(define-syntax (with-compile-time-unsyntax stx)
(syntax-case stx ()
[(wctu form ...)
(syntax-local-introduce
(syntax-local-eval #'(quasisyntax (begin form ...))))]))
Here's an example use:
(begin-for-syntax
(define the-symbol 'apple))
(with-compile-time-unsyntax
(define (f x)
(case x
((#,the-symbol) 'yes)
(else 'no))))
(f 'apple) ;; => 'yes
(f 'pear) ;; => 'no
If you wanted, you could make a custom language (see #%module-begin)
that implicitly wrapped the whole module body with
with-compile-time-unsyntax automatically.
You might find it difficult to use quasisyntax and unsyntax within such
a module. I haven't tried it or thought through how it would interact
with quasisyntax "levels". You could of course implement your own
quasisyntax-like form with a different escape binding.
Ryan
On 07/29/2013 10:07 PM, Roman Klochkov wrote:
> Yes. Really it is read-time.
>
> In CL every top level expression read-compile-load or simply load if it
> is compiled.
>
> So in Racket would be natural to have (for-syntax ...) environment in #.
> I hope, that Racket also read toplevel form, loads it, then reads next
> (not reads all at once, then load first form , second form and so on).
>
> Can you invent such read-macro?
>
>
> Понедельник, 29 июля 2013, 21:44 -04:00 от Sam Tobin-Hochstadt
> <samth at ccs.neu.edu>:
>
> On Mon, Jul 29, 2013 at 1:53 PM, Vincent St-Amour
> <stamourv at ccs.neu.edu
> <https://e.mail.ru/messages/sentmsg?compose&To=stamourv@ccs.neu.edu>> wrote:
> > Here's a quick solution:
> >
> > #lang racket
> >
> > (define-syntax-rule (hash-dot e)
> > (let-syntax ([computed-e (lambda (stx) (datum->syntax #'e e))])
> > (computed-e)))
> >
> > (hash-dot (+ 2 3))
> >
>
> In CL, #. is read-time, not compile-time, evaluation, so this isn't
> quite the same. It would be easy to create a read-table that enabled
> this, though. You'd have to make some decision about what environment
> the read-time expression sees -- I don't know what CL does here.
>
> Sam
>
>
>
> --
> Roman Klochkov
>
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>