+\ 5h1 class="section"\ 6Induction and Recursion\ 5/h1\ 6
include "basics/types.ma".
(* Most of the types we have seen so far are enumerated types, composed by a
| S a ⇒ \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 (add a m)
].
-(* It is worth to observe that the previous algorithm works by recursion over the
+(*
+\ 5h2 class="section"\ 6Elimination\ 5/h2\ 6
+It is worth to observe that the previous algorithm works by recursion over the
first argument. This means that, for instance, (add O x) will reduce to x, as
expected, but (add x O) is stuck.
How can we prove that, for a generic x, (add x O) = x? The mathematical tool to do
definition twice ≝ λn.\ 5a href="cic:/matita/tutorial/chapter2/add.fix(0,0,1)"\ 6add\ 5/a\ 6 n n.
-(* We are interested to prove that for any natural number n there exists a natural
+(*
+\ 5h2 class="section"\ 6Existential\ 5/h2\ 6
+We are interested to prove that for any natural number n there exists a natural
number m that is the integer half of n. This will give us the opportunity to
introduce new connectives and quantifiers and, later on, to make some interesting
consideration on proofs and computations. *)
*)
[@(\ 5a href="cic:/matita/basics/logic/ex.con(0,1,2)"\ 6ex_intro\ 5/a\ 6 … \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6) %1 //
-(* The case of G2 is more complex. We should start introducing x and the
+(*
+\ 5h2 class="section"\ 6Destructuration\ 5/h2\ 6
+The case of G2 is more complex. We should start introducing x and the
inductive hypothesis
IH: ∃m. x = add m m ∨ x = S (add m m)
At this point we should assume the existence of m enjoying the inductive
]
qed.
-(* Instead of proving the existence of a number corresponding to the half of n,
+(*
+\ 5h2 class="section"\ 6Computing vs. Proving\ 5/h2\ 6
+Instead of proving the existence of a number corresponding to the half of n,
we could be interested in computing it. The best way to do it is to define this
division operation together with the remainder, that in our case is just a
boolean value: tt if the input term is even, and ff if the input term is odd.
]
qed.
-(* There is still another possibility, however, namely to mix the program and its
+(* \ 5h2 class="section"\ 6Mixing proofs and computations\ 5/h2\ 6
+There is still another possibility, however, namely to mix the program and its
specification into a single entity. The idea is to refine the output type of the
div2 function: it should not be just a generic pair 〈q,r〉 of natural numbers but a
specific pair satisfying the specification of the function. In other words, we need