πŸ“₯ Download raw
module Padova2025.ProgrammingBasics.Naturals.Base where

Definition

In the previous chapter, we have defined the finite type of Booleans. Agda also supports infinite types. The natural numbers are the primordial example, and we will explore them now. The type of natural numbers is brought into existence with the following three lines of Agda code.

data β„• : Set where  -- enter "β„•" by "\bN"
  zero : β„•
  succ : β„• β†’ β„•      -- short for "successor"

The succ constructor outputs, for each input, a new freshly-minted value of the type β„•. Hence the type β„• contains the following distinct elements:

-- zero
-- succ zero
-- succ (succ zero)
-- succ (succ (succ zero))
-- ...

We can define abbreviations for often-occuring numbers:

one : β„•
one = succ zero

two : β„•
two = succ one

three : β„•
three = succ two

four : β„•
four = succ three

five : β„•
five = succ four

Writing natural numbers in unary is a pain. By adding {-# BUILTIN NATURAL β„• #-} on a line of its own, we can ask Agda to enable suitable syntactic sugar. From then on, we can write decimal expressions like 42 which Agda will transparently desugar to appropriate succ calls: succ (succ (... (zero))).