src/HOL/Data_Structures/AA_Set.thy
author nipkow
Wed Jun 13 15:24:20 2018 +0200 (10 months ago)
changeset 68440 6826718f732d
parent 68431 b294e095f64c
child 69505 cc2d676d5395
permissions -rw-r--r--
qualify interpretations to avoid clashes
nipkow@61793
     1
(*
nipkow@63411
     2
Author: Tobias Nipkow, Daniel Stüwe
nipkow@61793
     3
*)
nipkow@61793
     4
nipkow@62130
     5
section \<open>AA Tree Implementation of Sets\<close>
nipkow@61793
     6
nipkow@61793
     7
theory AA_Set
nipkow@61793
     8
imports
nipkow@61793
     9
  Isin2
nipkow@61793
    10
  Cmp
nipkow@61793
    11
begin
nipkow@61793
    12
nipkow@61793
    13
type_synonym 'a aa_tree = "('a,nat) tree"
nipkow@61793
    14
nipkow@68431
    15
definition empty :: "'a aa_tree" where
nipkow@68431
    16
"empty = Leaf"
nipkow@68431
    17
nipkow@61793
    18
fun lvl :: "'a aa_tree \<Rightarrow> nat" where
nipkow@61793
    19
"lvl Leaf = 0" |
nipkow@68413
    20
"lvl (Node _ _ lv _) = lv"
nipkow@62496
    21
nipkow@61793
    22
fun invar :: "'a aa_tree \<Rightarrow> bool" where
nipkow@61793
    23
"invar Leaf = True" |
nipkow@68413
    24
"invar (Node l a h r) =
nipkow@61793
    25
 (invar l \<and> invar r \<and>
nipkow@68413
    26
  h = lvl l + 1 \<and> (h = lvl r + 1 \<or> (\<exists>lr b rr. r = Node lr b h rr \<and> h = lvl rr + 1)))"
nipkow@62496
    27
nipkow@61793
    28
fun skew :: "'a aa_tree \<Rightarrow> 'a aa_tree" where
nipkow@68413
    29
"skew (Node (Node t1 b lvb t2) a lva t3) =
nipkow@68413
    30
  (if lva = lvb then Node t1 b lvb (Node t2 a lva t3) else Node (Node t1 b lvb t2) a lva t3)" |
nipkow@61793
    31
"skew t = t"
nipkow@61793
    32
nipkow@61793
    33
fun split :: "'a aa_tree \<Rightarrow> 'a aa_tree" where
nipkow@68413
    34
"split (Node t1 a lva (Node t2 b lvb (Node t3 c lvc t4))) =
wenzelm@67369
    35
   (if lva = lvb \<and> lvb = lvc \<comment> \<open>\<open>lva = lvc\<close> suffices\<close>
nipkow@68413
    36
    then Node (Node t1 a lva t2) b (lva+1) (Node t3 c lva t4)
nipkow@68413
    37
    else Node t1 a lva (Node t2 b lvb (Node t3 c lvc t4)))" |
nipkow@61793
    38
"split t = t"
nipkow@61793
    39
nipkow@61793
    40
hide_const (open) insert
nipkow@61793
    41
nipkow@63411
    42
fun insert :: "'a::linorder \<Rightarrow> 'a aa_tree \<Rightarrow> 'a aa_tree" where
nipkow@68413
    43
"insert x Leaf = Node Leaf x 1 Leaf" |
nipkow@68413
    44
"insert x (Node t1 a lv t2) =
nipkow@61793
    45
  (case cmp x a of
nipkow@68413
    46
     LT \<Rightarrow> split (skew (Node (insert x t1) a lv t2)) |
nipkow@68413
    47
     GT \<Rightarrow> split (skew (Node t1 a lv (insert x t2))) |
nipkow@68413
    48
     EQ \<Rightarrow> Node t1 x lv t2)"
nipkow@61793
    49
nipkow@61793
    50
fun sngl :: "'a aa_tree \<Rightarrow> bool" where
nipkow@61793
    51
"sngl Leaf = False" |
nipkow@61793
    52
"sngl (Node _ _ _ Leaf) = True" |
nipkow@68413
    53
"sngl (Node _ _ lva (Node _ _ lvb _)) = (lva > lvb)"
nipkow@61793
    54
nipkow@61793
    55
definition adjust :: "'a aa_tree \<Rightarrow> 'a aa_tree" where
nipkow@61793
    56
