src/HOL/Data_Structures/AVL_Set.thy
author nipkow
Sun, 08 Apr 2018 09:46:33 +0200
changeset 67964 08cc5ab18c84
parent 67406 23307fd33906
child 67967 5a4280946a25
permissions -rw-r--r--
better name; added binary operations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     1
(*
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
     2
Author:     Tobias Nipkow, Daniel Stüwe
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
     3
Largely derived from AFP entry AVL.
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     4
*)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     5
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     6
section "AVL Tree Implementation of Sets"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     7
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     8
theory AVL_Set
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
     9
imports
67964
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67406
diff changeset
    10
  Cmp
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67406
diff changeset
    11
  Isin2
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 63411
diff changeset
    12
  "HOL-Number_Theory.Fib"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    13
begin
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    14
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    15
type_synonym 'a avl_tree = "('a,nat) tree"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    16
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
    17
text \<open>Invariant:\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    18
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    19
fun avl :: "'a avl_tree \<Rightarrow> bool" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    20
"avl Leaf = True" |
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    21
"avl (Node h l a r) =
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    22
 ((height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1) \<and> 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    23
  h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    24
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    25
fun ht :: "'a avl_tree \<Rightarrow> nat" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    26
"ht Leaf = 0" |
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    27
"ht (Node h l a r) = h"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    28
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    29
definition node :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    30
"node l a r = Node (max (ht l) (ht r) + 1) l a r"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    31
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    32
definition balL :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    33
"balL l a r =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    34
  (if ht l = ht r + 2 then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    35
     case l of 
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    36
       Node _ bl b br \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    37
         if ht bl < ht br then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    38
           case br of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    39
             Node _ cl c cr \<Rightarrow> node (node bl b cl) c (node cr a r)
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    40
         else node bl b (node br a r)
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    41
   else node l a r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    42
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    43
definition balR :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    44
"balR l a r =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    45
   (if ht r = ht l + 2 then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    46
      case r of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    47
        Node _ bl b br \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    48
          if ht bl > ht br then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    49
            case bl of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    50
              Node _ cl c cr \<Rightarrow> node (node l a cl) c (node cr b br)
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    51
          else node (node l a bl) b br
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    52
  else node l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    53
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    54
fun insert :: "'a::linorder \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    55
"insert x Leaf = Node 1 Leaf x Leaf" |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    56
"insert x (Node h l a r) = (case cmp x a of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    57
   EQ \<Rightarrow> Node h l a r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    58
   LT \<Rightarrow> balL (insert x l) a r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    59
   GT \<Rightarrow> balR l a (insert x r))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    60
61647
nipkow
parents: 61588
diff changeset
    61
fun del_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    62
"del_max (Node _ l a r) =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    63
  (if r = Leaf then (l,a) else let (r',a') = del_max r in (balL l a r', a'))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    64
61647
nipkow
parents: 61588
diff changeset
    65
lemmas del_max_induct = del_max.induct[case_names Node Leaf]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    66
61647
nipkow
parents: 61588
diff changeset
    67
fun del_root :: "'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow
parents: 61588
diff changeset
    68
"del_root (Node h Leaf a r) = r" |
nipkow
parents: 61588
diff changeset
    69
"del_root (Node h l a Leaf) = l" |
nipkow
parents: 61588
diff changeset
    70
"del_root (Node h l a r) = (let (l', a') = del_max l in balR l' a' r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    71
61647
nipkow
parents: 61588
diff changeset
    72
lemmas del_root_cases = del_root.cases[case_names Leaf_t Node_Leaf Node_Node]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    73
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    74
fun delete :: "'a::linorder \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    75
"delete _ Leaf = Leaf" |
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    76
"delete x (Node h l a r) =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    77
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    78
     EQ \<Rightarrow> del_root (Node h l a r) |
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    79
     LT \<Rightarrow> balR (delete x l) a r |
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    80
     GT \<Rightarrow> balL l a (delete x r))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    81
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    82
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
    83
subsection \<open>Functional Correctness Proofs\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    84
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
    85
text\<open>Very different from the AFP/AVL proofs\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    86
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    87
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    88
subsubsection "Proofs for insert"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    89
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    90
lemma inorder_balL:
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    91
  "inorder (balL l a r) = inorder l @ a # inorder r"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    92
by (auto simp: node_def balL_def split:tree.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    93
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    94
lemma inorder_balR:
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    95
  "inorder (balR l a r) = inorder l @ a # inorder r"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    96
by (auto simp: node_def balR_def split:tree.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    97
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    98
theorem inorder_insert:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    99
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   100
by (induct t) 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   101
   (auto simp: ins_list_simps inorder_balL inorder_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   102
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   103
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   104
subsubsection "Proofs for delete"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   105
61647
nipkow
parents: 61588
diff changeset
   106
lemma inorder_del_maxD:
nipkow
parents: 61588
diff changeset
   107
  "\<lbrakk> del_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   108
   inorder t' @ [a] = inorder t"
61647
nipkow
parents: 61588
diff changeset
   109
by(induction t arbitrary: t' rule: del_max.induct)
nipkow
parents: 61588
diff changeset
   110
  (auto simp: inorder_balL split: if_splits prod.splits tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   111
61647
nipkow
parents: 61588
diff changeset
   112
lemma inorder_del_root:
nipkow
parents: 61588
diff changeset
   113
  "inorder (del_root (Node h l a r)) = inorder l @ inorder r"
62526
nipkow
parents: 61678
diff changeset
   114
by(cases "Node h l a r" rule: del_root.cases)
61647
nipkow
parents: 61588
diff changeset
   115
  (auto simp: inorder_balL inorder_balR inorder_del_maxD split: if_splits prod.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   116
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   117
theorem inorder_delete:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   118
  "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   119
by(induction t)
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   120
  (auto simp: del_list_simps inorder_balL inorder_balR
61647
nipkow
parents: 61588
diff changeset
   121
    inorder_del_root inorder_del_maxD split: prod.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   122
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   123
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   124
subsubsection "Overall functional correctness"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   125
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   126
interpretation Set_by_Ordered
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   127
where empty = Leaf and isin = isin and insert = insert and delete = delete
61588
nipkow
parents: 61581
diff changeset
   128
and inorder = inorder and inv = "\<lambda>_. True"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   129
proof (standard, goal_cases)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   130
  case 1 show ?case by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   131
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   132
  case 2 thus ?case by(simp add: isin_set)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   133
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   134
  case 3 thus ?case by(simp add: inorder_insert)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   135
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   136
  case 4 thus ?case by(simp add: inorder_delete)
61428
5e1938107371 added invar empty
nipkow
parents: 61232
diff changeset
   137
qed (rule TrueI)+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   138
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   139
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   140
subsection \<open>AVL invariants\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   141
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   142
text\<open>Essentially the AFP/AVL proofs\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   143
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   144
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   145
subsubsection \<open>Insertion maintains AVL balance\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   146
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   147
declare Let_def [simp]
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   148
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   149
lemma [simp]: "avl t \<Longrightarrow> ht t = height t"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   150
by (induct t) simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   151
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   152
lemma height_balL:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   153
  "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   154
   height (balL l a r) = height r + 2 \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   155
   height (balL l a r) = height r + 3"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   156
by (cases l) (auto simp:node_def balL_def split:tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   157
       
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   158
lemma height_balR:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   159
  "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   160
   height (balR l a r) = height l + 2 \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   161
   height (balR l a r) = height l + 3"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   162
by (cases r) (auto simp add:node_def balR_def split:tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   163
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   164
lemma [simp]: "height(node l a r) = max (height l) (height r) + 1"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   165
by (simp add: node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   166
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   167
lemma avl_node:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   168
  "\<lbrakk> avl l; avl r;
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   169
     height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   170
   \<rbrakk> \<Longrightarrow> avl(node l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   171
by (auto simp add:max_def node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   172
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   173
lemma height_balL2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   174
  "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   175
   height (balL l a r) = (1 + max (height l) (height r))"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   176
by (cases l, cases r) (simp_all add: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   177
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   178
lemma height_balR2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   179
  "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   180
   height (balR l a r) = (1 + max (height l) (height r))"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   181
by (cases l, cases r) (simp_all add: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   182
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   183
lemma avl_balL: 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   184
  assumes "avl l" "avl r" and "height l = height r \<or> height l = height r + 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   185
    \<or> height r = height l + 1 \<or> height l = height r + 2" 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   186
  shows "avl(balL l a r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   187
proof(cases l)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   188
  case Leaf
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   189
  with assms show ?thesis by (simp add: node_def balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   190
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   191
  case (Node ln ll lr lh)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   192
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   193
  proof(cases "height l = height r + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   194
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   195
    from True Node assms show ?thesis
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   196
      by (auto simp: balL_def intro!: avl_node split: tree.split) arith+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   197
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   198
    case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   199
    with assms show ?thesis by (simp add: avl_node balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   200
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   201
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   202
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   203
lemma avl_balR: 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   204
  assumes "avl l" and "avl r" and "height l = height r \<or> height l = height r + 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   205
    \<or> height r = height l + 1 \<or> height r = height l + 2" 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   206
  shows "avl(balR l a r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   207
proof(cases r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   208
  case Leaf
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   209
  with assms show ?thesis by (simp add: node_def balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   210
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   211
  case (Node rn rl rr rh)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   212
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   213
  proof(cases "height r = height l + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   214
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   215
      from True Node assms show ?thesis
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   216
        by (auto simp: balR_def intro!: avl_node split: tree.split) arith+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   217
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   218
    case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   219
    with assms show ?thesis by (simp add: balR_def avl_node)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   220
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   221
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   222
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   223
(* It appears that these two properties need to be proved simultaneously: *)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   224
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   225
text\<open>Insertion maintains the AVL property:\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   226
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   227
theorem avl_insert_aux:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   228
  assumes "avl t"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   229
  shows "avl(insert x t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   230
        "(height (insert x t) = height t \<or> height (insert x t) = height t + 1)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   231
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   232
proof (induction t)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   233
  case (Node h l a r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   234
  case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   235
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   236
  proof(cases "x = a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   237
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   238
    with Node 1 show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   239
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   240
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   241
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   242
    proof(cases "x<a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   243
      case True
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   244
      with Node 1 show ?thesis by (auto simp add:avl_balL)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   245
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   246
      case False
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   247
      with Node 1 \<open>x\<noteq>a\<close> show ?thesis by (auto simp add:avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   248
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   249
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   250
  case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   251
  from 2 Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   252
  proof(cases "x = a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   253
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   254
    with Node 1 show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   255
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   256
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   257
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   258
     proof(cases "x<a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   259
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   260
      with Node 2 show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   261
      proof(cases "height (insert x l) = height r + 2")
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   262
        case False with Node 2 \<open>x < a\<close> show ?thesis by (auto simp: height_balL2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   263
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   264
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   265
        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   266
          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   267
          using Node 2 by (intro height_balL) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   268
        thus ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   269
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   270
          assume ?A
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   271
          with 2 \<open>x < a\<close> show ?thesis by (auto)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   272
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   273
          assume ?B
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   274
          with True 1 Node(2) \<open>x < a\<close> show ?thesis by (simp) arith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   275
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   276
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   277
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   278
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   279
      with Node 2 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   280
      proof(cases "height (insert x r) = height l + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   281
        case False
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   282
        with Node 2 \<open>\<not>x < a\<close> show ?thesis by (auto simp: height_balR2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   283
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   284
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   285
        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   286
          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   287
          using Node 2 by (intro height_balR) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   288
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   289
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   290
          assume ?A
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   291
          with 2 \<open>\<not>x < a\<close> show ?thesis by (auto)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   292
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   293
          assume ?B
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   294
          with True 1 Node(4) \<open>\<not>x < a\<close> show ?thesis by (simp) arith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   295
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   296
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   297
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   298
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   299
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   300
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   301
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   302
subsubsection \<open>Deletion maintains AVL balance\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   303
61647
nipkow
parents: 61588
diff changeset
   304
lemma avl_del_max:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   305
  assumes "avl x" and "x \<noteq> Leaf"
61647
nipkow
parents: 61588
diff changeset
   306
  shows "avl (fst (del_max x))" "height x = height(fst (del_max x)) \<or>
nipkow
parents: 61588
diff changeset
   307
         height x = height(fst (del_max x)) + 1"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   308
using assms
61647
nipkow
parents: 61588
diff changeset
   309
proof (induct x rule: del_max_induct)
nipkow
parents: 61588
diff changeset
   310
  case (Node h l a r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   311
  case 1
61647
nipkow
parents: 61588
diff changeset
   312
  thus ?case using Node
nipkow
parents: 61588
diff changeset
   313
    by (auto simp: height_balL height_balL2 avl_balL
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   314
      linorder_class.max.absorb1 linorder_class.max.absorb2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   315
      split:prod.split)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   316
next
61647
nipkow
parents: 61588
diff changeset
   317
  case (Node h l a r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   318
  case 2
61647
nipkow
parents: 61588
diff changeset
   319
  let ?r' = "fst (del_max r)"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   320
  from \<open>avl x\<close> Node 2 have "avl l" and "avl r" by simp_all
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   321
  thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   322
    apply (auto split:prod.splits simp del:avl.simps) by arith+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   323
qed auto
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   324
61647
nipkow
parents: 61588
diff changeset
   325
lemma avl_del_root:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   326
  assumes "avl t" and "t \<noteq> Leaf"
61647
nipkow
parents: 61588
diff changeset
   327
  shows "avl(del_root t)" 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   328
using assms
61647
nipkow
parents: 61588
diff changeset
   329
proof (cases t rule:del_root_cases)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   330
  case (Node_Node h lh ll ln lr n rh rl rn rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   331
  let ?l = "Node lh ll ln lr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   332
  let ?r = "Node rh rl rn rr"
61647
nipkow
parents: 61588
diff changeset
   333
  let ?l' = "fst (del_max ?l)"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   334
  from \<open>avl t\<close> and Node_Node have "avl ?r" by simp
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   335
  from \<open>avl t\<close> and Node_Node have "avl ?l" by simp
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   336
  hence "avl(?l')" "height ?l = height(?l') \<or>
61647
nipkow
parents: 61588
diff changeset
   337
         height ?l = height(?l') + 1" by (rule avl_del_max,simp)+
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   338
  with \<open>avl t\<close> Node_Node have "height ?l' = height ?r \<or> height ?l' = height ?r + 1
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   339
            \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   340
  with \<open>avl ?l'\<close> \<open>avl ?r\<close> have "avl(balR ?l' (snd(del_max ?l)) ?r)"
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   341
    by (rule avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   342
  with Node_Node show ?thesis by (auto split:prod.splits)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   343
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   344
61647
nipkow
parents: 61588
diff changeset
   345
lemma height_del_root:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   346
  assumes "avl t" and "t \<noteq> Leaf" 
61647
nipkow
parents: 61588
diff changeset
   347
  shows "height t = height(del_root t) \<or> height t = height(del_root t) + 1"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   348
using assms
61647
nipkow
parents: 61588
diff changeset
   349
proof (cases t rule: del_root_cases)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   350
  case (Node_Node h lh ll ln lr n rh rl rn rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   351
  let ?l = "Node lh ll ln lr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   352
  let ?r = "Node rh rl rn rr"
61647
nipkow
parents: 61588
diff changeset
   353
  let ?l' = "fst (del_max ?l)"
nipkow
parents: 61588
diff changeset
   354
  let ?t' = "balR ?l' (snd(del_max ?l)) ?r"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   355
  from \<open>avl t\<close> and Node_Node have "avl ?r" by simp
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   356
  from \<open>avl t\<close> and Node_Node have "avl ?l" by simp
61647
nipkow
parents: 61588
diff changeset
   357
  hence "avl(?l')"  by (rule avl_del_max,simp)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   358
  have l'_height: "height ?l = height ?l' \<or> height ?l = height ?l' + 1" using \<open>avl ?l\<close> by (intro avl_del_max) auto
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   359
  have t_height: "height t = 1 + max (height ?l) (height ?r)" using \<open>avl t\<close> Node_Node by simp
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   360
  have "height t = height ?t' \<or> height t = height ?t' + 1" using  \<open>avl t\<close> Node_Node
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   361
  proof(cases "height ?r = height ?l' + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   362
    case False
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   363
    show ?thesis using l'_height t_height False by (subst  height_balR2[OF \<open>avl ?l'\<close> \<open>avl ?r\<close> False])+ arith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   364
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   365
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   366
    show ?thesis
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   367
    proof(cases rule: disjE[OF height_balR[OF True \<open>avl ?l'\<close> \<open>avl ?r\<close>, of "snd (del_max ?l)"]])
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   368
      case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   369
      thus ?thesis using l'_height t_height True by arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   370
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   371
      case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   372
      thus ?thesis using l'_height t_height True by arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   373
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   374
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   375
  thus ?thesis using Node_Node by (auto split:prod.splits)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   376
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   377
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   378
text\<open>Deletion maintains the AVL property:\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   379
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   380
theorem avl_delete_aux:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   381
  assumes "avl t" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   382
  shows "avl(delete x t)" and "height t = (height (delete x t)) \<or> height t = height (delete x t) + 1"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   383
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   384
proof (induct t)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   385
  case (Node h l n r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   386
  case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   387
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   388
  proof(cases "x = n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   389
    case True
61647
nipkow
parents: 61588
diff changeset
   390
    with Node 1 show ?thesis by (auto simp:avl_del_root)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   391
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   392
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   393
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   394
    proof(cases "x<n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   395
      case True
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   396
      with Node 1 show ?thesis by (auto simp add:avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   397
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   398
      case False
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   399
      with Node 1 \<open>x\<noteq>n\<close> show ?thesis by (auto simp add:avl_balL)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   400
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   401
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   402
  case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   403
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   404
  proof(cases "x = n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   405
    case True
61647
nipkow
parents: 61588
diff changeset
   406
    with 1 have "height (Node h l n r) = height(del_root (Node h l n r))
nipkow
parents: 61588
diff changeset
   407
      \<or> height (Node h l n r) = height(del_root (Node h l n r)) + 1"
nipkow
parents: 61588
diff changeset
   408
      by (subst height_del_root,simp_all)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   409
    with True show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   410
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   411
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   412
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   413
     proof(cases "x<n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   414
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   415
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   416
      proof(cases "height r = height (delete x l) + 2")
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   417
        case False with Node 1 \<open>x < n\<close> show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   418
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   419
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   420
        hence "(height (balR (delete x l) n r) = height (delete x l) + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   421
          height (balR (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   422
          using Node 2 by (intro height_balR) auto
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   423
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   424
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   425
          assume ?A
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   426
          with \<open>x < n\<close> Node 2 show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   427
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   428
          assume ?B
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   429
          with \<open>x < n\<close> Node 2 show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   430
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   431
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   432
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   433
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   434
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   435
      proof(cases "height l = height (delete x r) + 2")
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   436
        case False with Node 1 \<open>\<not>x < n\<close> \<open>x \<noteq> n\<close> show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   437
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   438
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   439
        hence "(height (balL l n (delete x r)) = height (delete x r) + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   440
          height (balL l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   441
          using Node 2 by (intro height_balL) auto
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   442
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   443
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   444
          assume ?A
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   445
          with \<open>\<not>x < n\<close> \<open>x \<noteq> n\<close> Node 2 show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   446
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   447
          assume ?B
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   448
          with \<open>\<not>x < n\<close> \<open>x \<noteq> n\<close> Node 2 show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   449
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   450
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   451
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   452
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   453
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   454
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   455
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   456
subsection \<open>Height-Size Relation\<close>
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   457
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   458
text \<open>By Daniel St\"uwe\<close>
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   459
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   460
fun fib_tree :: "nat \<Rightarrow> unit avl_tree" where
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   461
"fib_tree 0 = Leaf" |
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   462
"fib_tree (Suc 0) = Node 1 Leaf () Leaf" |
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   463
"fib_tree (Suc(Suc n)) = Node (Suc(Suc(n))) (fib_tree (Suc n)) () (fib_tree n)"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   464
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   465
lemma [simp]: "ht (fib_tree h) = h"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   466
by (induction h rule: "fib_tree.induct") auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   467
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   468
lemma [simp]: "height (fib_tree h) = h"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   469
by (induction h rule: "fib_tree.induct") auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   470
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   471
lemma "avl(fib_tree h)"          
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   472
by (induction h rule: "fib_tree.induct") auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   473
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   474
lemma fib_tree_size1: "size1 (fib_tree h) = fib (h+2)"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   475
by (induction h rule: fib_tree.induct) auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   476
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   477
lemma height_invers[simp]: 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   478
  "(height t = 0) = (t = Leaf)"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   479
  "avl t \<Longrightarrow> (height t = Suc h) = (\<exists> l a r . t = Node (Suc h) l a r)"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   480
by (induction t) auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   481
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   482
lemma fib_Suc_lt: "fib n \<le> fib (Suc n)"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   483
by (induction n rule: fib.induct) auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   484
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   485
lemma fib_mono: "n \<le> m \<Longrightarrow> fib n \<le> fib m"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   486
proof (induction n arbitrary: m rule: fib.induct )
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   487
  case (2 m)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   488
  thus ?case using fib_neq_0_nat[of m] by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   489
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   490
  case (3 n m)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   491
  from 3 obtain m' where "m = Suc (Suc m')"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   492
    by (metis le_Suc_ex plus_nat.simps(2)) 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   493
  thus ?case using 3(1)[of "Suc m'"] 3(2)[of m'] 3(3) by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   494
qed simp
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   495
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   496
lemma size1_fib_tree_mono:
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   497
  assumes "n \<le> m"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   498
  shows   "size1 (fib_tree n) \<le> size1 (fib_tree m)"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   499
using fib_tree_size1 fib_mono[OF assms] fib_mono[of "Suc n"] add_le_mono assms by fastforce 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   500
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   501
lemma fib_tree_minimal: "avl t \<Longrightarrow> size1 (fib_tree (ht t)) \<le> size1 t"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   502
proof (induction "ht t" arbitrary: t rule: fib_tree.induct)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   503
  case (2 t)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   504
  from 2 obtain l a r where "t = Node (Suc 0) l a r" by (cases t) auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   505
  with 2 show ?case by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   506
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   507
  case (3 h t)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   508
  note [simp] = 3(3)[symmetric] 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   509
  from 3 obtain l a r where [simp]: "t = Node (Suc (Suc h)) l a r" by (cases t) auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   510
  show ?case proof (cases rule: linorder_cases[of "ht l" "ht r"]) 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   511
    case equal
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   512
    with 3(3,4) have ht: "ht l = Suc h" "ht r = Suc h" by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   513
    with 3 have "size1 (fib_tree (ht l)) \<le> size1 l" by auto moreover
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   514
    from 3(1)[of r] 3(3,4) ht(2) have "size1 (fib_tree (ht r)) \<le> size1 r" by auto ultimately
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   515
    show ?thesis using ht size1_fib_tree_mono[of h "Suc h"] by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   516
  next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   517
    case greater
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   518
    with 3(3,4) have ht: "ht l = Suc h"  "ht r = h" by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   519
    from ht 3(1,2,4) have "size1 (fib_tree (Suc h)) \<le> size1 l" by auto moreover
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   520
    from ht 3(1,2,4) have "size1 (fib_tree h) \<le> size1 r" by auto ultimately
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   521
    show ?thesis by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   522
  next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   523
    case less (* analogously *)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   524
    with 3 have ht: "ht l = h"  "Suc h = ht r" by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   525
    from ht 3 have "size1 (fib_tree h) \<le> size1 l" by auto moreover
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   526
    from ht 3 have "size1 (fib_tree (Suc h)) \<le> size1 r" by auto ultimately
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   527
    show ?thesis by auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   528
  qed
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   529
qed auto
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   530
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   531
theorem avl_size_bound: "avl t \<Longrightarrow> fib(height t + 2) \<le> size1 t" 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   532
using fib_tree_minimal fib_tree_size1 by fastforce
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   533
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   534
end