src/HOL/Data_Structures/Brother12_Set.thy
author nipkow
Sat Apr 21 08:41:42 2018 +0200 (14 months ago)
changeset 68020 6aade817bee5
parent 67965 aaa31cd0caef
child 68431 b294e095f64c
permissions -rw-r--r--
del_min -> split_min
nipkow@63411
     1
(* Author: Tobias Nipkow, Daniel Stüwe *)
nipkow@61784
     2
nipkow@62130
     3
section \<open>1-2 Brother Tree Implementation of Sets\<close>
nipkow@61784
     4
nipkow@61784
     5
theory Brother12_Set
nipkow@61784
     6
imports
nipkow@61784
     7
  Cmp
nipkow@67965
     8
  Set_Specs
wenzelm@66453
     9
  "HOL-Number_Theory.Fib"
nipkow@61784
    10
begin
nipkow@61784
    11
nipkow@61784
    12
subsection \<open>Data Type and Operations\<close>
nipkow@61784
    13
nipkow@61784
    14
datatype 'a bro =
nipkow@61784
    15
  N0 |
nipkow@61784
    16
  N1 "'a bro" |
nipkow@61784
    17
  N2 "'a bro" 'a "'a bro" |
nipkow@61784
    18
  (* auxiliary constructors: *)
nipkow@61784
    19
  L2 'a |
nipkow@61784
    20
  N3 "'a bro" 'a "'a bro" 'a "'a bro"
nipkow@61784
    21
nipkow@61784
    22
fun inorder :: "'a bro \<Rightarrow> 'a list" where
nipkow@61784
    23
"inorder N0 = []" |
nipkow@61784
    24
"inorder (N1 t) = inorder t" |
nipkow@61784
    25
"inorder (N2 l a r) = inorder l @ a # inorder r" |
nipkow@61784
    26
"inorder (L2 a) = [a]" |
nipkow@61784
    27
"inorder (N3 t1 a1 t2 a2 t3) = inorder t1 @ a1 # inorder t2 @ a2 # inorder t3"
nipkow@61784
    28
nipkow@63411
    29
fun isin :: "'a bro \<Rightarrow> 'a::linorder \<Rightarrow> bool" where
nipkow@61784
    30
"isin N0 x = False" |
nipkow@61784
    31
"isin (N1 t) x = isin t x" |
nipkow@61784
    32
"isin (N2 l a r) x =
nipkow@61784
    33
  (case cmp x a of
nipkow@61784
    34
     LT \<Rightarrow> isin l x |
nipkow@61784
    35
     EQ \<Rightarrow> True |
nipkow@61784
    36
     GT \<Rightarrow> isin r x)"
nipkow@61784
    37
nipkow@61784
    38
fun n1 :: "'a bro \<Rightarrow> 'a bro" where
nipkow@61784
    39
"n1 (L2 a) = N2 N0 a N0" |
nipkow@61784
    40
"n1 (N3 t1 a1 t2 a2 t3) = N2 (N2 t1 a1 t2) a2 (N1 t3)" |
nipkow@61784
    41
"n1 t = N1 t"
nipkow@61784
    42
nipkow@61784
    43
hide_const (open) insert
nipkow@61784
    44
nipkow@61784
    45
locale insert
nipkow@61784
    46
begin
nipkow@61784
    47
nipkow@61784
    48
fun n2 :: "'a bro \<Rightarrow> 'a \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
nipkow@61784
    49
"n2 (L2 a1) a2 t = N3 N0 a1 N0 a2 t" |
nipkow@61784
    50
"n2 (N3 t1 a1 t2 a2 t3) a3 (N1 t4) = N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)" |
nipkow@61784
    51
"n2 (N3 t1 a1 t2 a2 t3) a3 t4 = N3 (N2 t1 a1 t2) a2 (N1 t3) a3 t4" |
nipkow@61784
    52
"n2 t1 a1 (L2 a2) = N3 t1 a1 N0 a2 N0" |
nipkow@61784
    53
"n2 (N1 t1) a1 (N3 t2 a2 t3 a3 t4) = N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)" |
nipkow@61784
    54
"n2 t1 a1 (N3 t2 a2 t3 a3 t4) = N3 t1 a1 (N1 t2) a2 (N2 t3 a3 t4)" |
nipkow@61784
    55
"n2 t1 a t2 = N2 t1 a t2"
nipkow@61784
    56
nipkow@63411
    57
fun ins :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
nipkow@61789
    58
"ins x N0 = L2 x" |
nipkow@61789
    59
"ins x (N1 t) = n1 (ins x t)" |
nipkow@61789
    60
