load used incorrectly, behavior of load change version 5 -> 6 ?

22 views
Skip to first unread message

Thomas Lynch

unread,
May 8, 2015, 1:02:38 AM5/8/15
to racke...@googlegroups.com
Her is a small pointless program

  #lang racket
  (define (neq args) (not (apply = args)))
  (provide neq)

So then loading this in racket:

racket@> (load "temp-lib.rkt")
racket@> (neq 1 0)
neq: undefined;
 cannot reference undefined identifier
  context...:
   /usr/share/racket/collects/racket/private/misc.rkt:87:7

Why is neq undefined after the load?  I could see symbols in a module with load before the upgrade ..

Now there is a (provide neq) in there so why not use require?  Lets try that in the same session:

racket@> (require "temp-lib.rkt")
racket@> (neq 1 0)
neq: arity mismatch;
 the expected number of arguments does not match the given number
  expected: 1
  given: 2
  arguments...:
   1
   0
  context...:
   /usr/share/racket/collects/racket/private/misc.rkt:87:7

Yes, now the symbol can be seen. Good, and oh yeah, a '.'is missing in the source so let me add that.  The edited and saved source code now appears as:

#lang racket
(define (neq . args) (not (apply = args)))
(provide neq)

So again, in the same session, lets use this new better source code:

racket@> (neq 1 0)
neq: arity mismatch;
 the expected number of arguments does not match the given number
  expected: 1
  given: 2
  arguments...:
   1
   0
  context...:
   /usr/share/racket/collects/racket/private/misc.rkt:87:7

Alas, racket does seem to see the updated code.  My guess is that the second require did nothing because racket already though it had that file. Is this the case?  Anyway, I did this twice in different variations, and indeed new edits do not come in.  Well then what to do, here I kill the racket process and run it again (kinda like rebooting windows after a problem):

racket@> 
It's been nice interacting with you!
Press C-c C-z to bring me back.
Welcome to Racket v6.1.
racket@> (require "temp-lib.rkt")
racket@> (neq 1 0)
#t
racket@> 
racket@> (require "temp-lib.rkt")
racket@> (neq 1 0)
#t
racket@> 

Yes, after rebooting racket I see my changes.

Ok, what is going on here?  What happened to load?  How do I load a file so that I can work on it while seeing all the bindings?  How do I reload a file after an edit?    



 

Matthew Flatt

unread,
May 9, 2015, 12:09:57 PM5/9/15
to Thomas Lynch, racke...@googlegroups.com
The `load` function can load a module declaration, but it doesn't
instantiate the module.

In other words, using `load` is like

```
> (module m racket/base
(define (neq . args) (not (apply = args)))
(provide neq))
> (neq 1 0)
neq: undefined;
cannot reference an identifier before its definition
in module: 'program
```

But `require` loads the module, instantiates it, and imports the
module's `provide`s, so using `require` on a file is analogous to

```
> (module m racket/base
(define (neq . args) (not (apply = args)))
(provide neq))
> (require 'm)
> (neq 1 0)
#t
```

As you observe, `racket` doesn't track changes to files, and `require`
doesn't re-load a module that it has previously loaded (even if the
module's source changed).

You might want to try a tool like `xrepl`, which supports re-loading
changed modules:

