Scheme modules and libraries
last modified November 25, 2026 Guile 3.0
A program that grows beyond one file needs a way to split its code into parts
that hide their internals and state what they offer. In Scheme these parts
are called modules or libraries. A module has a name,
a list of names it exports, and its own namespace for everything
else. Guile has its own module system, written with
define-module and use-modules, and it supports the
R7RS define-library form as well. The examples run with GNU
Guile 3.0.7.
Loading library modules
Earlier articles loaded library modules such as SRFI 1 without explaining
them. use-modules takes one or more module names. A module name
is a list of symbols, and Guile turns it into a file path: (srfi
srfi-1) is the file srfi/srfi-1.scm in one of the
directories of the load path, here
/usr/share/guile/3.0. All exported names of the module become
visible in the program.
(use-modules (srfi srfi-1)
(ice-9 receive))
(define scores '(72 85 91 64 85 78 91))
(display (delete-duplicates scores))
(newline)
(receive (passed failed) (partition (lambda (s) (>= s 75)) scores)
(display (list 'passed (length passed) 'failed (length failed)))
(newline))
(display (fold max 0 scores))
(newline)
The program loads SRFI 1 and the (ice-9 receive) module in one
use-modules form. delete-duplicates,
partition, and fold come from SRFI 1, and the
receive form from the second module. A module is loaded only
once, however many times it is named.
$ guile libs.scm (72 85 91 64 78) (passed 5 failed 2) 91
Selecting and renaming
Importing every name of a module can clash with names in the program. A
module name can be wrapped in a list with options. #:select
imports only the listed names, and a pair (name . new-name)
renames one of them. #:prefix imports all names with a prefix
in front.
(use-modules ((srfi srfi-1) #:select (take drop (last . last-item)))
((srfi srfi-43) #:prefix v:))
(define letters '(a b c d e f g))
(display (take letters 3))
(newline)
(display (drop letters 3))
(newline)
(display (last-item letters))
(newline)
(define v (vector 10 20 30 40))
(display (v:vector-map (lambda (i x) (+ i x)) v))
(newline)
(display (v:vector-count (lambda (i x) (> x 15)) v))
(newline)
(display ((@ (srfi srfi-1) count) symbol? '(a 1 b 2 c)))
(newline)
From SRFI 1 the program takes only take, drop,
and last, which it renames to last-item. The SRFI
43 vector procedures arrive as v:vector-map and
v:vector-count; they pass the index as the first argument, as
the vectors article explains. The
@ form refers to one exported name of a module without an
import: (@ (srfi srfi-1) count) is SRFI 1's
count, which was not selected.
$ guile select.scm (a b c) (d e f g) g #(10 21 32 43) 3 3
A first module
A module of our own is a file that starts with define-module.
The module name must match the path of the file: the module
(geometry) lives in geometry.scm. The
#:export option lists the names that other code may use.
(define-module (geometry)
#:export (circle-area rectangle-area triangle-area))
(define pi 3.141592653589793)
(define (circle-area r)
(* pi r r))
(define (rectangle-area w h)
(* w h))
(define (triangle-area a b c)
(let ((s (/ (+ a b c) 2)))
(sqrt (* s (- s a) (- s b) (- s c)))))
The module exports three procedures that compute areas; the triangle uses
Heron's formula. The constant pi is not exported, so it stays
private to the module.
(use-modules (geometry)) (display (circle-area 1)) (newline) (display (rectangle-area 3 4)) (newline) (display (triangle-area 3 4 5)) (newline)
The program loads the module with use-modules, as it would a
library module. Guile does not look for modules in the current directory, so
the directory is added to the load path with the -L option.
Guile compiles the module the first time it is used, just like the script.
$ guile -L . areas.scm 3.141592653589793 12 6
Private definitions
A name that the module does not export is not visible outside it. Using one is the same error as using any undefined variable.
(use-modules (geometry)) (display (circle-area 2)) (newline) (display pi) (newline)
circle-area uses pi inside the module, but the
program cannot see it. The compiler warns about the unbound variable, and
the program stops when it reaches it. Guile has an escape hatch for
debugging, (@@ (geometry) pi), which reads a private name of a
module, but regular code should use only the exports. The output omits the
backtrace.
$ guile -L . private.scm ;;; private.scm:5:9: warning: possibly unbound variable `pi' 12.566370614359172 private.scm:5:9: Unbound variable: pi
Hierarchical names
Module names with more than one part map to subdirectories, which keeps
related modules together. A module states the modules it depends on with
#:use-module in its define-module form.
(define-module (text words)
#:use-module (srfi srfi-1)
#:export (words word-count longest-word))
(define (words text)
(remove string-null? (string-split text #\space)))
(define (word-count text)
(length (words text)))
(define (longest-word text)
(fold (lambda (w best)
(if (> (string-length w) (string-length best)) w best))
""
(words text)))
The module (text words) lives in text/words.scm.
It uses SRFI 1 for remove and fold; these imports
belong to the module and do not leak into the programs that use it.
words splits a text on spaces and drops the empty strings that
two spaces in a row produce.
(use-modules (text words)) (define line "a quick brown fox jumps over the lazy dog") (write (words line)) (newline) (display (word-count line)) (newline) (display (longest-word line)) (newline)
The program uses the module by its full name. The -L option
still names the top directory; Guile finds the text
subdirectory itself.
$ guile -L . wordstats.scm
("a" "quick" "brown" "fox" "jumps" "over" "the" "lazy" "dog")
9
quick
Module state
A module can keep state in its private variables. Because Guile loads every module only once, all users of a module share the same state. The example splits a small shop into three modules: one hands out identifiers, and two others use it.
(define-module (app ids)) (define counter 0) (define-public (next-id! prefix) (set! counter (+ counter 1)) (string-append prefix "-" (number->string counter)))
define-public defines a name and exports it in one step, so
(app ids) needs no #:export option. The counter is
private; the only way to change it is next-id!.
(define-module (app users) #:use-module (app ids) #:export (make-user)) (define (make-user name) (list (next-id! "u") name))
The users module imports (app ids) and gives each user an
identifier with the prefix u.
(define-module (app orders) #:use-module (app ids) #:export (make-order)) (define (make-order user item) (list (next-id! "o") (car user) item))
The orders module uses the same module for identifiers with the prefix
o. An order refers to its user by the user's identifier.
(use-modules (app users) (app orders)) (define eva (make-user "Eva")) (define tomas (make-user "Tomas")) (display eva) (newline) (display tomas) (newline) (display (make-order eva 'book)) (newline) (display (make-order tomas 'lamp)) (newline) (display (make-order eva 'pen)) (newline)
The program imports only the users and orders modules. The numbers keep
growing across both kinds of objects, which shows that the two modules share
one instance of (app ids) and one counter.
$ guile -L . shop.scm (u-1 Eva) (u-2 Tomas) (o-3 u-1 book) (o-4 u-2 lamp) (o-5 u-1 pen)
Name conflicts
When two imported modules export the same name, Guile does not stop with an error. It prints a warning and uses the binding of the module named last. The example has two shape modules with the same interface.
(define-module (shapes circle) #:export (area perimeter)) (define (area r) (* 314/100 r r)) (define (perimeter r) (* 2 314/100 r))
The circle module approximates π with the exact fraction 314/100, so the results stay exact.
(define-module (shapes square) #:export (area perimeter)) (define (area a) (* a a)) (define (perimeter a) (* 4 a))
The square module exports area and perimeter as
well.
(use-modules (shapes circle) (shapes square)) (display (area 10)) (newline) (display (perimeter 10)) (newline)
Both area and perimeter come from the square
module, which is named last, so the program prints 100 and 40 for a circle
that was never asked for. Guile warns about each duplicate name once while
compiling the file and once while running it; at run time the
warning appears when the name is first used.
$ guile -L . conflict.scm WARNING: (guile-user): `area' imported from both (shapes circle) and (shapes square) WARNING: (guile-user): `perimeter' imported from both (shapes circle) and (shapes square) WARNING: (guile-user): `area' imported from both (shapes circle) and (shapes square) 100 WARNING: (guile-user): `perimeter' imported from both (shapes circle) and (shapes square) 40
Prefixes against conflicts
A prefix for each module keeps both sets of names apart. This is the usual way to import modules whose interfaces overlap.
(use-modules ((shapes circle) #:prefix circle:)
((shapes square) #:prefix square:))
(define (describe name area perimeter)
(display name)
(display ": area ")
(display area)
(display ", perimeter ")
(display perimeter)
(newline))
(describe "circle" (circle:area 10) (circle:perimeter 10))
(describe "square" (square:area 10) (square:perimeter 10))
circle:area and square:area are now distinct
names, and the program compiles without warnings. The prefix is part of the
import, not of the modules, so another program may choose a different one.
$ guile -L . figures.scm circle: area 314, perimeter 314/5 square: area 100, perimeter 40
Re-exporting
A module can pass on names it has imported with #:re-export.
This lets one module gather the interface of several smaller ones, so that
users need a single import.
(define-module (shapes)
#:use-module ((shapes circle) #:prefix circle-)
#:use-module ((shapes square) #:prefix square-)
#:re-export (circle-area circle-perimeter
square-area square-perimeter)
#:export (total-area))
(define (total-area circles squares)
(+ (apply + (map circle-area circles))
(apply + (map square-area squares))))
The module (shapes) lives in shapes.scm, next to
the shapes directory. It imports the two shape modules with the
prefixes circle- and square- and re-exports the
four prefixed names. It also adds a procedure of its own,
total-area.
(use-modules (shapes)) (display (circle-area 2)) (newline) (display (square-perimeter 3)) (newline) (display (total-area '(1 2) '(3 4 5))) (newline)
The program sees circle-area and the other names as if
(shapes) had defined them. The results are exact fractions:
2² × 314/100 is 314/25.
$ guile -L . plan.scm 314/25 12 657/10
R7RS libraries
R7RS defines a portable library form, define-library, which
Guile supports too. The library declares its exports and imports, and the
definitions go into a begin block. A program imports libraries
with import. R7RS libraries are often kept in files with the
.sld extension.
(define-library (stats basic)
(export mean variance)
(import (scheme base))
(begin
(define (mean xs)
(/ (apply + xs) (length xs)))
(define (variance xs)
(let ((m (mean xs)))
(mean (map (lambda (x) (square (- x m))) xs))))))
The library imports (scheme base), the core of R7RS. Inside it,
square is available; a plain Guile script has no such
procedure without an import. variance is the mean of the
squared distances from the mean.
(import (scheme base)
(scheme write)
(stats basic))
(define data '(2 4 4 4 5 5 7 9))
(write (mean data))
(newline)
(write (variance data))
(newline)
(write (exact->inexact (mean '(1 2))))
(newline)
The program imports (scheme base) and (scheme
write), which provides write and display,
and the library. Guile 3.0.7 searches only for .scm files by
default; without more options the import fails with no code for
module (stats basic). The -x .sld option adds the
extension to the search.
$ guile -x .sld -L . measure.scm 5 4 1.5
The load path
The -L option must be given on every run. A program can instead
add its own directory to the load path with add-to-load-path,
placed before the use-modules form. The
GUILE_LOAD_PATH environment variable is a third way, suited to
a set of modules shared by several programs.
(add-to-load-path (dirname (current-filename))) (use-modules (geometry) (text words)) (display (rectangle-area 2 5)) (newline) (display (word-count "modules keep the parts of a program apart")) (newline)
current-filename returns the absolute path of the source file,
and dirname takes its directory. add-to-load-path
takes effect while the file is compiled, so the following
use-modules finds both modules. The program runs without
-L, and it also works when started from another directory.
$ guile loadpath.scm 10 8
Source
Modules — GNU Guile Reference Manual
Creating Guile Modules — GNU Guile Reference Manual
In this article we have loaded library modules with use-modules,
selected, renamed, and prefixed imports, written modules with
define-module, #:export, and
define-public, kept definitions and state private, organised
modules in directories, resolved name conflicts, re-exported names, written
an R7RS library with define-library, and set the load path. The
SRFI 1 list library article covers the most
used library module, and the records article
defines types that a module can export.
Author
List all Scheme tutorials.