Graded types and usage tracking
Graded types constrain input usage, such as exactly once or at most twice. HoldFast tracks computation and type uses separately, so a vector length can determine a result type without being read by the computation.
How do graded types relate to linear and affine types?
Linear usage requires one use. Affine usage allows at most one. Natural grades generalize these to any exact count or upper bound. Quantitative type theory extends this to dependent types, where later parameter and result types can refer to earlier parameters. Robert Atkey's Quantitative Type Theory paper.
Grades count demand in a checked derivation. Ownership and loans govern memory access. Executable costs need evidence at the compiled representation. How borrow checking controls access. What a proof covers.
Exact and bounded natural grades
| Annotation | Computation allowance |
|---|---|
@ NatExact(0) |
Zero computation uses. Type uses are allowed. |
@ NatExact(1) |
Exactly one use (linear). |
@ NatBound(1) |
At most one use (affine). |
@ NatBound(2) |
Zero, one or two uses. |
A call scales the argument's demand by the parameter's declared grade.
Here n + n uses n twice. The lambda may use its parameter x up to twice,
so the checker charges the caller for up to four uses of n. The lambda
body uses x once, but the call is checked against its declared bound of two.
Changing NatBound(4) to NatBound(3) fails.
public definition Nat ScalePublishedBound(Nat n @ NatBound(4)) =>
((Nat x @ NatBound(2)) => x)(n + n);
Bounded usage source fixture. Grade domain rules.
A vector length used only in types
Specialize applies PreserveLength at n + n, the returned function's
input and output vector length.
public definition Fn(Vec<Nat, (n + n)> values @ NatExact(1)) -> Vec<Nat, (n + n)>
Specialize(Nat n @ NatExact(0)) => PreserveLength()(n + n);
The returned function uses values once. The checker counts four type uses of
n, two in each vector type. PreserveLength gives its first argument grade
zero, so n + n contributes zero computation uses.
What is a graded modal type?
A graded modal type attaches a usage rule to a wrapped value. Opening this
Box<Nat, 2> gives its natural payload exactly two computation uses. Other
grade domains can express other resource properties.
Orchard, Liepelt, and Eades on graded modal types in Granule.
record Box<T, grade q : NatExact>(T Value @ q);
OpenTwo opens the box once and returns a pair. The first component,
n + n, uses the payload twice in computation. The second is a function
whose input and output vector lengths depend on n.
Exact graded box source fixture.
Graded box replay.
Replay the grade checks with Rocq
From the repository root, run the commands with python on Windows or
python3 on Linux or in WSL. For n, the first command reports exactly
0 computation uses and 4 type uses. The second reports a computation bound
of 4 and 0 type uses.
python tools/run.py holdfast-parser check-grades tools/tests/fixtures/graded-dependent.hf Specialize
python tools/run.py holdfast-parser check-grades tools/tests/fixtures/graded-bounded.hf ScalePublishedBound --domain NatBound
In the PreserveLength lambda, change Nat size @ NatExact(0) to
Nat size @ NatExact(1) and recheck Specialize on your copy. The checker
follows the reference and rejects the grade because size has zero computation uses.
Dependent usage source fixture.
The native elaborator checks natural grades in supported pure definitions and
locked dependencies. Use check-grades to inspect how it counts uses. General
grade domains use separate research replays and qualification.
Graded syntax and interfaces the native elaborator checks.
Explore dependent indices in the browser
Open the indexed vector example and select Run in Console.
Second() returns 20. Enter Read(FZero<3>()) and run it with result
type Nat to see the mismatch between the supplied Fin<4> and required
Fin<3>. This exercise shows the vector length relationship that the native
Specialize example combines with grade checking.
Open the indexed vector exercise
How dependent types encode vector bounds.