"ins x (N2 l a r) =
nipkow@61789
    61
  (case cmp x a of
nipkow@61789
    62
     LT \<Rightarrow> n2 (ins x l) a r |
nipkow@61789
    63
     EQ \<Rightarrow> N2 l a r |
nipkow@61789
    64
     GT \<Rightarrow> n2 l a (ins x r))"
nipkow@61784
    65
nipkow@61784
    66
fun tree :: "'a bro \<Rightarrow> 'a bro" where
nipkow@61784
    67
"tree (L2 a) = N2 N0 a N0" |
nipkow@61784
    68
"tree (N3 t1 a1 t2 a2 t3) = N2 (N2 t1 a1 t2) a2 (N1 t3)" |
nipkow@61784
    69
"tree t = t"
nipkow@61784
    70
nipkow@63411
    71
definition insert :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
nipkow@61784
    72
"insert x t = tree(ins x t)"
nipkow@61784
    73
nipkow@61784
    74
end
nipkow@61784
    75
nipkow@61784
    76
locale delete
nipkow@61784
    77
begin
nipkow@61784
    78
nipkow@61784
    79
fun n2 :: "'a bro \<Rightarrow> 'a \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
nipkow@61784
    80
"n2 (N1 t1) a1 (N1 t2) = N1 (N2 t1 a1 t2)" |
nipkow@61784
    81
"n2 (N1 (N1 t1)) a1 (N2 (N1 t2) a2 (N2 t3 a3 t4)) =
nipkow@61784
    82
  N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
nipkow@61784
    83
"n2 (N1 (N1 t1)) a1 (N2 (N2 t2 a2 t3) a3 (N1 t4)) =
nipkow@61784
    84
  N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
nipkow@61784
    85
"n2 (N1 (N1 t1)) a1 (N2 (N2 t2 a2 t3) a3 (N2 t4 a4 t5)) =
nipkow@61784
    86
  N2 (N2 (N1 t1) a1 (N2 t2 a2 t3)) a3 (N1 (N2 t4 a4 t5))" |
nipkow@61784
    87
"n2 (N2 (N1 t1) a1 (N2 t2 a2 t3)) a3 (N1 (N1 t4)) =
nipkow@61784
    88
  N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
nipkow@61784
    89
"n2 (N2 (N2 t1 a1 t2) a2 (N1 t3)) a3 (N1 (N1 t4)) =
nipkow@61784
    90
  N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
nipkow@61784
    91
"n2 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)) a5 (N1 (N1 t5)) =
nipkow@61784
    92
  N2 (N1 (N2 t1 a1 t2)) a2 (N2 (N2 t3 a3 t4) a5 (N1 t5))" |
nipkow@61784
    93
"n2 t1 a1 t2 = N2 t1 a1 t2"
nipkow@61784
    94
nipkow@68020
    95
fun split_min :: "'a bro \<Rightarrow> ('a \<times> 'a bro) option" where
nipkow@68020
    96
"split_min N0 = None" |
nipkow@68020
    97
"split_min (N1 t) =
nipkow@68020
    98
  (case split_min t of
nipkow@61784
    99
     None \<Rightarrow> None |
nipkow@61784
   100
     Some (a, t') \<Rightarrow> Some (a, N1 t'))" |
nipkow@68020
   101
"split_min (N2 t1 a t2) =
nipkow@68020
   102
  (case split_min t1 of
nipkow@61784
   103
     None \<Rightarrow> Some (a, N1 t2) |
nipkow@61784
   104
     Some (b, t1') \<Rightarrow> Some (b, n2 t1' a t2))"
nipkow@61784
   105
nipkow@63411
   106
fun del :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
nipkow@61784
   107
"del _ N0         = N0" |
nipkow@61784
   108
"del x (N1 t)     = N1 (del x t)" |
nipkow@61784
   109
"del x (N2 l a r) =
nipkow@61784
   110
  (case cmp x a of
nipkow@61784
   111
     LT \<Rightarrow> n2 (del x l) a r |
nipkow@61784
   112
     GT \<Rightarrow> n2 l a (del x r) |
nipkow@68020
   113
     EQ \<Rightarrow> (case split_min r of
nipkow@61784
   114
              None \<Rightarrow> N1 l |
nipkow@61784
   115
              Some (b, r') \<Rightarrow> n2 l b r'))"
nipkow@61784
   116
nipkow@61784
   117
fun tree :: "'a bro \<Rightarrow> 'a bro" where
nipkow@61784
   118
"tree (N1 t) = t" |
nipkow@61784
   119
"tree t = t"
nipkow@61784
   120
nipkow@63411
   121
definition delete :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
nipkow@61784
   122
"delete a t = tree (del a t)"
nipkow@61784
   123
nipkow@61784
   124
end
nipkow@61784
   125
nipkow@61784
   126
subsection \<open>Invariants\<close>
nipkow@61784
   127
nipkow@61784
   128
fun B :: "nat \<Rightarrow> 'a bro set"
nipkow@61784
   129
and U :: "nat \<Rightarrow> 'a bro set" where
nipkow@61784
   130
"B 0 = {N0}" |
nipkow@61784
   131
"B (Suc h) = { N2 t1 a t2 | t1 a t2. 
nipkow@61784
   132
  t1 \<in> B h \<union> U h \<and> t2 \<in> B h \<or> t1 \<in> B h \<and> t2 \<in> B h \<union> U h}" |
