# Replacing lexical context in a macro

**URL:** <https://racket.discourse.group/t/replacing-lexical-context-in-a-macro/2004>\
**Category:** Questions & Answers\
**Created:** [June 13, 2023, 8:32am UTC](https://racket.discourse.group/t/replacing-lexical-context-in-a-macro/2004 "2023-06-13T08:32:29Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![rscho](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/rscho/32/726_2.png) [@rscho](https://racket.discourse.group/u/rscho)\
**Post date:** [June 13, 2023, 8:32am UTC](https://racket.discourse.group/t/replacing-lexical-context-in-a-macro/2004/1 "2023-06-13T08:32:29Z")

</div>

Hi,

I'm trying to do something about [typed/racket issue #1286](https://github.com/racket/typed-racket/issues/1286#issue-1388087586), and hopefully [racket/math issue #75](https://github.com/racket/math/issues/75#issue-1375401767) at some point. I patched `require/untyped-contract` as below:

```scheme
(define-syntax (require/untyped-contract stx)
  (syntax-parse stx #:literals (begin quote)
    [(_ (begin form ...) from-module-spec:expr (quote language-spec:id) [name:id T:expr] ...)
     (with-syntax* ([(typed-name ...) (generate-temporaries #'(name ...))]
                    [(untyped-name ...) (freshen #'(name ...))]
                    [(untyped2-name ...) (generate-temporaries #'(name ...))]
                    [typed-module (generate-temporary #'typed-module)]
                    [untyped-module (generate-temporary #'untyped-module)]
                    [*racket/base (datum->syntax #'from-module-spec 'racket/base)]
                    [*typed/racket (datum->syntax #'from-module-spec (format-symbol "~a" (syntax-e #'language-spec)))]
                    [*require (datum->syntax #'from-module-spec 'require)]
                    [*language-spec (datum->syntax #'racket/base (format-symbol "~a" (syntax-e #'language-spec)))]
                    [from-module-spec-for-submod
                      (syntax-parse #'from-module-spec #:literals (submod)
                        [(submod (~and base (~or "." "..")) elem ...)
                         (syntax/loc #'from-module-spec (submod base ".." elem ...))]
                        [x #'x])])
       (syntax/loc stx
         (begin
           (module typed-module *typed/racket ; to bind in `T`s
             (*require *language-spec) ; to bind introduced `begin`, etc.
             (begin form ...)
             (require (only-in from-module-spec-for-submod
                               [name untyped2-name] ...))

```

So, I added a symbol argument `(quote language-spec:id)`, and used it as

```scheme
(datum->syntax #'from-module-spec (format-symbol "~a" (syntax-e #'language-spec)))

```

which, in my understanding, just replaces the original lexical context of the `(quote language-spec:id)` symbol with the `#'from-module-spec` context? Is there a simpler way to write the above line (e.g. a library function that does just that instead of completely re-making the symbol)?

Thanks!

---

_[View the full topic](https://racket.discourse.group/t/replacing-lexical-context-in-a-macro/2004)._
