<!-- SPDX-License-Identifier: CC-BY-4.0 -->
<!-- seo-title: Graded Types Explained with HoldFast Examples -->
<!-- description: Learn how graded types track usage, from linear and affine types to exact counts, upper bounds, and graded modal types. Replay HoldFast examples with Rocq. -->

# 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.

[Replay the graded example](#grade-replay) ·
[Try length-indexed vectors in the browser](../?example=vectors#playground)

<a id="linear-and-affine"></a>

## 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](https://pureportal.strath.ac.uk/en/publications/the-syntax-and-semantics-of-quantitative-type-theory/).

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](../borrow-checking/).
[What a proof covers](../formal-verification/#compiled-artifacts).

<a id="natural-grades"></a>

## 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.

```hf
public definition Nat ScalePublishedBound(Nat n @ NatBound(4)) =>
    ((Nat x @ NatBound(2)) => x)(n + n);
```

[Bounded usage source fixture](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/fixtures/graded-bounded.hf).
[Grade domain rules](../extensions/graded-foundation/#dimensions-and-lawful-domains).

<a id="dependent-grade"></a>

## A vector length used only in types

`Specialize` applies `PreserveLength` at `n + n`, the returned function's
input and output vector length.

```hf
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.

<a id="graded-modal-types"></a>

## 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](https://kar.kent.ac.uk/74450/).

```hf
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](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/fixtures/graded-modal.hf).
[Graded box replay](../tooling/elaboration-prototype/#graded-box-source-replay).

<a id="grade-replay"></a>

## 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.

```shell
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](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/fixtures/graded-dependent.hf).

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](../tooling/elaboration-prototype/#default-grade-checking).

<a id="browser-exercise"></a>

## 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](../?example=vectors#playground)
[How dependent types encode vector bounds](../dependent-types/).
