A contract is how a Mica program learns what a foreign library offers: its functions, their exact machine-level names, the types that cross the boundary, and how each function announces failure. The compiler consumes contracts instead of parsing C headers — the contract states the facts once, in one reviewable file, and every import resolves against it.

This page is the normative reference for the format. The guided introduction is Mica calls C; the failure-lifting discipline has its own article in error domains.

Where contracts come from, and where they are found

Three roads produce a contract file:

  • You author one beside a C library you want to call — the MicaCallsC example is a full worked instance, and DebugWalk a minimal one.
  • The compiler writes one into every Mica library it links: the contract travels inside the library’s archive or shared object, in a section of its own beside the library’s manifest, so another program imports the library by naming the archive or the shared object itself. With --emit-contract the same document also lands beside the archive as the file mica.external.
  • Some ship inside the compiler. The C standard library, POSIX, and Linux surfaces are embedded contract sets selected by imp ... : c, : posix, : linux — nothing on disk is consulted. The Dragon SDK’s curated contract is installed for reading at /usr/share/doc/mica-dragon-sdk/dragon-engine.json, a real contract over a real shipped archive you can open on your own machine.

A consumed contract is found beside the sources, named explicitly with --external-contract, or listed in mica.project. Each entry names a contract file, the directory holding one, or a library’s archive or shared object. The published JSON schema ships in the container as reference/library_contracts.schema.json.

A contract inside the library

An import reads what it names. A contract file, or the directory holding one, is read as the file states it. A library’s archive or shared object is read through the contract inside it, and links that binary, wherever it has moved since its build:

{
    "version": "1.0.0",
    "sources": ["Survey.mica"],
    "contracts": ["lib/measure.a"]
}

A mica.external beside the binary that describes the same library is a second statement of one contract, so the two must state the same document. Where they differ, the import is refused and names both files, and the compiler never chooses one in silence:

Failed to resolve imported libraries: the contract inside '/home/dev/lib/measure.a' and the contract file '/home/dev/lib/mica.external' beside it describe the library 'measure' differently: rebuild the library, which writes both again, or remove the file that is stale

A binary that carries no contract inside it, a C library’s archive among them, is read through the mica.external beside it, the one statement there is. With no file beside it either, the import is refused by the binary’s name. A Mica library whose contract could not carry its surface, a generic instance or a fixed array written in place in an exported heading, built without --emit-contract, and for a generic instance without --emit-header too, carries no contract and records why in its manifest. A note of the build and the manifest cite each refusal as interop code <n>, the spelling by which a refusal that did not stop the build is cited, so no line of a finished build reads as a diagnostic of its own; mica --inspect build/rows.a shows the member no_contract among the manifest’s others:

"interface":null,"no_contract":["interop code 588: library 'rows' exports the routine 'Sum', whose heading spells the type 'array[1..3] of int64' in place: a compiled library's contract carries an array of fixed size only under the name a declaration gives it, so declare the type in a 'type' section, mark it 'exp' and spell its name here"]

An import of the file is refused by those words:

Failed to resolve imported libraries: '/home/dev/lib/rows.a' carries no Mica library contract: the build of the library 'rows' left it out, since its contract would be refused with interop code 588: library 'rows' exports the routine 'Sum', whose heading spells the type 'array[1..3] of int64' in place: a compiled library's contract carries an array of fixed size only under the name a declaration gives it, so declare the type in a 'type' section, mark it 'exp' and spell its name here

Any other binary without a contract is refused with the way out of a rebuild:

Failed to resolve imported libraries: '/home/dev/lib/libcscale.a' carries no Mica library contract and no 'mica.external' stands beside it: a library the Mica compiler links carries its contract inside its archive or shared object, so rebuild the library with this compiler, or name the contract file that describes it

mica --inspect on a library prints the contract it carries, as the member contract after the manifest’s own.

The file

One JSON document: a description and a list of libraries. Every name a program imports resolves against exactly one library entry.

