src/HOL/Data_Structures/AVL_Set.thy
changeset 61647 5121b9a57cce
parent 61588 1d2907d0ed73
child 61678 b594e9277be3
     1.1 --- a/src/HOL/Data_Structures/AVL_Set.thy	Thu Nov 12 21:12:09 2015 +0100
     1.2 +++ b/src/HOL/Data_Structures/AVL_Set.thy	Fri Nov 13 12:06:50 2015 +0100
     1.3 @@ -51,25 +51,23 @@
     1.4     LT \<Rightarrow> balL (insert x l) a r |
     1.5     GT \<Rightarrow> balR l a (insert x r))"
     1.6  
     1.7 -fun delete_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
     1.8 -"delete_max (Node _ l a Leaf) = (l,a)" |
     1.9 -"delete_max (Node _ l a r) =
    1.10 -  (let (r',a') = delete_max r in (balL l a r', a'))"
    1.11 +fun del_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
    1.12 +"del_max (Node _ l a r) = (if r = Leaf then (l,a)
    1.13 +  else let (r',a') = del_max r in (balL l a r', a'))"
    1.14  
    1.15 -lemmas delete_max_induct = delete_max.induct[case_names Leaf Node]
    1.16 +lemmas del_max_induct = del_max.induct[case_names Node Leaf]
    1.17  
    1.18 -fun delete_root :: "'a avl_tree \<Rightarrow> 'a avl_tree" where
    1.19 -"delete_root (Node h Leaf a r) = r" |
    1.20 -"delete_root (Node h l a Leaf) = l" |
    1.21 -"delete_root (Node h l a r) =
    1.22 -  (let (l', a') = delete_max l in balR l' a' r)"
    1.23 +fun del_root :: "'a avl_tree \<Rightarrow> 'a avl_tree" where
    1.24 +"del_root (Node h Leaf a r) = r" |
    1.25 +"del_root (Node h l a Leaf) = l" |
    1.26 +"del_root (Node h l a r) = (let (l', a') = del_max l in balR l' a' r)"
    1.27  
    1.28 -lemmas delete_root_cases = delete_root.cases[case_names Leaf_t Node_Leaf Node_Node]
    1.29 +lemmas del_root_cases = del_root.cases[case_names Leaf_t Node_Leaf Node_Node]
    1.30  
    1.31  fun delete :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    1.32  "delete _ Leaf = Leaf" |
    1.33  "delete x (Node h l a r) = (case cmp x a of
    1.34 -   EQ \<Rightarrow> delete_root (Node h l a r) |
    1.35 +   EQ \<Rightarrow> del_root (Node h l a r) |
    1.36     LT \<Rightarrow> balR (delete x l) a r |
    1.37     GT \<Rightarrow> balL l a (delete x r))"
    1.38  
    1.39 @@ -97,22 +95,22 @@
    1.40  
    1.41  subsubsection "Proofs for delete"
    1.42  
    1.43 -lemma inorder_delete_maxD:
    1.44 -  "\<lbrakk> delete_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
    1.45 +lemma inorder_del_maxD:
    1.46 +  "\<lbrakk> del_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
    1.47     inorder t' @ [a] = inorder t"
    1.48 -by(induction t arbitrary: t' rule: delete_max.induct)
    1.49 -  (auto simp: inorder_balL split: prod.splits tree.split)
    1.50 +by(induction t arbitrary: t' rule: del_max.induct)
    1.51 +  (auto simp: inorder_balL split: if_splits prod.splits tree.split)
    1.52  
    1.53 -lemma inorder_delete_root:
    1.54 -  "inorder (delete_root (Node h l a r)) = inorder l @ inorder r"
    1.55 -by(induction "Node h l a r" arbitrary: l a r h rule: delete_root.induct)
    1.56 -  (auto simp: inorder_balR inorder_delete_maxD split: prod.splits)
    1.57 +lemma inorder_del_root:
    1.58 +  "inorder (del_root (Node h l a r)) = inorder l @ inorder r"
    1.59 +by(induction "Node h l a r" arbitrary: l a r h rule: del_root.induct)
    1.60 +  (auto simp: inorder_balL inorder_balR inorder_del_maxD split: if_splits prod.splits)
    1.61  
    1.62  theorem inorder_delete:
    1.63    "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
    1.64  by(induction t)
    1.65    (auto simp: del_list_simps inorder_balL inorder_balR
    1.66 -    inorder_delete_root inorder_delete_maxD split: prod.splits)
    1.67 +    inorder_del_root inorder_del_maxD split: prod.splits)
    1.68  
    1.69  
    1.70  subsubsection "Overall functional correctness"
    1.71 @@ -295,65 +293,61 @@
    1.72  
    1.73  subsubsection {* Deletion maintains AVL balance *}
    1.74  
    1.75 -lemma avl_delete_max:
    1.76 +lemma avl_del_max:
    1.77    assumes "avl x" and "x \<noteq> Leaf"
    1.78 -  shows "avl (fst (delete_max x))" "height x = height(fst (delete_max x)) \<or>
    1.79 -         height x = height(fst (delete_max x)) + 1"
    1.80 +  shows "avl (fst (del_max x))" "height x = height(fst (del_max x)) \<or>
    1.81 +         height x = height(fst (del_max x)) + 1"
    1.82  using assms
    1.83 -proof (induct x rule: delete_max_induct)
    1.84 -  case (Node h l a rh rl b rr)
    1.85 +proof (induct x rule: del_max_induct)
    1.86 +  case (Node h l a r)
    1.87    case 1
    1.88 -  with Node have "avl l" "avl (fst (delete_max (Node rh rl b rr)))" by auto
    1.89 -  with 1 Node have "avl (balL l a (fst (delete_max (Node rh rl b rr))))"
    1.90 -    by (intro avl_balL) fastforce+
    1.91 -  thus ?case 
    1.92 -    by (auto simp: height_balL height_balL2
    1.93 +  thus ?case using Node
    1.94 +    by (auto simp: height_balL height_balL2 avl_balL
    1.95        linorder_class.max.absorb1 linorder_class.max.absorb2
    1.96        split:prod.split)
    1.97  next
    1.98 -  case (Node h l a rh rl b rr)
    1.99 +  case (Node h l a r)
   1.100    case 2
   1.101 -  let ?r = "Node rh rl b rr"
   1.102 -  let ?r' = "fst (delete_max ?r)"
   1.103 -  from `avl x` Node 2 have "avl l" and "avl ?r" by simp_all
   1.104 +  let ?r' = "fst (del_max r)"
   1.105 +  from `avl x` Node 2 have "avl l" and "avl r" by simp_all
   1.106    thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
   1.107      apply (auto split:prod.splits simp del:avl.simps) by arith+
   1.108  qed auto
   1.109  
   1.110 -lemma avl_delete_root:
   1.111 +lemma avl_del_root:
   1.112    assumes "avl t" and "t \<noteq> Leaf"
   1.113 -  shows "avl(delete_root t)" 
   1.114 +  shows "avl(del_root t)" 
   1.115  using assms
   1.116 -proof (cases t rule:delete_root_cases)
   1.117 +proof (cases t rule:del_root_cases)
   1.118    case (Node_Node h lh ll ln lr n rh rl rn rr)
   1.119    let ?l = "Node lh ll ln lr"
   1.120    let ?r = "Node rh rl rn rr"
   1.121 -  let ?l' = "fst (delete_max ?l)"
   1.122 +  let ?l' = "fst (del_max ?l)"
   1.123    from `avl t` and Node_Node have "avl ?r" by simp
   1.124    from `avl t` and Node_Node have "avl ?l" by simp
   1.125    hence "avl(?l')" "height ?l = height(?l') \<or>
   1.126 -         height ?l = height(?l') + 1" by (rule avl_delete_max,simp)+
   1.127 +         height ?l = height(?l') + 1" by (rule avl_del_max,simp)+
   1.128    with `avl t` Node_Node have "height ?l' = height ?r \<or> height ?l' = height ?r + 1
   1.129              \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
   1.130 -  with `avl ?l'` `avl ?r` have "avl(balR ?l' (snd(delete_max ?l)) ?r)"
   1.131 +  with `avl ?l'` `avl ?r` have "avl(balR ?l' (snd(del_max ?l)) ?r)"
   1.132      by (rule avl_balR)
   1.133    with Node_Node show ?thesis by (auto split:prod.splits)
   1.134  qed simp_all
   1.135  
   1.136 -lemma height_delete_root:
   1.137 +lemma height_del_root:
   1.138    assumes "avl t" and "t \<noteq> Leaf" 
   1.139 -  shows "height t = height(delete_root t) \<or> height t = height(delete_root t) + 1"
   1.140 +  shows "height t = height(del_root t) \<or> height t = height(del_root t) + 1"
   1.141  using assms
   1.142 -proof (cases t rule: delete_root_cases)
   1.143 +proof (cases t rule: del_root_cases)
   1.144    case (Node_Node h lh ll ln lr n rh rl rn rr)
   1.145    let ?l = "Node lh ll ln lr"
   1.146    let ?r = "Node rh rl rn rr"
   1.147 -  let ?l' = "fst (delete_max ?l)"
   1.148 -  let ?t' = "balR ?l' (snd(delete_max ?l)) ?r"
   1.149 +  let ?l' = "fst (del_max ?l)"
   1.150 +  let ?t' = "balR ?l' (snd(del_max ?l)) ?r"
   1.151    from `avl t` and Node_Node have "avl ?r" by simp
   1.152    from `avl t` and Node_Node have "avl ?l" by simp
   1.153 -  hence "avl(?l')"  by (rule avl_delete_max,simp)
   1.154 -  have l'_height: "height ?l = height ?l' \<or> height ?l = height ?l' + 1" using `avl ?l` by (intro avl_delete_max) auto
   1.155 +  hence "avl(?l')"  by (rule avl_del_max,simp)
   1.156 +  have l'_height: "height ?l = height ?l' \<or> height ?l = height ?l' + 1" using `avl ?l` by (intro avl_del_max) auto
   1.157    have t_height: "height t = 1 + max (height ?l) (height ?r)" using `avl t` Node_Node by simp
   1.158    have "height t = height ?t' \<or> height t = height ?t' + 1" using  `avl t` Node_Node
   1.159    proof(cases "height ?r = height ?l' + 2")
   1.160 @@ -362,7 +356,7 @@
   1.161    next
   1.162      case True
   1.163      show ?thesis
   1.164 -    proof(cases rule: disjE[OF height_balR[OF True `avl ?l'` `avl ?r`, of "snd (delete_max ?l)"]])
   1.165 +    proof(cases rule: disjE[OF height_balR[OF True `avl ?l'` `avl ?r`, of "snd (del_max ?l)"]])
   1.166        case 1
   1.167        thus ?thesis using l'_height t_height True by arith
   1.168      next
   1.169 @@ -385,7 +379,7 @@
   1.170    with Node show ?case
   1.171    proof(cases "x = n")
   1.172      case True
   1.173 -    with Node 1 show ?thesis by (auto simp:avl_delete_root)
   1.174 +    with Node 1 show ?thesis by (auto simp:avl_del_root)
   1.175    next
   1.176      case False
   1.177      with Node 1 show ?thesis 
   1.178 @@ -401,9 +395,9 @@
   1.179    with Node show ?case
   1.180    proof(cases "x = n")
   1.181      case True
   1.182 -    with 1 have "height (Node h l n r) = height(delete_root (Node h l n r))
   1.183 -      \<or> height (Node h l n r) = height(delete_root (Node h l n r)) + 1"
   1.184 -      by (subst height_delete_root,simp_all)
   1.185 +    with 1 have "height (Node h l n r) = height(del_root (Node h l n r))
   1.186 +      \<or> height (Node h l n r) = height(del_root (Node h l n r)) + 1"
   1.187 +      by (subst height_del_root,simp_all)
   1.188      with True show ?thesis by simp
   1.189    next
   1.190      case False