| 8797 |      1 | (*  Title:      HOL/ex/AVL.ML
 | 
|  |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Cornelia Pusch and Tobias Nipkow
 | 
|  |      4 |     Copyright   1998  TUM
 | 
|  |      5 | *)
 | 
|  |      6 | 
 | 
|  |      7 | (******************************    isbal    **********************************)
 | 
|  |      8 | 
 | 
|  |      9 | Addsimps[Let_def];
 | 
|  |     10 | 
 | 
|  |     11 | (* rotations preserve isbal property *)
 | 
|  |     12 | 
 | 
|  |     13 | Goalw [bal_def]
 | 
|  |     14 | "height l = Suc(Suc(height r)) --> bal l = Right --> isbal l --> isbal r \
 | 
|  |     15 | \ --> isbal (lr_rot (n, l, r))";
 | 
|  |     16 | by (case_tac "l" 1); 
 | 
|  |     17 |  by (Asm_simp_tac 1);
 | 
|  |     18 | by (case_tac "tree2" 1);
 | 
|  |     19 |  by (Asm_simp_tac 1);
 | 
|  |     20 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     21 | by (arith_tac 1);
 | 
|  |     22 | qed_spec_mp "isbal_lr_rot";
 | 
|  |     23 | 
 | 
|  |     24 | 
 | 
|  |     25 | Goalw [bal_def]
 | 
|  |     26 | "height l = Suc(Suc(height r)) --> bal l ~= Right --> isbal l --> isbal r \
 | 
|  |     27 | \ --> isbal (r_rot (n, l, r))";
 | 
|  |     28 | by (case_tac "l" 1); 
 | 
|  |     29 |  by (Asm_simp_tac 1);
 | 
|  |     30 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     31 | qed_spec_mp "isbal_r_rot";
 | 
|  |     32 | 
 | 
|  |     33 | 
 | 
|  |     34 | Goalw [bal_def]
 | 
|  |     35 | "height r = Suc(Suc(height l)) --> bal r = Left --> isbal l --> isbal r \
 | 
|  |     36 | \ --> isbal (rl_rot (n, l, r))";
 | 
|  |     37 | by (case_tac "r" 1); 
 | 
|  |     38 |  by (Asm_simp_tac 1);
 | 
|  |     39 | by (case_tac "tree1" 1);
 | 
|  |     40 |  by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     41 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     42 | by (arith_tac 1);
 | 
|  |     43 | qed_spec_mp "isbal_rl_rot";
 | 
|  |     44 | 
 | 
|  |     45 | 
 | 
|  |     46 | Goalw [bal_def]
 | 
|  |     47 | "height r = Suc(Suc(height l)) --> bal r ~= Left --> isbal l --> isbal r \
 | 
|  |     48 | \ --> isbal (l_rot (n, l, r))";
 | 
|  |     49 | by (case_tac "r" 1); 
 | 
|  |     50 |  by (Asm_simp_tac 1);
 | 
|  |     51 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     52 | qed_spec_mp "isbal_l_rot";
 | 
|  |     53 | 
 | 
|  |     54 | 
 | 
|  |     55 | (* lemmas about height after rotation *)
 | 
|  |     56 | 
 | 
|  |     57 | Goalw [bal_def]
 | 
|  |     58 | "bal l = Right --> height l = Suc(Suc(height r))   \
 | 
|  |     59 | \ -->     Suc(height (lr_rot (n, l, r)))  = height (MKT n l r) ";
 | 
|  |     60 | by (case_tac "l" 1); 
 | 
|  |     61 |  by (Asm_simp_tac 1);
 | 
|  |     62 | by (case_tac "tree2" 1);
 | 
|  |     63 |  by (Asm_simp_tac 1);
 | 
|  |     64 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     65 | qed_spec_mp "height_lr_rot";
 | 
|  |     66 | 
 | 
|  |     67 | 
 | 
|  |     68 | Goalw [bal_def]
 | 
|  |     69 | "height l = Suc(Suc(height r)) --> bal l ~= Right \
 | 
|  |     70 | \ --> Suc(height (r_rot (n, l, r))) = height (MKT n l r) | \
 | 
|  |     71 | \         height (r_rot (n, l, r))  = height (MKT n l r)";
 | 