{
  "description": "External library contracts for the MicaCallsC example",
  "libraries": [
    {
      "name": "micacallsc",
      "language": "c",
      "type": "external",
      "contract": { ... }
    }
  ]
}
FieldValuesMeaning
namestringthe namespace the import names: imp Touch : micacallsc
languagec · micawhich side of the boundary the library was written on — it decides calling and failure semantics
typeexternal · system · internalexternal links an archive the link block names; system names an external system library; internal marks a surface embedded in the compiler — you author external, and system where the linker should find the library itself
import_protectedbooleanwhen true, the library’s names are protected only at their import site — other units stay free to declare the same names for their own containers. The class a vocabulary unit wants: the bits unit’s Count beside the maps unit’s Count. The standard library carries this class, so a program may declare Length or Capacity for itself and reach the library’s own entry through the namespace it imported, std.Length(values); importing the entry and declaring the name in one unit is what the import site refuses
versionstringthe library’s version, as Semantic Versioning writes it: "2.0.0". A contract the compiler emits carries the version the library’s mica.project states, and every program built against the library names it in its build and its manifest. Optional; two contracts stating two versions of one library are refused
interfacestringthe digest of the library’s interface, sixty-four hexadecimal digits a Mica library’s build writes into the contract it emits and into the archive’s manifest. A program’s link compares the two and refuses an archive of another build: the archive 'lib/Meter.a' is another build of the library 'Meter' than the contract the program was built against describes. An authored contract omits it
pkg_configobjecta C library’s version as the system the program is built for holds it: {"package": "sqlite3", "minimum": "3.35.0"} names the package the system’s pkg-config knows the library by and, optionally, the oldest version the contract works with. Stated instead of version
contractobjectthe surface itself: link, types, constants, functions, generic_templates

A C library’s version belongs to the machine the program is built for, so a contract names the library’s package and the build asks that machine’s pkg-config, as autoconf, meson and CMake ask it: pkg-config for the machine’s own architecture, aarch64-linux-gnu-pkg-config or x86_64-linux-gnu-pkg-config for a cross build. The version it answers is the one the build names, Using library 'sqlite3' 3.46.1, the one the manifest records and the one a project’s requires reads, and a system below the minimum answers with the library’s name:

the C library 'sqlite3' is at version '3.31.1' on this machine, as 'pkg-config' answers for the package 'sqlite3', and its contract requires at least '3.35.0'

A tool that is not installed and a package it does not know are refused by the library’s name the same way; the development files of the library for the build’s target are what the system needs.

What joins the link line when any function of this library is consumed — stated per link profile, chosen by the --external flag:

"link": {
  "static": { "flags": ["../build/MicaCallsC/libmicacallsc.a"] },
  "shared": { "flags": ["-lmicacallsc"] }
}

Both profiles are required; each is a list of linker arguments, spelled exactly as they should reach the linker.

A Mica library’s build makes one file, its archive or its shared object, and the contract the compiler writes for it names that file under both profiles. So a program links a Mica library the same way under --external static and --external shared; the profile it chooses decides only for a library whose contract names two forms. Behind the file, an archive’s contract names under each profile that profile’s flags of the libraries the archive was built against, so a C library that comes as an archive and as a shared object reaches the program in the form the program’s profile chooses. A shared object linked those libraries at its own build, so its contract names the same flags under both profiles.

A Mica library whose contract names nothing to link under the profile a build chooses, and whose file no other library’s contract brings onto the line, is refused by name before the linker runs:

Failed to link: the contract of the library 'Meter' names nothing to link under the 'shared' link profile, so no file of the library would reach the link: rebuild the library with this compiler, whose contract names its file under both profiles, or link with '--external static'

A contract may name a library as an installed one is named, by its folder and its name:

"link": {
  "static": { "flags": ["-L/opt/meter/lib", "-lmeter"] },
  "shared": { "flags": ["-L/opt/meter/lib", "-lmeter"] }
}

The link finds the file such a name stands for as the linker finds it: the -L folders in their order, then the linker’s own folders; in each folder libmeter.so ahead of libmeter.a, and the archive alone where -Bstatic or --static is in force; -l:Meter.a names the file itself. A file built for another processor is passed over. When the file found is a Mica library, it is judged exactly as a file named by path, and it is that file the program links.

Two things the link does not follow: a linker script, one named by -T or a text file a name finds in place of a shared object, may name further files and folders, and a Mica library reached only through such a script is not judged; and of a thin archive found for a name (ar --thin), whose members stand in files of their own, the processor it was built for is not read.

