src/HOL/Data_Structures/AVL_Bal_Set.thy
author nipkow
Mon, 04 May 2020 23:34:46 +0200
changeset 71815 a86e37f4ad60
parent 71814 a9df6686ed0e
child 71816 489c907b9e05
permissions -rw-r--r--
tuned var. names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71814
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     1
(* Tobias Nipkow *)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     2
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     3
section "AVL Tree with Balance Tags (Set Implementation)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     4
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     5
theory AVL_Bal_Set
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     6
imports
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     7
  Cmp
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     8
  Isin2
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
     9
begin
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    10
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    11
datatype bal = Lh | Bal | Rh
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    12
(* Exercise: use 3 Node constructors instead *)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    13
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    14
type_synonym 'a tree_bal = "('a * bal) tree"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    15
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    16
text \<open>Invariant:\<close>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    17
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    18
fun avl :: "'a tree_bal \<Rightarrow> bool" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    19
"avl Leaf = True" |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    20
"avl (Node l (a,b) r) =
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    21
  ((case b of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    22
    Bal \<Rightarrow> height r = height l |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    23
    Lh \<Rightarrow> height l = height r + 1 |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    24
    Rh \<Rightarrow> height r = height l + 1)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    25
  \<and> avl l \<and> avl r)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    26
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    27
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    28
subsection \<open>Code\<close>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    29
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    30
datatype 'a change = Same 'a | Diff 'a
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    31
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    32
fun tree :: "'a change \<Rightarrow> 'a" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    33
"tree(Same t) = t" |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    34
"tree(Diff t) = t"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    35
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    36
fun rot21 :: "bal \<Rightarrow> bal" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    37
"rot21 b = (if b=Rh then Lh else Bal)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    38
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    39
fun rot22 :: "bal \<Rightarrow> bal" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    40
"rot22 b = (if b=Lh then Rh else Bal)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    41
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    42
fun balL :: "'a tree_bal change \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
71815
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    43
"balL AB' c bc C = (case AB' of
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    44
   Same AB \<Rightarrow> Same (Node AB (c,bc) C) |
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    45
   Diff AB \<Rightarrow> (case bc of
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    46
     Bal \<Rightarrow> Diff (Node AB (c,Lh) C) |
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    47
     Rh \<Rightarrow> Same (Node AB (c,Bal) C) |
71814
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    48
     Lh \<Rightarrow> Same(case AB of
71815
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    49
       Node A (ab,Lh) B \<Rightarrow> Node A (ab,Bal) (Node B (c,Bal) C) |
71814
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    50
       Node A (ab,Rh) B \<Rightarrow> (case B of
71815
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    51
         Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    52
           Node (Node A (ab,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C)))))"
71814
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    53
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    54
fun balR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal change \<Rightarrow> 'a tree_bal change" where
71815
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    55
"balR A a ba BC' = (case BC' of
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    56
   Same BC \<Rightarrow> Same (Node A (a,ba) BC) |
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    57
   Diff BC \<Rightarrow> (case ba of
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    58
     Bal \<Rightarrow> Diff (Node A (a,Rh) BC) |
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    59
     Lh \<Rightarrow> Same (Node A (a,Bal) BC) |
71814
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    60
     Rh \<Rightarrow> Same(case BC of
71815
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    61
       Node B (bc,Rh) C \<Rightarrow> Node (Node A (a,Bal) B) (bc,Bal) C |
71814
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    62
       Node B (bc,Lh) C \<Rightarrow> (case B of
71815
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    63
         Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
a86e37f4ad60 tuned var. names
nipkow
parents: 71814
diff changeset
    64
           Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (bc,rot22 bb) C)))))"