"adjust t =
nipkow@61793
    57
 (case t of
nipkow@68413
    58
  Node l x lv r \<Rightarrow>
nipkow@61793
    59
   (if lvl l >= lv-1 \<and> lvl r >= lv-1 then t else
nipkow@68413
    60
    if lvl r < lv-1 \<and> sngl l then skew (Node l x (lv-1) r) else
nipkow@61793
    61
    if lvl r < lv-1
nipkow@61793
    62
    then case l of
nipkow@68413
    63
           Node t1 a lva (Node t2 b lvb t3)
nipkow@68413
    64
             \<Rightarrow> Node (Node t1 a lva t2) b (lvb+1) (Node t3 x (lv-1) r) 
nipkow@61793
    65
    else
nipkow@68413
    66
    if lvl r < lv then split (Node l x (lv-1) r)
nipkow@61793
    67
    else
nipkow@61793
    68
      case r of
nipkow@68413
    69
        Node t1 b lvb t4 \<Rightarrow>
nipkow@61793
    70
          (case t1 of
nipkow@68413
    71
             Node t2 a lva t3
nipkow@68413
    72
               \<Rightarrow> Node (Node l x (lv-1) t2) a (lva+1)
nipkow@68413
    73
                    (split (Node t3 b (if sngl t1 then lva else lva+1) t4)))))"
nipkow@62496
    74
wenzelm@67406
    75
text\<open>In the paper, the last case of @{const adjust} is expressed with the help of an
nipkow@62496
    76
incorrect auxiliary function \texttt{nlvl}.
nipkow@62496
    77
nipkow@68023
    78
Function @{text split_max} below is called \texttt{dellrg} in the paper.
nipkow@62496
    79
The latter is incorrect for two reasons: \texttt{dellrg} is meant to delete the largest
nipkow@62496
    80
element but recurses on the left instead of the right subtree; the invariant
wenzelm@67406
    81
is not restored.\<close>
nipkow@62496
    82
nipkow@68023
    83
fun split_max :: "'a aa_tree \<Rightarrow> 'a aa_tree * 'a" where
nipkow@68413
    84
"split_max (Node l a lv Leaf) = (l,a)" |
nipkow@68413
    85
"split_max (Node l a lv r) = (let (r',b) = split_max r in (adjust(Node l a lv r'), b))"
nipkow@61793
    86
nipkow@63411
    87
fun delete :: "'a::linorder \<Rightarrow> 'a aa_tree \<Rightarrow> 'a aa_tree" where
nipkow@61793
    88
"delete _ Leaf = Leaf" |
nipkow@68413
    89
"delete x (Node l a lv r) =
nipkow@61793
    90
  (case cmp x a of
nipkow@68413
    91
     LT \<Rightarrow> adjust (Node (delete x l) a lv r) |
nipkow@68413
    92
     GT \<Rightarrow> adjust (Node l a lv (delete x r)) |
nipkow@61793
    93
     EQ \<Rightarrow> (if l = Leaf then r
nipkow@68413
    94
            else let (l',b) = split_max l in adjust (Node l' b lv r)))"
nipkow@61793
    95
nipkow@62496
    96
fun pre_adjust where
nipkow@68413
    97
"pre_adjust (Node l a lv r) = (invar l \<and> invar r \<and>
nipkow@62496
    98
    ((lv = lvl l + 1 \<and> (lv = lvl r + 1 \<or> lv = lvl r + 2 \<or> lv = lvl r \<and> sngl r)) \<or>
nipkow@62496
    99
     (lv = lvl l + 2 \<and> (lv = lvl r + 1 \<or> lv = lvl r \<and> sngl r))))"
nipkow@62496
   100
nipkow@62496
   101
declare pre_adjust.simps [simp del]
nipkow@62496
   102
nipkow@62496
   103
subsection "Auxiliary Proofs"
nipkow@62496
   104
nipkow@62496
   105
lemma split_case: "split t = (case t of
nipkow@68413
   106
  Node t1 x lvx (Node t2 y lvy (Node t3 z lvz t4)) \<Rightarrow>
nipkow@62496
   107
   (if lvx = lvy \<and> lvy = lvz
nipkow@68413
   108
    then Node (Node t1 x lvx t2) y (lvx+1) (Node t3 z lvx t4)
nipkow@62496
   109
    else t)
nipkow@62496
   110
  | t \<Rightarrow> t)"
nipkow@62496
   111
by(auto split: tree.split)
nipkow@62496
   112
nipkow@62496
   113
lemma skew_case: "skew t = (case t of
nipkow@68413
   114
  Node (Node t1 y lvy t2) x lvx t3 \<Rightarrow>
nipkow@68413
   115
  (if lvx = lvy then Node t1 y lvx (Node t2 x lvx t3) else t)
nipkow@62496
   116
 | t \<Rightarrow> t)"
nipkow@62496
   117
by(auto split: tree.split)
nipkow@62496
   118
nipkow@62496
   119
lemma lvl_0_iff: "invar t \<Longrightarrow> lvl t = 0 \<longleftrightarrow> t = Leaf"
nipkow@62496
   120
by(cases t) auto
nipkow@62496
   121
nipkow@68413
   122