Contracts are linked in the order they are named, their flags as written. A file that a second contract names again reaches the linker once. One repeat stays: an archive that a contract names again inside a --start-group … --end-group it writes, so the group holds every archive its contract put there.

The order decides nothing between Mica libraries. Behind the contracts the compiler names every Mica library archive once more, each ahead of the libraries it was built against, so a library finds the library it calls wherever their contracts are named, and so does a C archive that calls a Mica library. A C archive that needs another C archive is placed by its contract alone: name the caller’s contract first, or write the two archives in one group.

Every Mica library file the link takes, those another library’s contract brings included, is judged by its own name before the linker runs: the compiler that built it, its interface and version against what the libraries built against it record, its tasking flavor and the parts of the runtime floor it reaches. A shared object is judged as an archive is, with one difference: an archive joins only a program of its own tasking flavor, while a shared object built for one carrier joins a program of either. A file among a contract’s flags that carries no Mica manifest, a C library for one, is the linker’s to take. Two different builds of one library on one link line are refused by both paths:

Failed to link: the link takes two builds of the library 'Base': the archive '.../base/b1/Base.a', which the contract of 'Left' names, and the archive '.../base/b2/Base.a', which the contract of 'Right' names: link one build of 'Base', and rebuild against it each library whose contract names the other

One build at two paths, the same file or a copy of the same bytes, links.

Types

Types declared here become importable Mica types, and they are the only types that may cross the boundary by name. kind selects the shape:

kindDeclaresCarries
recorda field sequencefields, optional packed, optional layout
enumnamed literalsliterals, base_type
subrangea bounded integerbase_type, lower_bound, upper_bound
arraya fixed arrayelement_type, dimensions (each with index_type and both bounds)
dynamic_arrayan array of Telement_type
seta set over an ordinalbase_type
bitseta fixed-width bit setbase_type
bitrecordnamed bit fields in one carrierbit_fields (each with bit_offset and bit_width), base_type
aliasa second name for a typetype, the reference the name stands for; the alias resolves to that type’s own descriptor, so both names are one type and assign both ways
opaquea C type whose layout is the C side’s alonenothing: no fields, variants or layout; Mica holds it only through a pointer. Optional thread_safe
error_domaina library’s own failure codesliterals, the codes in order; the domain the errors block’s domain names. For the status_code convention every code but the first states the C value it stands for, and the first stands for every status the domain does not list
wide_integerint[N] or uint[N] above 64 bitswidth, optional signed

A library compiled with --emit-contract writes its exported aliases this way: exp Same = Inner becomes an alias over the named reference to Inner, and exp Count = int64 an alias over the built-in, so an importer spells the name the library declared and gets the type it declared it over.

An opaque type is C’s incomplete type, struct sqlite3;: the library knows its layout and the contract states none. Mica never holds one by value, only a pointer to it, and such a pointer is a handle: it names an object the C library owns and Mica never reads. So a handle crosses into a generator or a stream’s element as the word it is, and every activation it is handed to holds the same object. A task or a stream placed on a process refuses a handle, because the worker’s copy of the word names nothing there.

"thread_safe": true states that the C library allows one of its objects on several threads at once, as sqlite does for a connection opened in its serialized mode. Only a handle to such a type crosses into a task on a carrier or into a stream a scheduled producer feeds, because the task runs beside the activation that scheduled it and both hold the handle. Every other handle stays on the thread that holds it: a generator the program pulls takes it, and tasks share it only through a variable of the activation that scheduled them, touched in synchronized statements, whose lock then admits one task at a time. State thread_safe only when every function of the contract that takes the handle is safe from several threads at once, and never for an object whose functions answer memory another thread’s call can free, such as the text a sqlite statement answers for a column. When one function of an otherwise safe object answers such memory, a library keeps it in a contract of its own that programs never consume, and reaches it only under the lock that makes it safe: the sqlite library keeps sqlite3_errmsg beside the connection’s mutex in sqlite3-internal.external.

Everywhere a type is referenced — a field, a parameter, a return — the reference is an object with its own kind:

