The heap article had one pointer per cell, and it was always the owner. Real programs are not so tidy: a routine needs to look at a cell it must not free, a cursor structure wants a window into data that outlives it, a tree wants parent pointers. Each of them holds a pointer to a cell, and what matters is whether that pointer owns the cell or merely sees it: the owner releases the cell once, and no one else may. Mica makes the difference one word:
n : pointer int64 { owns: carries the release obligation }
n : borrowed pointer int64 { borrows: uses the cell, keeps nothing }— and then checks the word.
The example is
examples/Ownership.
Build and run it:
make -C mica-container/examples/Ownership runLend a cell, keep the obligation
procedure Inspect(n : borrowed pointer int64);
begin
WriteLn(" borrowed and read: %lld", value n);
end;
procedure Touch(n : borrowed pointer int64);
begin
value n := value n + 1;
end;cell := new int64;
value cell := 41;
Inspect(cell);
Touch(cell);
WriteLn(" after the borrowing write: %lld", value cell);
...
dispose cell; borrowed and read: 41
after the borrowing write: 42The caller lends with no ceremony — passing an owning pointer where a borrowed one
is expected is the lend — and the callee’s signature promises it will keep
nothing. Note what Touch did: a borrow can write. Borrowing is about
ownership, not mutation; the borrower may change the cell. What it may not do is
release it or re-home it, and those are refusals, not conventions.
Try to release:
procedure Rogue(n : borrowed pointer int64);
begin
dispose n;
end;analyzer error 5200: dispose statement cannot release a 'borrowed pointer'
value of data type 'borrowed pointer int64': a borrow is owned elsewhere (a
container, the program, or another scope), which releases it — dispose the
owning pointer insteadTry to smuggle the borrow into an owning slot:
k.b := n; { k.b is a plain 'pointer int64' field }analyzer error 5199: cannot store a 'borrowed pointer' value into the owning
pointer field or element 'k': an embedded pointer is owned by
construction and its owner's drain would release the borrowed cell — mark the
field 'borrowed pointer' to borrow, or store an owned pointerThese two refusals are what make borrowed a checked claim. The
obligation ledger never has to wonder whether a
callee took the debt: the parameter type already answered, and the compiler
enforced the answer inside the callee’s own body.
A view structure holds a window, not a duty
The word works in fields too:
type
Window = record
tag : int64;
seen : borrowed pointer int64;
end;w.seen := cell;
WriteLn(" window %lld sees %lld", w.tag, value (w.seen)); window 7 sees 42A cursor, an iterator’s position, a cache entry pointing at data owned by the
store — view structures are everywhere, and this is their honest type. The
5199 message above showed the other half: the same store into a field not
marked borrowed is refused, because a record’s drain frees its owning fields
and this record does not own the cell. Which duties a structure’s death
discharges is written in its field types, one by one.
(One spelling note: dereferencing a pointer field parenthesizes — value (w.seen) — so the value binds to the field, not the record.)
The back edge: parents without a cycle of owners
Here is the payoff the heap article promised. Its rule was that owning pointers must form a tree — the drain recurses owning fields leaves-first, so a cycle of owners is a misuse. But real structures are cyclic: children point at parents, list nodes point back, graph nodes interlink. The resolution is the same one word:
type
Node = record
v : int64;
next : pointer Node; { owns downward }
parent : borrowed pointer Node; { sees upward }
end;root := new Node;
root.v := 1;
root.next := new Node;
root.next.v := 2;
root.next.parent := root;
WriteLn(" child %lld sees parent %lld", root.next.v, root.next.parent.v);
dispose root; child 2 sees parent 1
one dispose drained the chain, parents includedThe data structure is cyclic — child and parent reach each other. The
owning subgraph is a tree — only next owns. And dispose root reclaimed
the entire chain in one statement, because the typed drain follows owning
fields and skips borrowed ones: the field types told it which arrows are
ownership and which are sight.
This is the idiom that replaces both C’s “remember not to free the parent pointer” and reference counting’s cycle problem. There is nothing to remember; the type remembers.
Stack cells lend the same way
Borrowing is not a heap-only idea. The out-parameter idiom was a lend all along:
local := 5;
Bump(address local); local after Bump(address local): 15A plain pointer parameter is a borrow by construction — the callee uses the
cell and keeps nothing — and the escape rules refuse the versions that would
keep it: returning address local was already a
refusal in the first article of this section, and storing a lent pointer into
state that outlives the call is refused by the same discipline.
One place, one name per call
Inside a routine, two parameter names are two places. Whoever writes the body
of a routine with the parameters first and second reasons about two
counters, and whoever reads the body later does the same. Mica holds every
call to that reading: a call may not reach one place through two of its
arguments when the parameter receiving either of them can write that place.
The program below makes each call this section talks about, every one in a form the compiler accepts:
{
CallRule - calls that reach every place through one argument, each in the form the compiler accepts.
Inside a routine two parameter names are two places, so a call hands a place a parameter can write to that parameter
alone. 'Twice' gets two counters, 'Add' a counter beside a copy of its own hits, 'AppendAll' a list beside a copy of
it, 'Refill' a view of another box's counter, 'Sum' two views that write nothing, 'Swap' two elements chosen by two
different constants, and 'SwapAt' one row with the two indices it selects itself.
}
program CallRule;
imp
WriteLn, Append, Length : std;
AppendAll, List : arrays;
const
TwiceLine = "Twice added 3 twice over: %lld";
AddLine = "the counter gained its own hits: %lld";
ListLine = "the list grew by a copy of itself to %lld numbers";
RefillLine = "the box was refilled from the spare's view: %lld";
SumLine = "two views of one cell, read together: %lld";
RowLine = "the row reads %lld %lld %lld";
StartHits = 3;
BoxHits = 5;
SpareHits = 8;
type
{ A count of hits. }
Counter = record
hits : int64;
end;
{ A box that owns one counter on the heap. }
Box = record
cell : pointer Counter;
end;
{ Three readings in a fixed array. }
Row = array[1..3] of int64;
{ A list of numbers that grows as the program runs. }
Numbers = List of int64;
{ The hits of 'first' are added to 'second' twice, and 'first' is read again for the second time. }
procedure Twice(first, second : pointer Counter);
begin
second.hits := second.hits + first.hits;
second.hits := second.hits + first.hits;
end;
{ The counter gains 'hits', a copy its caller took before the call. }
procedure Add(counter : pointer Counter, hits : int64);
begin
counter.hits := counter.hits + hits;
end;
{ The box's counter is released and replaced by a fresh one, counted on from what the view reads. }
procedure Refill(const view : borrowed pointer Counter, box : pointer Box);
var
old : pointer Counter;
begin
old := box.cell;
box.cell := nil;
dispose old;
box.cell := new Counter;
box.cell.hits := view.hits + 1;
end;
{ The hits of two counters, read through two views the routine writes nothing through. }
function Sum(const first : borrowed pointer Counter, const second : borrowed pointer Counter) : int64;
begin
Sum := first.hits + second.hits;
end;
{ Two numbers are exchanged through two pointers. }
procedure Swap(first, second : pointer int64);
var
kept : int64;
begin
kept := value first;
value first := value second;
value second := kept;
end;
{ Two readings of a row are exchanged, the row handed once and the two indices beside it. }
procedure SwapAt(row : pointer array[low..high : int64] of int64, first, second : int64);
var
kept : int64;
begin
kept := row[first];
row[first] := row[second];
row[second] := kept;
end;
{ Every call of the page, each reaching its places apart. }
procedure Run(first, last : int64);
var
a, b : Counter;
box, spare : Box;
list, twin : Numbers;
row : Row;
begin
a.hits := StartHits;
b.hits := 0;
Twice(address a, address b);
WriteLn(TwiceLine, b.hits);
Add(address a, a.hits);
WriteLn(AddLine, a.hits);
Append(address list, 1);
Append(address list, 2);
twin := list;
AppendAll(address list, address twin);
WriteLn(ListLine, Length(list));
box.cell := new Counter;
box.cell.hits := BoxHits;
spare.cell := new Counter;
spare.cell.hits := SpareHits;
Refill(spare.cell, address box);
WriteLn(RefillLine, box.cell.hits);
WriteLn(SumLine, Sum(box.cell, box.cell));
dispose box.cell;
dispose spare.cell;
row[1] := 4;
row[2] := 16;
row[3] := 9;
Swap(address row[1], address row[3]);
SwapAt(address row, first, last);
WriteLn(RowLine, row[1], row[2], row[3]);
end;
begin
Run(1, 2);
end.Saved as CallRule.mica, it runs with one command:
mica CallRule.micaTwice added 3 twice over: 6
the counter gained its own hits: 6
the list grew by a copy of itself to 4 numbers
the box was refilled from the spare's view: 9
two views of one cell, read together: 18
the row reads 16 9 4One counter handed in twice
Twice adds the hits of first to second, two times. Handed the counters
a and b, with a at 3, it leaves b at 6. Now hand it a for both
parameters. A reader of the body expects a to gain twice its 3 hits and end
at 9, but the first line writes the very place the second line reads again:
a becomes 6, then 12. Nothing in the body is wrong. The call broke the
promise the body was written under, so the compiler refuses the call:
Twice(address a, address a);analyzer error 5784: the call to 'Twice' reaches 'a' through parameter
'first', which can write it, and through parameter 'second', which can write
it, and a place one parameter can write reaches the routine through that
parameter alone: hand 'a' to one of the two, and give the other a place of
its ownThe same holds when the place arrives through a pointer parameter of the
calling routine: inside procedure Forward(c : pointer Counter), the call
Twice(c, c) is refused alike.
Which parameters count as writers is read from the routine’s heading alone. A
pointer parameter can write what it reaches, and so can a borrowed pointer
parameter, unless its heading says const. A plain value parameter receives a
copy taken before the call starts, and a copy never counts: Add(address a, a.hits) names a twice and compiles, because the routine only sees the
number a.hits held when the call began.
A list grown by itself
The standard library’s AppendAll grows the list behind its first parameter by
every element of the list behind its second, which it reads through a const
pointer. Handed one list for both, the writer grows the list while the reader
walks it:
AppendAll(address list, address list);analyzer error 5784: the call to 'AppendAll' reaches 'list' through
parameter 'xs', which can write it, and through parameter 'items', which
reads it, and a place one parameter can write reaches the routine through
that parameter alone: hand 'list' to one of the two, and give the other a
place of its ownThe way out the message names is a second place: copy the list first, and hand
the copy to the reader. twin := list copies every element, so
AppendAll(address list, address twin) grows list by what it held, as the
program above does.
A view beside the owner it was taken from
Refill releases the counter a box owns, puts a fresh one in its place and
counts on from what view reads. Handed a view of another box’s counter, as
Refill(spare.cell, address box) is, that works. Handed a view of the counter
the box itself owns, the read through view would land on the counter the
routine has just released:
Refill(box.cell, address box);analyzer error 5784: the call to 'Refill' reaches 'box' through parameter
'view', which reads a part of it, and through parameter 'box', which can
write it, and a place one parameter can write reaches the routine through
that parameter alone: hand 'box' to one of the two, and give the other a
place of its ownThe view is const, so it only reads, but the box beside it can write, and
that is enough. A borrowed pointer handed to a call beside its own owner is
refused for that reason.
Two views of one cell
Two views of one cell meet in a call too, because a borrowed pointer
parameter can write through what it borrows. SumViews only reads, but its
heading does not say so:
function SumViews(first, second : borrowed pointer Counter) : int64;
begin
SumViews := first.hits + second.hits;
end;
WriteLn(SumLine, SumViews(box.cell, box.cell));analyzer error 5784: the call to 'SumViews' reaches 'box.cell' through
parameter 'first', which can write it, and through parameter 'second', which
can write it, and a place one parameter can write reaches the routine
through that parameter alone: hand 'box.cell' to one of the two, and give
the other a place of its own, or write 'const' before the two borrowed
parameters if the routine only reads through themHere the message names a second way out, the one this routine wants. Sum in
the program above writes const before both of its views, so no parameter of
the call can write, and Sum(box.cell, box.cell) compiles.
Two elements of one array
Two elements of one array are two places when their indices are two different
constants, so Swap(address row[1], address row[3]) compiles. When an index
is computed as the program runs, the compiler cannot tell two elements from one
element named twice, and the call is refused with a code of its own:
Swap(address row[first], address row[last]);analyzer error 5785: the call to 'Swap' reaches two elements of 'row'
through parameter 'first', which can write one of them, and through
parameter 'second', which can write one of them, and their indices are not
two different constants, so the compiler cannot tell the two elements apart:
hand 'Swap' one 'address row' and the two indices, and let it select both
elementsThe way out is a routine that receives the array once, with the two indices
beside it, and selects both elements itself. SwapAt in the program above is
that routine: SwapAt(address row, first, last) hands one place to one
parameter, and inside the routine row[first] and row[second] are read and
written through one name, whatever the indices are.
The same word, under tasks
You have met borrowed once before, in
data-race freedom: inside a concurrent block, a bare
address of shared state is refused, and the legal spelling is a borrow
bound inside a synchronized statement, scoped to the mark that guards it.
Same word, same meaning — a view that cannot outlive what makes it safe —
with “safe” defined by the lock there and by the owner here. The two articles
describe one design.
What this does not do
A borrow does not extend a lifetime. It is a view, not a keeper: the owner’s release is not delayed because views exist. A borrow used after its cell’s owner released it is exactly the use-after-dispose story of the heap article — refused where provable, caught by the checked tier where not.
No reference counting is happening. Nothing counts the aliases. That is why they are free, and why they cannot keep a cell alive.
A borrow cannot be laundered into an owner. Not through a field (5199), not through a dispose (5200), not through a return — the borrow rides the type through every assignment.
Sharing between tasks is not this article. A borrow crossing task boundaries answers to the synchronized rules, which are stricter: there the view is scoped to one marked statement.
What the compiler proved
Two routines used a cell they provably could not free. A structure held a window whose type said “window”, and the store that would have made it an owner was refused. A cyclic data structure drained from one dispose because its owning subgraph was a tree by declaration. Every call handed each place it could write through one argument alone, and the calls that would have handed one place in twice were refused by name. And nothing on this page was a convention: every claim was a type, and every violation was quoted from a compiler that refused it.
Next
What the compiler proves about your heap — the same machinery, seen from above: the full list of heap-bug classes and, for each, whether it is refused or trapped — the claims this series has been cashing one article at a time.