71814
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    65
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    66
fun insert :: "'a::linorder \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    67
"insert x Leaf = Diff(Node Leaf (x, Bal) Leaf)" |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    68
"insert x (Node l (a, b) r) = (case cmp x a of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    69
   EQ \<Rightarrow> Same(Node l (a, b) r) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    70
   LT \<Rightarrow> balL (insert x l) a b r |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    71
   GT \<Rightarrow> balR l a b (insert x r))"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    72
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    73
fun baldR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal change \<Rightarrow> 'a tree_bal change" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    74
"baldR AB bc b C' = (case C' of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    75
   Same C \<Rightarrow> Same (Node AB (bc,b) C) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    76
   Diff C \<Rightarrow> (case b of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    77
     Bal \<Rightarrow> Same (Node AB (bc,Lh) C) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    78
     Rh \<Rightarrow> Diff (Node AB (bc,Bal) C) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    79
     Lh \<Rightarrow> (case AB of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    80
       Node A (ab,Lh) B \<Rightarrow> Diff(Node A (ab,Bal) (Node B (bc,Bal) C)) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    81
       Node A (ab,Bal) B \<Rightarrow> Same(Node A (ab,Rh) (Node B (bc,Lh) C)) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    82
       Node A (ab,Rh) B \<Rightarrow> (case B of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    83
         Node B\<^sub>1 (bb, bB) B\<^sub>2 \<Rightarrow>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    84
           Diff(Node (Node A (ab,rot21 bB) B\<^sub>1) (bb,Bal) (Node B\<^sub>2 (bc,rot22 bB) C))))))"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    85
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    86
fun baldL :: "'a tree_bal change \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    87
"baldL A' ab b BC = (case A' of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    88
   Same A \<Rightarrow> Same (Node A (ab,b) BC) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    89
   Diff A \<Rightarrow> (case b of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    90
     Bal \<Rightarrow> Same (Node A (ab,Rh) BC) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    91
     Lh \<Rightarrow> Diff (Node A (ab,Bal) BC) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    92
     Rh \<Rightarrow> (case BC of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    93
       Node B (bc,Rh) C \<Rightarrow> Diff(Node (Node A (ab,Bal) B) (bc,Bal) C) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    94
       Node B (bc,Bal) C \<Rightarrow> Same(Node (Node A (ab,Rh) B) (bc,Lh) C) |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    95
       Node B (bc,Lh) C \<Rightarrow> (case B of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    96
         Node B\<^sub>1 (bb, bB) B\<^sub>2 \<Rightarrow>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    97
           Diff(Node (Node A (ab,rot21 bB) B\<^sub>1) (bb,Bal) (Node B\<^sub>2 (bc,rot22 bB) C))))))"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    98
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
    99