|  |     72 | by (case_tac "l" 1); 
 | 
|  |     73 |  by (Asm_simp_tac 1);
 | 
|  |     74 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     75 | qed_spec_mp "height_r_rot";
 | 
|  |     76 | 
 | 
|  |     77 | 
 | 
|  |     78 | Goalw [l_bal_def]
 | 
|  |     79 | "height l = Suc(Suc(height r))  \
 | 
|  |     80 | \ --> Suc(height (l_bal n l r)) = height (MKT n l r) | \
 | 
|  |     81 | \         height (l_bal n l r)  = height (MKT n l r)";
 | 
|  |     82 | by (case_tac "bal l = Right" 1);
 | 
|  |     83 |  by (fast_tac (claset() addDs [height_lr_rot] addss simpset()) 1);
 | 
|  |     84 | by (fast_tac (claset() addDs [height_r_rot] addss simpset()) 1);
 | 
|  |     85 | qed_spec_mp "height_l_bal";
 | 
|  |     86 | 
 | 
|  |     87 | 
 | 
|  |     88 | Goalw [bal_def]
 | 
|  |     89 | "height r = Suc(Suc(height l)) --> bal r = Left \
 | 
|  |     90 | \ -->     Suc(height (rl_rot (n, l, r)))  = height (MKT n l r)";
 | 
|  |     91 | by (case_tac "r" 1); 
 | 
|  |     92 |  by (Asm_simp_tac 1);
 | 
|  |     93 | by (case_tac "tree1" 1);
 | 
|  |     94 |  by (Asm_simp_tac 1);
 | 
|  |     95 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |     96 | qed_spec_mp "height_rl_rot";
 | 
|  |     97 | 
 | 
|  |     98 | 
 | 
|  |     99 | Goalw [bal_def]
 | 
|  |    100 | "height r = Suc(Suc(height l)) --> bal r ~= Left \
 | 
|  |    101 | \ --> Suc(height (l_rot (n, l, r))) = height (MKT n l r) | \
 | 
|  |    102 | \         height (l_rot (n, l, r))  = height (MKT n l r)";
 | 
|  |    103 | by (case_tac "r" 1); 
 | 
|  |    104 |  by (Asm_simp_tac 1);
 | 
|  |    105 | by (asm_simp_tac (simpset() addsimps [max_def]) 1);
 | 
|  |    106 | qed_spec_mp "height_l_rot";
 | 
|  |    107 | 
 | 
|  |    108 | 
 | 
|  |    109 | Goalw [r_bal_def]
 | 
|  |    110 | "height r = Suc(Suc(height l))  \
 | 
|  |    111 | \ --> Suc(height (r_bal n l r)) = height (MKT n l r) | \
 | 
|  |    112 | \         height (r_bal n l r)  = height (MKT n l r)";
 | 
|  |    113 | by (case_tac "bal r = Left" 1);
 | 
|  |    114 |  by (fast_tac (claset() addDs [height_rl_rot] addss simpset()) 1);
 | 
|  |    115 | by (fast_tac (claset() addDs [height_l_rot] addss simpset()) 1);
 | 
|  |    116 | qed_spec_mp "height_r_bal";
 | 
|  |    117 | 
 | 
|  |    118 | 
 | 
|  |    119 | (* lemma about height after insert *)
 | 
|  |    120 | Goal
 | 
|  |    121 | "isbal t  \
 | 
|  |    122 | \ --> height (insert x t) = height t | height (insert x t) = Suc(height t)";
 | 
|  |    123 | by (induct_tac "t" 1);
 | 
|  |    124 |  by (Simp_tac 1);
 | 
|  |    125 | by (case_tac "x=nat" 1);
 | 
|  |    126 |  by (Asm_simp_tac 1);
 | 
|  |    127 | by (case_tac "x<nat" 1);
 | 
|  |    128 |  by (case_tac "height (insert x tree1) = Suc (Suc (height tree2))" 1);
 | 
|  |    129 |   by (forw_inst_tac [("n","nat")]  height_l_bal 1);  
 | 
|  |    130 |   by (asm_full_simp_tac (simpset() addsimps [max_def]) 1);
 | 
| 12486 |    131 |   by (fast_tac (claset() addss simpset()) 1);
 | 