lemma lvl_Suc_iff: "lvl t = Suc n \<longleftrightarrow> (\<exists> l a r. t = Node l a (Suc n) r)"
nipkow@62496
   123
by(cases t) auto
nipkow@62496
   124
nipkow@62496
   125
lemma lvl_skew: "lvl (skew t) = lvl t"
nipkow@62526
   126
by(cases t rule: skew.cases) auto
nipkow@62496
   127
nipkow@62496
   128
lemma lvl_split: "lvl (split t) = lvl t \<or> lvl (split t) = lvl t + 1 \<and> sngl (split t)"
nipkow@62526
   129
by(cases t rule: split.cases) auto
nipkow@62496
   130
nipkow@68413
   131
lemma invar_2Nodes:"invar (Node l x lv (Node rl rx rlv rr)) =
nipkow@68413
   132
     (invar l \<and> invar \<langle>rl, rx, rlv, rr\<rangle> \<and> lv = Suc (lvl l) \<and>
nipkow@62496
   133
     (lv = Suc rlv \<or> rlv = lv \<and> lv = Suc (lvl rr)))"
nipkow@62496
   134
by simp
nipkow@62496
   135
nipkow@62496
   136
lemma invar_NodeLeaf[simp]:
nipkow@68413
   137
  "invar (Node l x lv Leaf) = (invar l \<and> lv = Suc (lvl l) \<and> lv = Suc 0)"
nipkow@62496
   138
by simp
nipkow@62496
   139
nipkow@68413
   140
lemma sngl_if_invar: "invar (Node l a n r) \<Longrightarrow> n = lvl r \<Longrightarrow> sngl r"
nipkow@62496
   141
by(cases r rule: sngl.cases) clarsimp+
nipkow@62496
   142
nipkow@62496
   143
nipkow@62496
   144
subsection "Invariance"
nipkow@62496
   145
nipkow@62496
   146
subsubsection "Proofs for insert"
nipkow@62496
   147
nipkow@62496
   148
lemma lvl_insert_aux:
nipkow@62496
   149
  "lvl (insert x t) = lvl t \<or> lvl (insert x t) = lvl t + 1 \<and> sngl (insert x t)"
nipkow@62496
   150
apply(induction t)
nipkow@62496
   151
apply (auto simp: lvl_skew)
nipkow@62496
   152
apply (metis Suc_eq_plus1 lvl.simps(2) lvl_split lvl_skew)+
nipkow@62496
   153
done
nipkow@62496
   154
nipkow@62496
   155
lemma lvl_insert: obtains
nipkow@62496
   156
  (Same) "lvl (insert x t) = lvl t" |
nipkow@62496
   157
  (Incr) "lvl (insert x t) = lvl t + 1" "sngl (insert x t)"
nipkow@62496
   158
using lvl_insert_aux by blast
nipkow@62496
   159
nipkow@62496
   160
lemma lvl_insert_sngl: "invar t \<Longrightarrow> sngl t \<Longrightarrow> lvl(insert x t) = lvl t"
nipkow@62526
   161
proof (induction t rule: insert.induct)
nipkow@68413
   162
  case (2 x t1 a lv t2)
nipkow@62496
   163
  consider (LT) "x < a" | (GT) "x > a" | (EQ) "x = a" 
nipkow@62496
   164
    using less_linear by blast 
nipkow@62496
   165
  thus ?case proof cases
nipkow@62496
   166
    case LT
nipkow@62496
   167
    thus ?thesis using 2 by (auto simp add: skew_case split_case split: tree.splits)
nipkow@62496
   168
  next
nipkow@62496
   169
    case GT
nipkow@62496
   170
    thus ?thesis using 2 proof (cases t1)
nipkow@62496
   171
      case Node
nipkow@62496
   172
      thus ?thesis using 2 GT  
nipkow@62496
   173
        apply (auto simp add: skew_case split_case split: tree.splits)
nipkow@62496
   174
        by (metis less_not_refl2 lvl.simps(2) lvl_insert_aux n_not_Suc_n sngl.simps(3))+ 
nipkow@62496
   175
    qed (auto simp add: lvl_0_iff)
nipkow@62496
   176
  qed simp
nipkow@62496
   177
qed simp
nipkow@62496
   178
nipkow@62496
   179
lemma skew_invar: "invar t \<Longrightarrow> skew t = t"
nipkow@62526
   180
by(cases t rule: skew.cases) auto
nipkow@62496
   181
nipkow@62496
   182
lemma split_invar: "invar t \<Longrightarrow> split t = t"
nipkow@62526
   183
by(cases t rule: split.cases) clarsimp+
nipkow@62496
   184
nipkow@62496
   185
lemma invar_NodeL:
nipkow@68413
   186
  "\<lbrakk> invar(Node l x n r); invar l'; lvl l' = lvl l \<rbrakk> \<Longrightarrow> invar(Node l' x n r)"
