[racket] `provide` failed under `#lang racket/load`

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Sun Aug 4 04:40:01 EDT 2013

On Sun, Aug 4, 2013 at 4:32 AM, Yi Dai <plmday at gmail.com> wrote:

>
>
>
> On Sun, Aug 4, 2013 at 10:20 AM, Carl Eastlund <cce at ccs.neu.edu> wrote:
>
>> Yi,
>>
>> Most #lang languages implicitly create a module from the contents of the
>> file.  #lang racket/load, on the other hand, runs the contents of the file
>> as top-level terms outside of any module.  The provide form is not legal at
>> the top level, as it is meaningless -- there's no module to provide things
>> from.  You need to either switch back to #lang racket or remove the
>> provide, depending on what you're trying to accomplish.
>>
>
> Thanks for the answer, Karl.  I also found the answer in the doc:
> http://docs.racket-lang.org/reference/load-lang.html.  But very
> confusingly, the doc keeps saying "racket/load" module.
>

Yes, the file is technically a module.  However, the code you write is not
evaluated in the context of that module.  It is instead evaluated in a
dynamically-generated top-level context.  It might be clearer if we look at
the explicit module generated by the two #lang languages:

#lang racket
<code1>
<code2>
...

is equivalent to

(module <filename> racket
  <code1>
  <code2>
  ...)

However,

#lang racket/load
<code1>
<code2>
...

is (basically) equivalent to

(module <filename> racket
  (eval (quote <code1>))
  (eval (quote <code2>))
  ...)


>
>
>> On Sun, Aug 4, 2013 at 4:12 AM, Yi Dai <plmday at gmail.com> wrote:
>>
>>>  Hi,
>>>
>>>
>>> I have the following code in a file named `foo.rkt`:
>>>
>>> ```
>>> #lang racket/load
>>>
>>> (provide foo)
>>>
>>> (define foo 'bar)
>>> ```
>>>
>>> When trying to run the code, I got the following confusing error:
>>>
>>> > `provide`: not at module level in: `(provide foo)`
>>>
>>> What does this mean?  Why `provide` is not at module level?
>>> Use `#lang racket` instead does not pose this problem.  I am
>>> confused.  Any idea?
>>>
>>>
>>> Yours truly,
>>>
>>> Yi
>>>
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>>
>>>
>>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130804/747b5935/attachment.html>

Posted on the users mailing list.