Re: [racket-users] MIT/Chicken/Chibi Scheme style ir-macro-transformer in Racket

2018-08-29 Thread Shu-Hung You
I should've been clear that the loop example was taken from the
Chicken scheme wiki. Same for the while example mentioned in the
email:
https://wiki.call-cc.org/man/4/Macros#ir-macro-transformer

Using the syntax->datum here would drop lexical information though; if
the syntax object given to the macro has lexical context other than
the use-site of the macro, binding information will be lost.

(define-syntax (use-loop stx)
  (syntax-case stx ()
[(_ body) #'(loop (displayln "loop") body (exit))]))

(let ([i 0]) (loop (displayln "loop") (displayln i) (exit)))
(let ([i 0]) (use-loop (displayln i)))



On Wed, Aug 29, 2018 at 4:32 PM, t791bc via Racket Users
 wrote:
> Thanks for the responses. In case anybody is interested, I came up with the
> following implementation. Since (at least in this simple implementation)
> there is an obvious symmetry between explicit renaming and implicit renaming
> I added the er-macro-transformer also. They seem to work correctly. One
> minor difference to the ir-macro-transformer in Chicken though is that
> parameters also need to be injected (which kind of logically makes sense).
>
> I am not exactly sure whether datum->syntax and syntax->datum go through the
> entire expression but if they do, the implementation will be very
> inefficient. (If anybody knows more on that, I would be interested.)
>
> Implementation:
>
> #lang racket
>
> (provide (for-syntax ir-macro-transformer er-macro-transformer))
>
> (begin-for-syntax
>   (require (for-syntax (only-in racket/base lambda syntax with-syntax)))
>   (define-syntax ir-macro-transformer
> (lambda (stx)
>   (with-syntax ([(_ proc) stx])
> #'(lambda (x)
> (datum->syntax #'proc (proc (syntax->datum x) (lambda (v)
> (datum->syntax x v
>   (define-syntax er-macro-transformer
> (lambda (stx)
>   (with-syntax ([(_ proc) stx])
> #'(lambda (x)
> (datum->syntax x (proc (syntax->datum x) (lambda (v)
> (datum->syntax #'proc v)
>
> Examples:
>
> ; loop example from Shu-Hung's post is now working as expected
>
> (define-syntax loop
>   (ir-macro-transformer
>(lambda (expr inject)
>  (let ((body (cdr expr)))
>`(call-with-current-continuation
>  (lambda (,(inject 'exit))
>(let f () (begin .,(inject body)) (f   ; notice the
> (inject body) here... dropping the inject will break the code
>
> ;=> 543210
>
> ; the following suggests that the lexical environments work as they should
>
> (let ([x 1]
>   [y 2])
>   (swap! x y)
>   x)
>
> => 2
>
> (define t 3)
>
> (define-syntax tripple
>   (ir-macro-transformer
>(lambda (expr inject)
>  (let ([a (cadr expr)])
>`(* t ,(inject a))
>
> (let ([t 29]
>   [* -])
>   (tripple 2))
>
> ;=> 6
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] MIT/Chicken/Chibi Scheme style ir-macro-transformer in Racket

2018-08-29 Thread t791bc via Racket Users
Thanks for the responses. In case anybody is interested, I came up with the 
following implementation. Since (at least in this simple implementation) 
there is an obvious symmetry between explicit renaming and implicit 
renaming I added the er-macro-transformer also. They seem to work 
correctly. One minor difference to the ir-macro-transformer in Chicken 
though is that parameters also need to be injected (which kind of logically 
makes sense).

I am not exactly sure whether datum->syntax and syntax->datum go through 
the entire expression but if they do, the implementation will be very 
inefficient. (If anybody knows more on that, I would be interested.)

Implementation:

#lang racket

(provide (for-syntax ir-macro-transformer er-macro-transformer))

(begin-for-syntax
  (require (for-syntax (only-in racket/base lambda syntax with-syntax)))
  (define-syntax ir-macro-transformer 
(lambda (stx)
  (with-syntax ([(_ proc) stx])
#'(lambda (x)
(datum->syntax #'proc (proc (syntax->datum x) (lambda (v) 
(datum->syntax x v
  (define-syntax er-macro-transformer 
(lambda (stx)
  (with-syntax ([(_ proc) stx])
#'(lambda (x)
(datum->syntax x (proc (syntax->datum x) (lambda (v) 
(datum->syntax #'proc v)

Examples:

; loop example from Shu-Hung's post is now working as expected

(define-syntax loop
  (ir-macro-transformer
   (lambda (expr inject)
 (let ((body (cdr expr)))
   `(call-with-current-continuation
 (lambda (,(inject 'exit))
   (let f () (begin .,(inject body)) (f   ; notice the 
(inject body) here... dropping the inject will break the code

;=> 543210

; the following suggests that the lexical environments work as they should

(let ([x 1]
  [y 2])
  (swap! x y)
  x)

=> 2

(define t 3)

(define-syntax tripple
  (ir-macro-transformer
   (lambda (expr inject)
 (let ([a (cadr expr)])
   `(* t ,(inject a))

(let ([t 29]
  [* -])
  (tripple 2))

;=> 6

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] MIT/Chicken/Chibi Scheme style ir-macro-transformer in Racket

2018-08-29 Thread Shu-Hung You
I think the following program illustrates the idea, though it doesn't
really work:

#lang racket

(begin-for-syntax
  (define (syntax-pair->cons stx)
(define datum (syntax-e stx))
(cond
  [(list? datum)
   (map syntax-pair->cons datum)]
  [(pair? datum)
   (cons (syntax-pair->cons (car datum))
 (syntax-pair->cons (cdr datum)))]
  [else stx]))

  (define (ir-macro-transformer proc)
(lambda (stx)
  (define transformed-s-expr
(proc (syntax-pair->cons stx)
  (λ (id) (datum->syntax stx id
  (datum->syntax #'here transformed-s-expr ;; hack

(define-syntax loop
  (ir-macro-transformer
   (lambda (expr inject)
 (let ((body (cdr expr)))
   `(call-with-current-continuation
 (lambda (,(inject 'exit))
   (let f () ,@body (f

(let ([i 0])
  (loop
   (printf "i = ~a\n" i)
   (set! i (+ i 1))
   (when (>= i 5)
 (exit 'ok

Syntax objects are the representation of syntaxes used in expansion
and compilation. Racket doesn't just use quoted expressions since the
expander and the compiler need to keep track of lexical information,
source location and other properties. The form #' and #` are
constructors of syntax objects that take templates and produce the
syntax objects with the desired shape, just like ' and ` are
constructors of quoted s-expressions.

If you want to provide a function that manipulates lists and symbols
as a transformer, ir-macro-transformer would at least need to be a
function that maps between syntax objects and quoted expressions. Such
maps are imperfect though.

One problem I found with the above program is that `inject' doesn't
have the desired lexical context, so the while example does not work.

phase 1 phase 0
 + syntax object (instead of quoted s-expression)
 |
transformer
(lambda functions bound
 using define-syntax)
 |
 +---> syntax object

On Wed, Aug 29, 2018 at 7:25 AM, Philip McGrath
 wrote:
> For future reference, you should try the wonderful macro stepper in
> DrRacket, which shows you exactly how expansion happens. It can even handle
> buggy, non-terminating examples like the version of `test` you wrote.
> -Philip
>
>
> On Wed, Aug 29, 2018 at 7:20 AM Philip McGrath 
> wrote:
>>
>> I'm not familiar with how `ir-macro-transformer` is supposed to work, but
>> your macro is currently fails for essentially the same reason as:
>> (define-syntax (diverge stx)
>>   stx
>>
>> The `expr` given to `test` is a syntactic list beginning with the
>> identifier `test`, so including it in the output triggers another expansion
>> of `test`, infinitely.
>>
>> -Philip
>>
>>
>> On Wed, Aug 29, 2018 at 7:06 AM t791bc via Racket Users
>>  wrote:
>>>
>>> Hi,
>>>
>>> while syntax-case has some advantages, I was trying to implement a
>>> Chicken style ir-macro-transformer in Racket so that I can write macros that
>>> will run both on Racket and systems like Chicken/Chibi Scheme.
>>>
>>> My first attempt was as follows:
>>>
>>> (begin-for-syntax
>>>   (require (for-syntax racket/base))
>>>   (define-syntax (ir-macro-transformer stx)
>>> (syntax-case stx ()
>>>   [(_ ir-trans)
>>> #'(lambda (x)
>>> #`(ir-trans #,(syntax->datum x) 23))])))
>>>
>>> (define-syntax test
>>>   (ir-macro-transformer
>>>(lambda (expr inject)
>>>expr)))
>>>
>>> The result was "Background expansion terminated abnormally (out of
>>> memory)". If the #, is taken out I get an error that x is unbound. As should
>>> be obvious from the above,  I find it hard to reason about all those syntax
>>> effects - which only makes me want the ir-macro-transformer more despite all
>>> its shortcomings.
>>>
>>> Any help would be highly appreciated.
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to racket-users+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] MIT/Chicken/Chibi Scheme style ir-macro-transformer in Racket

2018-08-29 Thread Philip McGrath
For future reference, you should try the wonderful macro stepper in
DrRacket, which shows you exactly how expansion happens. It can even handle
buggy, non-terminating examples like the version of `test` you wrote.
-Philip


On Wed, Aug 29, 2018 at 7:20 AM Philip McGrath 
wrote:

> I'm not familiar with how `ir-macro-transformer` is supposed to work, but
> your macro is currently fails for essentially the same reason as:
> (define-syntax (diverge stx)
>   stx
>
> The `expr` given to `test` is a syntactic list beginning with the
> identifier `test`, so including it in the output triggers another expansion
> of `test`, infinitely.
>
> -Philip
>
>
> On Wed, Aug 29, 2018 at 7:06 AM t791bc via Racket Users <
> racket-users@googlegroups.com> wrote:
>
>> Hi,
>>
>> while syntax-case has some advantages, I was trying to implement a
>> Chicken style ir-macro-transformer in Racket so that I can write macros
>> that will run both on Racket and systems like Chicken/Chibi Scheme.
>>
>> My first attempt was as follows:
>>
>> (begin-for-syntax
>>   (require (for-syntax racket/base))
>>   (define-syntax (ir-macro-transformer stx)
>> (syntax-case stx ()
>>   [(_ ir-trans)
>> #'(lambda (x)
>> #`(ir-trans #,(syntax->datum x) 23))])))
>>
>> (define-syntax test
>>   (ir-macro-transformer
>>(lambda (expr inject)
>>expr)))
>>
>> The result was "Background expansion terminated abnormally (out of
>> memory)". If the #, is taken out I get an error that x is unbound. As
>> should be obvious from the above,  I find it hard to reason about all those
>> syntax effects - which only makes me want the ir-macro-transformer more
>> despite all its shortcomings.
>>
>> Any help would be highly appreciated.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] MIT/Chicken/Chibi Scheme style ir-macro-transformer in Racket

2018-08-29 Thread Philip McGrath
I'm not familiar with how `ir-macro-transformer` is supposed to work, but
your macro is currently fails for essentially the same reason as:
(define-syntax (diverge stx)
  stx

The `expr` given to `test` is a syntactic list beginning with the
identifier `test`, so including it in the output triggers another expansion
of `test`, infinitely.

-Philip


On Wed, Aug 29, 2018 at 7:06 AM t791bc via Racket Users <
racket-users@googlegroups.com> wrote:

> Hi,
>
> while syntax-case has some advantages, I was trying to implement a Chicken
> style ir-macro-transformer in Racket so that I can write macros that will
> run both on Racket and systems like Chicken/Chibi Scheme.
>
> My first attempt was as follows:
>
> (begin-for-syntax
>   (require (for-syntax racket/base))
>   (define-syntax (ir-macro-transformer stx)
> (syntax-case stx ()
>   [(_ ir-trans)
> #'(lambda (x)
> #`(ir-trans #,(syntax->datum x) 23))])))
>
> (define-syntax test
>   (ir-macro-transformer
>(lambda (expr inject)
>expr)))
>
> The result was "Background expansion terminated abnormally (out of
> memory)". If the #, is taken out I get an error that x is unbound. As
> should be obvious from the above,  I find it hard to reason about all those
> syntax effects - which only makes me want the ir-macro-transformer more
> despite all its shortcomings.
>
> Any help would be highly appreciated.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] MIT/Chicken/Chibi Scheme style ir-macro-transformer in Racket

2018-08-29 Thread t791bc via Racket Users
Hi,

while syntax-case has some advantages, I was trying to implement a Chicken 
style ir-macro-transformer in Racket so that I can write macros that will 
run both on Racket and systems like Chicken/Chibi Scheme. 

My first attempt was as follows:

(begin-for-syntax
  (require (for-syntax racket/base))
  (define-syntax (ir-macro-transformer stx)
(syntax-case stx ()
  [(_ ir-trans)
#'(lambda (x)
#`(ir-trans #,(syntax->datum x) 23))])))

(define-syntax test
  (ir-macro-transformer
   (lambda (expr inject)
   expr)))

The result was "Background expansion terminated abnormally (out of 
memory)". If the #, is taken out I get an error that x is unbound. As 
should be obvious from the above,  I find it hard to reason about all those 
syntax effects - which only makes me want the ir-macro-transformer more 
despite all its shortcomings. 

Any help would be highly appreciated. 


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.