nipkow@61784
   133
"U 0 = {}" |
nipkow@61784
   134
"U (Suc h) = N1 ` B h"
nipkow@61784
   135
nipkow@61784
   136
abbreviation "T h \<equiv> B h \<union> U h"
nipkow@61784
   137
nipkow@61784
   138
fun Bp :: "nat \<Rightarrow> 'a bro set" where
nipkow@61784
   139
"Bp 0 = B 0 \<union> L2 ` UNIV" |
nipkow@61784
   140
"Bp (Suc 0) = B (Suc 0) \<union> {N3 N0 a N0 b N0|a b. True}" |
nipkow@61784
   141
"Bp (Suc(Suc h)) = B (Suc(Suc h)) \<union>
nipkow@61784
   142
  {N3 t1 a t2 b t3 | t1 a t2 b t3. t1 \<in> B (Suc h) \<and> t2 \<in> U (Suc h) \<and> t3 \<in> B (Suc h)}"
nipkow@61784
   143
nipkow@61784
   144
fun Um :: "nat \<Rightarrow> 'a bro set" where
nipkow@61784
   145
"Um 0 = {}" |
nipkow@61784
   146
"Um (Suc h) = N1 ` T h"
nipkow@61784
   147
nipkow@61784
   148
nipkow@61784
   149
subsection "Functional Correctness Proofs"
nipkow@61784
   150
nipkow@61784
   151
subsubsection "Proofs for isin"
nipkow@61784
   152
nipkow@67929
   153
lemma isin_set:
nipkow@67929
   154
  "t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> set(inorder t))"
nipkow@67929
   155
by(induction h arbitrary: t) (fastforce simp: isin_simps split: if_splits)+
nipkow@61784
   156
nipkow@61784
   157
subsubsection "Proofs for insertion"
nipkow@61784
   158
nipkow@61784
   159
lemma inorder_n1: "inorder(n1 t) = inorder t"
nipkow@62526
   160
by(cases t rule: n1.cases) (auto simp: sorted_lems)
nipkow@61784
   161
nipkow@61784
   162
context insert
nipkow@61784
   163
begin
nipkow@61784
   164
nipkow@61784
   165
lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r"
nipkow@61784
   166
by(cases "(l,a,r)" rule: n2.cases) (auto simp: sorted_lems)
nipkow@61784
   167
nipkow@61784
   168
lemma inorder_tree: "inorder(tree t) = inorder t"
nipkow@61784
   169
by(cases t) auto
nipkow@61784
   170
nipkow@61784
   171
lemma inorder_ins: "t \<in> T h \<Longrightarrow>
nipkow@61784
   172
  sorted(inorder t) \<Longrightarrow> inorder(ins a t) = ins_list a (inorder t)"
nipkow@61784
   173
by(induction h arbitrary: t) (auto simp: ins_list_simps inorder_n1 inorder_n2)
nipkow@61784
   174
nipkow@61784
   175
lemma inorder_insert: "t \<in> T h \<Longrightarrow>
nipkow@61784
   176
  sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
nipkow@61784
   177
by(simp add: insert_def inorder_ins inorder_tree)
nipkow@61784
   178
nipkow@61784
   179
end
nipkow@61784
   180
nipkow@61784
   181
subsubsection \<open>Proofs for deletion\<close>
nipkow@61784
   182
nipkow@61784
   183
context delete
nipkow@61784
   184
begin
nipkow@61784
   185
nipkow@61784
   186
lemma inorder_tree: "inorder(tree t) = inorder t"
nipkow@61784
   187
by(cases t) auto
nipkow@61784
   188
nipkow@61784
   189
lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r"
nipkow@62526
   190
by(cases "(l,a,r)" rule: n2.cases) (auto)
nipkow@61784
   191
nipkow@68020
   192
lemma inorder_split_min:
nipkow@68020
   193
  "t \<in> T h \<Longrightarrow> (split_min t = None \<longleftrightarrow> inorder t = []) \<and>
