]> matita.cs.unibo.it Git - helm.git/blob - weblib/tutorial/chapter2.ma
commit by user andrea
[helm.git] / weblib / tutorial / chapter2.ma
1 include "basics/types.ma".
2
3 (* Most of the types we have seen so far are enumerated types, composed by a finite set of 
4 alternatives, and records, composed by tuples of heteregoneous elements. 
5 A more interesting case of type definition is when some of the rules defining its elements are 
6 recursive, i.e. they allow the formation of more elements of the type in terms of the already
7 defined ones. The most typical case is provided by the natural numbers, that can be defined as 
8 the smallest set generated by a constant 0 and a successor function from natural numbers to 
9 natural numbers *)
10
11 inductive nat : Type[0] ≝ 
12 | O :nat
13 | S: nat →nat.
14
15 (* The two terms O and S are called constructors: they define the signature of the type, whose 
16 objects are the elements freely generated by means of them. So, examples of natural numbers are 
17 O, S O, S (S O), S (S (S O)) and so on. 
18
19 The language of Matita allows the definition of well founded recursive functions over inductive 
20 types; in order to guarantee termination of recursion you are only allowed to make recursive 
21 calls on structurally smaller arguments than the ones you received in input. 
22 Most mathematical functions can be naturally defined in this way. For instance, the sum of two 
23 natural numbers can be defined as follows *)
24
25 let rec add n m ≝ 
26 match n with
27 [ O ⇒ m
28 | S a ⇒ \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 (add a m)
29 ].
30
31 (* It is worth to observe that the previous algorithm works by recursion over the first 
32 argument. This means that, for instance, (add O x) will reduce to x, as expected, but (add x O)
33 is stack. How can we prove that, for a generic x, (add x O) = x? The mathematical tool do it is 
34 called induction. The induction principle states that, given a property P(n) over natural 
35 numbers, if we prove P(0) and prove that, for any m, P(m) implies P(S m), than we can conclude
36 P(n) for any n. 
37
38 The elim tactic, allow you to apply induction in a very simple way. If your goal is P(n), the 
39 invocation of
40   elim n
41 will break down your task to prove the two subgoals P(0) and ∀m.P(m) → P(S m).
42
43 Let us apply it to our case *)
44
45 lemma add_0: ∀a. \ 5a href="cic:/matita/tutorial/chapter2/add.fix(0,0,1)"\ 6add\ 5/a\ 6 a \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6 \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 a.
46 #a elim a
47
48 (* If you stop the computation here, you will see on the right the two subgoals 
49     - add O O = O
50     - ∀x. add x 0 = x → add (S x) O = S x
51 After normalization, both goals are trivial.
52 *)
53
54 normalize // qed.
55
56 (* In a similar way, it is convenient to state a lemma about the behaviour of add when the 
57 second argument is not zero. *)
58
59 lemma add_S : ∀a,b. \ 5a href="cic:/matita/tutorial/chapter2/add.fix(0,0,1)"\ 6add\ 5/a\ 6 a (\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 b) \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 (\ 5a href="cic:/matita/tutorial/chapter2/add.fix(0,0,1)"\ 6add\ 5/a\ 6 a b).
60
61 (* In the same way as before, we proceed by induction over a. *)
62
63 #a #b elim a normalize //
64 qed. 
65
66 (* We are now in the position to prove the commutativity of the sum *)
67
68 theorem add_comm : ∀a,b. \ 5a href="cic:/matita/tutorial/chapter2/add.fix(0,0,1)"\ 6add\ 5/a\ 6 a b \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/add.fix(0,0,1)"\ 6add\ 5/a\ 6 b a.
69 #a elim a normalize
70
71 (* We have two sub goals:
72   G1: ∀b. b = add b O
73   G2: ∀x.(∀b. add x b = add b x) → ∀b. S (add x b) = add b (S x). 
74 G1 is just our lemma add_O. For G2, we start introducing x and the induction hypothesis IH; 
75 then, the goal is proved by rewriting using add_S and IH.
76 For Matita, the task is trivial and we can simply close the goal with // *)
77
78 // qed.
79
80 (* Let us now define the following function: *)
81
82 definition twice ≝ λn.\ 5a href="cic:/matita/tutorial/chapter2/add.fix(0,0,1)"\ 6add\ 5/a\ 6 n n. 
83
84 (* We are interested to prove that for any natural number m there exists a natural number m that 
85 is the integer half of m. This will give us the opportunity to introduce new connectives and 
86 quantifiers, and later on to make some interesting consideration on proofs and computations. *)
87
88 theorem ex_half: ∀n.\ 5a title="exists" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6m. n \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/twice.def(2)"\ 6twice\ 5/a\ 6 m \ 5a title="logical or" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6 n \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 (\ 5a href="cic:/matita/tutorial/chapter2/twice.def(2)"\ 6twice\ 5/a\ 6 m).
89 #n elim n normalize 
90
91 (* We proceed by induction on n, that breaks down to the following goals:
92   G1: ∃m.O = add O O ∨ O = S (add m m)
93   G2: ∀x.(∃m. x = add m m ∨ x = S (add m m))→ ∃m. S x = add m m ∨ S x = S (add m m)
94 The only way we have to prove an existential goal is by exhibiting the witness, that in the case 
95 of first goal is O. We do it by apply the term called ex_intro instantiated by the witness. 
96 Then, it is clear that we must follow the left branch of the disjunction. One way to do it is by 
97 applying the term or_introl, that is the first constructor of the disjunction. However, 
98 remembering the names of constructors can be annyoing: we can invoke the application of the n-th
99 constructor of an inductive type (inferred by the current goal) by typing %n. At this point 
100 we are left with the subgoal O = add O O, that is closed by computation.
101 It is worth to observe that invoking automation at depth /3/ would also automatically close G1.
102 *)
103   [@(\ 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 //
104
105 (* The case of G2 is more complex. We should start introducing x and the inductive hypothesis
106      IH: ∃m. x = add m m ∨ x = S (add m m) 
107 At this point we should assume the existence of m enjoying the inductive hypothesis. To 
108 eliminate the existential from the context we can just use the case tactic. This situation where
109 we introduce something into the context and immediately eliminate it by case analysis is so 
110 frequent that Matita provides a convenient shorthand: you can just type a single "*". 
111 The star symbol should be reminiscent of an explosion: the idea is that you have a structured
112 hypothesis, and you ask to explode it into its constituents. In the case of the existential,
113 it allows to pass from a goal of the shape 
114     (∃x.P x) → Q
115 to a goal of the shape
116     ∀x.P x → Q
117 *)
118   |#x *
119 (* At this point we are left with a new goal with the following shape
120   G3: ∀m. x = add m m ∨ x = S (add m m) → ....  
121 We should introduce m, the hypothesis H: x = add m m ∨ x = S (add m m), and then reason by
122 cases on this hypothesis. It is the same situation as before: we explode the disjunctive 
123 hypothesis into its possible consituents. In the case of a disjunction, the * tactic allows
124 to pass from a goal of the form
125     A∨B → Q
126 to two subgoals of the form
127     A → Q  and  B → Q
128 *)
129   #m * #eqx
130 (* In the first subgoal, we are under the assumption that x = add m m. The half of (S x)
131 is hence m, and we have to prove the right branch of the disjunction. 
132 In the second subgoal, we are under the assumption that x = S (add m m). The halh of (S x)
133 is hence (S m), and have to follow the left branch of the disjunction.
134 *)
135   [@(\ 5a href="cic:/matita/basics/logic/ex.con(0,1,2)"\ 6ex_intro\ 5/a\ 6 … m) /2/ | @(\ 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,2,0)"\ 6S\ 5/a\ 6 m)) normalize /2/
136   ]
137 qed. 
138
139 (* Instead of proving the existence of a number corresponding to the half of n, we could
140 be interested in computing it. The best way to do it is to define this division operation
141 together with the remainder, that is 0 if the input term is even, and 1 if the input term
142 is odd. Since we must return a pair, we could use a suitably defined record type, or simply
143 a product type N × N, defined in the basic library. The product type is just a sort of
144 general purpose record, with standard fields fst and snd, called projections. A pair of
145 values n and m is written (pair … m n) or \langle n,m \rangle - visually rendered as 〈n,m〉 
146
147 We first write down the function, and then discuss it.*)
148
149 let rec div2 n ≝ 
150 match n with
151 [ O ⇒ \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6,\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6
152 | S a ⇒ \ 5span style="text-decoration: underline;"\ 6\ 5/span\ 6
153    let p ≝ (div2 a) in
154    match (\ 5a href="cic:/matita/basics/types/snd.def(1)"\ 6snd\ 5/a\ 6 … p) with
155    [ O ⇒ \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6\ 5a href="cic:/matita/basics/types/fst.def(1)"\ 6fst\ 5/a\ 6 … p, \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6
156    | S b ⇒ \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 (\ 5a href="cic:/matita/basics/types/fst.def(1)"\ 6fst\ 5/a\ 6 … p),\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6〉 
157    ]
158 ]. 
159
160 (* The function is computed by recursion over the input n. If n is 0, then both quotient
161 and remainder are 0. If n = S a, we start computing the half of a, say 〈q,r〉. Then we
162 have two cases according to the possible values of b: if b is 0, then we must return
163  〈q,1〉, while if b = 1 (no other case is possible), then we must return 〈S q,0〉.
164
165 It is important to point out the deep, substantial analogy between the algorithm for 
166 computing div2 and the the proof of ex_half. In particular ex_half returns a 
167 proof of the kind ∃n.A(n)∨B(n): the really informative content in it is the witness
168 n and a boolean indicating which one between the two conditions A(n) and B(n) is met.
169 This is precisely the quotient-remainder pair returned by div2.
170 In both cases we proceed by recurrence (respectively, induction or recursion) over the 
171 input argument n. In case n = 0, we conclude the proof in ex_half by providing the
172 witness O and a proof of A(O); this corresponds to returning the pair 〈O,O〉 in div2.
173 Similarly, in the inductive case n = S a, we must exploit the inductive hypothesis 
174 for a (i.e. the result of the recursive call), distinguishing two subcases according 
175 to the the two possibilites A(a) or B(a) (i.e. the two possibile values of the remainder 
176 for a). The reader is strongly invited to check all remaining details.
177
178 Let us now prove that our div2 function has the expected behaviour.
179 *)
180
181 lemma surjective_pairing: ∀A,B.∀p:A\ 5a title="Product" href="cic:/fakeuri.def(1)"\ 6×\ 5/a\ 6B. p \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6\ 5a href="cic:/matita/basics/types/fst.def(1)"\ 6fst\ 5/a\ 6 … p,\ 5a title="pair pi2" href="cic:/fakeuri.def(1)"\ 6\snd\ 5/a\ 6 … p〉.
182 #A #B * // qed.
183
184 lemma div2SO: ∀n,q. \ 5a href="cic:/matita/tutorial/chapter2/div2.fix(0,0,2)"\ 6div2\ 5/a\ 6 n \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6q,\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6〉 → \ 5a href="cic:/matita/tutorial/chapter2/div2.fix(0,0,2)"\ 6div2\ 5/a\ 6 (\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 n) \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6\ 5span style="text-decoration: underline;"\ 6\ 5/span\ 6q, \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6〉.
185 #n #q #H normalize >H normalize // qed.
186
187 lemma div2S1: ∀n,q. \ 5a href="cic:/matita/tutorial/chapter2/div2.fix(0,0,2)"\ 6div2\ 5/a\ 6 n \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6q,\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6〉 → \ 5a href="cic:/matita/tutorial/chapter2/div2.fix(0,0,2)"\ 6div2\ 5/a\ 6 (\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 n) \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 q,\ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6〉.
188 #n #q #H normalize >H normalize // qed.
189
190  lemma div2_ok: ∀n,q,r. \ 5a href="cic:/matita/tutorial/chapter2/div2.fix(0,0,2)"\ 6div2\ 5/a\ 6 n \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a title="Pair construction" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6q,r〉 →
191    r \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6 \ 5span style="text-decoration: underline;"\ 6\ 5/span\ 6\ 5a title="logical and" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6 n \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/twice.def(2)"\ 6twice\ 5/a\ 6 q \ 5a title="logical or" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6 
192    r \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,1,0)"\ 6O\ 5/a\ 6 \ 5a title="logical and" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6 n \ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6=\ 5/a\ 6 \ 5a href="cic:/matita/tutorial/chapter2/nat.con(0,2,0)"\ 6S\ 5/a\ 6 (\ 5a href="cic:/matita/tutorial/chapter2/twice.def(2)"\ 6twice\ 5/a\ 6 q).
193 #n elim n
194   [#q #r normalize #H destruct %1 /2/
195   |#a #Hind #q #r cases (Hind … (\ 5a href="cic:/matita/tutorial/chapter2/surjective_pairing.def(3)"\ 6surjective_pairing\ 5/a\ 6 …)) * #eqr #eqa
196     [>(\ 5a href="cic:/matita/tutorial/chapter2/div2SO.def(3)"\ 6div2SO\ 5/a\ 6 a (\ 5a href="cic:/matita/basics/types/fst.def(1)"\ 6fst\ 5/a\ 6 … (\ 5a href="cic:/matita/tutorial/chapter2/div2.fix(0,0,2)"\ 6div2\ 5/a\ 6 a))) // #H %2 destruct <eqa /2/
197     |>(\ 5a href="cic:/matita/tutorial/chapter2/div2S1.def(3)"\ 6div2S1\ 5/a\ 6 a (\ 5a href="cic:/matita/basics/types/fst.def(1)"\ 6fst\ 5/a\ 6 … (\ 5a href="cic:/matita/tutorial/chapter2/div2.fix(0,0,2)"\ 6div2\ 5/a\ 6 a))) // #H %1 destruct >\ 5a href="cic:/matita/tutorial/chapter2/add_S.def(2)"\ 6add_S\ 5/a\ 6 <eqa /2/
198     ]
199   ]
200 qed.
201
202 (* There is still another possibility, however, namely to mix the program and its 
203 specification into a single entity. The idea is to refine the output type of the div2 
204 function: it should not be just a generic pair 〈q,r〉 of natural numbers but a specific 
205 pair satisfying the specification of the function. In other words, we need the
206 possibility to define, for a type A and a property P over A, the subset type 
207 {a:A|P(a)} of all elements a of type A that satisfy the property P. Subset type are
208 just a particular case of the so called dependent types, that is types that can 
209 depend over arguments (such as arrays of a specified lenght, taken as a parameter).
210 These kind of types are quite unusual in traditional programming languages, and their
211 study is one of the new frontiers of the current research on type systems. 
212
213 *)
214
215