| 8797 |    132 |  by (asm_full_simp_tac (simpset() addsimps [max_def]) 1);
 | 
| 12486 |    133 |  by (fast_tac (claset() addss simpset()) 1);
 | 
| 8797 |    134 | by (case_tac "height (insert x tree2) = Suc (Suc (height tree1))" 1);
 | 
|  |    135 |  by (forw_inst_tac [("n","nat")]  height_r_bal 1); 
 | 
|  |    136 |  by (asm_full_simp_tac (simpset() addsimps [max_def]) 1);
 | 
| 12486 |    137 |  by (fast_tac (claset() addss simpset()) 1);
 | 
| 8797 |    138 | by (asm_full_simp_tac (simpset() addsimps [max_def]) 1);
 | 
| 12486 |    139 | by (fast_tac (claset() addss simpset()) 1);
 | 
| 8797 |    140 | qed_spec_mp "height_insert";
 | 
|  |    141 | 
 | 
|  |    142 | 
 | 
|  |    143 | Goal
 | 
|  |    144 | "!!x. [| height (insert x l) ~= Suc(Suc(height r)); isbal (insert x l); isbal (MKT n l r) |] \
 | 
|  |    145 | \ ==> isbal (MKT n (insert x l) r)";
 | 
|  |    146 | by (cut_inst_tac [("x","x"),("t","l")] height_insert 1);
 | 
|  |    147 |  by (Asm_full_simp_tac 1); 
 | 
|  |    148 | by (fast_tac (claset() addss simpset()) 1);
 | 
|  |    149 | qed "isbal_insert_left";
 | 
|  |    150 | 
 | 
|  |    151 | 
 | 
|  |    152 | Goal
 | 
|  |    153 | "!!x. [| height (insert x r) ~= Suc(Suc(height l)); isbal (insert x r); isbal (MKT n l r) |] \
 | 
|  |    154 | \ ==> isbal (MKT n l (insert x r))";
 | 
|  |    155 | by (cut_inst_tac [("x","x"),("t","r")] height_insert 1);
 | 
|  |    156 |  by (Asm_full_simp_tac 1); 
 | 
|  |    157 | by (fast_tac (claset() addss simpset()) 1);
 | 
|  |    158 | qed "isbal_insert_right";
 | 
|  |    159 | 
 | 
|  |    160 | (* insert-operation preserves isbal property *)
 | 
|  |    161 | 
 | 
|  |    162 | Goal
 | 
|  |    163 | "isbal t --> isbal(insert x t)";
 | 
|  |    164 | by (induct_tac "t" 1);
 | 
|  |    165 |  by (Simp_tac 1);
 | 
|  |    166 | by (case_tac "x=nat" 1);
 | 
|  |    167 |  by (Asm_simp_tac 1); 
 | 
|  |    168 | by (case_tac "x<nat" 1);
 | 
|  |    169 |  by (case_tac "height (insert x tree1) = Suc (Suc (height tree2))" 1);
 | 
|  |    170 |   by (case_tac "bal (insert x tree1) = Right" 1);
 | 
|  |    171 |    by (fast_tac (claset() addIs [isbal_lr_rot] addss (simpset() 
 | 
|  |    172 | 	addsimps [l_bal_def])) 1);  
 | 
|  |    173 |   by (fast_tac (claset() addIs [isbal_r_rot] addss (simpset() 
 | 
|  |    174 | 	addsimps [l_bal_def])) 1);  
 | 
|  |    175 |  by (Clarify_tac 1);
 | 
| 12486 |    176 |  by (ftac isbal_insert_left 1);
 | 
| 8797 |    177 |    by (Asm_full_simp_tac 1); 
 | 
| 12486 |    178 |   by (assume_tac 1);
 | 
| 8797 |    179 |  by (Asm_full_simp_tac 1); 
 | 
|  |    180 | by (case_tac "height (insert x tree2) = Suc (Suc (height tree1))" 1);
 | 
|  |    181 |  by (case_tac "bal (insert x tree2) = Left" 1);
 | 
|  |    182 |   by (fast_tac (claset() addIs [isbal_rl_rot] addss (simpset() 
 | 
|  |    183 | 	addsimps [r_bal_def])) 1);  
 | 