{"kind": "builtin", "name": "int32"}
{"kind": "named",   "name": "ScalarPacket"}
{"kind": "pointer",       "to": {"kind": "named", "name": "ScalarPacket"}}
{"kind": "const_pointer", "to": {"kind": "builtin", "name": "uint8"}}
{"kind": "borrowed_pointer", "to": {"kind": "named", "name": "Sqlite"}}
{"kind": "dynamic_array", "element": {"kind": "builtin", "name": "float64"}}
{"kind": "tracked", "element": {"kind": "builtin", "name": "float64"}}
{"kind": "c_text"}
{"kind": "void"}

A named reference names a type of the contract’s own types by its bare name, or another library’s type as library.Type; blanks and tabs around the dot belong to neither name. A name with a blank, a tab or either bracket inside it, the spelling of a type written in place, or with more than one dot is refused where the contract loads, with interop error 421, so a fixed array crosses only under a name of its own, as an array entry of types.

pointer and const_pointer differ exactly as the C qualifier does, and the compiler holds an import to it: a const_pointer parameter cannot be written through. A borrowed_pointer carries no ownership with it: the cell it designates belongs to someone else — a C library’s own handle above all — so the importing program’s ownership analysis demands no release for it. It is how a Mica library’s borrowed pointer T result crosses an emitted contract whole. c_text is a C function’s result alone: the NUL-terminated UTF-8 a const char * points to, which the C side owns. The Mica signature answers a string, and every call copies the text into it, decoded for the program’s encoding, each ill-formed byte sequence one replacement character and a null pointer the empty string. tracked belongs to a Mica library alone: a tracked value, a float or a float tensor paired with the id of its tape node, whose id crosses a call beside the value as a word of its own, so no C signature has a place for it. A record whose shape genuinely differs per target may declare variants keyed by architecture (amd64, arm64) in place of the base fields — it must state every supported architecture, so a port cannot silently inherit the wrong layout. An emitted contract may additionally carry layout (sizes, alignments, field offsets), and it states every type’s fingerprint with its fingerprint_version: a digest of the type’s shape as the library compiled it. The importing compiler computes the digest again from the contract it reads, so a contract edited after the compiler wrote it is refused by the type’s name. An authored contract omits them.

Functions

{
  "language_name": "InotifyInit",
  "external_name_utf8": "inotify_init1",
  "external_name_utf32": "",
  "parameters": [
    {"name": "flags", "type": {"kind": "builtin", "name": "int32"},
     "passing_mode": "call_by_value"}
  ],
  "return_type": {"kind": "builtin", "name": "int32"},
  "variadic": false,
  "format_string": "none",
  "errors": {
    "convention": "negative_errno",
    "domain": "posix.OsError"
  },
  "description": "Create an inotify instance ...",
  "interface_file": "sys/inotify.h"
}
FieldValuesMeaning
language_namePascalCasethe name Mica source calls
external_name_utf8 / external_name_utf32symbol namesthe machine-level symbol per source encoding. A function that exists for one encoding only leaves the other empty, and consuming it from the wrong encoding is a compile error
parameterslisteach with name, type, and passing_mode — call_by_value, call_by_constant_value (passed as call_by_value passes it, under a read-only promise enforced on the Mica side: the const promise on a scalar or small value, and the promise the compiler writes for a dynamic array or an aggregate wider than 16 bytes that an exported Mica routine only reads, which a C consumer still passes by value), call_by_constant_address (the const promise on a plain aggregate wider than 16 bytes: the value arrives as one pointer into caller-owned read-only storage, so a C consumer declares the parameter const T *), fixed or call_by_result, both below
return_typetype referenceomitted or {"kind": "void"} for none
variadicbooleanthe C ... — a Mica-borne entry never carries it, because the language has no variadic functions
variadic_keepsboolean, optionalwhether the routine may keep an argument past its parameters beyond the call, stated only beside "variadic": true, below
format_stringnone · write · readdeclares a printf/scanf-class function, which turns on compile-time checking of the format against the variadic arguments; format_string_index is the format parameter’s zero-based position — 0 for the printf shape, 1 for fprintf, 2 for snprintf
errorsobjectsee below
abiobjectoptional; its aggregate_return_mode — register or hidden_return — states how the C side returns an aggregate by value, where the two conventions differ
blockingbooleanoptional, default false: this C entry waits on the outside world by design — the fsync class. The stamp sharpens the compiler’s boundary-under-mark warning from “may stall” to “is declared to wait”; it never rejects a call
parksbooleanoptional, default false: the call may park the calling task on a descriptor. A parking entry’s external names must name a __mica_ parking shim — the runtime routine that tries the operation without blocking and suspends only the calling task until readiness
heaplessbooleanoptional, default false: a call never allocates or releases heap memory, so a heapless routine may call the entry. The compiler writes it for an exported Mica routine declared heapless; written by hand for a C function, it is the contract author’s promise, because no compiler sees into C code
taskbooleanoptional, default false: the entry is a Mica task. The compiler writes it for an exported task, so a program that imports the entry schedules it in a concurrent block and cannot call it
recordinglanguage nameoptional: the hidden entry that is this routine’s recording copy. The compiler writes it for an exported Mica routine that works on tracked values, so a call inside a program’s tape window runs the copy and records the routine’s body — see routines over tracked values
async_signal_safebooleanoptional, default false: a call may run inside a signal handler, because the function never waits and never takes a lock. Written by hand for a C function on POSIX’s async-signal-safe list; a callback bound at a signal slot may call only C functions that carry it
requires_flagslistadditional compiler or linker flags this one function needs on the link line
descriptionstringone sentence; surfaces in editor hover
interface_filestringthe C header documenting the function — documentation, not consumed

