From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Israelsson Tampe Newsgroups: gmane.lisp.guile.devel Subject: syntax closures Date: Thu, 17 Jan 2013 21:51:11 +0100 Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=002354470d94a3812e04d38227cf X-Trace: ger.gmane.org 1358455878 5445 80.91.229.3 (17 Jan 2013 20:51:18 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 17 Jan 2013 20:51:18 +0000 (UTC) To: guile-devel , Eli Barzilay Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Thu Jan 17 21:51:36 2013 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TvwQv-0002F0-NV for guile-devel@m.gmane.org; Thu, 17 Jan 2013 21:51:33 +0100 Original-Received: from localhost ([::1]:33696 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvwQe-00025I-S8 for guile-devel@m.gmane.org; Thu, 17 Jan 2013 15:51:16 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:39528) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvwQb-000256-Aj for guile-devel@gnu.org; Thu, 17 Jan 2013 15:51:14 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TvwQZ-0005UG-Tz for guile-devel@gnu.org; Thu, 17 Jan 2013 15:51:13 -0500 Original-Received: from mail-qc0-f182.google.com ([209.85.216.182]:56937) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvwQZ-0005UB-Pp for guile-devel@gnu.org; Thu, 17 Jan 2013 15:51:11 -0500 Original-Received: by mail-qc0-f182.google.com with SMTP id k19so1968134qcs.27 for ; Thu, 17 Jan 2013 12:51:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=c4ZSTKGzp3TnAqzMMahAWrUWTsJPDiw5efJKRpHs0O0=; b=GwU9WKAcDKPQe0EsQ0DeWld+cjL2+3keIE++u2RiYXXqEgMG59A4bu6Ek33t9cWM7g w4X/+sQirTmAH1waJ0xj23HWoFChKAOkJCOzDh6xmPM2xea2uRoiy9sxEwsXpbtnRzEe HmlpPagMAFhYy/Z0+t2cl3OnUy8cKBl/BlJb8+rBWffWQLzgpkTRqXosD5Q4V1O8qzNk Ts9AFILcc201FqU/wJcAf7R5OROhbK9U7qMMC52Ort85Wlox3yvlCrF1SG4ZQrqgzllr G/yfjEjK3YtgDH7W7a3o5kVp3Q6hmNMuWcm64+Q6iORDahXqXridIdcC90oNmI7bkmZI 3EHQ== X-Received: by 10.229.106.31 with SMTP id v31mr1666339qco.26.1358455871078; Thu, 17 Jan 2013 12:51:11 -0800 (PST) Original-Received: by 10.49.28.135 with HTTP; Thu, 17 Jan 2013 12:51:11 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.216.182 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:15470 Archived-At: --002354470d94a3812e04d38227cf Content-Type: text/plain; charset=ISO-8859-1 ____ 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 --002354470d94a3812e04d38227cf Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
____ FUN with syntax case ____=A0
Hi all, I wanted to=A0resurrect=A0the idea of a syntactic closure. I= did some thinking and
decided on the following system:

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

=A0 (syntax-closure c= losure-object template1 ...)

e.g.

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

1. Note that we use #, to insert a functi= on object into the syntax expression
2. The idea is to capture the syntactic environment in template1= ... and =A0call the
=A0 =A0 closure with the captured synt= actic elements and insert the resulting syntax into
.
=

The code for syntax-closure is simple:

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

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

(define (syntax-closure-reader chr por= t)
=A0 (define (g-loop x)
=A0 =A0 (let loop ((x x) (a &= #39;()) (g '()) (t '()))
=A0 =A0 =A0 (match x
= =A0 =A0 =A0 =A0 (((and stx ('syntax x)) . l)
=A0 =A0 =A0 =A0 =A0(let ((gen (gensym "template")))
=A0 =A0 =A0 =A0 =A0 =A0(loop l (cons gen a) (cons gen g) (cons x t))))
=A0 =A0 =A0 =A0 ((x . l)
=A0 =A0 =A0 =A0 =A0(loop l (cons= x a) g t))
=A0 =A0 =A0 =A0 (()
=A0 =A0 =A0 =A0 =A0(values (reverse a) (reverse g) (reverse t))))))
=A0 =A0 =A0 =A0
=A0 (let ((code (read port)))
= =A0 =A0 (match code
=A0 =A0 =A0 ((F X ...)
=A0 =A0 =A0 = =A0(call-with-values (lambda () (g-loop X))
=A0 =A0 =A0 =A0 =A0(lambda (args gensyms template)
=A0 =A0 = =A0 =A0 =A0 =A0`(syntax-closure=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0(un= syntax (lambda ,gensyms
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0(,F ,@args)))
=A0 =A0 =A0 =A0 =A0 =A0 =A0,@template)))= )
=A0 =A0 =A0 (_ (error "wrong format in syntax-closure-reader"= ;)))))
=A0 =A0=A0
(read-hash-extend #\_ syntax-closure-= reader)

And with this we can now do

=A0 (define (g x) x)
=A0 (define-syntax= f (lambda (x) #`(let ((x 1)) #_(g #'x))))
<= div style>

leading to

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

The reader extension will be of the form
#_(F a ...)<= /div>

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

With this tool we will get close enough to srfi-72 and that code wi= ll 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.=A0
racket and gu= ile 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 guil= e. Is #_ ok? do you have any other suggestion?

Regards
/Stefan
=


--002354470d94a3812e04d38227cf--