unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* syntax closures
@ 2013-01-17 20:51 Stefan Israelsson Tampe
  2013-01-22 12:56 ` Andy Wingo
  0 siblings, 1 reply; 15+ messages in thread
From: Stefan Israelsson Tampe @ 2013-01-17 20:51 UTC (permalink / raw)
  To: guile-devel, Eli Barzilay

[-- Attachment #1: Type: text/plain, Size: 2616 bytes --]

____ FUN with syntax case ____

Hi all, I wanted to resurrect the idea of a syntactic closure. I did some
thinking and
decided on the following system:

The low level routine is a special macro 'syntax-closure' put in (ice-9
syntax-closure)
The macro has the following ideom

  (syntax-closure closure-object template1 ...)

e.g.

  #`(syntax-closure #,(lambda (x y) (f x 1 y)) #'(+ x z) #'w)

1. Note that we use #, to insert a function object into the syntax
expression
2. The idea is to capture the syntactic environment in template1 ... and
 call the
    closure with the captured syntactic elements and insert the resulting
syntax into
.

The code for syntax-closure is simple:

(define-syntax-rule (syntax-closure f x ...)
  (let-syntax ((capture (lambda (stx)
                          (syntax-case stx ()
                            ((_ y (... ...))
                             (apply f #'(y (... ...))))))))
    (capture x ...)))

And with this tool we can device a very useful reader extension #_ e.g.

(define (syntax-closure-reader chr port)
  (define (g-loop x)
    (let loop ((x x) (a '()) (g '()) (t '()))
      (match x
        (((and stx ('syntax x)) . l)
         (let ((gen (gensym "template")))
           (loop l (cons gen a) (cons gen g) (cons x t))))
        ((x . l)
         (loop l (cons x a) g t))
        (()
         (values (reverse a) (reverse g) (reverse t))))))

  (let ((code (read port)))
    (match code
      ((F X ...)
       (call-with-values (lambda () (g-loop X))
         (lambda (args gensyms template)
           `(syntax-closure
             (unsyntax (lambda ,gensyms
                         (,F ,@args)))
             ,@template))))
      (_ (error "wrong format in syntax-closure-reader")))))

(read-hash-extend #\_ syntax-closure-reader)

And with this we can now do

  (define (g x) x)
  (define-syntax f (lambda (x) #`(let ((x 1)) #_(g #'x))))

leading to

scheme@(guile-user)> f
$2 = 1

The reader extension will be of the form
#_(F a ...)

And all (syntax x) arguments in a ... will be automatically captured from
the environment
e.g. #_ is as #, but with the syntax arguments correctly captured from the
surrounding
environment.

With this tool we will get close enough to srfi-72 and that code will not
be needed.

I'm I out of the blue here?

The question for me is how to treat this code, as a library of it's own
supporting e.g.
racket and guile and other schemes with the syntax case system, or should
we try to make this reader extension as nice as possible and incorporate
the code in guile. Is #_ ok? do you have any other suggestion?

Regards
/Stefan

[-- Attachment #2: Type: text/html, Size: 4006 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2013-02-15 10:08 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-17 20:51 syntax closures Stefan Israelsson Tampe
2013-01-22 12:56 ` Andy Wingo
2013-01-22 16:19   ` Stefan Israelsson Tampe
2013-01-22 16:38     ` Andy Wingo
2013-01-22 19:06       ` Stefan Israelsson Tampe
2013-01-22 20:37       ` Stefan Israelsson Tampe
2013-01-23 23:24       ` Stefan Israelsson Tampe
2013-01-24  2:08         ` Alex Shinn
2013-01-24  7:11           ` Stefan Israelsson Tampe
2013-01-24  8:45             ` Alex Shinn
2013-02-14  9:42               ` Mikael Djurfeldt
2013-02-14 11:13                 ` Stefan Israelsson Tampe
2013-02-15  4:16                   ` Mark H Weaver
2013-02-15  9:34                     ` Stefan Israelsson Tampe
2013-02-15 10:08                     ` Stefan Israelsson Tampe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).