]> matita.cs.unibo.it Git - helm.git/blobdiff - weblib/tutorial/chapter5.ma
commit by user andrea
[helm.git] / weblib / tutorial / chapter5.ma
index 12df4cc6d2ab877e151011c19028359c61336a13..bc01f3c7021d350d8a490f3e5fb99f1aabbbfe32 100644 (file)
@@ -1,58 +1,76 @@
-(* boolean functions over lists *)
+(* The fact of being able to decide, via a computable boolean function, the 
+equality between elements of a given set is an essential prerequisite for 
+effectively searching an element of that set inside a data structure. In this 
+section we shall define several boolean functions acting on lists of elements in 
+a DeqSet, and prove some of their properties.*)
 
 include "basics/list.ma".
-include "basics/sets.ma".
-include "basics/deqsets.ma".
+include "tutorial/chapter4.ma".
 
-(********* search *********)
+(* The first function we define is an effective version of the membership relation,
+between an element x and a list l. Its definition is a straightforward recursion on
+l.*)
 
-let rec memb (S:DeqSet) (x:S) (l: list S) on l  ≝
+let rec memb (S:DeqSet) (x:S) (l: list\ 5span class="error" title="Parse error: RPAREN expected after [term] (in [arg])"\ 6\ 5/span\ 6 S) on l  ≝
   match l with
   [ nil ⇒ false
-  | cons a tl ⇒ (x == a) ∨ memb S x tl
+  | cons a tl ⇒ (x =\ 5span class="error" title="Parse error: NUMBER '1' or [term] or [sym=] expected after [sym=] (in [term])"\ 6\ 5/span\ 6= a) ∨ memb S x tl
   ].
 
 notation < "\memb x l" non associative with precedence 90 for @{'memb $x $l}.
 interpretation "boolean membership" 'memb a l = (memb ? a l).
 
+(* We can now prove several interesing properties for memb:
+- memb_hd: x is a member of x::l
+- memb_cons: if x is a member of l than x is a member of a::l
+- memb_single: if x is a member of [a] then x=a
+- memb_append: if x is a member of l1@l2 then either x is a member of l1
+  or x is a member of l2
+- memb_append_l1: if x is a member of l1 then x is a member of l1@l2
+- memb_append_l2: if x is a member of l2 then x is a member of l1@l2
+- memb_exists: if x is a member of l, than l can decomposed as l1@(x::l2)
+- not_memb_to_not_eq: if x is not a member of l and y is, then x≠y
+- memb_map: if a is a member of l, then (f a) is a member of (map f l)
+- memb_compose: if a is a member of l1 and b is a meber of l2 than
+  (op a b) is a member of (compose op l1 l2)
+*)
+
 lemma memb_hd: ∀S,a,l. memb S a (a::l) = true.
 #S #a #l normalize >(proj2 … (eqb_true S …) (refl S a)) //
 qed.
 
 lemma memb_cons: ∀S,a,b,l. 
-  memb S a l = true → memb S a (b::l) = true.
+  memb S a l = true → memb\ 5a href="cic:/matita/tutorial/chapter5/memb.fix(0,2,4)"\ 6\ 5/a\ 6 S a (b::l) = true.
 #S #a #b #l normalize cases (a==b) normalize // 
 qed.
 
-lemma memb_single: ∀S,a,x. memb S a [x] = true → a = x.
+lemma memb_single: ∀S,a,x. memb S a (x::[]) = true → a = x.
 #S #a #x normalize cases (true_or_false … (a==x)) #H
-  [#_ >(\P H) // |>H normalize #abs @False_ind /2/]
+  [#_ >(\P H) // |>H normalize #abs @False_ind /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/]
 qed.
 
 lemma memb_append: ∀S,a,l1,l2. 
-memb S a (l1@l2) = true →
-  memb S a l1= true ∨ memb S a l2 = true.
+memb S a (l1@\ 5a title="append" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6l2) = true → memb S a l1= true ∨ memb S a l2 = true.
 #S #a #l1 elim l1 normalize [#l2 #H %2 //] 
-#b #tl #Hind #l2 cases (a==b) normalize /2
+#b #tl #Hind #l2 cases (a==b) normalize /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace orb_true_l\ 5/span\ 6\ 5/span\ 6
 qed. 
 
 lemma memb_append_l1: ∀S,a,l1,l2. 
- memb S a l1= true → memb S a (l1@l2) = true.
+ memb S a l1= true → memb S a (l1@\ 5a title="append" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6l2) = true.
 #S #a #l1 elim l1 normalize
-  [normalize #le #abs @False_ind /2/
-  |#b #tl #Hind #l2 cases (a==b) normalize /2
+  [normalize #le #abs @False_ind /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/
+  |#b #tl #Hind #l2 cases (a==b) normalize /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace \ 5/span\ 6\ 5/span\ 6
   ]
 qed. 
 
 lemma memb_append_l2: ∀S,a,l1,l2. 
  memb S a l2= true → memb S a (l1@l2) = true.
 #S #a #l1 elim l1 normalize //
-#b #tl #Hind #l2 cases (a==b) normalize /2
+#b #tl #Hind #l2 cases (a==b) normalize /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace \ 5/span\ 6\ 5/span\ 6
 qed. 
 
-lemma memb_exists: ∀S,a,l.memb S a l = true → 
-  ∃l1,l2.l=l1@(a::l2).
-#S #a #l elim l [normalize #abs @False_ind /2/]
+lemma memb_exists: ∀S,a,l.memb S a l = true\ 5a href="cic:/matita/basics/bool/bool.con(0,1,0)"\ 6\ 5/a\ 6 → ∃l1,l2.l=l1@(a::l2).
+#S #a #l elim l [normalize #abs @False_ind /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/]
 #b #tl #Hind #H cases (orb_true_l … H)
   [#eqba @(ex_intro … (nil S)) @(ex_intro … tl) >(\P eqba) //
   |#mem_tl cases (Hind mem_tl) #l1 * #l2 #eqtl
@@ -61,17 +79,17 @@ lemma memb_exists: ∀S,a,l.memb S a l = true →
 qed.
 
 lemma not_memb_to_not_eq: ∀S,a,b,l. 
- memb S a l = false → memb S b l = true → a==b = false.
+ memb S a l = false\ 5a href="cic:/matita/basics/bool/bool.con(0,2,0)"\ 6\ 5/a\ 6 → memb S b l = true → a==b = false.
 #S #a #b #l cases (true_or_false (a==b)) // 
-#eqab >(\P eqab) #H >H #abs @False_ind /2/
+#eqab >(\P eqab) #H >H #abs @False_ind /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/
 qed. 
  
 lemma memb_map: ∀S1,S2,f,a,l. memb S1 a l= true → 
-  memb S2 (f a) (map … f l) = true.
+  memb S2 (f a) (map … f l) =\ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6 true.
 #S1 #S2 #f #a #l elim l normalize [//]
 #x #tl #memba cases (true_or_false (a==x))
   [#eqx >eqx >(\P eqx) >(\b (refl … (f x))) normalize //
-  |#eqx >eqx cases (f a==f x) normalize /2/
+  |#eqx >eqx cases (f a==f x) normalize /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace \ 5/span\ 6\ 5/span\ 6/
   ]
 qed.
 
@@ -79,13 +97,16 @@ lemma memb_compose: ∀S1,S2,S3,op,a1,a2,l1,l2.
   memb S1 a1 l1 = true → memb S2 a2 l2 = true →
   memb S3 (op a1 a2) (compose S1 S2 S3 op l1 l2) = true.
 #S1 #S2 #S3 #op #a1 #a2 #l1 elim l1 [normalize //]
-#x #tl #Hind #l2 #memba1 #memba2 cases (orb_true_l … memba1)
+#x #tl #Hind #l2 #memba1 #memba2 cases (orb_true_l\ 5a href="cic:/matita/basics/bool/orb_true_l.def(2)"\ 6\ 5/a\ 6 … memba1)
   [#eqa1 >(\P eqa1) @memb_append_l1 @memb_map // 
   |#membtl @memb_append_l2 @Hind //
   ]
 qed.
 
-(**************** unicity test *****************)
+(* If we are interested in representing finite sets as lists, is is convenient
+to avoid duplications of elements. The following uniqueb check this property. *)
+
+(*************** unicity test *****************)
 
 let rec uniqueb (S:DeqSet) l on l : bool ≝
   match l with 
@@ -104,13 +125,13 @@ let rec unique_append (S:DeqSet) (l1,l2: list S) on l1 ≝
   ].
 
 axiom unique_append_elim: ∀S:DeqSet.∀P: S → Prop.∀l1,l2. 
-(∀x. memb S x l1 = true → P x) → (∀x. memb S x l2 = true → P x) →
+(∀x. memb S x l1 =\ 5a title="leibnitz's equality" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6 true → P x) → (∀x. memb S x l2 = true → P x) →
 ∀x. memb S x (unique_append S l1 l2) = true → P x. 
 
 lemma unique_append_unique: ∀S,l1,l2. uniqueb S l2 = true →
   uniqueb S (unique_append S l1 l2) = true.
 #S #l1 elim l1 normalize // #a #tl #Hind #l2 #uniquel2
-cases (true_or_false … (memb S a (unique_append S tl l2))) 
+cases (true_or_false\ 5a href="cic:/matita/basics/bool/true_or_false.def(1)"\ 6\ 5/a\ 6 … (memb S a (unique_append S tl l2))) 
 #H >H normalize [@Hind //] >H normalize @Hind //
 qed.
 
@@ -122,7 +143,7 @@ lemma sublist_length: ∀S,l1,l2.
  uniqueb S l1 = true → sublist S l1 l2 → |l1| ≤ |l2|.
 #S #l1 elim l1 // 
 #a #tl #Hind #l2 #unique #sub
-cut (∃l3,l4.l2=l3@(a::l4)) [@memb_exists @sub //]
+cut (∃\ 5a title="exists" href="cic:/fakeuri.def(1)"\ 6\ 5/a\ 6l3,l4.l2=l3@(a::l4)) [@memb_exists @sub //]
 * #l3 * #l4 #eql2 >eql2 >length_append normalize 
 applyS le_S_S <length_append @Hind [@(andb_true_r … unique)]
 >eql2 in sub; #sub #x #membx 
@@ -130,41 +151,41 @@ cases (memb_append … (sub x (orb_true_r2 … membx)))
   [#membxl3 @memb_append_l1 //
   |#membxal4 cases (orb_true_l … membxal4)
     [#eqxa @False_ind lapply (andb_true_l … unique)
-     <(\P eqxa) >membx normalize /2/ |#membxl4 @memb_append_l2 //
+     <(\P eqxa) >membx normalize /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/ |#membxl4 @memb_append_l2\ 5a href="cic:/matita/tutorial/chapter5/memb_append_l2.def(5)"\ 6\ 5/a\ 6 //
     ]
   ]
 qed.
 
 lemma sublist_unique_append_l1: 
   ∀S,l1,l2. sublist S l1 (unique_append S l1 l2).
-#S #l1 elim l1 normalize [#l2 #S #abs @False_ind /2/]
+#S #l1 elim l1 normalize [#l2 #S #abs @False_ind /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/]
 #x #tl #Hind #l2 #a 
 normalize cases (true_or_false … (a==x)) #eqax >eqax 
 [<(\P eqax) cases (true_or_false (memb S a (unique_append S tl l2)))
   [#H >H normalize // | #H >H normalize >(\b (refl … a)) //]
 |cases (memb S x (unique_append S tl l2)) normalize 
-  [/2/ |>eqax normalize /2/]
+  [/\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace \ 5/span\ 6\ 5/span\ 6/ |>eqax normalize /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace \ 5/span\ 6\ 5/span\ 6/]
 ]
 qed.
 
 lemma sublist_unique_append_l2: 
   ∀S,l1,l2. sublist S l2 (unique_append S l1 l2).
 #S #l1 elim l1 [normalize //] #x #tl #Hind normalize 
-#l2 #a cases (memb S x (unique_append S tl l2)) normalize
+#l2 #a cases (memb S x (unique_append\ 5a href="cic:/matita/tutorial/chapter5/unique_append.fix(0,1,5)"\ 6\ 5/a\ 6 S tl l2)) normalize
 [@Hind | cases (a==x) normalize // @Hind]
 qed.
 
 lemma decidable_sublist:∀S,l1,l2. 
   (sublist S l1 l2) ∨ ¬(sublist S l1 l2).
 #S #l1 #l2 elim l1 
-  [%1 #a normalize in ⊢ (%→?); #abs @False_ind /2/
+  [%1 #a normalize in ⊢ (%→?); #abs @False_ind /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/
   |#a #tl * #subtl 
     [cases (true_or_false (memb S a l2)) #memba
       [%1 whd #x #membx cases (orb_true_l … membx)
         [#eqax >(\P eqax) // |@subtl]
       |%2 @(not_to_not … (eqnot_to_noteq … true memba)) #H1 @H1 @memb_hd
       ]
-    |%2 @(not_to_not … subtl) #H1 #x #H2 @H1 @memb_cons //
+    |%2 @(not_to_not … subtl) #H1 #x #H2 @H1 @memb_cons\ 5a href="cic:/matita/tutorial/chapter5/memb_cons.def(5)"\ 6\ 5/a\ 6 //
     ] 
   ]
 qed.
@@ -173,7 +194,7 @@ qed.
 
 lemma filter_true: ∀S,f,a,l. 
   memb S a (filter S f l) = true → f a = true.
-#S #f #a #l elim l [normalize #H @False_ind /2/]
+#S #f #a #l elim l [normalize #H @False_ind /\ 5span class="autotactic"\ 62\ 5span class="autotrace"\ 6 trace absurd\ 5/span\ 6\ 5/span\ 6/]
 #b #tl #Hind cases (true_or_false (f b)) #H
 normalize >H normalize [2:@Hind]
 cases (true_or_false (a==b)) #eqab
@@ -181,7 +202,7 @@ cases (true_or_false (a==b)) #eqab
 qed. 
   
 lemma memb_filter_memb: ∀S,f,a,l. 
-  memb S a (filter S f l) = true → memb S a l = true.
+  memb S a (filter S f l) = true → memb\ 5a href="cic:/matita/tutorial/chapter5/memb.fix(0,2,4)"\ 6\ 5/a\ 6 S a l = true.
 #S #f #a #l elim l [normalize //]
 #b #tl #Hind normalize (cases (f b)) normalize 
 cases (a==b) normalize // @Hind
@@ -189,7 +210,7 @@ qed.
   
 lemma memb_filter: ∀S,f,l,x. memb S x (filter ? f l) = true → 
 memb S x l = true ∧ (f x = true).
-/3/ qed.
+/\ 5span class="autotactic"\ 63\ 5span class="autotrace"\ 6 trace conj, filter_true, memb_filter_memb\ 5a href="cic:/matita/tutorial/chapter5/memb_filter_memb.def(5)"\ 6\ 5/a\ 6\ 5/span\ 6\ 5/span\ 6/ qed.
 
 lemma memb_filter_l: ∀S,f,x,l. (f x = true) → memb S x l = true →
 memb S x (filter ? f l) = true.
@@ -202,14 +223,8 @@ qed.
 
 (********************* exists *****************)
 
-let rec exists (A:Type[0]) (p:A → bool) (l:list A) on l : bool ≝
+let rec exists (A:Type[0]) (p:A → bool) (l:list A) on l : bool\ 5a href="cic:/matita/basics/bool/bool.ind(1,0,0)"\ 6\ 5/a\ 6 ≝
 match l with
 [ nil ⇒ false
 | cons h t ⇒ orb (p h) (exists A p t)
-].
-
-lemma Exists_exists : ∀A,P,l.
-  Exists A P l →
-  ∃x. P x.
-#A #P #l elim l [ * | #hd #tl #IH * [ #H %{hd} @H | @IH ]
-qed.
+].
\ No newline at end of file