Captain / Documentation / Part 3. The language

Procedures and keyword selectors

Contents ↓

A procedure is Captain's unit of code. This chapter covers how one is declared, how it is named, how calls are written and where a call ends, how values come back, and which procedures a host can run.

Declaring a procedure

Captain
module Example::Areas.

export area width: i32 height: i32 -> i32 [
  ret width * height.
].

A declaration has, in order:

  1. export, if other modules and hosts may call it;
  2. a lowercase verb, here area;
  3. zero or more parameters, each a selector and a type: width: i32;
  4. -> and the result type, or nothing for a procedure that returns no value;
  5. the body, a block in [ ];
  6. a . to end the declaration.

Each parameter is named by its selector: the body above reads width and height. Every parameter and the result have written types; only locals take their type from their first value.

A procedure's name is its verb and selectors

A procedure is identified by its verb together with its selectors, in order. These are three different procedures, and they can live side by side:

areas.capt
module Example::Areas.

export area width: i32 height: i32 -> i32 [
  ret width * height.
].

export area side: i32 -> i32 [
  ret side * side.
].

export area radius: i32 -> i32 [
  ret 3 * radius * radius.
].

Diagnostics and captain inspect name them area width:height:, area side: and area radius:. There is no other overloading: no two declarations in a module may share a verb and a selector spelling, and parameter types do not distinguish procedures.

Because the selectors are part of the name, a call must give them in the declared order. area height: 2 width: 3 is a call to a procedure named area height:width:, and if none exists the checker says so and names the nearest one:

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

export room -> i32 [
  ret area height: 2 width: 3.
].

That is the point of keyword selectors: at every call, each argument is labelled with what it means. A reviewer reading transfer amount: a from: x to: y cannot mistake which account is which, and neither can a model writing it.

Calls

Every call is a head followed by one or more selector: argument pairs.

  • In a free call the head is a procedure's verb: area side: 4.
  • In a qualified call the head names an imported module's procedure: Prices::total of: line (see Modules and names).
  • In a method call the head is a value, and the selectors name an operation on it: name append: "!", xs at: 0, flag ifTrue: [ ... ]. The built-in types' methods are listed in the standard library.

These are one form in the grammar. Name resolution decides which it is.

Where a call ends

A keyword call binds more loosely than any operator. Each argument is a whole operator expression, and a call takes every selector that follows it:

  • area side: n + 1 is area side: (n + 1): the argument is the sum.
  • area width: a height: b is one call, with the selectors width:height:.
  • f x: g y: z is also one call, to f x:y:, with the arguments g and z. It is never f x: (g y: z).

To pass the result of one call to another, put the inner call in parentheses. Parentheses are the only way to nest or chain keyword calls:

calls.capt
module Example::Calls.

double n: i32 -> i32 [ ret n * 2. ].

export quadruple n: i32 -> i32 [
  ret double n: (double n: n).
].

export doubledPlusOne n: i32 -> i32 [
  ret (double n: n) + 1.
].

export label n: i32 -> Text [
  ret "n = " append: (Text from: n).
].

Leaving the parentheses out gives a call to a procedure that doesn't exist:

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

double n: i32 -> i32 [ ret n * 2. ].

export quadruple n: i32 -> i32 [
  ret double n: double n: n.
].

The checker reads that as one call to double n:n: and answers no `double n:n:` in scope; nearest is `double n:`. A call used as an operand needs parentheses too: n + double n: 1 is read as the call (n + double) n: 1, and double alone is not a value.

The same rule applies to methods: Octets fromUtf8: "a" append: "b" asks Octets for a method fromUtf8:append:. Write (Octets fromUtf8: "a") append: (Octets fromUtf8: "b").

These rules are fixed by DR-0003. The alternative, where calls bind tighter than operators or chain without parentheses, makes the extent of a call depend on what the reader knows about each procedure's arity. In Captain the extent is visible on the page.

Reading members

A member, such as a struct's field or a text's size, has no colon and is read inside parentheses: (line price), (t size), (xs count). Several members chain left to right inside one pair: ((Octets fromUtf8: t) sha256 hex) is the hex of the digest of the octets.

Outside parentheses, a name after a value is a syntax error:

Refused by the checker: CAP-PARSE-SEP
module Example::Members.