Two passing modes keep a parameter out of the Mica signature:

  • fixed with a value: the contract’s own argument, a machine integer every call passes at the parameter’s position, such as the -1 byte count and the SQLITE_TRANSIENT destructor sqlite3_bind_text takes. A value outside the parameter’s type is refused, and so is a fixed parameter in a variadic function or one that takes a callback.
  • call_by_result: the parameter’s type is a pointer to the word the C function writes, an integer or a pointer such as a handle, and the Mica call answers that word, as sqlite3_open_v2 answers its connection through a sqlite3 **. The call passes the address of a hidden word, zeroed before every call. The C function’s own result, its status or nothing, is what its errors convention reads. Where the C library answers a resource even when the call fails and asks for it back, the parameter’s release_on_failure names the library’s function that releases it, and a failing call hands a non-null word to it before the failure reaches the caller.
{"name": "database", "type": {"kind": "pointer", "to": {"kind": "pointer", "to": {"kind": "named", "name": "Sqlite"}}},
 "passing_mode": "call_by_result", "release_on_failure": "SqliteCloseV2"},
{"name": "vfs", "type": {"kind": "builtin", "name": "int64"}, "passing_mode": "fixed", "value": 0}

Two words of a parameter say what the routine does with what it is handed. The compiler writes both for an exported Mica routine, and a row leaves out a word that is false:

  • keeps: the routine may keep what it receives in storage that outlives the call, as its body proves. A container that may hold a cell, handed to such a parameter by value, is refused as a second owner.
  • owning: the routine’s heading writes owning, so the call hands over the cells the argument owns, a pointer’s cell or every cell a record’s fields own, and the routine releases them. The word stands only over a pointer or such a record passed call_by_value; a contract that states it over anything else is refused when a program imports it.
{"name": "root", "type": {"kind": "pointer", "to": {"kind": "named", "name": "Leaf"}},
 "passing_mode": "call_by_value", "owning": true}

A library’s exp procedure Release(root : owning pointer Leaf) writes that row, and a program that imports it hands the cell over with the call: the cell is released, and a release of it afterwards is refused by name. Where a row states no owning, the caller keeps the cell and releases it itself.

obligation error 13104 [20,5]: heap cell is disposed again after it was already released on every path reaching here — the call to 'Release' at line 19 took the cell from the call to 'Make' at line 18 through an owning parameter, which hands its release to the callee; remove this dispose

The arguments a variadic routine takes past its parameters have no row of their own, so the routine’s row says once for all of them what it keeps: variadic_keeps, read as keeps is read on a parameter. false promises that the routine keeps none of them beyond the call, as a printf-class routine that formats its arguments and forgets them; true says that it may keep them. A row that states nothing may keep them, so a text handed there counts as kept, and a store through a pointer keeps the text it replaces wherever that text may still be read. The standard library’s WriteLn, Write, WriteErrLn, WriteErr, ReadLn and Read state false, and a row that states the word without "variadic": true is refused when the contract loads.