fun split_max :: "'a tree_bal \<Rightarrow> 'a tree_bal change * 'a" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   100
"split_max (Node l (a, ba) r) =
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   101
  (if r = Leaf then (Diff l,a) else let (r',a') = split_max r in (baldR l a ba r', a'))"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   102
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   103
fun delete :: "'a::linorder \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   104
"delete _ Leaf = Same Leaf" |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   105
"delete x (Node l (a, ba) r) =
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   106
  (case cmp x a of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   107
     EQ \<Rightarrow> if l = Leaf then Diff r
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   108
           else let (l', a') = split_max l in baldL l' a' ba r |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   109
     LT \<Rightarrow> baldL (delete x l) a ba r |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   110
     GT \<Rightarrow> baldR l a ba (delete x r))"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   111
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   112
lemmas split_max_induct = split_max.induct[case_names Node Leaf]
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   113
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   114
lemmas splits =  if_splits tree.splits tree.splits change.splits bal.splits
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   115
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   116
subsection \<open>Proofs\<close>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   117
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   118
lemma insert_Diff1[simp]: "insert x t \<noteq> Diff Leaf"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   119
by (cases t)(auto split!: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   120
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   121
lemma insert_Diff2[simp]: "insert x t = Diff (Node l (a,Bal) r) \<longleftrightarrow> t = Leaf \<and> a = x \<and> l=Leaf \<and> r=Leaf"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   122
by (cases t)(auto split!: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   123
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   124
lemma insert_Diff3[simp]: "insert x t \<noteq> Diff (Node l (a,Rh) Leaf)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   125
by (cases t)(auto split!: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   126
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   127
lemma insert_Diff4[simp]: "insert x t \<noteq> Diff (Node Leaf (a,Lh) r)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   128
by (cases t)(auto split!: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   129
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   130
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   131
subsubsection "Proofs for insert"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   132
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   133
theorem inorder_insert:
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   134
  "\<lbrakk> avl t;  sorted(inorder t) \<rbrakk> \<Longrightarrow> inorder(tree(insert x t)) = ins_list x (inorder t)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   135
by(induction t) (auto simp: ins_list_simps split!: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   136
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   137
lemma avl_insert_case: "avl t \<Longrightarrow> case insert x t of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   138
   Same t' \<Rightarrow> avl t' \<and> height t' = height t |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   139
   Diff t' \<Rightarrow> avl t' \<and> height t' = height t + 1"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   140
apply(induction x t rule: insert.induct)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   141
apply(auto simp: max_absorb1 split!: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   142
done
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   143
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   144
corollary avl_insert: "avl t \<Longrightarrow> avl(tree(insert x t))"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   145
using avl_insert_case[of t x] by (simp split: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   146
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   147
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   148
subsubsection "Proofs for delete"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   149
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   150
lemma inorder_baldL:
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   151
  "\<lbrakk> ba = Rh \<longrightarrow> r \<noteq> Leaf; avl r \<rbrakk>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   152
  \<Longrightarrow> inorder (tree(baldL l a ba r)) = inorder (tree l) @ a # inorder r"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   153
by (auto split: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   154
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   155
lemma inorder_baldR:
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   156
  "\<lbrakk> ba = Lh \<longrightarrow> l \<noteq> Leaf; avl l \<rbrakk>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   157
   \<Longrightarrow> inorder (tree(baldR l a ba r)) = inorder l @ a # inorder (tree r)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   158
by (auto split: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   159
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   160
lemma avl_split_max:
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   161
  "\<lbrakk> split_max t = (t',a); avl t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow> case t' of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   162
   Same t' \<Rightarrow> avl t' \<and> height t = height t' |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   163
   Diff t' \<Rightarrow> avl t' \<and> height t = height t' + 1"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   164
apply(induction t arbitrary: t' a rule: split_max_induct)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   165
 apply(fastforce simp: max_absorb1 max_absorb2 split!: splits prod.splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   166
apply simp
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   167
done
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   168
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   169
lemma avl_delete_case: "avl t \<Longrightarrow> case delete x t of
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   170
   Same t' \<Rightarrow> avl t' \<and> height t = height t' |
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   171
   Diff t' \<Rightarrow> avl t' \<and> height t = height t' + 1"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   172
apply(induction x t rule: delete.induct)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   173
 apply(auto simp: max_absorb1 max_absorb2 dest: avl_split_max split!: splits prod.splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   174
done
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   175
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   176
corollary avl_delete: "avl t \<Longrightarrow> avl(tree(delete x t))"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   177
using avl_delete_case[of t x] by(simp split: splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   178
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   179
lemma inorder_split_maxD:
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   180
  "\<lbrakk> split_max t = (t',a); t \<noteq> Leaf; avl t \<rbrakk> \<Longrightarrow>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   181
   inorder (tree t') @ [a] = inorder t"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   182
apply(induction t arbitrary: t' rule: split_max.induct)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   183
 apply(fastforce split!: splits prod.splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   184
apply simp
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   185
done
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   186
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   187
lemma neq_Leaf_if_height_neq_0[simp]: "height t \<noteq> 0 \<Longrightarrow> t \<noteq> Leaf"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   188
by auto
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   189
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   190
theorem inorder_delete:
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   191
  "\<lbrakk> avl t; sorted(inorder t) \<rbrakk>  \<Longrightarrow> inorder (tree(delete x t)) = del_list x (inorder t)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   192
apply(induction t rule: tree2_induct)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   193
apply(auto simp: del_list_simps inorder_baldL inorder_baldR avl_delete inorder_split_maxD
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   194
           simp del: baldR.simps baldL.simps split!: splits prod.splits)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   195
done
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   196
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   197
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   198
subsubsection \<open>Set Implementation\<close>
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   199
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   200
interpretation S: Set_by_Ordered
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   201
where empty = Leaf and isin = isin
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   202
  and insert = "\<lambda>x t. tree(insert x t)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   203
  and delete = "\<lambda>x t. tree(delete x t)"
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   204
  and inorder = inorder and inv = avl
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   205
proof (standard, goal_cases)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   206
  case 1 show ?case by (simp)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   207
next
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   208
  case 2 thus ?case by(simp add: isin_set_inorder)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   209
next
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   210
  case 3 thus ?case by(simp add: inorder_insert)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   211
next
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   212
  case 4 thus ?case by(simp add: inorder_delete)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   213
next
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   214
  case 5 thus ?case by (simp add: empty_def)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   215
next
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   216
  case 6 thus ?case by (simp add: avl_insert)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   217
next
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   218
  case 7 thus ?case by (simp add: avl_delete)
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   219
qed
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   220
a9df6686ed0e AVL trees with balance tags
nipkow
parents:
diff changeset
   221
end