In Part 1, we gave names to constants using Definition. Now we add parameters to define functions and see why Rocq separates ordinary non-recursive functions and recursive functions into two distinct keywords.
Put the first example in a new Functions.v and run rocq compile Functions.v. Append the factorial example to that file and compile it again to see the extra result at the end.
1. Functions with Definition and Currying
Function parameters are written in parentheses after the function name, such as (a b : nat), and function calls pass arguments separated by spaces without parentheses or commas. To fix some of the arguments in advance, simply pass only those first arguments. Below, we bind add 10 to the name add_ten so we can add 10 to any number we pass later.
In the definition, a and b are parameters. In the call add 3 5, 3 and 5 are the arguments. The : nat after (a b : nat) gives the result type, and the a + b after := says what to compute from the two inputs.
Definition double (n : nat) : nat :=
n * 2.
Definition add (a b : nat) : nat :=
a + b.
Definition add_ten : nat -> nat :=
add 10.
Compute double 7.
Compute add 3 5.
Compute add_ten 25.
Check add.Output:
= 14 : nat
= 8 : nat
= 35 : nat
add : nat -> nat -> natFour lines appear in the output. The first line, = 14 : nat, is the result of double 7 evaluating 7 * 2; the second line, = 8 : nat, is add 3 5 evaluating 3 + 5; and the third line, = 35 : nat, passes 25 to add_ten (whose first argument was fixed at 10) to compute 10 + 25.
Fourth, notice that unlike Lean’s #check add (which displays the parameter signature first), Rocq’s Check add. directly prints the curried arrow type add : nat -> nat -> nat (nat -> (nat -> nat))!
A two-argument function is really a higher-order function that takes the first argument a and returns a function (nat -> nat) waiting for the second argument, which is why supplying one argument in add 10 immediately produces add_ten. Whenever you need a one-off anonymous function, use the fun ... => ... syntax as in fun x : nat => x + 1.
Fixing the first argument at 10 is written in mathematical notation as below. Here the Greek letter lambda () corresponds to fun in Rocq code, so reads “a function that takes the still-open second argument and returns .”
Because we want to supply the right-hand operand of the addition later, add 10 remains a function rather than a finished number. Every time add_ten receives a new input, it reuses that fixed first argument.1
fun x : nat => x + 1 takes a natural number x and adds 1. The input name and type go before =>, and the calculation goes after it. For a one-off function, put the whole expression in parentheses and call it directly: Compute (fun x : nat => x + 1) 5. gives 6.
2. Why Do Recursive Functions Use Fixpoint Instead of Definition?
The factorial function returns 1 at 0, and for larger numbers multiplies the current number by the factorial of its predecessor. We split these two cases with match n with … end: because S k is the constructor for the number one greater than k, the variable k inside that branch is the predecessor .
Because the body calls the function being defined, we declare it with Fixpoint, and Rocq verifies that the argument passed to the recursive call is structurally smaller.
Let denote factorial and denote the natural number immediately following . We assign one equation to each of the two constructors and :
Second, once is known, the value for is finished as well. If the right-hand side called again without shrinking its input, it would never get closer to the first line . That structural decrease is exactly what Fixpoint checks.2
To say explicitly which parameter decreases, write Fixpoint factorial (n : nat) {struct n} : nat :=. The {struct n} tells Rocq which parameter the recursive call must take a smaller subterm from. Rocq can infer it here from the single candidate, and it checks that k really came from inside S k.
Fixpoint factorial (n : nat) : nat :=
match n with
| 0 => 1
| S k => n * factorial k
end.
Compute factorial 5.Output:
= 120 : natThe output = 120 : nat above comes from factorial 5 starting at 5 = S 4 and unfolding step by step as 5 * factorial 4 5 * 4 * factorial 3 5 * 4 * 3 * 2 * 1 * factorial 0, where factorial 0 evaluates to 1 to produce 120. Now what would happen if you wrote the original parameter factorial n instead of the one-step smaller factorial k?
Rocq rejects that definition immediately! It tells you that the recursive call passes the original n instead of a smaller subterm, whereas in our valid code calling factorial k on the k unpacked from S k shrinks the argument by one constructor at every step until it hits 0. Enforcing this structural decrease is the core job of Fixpoint. In Part 3, we will define our own constructors, just like 0 and S, from scratch.3
Footnotes
- The Rocq Prover Reference Manual 9.2. Functions and Assumptions: Functions and Function Types · Functions and Assumptions: Function Application. Explains how
funbuilds lambda abstractions and how partial application yields remaining function types. ↩ - The Rocq Prover Reference Manual 9.2. Inductive Types: Top-Level Recursive Functions · Inductive Types: Fixpoint Definitions. Explains how
Fixpointenables self-reference and why recursive calls must pass a structurally smaller constructor argument. ↩ - The Rocq Prover Reference Manual 9.2. Inductive types and recursive functions. Explains the guardedness condition requiring recursive arguments in
Fixpointto decrease structurally. ↩