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