Re: [racket-dev] Enhancement to the syntax system?

2012-07-10 Thread Stefan Israelsson Tampe
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é 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 = 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.)
_
  Racket Developers list:
  http://lists.racket-lang.org/dev


Re: [racket-dev] Enhancement to the syntax system?

2012-07-10 Thread Stefan Israelsson Tampe
samth made a pointer to

http://srfi.schemers.org/srfi-72/srfi-72.html

It does not look like guile racket etc. have implemented this yet.

Am I wrong?

This is precisely what I'm after!

On Tue, Jul 10, 2012 at 5:26 PM, Ludovic Courtès  wrote:

> Hi,
>
> Matthew Flatt  skribis:
>
> > It's natural --- but not correct --- to think that #` is responsible
> > for hygiene, in which case `(f #'x)' should keep the given `x' separate
> > from the `let'-bound `x' in the result.
>
> [...]
>
> > If you change the example to
> >
> >  #lang racket
> >  (begin-for-syntax
> >   (define-syntax-rule (f body)
> > #`(let ([x 1]) body)))
> >  (define-syntax (m stx)
> >(with-syntax ([zz (f x)]) #`(let ([x 2]) zz)))
> >  (m)
> >
> > so that `f' is used as a macro instead of a function, then you get 2,
> > since the macro-expansion of `(f x)' keeps the `x's separate.
>
> Interesting.  Thanks for the clarification and examples.
>
> Ludo’.
>
>
_
  Racket Developers list:
  http://lists.racket-lang.org/dev