Ownership roles

View source

What erg, abs, ins and dat mean, what the compiler refuses, and why the roles make data flow easy to read.

Every value in a program has to be somewhere, and someone has to be responsible for it. Actus makes you write down who. The word you write is a role. There are four of them: erg, abs, ins and dat.

RoleShort nameWhat it means
ergownerYou own the value. You may read it, change it and hand it on.
absviewYou may only look at the value. The owner keeps it.
insloanYou may change the value during this call. The owner gets it back.
dattransferThe value is handed over for good. The old owner cannot use it again. A number or a Bool is copied.

You have already seen all four. This page goes through them one at a time, with the mistakes the compiler catches for each one.

Roles appear in two places: where a variable or parameter is declared (abs value: Int), and where a value is passed in a call (show(value: abs score)). The declaration says what the verb will do. The call says what the caller allows. The two must agree.

erg: you own it

erg makes an owner. A variable made with erg can be read, changed and passed on. You have used it since Variables and values. A parameter can be erg too. The verb then gets a value of its own to use and change. In the next program, add_one changes its own value, and for a plain Int the caller's n is not touched:

actus
verb add_one(erg value: Int) -> Int {
    value += 1;
    return value;
}

verb main() {
    erg n = 1;
    erg bigger = add_one(value: erg n);
    println(int: n);
    println(int: bigger);
}
bash
actus run
text
1
2
process exited with status 0

The argument can also be a calculation, as in add_one(value: erg n + 1).

abs: look but do not touch

abs gives read-only access. Use it for every parameter that a verb only needs to read. Then the caller can see from the call that its value is safe:

actus
verb main() {
    erg score = 10;
    show(value: abs score);
    score += 5;
    show(value: abs score);
}

verb show(abs value: Int) {
    println(int: value);
}
bash
actus run
text
10
15
process exited with status 0

show reads the value twice, and score is still ours afterwards. If a verb tries to change something it only has a view of, the compiler refuses:

actus
verb main() {
    erg n = 1;
    change(value: abs n);
}

verb change(abs value: Int) {
    value += 1;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1051] at 7:5: cannot mutate read-only binding `value`

The mistake is in change: it said abs, so it may not write. The fix is to declare what you really mean. If the verb should change the value, it takes ins.

Views freeze the owner

You can also make a view in a variable with ref: abs view = ref n;. While a view of n exists, n is frozen. Nobody can change it, because the view would then see a value that moved under it:

actus
verb main() {
    erg n = 1;
    abs view = ref n;
    n += 1;
    println(int: view);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1011] at 4:5: cannot assign to frozen binding `n`: it is borrowed by a live `abs` view and cannot change until the view ends

The message names what you tried (here, assigning to n) and why it is not allowed. The view view comes from ref n, so n += 1; is not allowed while view is in scope. If you try to move n instead, for example with erg other = n;, the message is the shorter cannot move frozen binding.

A view cannot outlive its owner

A view only makes sense while the value it looks at exists. A verb cannot return a view of its own local variable:

actus
verb main() {
    println(int: make());
}

verb make() -> Int {
    erg local = 5;
    abs view = ref local;
    return view;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1018] at 8:12: borrow `view` cannot escape its scope

ins: borrow it to change it

ins lends a value to a verb so that the verb can change it. When the call ends, the value is back with its owner, and it keeps the change:

actus
verb main() {
    erg n = 1;
    bump(value: ins n);
    bump(value: ins n);
    println(int: n);
}

verb bump(ins value: Int) {
    value += 1;
}
bash
actus run
text
3
process exited with status 0

bump added 1 twice, and n is 3. The caller writes ins n to say: yes, I allow this call to change n. A verb can take more than one loan:

actus
verb main() {
    erg a = 1;
    erg b = 2;
    bump_both(first: ins a, second: ins b);
    println(int: a);
    println(int: b);
}

verb bump_both(ins first: Int, ins second: Int) {
    first += 1;
    second += 1;
}
bash
actus run
text
2
3
process exited with status 0

Only one loan at a time

A loan is exclusive. While a verb may change a, nothing else may look at a or change it. This is the rule that stops many bugs. Passing the same variable as two loans is refused:

actus
verb main() {
    erg a = 1;
    bump_both(first: ins a, second: ins a);
}

verb bump_both(ins first: Int, ins second: Int) {
    first += 1;
    second += 1;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1065] at 3:41: exclusive loan aliases resource `a` more than once

