A program outgrows one file the day two ideas stop wanting to share a
screen. Mica’s answer is a compilation unit whose surface is declared —
exp marks what escapes, everything else is private — and an import
doctrine with one rule and no special cases.
The example is Units: three files, one dependency order, the smallest complete build.
An object unit and its surface
object geometry;
imp
SquareRoot = Sqrt : math;
type
exp Point = record
X : float64;
Y : float64;
end;
function SquareOf(v : float64) : float64; { private — no 'exp' }
begin
SquareOf := v * v;
end;
exp function Distance(a : Point, b : Point) : float64;
begin
Distance := SquareRoot(SquareOf(b.X - a.X) + SquareOf(b.Y - a.Y));
end;
exp function Midpoint(a : Point, b : Point) : Point;
var
m : Point;
begin
m.X := (a.X + b.X) / 2.0;
m.Y := (a.Y + b.Y) / 2.0;
Midpoint := m;
end;
begin
end.An object unit is a compilation unit without a program entry:
declarations, a surface, and a body, empty here, that runs when the program
starts. The surface is Point, Distance, Midpoint — and not SquareOf, however
useful it looks. Try to import it and the compiler explains the whole
model in one sentence:
analyzer error 5021: no declarations found in namespace 'geometry' for
identifier: SquareOf — a library exports only its 'exp'-marked
declarations, so a name that exists in its source may still be privateA private helper is not a convention; it is unreachable. The unit’s inside stays free to change without a single importer noticing.
One import rule, no special cases
imp
WriteLn : std;
Point, Distance, Midpoint : geometry;
PathLength : statistics;geometry is a namespace exactly like std or strings — the import
doctrine is one rule: everything arrives by imp, whether written by
the Mica project years ago or by you, this morning, in the file beside this
one. Nothing is ambient; every name in a file traces to a declaration or an
import line at the top. The rename SquareRoot = Sqrt : math is part of the
same rule: the name left of = is the one the file uses, so geometry calls
math’s Sqrt as SquareRoot — the importer chooses its local vocabulary.
Surfaces compose: statistics imports Point from geometry and speaks it
in its own exported signature, so the program above it receives a type that
traveled through two units:
Units — a program grown past one file
From geometry
home to shop: 5.000000
midpoint of that walk: (1.500000, 2.000000)
From statistics
home via shop to park: 11.000000The build is a list, the order is a discipline
MICA_SOURCE_FILES := geometry.mica statistics.mica Units.mica
The compiler accepts the sources in any order — it resolves the imports itself. The discipline of listing each unit before its importers is for the reader: a build whose list is a valid dependency order can be read top to bottom, each file naming only files already understood. Every multi-unit program on this site keeps that discipline, and none of them needs to.
The mica.project beside the sources states the same list, so
the editor’s diagnostics are the build’s — same
units, same surfaces, while you type.
The third road: a library, and its emitted contract
Inside one build, units see each other’s surfaces directly. The moment a library is built separately — its own archive, consumed by programs it knows nothing about — the surface must travel with it. Mica’s rule: the compiler emits it, nobody writes it.
The Utilities library builds with two switches, and through its project file, which states the version a library is linked under:
MICA_EXTRA_FLAGS := --output-modifier archive --emit-contract --project .
and the build directory then holds the archive and mica.external — the
import surface of every unit in it, written by the compiler from the exp
marks. A consumer names that contract in its own project file and imports
as if the library were a sibling:
{
"version": "1.0.0",
"sources": ["Consumer.mica"],
"contracts": ["../build/Utilities"]
}The archive carries the same contract inside it, so a consumer may name the
library’s archive or shared object in contracts instead of the directory,
and the file beside it becomes optional; where both stand, they must state the
same contract, as the contracts reference
explains.
A library’s generic routines and types travel in its contract as source and are compiled where they are imported, so a consumer whose import uses one names the contracts of the libraries that source uses as well, beside the library’s own. A library’s compiled exports cannot yet spell an instance of a generic type in their headings; the known limitations say what to export instead.
The compiler’s test corpus holds such pairings at every change. In the starter repository, Utilities’ consumer is a plain C program, the boundary’s proof from the other side.
No header to repeat, no binding file to maintain, no drift possible between what a library exports and what its consumers see — the contract is compiler output, regenerated with the archive it describes.
The unit lifecycle: who runs, and when
The closing begin ... end. block of an object unit and of a library is
its initialization, and it runs once when the program starts:
- An
objectunit’s body runs before the program’s own body, and after the bodies of the units it imports, whatever order the build lists them in. It sets up the unit’s variables, which live in the unit’s own storage. - A separately built
libraryunit’s body runs the same way: real initialization, executed before the consuming program’s own body, establishing the library’s static state. - Inside either body,
defer until programregisters a program-lifetime finalizer: its statement runs once at process exit, after the program’s body, in last-in-first-out registration order, reading the unit’s statics as the program last left them — not as initialization first set them. Across units the finalizers run in the reverse of the order the bodies ran, so a library’s finalizer runs after the finalizers of every unit that imports it, which may still call it.
The compiler’s own test corpus pins the whole shape; its output is the lifecycle in four lines:
main start count=10 { the library's body ran first: count is 10 }
main end count=13 { the program mutated the library's static }
fin-second { registered last, runs first — LIFO }
fin-first count=13 { reads the static as the program left it }Acquisition in the body, release at exit, both beside the state they manage — the same shape defer gives an activation, lifted to the library’s whole lifetime.
What this does not do
- Signature-private types are legal, and they mean something. A unit may export a function whose result type it keeps private; importers hold such a value without naming its parts — the opaque-handle idiom, chosen rather than forbidden.
- No startup cycle. Object units may import each other, but a cycle in
which a unit’s body runs has no order to start in, and the build refuses
it:
The object units import each other in a cycle through 'left', and an object in it initializes: no startup order exists, so one of the imports must be broken apart. Two units that need each other are usually one idea split wrongly; the dependency order the site’s examples keep visible is the design tool for finding the right split. - No transitive re-export. Importing
statisticsdoes not hand yougeometry— every file’s imports name exactly what that file uses.
Try it
make -C mica-container/examples/Units runAdd imp SquareOf : geometry; to Units.mica and meet refusal 5021. Then
mark SquareOf with exp, watch the import succeed — and notice you just
made a promise to every future consumer that the helper will keep existing.
exp is an API decision; the refusal is the compiler asking whether you
meant to make one.
Next
Generics composes with everything here — an exported
generic function is a surface too. The series continues with the tour’s
re-homed lessons: maps, bit records, and the leave family.