{"language_name": "Report", "external_name_utf8": "report", "external_name_utf32": "report",
 "effects": {"never_calls_back": true},
 "parameters": [{"name": "format", "type": {"kind": "builtin", "name": "string"}, "passing_mode": "call_by_value", "keeps": false}],
 "return_type": {"kind": "void"}, "variadic": true, "variadic_keeps": false, "format_string": "write",
 "description": "prints a formatted line and keeps nothing it prints"}

A call of such a routine reaches the program’s storage only through a pointer it is handed: it never calls back and keeps nothing, so a for ... in walk whose body prints its texts with it lends nothing, and a task that prints a text with it keeps its own stores to itself.

A text parameter’s "keeps": false beside never_calls_back is the same promise, and since release 7.8.1 a store through a pointer relies on it as it relies on variadic_keeps: the store gives back the text the call was handed. A routine whose row states either word falsely and keeps the text reads another text, or freed bytes, at its next call.

One routine under several names

A C library may publish one routine under several names, and a contract says so with one entry per name on the same linked symbol. Every entry on a symbol states that routine word for word: the same external names, the same parameters with their names and passing modes, the same result and every other field, with only language_name and description its own. One word may differ, a parameter’s keeps and a variadic routine’s variadic_keeps alike: a second name for a routine of the standard library may leave out the word where the standard library’s row states it, since a row without the word promises nothing, and a call through that name counts as one the routine may keep from. It may not state a word the standard library’s row leaves out, nor state the opposite: the standard library’s row is the authority for its routines. Two entries of a routine the standard library does not state agree where one leaves the word out and the other says it keeps nothing. A program imports any of the names, and a call through each one reaches the one C function, called with the one signature the symbol carries. The entries may stand in one contract file or in several files that describe the library, a contract that extends a library the compiler ships among them.

{"language_name": "Twice", "external_name_utf8": "alias_twice", "external_name_utf32": "alias_twice",
 "parameters": [{"name": "value", "type": {"kind": "builtin", "name": "int64"}, "passing_mode": "call_by_value"}],
 "return_type": {"kind": "builtin", "name": "int64"}, "variadic": false, "format_string": "none",
 "description": "Doubles a 64-bit integer"},
{"language_name": "Doubled", "external_name_utf8": "alias_twice", "external_name_utf32": "alias_twice",
 "parameters": [{"name": "value", "type": {"kind": "builtin", "name": "int64"}, "passing_mode": "call_by_value"}],
 "return_type": {"kind": "builtin", "name": "int64"}, "variadic": false, "format_string": "none",
 "description": "The same routine under a second name"}

Two entries on one symbol that state different routines are refused when the contract loads, as a C compiler refuses two declarations of one function with conflicting types, and so are entries of two libraries on one symbol, because a symbol belongs to the library whose link line provides it. The refusal names the symbol, both entries, their libraries and the file each entry stands in. Here a second file of the library states the symbol with a 32-bit parameter and result:

Failed to resolve imported libraries: interop error 467: the linked symbol 'alias_twice' is stated as 'Twice' of library 'aliaslib' in 'twice.external' and as 'Double' of library 'aliaslib' in 'double.external': a linked symbol is one routine of one library, so every entry on it states that routine and differs only in its name and description

Two more fields appear only in contracts the compiler writes, never in one you author: effects on a Mica-borne entry records what the exporting unit’s transitive effect walk proved about the body (an empty object is the walked-and-clean statement), and intrinsic names an entry the compiler realizes inline at each call site — the embedded standard-library surface’s own bookkeeping.

Callback slots

A C library that takes a function pointer declares that parameter a callback slot: the parameter itself carries the void-pointer type — the pointer-sized code address is what actually crosses — and the slot beside it states everything the compiler holds a binding to:

