This chapter writes two small programs, checks them, builds them and runs
them. It introduces the shape of every Captain source file and the four
commands you will use most: check, build, inspect and run.
Hello
Save this as hello.capt:
module Hello.
;; A greeting, built from a name.
export greet name: Text -> Text [
ret "Hello, " append: name.
].Line by line:
module Hello.names the module. Every source file begins with amoduleline, and every top-level form ends with a full stop.;;starts a comment that runs to the end of the line.export greet name: Text -> Text [ ... ].declares a procedure. Its name is the verbgreettogether with the selectorname:. It takes one parameter,name, of typeText, and returnsText.exportmakes it visible outside the module, which also makes it an entry point you can run.- The body is a block,
[ ... ].retreturns a value. "Hello, " append: namesendsappend:to the text"Hello, "withnameas the argument. Text joins withappend:; there is no+for text.
Check it
$ captain check hello.capt
ok hello.capt (module Hello, 1 declaration)
checked 1 file as 1 module, no errorscaptain check parses the file and runs the whole static analysis: names,
types, effects, secrets. It runs nothing. A program that passes check is
one whose types fit and whose every effect is known.
Build and run it
$ captain build hello.capt
built hello.capb ([..] bytes, sha256 [..])
$ captain run hello.capb greet name=Ada
run log: greet.runlog
"Hello, Ada"captain build compiles the module to an artifact, hello.capb. Without
-o, the artifact takes the source file's name. captain run takes the
artifact, the entry point, and one selector=value argument for each of the
entry's selectors. It prints the result as a Captain value: text is shown in
quotes.
It also writes a run log, greet.runlog, which records the program, the
arguments and the outcome. A pure program has no effects to record, so this
log is short. Determinism, run records and
replay explains what a run log
holds and how captain replay uses it.
Ask the artifact what it is
$ captain inspect hello.capb
package Hello 0.0.0
sha256 [..]
compiler [..] vm [..] stripped
exports
greet name: Text -> Text (pure)
entry points
greet name: name=<Text>
pools: [..]captain inspect reads the artifact's manifest and runs nothing. The
(pure) after greet says that the checker found no effects in it at all.
An export that can have effects says (mutating) instead, with the effects
it can reach, and the manifest lists the tools and secrets the artifact
needs. Before you
run an artifact someone else built, inspect tells you what it can do.
The vm field is the virtual-machine generation the artifact needs; the
compiler records the lowest one that can run it
(Versions and VM generations).
A mistake
Captain looks a little like Smalltalk, and a little like languages where +
joins strings. Try that:
module Oops.
export greet name: Text -> Text [
ret "Hello, " + name.
].$ captain check oops.capt
error[CAP-TYPE-015]: cannot apply arithmetic to Text and Text
--> oops.capt:4:7
4 | ret "Hello, " + name.
| ^^^^^^^^^^^^^^^^
help: texts join with `append:`: `(a append: b)`, and a number becomes text with `Text from: n`
checked 1 file as 1 module, 1 with errorsEvery diagnostic has a stable code (CAP-TYPE-015), the place it applies
to, and, where the checker can tell, a help line saying what to write
instead. Codes are stable so that tools and tests can rely on them;
Diagnostic codes lists them all.
A calculation
The second program does arithmetic with money. Save it as price.capt:
module Price.
;; The price of `count` items at `each`, with tax at `rate` percent,
;; rounded to cents, ties to the even cent.
export total count: i32 each: Decimal rate: Decimal -> Decimal [
net := each * (count as: Decimal).
tax := (net * rate) divide: 100.0 scale: 2 rounding: #HalfEven.
ret net + tax.
].New here:
- A procedure with three selectors. Its full name is
total count:each:rate:, and a call passes all three, in that order. net := ...declares a local. Its type comes from the value:Decimal.countis ani32. Captain never converts between number types on its own, so the program sayscount as: Decimal, which is exact.net * rateis exact:59.97 * 7.5is449.775. Tax is due in cents, so the program divides by 100 and keeps two places, rounding ties to the even cent. That is the only place a digit is dropped, and the program says so.
$ captain build price.capt
built price.capb ([..] bytes, sha256 [..])
$ captain run price.capb total count=3 each=19.99 rate=7.5
run log: total.runlog
64.47Three items at 19.99 are 59.97; tax at 7.5% is 4.49775, which rounds to 4.50; the total is 64.47. Run it again with the same arguments and you get the same answer, on any machine, every time.
If the program had written (net * rate) / 100.0, the division would have
been exact here (4.49775), and the total would have had five decimal
places. Had the quotient been one no decimal can hold, such as a third, the
run would have failed with CAP-RUN-INEXACT rather than quietly cutting
digits. Division and rounding
explains why.
What to read next
- The toolchain goes through every
captaincommand. - A tour follows a program that changes something through plan, approval and run.
- The language starts the full description of Captain.