1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "basics/types.ma".
16 include "arithmetics/nat.ma".
17 include "basics/lists/list.ma".
19 inductive t : Type[0] ≝
23 definition path ≝ list bool.
25 definition tp ≝ t × path.
27 let rec setleaf_fun (v:nat) (x:t) (p:path) on p : t × bool ≝
31 [ leaf _ ⇒ 〈leaf v,true〉
32 | node x1 x2 ⇒ 〈node x1 x2,false〉 ]
35 [ leaf n ⇒ 〈leaf n,false〉
38 let 〈x2',res〉 ≝ setleaf_fun v x2 tl in
41 let 〈x1',res〉 ≝ setleaf_fun v x1 tl in
42 〈node x1' x2, res〉 ]].
44 let rec admissible (x:t) (p:path) on p : bool ≝
51 if b then admissible x2 tl else admissible x1 tl ]].
53 definition left: ∀A:Type[0]. (bool → tp → A) → tp → A ≝
57 k (admissible t (reverse … p')) 〈t,p'〉.
59 definition right: ∀A:Type[0]. (bool → tp → A) → tp → A ≝
63 k (admissible t (reverse … p')) 〈t,p'〉.
65 definition reset: ∀A:Type[0]. (tp → A) → tp → A ≝
70 definition setleaf: ∀A:Type[0]. nat → (bool → tp → A) → tp → A ≝
73 let 〈t',res〉 ≝ setleaf_fun v t (reverse … p) in
76 (*****************************)
78 let rec update (A:Type[0]) (v:nat) (k: bool → tp → A) (p:path) on p:
82 [ nil ⇒ setleaf … v (λres. reset … (k res))
85 right … (λres1.update … v (λres2. k (res1 ∧ res2)) tl)
87 left … (λres1. update … v (λres2.k (res1 ∧ res2)) tl) ].
90 node (node (leaf 0) (leaf 1)) (node (leaf 2) (leaf 3)).
92 lemma test: update ? 5 (λres,x. 〈res,x〉) [false;false] 〈example,nil …〉 = ?.
96 lemma setleaf_fun_correct:
98 admissible t p = false → setleaf_fun v t p = 〈t,false〉.
99 #v #p elim p normalize [#t #abs destruct ]
100 #hd #tl #IH * normalize // #x1 #x2 cases hd normalize #H >IH //
103 lemma rev_append_cons:
104 ∀A,x,l1,l2. rev_append A (x::l1) [] @ l2 = rev_append A l1 []@x::l2.
105 #A #x #l1 #l2 <(associative_append ?? [?]) <reverse_cons //
108 lemma admissible_leaf_cons:
109 ∀n,p1,dir,p2. admissible (leaf n) (p1@dir::p2) = false.
113 lemma admissible_append_true:
114 ∀p1,p2,t. admissible t (p1@p2)=true → admissible t p1=true.
115 #p1 elim p1 normalize // #hd #tl #IH #p2 * normalize //
116 #x1 #x2 cases hd normalize @IH
119 theorem update_correct1:
121 admissible t (reverse … p2 @ p1) = false →
122 update A v k p1 〈t,p2〉 = k false 〈t,[]〉.
123 #A #v #p1 elim p1 normalize
124 [ #p2 #k #t #H >setleaf_fun_correct //
125 | #hd #tl #IH #p2 #k #t cases hd normalize nodelta
126 cases t normalize [1,3:#n|2,4:#x1 #x2] #H >IH // cases (admissible ??) //
129 theorem update_correct2:
131 admissible t (reverse … p2 @ p1) = true →
132 update A v k p1 〈t,p2〉 = update … v k [] 〈t,reverse … p1 @ p2〉.
133 #A #v #p1 elim p1 normalize //
134 #dir #ptl #IH #p2 #k #t cases dir normalize nodelta cases t normalize nodelta
135 [1,3: #n >admissible_leaf_cons #abs destruct
137 [2,4: normalize >rev_append_def >associative_append //
138 |*: >(rev_append_def … ptl [?]) >associative_append
139 >(?:admissible ?? = true) // @(admissible_append_true … ptl) // ]]
142 theorem final_update_correct:
144 let 〈t',res〉 ≝ setleaf_fun v t (reverse … p1 @ p2) in
145 update ? v (λres,x.〈res,x〉) p2 〈t,p1〉 = 〈res,〈t',nil …〉〉.
146 #v #p1 #p2 #t @pair_elim #t' #res #EQ inversion (admissible t (reverse … p1 @ p2))
147 [ #H >update_correct2 // whd in ⊢ (??%?);
148 >(reverse_append ? (reverse ? p2) p1) >reverse_reverse >EQ %
149 | #H >update_correct1 // >setleaf_fun_correct in EQ; // ]