nipkow@62496
   187
by(auto)
nipkow@62496
   188
nipkow@62496
   189
lemma invar_NodeR:
nipkow@68413
   190
  "\<lbrakk> invar(Node l x n r); n = lvl r + 1; invar r'; lvl r' = lvl r \<rbrakk> \<Longrightarrow> invar(Node l x n r')"
nipkow@62496
   191
by(auto)
nipkow@62496
   192
nipkow@62496
   193
lemma invar_NodeR2:
nipkow@68413
   194
  "\<lbrakk> invar(Node l x n r); sngl r'; n = lvl r + 1; invar r'; lvl r' = n \<rbrakk> \<Longrightarrow> invar(Node l x n r')"
nipkow@62496
   195
by(cases r' rule: sngl.cases) clarsimp+
nipkow@62496
   196
nipkow@62496
   197
nipkow@62496
   198
lemma lvl_insert_incr_iff: "(lvl(insert a t) = lvl t + 1) \<longleftrightarrow>
nipkow@68413
   199
  (\<exists>l x r. insert a t = Node l x (lvl t + 1) r \<and> lvl l = lvl r)"
nipkow@62496
   200
apply(cases t)
nipkow@62496
   201
apply(auto simp add: skew_case split_case split: if_splits)
nipkow@62496
   202
apply(auto split: tree.splits if_splits)
nipkow@62496
   203
done
nipkow@62496
   204
nipkow@62496
   205
lemma invar_insert: "invar t \<Longrightarrow> invar(insert a t)"
nipkow@62496
   206
proof(induction t)
nipkow@68413
   207
  case N: (Node l x n r)
nipkow@62496
   208
  hence il: "invar l" and ir: "invar r" by auto
nipkow@67040
   209
  note iil = N.IH(1)[OF il]
nipkow@67040
   210
  note iir = N.IH(2)[OF ir]
nipkow@68413
   211
  let ?t = "Node l x n r"
nipkow@62496
   212
  have "a < x \<or> a = x \<or> x < a" by auto
nipkow@62496
   213
  moreover
nipkow@67040
   214
  have ?case if "a < x"
nipkow@67040
   215
  proof (cases rule: lvl_insert[of a l])
nipkow@67040
   216
    case (Same) thus ?thesis
nipkow@67040
   217
      using \<open>a<x\<close> invar_NodeL[OF N.prems iil Same]
nipkow@67040
   218
      by (simp add: skew_invar split_invar del: invar.simps)
nipkow@67040
   219
  next
nipkow@67040
   220
    case (Incr)
nipkow@68413
   221
    then obtain t1 w t2 where ial[simp]: "insert a l = Node t1 w n t2"
nipkow@67040
   222
      using N.prems by (auto simp: lvl_Suc_iff)
nipkow@67040
   223
    have l12: "lvl t1 = lvl t2"
nipkow@67040
   224
      by (metis Incr(1) ial lvl_insert_incr_iff tree.inject)
nipkow@68413
   225
    have "insert a ?t = split(skew(Node (insert a l) x n r))"
nipkow@67040
   226
      by(simp add: \<open>a<x\<close>)
nipkow@68413
   227
    also have "skew(Node (insert a l) x n r) = Node t1 w n (Node t2 x n r)"
nipkow@67040
   228
      by(simp)
nipkow@67040
   229
    also have "invar(split \<dots>)"
nipkow@67040
   230
    proof (cases r)
nipkow@67040
   231
      case Leaf
nipkow@67040
   232
      hence "l = Leaf" using N.prems by(auto simp: lvl_0_iff)
nipkow@67040
   233
      thus ?thesis using Leaf ial by simp
nipkow@62496
   234
    next
nipkow@68413
   235
      case [simp]: (Node t3 y m t4)
nipkow@67040
   236
      show ?thesis (*using N(3) iil l12 by(auto)*)
nipkow@67040
   237
      proof cases
nipkow@67040
   238
        assume "m = n" thus ?thesis using N(3) iil by(auto)
nipkow@62496
   239
      next
nipkow@67040
   240
        assume "m \<noteq> n" thus ?thesis using N(3) iil l12 by(auto)
nipkow@62496
   241
      qed
nipkow@62496
   242
    qed
nipkow@67040
   243
    finally show ?thesis .
nipkow@67040
   244
  qed
nipkow@62496
   245
  moreover
nipkow@67040
   246
  have ?case if "x < a"
nipkow@67040
   247
  proof -
nipkow@62496
   248
    from \<open>invar ?t\<close> have "n = lvl r \<or> n = lvl r + 1" by auto
nipkow@67040
   249
    thus ?case
nipkow@62496
   250
    proof
nipkow@62496
   251
      assume 0: "n = lvl r"
nipkow@68413
   252
      have "insert a ?t = split(skew(Node l x n (insert a r)))"
nipkow@62496
   253
        using \<open>a>x\<close> by(auto)
nipkow@68413
   254
      also have "skew(Node l x n (insert a r)) = Node l x n (insert a r)"
nipkow@67040
   255
        using N.prems by(simp add: skew_case split: tree.split)
nipkow@62496
   256
      also have "invar(split \<dots>)"
nipkow@62496
   257
      proof -
nipkow@62496
   258
        from lvl_insert_sngl[OF ir sngl_if_invar[OF \<open>invar ?t\<close> 0], of a]
nipkow@68413
   259
        obtain t1 y t2 where iar: "insert a r = Node t1 y n t2"
nipkow@67040
   260
          using N.prems 0 by (auto simp: lvl_Suc_iff)
nipkow@67040
   261
        from N.prems iar 0 iir
nipkow@62496
   262
        show ?thesis by (auto simp: split_case split: tree.splits)
nipkow@62496
   263
      qed
nipkow@62496
   264
      finally show ?thesis .
nipkow@62496
   265
    next
nipkow@62496
   266
      assume 1: "n = lvl r + 1"
nipkow@62496
   267
      hence "sngl ?t" by(cases r) auto
nipkow@62496
   268
      show ?thesis
nipkow@62496
   269
      proof (cases rule: lvl_insert[of a r])
nipkow@62496
   270
        case (Same)
nipkow@67040
   271
        show ?thesis using \<open>x<a\<close> il ir invar_NodeR[OF N.prems 1 iir Same]
nipkow@62496
   272
          by (auto simp add: skew_invar split_invar)
nipkow@62496
   273
      next
nipkow@62496
   274
        case (Incr)
wenzelm@67406
   275
        thus ?thesis using invar_NodeR2[OF \<open>invar ?t\<close> Incr(2) 1 iir] 1 \<open>x < a\<close>
nipkow@62496
   276
          by (auto simp add: skew_invar split_invar split: if_splits)
nipkow@62496
   277
      qed
nipkow@62496
   278
    qed
nipkow@67040
   279
  qed
nipkow@67040
   280
  moreover
nipkow@67040
   281
  have "a = x \<Longrightarrow> ?case" using N.prems by auto
nipkow@62496
   282
  ultimately show ?case by blast
nipkow@62496
   283
qed simp
nipkow@62496
   284
nipkow@62496
   285
nipkow@62496
   286
subsubsection "Proofs for delete"
nipkow@62496
   287
nipkow@68413
   288
lemma invarL: "ASSUMPTION(invar \<langle>l, a, lv, r\<rangle>) \<Longrightarrow> invar l"
nipkow@62496
   289
by(simp add: ASSUMPTION_def)
nipkow@62496
   290
nipkow@62496
   291
lemma invarR: "ASSUMPTION(invar \<langle>lv, l, a, r\<rangle>) \<Longrightarrow> invar r"
nipkow@62496
   292
by(simp add: ASSUMPTION_def)
nipkow@62496
   293
nipkow@62496
   294
lemma sngl_NodeI:
nipkow@68413
   295
  "sngl (Node l a lv r) \<Longrightarrow> sngl (Node l' a' lv r)"
nipkow@62496
   296
by(cases r) (simp_all)
nipkow@62496
   297
nipkow@62496
   298
nipkow@62496
   299
declare invarL[simp] invarR[simp]
nipkow@62496
   300
nipkow@62496
   301
lemma pre_cases:
nipkow@68413
   302
assumes "pre_adjust (Node l x lv r)"
nipkow@62496
   303
obtains
nipkow@62496
   304
 (tSngl) "invar l \<and> invar r \<and>
nipkow@62496
   305
    lv = Suc (lvl r) \<and> lvl l = lvl r" |
nipkow@62496
   306
 (tDouble) "invar l \<and> invar r \<and>
nipkow@62496
   307
    lv = lvl r \<and> Suc (lvl l) = lvl r \<and> sngl r " |
nipkow@62496
   308
 (rDown) "invar l \<and> invar r \<and>
nipkow@62496
   309
    lv = Suc (Suc (lvl r)) \<and>  lv = Suc (lvl l)" |
nipkow@62496
   310
 (lDown_tSngl) "invar l \<and> invar r \<and>
nipkow@62496
   311
    lv = Suc (lvl r) \<and> lv = Suc (Suc (lvl l))" |
nipkow@62496
   312
 (lDown_tDouble) "invar l \<and> invar r \<and>
nipkow@62496
   313
    lv = lvl r \<and> lv = Suc (Suc (lvl l)) \<and> sngl r"
nipkow@62496
   314
using assms unfolding pre_adjust.simps
nipkow@62496
   315
by auto
nipkow@62496
   316
nipkow@62496
   317
declare invar.simps(2)[simp del] invar_2Nodes[simp add]
nipkow@62496
   318
nipkow@62496
   319
lemma invar_adjust:
nipkow@68413
   320
  assumes pre: "pre_adjust (Node l a lv r)"
nipkow@68413
   321
  shows  "invar(adjust (Node l a lv r))"
nipkow@62496
   322
using pre proof (cases rule: pre_cases)
nipkow@62496
   323
  case (tDouble) thus ?thesis unfolding adjust_def by (cases r) (auto simp: invar.simps(2)) 
nipkow@62496
   324
next 
nipkow@62496
   325
  case (rDown)
nipkow@68413
   326
  from rDown obtain llv ll la lr where l: "l = Node ll la llv lr" by (cases l) auto
nipkow@62496
   327
  from rDown show ?thesis unfolding adjust_def by (auto simp: l invar.simps(2) split: tree.splits)
nipkow@62496
   328
next
nipkow@62496
   329
  case (lDown_tDouble)
nipkow@68413
   330
  from lDown_tDouble obtain rlv rr ra rl where r: "r = Node rl ra rlv rr" by (cases r) auto
nipkow@62496
   331
  from lDown_tDouble and r obtain rrlv rrr rra rrl where
nipkow@68413
   332
    rr :"rr = Node rrr rra rrlv rrl" by (cases rr) auto
nipkow@62496
   333
  from  lDown_tDouble show ?thesis unfolding adjust_def r rr
nipkow@63636
   334
    apply (cases rl) apply (auto simp add: invar.simps(2) split!: if_split)
nipkow@62496
   335
    using lDown_tDouble by (auto simp: split_case lvl_0_iff  elim:lvl.elims split: tree.split)
nipkow@62496
   336
qed (auto simp: split_case invar.simps(2) adjust_def split: tree.splits)
nipkow@62496
   337
nipkow@62496
   338
lemma lvl_adjust:
nipkow@68413
   339
  assumes "pre_adjust (Node l a lv r)"
nipkow@68413
   340
  shows "lv = lvl (adjust(Node l a lv r)) \<or> lv = lvl (adjust(Node l a lv r)) + 1"
nipkow@62496
   341
using assms(1) proof(cases rule: pre_cases)
nipkow@62496
   342
  case lDown_tSngl thus ?thesis
nipkow@68413
   343
    using lvl_split[of "\<langle>l, a, lvl r, r\<rangle>"] by (auto simp: adjust_def)
nipkow@62496
   344
next
nipkow@62496
   345
  case lDown_tDouble thus ?thesis
nipkow@62496
   346
    by (auto simp: adjust_def invar.simps(2) split: tree.split)
nipkow@62496
   347
qed (auto simp: adjust_def split: tree.splits)
nipkow@62496
   348
nipkow@68413
   349
lemma sngl_adjust: assumes "pre_adjust (Node l a lv r)"
nipkow@68413
   350
  "sngl \<langle>l, a, lv, r\<rangle>" "lv = lvl (adjust \<langle>l, a, lv, r\<rangle>)"
nipkow@68413
   351
  shows "sngl (adjust \<langle>l, a, lv, r\<rangle>)" 
nipkow@62496
   352
using assms proof (cases rule: pre_cases)
nipkow@62496
   353
  case rDown
nipkow@62496
   354
  thus ?thesis using assms(2,3) unfolding adjust_def
nipkow@62496
   355
    by (auto simp add: skew_case) (auto split: tree.split)
nipkow@62496
   356
qed (auto simp: adjust_def skew_case split_case split: tree.split)
nipkow@62496
   357
nipkow@62496
   358
definition "post_del t t' ==
nipkow@62496
   359
  invar t' \<and>
nipkow@62496
   360
  (lvl t' = lvl t \<or> lvl t' + 1 = lvl t) \<and>
nipkow@62496
   361
  (lvl t' = lvl t \<and> sngl t \<longrightarrow> sngl t')"
nipkow@62496
   362
nipkow@62496
   363
lemma pre_adj_if_postR:
nipkow@62496
   364
  "invar\<langle>lv, l, a, r\<rangle> \<Longrightarrow> post_del r r' \<Longrightarrow> pre_adjust \<langle>lv, l, a, r'\<rangle>"
nipkow@62496
   365
by(cases "sngl r")
nipkow@62496
   366
  (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims)
nipkow@62496
   367
nipkow@62496
   368
lemma pre_adj_if_postL:
nipkow@68413
   369
  "invar\<langle>l, a, lv, r\<rangle> \<Longrightarrow> post_del l l' \<Longrightarrow> pre_adjust \<langle>l', b, lv, r\<rangle>"
nipkow@62496
   370
by(cases "sngl r")
nipkow@62496
   371
  (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims)
nipkow@62496
   372
nipkow@62496
   373
lemma post_del_adjL:
nipkow@68413
   374
  "\<lbrakk> invar\<langle>l, a, lv, r\<rangle>; pre_adjust \<langle>l', b, lv, r\<rangle> \<rbrakk>
nipkow@68413
   375
  \<Longrightarrow> post_del \<langle>l, a, lv, r\<rangle> (adjust \<langle>l', b, lv, r\<rangle>)"
nipkow@62496
   376
unfolding post_del_def
nipkow@62496
   377
by (metis invar_adjust lvl_adjust sngl_NodeI sngl_adjust lvl.simps(2))
nipkow@62496
   378
nipkow@62496
   379
lemma post_del_adjR:
nipkow@62496
   380
assumes "invar\<langle>lv, l, a, r\<rangle>" "pre_adjust \<langle>lv, l, a, r'\<rangle>" "post_del r r'"
nipkow@62496
   381
shows "post_del \<langle>lv, l, a, r\<rangle> (adjust \<langle>lv, l, a, r'\<rangle>)"
nipkow@62496
   382
proof(unfold post_del_def, safe del: disjCI)
nipkow@62496
   383
  let ?t = "\<langle>lv, l, a, r\<rangle>"
nipkow@62496
   384
  let ?t' = "adjust \<langle>lv, l, a, r'\<rangle>"
nipkow@62496
   385
  show "invar ?t'" by(rule invar_adjust[OF assms(2)])
nipkow@62496
   386
  show "lvl ?t' = lvl ?t \<or> lvl ?t' + 1 = lvl ?t"
nipkow@62496
   387
    using lvl_adjust[OF assms(2)] by auto
nipkow@62496
   388
  show "sngl ?t'" if as: "lvl ?t' = lvl ?t" "sngl ?t"
nipkow@62496
   389
  proof -
nipkow@62496
   390
    have s: "sngl \<langle>lv, l, a, r'\<rangle>"
nipkow@62496
   391
    proof(cases r')
nipkow@62496
   392
      case Leaf thus ?thesis by simp
nipkow@62496
   393
    next
nipkow@62496
   394
      case Node thus ?thesis using as(2) assms(1,3)
nipkow@62496
   395
      by (cases r) (auto simp: post_del_def)
nipkow@62496
   396
    qed
nipkow@62496
   397
    show ?thesis using as(1) sngl_adjust[OF assms(2) s] by simp
nipkow@62496
   398
  qed
nipkow@62496
   399
qed
nipkow@62496
   400
nipkow@62496
   401
declare prod.splits[split]
nipkow@62496
   402
nipkow@68023
   403
theorem post_split_max:
nipkow@68023
   404
 "\<lbrakk> invar t; (t', x) = split_max t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow> post_del t t'"
nipkow@68023
   405
proof (induction t arbitrary: t' rule: split_max.induct)
nipkow@62496
   406
  case (2 lv l a lvr rl ra rr)
nipkow@62496
   407
  let ?r =  "\<langle>lvr, rl, ra, rr\<rangle>"
nipkow@62496
   408
  let ?t = "\<langle>lv, l, a, ?r\<rangle>"
nipkow@68023
   409
  from "2.prems"(2) obtain r' where r': "(r', x) = split_max ?r"
nipkow@62496
   410
    and [simp]: "t' = adjust \<langle>lv, l, a, r'\<rangle>" by auto
nipkow@62496
   411
  from  "2.IH"[OF _ r'] \<open>invar ?t\<close> have post: "post_del ?r r'" by simp
nipkow@62496
   412
  note preR = pre_adj_if_postR[OF \<open>invar ?t\<close> post]
nipkow@62496
   413
  show ?case by (simp add: post_del_adjR[OF "2.prems"(1) preR post])
nipkow@62496
   414
qed (auto simp: post_del_def)
nipkow@62496
   415
nipkow@62496
   416
theorem post_delete: "invar t \<Longrightarrow> post_del t (delete x t)"
nipkow@62496
   417
proof (induction t)
nipkow@68413
   418
  case (Node l a lv r)
nipkow@62496
   419
nipkow@62496
   420
  let ?l' = "delete x l" and ?r' = "delete x r"
nipkow@68413
   421
  let ?t = "Node l a lv r" let ?t' = "delete x ?t"
nipkow@62496
   422
nipkow@62496
   423
  from Node.prems have inv_l: "invar l" and inv_r: "invar r" by (auto)
nipkow@62496
   424
nipkow@62496
   425
  note post_l' = Node.IH(1)[OF inv_l]
nipkow@62496
   426
  note preL = pre_adj_if_postL[OF Node.prems post_l']
nipkow@62496
   427
nipkow@62496
   428
  note post_r' = Node.IH(2)[OF inv_r]
nipkow@62496
   429
  note preR = pre_adj_if_postR[OF Node.prems post_r']
nipkow@62496
   430
nipkow@62496
   431
  show ?case
nipkow@62496
   432
  proof (cases rule: linorder_cases[of x a])
nipkow@62496
   433
    case less
nipkow@62496
   434
    thus ?thesis using Node.prems by (simp add: post_del_adjL preL)
nipkow@62496
   435
  next
nipkow@62496
   436
    case greater
nipkow@62496
   437
    thus ?thesis using Node.prems by (simp add: post_del_adjR preR post_r')
nipkow@62496
   438
  next
nipkow@62496
   439
    case equal
nipkow@62496
   440
    show ?thesis
nipkow@62496
   441
    proof cases
nipkow@62496
   442
      assume "l = Leaf" thus ?thesis using equal Node.prems
nipkow@62496
   443
        by(auto simp: post_del_def invar.simps(2))
nipkow@62496
   444
    next
nipkow@62496
   445
      assume "l \<noteq> Leaf" thus ?thesis using equal
nipkow@68023
   446
        by simp (metis Node.prems inv_l post_del_adjL post_split_max pre_adj_if_postL)
nipkow@62496
   447
    qed
nipkow@62496
   448
  qed
nipkow@62496
   449
qed (simp add: post_del_def)
nipkow@62496
   450
nipkow@62496
   451
declare invar_2Nodes[simp del]
nipkow@62496
   452
nipkow@61793
   453
nipkow@61793
   454
subsection "Functional Correctness"
nipkow@61793
   455
nipkow@62496
   456
nipkow@61793
   457
subsubsection "Proofs for insert"
nipkow@61793
   458
nipkow@61793
   459
lemma inorder_split: "inorder(split t) = inorder t"
nipkow@61793
   460
by(cases t rule: split.cases) (auto)
nipkow@61793
   461
nipkow@61793
   462
lemma inorder_skew: "inorder(skew t) = inorder t"
nipkow@61793
   463
by(cases t rule: skew.cases) (auto)
nipkow@61793
   464
nipkow@61793
   465
lemma inorder_insert:
nipkow@61793
   466
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61793
   467
by(induction t) (auto simp: ins_list_simps inorder_split inorder_skew)
nipkow@61793
   468
nipkow@62496
   469
nipkow@61793
   470
subsubsection "Proofs for delete"
nipkow@61793
   471
nipkow@62496
   472
lemma inorder_adjust: "t \<noteq> Leaf \<Longrightarrow> pre_adjust t \<Longrightarrow> inorder(adjust t) = inorder t"
nipkow@62526
   473
by(cases t)
nipkow@62496
   474
  (auto simp: adjust_def inorder_skew inorder_split invar.simps(2) pre_adjust.simps
nipkow@62496
   475
     split: tree.splits)
nipkow@62496
   476
nipkow@68023
   477
lemma split_maxD:
nipkow@68023
   478
  "\<lbrakk> split_max t = (t',x); t \<noteq> Leaf; invar t \<rbrakk> \<Longrightarrow> inorder t' @ [x] = inorder t"
nipkow@68023
   479
by(induction t arbitrary: t' rule: split_max.induct)
nipkow@68023
   480
  (auto simp: sorted_lems inorder_adjust pre_adj_if_postR post_split_max split: prod.splits)
nipkow@61793
   481
nipkow@61793
   482
lemma inorder_delete:
nipkow@62496
   483
  "invar t \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@61793
   484
by(induction t)
nipkow@62496
   485
  (auto simp: del_list_simps inorder_adjust pre_adj_if_postL pre_adj_if_postR 
nipkow@68023
   486
              post_split_max post_delete split_maxD split: prod.splits)
nipkow@61793
   487
nipkow@68440
   488
interpretation S: Set_by_Ordered
nipkow@68431
   489
where empty = empty and isin = isin and insert = insert and delete = delete
nipkow@62496
   490
and inorder = inorder and inv = invar
nipkow@61793
   491
proof (standard, goal_cases)
nipkow@68431
   492
  case 1 show ?case by (simp add: empty_def)
nipkow@61793
   493
next
nipkow@67967
   494
  case 2 thus ?case by(simp add: isin_set_inorder)
nipkow@61793
   495
next
nipkow@61793
   496
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@61793
   497
next
nipkow@61793
   498
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@62496
   499
next
nipkow@68431
   500
  case 5 thus ?case by(simp add: empty_def)
nipkow@62496
   501
next
nipkow@62496
   502
  case 6 thus ?case by(simp add: invar_insert)
nipkow@62496
   503
next
nipkow@62496
   504
  case 7 thus ?case using post_delete by(auto simp: post_del_def)
nipkow@62496
   505
qed
nipkow@61793
   506
nipkow@62390
   507
end