http://docs.racket-lang.org/xrepl/index.html
> --
> 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 post to this group, send email to racke...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-dev/76ffc46f-7231-4bf1-aa66-e873904ff18
> 1%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
```
Output:
```
program:40:57: read: illegal use of `.'

Greg Hendershott

unread,
May 9, 2015, 10:32:01 PM5/9/15
to Thomas Lynch, racke...@googlegroups.com, Matthew Flatt
> As you observe, `racket` doesn't track changes to files, and `require`
> doesn't re-load a module that it has previously loaded (even if the
> module's source changed).
>
> You might want to try a tool like `xrepl`, which supports re-loading
> changed modules:
>
> http://docs.racket-lang.org/xrepl/index.html

Along those lines, from the prompt and messages, it looks like you're
using Racket with Geiser in Emacs?

If you want to use `load` and dynamically re-require things, it might
be simpler to use plain command-line Racket, and, as Matthew
suggested, with xrepl and its dynamic re-require command.

(This is nothing against Geiser. I'd suggest the same if you were
using racket-mode, or Dr Racket. And I suggest it not because I know
Geiser definitely won't work in this case. Instead, it's just my
intuition that it might help to minimize the number of moving parts --
especially if you're trying to use Racket in an atypical way (with
`load` instead of `require`). If/as/when things work as you want with
plain command-line Racket, you could try adding Geiser or whatever
back to the picture. Again, this is only my intuition.)

Thomas Lynch

unread,
May 10, 2015, 8:52:14 AM5/10/15
to racke...@googlegroups.com, thomas...@reasoningtechnology.com, mfl...@cs.utah.edu
yes geiser in emacs .. I better move to Dr Racket IDE so I'm in the same boat with most everyone else.  ..  The world would be a better place if we just all booted into emacs and left it at that ;-)

Just to make sure I am not missing something simple, is there a command for loading a module and having all the variables in that module come into scope (to facilitate debug)  and not have any caching, while in emacs with geiser?  Before I updated, 'load' did this.

Greg Hendershott

unread,
May 10, 2015, 3:27:24 PM5/10/15
to Thomas Lynch, racke...@googlegroups.com, Matthew Flatt
> yes geiser in emacs .. I better move to Dr Racket IDE so I'm in the same
> boat with most everyone else.

Actually, Matthew and I were suggesting that you try using
command-line Racket with xrepl.

For example:

$ cat file.rkt
#lang racket
(define x 10)

$ racket
Welcome to Racket v6.1.1.
> (require xrepl)
-> ,enter file.rkt
"file.rkt"> x
10
;; edit file.rkt to add (define y 42)
"file.rkt"> ,enter file.rkt
[re-loading /tmp/file.rkt]
"file.rkt"> y
42
"file.rkt">

Notice that the xrepl command `,enter` detected that file.rkt changed,
and re-loaded it.

Keep in mind you could still use Emacs to _edit_ the file. You could
run racket in an M-x shell buffer.

If this turns out to work well, you can put the (require xrepl) in
your ~/.racketrc.


> Just to make sure I am not missing something simple, is there a command for
> loading a module and having all the variables in that module come into scope
> (to facilitate debug) and not have any caching, while in emacs with geiser?
> Before I updated, 'load' did this.

I think Geiser does the equivalent of ,enter. So I'm not sure why that
doesn't work for you. You could ask the Geiser author:
https://github.com/jaor/geiser/issues


If you were to use Dr Racket, or racket-mode for Emacs (which I
maintain), keep in mind they work by re-evaluating everything on each
"Run". Anything you've done in the REPL is discarded, and the files
are evaluated again. Some people love that approach (I'm one!); some
don't. I'm not clear if you're using `load` because you want to work
in a more "dynamic" style, or only because it happened to work for you
in the past. Anyway I hope some of the above is helpful.

Matthew Flatt

unread,
May 10, 2015, 6:06:37 PM5/10/15
to Thomas Lynch, racke...@googlegroups.com
At Sun, 10 May 2015 15:26:43 -0400, Greg Hendershott wrote:
> > yes geiser in emacs .. I better move to Dr Racket IDE so I'm in the same
> > boat with most everyone else.
>
> Actually, Matthew and I were suggesting that you try using
> command-line Racket with xrepl.

One more possibility is to use the `enter!` form of `racket/enter`.
The xrepl ,enter command is based on `enter!`.

Thomas Lynch

unread,
May 11, 2015, 7:02:33 AM5/11/15
to racke...@googlegroups.com, thomas...@reasoningtechnology.com
Thanks guys, both xrepl in a shell (run in emacs ;-), and the (enter!) form in geiser worked.  Yes, it was a bad habit.  

Thomas Lynch

unread,
May 11, 2015, 7:50:43 AM5/11/15
to racke...@googlegroups.com
Nice description of the issue I ran up against here in the guide,  15.3 Scripting Evaluation and Using load.  
Reply all
Reply to author
Forward
0 new messages