[racket] Confused about submodule + state

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Sat Apr 12 16:56:24 EDT 2014

Hi all,

I'm trying to figure out how submodules and compile-time state interact,
in particular when the submodule is in a `begin-for-syntax`.

Example: https://gist.github.com/takikawa/10555205 (also inlined below)

As the interaction shows, the submodule can read the compile-time table
but when it tries to modify it, the modifications only appear to be
visible from the submodule and not the outer module.

Is that the expected behavior? Should I not rely on side effects
persisting between a submodule and its containing module?



#lang racket

(require (for-syntax racket/dict syntax/id-table))

;; track stuff in this table
  (define tbl (make-free-id-table)))

;; a binding we can use in the table
(define x 3)
(begin-for-syntax (define q-x #'x))

  ;; initially set x -> "outer"
  (dict-set! tbl q-x "outer")

  (module* sub #f
    ;; the submodule sees "outer"
    (for ([(k v) (in-dict tbl)])
      (printf "~a -> ~a~n" k v))
    (dict-set! tbl q-x "inner")
    ;; the submodule now sees "inner"
    (for ([(k v) (in-dict tbl)])
      (printf "~a -> ~a~n" k v))))

;; this macro just prints what's in the table
(define-syntax (print stx)
  (syntax-case stx ()
     (for ([(k v) (in-dict tbl)])
       (printf "~a -> ~a~n" k v))

(provide print)


$ racket
Welcome to Racket v6.0.1.1.
-> (require "test.rkt")
-> (print)
#<syntax:/tmp/test.rkt:11:32 x> -> outer
-> (require (submod "test.rkt" sub))
#<syntax:/tmp/test.rkt:11:32 x> -> outer
#<syntax:/tmp/test.rkt:11:32 x> -> inner
-> (print)
#<syntax:/tmp/test.rkt:11:32 x> -> outer

Posted on the users mailing list.