export shout t: Text -> Text [
  ret t trimmed.
].

The help line says to write (t trimmed). Captain has no unary messages: every operation either takes a keyword argument or is a member read in parentheses, so the reader never has to decide where one ends.

There are no zero-argument calls

A call always has at least one selector: argument pair. A bare name is a value, never a call. So a procedure with no parameters cannot be called from Captain code:

Refused by the checker: CAP-RESOLVE-001
module Example::Answer.

export answer -> i32 [ ret 42. ].

export twice -> i32 [
  ret answer * 2.
].

`answer` is not bound here: there is no local named answer, and the procedure cannot be invoked by its name alone. A procedure with no parameters is useful only as an entry point: an export that a host runs. To share a constant between procedures, give the procedure that needs it a parameter, or compute it where it is used.

Returning a value

ret expr returns from the procedure. It may appear anywhere in the body, including inside a nested block, where it returns from the whole procedure, not just the block:

sign.capt
module Example::Sign.

export sign n: i32 -> Text [
  (n < 0) ifTrue: [ ret "negative" ].
  (n = 0) ifTrue: [ ret "zero" ].
  ret "positive".
].

There is no implicit return: the value of the last statement is not the procedure's result. A procedure with a result type must reach a ret with a value of that type.

The checker proves that every path through the body reaches a ret, and refuses a procedure that could fall off its end (CAP-TYPE-019):

Refused by the checker: CAP-TYPE-019
module Example::Fall.

export pick flag: bool -> i32 [
  flag ifTrue: [ ret 1 ].
].

A path ends when it reaches ret, when both blocks of ifTrue:ifFalse:, ifOk:ifFail: or ifSome:ifNone: end, or in a loop that never stops, [ true ] whileTrue: [ ... ]. The proof is plain: it does not reason about values, so (n > 0) ifTrue: [ ... ] followed by (n <= 0) ifTrue: [ ... ] is not enough, even though one of them always runs. End such a procedure with a ret.

Procedures that return nothing

A procedure with no -> (or with -> void) returns no value. Inside it, ret with no expression returns early. Its call is a statement; binding its result to a name is refused:

Refused by the checker: CAP-TYPE-010
module Example::Void.

check n: i32 [
  (n < 0) ifTrue: [ ret ].
].

export run n: i32 -> i32 [
  result := check n: n.
  ret n.
].

The message is `result` cannot be bound to a void expression.

A computed value must be used

A statement whose value is the result of a pure built-in operation, and which throws that value away, does nothing. Values in Captain never change in place: names push: x gives a new list and leaves names as it was. So such a statement is refused, CAP-TYPE-030:

Refused by the checker: CAP-TYPE-030
module Example::Lost.

export trimmed t: Text -> Text [
  t replace: " " with: "".
  ret t.
].

Assign the result: t := t replace: " " with: "". The rule exists because the mistake looks like working code, to a person and to a model. The control forms (ifTrue:, forEach:, ifOk: and the rest) are statements and are not affected. A call to one of your own procedures is not refused either, since a procedure may perform effects that are worth running for themselves.

Recursion and order

Procedures may call themselves and each other, and may be declared in any order: a body may call a procedure declared further down the file.

Entry points

An exported procedure is an entry point: a host can call it by name. captain inspect lists a module's exports, and the entry points that the command line can call with the arguments it can spell:

Terminal
$ captain inspect sign.capt
package  Example::Sign 0.0.0
sha256   [..]
compiler 0.1.0   vm [..]   stripped

exports
  sign n: i32 -> Text  (pure)

entry points
  sign n:  n=<i32>

pools: [..]

(pure) means the procedure performs no effects at all. captain run passes each argument as selector=value:

Terminal
$ captain build sign.capt -o sign.capb
built sign.capb ([..] bytes, sha256 [..])
$ captain run sign.capb sign n=-4
run log: sign.runlog
"negative"

The command line spells scalars, text, symbols, tagged values and octets. An export that takes a list or a struct is still an export, and a host embedding Captain can pass those values (Values in and out), but captain run does not list it as an entry point.

Order of evaluation

A call evaluates its head first, then its arguments from left to right. Operands of an operator are evaluated left to right, and & and | evaluate their right operand only when the left one does not decide the answer.