{
  "name": "visit",
  "type": {"kind": "pointer", "to": {"kind": "void"}},
  "passing_mode": "call_by_value",
  "callback": {
    "class": "during_call",
    "parameters": [
      {"name": "context", "type": {"kind": "pointer", "to": {"kind": "void"}},
       "passing_mode": "call_by_value"},
      {"name": "value", "type": {"kind": "builtin", "name": "int64"},
       "passing_mode": "call_by_value"}
    ],
    "return_type": {"kind": "builtin", "name": "int64"},
    "userdata_index": 2
  }
}
FieldValuesMeaning
classduring_call · registered · signalwhen the library fires the bound callback: during_call only inside the imported call itself, so the caller’s frame outlives every firing; registered stores the callback and fires it after the call returns — served by a library built with --tasking multicore, each firing thread a Mica task of its own, and refused by name in a single-carrier build; signal installs the callback as a signal handler, which the kernel runs between any two instructions, so only a heapless callback binds there — see signal handlers
parameters / return_typetype referencesthe C shape a bound callback declaration must match, position for position; a slot position declared void-pointer admits any Mica pointer, which is how the callback declares the pointer to its own context record
userdata_indexzero-based ordinalthe sibling parameter carrying the userdata pointer the library passes through unchanged; omitted when the API declares none

The only thing that may stand at a callback slot is the name of a Mica callback declaration — the third declared subprogram kind beside task and generator. A callback is never called from Mica and never a value; the library behind the slot performs every firing, and the callback’s whole environment is the explicit context record its userdata pointer carries plus the unit’s own root storage.

The errors block

The errors block is what turns a C failure idiom into Mica’s failure channel: the compiler synthesizes the check the convention describes and lifts the failure, so the caller handles it with on fail like any Mica failure — no -1 tests in user code, ever.

conventionThe C idiom it lifts
negative_errnoa negative return announces failure — most of POSIX: read, write, open, the socket family
null_errnoa null pointer return announces failure — getcwd, the fopen class
sentinel:<value>a stated sentinel value announces failure — mmap’s all-ones is the classic
status_codethe returned status is itself the reason, as sqlite’s result codes are: a status outside success fails the call with the domain’s code for it

The first three read the reason from errno. status_code reads none: the block states success, the statuses a call answers when it succeeded, each the call’s plain result, and every other status fails the call with the code whose value it is, or with the domain’s first code where no code states it.

"errors": {"convention": "status_code", "success": [100, 101], "domain": "SqliteCode"}

domain names the Mica error domain the lifted code lands in (posix.OsError above). A convention tests the value a function returns, so a function that returns nothing cannot state one. A language: "mica" entry never carries the block: a Mica-borne callee speaks the failure channel natively, with nothing to lift.

Constants

{"name": "InotifyCloexec", "type": {"kind": "builtin", "name": "int32"}, "value": 524288}

A constant carries name, type, and value — or values keyed per target architecture for the rare constant whose value depends on it, or encodings keyed utf-32 and utf-8 for the few facts that follow the build’s string encoding, which is how std states CodeUnitBytes. Exactly one of the three forms is present. Enum literals do not repeat here; declaring the enum type already imports its literals.

{"name": "Pi", "type": {"kind": "builtin", "name": "float64"}, "value": 3.141592653589793,
 "exact": "157079632679489661923/50000000000000000000"}

A real constant written without a type, such as math’s exp Pi = 3.14159265358979323846;, carries exact beside value: the value as written, as a rational p/q or a bare integer, or -0 for a negative zero. value is that value rounded to float64. A program that imports the constant rounds exact once to the type it meets, so it reads the value the library’s own code reads. exact stands beside value only, and the two agree: exact rounded to the constant’s type is value. The compiler writes the field under --emit-contract; a contract that breaks one of these rules is refused when it loads, naming the constant.

{"name": "Row", "type": {"kind": "named", "name": "Pair"}, "value": [1.5, 2.5]}

A typed array constant carries its elements’ values as a list, in index order, under its array type, which the library exports beside it: exp Row : Pair = (1.5, 2.5); is published with exp Pair = array[0..1] of float64;. A program that imports Row reads it whole or element by element, as the library’s own code does. A list of another length is refused when the contract loads, naming the constant. A float16 or bfloat16 constant carries its value as a number, which the program rounds once to its type.

{"name": "Order", "type": {"kind": "named", "name": "Palette"}, "value": [2, 0, 1]}

