Re: [racket-users] Re: catch and bind an unbound id in a macro

2019-04-21 Thread zeRusski

On Saturday, 20 April 2019 23:55:50 UTC+1, Stephen Chang wrote:
>
> fwiw, I think here is a working version along the lines of your 
> original attempt. 
>

I see what you did there. This is both icky (as requested) and cute  :0 
Thanks, it's actually illuminating 

-- 
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] Re: catch and bind an unbound id in a macro

2019-04-20 Thread Stephen Chang
fwiw, I think here is a working version along the lines of your
original attempt.

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

(define-syntax set/define
  (syntax-parser
[(_ h k v)
 #:when (with-handlers ([exn:fail:syntax:unbound? (lambda _ #f)])
  (local-expand #'h 'expression null))
 #'(hash-set! h 'k v)]
[(_ h k v)
 #'(begin
 (define h (make-hash))
 (hash-set! h 'k v))]))

(set/define h a 1)
(set/define h a 2)
(hash-set! h 'b 42)
h ; '#hash((b . 42) (a . 2))

(define bar (make-hash))
(set/define bar a 1)
bar ; '#hash((a . 1))

(define (foo)
  (set/define local-h a 1)
  (set/define local-h a 2)
  (hash-set! local-h 'b 42)
  local-h)

(foo) ; '#hash((b . 42) (a . 2))

On Sat, Apr 20, 2019 at 6:24 PM Shu-Hung You
 wrote:
>
> identifier-binding returns #f for top-level bindings. I believe
> make-base-eval is evaluating expressions at top-level.
>
> On Sat, Apr 20, 2019 at 4:56 PM Ryan Kramer  wrote:
> >
> > Below is a short example showing that identifier-binding doesn't seem to 
> > work quite right with Scribble's make-base-eval. It works fine with 
> > make-evaluator from racket/sandbox.
> >
> > I'm not sure why this is, but using syntax-local-value instead works 
> > everywhere. (Implementation here: 
> > https://github.com/default-kramer/plisqin/commit/2723d7c11be5b6938e681ea869e8b9f4957849b0#diff-1a91d998f2be05413392d588a89323d3R31)
> >
> > #lang racket
> > (require scribble/eval
> >  racket/sandbox)
> >
> > (define (setup eval)
> >   (eval
> >'(define-syntax (define-if-not stx)
> >   (syntax-case stx ()
> > [(_ id val)
> >  (identifier-binding #'id)
> >  #'(void)]
> > [(_ id val)
> >  #'(define id val)]
> >
> > (define scribble-eval (make-base-eval))
> > (scribble-eval '(require racket))
> > (setup scribble-eval)
> >
> > (define racket-eval (make-evaluator 'racket))
> > (setup racket-eval)
> >
> > (scribble-eval '(define-if-not foo 1))
> > (scribble-eval '(define-if-not foo 2))
> > (scribble-eval 'foo) ; want 1, get 2
> >
> > (racket-eval '(define-if-not bar 3))
> > (racket-eval '(define-if-not bar 4))
> > (racket-eval 'bar) ; want 3, get 3
> >
> > --
> > 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] Re: catch and bind an unbound id in a macro

2019-04-20 Thread Shu-Hung You
identifier-binding returns #f for top-level bindings. I believe
make-base-eval is evaluating expressions at top-level.

On Sat, Apr 20, 2019 at 4:56 PM Ryan Kramer  wrote:
>
> Below is a short example showing that identifier-binding doesn't seem to work 
> quite right with Scribble's make-base-eval. It works fine with make-evaluator 
> from racket/sandbox.
>
> I'm not sure why this is, but using syntax-local-value instead works 
> everywhere. (Implementation here: 
> https://github.com/default-kramer/plisqin/commit/2723d7c11be5b6938e681ea869e8b9f4957849b0#diff-1a91d998f2be05413392d588a89323d3R31)
>
> #lang racket
> (require scribble/eval
>  racket/sandbox)
>
> (define (setup eval)
>   (eval
>'(define-syntax (define-if-not stx)
>   (syntax-case stx ()
> [(_ id val)
>  (identifier-binding #'id)
>  #'(void)]
> [(_ id val)
>  #'(define id val)]
>
> (define scribble-eval (make-base-eval))
> (scribble-eval '(require racket))
> (setup scribble-eval)
>
> (define racket-eval (make-evaluator 'racket))
> (setup racket-eval)
>
> (scribble-eval '(define-if-not foo 1))
> (scribble-eval '(define-if-not foo 2))
> (scribble-eval 'foo) ; want 1, get 2
>
> (racket-eval '(define-if-not bar 3))
> (racket-eval '(define-if-not bar 4))
> (racket-eval 'bar) ; want 3, get 3
>
> --
> 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] Re: catch and bind an unbound id in a macro

2019-04-20 Thread Alexis King
The third argument to identifier-binding, top-level-symbol?, controls the 
result of identifier-binding when the identifier is bound to a top-level 
binding (and both the REPL and sandboxed evaluators are kinds of top-level 
evaluation). The docs elaborate this way:
> The result is (list source-id) if id-stx has a top-level binding and 
> top-level-symbol? is true.
> 
> The result is #f if id-stx has a top-level binding and top-level-symbol? is 
> #f or if id-stx is unbound. An unbound identifier is typically treated the 
> same as an identifier whose top-level binding is a variable.

I’m not actually sure what the full implications are of passing #t for that 
argument, but you might try that and see if it works for you. A disadvantage of 
using syntax-local-value is that it will fail if the identifier is bound to a 
runtime variable rather than syntax.

Alexis

> On Apr 20, 2019, at 16:56, Ryan Kramer  wrote:
> 
> Below is a short example showing that identifier-binding doesn't seem to work 
> quite right with Scribble's make-base-eval. It works fine with make-evaluator 
> from racket/sandbox.
> 
> I'm not sure why this is, but using syntax-local-value instead works 
> everywhere. (Implementation here: 
> https://github.com/default-kramer/plisqin/commit/2723d7c11be5b6938e681ea869e8b9f4957849b0#diff-1a91d998f2be05413392d588a89323d3R31)
> 
> #lang racket
> (require scribble/eval
>  racket/sandbox)
> 
> (define (setup eval)
>   (eval
>'(define-syntax (define-if-not stx)
>   (syntax-case stx ()
> [(_ id val)
>  (identifier-binding #'id)
>  #'(void)]
> [(_ id val)
>  #'(define id val)]
> 
> (define scribble-eval (make-base-eval))
> (scribble-eval '(require racket))
> (setup scribble-eval)
> 
> (define racket-eval (make-evaluator 'racket))
> (setup racket-eval)
> 
> (scribble-eval '(define-if-not foo 1))
> (scribble-eval '(define-if-not foo 2))
> (scribble-eval 'foo) ; want 1, get 2
> 
> (racket-eval '(define-if-not bar 3))
> (racket-eval '(define-if-not bar 4))
> (racket-eval 'bar) ; want 3, get 3

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