The heart of the Gallina language is **`Inductive`**, which declares data types. From booleans and natural numbers to lists, syntax trees, and even logical connectives like ‘and (∧)’ and ‘or (∨)’, nearly everything inside Rocq is defined with Inductive.
Start the payment example in DataTypes.v. Add the record and Print examples to the same file in order, and use rocq compile DataTypes.v after each section to compare the new results.
1. Enumerating Cases with Inductive
We can use Inductive to list a fixed set of choices like days of the week, or to build a sum type like a payment status where each case carries its own data (transaction ID and amount, or an error code).
Each name separated by | under Inductive payment_state : Type := is a constructor that builds one payment state. To compute the settled amount, match s with inspects the state because an actual payment amount exists only in the completed (Paid) branch.1
A payment status is one of three branches: pending with no extra payload (Pending), completed with two natural numbers for transaction ID and amount (Paid), or failed with one natural number for the error code (Failed).
In the formula below, the square cup symbol (disjoint union) unites the cases while tagging which constructor branch each value came from, denotes the payload-free Pending branch, and denotes the ordered pair of natural numbers (tx_id, amount).
The function must return a natural number in every branch. Only the completed branch carries an amount to extract; for pending and failed payments, no amount has been settled, so we return 0.2
In Paid 901 15000, Paid takes an ID and an amount and builds a payment-state value. In the pattern Paid _ amount => amount, the same name tells Rocq how to take the amount out of a value built that way.
Inductive payment_state : Type :=
| Pending
| Paid (tx_id : nat) (amount : nat)
| Failed (error_code : nat).
Definition settled_amount (s : payment_state) : nat :=
match s with
| Pending => 0
| Paid _ amount => amount
| Failed _ => 0
end.
Compute settled_amount Pending.
Compute settled_amount (Paid 901 15000).
Compute settled_amount (Failed 403).Output:
= 0 : nat
= 15000 : nat
= 0 : natRocq 9.2 reports an abstract-large-number warning on the line with 15000. It reads the large natural number using Nat.of_num_uint instead of a long constructor chain to avoid a stack overflow. The calculation still gives the output above.
Each of the three output lines comes from one branch of `match`. First, settled_amount Pending takes the Pending => 0 arm and outputs = 0 : nat; on the second line, settled_amount (Paid 901 15000) takes the Paid _ amount => amount arm, skips the transaction ID 901 with an underscore (_), and extracts the second argument 15000 to return = 15000 : nat.
Third, settled_amount (Failed 403) likewise ignores the error code 403 with an underscore and outputs = 0 : nat. Pattern matching in Rocq is written match val with | pattern => result ... end and must always be closed with end. Omitting even one of the three constructors halts compilation immediately with a Non exhaustive pattern-matching error.
2. Bundling Multiple Fields Together with Record
A bank account holds an account ID and a balance together. When multiple fields belong in a single value, Record is the right fit: mkAccount names the constructor that builds the value, and the curly braces declare each field’s name and type.
Because Rocq automatically generates projection functions for every field, deposit can copy the original account ID and add the deposit delta to the balance to construct an updated account value.3
You could also build the first account with mkAccount 1 5000. The {| ... |} notation used for alice fills the same fields by name. With acc_id := 1 beside the number, you can see straight away which natural number is the ID and which is the balance.
Record account : Type := mkAccount {
acc_id : nat;
balance : nat
}.
Definition alice : account := {| acc_id := 1; balance := 5000 |}.
Definition deposit (a : account) (delta : nat) : account :=
{| acc_id := a.(acc_id); balance := a.(balance) + delta |}.
Compute (deposit alice 2500).(balance).Output:
= 7500 : natHere is how the output = 7500 : nat was computed. Record literals are written inside {| field := val; ... |} brackets, and individual fields are read via a.(balance) or balance a.
Calling deposit alice 2500 keeps alice’s account ID 1 unchanged and adds 2500 to the existing balance 5000 to return the new record {| acc_id := 1; balance := 7500 |}, and the trailing .(balance) projects the balance field out of that new record to print = 7500 : nat.
In contrast to a sum type, an account is a product requiring both values at once. Writing for the account ID and for the existing balance, depositing units looks like this:
Because the new record preserves the account ID and stores the updated balance in the second component, (deposit alice 2500).(balance) projects the second element of the pair. Using field names documents in code which component is being read.4
3. Inspecting Standard bool and nat Under the Hood with Print
We can inspect the standard library definitions of bool and nat using Print:
Print bool.
Print nat.Output:
Inductive bool : Set := true : bool | false : bool.
Inductive nat : Set := O : nat | S : nat -> nat.
Arguments S _%_nat_scopeThe final Arguments line reports the notation setting for the argument to S. The constructor’s type is S : nat -> nat in the definition above it.
In Arguments S _%_nat_scope, _ stands for the single unnamed argument, and the suffix gives the notation scope to use there. For example, the multiplication inside S (2 * 3) is read over the natural numbers, and S produces the successor of 6, which is 7.
Written purely with constructors, the natural number 3 is S (S (S O)); decimal numerals are shorthand notation for those nested constructors. In Part 4, we will add a type parameter to Inductive to work with polymorphic option and list types.
Writing in a formula unfolds the constructor structure of 3, and S : nat -> nat in the output of Print nat. shows the type of the constructor that builds the next natural number from a given one.
At the same time, Set appearing as the sort of bool and nat is the lowest universe where computational data types live. When you write Type without an explicit universe level as in the earlier sections, Rocq infers the level needed, in fact, running Check account. after Record account : Type prints account : Set.5
Footnotes
- The Rocq Prover Reference Manual 9.2. Inductive types and recursive functions. Explains
Inductivedeclarations that introduce a new type by listing its constructors. ↩ - The Rocq Prover Reference Manual 9.2. Inductive Types: Simple Inductive Types · Inductive Types: The match … with … end Construction. Explains constructing inductive values and branching on their constructors with
match. ↩ - The Rocq Prover Reference Manual 9.2. Record types. Explains how
Recorddefines field projection functions. ↩ - The Rocq Prover Reference Manual 9.2. Record Types: Constructing Records · Record Types: Accessing Fields. Covers record construction syntax and field projection notation. ↩
- Rocq Core Library 9.2. Corelib.Init.Datatypes · The Rocq Prover Reference Manual 9.2. Sorts. Shows the constructor definitions of
boolandnatand the relationship amongProp,Set, and theTypeuniverse hierarchy. ↩