3.2 KiB
Pi Types
We will try to formulate and prove the statement
The sum of two even naturals is even. To do so we must define
+
on the naturals. Addition takes in two naturals and spits out a natural, so it should have typeℕ → ℕ → ℕ
.
_+_ : ℕ → ℕ → ℕ
n + m = ?
Try coming up with a sensible definition. It may not look 'the same' as ours.
Hint
n + 0
should be n
and n + (m + 1)
should be (n + m) + 1
Now we can make the statement:
SumOfEven : (x : Σ ℕ isEven) → (y : Σ ℕ isEven) → isEven (x .fst + y .fst)
SumOfEven x y = ?
Tip:
x .fst
is another notation forfst x
. This works for all sigma types. There are three ways to interpret this:
- For all even naturals
x
and for all even naturalsy
, their sum is even. isEven (x .fst + y .fst)
is a construction depending on two recipesx
andy
. Given two recipesx
andy
ofΣ ℕ isEven
, we break them down into their first components, apply the conversion_+_
, and form a recipe forisEven
of the result.isEven (_ .fst + _ .fst)
is a bundle over the categorical productΣ ℕ isEven × Σ ℕ isEven
andSumOfEven
is a section of the bundle.
More generally given A : Type
and B : A → Type
we can form the pi type (x : A) → B x : Type
(in other languages Π (x : ℕ), isEven n
).
The notation suggests that these behave like functions,
and indeed in the special case where the fiber is constant
with respect to the base space
a section is just a term of type A → B
, i.e. a function.
Hence pi types are also known as dependent function types.
We are now in a position to prove the statement. Have fun!
Important: Once you have proven the statement,
check out our two ways of defining addition _+_
and _+'_
(in the solutions).
- Use
C-c C-n
to check that they compute the same values on different examples. - Uncomment the code for
Sum'OfEven
in the solutions. It is justSumOfEven
but with+
s changed for+'
s. - Load the file. Does the proof still work?
Our proof SumOfEven
relied on
the explicit definition of _+_
,
which means if we wanted to use our proof on
someone else's definition of addition,
it might not work anymore.
But
_+_
and_+'_
compute the same values. Are_+_
and_+'_
'the same'? What is 'the same'?
As the final task of the Quest, try to express and prove in agda the statement
For any natural number it is even or is is not even. We will make a summary of what is needed:
- a definition of the type
A ⊕ B
(input\oplus
), which has three interpretations- the proposition '
A
orB
' - the construction with two ways of making recipes
left : A → A ⊕ B
andright : B → A ⊕ B
. - the coproduct of two objects
A
andB
. The type needs to take in parametersA : Type
andB : Type
data _⊕_ (A : Type) (B : Type) : Type where ???
- the proposition '
- a definition of negation. One can motivate it by the following
- Define
A ↔ B : Type
for two typesA : Type
andB : Type
. - Show that for any
A : Type
we have(A ↔ ⊥) ↔ (A → ⊥)
- Define
¬ : Type → Type
to beλ A → (A → ⊥)
.
- Define
- a formulation and proof of the statement above