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,gmane.comp.lang.racket.devel Subject: Re: [racket-dev] Enhancement to the syntax system? Date: Tue, 10 Jul 2012 19:47:14 +0200 Message-ID: References: <877gumufmq.fsf@gnu.org> <87r4stss4d.fsf@gnu.org> <878vf0o7gs.fsf@gnu.org> <87pq85dj8o.fsf@gnu.org> <87sjd058ia.fsf@gnu.org> <4FFC3D72.8070506@gentoo.org> <20476.16781.257276.194149@winooski.ccs.neu.edu> <20120710150315.B7B27650059@mail-svr1.cs.utah.edu> <87a9z7tz6n.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=20cf3003c39c22952b04c47d520d X-Trace: dough.gmane.org 1341942456 20903 80.91.229.3 (10 Jul 2012 17:47:36 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 10 Jul 2012 17:47:36 +0000 (UTC) To: dev@racket-lang.org, guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Tue Jul 10 19:47:35 2012 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 1SoeX8-000378-8S for guile-devel@m.gmane.org; Tue, 10 Jul 2012 19:47:34 +0200 Original-Received: from localhost ([::1]:38855 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SoeX7-0003fS-Cl for guile-devel@m.gmane.org; Tue, 10 Jul 2012 13:47:33 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:52608) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SoeWy-0003Ma-Mb for guile-devel@gnu.org; Tue, 10 Jul 2012 13:47:31 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SoeWr-0003A7-TC for guile-devel@gnu.org; Tue, 10 Jul 2012 13:47:24 -0400 Original-Received: from mail-gg0-f169.google.com ([209.85.161.169]:47785) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SoeWr-00039p-LS for guile-devel@gnu.org; Tue, 10 Jul 2012 13:47:17 -0400 Original-Received: by ggm4 with SMTP id 4so323187ggm.0 for ; Tue, 10 Jul 2012 10:47:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Ppz3HoBDpldzYQ1vRe6QjhirMqfsXNSbab6GRtYzL+I=; b=VpXwCktEtwi7C7cjimBNXzOxXV/vQnErsQmCNqQQc+psGG8OEAUneEo/zQ/SzovPGW x0PtmihSPWZGY58RIW3W5dLt7H+q5FnqjL9KGeaGkIubKAyEpe7HJ3cYN//stdHGmRTF SGf4QDaJtAEiRrpVWfPcn/Q9FIrGd2ZyyLCKLsNetx+iKmGrpEecOH3ZKJZmC3NBGEY7 PZth94+h74iPyoFbJM5JMvNN3lE6ULuqL2duCEi3GGwP/2jfpVEerXsY6QPHwo7KhZPc pR1vLDk8Mttil5maFlP9PV4D4IY1dxlcijQ8iUU3QuxtMsIzoCY/hflVNRk7Pz9Q+t1J XPjw== Original-Received: by 10.42.19.131 with SMTP id c3mr2004815icb.36.1341942434796; Tue, 10 Jul 2012 10:47:14 -0700 (PDT) Original-Received: by 10.50.41.196 with HTTP; Tue, 10 Jul 2012 10:47:14 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.161.169 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:14736 gmane.comp.lang.racket.devel:6406 Archived-At: --20cf3003c39c22952b04c47d520d Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable The question I posed was If it's possible to use srfi-72 in guile or racket. It is indeed a wish of mine that it's implemented because it will most certainly help me write beutiful code because that srfi cater to the coding style Ichoose to have. Without it one most certainly need to use with-syntax to introduce the bindings in order to be safe and avoid a multiple of possible syntactic loopholes as can see if you read Andr=E9 van Tonder's http://srfi.schemers.org/srfi-72/srfi-72.html Anyway one does not need to change psyntax in order to come close to that system. A more hacky way is to use code like the one at the end of this document. It's a reiteration and improvement on a previous version. So with this hack I can write, ------------- (use-modules (syntax unquote++)) (define (f x y) #`(let ((x 1)) (+ #,x #,y))) (define (h y) ##`(let ((x 2)) #.((x) (f x y)))) (define-syntax g (lambda (x) (syntax-case x () ((_ y) ##`(let ((x y)) #.((x) (h x))))))) scheme@(guile-user)> (g 3) $2 =3D 5 -------------- In racket, (define-for-syntax (f x y) #`(let ((x 1)) (+ #,x #,y))) (define-for-syntax (h y) #`(let ((x 2)) #,(f #'x y)) (define-syntax (g stx) (syntax-case stx () ((_ y) #`(let ((x y)) #,(h #'x))))) > (g 3) 4 -------------- This shows that it was just luck previously when racket produced the correct (for my intention) answer. with srfi-72 a correct answer would have been produced. Without srfi-72 I will then move over to use ##` and #. in my code because it will be easy to transfer later on if when srfi-72 is available in some form. /Stefan (define-module (syntax unquote++) #:export (quasisyntax++ insyntax)) (define *s* (make-fluid '())) (define *t* (make-fluid '())) (define table (make-weak-key-hash-table)) (define (add-lambda lam) (let* ((id (gensym "id")) (x (datum->syntax #'table id))) (hashq-set! table id lam) x)) (define (plexer x . l) (let ((lam (hashq-ref table x))) (apply lam l))) (define (parse stx x) (syntax-case x (unsyntax insyntax unsyntax-splicing) ((unsyntax . _) x) ((unsyntax-splicing . _) x) ((insyntax ((p ...) c ...)) (with-syntax ((g (datum->syntax stx (gensym "g"))) (i (datum->syntax stx (gensym "i")))) (fluid-set! *s* (cons #'(g (lambda (x) (syntax-case x () ((_ p ...) (plexer 'i #'p ...))))) (fluid-ref *s*))) (fluid-set! *t* (cons #'(i (add-lambda (lambda (p ...) (begin c ...)))) (fluid-ref *t*))) #'(g p ...))) ((x . l) (cons (parse stx #'x) (parse stx #'l))) (x #'x))) (define-syntax quasisyntax++ (lambda (x) (syntax-case x () ((_ y) (begin (fluid-set! *s* '()) (fluid-set! *t* '()) (with-syntax ((statement (parse x #'y)) (lets (fluid-ref *s*)) (withs (fluid-ref *t*))) #'(with-syntax withs #`(let-syntax lets statement)))))))) (define (rg ch stream) (let ((x (read-char stream))) (cond ((eqv? x #\`) `(quasisyntax++ ,(read stream))) (#t (error "Wrong format of # reader extension"))))) (define (rg. ch stream) `(insyntax ,(read stream))) (read-hash-extend #\# rg) (read-hash-extend #\. rg.) --20cf3003c39c22952b04c47d520d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable The question I posed was If it's possible to use srfi-72 in guile or ra= cket. It is indeed a wish
of mine that it's implemented because it w= ill most certainly help me write beutiful code because
that srfi cater = to the coding style Ichoose to have. Without it one most certainly need to = use with-syntax to introduce the bindings in order to be safe and avoid a m= ultiple of possible syntactic loopholes as can see if you read Andr=E9 van = Tonder's

http://srfi.s= chemers.org/srfi-72/srfi-72.html

Anyway one does not need to cha= nge psyntax in order to come close to that system. A more hacky
way is t= o use code like the one at the end of this document. It's a reiteration= and improvement on a
previous version. So with this hack I can write,

-------------
(u= se-modules (syntax unquote++))
(define (f x y) #`(let ((x 1)) (+ #,x #,y= )))
(define (h y) ##`(let ((x 2)) #.((x) (f x y))))
(define-syntax g = (lambda (x) (syntax-case x () ((_ y) ##`(let ((x y)) #.((x) (h x)))))))
scheme@(guile-user)> (g 3)
$2 =3D 5

--------------
In racke= t,
(define-for-syntax (f x y) #`(let ((x 1)) (+ #,x #,y)))
(define-f= or-syntax (h y) #`(let ((x 2)) #,(f #'x y))
(define-syntax (g stx) (= syntax-case stx () ((_ y) #`(let ((x y)) #,(h #'x)))))
> (g 3)
4
--------------

This shows that it was just luck p= reviously when racket produced the correct (for my intention) answer.
wi= th srfi-72 a correct answer would have been produced. Without srfi-72 I wil= l then move over to use
##` and #. in my code because it will be easy to transfer later on if when = srfi-72 is available in some form.

/Stefan

(define-module (sy= ntax unquote++)
=A0 #:export (quasisyntax++ insyntax))

(define *s= * (make-fluid '()))
(define *t* (make-fluid '()))

(define table (make-weak-key-hash-= table))
(define (add-lambda lam)
=A0 (let* ((id (gensym "id"= ;))
=A0=A0=A0=A0=A0=A0=A0=A0 (x=A0 (datum->syntax #'table id)))=A0=A0=A0 (hashq-set! table id lam)
=A0=A0=A0 x))
(define (plexer x . l)
=A0 (let ((lam (hashq-ref table = x)))
=A0=A0=A0 (apply lam l)))

(define (parse stx x)
=A0 (synt= ax-case x (unsyntax insyntax unsyntax-splicing)
=A0=A0=A0 ((unsyntax=A0= =A0=A0=A0=A0=A0=A0=A0=A0 . _) x)
=A0=A0=A0 ((unsyntax-splicing . _) x) =A0=A0=A0 ((insyntax ((p ...) c ...))
=A0=A0=A0=A0 (with-syntax ((g (dat= um->syntax stx (gensym "g")))
=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 (i (datum->syntax stx (gensym "i")= )))
=A0=A0=A0=A0=A0=A0 (fluid-set! *s* (cons #'(g (lambda (x)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (syntax-case x ()
=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0 ((_ p ...) (plexer 'i #'p ...)))))
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (f= luid-ref *s*)))
=A0=A0=A0=A0=A0=A0 (fluid-set! *t* (cons #'(i (add-l= ambda
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 (lambda (p ...) (begin c ...))))
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= (fluid-ref *t*)))
=A0=A0=A0=A0=A0=A0 #'(g p ...)))
=A0=A0=A0 ((x= . l)
=A0=A0=A0=A0 (cons (parse stx #'x) (parse stx #'l)))
=A0=A0=A0 (x #'x)))

(define-syntax quasisyntax++
=A0 (lambda = (x)
=A0=A0=A0 (syntax-case x ()
=A0=A0=A0=A0=A0 ((_ y)
=A0=A0=A0= =A0=A0=A0 (begin
=A0=A0=A0=A0=A0=A0=A0=A0 (fluid-set! *s* '())
= =A0=A0=A0=A0=A0=A0=A0=A0 (fluid-set! *t* '())
=A0=A0=A0=A0=A0=A0=A0= =A0 (with-syntax ((statement (parse x #'y))
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (lets=A0= =A0=A0=A0=A0 (fluid-ref *s*))
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0 (withs=A0=A0=A0=A0 (fluid-ref *t*)))
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 #'(with-syntax withs
=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 #`(let-syntax lets statement))))))))
=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0
(define (rg ch stream)
=A0 (let ((x (read-char stream)))
=A0=A0=A0 (c= ond
=A0=A0=A0=A0 ((eqv? x #\`)
=A0=A0=A0=A0=A0 `(quasisyntax++ ,(read= stream)))
=A0=A0=A0=A0 (#t
=A0=A0=A0=A0=A0 (error "Wrong format= of # reader extension")))))

(define (rg. ch stream) `(insyntax ,(read stream)))

(read-hash-exten= d #\# rg)
(read-hash-extend #\. rg.)

--20cf3003c39c22952b04c47d520d--