Captain / Documentation / Part 6. The standard library

Using library modules

Contents ↓

A Captain program reaches library code in three ways. The prelude needs nothing. A compiler-known module needs a use line. A source module needs a use line, qualified calls, and its source file given to the compiler.

The prelude

Every module can call the prelude's operations and effects without importing anything: (t trimmed), xs join: ", ", Int parse: t, readFileText path: p maxBytes: 64KiB. Pages 2 to 7 of this part list them.

Compiler-known modules

Std::Text, Std::Json, Std::EcmaNumber and a handful of others have no .capt source. The compiler adds their names when a module says use. How the names are then spelled depends on the module, and Compiler-known modules gives each one:

  • use Std::Json. brings the module value Json into scope, and its operations are sent to it: Json decode: text as: Release.
  • use Std::Text. brings one free procedure, called without a prefix: split text: line on: ",".
  • use Host::Debian. brings effects that are called without a prefix inside an SSH frontier: package install: #( "curl" ).

Nothing else is needed: captain check and captain build know these modules.

Source modules

A source module is an ordinary .capt file that starts module A::B. and marks what it offers with export. To use one:

  1. Import it with its full path: use Std::Utc.
  2. Call its exported procedures qualified by the last segment of the path: Utc::leap year: 2028, AptGet::install package: "curl", File::readText path: p maxBytes: 64KiB. An unqualified call (leap year: 2028) does not resolve, and neither does the full path (Std::Utc::leap year: 2028).
  3. Name its exported types the same way: Http::Request, Linux::LinuxHost, List[FileTree::Entry].
  4. Give the compiler the module's source, either as another file on the captain check command line or as an include path with -I.
dates.capt
module Example::Dates.

use Std::Utc.

;; An observed instant, as ECMAScript's Date.toISOString writes it.
export stamp millis: i64 -> Result[Text] [
  ret Utc::isoMillis milliseconds: millis.
].

export february year: i64 -> i64 [
  ret Utc::monthDays month: 2 year: year.
].

The full path is refused:

Refused by the checker: CAP-RESOLVE-002
module Example::Dates.

use Std::Utc.

export leap year: i64 -> bool [
  ret Std::Utc::leap year: year.
].

Checking and building

captain check takes every file it should check together. captain build takes the root file and finds imported modules beneath each -I path: it reads every .capt file under that directory, matches use lines against their module declarations, and links only the modules the program reaches. So -I lib/std and -I lib give the same artifact for this program.

Terminal
$ captain check dates.capt lib/std/utc.capt
ok    lib/std/utc.capt (module Std::Utc, 8 declarations)
ok    dates.capt (module Example::Dates, 2 declarations)
checked 2 files as 2 modules, no errors
$ captain build dates.capt -I lib/std -o dates.capb
built dates.capb ([..] bytes, sha256 [..])
$ captain run dates.capb stamp millis=1767225600123
run log: stamp.runlog
Ok { v: "2026-01-01T00:00:00.123Z" }
$ captain run dates.capb february year=2028
run log: february.runlog
29

The linked artifact carries the library code it used. Nothing is loaded at run time, and the artifact's digest covers the library as well as your module.

Reading a module's surface

captain inspect accepts a source file as well as an artifact, so it is the quickest way to see what a library module exports, which effects each export can perform, and which external tools it needs:

Terminal
$ captain inspect lib/std/text_lists.capt
package  Std::TextLists 0.0.0
sha256   [..]
compiler 0.1.0   vm 2   stripped

exports
  equalOrdered left: List[Text] right: List[Text] -> bool  (pure)

pools: [..]

For a module that imports others, add -I lib: captain inspect lib/tools/curl.capt -I lib lists get url: as (mutating) [exec program:args:] and the tool requirement curl >=7.68.0.

Effects a module exports

Some modules export effects rather than procedures, written export effect name on: Frontier .... An exported effect is called the same way, qualified, but only where its frontier is current. System::RemoteFile exports installText on SshHost, so it is called inside an SSH block:

Fragment
on host: server using: #Ssh do: [
  RemoteFile::installText path: "/etc/app.conf"#path content: text
    expectedDigest: digest owner: "root:root" mode: "0644".
].

That fragment belongs inside a procedure that has bound server with observe serverAt: (see Processes, HTTP and hosts). captain inspect lists exported effects under effects with their frontier (@ SshHost) and (defined); effects the runtime supplies are (declared).

Structs from another module

A program cannot write a record literal of another module's struct: Http::Header { name: "a", value: "b" } does not parse. Modules whose structs callers need to build export builder procedures instead, such as Http::header name:value: and Ui::text text:.