Re: [racket-users] Fully expanded programs grammar: print-values

2019-02-07 Thread 'Paulo Matos' via Racket Users



On 06/02/2019 21:09, Shu-Hung You wrote:
> 
> 
> On Wed, Feb 6, 2019 at 9:42 AM 'Paulo Matos' via Racket Users
> mailto: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.
> 

Thanks for the clarification and reference. Actually after reading what
you said, it makes total sense and what I tried to do was completely
ridiculous. :)

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


Re: [racket-users] Fully expanded programs grammar: print-values

2019-02-06 Thread Shu-Hung You
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.


Re: [racket-users] Fully expanded programs grammar: print-values

2019-02-06 Thread 'Paulo Matos' via Racket Users



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.

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

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


Re: [racket-users] Fully expanded programs grammar: print-values

2019-02-06 Thread Shu-Hung You
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.

On Feb 6, 2019 5:02 AM, "'Paulo Matos' via Racket Users" <
racket-users@googlegroups.com> wrote:

> Hi,
>
> The docs[1] give a grammar for fully expanded programs.
> I found it amazingly concise so I decided to give it a try and expanded
> the following:
>
> ```
> #lang racket
>
> 42
> ```
>
> This expands to (nicely formatted):
>
> # (module forty-two racket
>   (#%module-begin
> (module configure-runtime
>   (quote #%kernel)
>   (#%module-begin
> (#%require racket/runtime-config)
> (#%app configure (quote #f
>   (#%app call-with-values (lambda () (quote 42)) print-values)))
> >
>
> That looks good to me. However, in the documented grammar there's no
> reference to print-values. I assume print-values is an `id` in the
> grammar, but I cannot find a reference to `print-values` in the docs
> either. Is this an omission?
>
> [1]
> https://docs.racket-lang.org/reference/syntax-model.html?#(
> part._fully-expanded)
> --
> 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.


[racket-users] Fully expanded programs grammar: print-values

2019-02-06 Thread 'Paulo Matos' via Racket Users
Hi,

The docs[1] give a grammar for fully expanded programs.
I found it amazingly concise so I decided to give it a try and expanded
the following:

```
#lang racket

42
```

This expands to (nicely formatted):

#

That looks good to me. However, in the documented grammar there's no
reference to print-values. I assume print-values is an `id` in the
grammar, but I cannot find a reference to `print-values` in the docs
either. Is this an omission?

[1]
https://docs.racket-lang.org/reference/syntax-model.html?#(part._fully-expanded)
-- 
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.