abstraction suggestion?

45 views
Skip to first unread message

Shriram Krishnamurthi

unread,
Aug 31, 2020, 1:06:43 PM8/31/20
to Racket Users
I'm having some trouble abstracting over this code. Any suggestions?

I have numerous files that follow this boilerplate:

#lang racket

(require <LIBRARY FILE>)

(provide (rename-out [mod-begin #%module-begin]
                     [ti        #%top-interaction]))

(define-values (namespaces lang-print-names)
  <THE ONLY PART THAT CHANGES>)

(define-syntax (multi-runner stx)
  (syntax-case stx (TEST)
    [(_ (TEST e r ...))
     #`(test-output 'e (list 'r ...) namespaces)]
    [(_ e)
     #`(show-output 'e namespaces lang-print-names)]))

(define-syntax mod-begin
  (λ (stx)
    (syntax-case stx ()
      [(_ b ...)
       #'(#%printing-module-begin (multi-runner b) ...)])))

(define-syntax ti
  (λ (stx)
    (syntax-case stx ()
      ([_ . e]
       #'(#%top-interaction . (multi-runner e))))))

I've abstract most of the details into `test-output` and `show-output` into <LIBRARY FILE>. I would ideally like to move as much of what's left as possible into the same file. 

The key problem is that the MB and TI depend on `multi-runner`, which in turn depends on `namespaces`, which is a name at run time. As long as everything is in the same module, no problem. But when I start to move the boilerplate out…

Concrete suggestions welcome — I've tried several different things (various forms of abstraction, syntax parameters, etc.) without luck.

Thanks,
Shriram

Hendrik Boom

unread,
Aug 31, 2020, 1:24:42 PM8/31/20
to Racket Users
Maybe a macro or two? Perhaps a nonhygienic one?

-- hendrik

>
> Thanks,
> Shriram
>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/362f807e-3561-4be6-8b4d-937776fea36bn%40googlegroups.com.

Philip McGrath

unread,
Aug 31, 2020, 1:32:43 PM8/31/20
to Racket Users
There might be a better way, but I'd probably make a language for writing this kind of module, in the spirit of `#lang syntax/module-reader`, where its `#%module-begin` would expect the module body to be `<THE ONLY PART THAT CHANGES>`, similar to the way that the body of `(module reader syntax/module-reader my-language-implementation-module)` is a module name.

-Philip


Philip McGrath

unread,
Aug 31, 2020, 1:46:30 PM8/31/20
to Shriram Krishnamurthi, Racket Users
In addition (or instead, if this is good enough and less painful), you could use a compile-time helper function like:

(define-for-syntax (make-mb+ti namespaces-stx
                               lang-print-names-stx)
  (define-syntax-class to-run
    #:attributes (parsed)
    #:literals (TEST)
    (pattern (TEST e r ...)
             #:with parsed
             #`(test-output 'e (list 'r ...) #,namespaces-stx))
    (pattern e
             #:with parsed
             #`(show-output 'e #,namespaces-stx #,lang-print-names-stx)))
  (values (syntax-parser
            #:track-literals
            [(_ :to-run ...)
             #'(#%printing-module-begin parsed ...)])
          (syntax-parser
            #:track-literals
            [(_ . :to-run)
             #'(#%top-interaction . parsed)])))

-Philip


On Mon, Aug 31, 2020 at 1:39 PM Shriram Krishnamurthi <s...@cs.brown.edu> wrote:
Oooh, that's pretty clever! A bit painful, but clever!

You received this message because you are subscribed to a topic in the Google Groups "Racket Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/racket-users/AH_MtfTEmyw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/010001744593f3c3-1bc786d7-6d32-4e00-ab62-7d3ad7a42359-000000%40email.amazonses.com.
Reply all
Reply to author
Forward
0 new messages