nipkow@68020
   194
  (split_min t = Some(a,t') \<longrightarrow> inorder t = a # inorder t')"
nipkow@61784
   195
by(induction h arbitrary: t a t') (auto simp: inorder_n2 split: option.splits)
nipkow@61784
   196
nipkow@61784
   197
lemma inorder_del:
nipkow@61792
   198
  "t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(del x t) = del_list x (inorder t)"
nipkow@61792
   199
by(induction h arbitrary: t) (auto simp: del_list_simps inorder_n2
nipkow@68020
   200
     inorder_split_min[OF UnI1] inorder_split_min[OF UnI2] split: option.splits)
nipkow@61792
   201
nipkow@61792
   202
lemma inorder_delete:
nipkow@61792
   203
  "t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@61792
   204
by(simp add: delete_def inorder_del inorder_tree)
nipkow@61784
   205
nipkow@61784
   206
end
nipkow@61784
   207
nipkow@61784
   208
nipkow@61784
   209
subsection \<open>Invariant Proofs\<close>
nipkow@61784
   210
nipkow@61789
   211
subsubsection \<open>Proofs for insertion\<close>
nipkow@61784
   212
nipkow@61784
   213
lemma n1_type: "t \<in> Bp h \<Longrightarrow> n1 t \<in> T (Suc h)"
nipkow@61784
   214
by(cases h rule: Bp.cases) auto
nipkow@61784
   215
nipkow@61784
   216
context insert
nipkow@61784
   217
begin
nipkow@61784
   218
nipkow@61809
   219
lemma tree_type: "t \<in> Bp h \<Longrightarrow> tree t \<in> B h \<union> B (Suc h)"
nipkow@61784
   220
by(cases h rule: Bp.cases) auto
nipkow@61784
   221
nipkow@61784
   222
lemma n2_type:
nipkow@61784
   223
  "(t1 \<in> Bp h \<and> t2 \<in> T h \<longrightarrow> n2 t1 a t2 \<in> Bp (Suc h)) \<and>
nipkow@61784
   224
   (t1 \<in> T h \<and> t2 \<in> Bp h \<longrightarrow> n2 t1 a t2 \<in> Bp (Suc h))"
nipkow@61784
   225
apply(cases h rule: Bp.cases)
nipkow@61784
   226
apply (auto)[2]
nipkow@61784
   227
apply(rule conjI impI | erule conjE exE imageE | simp | erule disjE)+
nipkow@61784
   228
done
nipkow@61784
   229
nipkow@61784
   230
lemma Bp_if_B: "t \<in> B h \<Longrightarrow> t \<in> Bp h"
nipkow@61784
   231
by (cases h rule: Bp.cases) simp_all
nipkow@61784
   232
wenzelm@67406
   233
text\<open>An automatic proof:\<close>
nipkow@61784
   234
nipkow@61784
   235
lemma
nipkow@61784
   236
  "(t \<in> B h \<longrightarrow> ins x t \<in> Bp h) \<and> (t \<in> U h \<longrightarrow> ins x t \<in> T h)"
nipkow@61784
   237
apply(induction h arbitrary: t)
nipkow@61784
   238
 apply (simp)
nipkow@61784
   239
apply (fastforce simp: Bp_if_B n2_type dest: n1_type)
nipkow@61784
   240
done
nipkow@61784
   241
wenzelm@67406
   242
text\<open>A detailed proof:\<close>
nipkow@61784
   243
nipkow@61784
   244
lemma ins_type:
nipkow@61784
   245
shows "t \<in> B h \<Longrightarrow> ins x t \<in> Bp h" and "t \<in> U h \<Longrightarrow> ins x t \<in> T h"
nipkow@61784
   246
proof(induction h arbitrary: t)
nipkow@61784
   247
  case 0
nipkow@61784
   248
  { case 1 thus ?case by simp
nipkow@61784
   249
  next
nipkow@61784
   250
    case 2 thus ?case by simp }
nipkow@61784
   251
next
nipkow@61784
   252
  case (Suc h)
nipkow@61784
   253
  { case 1
nipkow@61784
   254
    then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and
nipkow@61784
   255
      t1: "t1 \<in> T h" and t2: "t2 \<in> T h" and t12: "t1 \<in> B h \<or> t2 \<in> B h"
nipkow@61784
   256
      by auto
nipkow@67040
   257
    have ?case if "x < a"
nipkow@67040
   258
    proof -
nipkow@67040
   259
      have "n2 (ins x t1) a t2 \<in> Bp (Suc h)"
nipkow@61784
   260
      proof cases
nipkow@61784
   261
        assume "t1 \<in> B h"
nipkow@61784
   262
        with t2 show ?thesis by (simp add: Suc.IH(1) n2_type)
nipkow@61784
   263
      next
nipkow@61784
   264
        assume "t1 \<notin> B h"
nipkow@61784
   265
        hence 1: "t1 \<in> U h" and 2: "t2 \<in> B h" using t1 t12 by auto
nipkow@61784
   266
        show ?thesis by (metis Suc.IH(2)[OF 1] Bp_if_B[OF 2] n2_type)
nipkow@61784
   267
      qed
wenzelm@67406
   268
      with \<open>x < a\<close> show ?case by simp
nipkow@67040
   269
    qed
nipkow@61784
   270
    moreover
nipkow@67040
   271
    have ?case if "a < x"
nipkow@67040
   272
    proof -
nipkow@67040
   273
      have "n2 t1 a (ins x t2) \<in> Bp (Suc h)"
nipkow@61784
   274
      proof cases
nipkow@61784
   275
        assume "t2 \<in> B h"
nipkow@61784
   276
        with t1 show ?thesis by (simp add: Suc.IH(1) n2_type)
nipkow@61784
   277
      next
nipkow@61784
   278
        assume "t2 \<notin> B h"
nipkow@61784
   279
        hence 1: "t1 \<in> B h" and 2: "t2 \<in> U h" using t2 t12 by auto
nipkow@61784
   280
        show ?thesis by (metis Bp_if_B[OF 1] Suc.IH(2)[OF 2] n2_type)
nipkow@61784
   281
      qed
wenzelm@67406
   282
      with \<open>a < x\<close> show ?case by simp
nipkow@67040
   283
    qed
nipkow@67040
   284
    moreover
nipkow@67040
   285
    have ?case if "x = a"
nipkow@67040
   286
    proof -
nipkow@61784
   287
      from 1 have "t \<in> Bp (Suc h)" by(rule Bp_if_B)
wenzelm@67406
   288
      thus "?case" using \<open>x = a\<close> by simp
nipkow@67040
   289
    qed
nipkow@61784
   290
    ultimately show ?case by auto
nipkow@61784
   291
  next
nipkow@61784
   292
    case 2 thus ?case using Suc(1) n1_type by fastforce }
nipkow@61784
   293
qed
nipkow@61784
   294
nipkow@61784
   295
lemma insert_type:
nipkow@61809
   296
  "t \<in> B h \<Longrightarrow> insert x t \<in> B h \<union> B (Suc h)"
nipkow@61809
   297
unfolding insert_def by (metis ins_type(1) tree_type)
nipkow@61784
   298
nipkow@61784
   299
end
nipkow@61784
   300
nipkow@61789
   301
subsubsection "Proofs for deletion"
nipkow@61784
   302
nipkow@61784
   303
lemma B_simps[simp]: 
nipkow@61784
   304
  "N1 t \<in> B h = False"
nipkow@61784
   305
  "L2 y \<in> B h = False"
nipkow@61784
   306
  "(N3 t1 a1 t2 a2 t3) \<in> B h = False"
nipkow@61784
   307
  "N0 \<in> B h \<longleftrightarrow> h = 0"
nipkow@61784
   308
by (cases h, auto)+
nipkow@61784
   309
nipkow@61784
   310
context delete
nipkow@61784
   311
begin
nipkow@61784
   312
nipkow@61784
   313
lemma n2_type1:
nipkow@61784
   314
  "\<lbrakk>t1 \<in> Um h; t2 \<in> B h\<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)"
nipkow@61784
   315
apply(cases h rule: Bp.cases)
nipkow@61784
   316
apply auto[2]
nipkow@61784
   317
apply(erule exE bexE conjE imageE | simp | erule disjE)+
nipkow@61784
   318
done
nipkow@61784
   319
nipkow@61784
   320
lemma n2_type2:
nipkow@61784
   321
  "\<lbrakk>t1 \<in> B h ; t2 \<in> Um h \<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)"
nipkow@61784
   322
apply(cases h rule: Bp.cases)
nipkow@61784
   323
apply auto[2]
nipkow@61784
   324
apply(erule exE bexE conjE imageE | simp | erule disjE)+
nipkow@61784
   325
done
nipkow@61784
   326
nipkow@61784
   327
lemma n2_type3:
nipkow@61784
   328
  "\<lbrakk>t1 \<in> T h ; t2 \<in> T h \<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)"
nipkow@61784
   329
apply(cases h rule: Bp.cases)
nipkow@61784
   330
apply auto[2]
nipkow@61784
   331
apply(erule exE bexE conjE imageE | simp | erule disjE)+
nipkow@61784
   332
done
nipkow@61784
   333
nipkow@68020
   334
lemma split_minNoneN0: "\<lbrakk>t \<in> B h; split_min t = None\<rbrakk> \<Longrightarrow>  t = N0"
nipkow@61784
   335
by (cases t) (auto split: option.splits)
nipkow@61784
   336
nipkow@68020
   337
lemma split_minNoneN1 : "\<lbrakk>t \<in> U h; split_min t = None\<rbrakk> \<Longrightarrow> t = N1 N0"
nipkow@68020
   338
by (cases h) (auto simp: split_minNoneN0  split: option.splits)
nipkow@61784
   339
nipkow@68020
   340
lemma split_min_type:
nipkow@68020
   341
  "t \<in> B h \<Longrightarrow> split_min t = Some (a, t') \<Longrightarrow> t' \<in> T h"
nipkow@68020
   342
  "t \<in> U h \<Longrightarrow> split_min t = Some (a, t') \<Longrightarrow> t' \<in> Um h"
nipkow@61784
   343
proof (induction h arbitrary: t a t')
nipkow@61784
   344
  case (Suc h)
nipkow@61784
   345
  { case 1
nipkow@61784
   346
    then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and
nipkow@61784
   347
      t12: "t1 \<in> T h" "t2 \<in> T h" "t1 \<in> B h \<or> t2 \<in> B h"
nipkow@61784
   348
      by auto
nipkow@61784
   349
    show ?case
nipkow@68020
   350
    proof (cases "split_min t1")
nipkow@61784
   351
      case None
nipkow@61784
   352
      show ?thesis
nipkow@61784
   353
      proof cases
nipkow@61784
   354
        assume "t1 \<in> B h"
nipkow@68020
   355
        with split_minNoneN0[OF this None] 1 show ?thesis by(auto)
nipkow@61784
   356
      next
nipkow@61784
   357
        assume "t1 \<notin> B h"
nipkow@61784
   358
        thus ?thesis using 1 None by (auto)
nipkow@61784
   359
      qed
nipkow@61784
   360
    next
nipkow@61784
   361
      case [simp]: (Some bt')
nipkow@61784
   362
      obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce
nipkow@61784
   363
      show ?thesis
nipkow@61784
   364
      proof cases
nipkow@61784
   365
        assume "t1 \<in> B h"
nipkow@61784
   366
        from Suc.IH(1)[OF this] 1 have "t1' \<in> T h" by simp
nipkow@61784
   367
        from n2_type3[OF this t12(2)] 1 show ?thesis by auto
nipkow@61784
   368
      next
nipkow@61784
   369
        assume "t1 \<notin> B h"
nipkow@61784
   370
        hence t1: "t1 \<in> U h" and t2: "t2 \<in> B h" using t12 by auto
nipkow@61784
   371
        from Suc.IH(2)[OF t1] have "t1' \<in> Um h" by simp
nipkow@61784
   372
        from n2_type1[OF this t2] 1 show ?thesis by auto
nipkow@61784
   373
      qed
nipkow@61784
   374
    qed
nipkow@61784
   375
  }
nipkow@61784
   376
  { case 2
nipkow@61784
   377
    then obtain t1 where [simp]: "t = N1 t1" and t1: "t1 \<in> B h" by auto
nipkow@61784
   378
    show ?case
nipkow@68020
   379
    proof (cases "split_min t1")
nipkow@61784
   380
      case None
nipkow@68020
   381
      with split_minNoneN0[OF t1 None] 2 show ?thesis by(auto)
nipkow@61784
   382
    next
nipkow@61784
   383
      case [simp]: (Some bt')
nipkow@61784
   384
      obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce
nipkow@61784
   385
      from Suc.IH(1)[OF t1] have "t1' \<in> T h" by simp
nipkow@61784
   386
      thus ?thesis using 2 by auto
nipkow@61784
   387
    qed
nipkow@61784
   388
  }
nipkow@61784
   389
qed auto
nipkow@61784
   390
nipkow@61784
   391
lemma del_type:
nipkow@61784
   392
  "t \<in> B h \<Longrightarrow> del x t \<in> T h"
nipkow@61784
   393
  "t \<in> U h \<Longrightarrow> del x t \<in> Um h"
nipkow@61784
   394
proof (induction h arbitrary: x t)
nipkow@61784
   395
  case (Suc h)
nipkow@61784
   396
  { case 1
nipkow@61784
   397
    then obtain l a r where [simp]: "t = N2 l a r" and
nipkow@61784
   398
      lr: "l \<in> T h" "r \<in> T h" "l \<in> B h \<or> r \<in> B h" by auto
nipkow@67040
   399
    have ?case if "x < a"
nipkow@67040
   400
    proof cases
nipkow@67040
   401
      assume "l \<in> B h"
nipkow@67040
   402
      from n2_type3[OF Suc.IH(1)[OF this] lr(2)]
wenzelm@67406
   403
      show ?thesis using \<open>x<a\<close> by(simp)
nipkow@67040
   404
    next
nipkow@67040
   405
      assume "l \<notin> B h"
nipkow@67040
   406
      hence "l \<in> U h" "r \<in> B h" using lr by auto
nipkow@67040
   407
      from n2_type1[OF Suc.IH(2)[OF this(1)] this(2)]
wenzelm@67406
   408
      show ?thesis using \<open>x<a\<close> by(simp)
nipkow@67040
   409
    qed
nipkow@67040
   410
    moreover
nipkow@67040
   411
    have ?case if "x > a"
nipkow@67040
   412
    proof cases
nipkow@67040
   413
      assume "r \<in> B h"
nipkow@67040
   414
      from n2_type3[OF lr(1) Suc.IH(1)[OF this]]
wenzelm@67406
   415
      show ?thesis using \<open>x>a\<close> by(simp)
nipkow@67040
   416
    next
nipkow@67040
   417
      assume "r \<notin> B h"
nipkow@67040
   418
      hence "l \<in> B h" "r \<in> U h" using lr by auto
nipkow@67040
   419
      from n2_type2[OF this(1) Suc.IH(2)[OF this(2)]]
wenzelm@67406
   420
      show ?thesis using \<open>x>a\<close> by(simp)
nipkow@67040
   421
    qed
nipkow@67040
   422
    moreover
nipkow@67040
   423
    have ?case if [simp]: "x=a"
nipkow@68020
   424
    proof (cases "split_min r")
nipkow@67040
   425
      case None
nipkow@67040
   426
      show ?thesis
nipkow@61784
   427
      proof cases
nipkow@61784
   428
        assume "r \<in> B h"
nipkow@68020
   429
        with split_minNoneN0[OF this None] lr show ?thesis by(simp)
nipkow@61784
   430
      next
nipkow@61784
   431
        assume "r \<notin> B h"
nipkow@67040
   432
        hence "r \<in> U h" using lr by auto
nipkow@68020
   433
        with split_minNoneN1[OF this None] lr(3) show ?thesis by (simp)
nipkow@61784
   434
      qed
nipkow@67040
   435
    next
nipkow@67040
   436
      case [simp]: (Some br')
nipkow@67040
   437
      obtain b r' where [simp]: "br' = (b,r')" by fastforce
nipkow@67040
   438
      show ?thesis
nipkow@67040
   439
      proof cases
nipkow@67040
   440
        assume "r \<in> B h"
nipkow@68020
   441
        from split_min_type(1)[OF this] n2_type3[OF lr(1)]
nipkow@67040
   442
        show ?thesis by simp
nipkow@61784
   443
      next
nipkow@67040
   444
        assume "r \<notin> B h"
nipkow@67040
   445
        hence "l \<in> B h" and "r \<in> U h" using lr by auto
nipkow@68020
   446
        from split_min_type(2)[OF this(2)] n2_type2[OF this(1)]
nipkow@67040
   447
        show ?thesis by simp
nipkow@61784
   448
      qed
nipkow@67040
   449
    qed
nipkow@67040
   450
    ultimately show ?case by auto
nipkow@61784
   451
  }
nipkow@61784
   452
  { case 2 with Suc.IH(1) show ?case by auto }
nipkow@61784
   453
qed auto
nipkow@61784
   454
wenzelm@67613
   455
lemma tree_type: "t \<in> T (h+1) \<Longrightarrow> tree t \<in> B (h+1) \<union> B h"
nipkow@61784
   456
by(auto)
nipkow@61784
   457
nipkow@61809
   458
lemma delete_type: "t \<in> B h \<Longrightarrow> delete x t \<in> B h \<union> B(h-1)"
nipkow@61784
   459
unfolding delete_def
nipkow@61809
   460
by (cases h) (simp, metis del_type(1) tree_type Suc_eq_plus1 diff_Suc_1)
nipkow@61784
   461
nipkow@61784
   462
end
nipkow@61784
   463
nipkow@61789
   464
nipkow@61784
   465
subsection "Overall correctness"
nipkow@61784
   466
nipkow@61784
   467
interpretation Set_by_Ordered
nipkow@61789
   468
where empty = N0 and isin = isin and insert = insert.insert
nipkow@61809
   469
and delete = delete.delete and inorder = inorder and inv = "\<lambda>t. \<exists>h. t \<in> B h"
nipkow@61784
   470
proof (standard, goal_cases)
nipkow@61784
   471
  case 2 thus ?case by(auto intro!: isin_set)
nipkow@61784
   472
next
nipkow@61784
   473
  case 3 thus ?case by(auto intro!: insert.inorder_insert)
nipkow@61784
   474
next
nipkow@61792
   475
  case 4 thus ?case by(auto intro!: delete.inorder_delete)
nipkow@61784
   476
next
nipkow@61784
   477
  case 6 thus ?case using insert.insert_type by blast
nipkow@61784
   478
next
nipkow@61784
   479
  case 7 thus ?case using delete.delete_type by blast
nipkow@61784
   480
qed auto
nipkow@61784
   481
nipkow@63411
   482
nipkow@63411
   483
subsection \<open>Height-Size Relation\<close>
nipkow@63411
   484
nipkow@63411
   485
text \<open>By Daniel St\"uwe\<close>
nipkow@63411
   486
nipkow@63411
   487
fun fib_tree :: "nat \<Rightarrow> unit bro" where
nipkow@63411
   488
  "fib_tree 0 = N0" 
nipkow@63411
   489
| "fib_tree (Suc 0) = N2 N0 () N0"
nipkow@63411
   490
| "fib_tree (Suc(Suc h)) = N2 (fib_tree (h+1)) () (N1 (fib_tree h))"
nipkow@63411
   491
nipkow@63411
   492
fun fib' :: "nat \<Rightarrow> nat" where
nipkow@63411
   493
  "fib' 0 = 0" 
nipkow@63411
   494
| "fib' (Suc 0) = 1"
nipkow@63411
   495
| "fib' (Suc(Suc h)) = 1 + fib' (Suc h) + fib' h"
nipkow@63411
   496
nipkow@63411
   497
fun size :: "'a bro \<Rightarrow> nat" where
nipkow@63411
   498
  "size N0 = 0" 
nipkow@63411
   499
| "size (N1 t) = size t"
nipkow@63411
   500
| "size (N2 t1 _ t2) = 1 + size t1 + size t2"
nipkow@63411
   501
nipkow@63411
   502
lemma fib_tree_B: "fib_tree h \<in> B h"
nipkow@63411
   503
by (induction h rule: fib_tree.induct) auto
nipkow@63411
   504
nipkow@63411
   505
declare [[names_short]]
nipkow@63411
   506
nipkow@63411
   507
lemma size_fib': "size (fib_tree h) = fib' h"
nipkow@63411
   508
by (induction h rule: fib_tree.induct) auto
nipkow@63411
   509
nipkow@63411
   510
lemma fibfib: "fib' h + 1 = fib (Suc(Suc h))"
nipkow@63411
   511
by (induction h rule: fib_tree.induct) auto
nipkow@63411
   512
nipkow@63411
   513
lemma B_N2_cases[consumes 1]:
nipkow@63411
   514
assumes "N2 t1 a t2 \<in> B (Suc n)"
nipkow@63411
   515
obtains 
nipkow@63411
   516
  (BB) "t1 \<in> B n" and "t2 \<in> B n" |
nipkow@63411
   517
  (UB) "t1 \<in> U n" and "t2 \<in> B n" |
nipkow@63411
   518
  (BU) "t1 \<in> B n" and "t2 \<in> U n"
nipkow@63411
   519
using assms by auto
nipkow@63411
   520
nipkow@63411
   521
lemma size_bounded: "t \<in> B h \<Longrightarrow> size t \<ge> size (fib_tree h)"
nipkow@63411
   522
unfolding size_fib' proof (induction h arbitrary: t rule: fib'.induct)
nipkow@63411
   523
case (3 h t')
nipkow@63411
   524
  note main = 3
nipkow@63411
   525
  then obtain t1 a t2 where t': "t' = N2 t1 a t2" by auto
nipkow@63411
   526
  with main have "N2 t1 a t2 \<in> B (Suc (Suc h))" by auto
nipkow@63411
   527
  thus ?case proof (cases rule: B_N2_cases)
nipkow@63411
   528
    case BB
nipkow@63411
   529
    then obtain x y z where t2: "t2 = N2 x y z \<or> t2 = N2 z y x" "x \<in> B h" by auto
nipkow@63411
   530
    show ?thesis unfolding t' using main(1)[OF BB(1)] main(2)[OF t2(2)] t2(1) by auto
nipkow@63411
   531
  next
nipkow@63411
   532
    case UB
nipkow@63411
   533
    then obtain t11 where t1: "t1 = N1 t11" "t11 \<in> B h" by auto
nipkow@63411
   534
    show ?thesis unfolding t' t1(1) using main(2)[OF t1(2)] main(1)[OF UB(2)] by simp
nipkow@63411
   535
  next
nipkow@63411
   536
    case BU
nipkow@63411
   537
    then obtain t22 where t2: "t2 = N1 t22" "t22 \<in> B h" by auto
nipkow@63411
   538
    show ?thesis unfolding t' t2(1) using main(2)[OF t2(2)] main(1)[OF BU(1)] by simp
nipkow@63411
   539
  qed
nipkow@63411
   540
qed auto
nipkow@63411
   541
nipkow@63411
   542
theorem "t \<in> B h \<Longrightarrow> fib (h + 2) \<le> size t + 1"
nipkow@63411
   543
using size_bounded
nipkow@63411
   544
by (simp add: size_fib' fibfib[symmetric] del: fib.simps)
nipkow@63411
   545
nipkow@61784
   546
end