|  |    184 |  by (fast_tac (claset() addIs [isbal_l_rot] addss (simpset() 
 | 
|  |    185 | 	addsimps [r_bal_def])) 1);  
 | 
|  |    186 | by (Clarify_tac 1);
 | 
| 12486 |    187 | by (ftac isbal_insert_right 1);
 | 
| 8797 |    188 |    by (Asm_full_simp_tac 1); 
 | 
| 12486 |    189 |   by (assume_tac 1);
 | 
| 8797 |    190 |  by (Asm_full_simp_tac 1); 
 | 
|  |    191 | qed_spec_mp "isbal_insert";
 | 
|  |    192 | 
 | 
|  |    193 | 
 | 
|  |    194 | (******************************    isin    **********************************)
 | 
|  |    195 | 
 | 
|  |    196 | 
 | 
|  |    197 | (* rotations preserve isin property *)
 | 
|  |    198 | 
 | 
|  |    199 | Goalw [bal_def]
 | 
|  |    200 | "height l = Suc(Suc(height r)) --> bal l = Right  \
 | 
|  |    201 | \ --> isin x (lr_rot (n, l, r)) = isin x (MKT n l r)";
 | 
|  |    202 | by (case_tac "l" 1); 
 | 
|  |    203 |  by (Asm_simp_tac 1);
 | 
|  |    204 | by (case_tac "tree2" 1);
 | 
|  |    205 |  by (Asm_simp_tac 1);
 | 
|  |    206 | by (fast_tac (claset() addss simpset()) 1);
 | 
|  |    207 | qed_spec_mp "isin_lr_rot";
 | 
|  |    208 | 
 | 
|  |    209 | 
 | 
|  |    210 | Goalw [bal_def]
 | 
|  |    211 | "height l = Suc(Suc(height r)) --> bal l ~= Right  \
 | 
|  |    212 | \ --> isin x (r_rot (n, l, r)) = isin x (MKT n l r)";
 | 
|  |    213 | by (case_tac "l" 1); 
 | 
|  |    214 |  by (Asm_simp_tac 1);
 | 
|  |    215 | by (fast_tac (claset() addss simpset()) 1);
 | 
|  |    216 | qed_spec_mp "isin_r_rot";
 | 
|  |    217 | 
 | 
|  |    218 | 
 | 
|  |    219 | Goalw [bal_def]
 | 
|  |    220 | "height r = Suc(Suc(height l)) --> bal r = Left  \
 | 
|  |    221 | \ --> isin x (rl_rot (n, l, r)) = isin x (MKT n l r)";
 | 
|  |    222 | by (case_tac "r" 1); 
 | 
|  |    223 |  by (Asm_simp_tac 1);
 | 
|  |    224 | by (case_tac "tree1" 1);
 | 
|  |    225 |  by (asm_simp_tac (simpset() addsimps [max_def,le_def]) 1);
 | 
|  |    226 | by (fast_tac (claset() addss simpset()) 1);
 | 
|  |    227 | qed_spec_mp "isin_rl_rot";
 | 
|  |    228 | 
 | 
|  |    229 | 
 | 
|  |    230 | Goalw [bal_def]
 | 
|  |    231 | "height r = Suc(Suc(height l)) --> bal r ~= Left  \
 | 
|  |    232 | \ --> isin x (l_rot (n, l, r)) = isin x (MKT n l r)";
 | 
|  |    233 | by (case_tac "r" 1); 
 | 
|  |    234 |  by (Asm_simp_tac 1);
 | 
|  |    235 | by (fast_tac (claset() addss simpset()) 1);
 | 
|  |    236 | qed_spec_mp "isin_l_rot";
 | 
|  |    237 | 
 | 
|  |    238 | 
 | 
|  |    239 | (* isin insert *)
 | 
|  |    240 | 
 | 
|  |    241 | Goal
 | 
|  |    242 | "isin y (insert x t) = (y=x | isin y t)";
 | 
|  |    243 | by (induct_tac "t" 1);
 | 
|  |    244 |  by (Asm_simp_tac 1);
 | 
| 12486 |    245 | by (asm_simp_tac (simpset() addsimps [l_bal_def,isin_lr_rot,isin_r_rot,
 | 
| 8797 |    246 | 	r_bal_def,isin_rl_rot,isin_l_rot]) 1);
 | 