The same holds for a loan together with a view of the same variable. The verb could read amount after changing target, and get a surprise:

actus
verb main() {
    erg n = 1;
    add_to(target: ins n, amount: abs n);
}

verb add_to(ins target: Int, abs amount: Int) {
    target += amount;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1065] at 3:39: exclusive loan aliases resource `n` more than once

The call must match the declaration

The role in the call has to match the role in the declaration. If a verb asks for ins, abs is not enough, and the other way around:

actus
verb main() {
    erg n = 1;
    bump(value: abs n);
}

verb bump(ins value: Int) {
    value += 1;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1016] at 3:21: argument does not satisfy role of `value` in `bump`

Promising too much or too little are both errors. If you get argument does not satisfy role, compare the role at the call with the role in the declaration.

dat: hand it over

dat moves a value to the verb. From then on the verb owns it, and the caller must not use it again:

actus
struct Counter {
    count: Int,
}

verb main() {
    erg counter: Counter = Counter {
        count: 7,
    };
    erg total = finish(counter: dat counter);
    println(int: total);
}

verb finish(dat counter: Counter) -> Int {
    return counter.count;
}
bash
actus run
text
7
process exited with status 0

finish takes the counter and returns its count. It is the last use of counter in main. If main uses it again, the compiler stops it:

actus
struct Counter {
    count: Int,
}

verb main() {
    erg counter: Counter = Counter {
        count: 7,
    };
    erg total = finish(counter: dat counter);
    println(int: total);
    println(int: counter.count);
}

verb finish(dat counter: Counter) -> Int {
    return counter.count;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1005] at 11:18: use of moved binding `counter`

You will recognise this message. It is the same one you got in Variables and values when a String was moved with erg second = first;. A move is a move, whether it is an assignment or a dat call.

A number is different. It is a simple value, and dat hands over a copy of it, so the caller can still use its own. The argument can be a calculation too:

actus
verb double(dat value: Int) -> Int {
    return value * 2;
}

verb main() {
    erg n = 4;
    println(int: double(value: dat n));
    println(int: double(value: dat n + 1));
    println(int: n);
}
bash
actus run
text
8
10
4
process exited with status 0

This holds for the same types that erg second = first; copies. A struct moves when you pass it with dat, as Counter did above.

Use dat when a verb should finish with a value for good, such as closing a file or turning one value into another. If the verb only reads or changes it for a moment, use abs or ins.

Reading data flow from the calls

Now look at the calls in one program. You can tell what happens to counter without reading the verbs:

actus
struct Counter {
    count: Int,
}

verb show(abs counter: Counter) {
    println(int: counter.count);
}

verb bump(ins counter: Counter) {
    counter.count += 1;
}

verb finish(dat counter: Counter) -> Int {
    return counter.count;
}

verb main() {
    erg counter: Counter = Counter {
        count: 1,
    };
    show(counter: abs counter);
    bump(counter: ins counter);
    show(counter: abs counter);
    erg last = finish(counter: dat counter);
    println(int: last);
}
bash
actus run
text
1
2
2
process exited with status 0

In main:

  • show(counter: abs counter) only reads it.
  • bump(counter: ins counter) changes it, and it comes back.
  • finish(counter: dat counter) takes it. It is the last line that uses it.

Each call line tells you, on its own, whether the value is only read, changed or consumed. You do not have to open the verbs to know. This is the reason for the roles. When you read a program, you can follow the life of a value line by line. When the program changes, the compiler checks again that every change still follows the rules. A change that breaks one of these promises is found at compile time, not in a running program.

What the compiler refuses

These are the errors from this page. Each is a promise that was broken:

MessageWhat happened
cannot mutate read-only bindingA change through abs.
cannot assign to frozen binding, cannot move frozen bindingA change to a value, or a move of it, while a view of it exists.
exclusive loan aliases resource ... more than onceTwo loans, or a loan and a view, of the same variable.
argument does not satisfy role of ...The role in the call does not match the declaration.
use of moved bindingA value used after it was moved.
borrow ... cannot escape its scopeA view returned from the place its value lives in.

What you learned

  • erg owns a value. abs only looks. ins borrows to change and gives it back. dat hands the value over for good.
  • The role is written in the declaration and again in the call, and they must match.
  • While a view or a loan exists, the owner cannot be changed in other ways.
  • After a dat call or an assignment, the old name cannot be used when the value is a struct or a String. A number is copied, and the old name keeps working.
  • The roles make the flow of data readable from the calls alone.

Next: Errors: Result and Option.