Re: [racket-users] How to use the REPL with #lang s-exp "expander.rkt"

2017-03-21 Thread Jan Hondebrink
On Monday, March 20, 2017 at 7:25:35 PM UTC+1, Matthew Butterick wrote:
> On Mar 20, 2017, at 2:59 AM, Jan Hondebrink  wrote:
> 
> Testing top-interaction with expression: (#%top-interaction + 1 2)
> . +: unbound identifier;
> also, no #%app syntax transformer is bound in: +
> 
> So when you pass an expression through to the racket/base #%module-begin, it 
> can use the racket/base bindings, but if you pass an expression through to 
> the racket/base #%top-interaction it cannot. How can you make 
> #%top-interaction use the racket/base bindings?
> 
> 
> 
> 
> When you start a source file with this line:
> 
> 
> #lang s-exp "ME-expander.rkt"
> 
> 
> You are telling Racket to 1) parse the code into expressions using the 
> default `s-exp` (aka Racket-style) reader and 2) use "ME-expander.rkt" as the 
> expander module.
> 
> 
> The expander module supplies the initial set of bindings for the code. 
> Conversely, if the expander doesn't provide a certain binding, the code can't 
> use it, and you get an unbound-identifier error.
> 
> 
> Even though "ME-expander.rkt" is written with `#lang racket/base`, it does 
> not automatically export the bindings from `racket/base`. If that's the 
> behavior you want, you need to add this to "ME-expander.rkt":
> 
> 
> (provide (all-from-out racket/base))
> 
> 
> Though you have to be a little careful if you plan to override bindings from 
> `racket/base` with your own (say, `#%top-interaction` and `#%module-begin`). 
> In that case, you can omit those bindings with `except-out`:
> 
> 
> (provide (except-out (all-from-out racket/base) #%top-interaction 
> #%module-begin))

Thanks for the explanation! It turns out that I don't need "ME-expander.rkt" to 
export any bindings apart from #%module-begin and #%top-interaction. I 
overcomplicated Greg's suggestion to supply my own #%top-interaction by 
thinking it needed to pass its result "up the chain" to the racket/base 
#%top-interaction. Just as Beautiful Racket taught me to do with #%module-begin.

But for #%top-interaction it is not necessary. In "ME-expander.rkt" the 
following works:

#lang racket/base

(require (for-syntax racket/base))

(define-syntax (ME-module-begin stx)
  (syntax-case stx ()
[(_ s-expressions ...)
 #'(#%module-begin (apply values
  (for/list ([expr (list 's-expressions ...)])
(m-eval expr global-environment)))
   )]))

(define-syntax (#%top-interaction stx)
  #`(m-eval '#,(cdr (syntax-e stx)) global-environment))

(provide (rename-out (ME-module-begin #%module-begin))
 #%top-interaction)

(define (m-eval exp env)
  (...

---
So let me try and formulate my understanding if this: 
when using #lang s-expr "ME-expander.rkt" in a source file:
- The code in the file (DrRacket definition window) is caught by ME-expander's 
ME-module-begin which wraps the transformed code in (#%module-begin ...) which 
is then evaluated using the bindings from racket/base + the bindings in 
ME-expander (although I'm still a bit shady on how *exactly* that works)
- Anything typed in the REPL window is caught by ME-expander's 
#%top-interaction which just transforms code without wrapping it in a special 
form, which is then evaluated using the bindings from racket/base + the 
bindings in ME-expander.

-- 
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] How to use the REPL with #lang s-exp "expander.rkt"

2017-03-20 Thread Matthew Butterick

> On Mar 20, 2017, at 2:59 AM, Jan Hondebrink  wrote:
> 
> Testing top-interaction with expression: (#%top-interaction + 1 2)
> . +: unbound identifier;
> also, no #%app syntax transformer is bound in: +
> 
> So when you pass an expression through to the racket/base #%module-begin, it 
> can use the racket/base bindings, but if you pass an expression through to 
> the racket/base #%top-interaction it cannot. How can you make 
> #%top-interaction use the racket/base bindings?


When you start a source file with this line:

#lang s-exp "ME-expander.rkt"

You are telling Racket to 1) parse the code into expressions using the default 
`s-exp` (aka Racket-style) reader and 2) use "ME-expander.rkt" as the expander 
module.

The expander module supplies the initial set of bindings for the code. 
Conversely, if the expander doesn't provide a certain binding, the code can't 
use it, and you get an unbound-identifier error.

Even though "ME-expander.rkt" is written with `#lang racket/base`, it does not 
automatically export the bindings from `racket/base`. If that's the behavior 
you want, you need to add this to "ME-expander.rkt":

(provide (all-from-out racket/base))

Though you have to be a little careful if you plan to override bindings from 
`racket/base` with your own (say, `#%top-interaction` and `#%module-begin`). In 
that case, you can omit those bindings with `except-out`:

(provide (except-out (all-from-out racket/base) #%top-interaction 
#%module-begin))



-- 
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] How to use the REPL with #lang s-exp "expander.rkt"

2017-03-20 Thread Jan Hondebrink
On Sunday, March 19, 2017 at 6:46:24 PM UTC+1, Greg Hendershott wrote:
> > This works fine in the ME-test.rkt Definition window, but not in the REPL 
> > which doesn't have bindings for anything. I would like anything typed into 
> > the ME-test.rkt REPL to be used as argument to the ME-module-begin macro in 
> > ME-expander.rkt.
> 
> I'm not sure but since no one has replied yet:
> 
> #%top-interaction is used by a REPL. It looks like you're providing
> the one from racket/base;
> maybe you need to provide your own that uses your m-eval?

Ah, cool, thanks! It never occurred to me that that was possible! A lot to 
learn for sure. Now to make it work...

I can now loop the REPL input through m-eval first, but it doesn't help me, 
which I'll try to illustrate: If I just pass the relevant part of the REPL 
input without modification through to the racket/base #%top-interaction:

(define-syntax (ME-top-interaction stx)
  (printf "Testing top-interaction with expression: ~a\n" (syntax->datum stx))
  #`(#%top-interaction . #,(cdr (syntax-e stx

(provide (rename-out (ME-top-interaction #%top-interaction)))

In ME-test.rkt:

> 4
Testing top-interaction with expression: (#%top-interaction . 4)
. ?: literal data is not allowed;
 no #%datum syntax transformer is bound in: 4

After providing #%datum in ME-expander.rkt:

> 4
Testing top-interaction with expression: (#%top-interaction . 4)
4
> (+ 1 2)
Testing top-interaction with expression: (#%top-interaction + 1 2)
. +: unbound identifier;
 also, no #%app syntax transformer is bound in: +

So when you pass an expression through to the racket/base #%module-begin, it 
can use the racket/base bindings, but if you pass an expression through to the 
racket/base #%top-interaction it cannot. How can you make #%top-interaction use 
the racket/base bindings?

I obviously have a very muddied and incomplete understanding of this whole 
subject, so please forgive me for any stupid/obvious questions.

-- 
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] How to use the REPL with #lang s-exp "expander.rkt"

2017-03-19 Thread Greg Hendershott
> This works fine in the ME-test.rkt Definition window, but not in the REPL 
> which doesn't have bindings for anything. I would like anything typed into 
> the ME-test.rkt REPL to be used as argument to the ME-module-begin macro in 
> ME-expander.rkt.

I'm not sure but since no one has replied yet:

#%top-interaction is used by a REPL. It looks like you're providing
the one from racket/base;
maybe you need to provide your own that uses your m-eval?

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