How to define two R7RS libraries in Guile

272 Views Asked by At

I have these two R7RS libraries, which I enter into the Guile REPL one by one:

(define-library (example one)
  (import (scheme base))
  (export f)
  (begin
    (define (f x)
      (+ x 1))))

(define-library (example two)
  (import (scheme base))
  (export g)
  (begin
    (define (g x)
      (* x 2))))

When I enter the second library definition in the Guile REPL, I get this error:

While compiling expression:
Syntax error:
unknown file:13:4: definition in expression context, where definitions are not allowed, in form (define (g x) (* x 2))

I tried to put the two libraries into the same file (mylibs.sld) and ran guile mylibs.sld, but I get exactly the same error.

From my understanding, I am getting this error because define-library somehow becomes undefined by the time Guile reads the second library definition. Guile no longer knows what define-library means. This is strange behavior. What is going on? How do I define two R7RS libraries in the REPL?

Guile version: 3.0.1

1

There are 1 best solutions below

1
On BEST ANSWER

If you just want to define them at the repl, you can do this by setting the current module to (guile-user). Like you have guessed, after the first define-library the compiler is in a state where the symbols available by default in (guile-user) are unavailable.

So if you do a ,m (guile-user) between defining the two libraries you will be fine.

scheme@(guile-user)> (define-library (example one)
  (import (scheme base))
  (export f)
  (begin
    (define (f x)
      (+ x 1))))
scheme@(example one)> ,m (guile-user)
scheme@(guile-user)> (define-library (example two)
  (import (scheme base))
  (export g)
  (begin
    (define (g x)
      (* x 2))))
scheme@(example two)> ,m (guile-user)
scheme@(guile-user)> (use-modules (example one) (example two))
scheme@(guile-user)> (f 12)
$1 = 13
scheme@(guile-user)> (g 9)
$2 = 18
scheme@(guile-user)>

I am not sure about how to do get this to work from a single file(lib per file works fine). I tried to set the current module to (guile-user) with eval-when after the first library but even the symbol eval-when was unavailable. Must be a bug.