A table of truth values carries true and false, and a table of an enumeration’s members carries each member’s ordinal: exp Order : Palette = (Blue, Red, Green); over exp Color = (Red, Green, Blue); is published as above. A contract written by hand may name the members instead, ["Blue", "Red", "Green"], and a name the enumeration does not declare is refused when the contract loads.

{"name": "Names", "type": {"kind": "named", "name": "NameTable"}, "value": ["alpha", "", "grün", "gamma"]}

A typed string constant carries its text as a JSON string: exp Motto : string = "carpe diem"; is published with "value": "carpe diem". A typed array constant of strings carries the text of each element in index order, a join already computed: exp Names : NameTable = ("alpha", "", "grün", Stem + "ma"); is published with "gamma" as its last element. A typed constant of a wide integer type carries its value as decimal text: exp Radix : Big = 18446744073709551616; over exp Big = uint[128]; is published with "value": "18446744073709551616".

Mica-borne libraries: generic templates

A contract with language: "mica" may carry generic_templates — the gen blocks of a Mica library, travelling as source because generics instantiate in the consuming compilation. Each entry names the template, its kind (type or function), its type parameters with their constraints, and the imports its body needs. You do not author these: the compiler emits them with the rest of the contract under --emit-contract.

An exported generator travels the same way, and so does every generator or template of the library that an exported one names. Because such a body is compiled in the program that imports it, what it names must reach that program with it:

A copied body may nameA copied body may not name
the library’s imports and its exported functions, procedures, types and constantsa private variable of the library: storage never crosses, so the body reads and writes it through a private routine
the library’s generators and generic templates, exported or nota private type in the signature of an exported generator or template, because every caller names the types a signature spells
a private function or procedure, and a private type with its literals or error codes, which the contract lists hidden
a private constant, whose text travels beside the body
its own parameters and locals, a local shadowing a private name included

A hidden entry carries "hidden": true and belongs to the library alone: the importing compilation imports it inside the copied body and nowhere else, and no import a program writes selects it. A private routine the body calls keeps its code in the library’s archive, linked by its symbol, and a private type brings the private types its own shape names.

A body that names a declaration from the right-hand column is refused when the library compiles, at the name, rather than in every program that imports it:

analyzer error 5557: the variable 'base' is private to this library, but the body of 'Counting' is compiled in every unit that imports 'Counting', where no private name of this library exists: read and write it through a private function or procedure, which crosses hidden with the body
analyzer error 5612: the type 'Fault' is private to this library, but the signature of 'Checked' is compiled in every unit that imports 'Checked', where every caller names the types it spells: export it with 'exp'

An object compiled beside a program follows the same rule. An ordinary exported function is compiled in its library and may name anything there.

Routines over tracked values

A library keeps a second body of every routine it exports that works on tracked values: the recording copy, whose body records onto the tape of the program that calls it. The copy is a hidden entry of its own, and the routine’s entry names it in recording:

{"language_name": "Logistic", "external_name_utf32": "Neural__Logistic",
 "parameters": [{"name": "z", "passing_mode": "call_by_value",
   "type": {"kind": "tracked", "element": {"kind": "builtin", "name": "float64"}}}],
 "return_type": {"kind": "tracked", "element": {"kind": "builtin", "name": "float64"}},
 "recording": "Logistic__tape", ...}
{"language_name": "Logistic__tape", "external_name_utf32": "Neural__Logistic__tape",
 "hidden": true, ...}

Outside every window a program calls the routine and computes plainly; a call inside a tape window runs the copy, which the compiler imports beside the routine’s own import, so what the routine records joins the program’s tape. A contract that names a copy without listing it is refused at that call, because the call could only record nothing:

analyzer error 5632: 'Logistic' records onto the tape through a recording copy, and library 'Neural' names the copy 'Logistic__tape' without listing it, so a call inside a window would record nothing — rebuild the library with this compiler, which publishes the copy beside the routine

Reading order for a new contract

Author top-down, the order the compiler resolves: the library entry and its link block first (the import now resolves and links), then the types the boundary needs, then functions one at a time — each one testable as soon as it is declared — and the errors block on every fallible entry before the function is called in anger. The MicaCallsC worked instance follows exactly this order, and every field this page documents appears in it, in the compiler’s shipped contracts, or in both.