Hi Matt,

The following is one way to define the macro.
The recipe is as follows:

1. Define format-functions that produce the identifiers needed.
2. Write the template (the syntax/loc expression) using the generated names
3. Wrap the template with a with-syntax that binds the generated names
    to names produced by format functions

/Jens Axel


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

; Example:
; (introduce tag a b c)
;  =>
; (begin
;   (define tag-ht (make-hasheq))
;   (define (tag-get-a)    (hash-ref  tag-ht 'a #f)) ...
;   (define (tag-set-a! v) (hash-set! tag-ht 'a v))) ...)

(begin-for-syntax
  (define (format-tag-ht        ctx loc tag)      (format-id ctx "~a-ht"
  tag      #:source loc))
  (define (format-tag-get-name  ctx loc tag name) (format-id ctx
"~a-get-~a"  tag name #:source loc))
  (define (format-tag-set-name! ctx loc tag name) (format-id ctx
"~a-set-~a!" tag name #:source loc)))

(define-syntax (introduce stx)
  (syntax-parse stx
    [(_introduce tag:id x:id ...)
     (define tag-sym (syntax-e #'tag))
     (with-syntax ([tag-ht           (format-tag-ht stx #'tag tag-sym)]
                   [(tag-get-x ...)  (for/list ([name (in-syntax #'(x
...))])
                                       (format-tag-get-name stx name
tag-sym (syntax-e name)))]
                   [(tag-set-x! ...) (for/list ([name (in-syntax #'(x
...))])
                                       (format-tag-set-name! stx name
tag-sym (syntax-e name)))])
       (syntax/loc stx
         (begin
           (define tag-ht (make-hasheq))
           (define (tag-get-x)    (hash-ref  tag-ht 'x #f)) ...
           (define (tag-set-x! v) (hash-set! tag-ht 'x v))  ...)))]))

(introduce label a b c)
(label-set-a! 42)
(label-get-a)




Den tir. 11. dec. 2018 kl. 23.08 skrev Matt Jadud <m...@jadud.com>:

> Many thanks, John.
>
> I've made it that far. I'll be more specific...
>
> Conceptually, I want...
>
> (define-syntax (introduce stx)
>   (syntax-case stx ()
>     [(_ tag ids ...)
>      (with-syntax ([(getters ...)
>                     (map (λ (id) (format-id #'tag "~a-get-~a" tag id)) ids
> ...)])
>        #`(begin
>            (define (getters) 'get-something) ...)
>        )]))
>
> so that
>
> (introduce tag a b c)
>
> produces
>
> (define (tag-get-a) ...)
> (define (tag-get-b) ...)
> (define (tag-get-c) ...)
>
> I am aware that I can't use 'map' in the context above. I... know, but do
> not understand, that the LHS of with-syntax is a syntax pattern. However,
> how I would generate a syntactic form that I would match against that
> pattern where I generate the identifiers for the defines (and then how I
> would appropriately use ... to indicate repetition of the defines form, if
> the structure above is even close) is what I'm wrestling with.
>
> Cheers,
> M
>
>
>
> On Tue, Dec 11, 2018 at 4:16 PM John Clements <cleme...@brinckerhoff.org>
> wrote:
>
>> In answer to at least one of your questions, top-level “begin”s are
>> “spliced” into their context to produce top-level bindings. So, for
>> instance,
>>
>> #lang racket
>>
>> (begin
>>   (define a 3)
>>   (define b 4))
>>
>> (+ b a)
>>
>> … evaluates to 7
>>
>> I think this is not the only issue you’re going to run into, but it’s at
>> least one of them.
>>
>> Apologies if I misunderstood your question!
>>
>> John
>>
>> > On Dec 11, 2018, at 1:12 PM, Matt Jadud <m...@jadud.com> wrote:
>> >
>> > Hi all,
>> >
>> > I'm exploring macros today.
>> >
>> > I'd like to introduce a set of bindings. For example, I would like
>> >
>> > (introduce tag a b c)
>> >
>> > to become
>> >
>> > (define (tag-get-a) ...)
>> > (define (tag-set-a! v) ...)
>> >
>> > or somesuch set of defines. I've discovered with-syntax and format-id,
>> and have had good success with introducing singular defines, but how to
>> expand the list of ids into a list of defines is something I'm not seeing
>> yet. Somehow, I feel like a macro that simply follows the natural recursion
>> on the syntax objects is probably the answer, but... that's like saying the
>> answer to a question in quantum mechanics is either 0, 1, or h-bar/2...
>> >
>> > I'm looking at the define-cbr example in the documentation, and
>> suspicious that I'm reaching a point with my macro authoring where the ...
>> and (... ...) are leaving me confused as to which layer of expansion I'm
>> dealing with as I look at my code. Hence, I'm asking for a nudge in the
>> right direction.
>> >
>> > Cheers,
>> > Matt
>> >
>> >
>> >
>> > --
>> > 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.
>


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

Reply via email to