For Gauche 0.9.5

Next: , Previous: , Up: Library modules - R7RS integration   [Contents][Index]

10.1 Traveling between two worlds back and forth

When you start Gauche, either in REPL or as a script, you’re in user module, which inherits gauche module. Likewise, when you read a library, the initial module inherits gauche module (until you call select-module). That’s why you can access all the built-in procedures of Gauche without saying (use something). (See Module inheritance, for the details about inheriting modules).

On the other hand, R7RS requires to be explicit about which namespaces you’ll be using, by import form, e.g. (import (scheme base)). Besides, R7RS library must be explicitly enclosed by define-library form. Before the first import form of a program, or outside of define-library, is beyond R7RS world—the standard defines nothings about it.

These facts let Gauche to set up appropriate “world”, and you can use R7RS code and traditional Gauche code transparently.

NB: As explained in Three forms of import, R7RS import is rather different from Gauche import, so we note the former r7rs#import and the latter gauche#import in this section for clarity. When you write code don’t use prefixes r7rs# and gauche#; just write import.

Loading R7RS libraries

The define-library form is defined as a macro in gauche module; it sets up R7RS environment before evaluating its contents. So, when you load an R7RS library (either from Gauche code via use form, or from R7RS code via r7rs#import form), Gauche starts loading the file in gauche module, but immediately see define-library form, and the rest is handled in R7RS environment.

Suppose you have an R7RS library (mylib foo) with the following code:

(define-library (mylib foo)
  (import (scheme base))
  (export snoc)
    (define (snoc x y) (cons y x))))

It should be saved as mylib/foo.scm in one of the directories in *load-path*.

From R7RS code, this library can be loaded by r7rs#import:

(import (mylib foo))

(snoc 1 2) ⇒ (2 . 1)

To use this library from Gauche code, concatenate elements of library names by . to get a module name, and use it:


(snoc 1 2) ⇒ (2 . 1)

Loading Gauche libraries

To use Gauche library from R7RS code, split the module name by . to make a list for the name of the library. For example, gauche.lazy module can be used from R7RS as follows:

(import (gauche lazy))

For SRFI modules, R7RS implementations have a convention to name it as (srfi n), and Gauche follows it. The following code loads srfi-1 and srfi-13 from R7RS code:

(import (srfi 1) (srfi 13))

(It’s not that Gauche treat srfi name specially; installation of Gauche includes adapter libraries such as srfi/1.scm.)

A tip: To use Gauche’s built-in features (the bindings that are available by default in Gauche code) from R7RS code, import (gauche base) library (see Importing gauche built-ins):

(import (gauche base))

filter ⇒ #<closure filter>

Running R7RS scripts

R7RS scripts always begin with import form. However, r7rs#import has a different syntax and semantics from gauche#import—so we employ a trick.

When gosh is started, it loads the given script file in user module. We have a separate user#import macro, which examines its arguments and if it is R7RS import syntax, switch to the r7rs.user module and run the r7rs#import. Otherwise, it runs gauche#import. See Three forms of import, for the details.

An example of R7RS script:

(import (scheme base) (scheme write))
(display "Hello, world!\n")

If you’re already familiar with Gauche scripts, keep in mind that R7RS program doesn’t treat main procedure specially; it just evaluates toplevel forms from top to bottom. So the following script doesn’t output anything:

(import (scheme base) (scheme write))
(define (main args)
  (display "Hello, world!\n")

To access the command-line arguments in R7RS scripts, use command-line in (scheme process-context) library (see R7RS process context, also see Command-line arguments).


When gosh is invoked with -r7 option and no script file is given, it enters an R7RS REPL mode. For the convenience, the following modules (“libraries”, in R7RS term) are pre-loaded.

(scheme base) (scheme case-lambda) (scheme char)
(scheme complex) (scheme cxr) (scheme eval)
(scheme file) (scheme inexact) (scheme lazy)
(scheme load) (scheme process-context) (scheme read)
(scheme repl) (scheme time) (scheme write)

Besides, the history variables *1, *2, *3, *1+, *2+, *3+, *e and *history are available (See Working in REPL, for the details of history variables).

You can know you’re in R7RS REPL by looking at the prompt, where gosh shows the current module (r7rs.user):


To switch Gauche REPL from R7RS REPL, import (gauche base) and select user module using select-module:

gosh[r7rs.user]> (import (gauche base))
gosh[r7rs.user]> (select-module user)

(You can (select-module gauche) but that’s usually not what you want to do—changing gauche module can have unwanted side effects.)

When you’re working on R7RS code in file and load it into R7RS REPL (for example, if you’re using Emacs Scheme mode, C-c C-l does the job), make sure the file is in proper shape as R7RS; that is, the file must start with appropriate import declarations, or the file contains define-library form(s). If you load file without those forms, it is loaded into Gauche’s user module no matter what your REPL’s current module is, and the definitions won’t be visible from r7rs.user module by default.

Switching from Gauche REPL

By default, gosh enters Gauche REPL when no script file is given. See Working in REPL, for detailed explanation of using REPL.

To switch Gauche REPL to R7RS REPL, simply use r7rs-style import; user#import knows you want R7RS and make a switch.

gosh> (import (scheme base))

If you don’t start gosh with -r7 option, however, only the libraries you given to user#import are loaded at this moment.

If you want to switch the “vanilla” r7rs environment, that is, even not loading (scheme base), then you can use r7rs module and directly select r7rs.user:

gosh> (use r7rs)
gosh> (select-module r7rs.user)

If you do this, the only bindings visible initially are import and define-library; even define is undefined! You have to manually do (import (scheme base)) etc. to start writing Scheme in this environment.

Next: , Previous: , Up: Library modules - R7RS integration   [Contents][Index]