Re: [racket-users] Macro that does substitution

2015-08-03 Thread Matthias Felleisen


Correct. There should never be an exception because the value never ends up in 
a strict position. -- Matthias





On Aug 3, 2015, at 12:44 PM, Andrew Gwozdziewycz  wrote:

> On Wed, Jul 29, 2015 at 9:30 AM, Matthias Felleisen  
> wrote:
> 
> On Jul 29, 2015, at 7:50 AM, Klaus Ostermann wrote:
> 
> > Thanks, Matthew and Matthias. The service on this mailing list is 
> > incredible!
> >
> > I know it is not cbn because it is local, but a better name didn't come to 
> > my mind and it is what I need to solve my problem.
> 
> 
> It's not about locality, the problem is that it runs the thunks at the wrong 
> time.
> 
> Example:
> 
>   (let ((x (/ 1 0)))
> ((lambda (y) (displayln `(hello world)))
>  x))
> 
> in CBN would _first_ show hello-world and then raise the exception but your 
> let-cbn will raise the exception first and never display anything on the 
> output port.
> 
> I'm confused. Why would this raise an exception? x is never in strict 
> position, as `y` is unused in the lambda. But, I could simply be missing 
> something, and if so, please ignore. :)
> 
> -- 
> http://www.apgwoz.com

-- 
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] Macro that does substitution

2015-08-03 Thread Andrew Gwozdziewycz
On Wed, Jul 29, 2015 at 9:30 AM, Matthias Felleisen 
wrote:

>
> On Jul 29, 2015, at 7:50 AM, Klaus Ostermann wrote:
>
> > Thanks, Matthew and Matthias. The service on this mailing list is
> incredible!
> >
> > I know it is not cbn because it is local, but a better name didn't come
> to my mind and it is what I need to solve my problem.
>
>
> It's not about locality, the problem is that it runs the thunks at the
> wrong time.
>
> Example:
>
>   (let ((x (/ 1 0)))
> ((lambda (y) (displayln `(hello world)))
>  x))
>
> in CBN would _first_ show hello-world and then raise the exception but
> your let-cbn will raise the exception first and never display anything on
> the output port.
>

I'm confused. Why would this raise an exception? x is never in strict
position, as `y` is unused in the lambda. But, I could simply be missing
something, and if so, please ignore. :)

-- 
http://www.apgwoz.com

-- 
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] Macro that does substitution

2015-07-29 Thread Matthias Felleisen

On Jul 29, 2015, at 7:50 AM, Klaus Ostermann wrote:

> Thanks, Matthew and Matthias. The service on this mailing list is incredible!
> 
> I know it is not cbn because it is local, but a better name didn't come to my 
> mind and it is what I need to solve my problem.


It's not about locality, the problem is that it runs the thunks at the wrong 
time. 