| 12486 |    247 | by (Blast_tac 1);
 | 
| 8797 |    248 | qed "isin_insert";
 | 
|  |    249 | 
 | 
|  |    250 | 
 | 
|  |    251 | (******************************    isord    **********************************)
 | 
|  |    252 | 
 | 
|  |    253 | (* rotations preserve isord property *)
 | 
|  |    254 | 
 | 
|  |    255 | Goalw [bal_def]
 | 
|  |    256 | "height l = Suc(Suc(height r)) --> bal l = Right --> isord (MKT n l r) \
 | 
|  |    257 | \ --> isord (lr_rot (n, l, r))";
 | 
|  |    258 | by (case_tac "l" 1); 
 | 
|  |    259 |  by (Asm_simp_tac 1);
 | 
|  |    260 | by (case_tac "tree2" 1);
 | 
|  |    261 |  by (Asm_simp_tac 1);
 | 
|  |    262 | by (Asm_simp_tac 1);
 | 
| 12486 |    263 | by (blast_tac (claset() addIs [less_trans])1);
 | 
| 8797 |    264 | qed_spec_mp "isord_lr_rot";
 | 
|  |    265 | 
 | 
|  |    266 | 
 | 
|  |    267 | Goalw [bal_def]
 | 
|  |    268 | "height l = Suc(Suc(height r)) --> bal l ~= Right --> isord (MKT n l r) \
 | 
|  |    269 | \ --> isord (r_rot (n, l, r))";
 | 
|  |    270 | by (case_tac "l" 1); 
 | 
|  |    271 |  by (Asm_simp_tac 1);
 | 
|  |    272 | by (auto_tac (claset() addIs [less_trans],simpset()));
 | 
|  |    273 | qed_spec_mp "isord_r_rot";
 | 
|  |    274 | 
 | 
|  |    275 | 
 | 
|  |    276 | Goalw [bal_def]
 | 
|  |    277 | "height r = Suc(Suc(height l)) --> bal r = Left --> isord (MKT n l r) \
 | 
|  |    278 | \ --> isord (rl_rot (n, l, r))";
 | 
|  |    279 | by (case_tac "r" 1); 
 | 
|  |    280 |  by (Asm_simp_tac 1);
 | 
|  |    281 | by (case_tac "tree1" 1);
 | 
|  |    282 |  by (asm_simp_tac (simpset() addsimps [le_def]) 1);
 | 
|  |    283 | by (Asm_simp_tac 1);
 | 
| 12486 |    284 | by (blast_tac (claset() addIs [less_trans])1);
 | 
| 8797 |    285 | qed_spec_mp "isord_rl_rot";
 | 
|  |    286 | 
 | 
|  |    287 | 
 | 
|  |    288 | Goalw [bal_def]
 | 
|  |    289 | "height r = Suc(Suc(height l)) --> bal r ~= Left --> isord (MKT n l r)  \
 | 
|  |    290 | \ --> isord (l_rot (n, l, r))";
 | 
|  |    291 | by (case_tac "r" 1); 
 | 
|  |    292 |  by (Asm_simp_tac 1);
 | 
|  |    293 | by (Asm_simp_tac 1);
 | 
| 12486 |    294 | by (blast_tac (claset() addIs [less_trans])1);
 | 
| 8797 |    295 | qed_spec_mp "isord_l_rot";
 | 
|  |    296 | 
 | 
|  |    297 | (* insert operation presreves isord property *)
 | 
|  |    298 | 
 | 
|  |    299 | Goal
 | 
|  |    300 | "isord t --> isord(insert x t)";
 | 
|  |    301 | by (induct_tac "t" 1);
 | 
|  |    302 |  by (Simp_tac 1);
 | 
|  |    303 | by (cut_inst_tac [("m","x"),("n","nat")] less_linear 1);
 | 
|  |    304 |  by (fast_tac (claset() addss (simpset() addsimps [l_bal_def,isord_lr_rot,
 | 
|  |    305 | 	isord_r_rot,r_bal_def,isord_l_rot,isord_rl_rot,isin_insert])) 1);  
 | 
|  |    306 | qed_spec_mp "isord_insert";
 |