On Wed, Feb 6, 2019 at 9:42 AM 'Paulo Matos' via Racket Users <
racket-users@googlegroups.com> wrote:
>
>
>
> On 06/02/2019 16:00, Shu-Hung You wrote:
> > print-values is a normal identifier introduced by the racket/base's
> > macro module-begin. It is a (private) function defined in
> > racket/private/modbeg.
> >
> That's sort of surprising. I actually expected fully expanded programs
> to be evaluable by the user. This obviously can't happen if it expands
> to private functions.
>

Actually, the fully expanded programs _can_ be evaluated by the users. It
is possible to first expand an input program then pass the result to eval.
The identifier has the right binding information, referencing to the
private function in racket/private/modbeg, and that module will be
instantiated because racket/base depends on it.

This sort of expanding to some identifier referring to not-provided
definitions is very common from the use of macros.

#lang racket

(define ns (make-base-namespace))

(define code
  `(,(namespace-module-identifier ns) M racket/base
     (define x 10)
     (+ x 3)))

(pretty-write code)
(define expanded-code (expand code))
(pretty-write (syntax->datum expanded-code))

(parameterize ([current-namespace ns])
  (eval expanded-code)
  (eval '(require 'M)))

To examine the lexical information of an identifier, you can print
expanded-code in DrRacket and click on print-values to see its syntax
information:

[image: binding.png]

> Is there a specific reason for this or just happened and nobody every
cared?
>

I would say this is what the code looks like when you look under the hood.
Macros and modules work with each other like magic; identifiers come with
lexical information. Conceptually, instantiating a module M containing a
variable x would introduce a mapping M.x |-> value in the namespace. If, at
expansion time, some macro in M introduces x to a client module, the x in
the client module is compiled to be a runtime reference to M.x.

This part in the document contains an informal description:
https://docs.racket-lang.org/reference/eval-model.html#(part._module-eval-model)

Matthew wrote a paper about how modules and macros work together:

    Matthew Flatt. Composable and Compilable Macros: You Want it When?
    https://www.cs.utah.edu/plt/publications/macromod.pdf

The paper contains a model showing how things like 'print-values' work. In
page 9, the grammar of c-exp contains mod.id.p to handle reference to
variables in other modules.

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

Reply via email to