Example: 

  (let ((x (/ 1 0)))
((lambda (y) (displayln `(hello world))) 
 x))

in CBN would _first_ show hello-world and then raise the exception but your 
let-cbn will raise the exception first and never display anything on the output 
port. 

-- Matthias

-- 
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.


smime.p7s
Description: S/MIME cryptographic signature


Re: [racket-users] Macro that does substitution

2015-07-29 Thread Jens Axel Søgaard
Something like this:

#lang racket
(require (for-syntax syntax/parse racket/syntax))

(define-syntax (let-cbn stx)
  (syntax-parse stx
[(_let-cbn ([x:id e:expr] ...) body)
 ;; For each identifier x we need a new identifier bound to (λ () e)
 (define/with-syntax (x* ...) (generate-temporaries #'(x ...)))
 (syntax/loc stx
   (let ([x* (λ () e)] ...)
 ;; in the boby we need to rewrite x to (x*)
 ;; we need to handle references, applications and assignments
 (let-syntax ([x (λ (so)
   (syntax-parse so
 #:literals (set!)
 [_x:id (syntax/loc so (x*))]
; reference to x*
 [(_x:id e ...) (syntax/loc so ((x*) e ...))]
; application
  [(set! _ __)  ; assignment
   (raise-syntax-error
'let-cbn "assignment to cbn variable not
allowed" #'stx so)]))]
  ...)
   body)))]))

> (let-cbn ([x 0] [y (/ 1 0)]) x)
0

> (let-cbn ([x 0] [y (/ 1 0)]) y)
/: division by zero


2015-07-29 15:28 GMT+02:00 Klaus Ostermann :

> I'd like to have a macro "let-cbn" which does this:
>
> (let-cbn ((x1 e1) ...) body)
>
> is transformed to
>
> (let ((x1 (thunk e1)) ...) newbody)
>
> where newbody is the result of replacing every occurence of x1... by
> (x1)... .
>
> What is the best way to do that in Racket?
>
> --
> 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.
>



-- 
-- 
Jens Axel Søgaard

-- 
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] Macro that does substitution

2015-07-29 Thread Klaus Ostermann
Thanks, Matthew and Matthias. The service on this mailing list is incredible!

I know it is not cbn because it is local, but a better name didn't come to my 
mind and it is what I need to solve my problem.

Klaus

-- 
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] Macro that does substitution

2015-07-29 Thread Matthias Felleisen

(I was 2/3 there when I noticed Matthew's response.)

Klaus, fwiw this is NOT cbn. A cbn let will run the thunks only when they show 
up in strict positions. Not every identifier shows up in strict positions. 


On Jul 29, 2015, at 7:37 AM, Matthew Flatt wrote:

> At Wed, 29 Jul 2015 06:28:48 -0700 (PDT), Klaus Ostermann wrote:
>> I'd like to have a macro "let-cbn" which does this:
>> 
>> (let-cbn ((x1 e1) ...) body)
>> 
>> is transformed to
>> 
>> (let ((x1 (thunk e1)) ...) newbody)
>> 
>> where newbody is the result of replacing every occurence of x1... by (x1)... 
>> .
>> 
>> What is the best way to do that in Racket?
> 
> I'd expand
> 
> (let-cbn ((x1 e1) ...) body)
> 
> to
> 
> (let ((x1 (thunk e1)) ...)
>   (let-syntax ([x1 (make-thunk-call #'x1)] ...)
>  body))
> 
> where `make-thunk-call` generates a macro that expands to a call of the
> given argument:
> 
> (define-for-syntax (make-thunk-call id-stx)
>(lambda (stx)
>  (with-syntax ([id id-stx])
>(syntax-case stx ()
>  [(_ arg ...) (syntax/loc stx (id arg ...))]
>  [_ #'(id)]
> 
> A complete example is below.
> 
> 
> 
> #lang racket/base
> (require (for-syntax racket/base))
> 
> (define-for-syntax (make-thunk-call id-stx)
>  (lambda (stx)
>(with-syntax ([id id-stx])
>  (syntax-case stx ()
>[(_ arg ...) (syntax/loc stx (id arg ...))]
>[_ #'(id)]
> 
> (define-syntax-rule (thunk e0 e ...)
>  (lambda () e0 e ...))
> 
> (define-syntax-rule (let-cbn ((x1 e1) ...) body0 body ...)
>  (let ((x1 (thunk e1)) ...)
>(let-syntax ([x1 (make-thunk-call #'x1)] ...)
>  body0 body ...)))
> 
> (let-cbn ([x (begin (displayln "here") 1)])
>  (list x x))
> 
> -- 
> 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.


smime.p7s
Description: S/MIME cryptographic signature


Re: [racket-users] Macro that does substitution

2015-07-29 Thread Matthew Flatt
At Wed, 29 Jul 2015 06:28:48 -0700 (PDT), Klaus Ostermann wrote:
> I'd like to have a macro "let-cbn" which does this:
> 
> (let-cbn ((x1 e1) ...) body)
> 
> is transformed to
> 
> (let ((x1 (thunk e1)) ...) newbody)
> 
> where newbody is the result of replacing every occurence of x1... by (x1)... .
> 
> What is the best way to do that in Racket?

I'd expand

 (let-cbn ((x1 e1) ...) body)

to

 (let ((x1 (thunk e1)) ...)
   (let-syntax ([x1 (make-thunk-call #'x1)] ...)
  body))

where `make-thunk-call` generates a macro that expands to a call of the
given argument:

 (define-for-syntax (make-thunk-call id-stx)
(lambda (stx)
  (with-syntax ([id id-stx])
(syntax-case stx ()
  [(_ arg ...) (syntax/loc stx (id arg ...))]
  [_ #'(id)]

A complete example is below.



#lang racket/base
(require (for-syntax racket/base))

(define-for-syntax (make-thunk-call id-stx)
  (lambda (stx)
(with-syntax ([id id-stx])
  (syntax-case stx ()
[(_ arg ...) (syntax/loc stx (id arg ...))]
[_ #'(id)]

(define-syntax-rule (thunk e0 e ...)
  (lambda () e0 e ...))

(define-syntax-rule (let-cbn ((x1 e1) ...) body0 body ...)
  (let ((x1 (thunk e1)) ...)
(let-syntax ([x1 (make-thunk-call #'x1)] ...)
  body0 body ...)))

(let-cbn ([x (begin (displayln "here") 1)])
  (list x x))

-- 
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.