Question about `typed-context?`

26 views
Skip to first unread message

Nate Griswold

unread,
Oct 7, 2020, 6:18:22 AM10/7/20
to racke...@googlegroups.com
I just finished reading the paper "Languages as Libraries", which really helped as an introduction to racket's language system (i had previously been ignoring the syntax features).

I did have one question from it, which wasn't quite made clear in the interoperability section, or so it seems: how does the box `typed-context?` get into the modules that are not part of that language? Or more generally how would you solve this problem of needing to know which language of a module you are in without modifying the other languages? I was wondering if it was a hackaround or if there was a way to do generically in any language i might make.

I grepped the source code and it looks like it's even exposed to the user in a function `syntax-local-typed-context?`, but i noticed that module never actually imports `typed-context?`.

I suppose if it is a hack i could just reflect on the namespace to solve the problem, but i thought i would send a mail here to check.

Thanks for the great work everyone!

Nate

Sam Tobin-Hochstadt

unread,
Oct 7, 2020, 9:44:56 PM10/7/20
to Nate Griswold, racke...@googlegroups.com
The way this works, the box doesn't have to "get into" any modules
that aren't typed. Instead, when expanding a module A, that module
may contain uses of identifiers defined in a typed module T. Those
identifiers are macros that look something like this:

(define-syntax (id stx) (if typed-context? #'typed-id #'id-for-untyped))

Furthermore, the typed-context? value is initially set to #f, and it's
set to #true only inside the typechecker. When `id` is used in an
untyped module, it never gets set to #true, and so the
`id-for-untyped` is used. That means that the untyped code doesn't
need to know or do anything to make the system work, which is
necessary for Typed Racket to be a library.

There's more detail about this in our Scheme Workshop paper, here:
https://www2.ccs.neu.edu/racket/pubs/scheme2007-ctf.pdf

Sam
> --
> You received this message because you are subscribed to the Google Groups "Racket Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-dev/CAM-xLPpey6e2S_JsBi_ZA_0OAC%2Bh8NJDrVYRv63fWmYrWiig1A%40mail.gmail.com.

Nathaniel Griswold

unread,
Oct 7, 2020, 10:08:00 PM10/7/20
to Sam Tobin-Hochstadt, racke...@googlegroups.com
Hm, I have this two reads for a over and I didn’t quite get it. I thought every module was compiled in a fresh context so my issue is I’m wondering how that identifier for the box is defined for syntax expansion in untyped modules. Maybe you understand what I mean; otherwise I’ll give the paper a look later tonight or tomorrow to see what I’m missing here.

Thanks

> On Oct 7, 2020, at 8:44 PM, Sam Tobin-Hochstadt <sa...@cs.indiana.edu> wrote:
>
> The way this works, the box doesn't have to "get into" any modules

Sam Tobin-Hochstadt

unread,
Oct 7, 2020, 10:18:35 PM10/7/20
to Nathaniel Griswold, racke...@googlegroups.com
Every module is indeed compiled with a fresh context. The untyped
module doesn't directly refer to typed-racket/utils/tc-utils, which is
the file that defines typed-context?, but it does refer to a typed
module, whose expansion will contain a reference to that module.
Consider:

#lang typed/racket
(provide f)
(define (f [x : Integer]) (+ x 1))

That expands to (among a lot of other things):

(define-values (f) (lambda (x) (#%app + x '1)))
(define-syntaxes (f.1) (#%app make-redirect2 (quote-syntax f.3)))
(define-syntaxes
(f.2)
(#%app make-typed-renaming (t-quote-syntax f) (t-quote-syntax f.1)))

Where f.2 is provided as f, and f.3 is the contracted version of the identifier.

Then `make-typed-renaming` is effectively `(lambda (a b) (lambda (stx)
(if (unbox typed-context?) a b)))`

So it's the definition of `f` that depends on the box, and so there's
a fresh instantiation of `tc-utils` for every module that gets
expanded that depends on the typed module above.

Hopefully that clarifies things a bit more.

Sam

On Wed, Oct 7, 2020 at 10:07 PM Nathaniel Griswold
<nategr...@gmail.com> wrote:
>
> Hm, I have this two reads for a over and I didn’t quite get it. I thought every module was compiled in a fresh context so my issue is I’m wondering how that identifier for the box is defined for syntax expansion in untyped modules. Maybe you understand what I mean; otherwise I’ll give the paper a look later tonight or tomorrow to see what I’m missing here.
>
> Thanks
>
> > On Oct 7, 2020, at 8:44 PM, Sam Tobin-Hochstadt <sa...@cs.indiana.edu> wrote:
> >
> > The way this works, the box doesn't have to "get into" any modules

Nathaniel Griswold

unread,
Oct 7, 2020, 10:30:06 PM10/7/20
to Sam Tobin-Hochstadt, racke...@googlegroups.com
Thanks. I feel better knowing there will be a fresh instantiation for every compile now I just have to trace through some stuff I didn’t quite get. That should be enough for me to go on.

Nate

> On Oct 7, 2020, at 9:18 PM, Sam Tobin-Hochstadt <sa...@cs.indiana.edu> wrote:
>
> Every module is indeed compiled with a fresh context. The untyped
Reply all
Reply to author
Forward
0 new messages