Convertd to 3-way comparisons
authornipkow
Thu Nov 05 08:27:14 2015 +0100 (2015-11-05)
changeset 6158100d9682e8dd7
parent 61569 947ce60a06e1
child 61582 69492d32263a
Convertd to 3-way comparisons
src/HOL/Data_Structures/AVL_Map.thy
src/HOL/Data_Structures/AVL_Set.thy
src/HOL/Data_Structures/Cmp.thy
src/HOL/Data_Structures/RBT_Map.thy
src/HOL/Data_Structures/RBT_Set.thy
src/HOL/Data_Structures/Splay_Map.thy
src/HOL/Data_Structures/Splay_Set.thy
src/HOL/Data_Structures/Tree234_Map.thy
src/HOL/Data_Structures/Tree234_Set.thy
src/HOL/Data_Structures/Tree23_Map.thy
src/HOL/Data_Structures/Tree23_Set.thy
src/HOL/Data_Structures/Tree_Map.thy
src/HOL/Data_Structures/Tree_Set.thy
     1.1 --- a/src/HOL/Data_Structures/AVL_Map.thy	Wed Nov 04 15:07:23 2015 +0100
     1.2 +++ b/src/HOL/Data_Structures/AVL_Map.thy	Thu Nov 05 08:27:14 2015 +0100
     1.3 @@ -8,36 +8,34 @@
     1.4    Lookup2
     1.5  begin
     1.6  
     1.7 -fun update :: "'a::order \<Rightarrow> 'b \<Rightarrow> ('a*'b) avl_tree \<Rightarrow> ('a*'b) avl_tree" where
     1.8 +fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) avl_tree \<Rightarrow> ('a*'b) avl_tree" where
     1.9  "update x y Leaf = Node 1 Leaf (x,y) Leaf" |
    1.10 -"update x y (Node h l (a,b) r) = 
    1.11 -   (if x = a then Node h l (x,y) r else
    1.12 -    if x < a then node_bal_l (update x y l) (a,b) r
    1.13 -    else node_bal_r l (a,b) (update x y r))"
    1.14 +"update x y (Node h l (a,b) r) = (case cmp x a of
    1.15 +   EQ \<Rightarrow> Node h l (x,y) r |
    1.16 +   LT \<Rightarrow> balL (update x y l) (a,b) r |
    1.17 +   GT \<Rightarrow> balR l (a,b) (update x y r))"
    1.18  
    1.19 -fun delete :: "'a::order \<Rightarrow> ('a*'b) avl_tree \<Rightarrow> ('a*'b) avl_tree" where
    1.20 +fun delete :: "'a::cmp \<Rightarrow> ('a*'b) avl_tree \<Rightarrow> ('a*'b) avl_tree" where
    1.21  "delete _ Leaf = Leaf" |
    1.22 -"delete x (Node h l (a,b) r) = (
    1.23 -   if x = a then delete_root (Node h l (a,b) r) else
    1.24 -   if x < a then node_bal_r (delete x l) (a,b) r
    1.25 -   else node_bal_l l (a,b) (delete x r))"
    1.26 +"delete x (Node h l (a,b) r) = (case cmp x a of
    1.27 +   EQ \<Rightarrow> delete_root (Node h l (a,b) r) |
    1.28 +   LT \<Rightarrow> balR (delete x l) (a,b) r |
    1.29 +   GT \<Rightarrow> balL l (a,b) (delete x r))"
    1.30  
    1.31  
    1.32  subsection {* Functional Correctness Proofs *}
    1.33  
    1.34  theorem inorder_update:
    1.35    "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
    1.36 -by (induct t) 
    1.37 -   (auto simp: upd_list_simps inorder_node_bal_l inorder_node_bal_r)
    1.38 +by (induct t) (auto simp: upd_list_simps inorder_balL inorder_balR)
    1.39  
    1.40  
    1.41  theorem inorder_delete:
    1.42    "sorted1(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
    1.43  by(induction t)
    1.44 -  (auto simp: del_list_simps inorder_node_bal_l inorder_node_bal_r
    1.45 +  (auto simp: del_list_simps inorder_balL inorder_balR
    1.46       inorder_delete_root inorder_delete_maxD split: prod.splits)
    1.47  
    1.48 -
    1.49  interpretation Map_by_Ordered
    1.50  where empty = Leaf and lookup = lookup and update = update and delete = delete
    1.51  and inorder = inorder and wf = "\<lambda>_. True"
     2.1 --- a/src/HOL/Data_Structures/AVL_Set.thy	Wed Nov 04 15:07:23 2015 +0100
     2.2 +++ b/src/HOL/Data_Structures/AVL_Set.thy	Thu Nov 05 08:27:14 2015 +0100
     2.3 @@ -6,7 +6,7 @@
     2.4  section "AVL Tree Implementation of Sets"
     2.5  
     2.6  theory AVL_Set
     2.7 -imports Isin2
     2.8 +imports Cmp Isin2
     2.9  begin
    2.10  
    2.11  type_synonym 'a avl_tree = "('a,nat) tree"
    2.12 @@ -26,8 +26,8 @@
    2.13  definition node :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.14  "node l a r = Node (max (ht l) (ht r) + 1) l a r"
    2.15  
    2.16 -definition node_bal_l :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.17 -"node_bal_l l a r = (
    2.18 +definition balL :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.19 +"balL l a r = (
    2.20    if ht l = ht r + 2 then (case l of 
    2.21      Node _ bl b br \<Rightarrow> (if ht bl < ht br
    2.22      then case br of
    2.23 @@ -35,8 +35,8 @@
    2.24      else node bl b (node br a r)))
    2.25    else node l a r)"
    2.26  
    2.27 -definition node_bal_r :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.28 -"node_bal_r l a r = (
    2.29 +definition balR :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.30 +"balR l a r = (
    2.31    if ht r = ht l + 2 then (case r of
    2.32      Node _ bl b br \<Rightarrow> (if ht bl > ht br
    2.33      then case bl of
    2.34 @@ -44,19 +44,17 @@
    2.35      else node (node l a bl) b br))
    2.36    else node l a r)"
    2.37  
    2.38 -fun insert :: "'a::order \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.39 +fun insert :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.40  "insert x Leaf = Node 1 Leaf x Leaf" |
    2.41 -"insert x (Node h l a r) = 
    2.42 -   (if x=a then Node h l a r
    2.43 -    else if x<a
    2.44 -      then node_bal_l (insert x l) a r
    2.45 -      else node_bal_r l a (insert x r))"
    2.46 +"insert x (Node h l a r) = (case cmp x a of
    2.47 +   EQ \<Rightarrow> Node h l a r |
    2.48 +   LT \<Rightarrow> balL (insert x l) a r |
    2.49 +   GT \<Rightarrow> balR l a (insert x r))"
    2.50  
    2.51  fun delete_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
    2.52  "delete_max (Node _ l a Leaf) = (l,a)" |
    2.53 -"delete_max (Node _ l a r) = (
    2.54 -  let (r',a') = delete_max r in
    2.55 -  (node_bal_l l a r', a'))"
    2.56 +"delete_max (Node _ l a r) =
    2.57 +  (let (r',a') = delete_max r in (balL l a r', a'))"
    2.58  
    2.59  lemmas delete_max_induct = delete_max.induct[case_names Leaf Node]
    2.60  
    2.61 @@ -64,16 +62,16 @@
    2.62  "delete_root (Node h Leaf a r) = r" |
    2.63  "delete_root (Node h l a Leaf) = l" |
    2.64  "delete_root (Node h l a r) =
    2.65 -  (let (l', a') = delete_max l in node_bal_r l' a' r)"
    2.66 +  (let (l', a') = delete_max l in balR l' a' r)"
    2.67  
    2.68  lemmas delete_root_cases = delete_root.cases[case_names Leaf_t Node_Leaf Node_Node]
    2.69  
    2.70 -fun delete :: "'a::order \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.71 +fun delete :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
    2.72  "delete _ Leaf = Leaf" |
    2.73 -"delete x (Node h l a r) = (
    2.74 -   if x = a then delete_root (Node h l a r)
    2.75 -   else if x < a then node_bal_r (delete x l) a r
    2.76 -   else node_bal_l l a (delete x r))"
    2.77 +"delete x (Node h l a r) = (case cmp x a of
    2.78 +   EQ \<Rightarrow> delete_root (Node h l a r) |
    2.79 +   LT \<Rightarrow> balR (delete x l) a r |
    2.80 +   GT \<Rightarrow> balL l a (delete x r))"
    2.81  
    2.82  
    2.83  subsection {* Functional Correctness Proofs *}
    2.84 @@ -83,18 +81,18 @@
    2.85  
    2.86  subsubsection "Proofs for insert"
    2.87  
    2.88 -lemma inorder_node_bal_l:
    2.89 -  "inorder (node_bal_l l a r) = inorder l @ a # inorder r"
    2.90 -by (auto simp: node_def node_bal_l_def split:tree.splits)
    2.91 +lemma inorder_balL:
    2.92 +  "inorder (balL l a r) = inorder l @ a # inorder r"
    2.93 +by (auto simp: node_def balL_def split:tree.splits)
    2.94  
    2.95 -lemma inorder_node_bal_r:
    2.96 -  "inorder (node_bal_r l a r) = inorder l @ a # inorder r"
    2.97 -by (auto simp: node_def node_bal_r_def split:tree.splits)
    2.98 +lemma inorder_balR:
    2.99 +  "inorder (balR l a r) = inorder l @ a # inorder r"
   2.100 +by (auto simp: node_def balR_def split:tree.splits)
   2.101  
   2.102  theorem inorder_insert:
   2.103    "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
   2.104  by (induct t) 
   2.105 -   (auto simp: ins_list_simps inorder_node_bal_l inorder_node_bal_r)
   2.106 +   (auto simp: ins_list_simps inorder_balL inorder_balR)
   2.107  
   2.108  
   2.109  subsubsection "Proofs for delete"
   2.110 @@ -103,17 +101,17 @@
   2.111    "\<lbrakk> delete_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
   2.112     inorder t' @ [a] = inorder t"
   2.113  by(induction t arbitrary: t' rule: delete_max.induct)
   2.114 -  (auto simp: inorder_node_bal_l split: prod.splits tree.split)
   2.115 +  (auto simp: inorder_balL split: prod.splits tree.split)
   2.116  
   2.117  lemma inorder_delete_root:
   2.118    "inorder (delete_root (Node h l a r)) = inorder l @ inorder r"
   2.119  by(induction "Node h l a r" arbitrary: l a r h rule: delete_root.induct)
   2.120 -  (auto simp: inorder_node_bal_r inorder_delete_maxD split: prod.splits)
   2.121 +  (auto simp: inorder_balR inorder_delete_maxD split: prod.splits)
   2.122  
   2.123  theorem inorder_delete:
   2.124    "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
   2.125  by(induction t)
   2.126 -  (auto simp: del_list_simps inorder_node_bal_l inorder_node_bal_r
   2.127 +  (auto simp: del_list_simps inorder_balL inorder_balR
   2.128      inorder_delete_root inorder_delete_maxD split: prod.splits)
   2.129  
   2.130  
   2.131 @@ -145,17 +143,17 @@
   2.132  lemma [simp]: "avl t \<Longrightarrow> ht t = height t"
   2.133  by (induct t) simp_all
   2.134  
   2.135 -lemma height_node_bal_l:
   2.136 +lemma height_balL:
   2.137    "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
   2.138 -   height (node_bal_l l a r) = height r + 2 \<or>
   2.139 -   height (node_bal_l l a r) = height r + 3"
   2.140 -by (cases l) (auto simp:node_def node_bal_l_def split:tree.split)
   2.141 +   height (balL l a r) = height r + 2 \<or>
   2.142 +   height (balL l a r) = height r + 3"
   2.143 +by (cases l) (auto simp:node_def balL_def split:tree.split)
   2.144         
   2.145 -lemma height_node_bal_r:
   2.146 +lemma height_balR:
   2.147    "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
   2.148 -   height (node_bal_r l a r) = height l + 2 \<or>
   2.149 -   height (node_bal_r l a r) = height l + 3"
   2.150 -by (cases r) (auto simp add:node_def node_bal_r_def split:tree.split)
   2.151 +   height (balR l a r) = height l + 2 \<or>
   2.152 +   height (balR l a r) = height l + 3"
   2.153 +by (cases r) (auto simp add:node_def balR_def split:tree.split)
   2.154  
   2.155  lemma [simp]: "height(node l a r) = max (height l) (height r) + 1"
   2.156  by (simp add: node_def)
   2.157 @@ -166,53 +164,53 @@
   2.158     \<rbrakk> \<Longrightarrow> avl(node l a r)"
   2.159  by (auto simp add:max_def node_def)
   2.160  
   2.161 -lemma height_node_bal_l2:
   2.162 +lemma height_balL2:
   2.163    "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
   2.164 -   height (node_bal_l l a r) = (1 + max (height l) (height r))"
   2.165 -by (cases l, cases r) (simp_all add: node_bal_l_def)
   2.166 +   height (balL l a r) = (1 + max (height l) (height r))"
   2.167 +by (cases l, cases r) (simp_all add: balL_def)
   2.168  
   2.169 -lemma height_node_bal_r2:
   2.170 +lemma height_balR2:
   2.171    "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
   2.172 -   height (node_bal_r l a r) = (1 + max (height l) (height r))"
   2.173 -by (cases l, cases r) (simp_all add: node_bal_r_def)
   2.174 +   height (balR l a r) = (1 + max (height l) (height r))"
   2.175 +by (cases l, cases r) (simp_all add: balR_def)
   2.176  
   2.177 -lemma avl_node_bal_l: 
   2.178 +lemma avl_balL: 
   2.179    assumes "avl l" "avl r" and "height l = height r \<or> height l = height r + 1
   2.180      \<or> height r = height l + 1 \<or> height l = height r + 2" 
   2.181 -  shows "avl(node_bal_l l a r)"
   2.182 +  shows "avl(balL l a r)"
   2.183  proof(cases l)
   2.184    case Leaf
   2.185 -  with assms show ?thesis by (simp add: node_def node_bal_l_def)
   2.186 +  with assms show ?thesis by (simp add: node_def balL_def)
   2.187  next
   2.188    case (Node ln ll lr lh)
   2.189    with assms show ?thesis
   2.190    proof(cases "height l = height r + 2")
   2.191      case True
   2.192      from True Node assms show ?thesis
   2.193 -      by (auto simp: node_bal_l_def intro!: avl_node split: tree.split) arith+
   2.194 +      by (auto simp: balL_def intro!: avl_node split: tree.split) arith+
   2.195    next
   2.196      case False
   2.197 -    with assms show ?thesis by (simp add: avl_node node_bal_l_def)
   2.198 +    with assms show ?thesis by (simp add: avl_node balL_def)
   2.199    qed
   2.200  qed
   2.201  
   2.202 -lemma avl_node_bal_r: 
   2.203 +lemma avl_balR: 
   2.204    assumes "avl l" and "avl r" and "height l = height r \<or> height l = height r + 1
   2.205      \<or> height r = height l + 1 \<or> height r = height l + 2" 
   2.206 -  shows "avl(node_bal_r l a r)"
   2.207 +  shows "avl(balR l a r)"
   2.208  proof(cases r)
   2.209    case Leaf
   2.210 -  with assms show ?thesis by (simp add: node_def node_bal_r_def)
   2.211 +  with assms show ?thesis by (simp add: node_def balR_def)
   2.212  next
   2.213    case (Node rn rl rr rh)
   2.214    with assms show ?thesis
   2.215    proof(cases "height r = height l + 2")
   2.216      case True
   2.217        from True Node assms show ?thesis
   2.218 -        by (auto simp: node_bal_r_def intro!: avl_node split: tree.split) arith+
   2.219 +        by (auto simp: balR_def intro!: avl_node split: tree.split) arith+
   2.220    next
   2.221      case False
   2.222 -    with assms show ?thesis by (simp add: node_bal_r_def avl_node)
   2.223 +    with assms show ?thesis by (simp add: balR_def avl_node)
   2.224    qed
   2.225  qed
   2.226  
   2.227 @@ -237,10 +235,10 @@
   2.228      with Node 1 show ?thesis 
   2.229      proof(cases "x<a")
   2.230        case True
   2.231 -      with Node 1 show ?thesis by (auto simp add:avl_node_bal_l)
   2.232 +      with Node 1 show ?thesis by (auto simp add:avl_balL)
   2.233      next
   2.234        case False
   2.235 -      with Node 1 `x\<noteq>a` show ?thesis by (auto simp add:avl_node_bal_r)
   2.236 +      with Node 1 `x\<noteq>a` show ?thesis by (auto simp add:avl_balR)
   2.237      qed
   2.238    qed
   2.239    case 2
   2.240 @@ -255,12 +253,12 @@
   2.241        case True
   2.242        with Node 2 show ?thesis
   2.243        proof(cases "height (insert x l) = height r + 2")
   2.244 -        case False with Node 2 `x < a` show ?thesis by (auto simp: height_node_bal_l2)
   2.245 +        case False with Node 2 `x < a` show ?thesis by (auto simp: height_balL2)
   2.246        next
   2.247          case True 
   2.248 -        hence "(height (node_bal_l (insert x l) a r) = height r + 2) \<or>
   2.249 -          (height (node_bal_l (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
   2.250 -          using Node 2 by (intro height_node_bal_l) simp_all
   2.251 +        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
   2.252 +          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
   2.253 +          using Node 2 by (intro height_balL) simp_all
   2.254          thus ?thesis
   2.255          proof
   2.256            assume ?A
   2.257 @@ -275,12 +273,12 @@
   2.258        with Node 2 show ?thesis 
   2.259        proof(cases "height (insert x r) = height l + 2")
   2.260          case False
   2.261 -        with Node 2 `\<not>x < a` show ?thesis by (auto simp: height_node_bal_r2)
   2.262 +        with Node 2 `\<not>x < a` show ?thesis by (auto simp: height_balR2)
   2.263        next
   2.264          case True 
   2.265 -        hence "(height (node_bal_r l a (insert x r)) = height l + 2) \<or>
   2.266 -          (height (node_bal_r l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
   2.267 -          using Node 2 by (intro height_node_bal_r) simp_all
   2.268 +        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
   2.269 +          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
   2.270 +          using Node 2 by (intro height_balR) simp_all
   2.271          thus ?thesis 
   2.272          proof
   2.273            assume ?A
   2.274 @@ -306,10 +304,10 @@
   2.275    case (Node h l a rh rl b rr)
   2.276    case 1
   2.277    with Node have "avl l" "avl (fst (delete_max (Node rh rl b rr)))" by auto
   2.278 -  with 1 Node have "avl (node_bal_l l a (fst (delete_max (Node rh rl b rr))))"
   2.279 -    by (intro avl_node_bal_l) fastforce+
   2.280 +  with 1 Node have "avl (balL l a (fst (delete_max (Node rh rl b rr))))"
   2.281 +    by (intro avl_balL) fastforce+
   2.282    thus ?case 
   2.283 -    by (auto simp: height_node_bal_l height_node_bal_l2
   2.284 +    by (auto simp: height_balL height_balL2
   2.285        linorder_class.max.absorb1 linorder_class.max.absorb2
   2.286        split:prod.split)
   2.287  next
   2.288 @@ -318,7 +316,7 @@
   2.289    let ?r = "Node rh rl b rr"
   2.290    let ?r' = "fst (delete_max ?r)"
   2.291    from `avl x` Node 2 have "avl l" and "avl ?r" by simp_all
   2.292 -  thus ?case using Node 2 height_node_bal_l[of l ?r' a] height_node_bal_l2[of l ?r' a]
   2.293 +  thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
   2.294      apply (auto split:prod.splits simp del:avl.simps) by arith+
   2.295  qed auto
   2.296  
   2.297 @@ -337,8 +335,8 @@
   2.298           height ?l = height(?l') + 1" by (rule avl_delete_max,simp)+
   2.299    with `avl t` Node_Node have "height ?l' = height ?r \<or> height ?l' = height ?r + 1
   2.300              \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
   2.301 -  with `avl ?l'` `avl ?r` have "avl(node_bal_r ?l' (snd(delete_max ?l)) ?r)"
   2.302 -    by (rule avl_node_bal_r)
   2.303 +  with `avl ?l'` `avl ?r` have "avl(balR ?l' (snd(delete_max ?l)) ?r)"
   2.304 +    by (rule avl_balR)
   2.305    with Node_Node show ?thesis by (auto split:prod.splits)
   2.306  qed simp_all
   2.307  
   2.308 @@ -351,7 +349,7 @@
   2.309    let ?l = "Node lh ll ln lr"
   2.310    let ?r = "Node rh rl rn rr"
   2.311    let ?l' = "fst (delete_max ?l)"
   2.312 -  let ?t' = "node_bal_r ?l' (snd(delete_max ?l)) ?r"
   2.313 +  let ?t' = "balR ?l' (snd(delete_max ?l)) ?r"
   2.314    from `avl t` and Node_Node have "avl ?r" by simp
   2.315    from `avl t` and Node_Node have "avl ?l" by simp
   2.316    hence "avl(?l')"  by (rule avl_delete_max,simp)
   2.317 @@ -360,11 +358,11 @@
   2.318    have "height t = height ?t' \<or> height t = height ?t' + 1" using  `avl t` Node_Node
   2.319    proof(cases "height ?r = height ?l' + 2")
   2.320      case False
   2.321 -    show ?thesis using l'_height t_height False by (subst  height_node_bal_r2[OF `avl ?l'` `avl ?r` False])+ arith
   2.322 +    show ?thesis using l'_height t_height False by (subst  height_balR2[OF `avl ?l'` `avl ?r` False])+ arith
   2.323    next
   2.324      case True
   2.325      show ?thesis
   2.326 -    proof(cases rule: disjE[OF height_node_bal_r[OF True `avl ?l'` `avl ?r`, of "snd (delete_max ?l)"]])
   2.327 +    proof(cases rule: disjE[OF height_balR[OF True `avl ?l'` `avl ?r`, of "snd (delete_max ?l)"]])
   2.328        case 1
   2.329        thus ?thesis using l'_height t_height True by arith
   2.330      next
   2.331 @@ -393,10 +391,10 @@
   2.332      with Node 1 show ?thesis 
   2.333      proof(cases "x<n")
   2.334        case True
   2.335 -      with Node 1 show ?thesis by (auto simp add:avl_node_bal_r)
   2.336 +      with Node 1 show ?thesis by (auto simp add:avl_balR)
   2.337      next
   2.338        case False
   2.339 -      with Node 1 `x\<noteq>n` show ?thesis by (auto simp add:avl_node_bal_l)
   2.340 +      with Node 1 `x\<noteq>n` show ?thesis by (auto simp add:avl_balL)
   2.341      qed
   2.342    qed
   2.343    case 2
   2.344 @@ -414,38 +412,38 @@
   2.345        case True
   2.346        show ?thesis
   2.347        proof(cases "height r = height (delete x l) + 2")
   2.348 -        case False with Node 1 `x < n` show ?thesis by(auto simp: node_bal_r_def)
   2.349 +        case False with Node 1 `x < n` show ?thesis by(auto simp: balR_def)
   2.350        next
   2.351          case True 
   2.352 -        hence "(height (node_bal_r (delete x l) n r) = height (delete x l) + 2) \<or>
   2.353 -          height (node_bal_r (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
   2.354 -          using Node 2 by (intro height_node_bal_r) auto
   2.355 +        hence "(height (balR (delete x l) n r) = height (delete x l) + 2) \<or>
   2.356 +          height (balR (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
   2.357 +          using Node 2 by (intro height_balR) auto
   2.358          thus ?thesis 
   2.359          proof
   2.360            assume ?A
   2.361 -          with `x < n` Node 2 show ?thesis by(auto simp: node_bal_r_def)
   2.362 +          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
   2.363          next
   2.364            assume ?B
   2.365 -          with `x < n` Node 2 show ?thesis by(auto simp: node_bal_r_def)
   2.366 +          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
   2.367          qed
   2.368        qed
   2.369      next
   2.370        case False
   2.371        show ?thesis
   2.372        proof(cases "height l = height (delete x r) + 2")
   2.373 -        case False with Node 1 `\<not>x < n` `x \<noteq> n` show ?thesis by(auto simp: node_bal_l_def)
   2.374 +        case False with Node 1 `\<not>x < n` `x \<noteq> n` show ?thesis by(auto simp: balL_def)
   2.375        next
   2.376          case True 
   2.377 -        hence "(height (node_bal_l l n (delete x r)) = height (delete x r) + 2) \<or>
   2.378 -          height (node_bal_l l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
   2.379 -          using Node 2 by (intro height_node_bal_l) auto
   2.380 +        hence "(height (balL l n (delete x r)) = height (delete x r) + 2) \<or>
   2.381 +          height (balL l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
   2.382 +          using Node 2 by (intro height_balL) auto
   2.383          thus ?thesis 
   2.384          proof
   2.385            assume ?A
   2.386 -          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: node_bal_l_def)
   2.387 +          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
   2.388          next
   2.389            assume ?B
   2.390 -          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: node_bal_l_def)
   2.391 +          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
   2.392          qed
   2.393        qed
   2.394      qed
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Data_Structures/Cmp.thy	Thu Nov 05 08:27:14 2015 +0100
     3.3 @@ -0,0 +1,21 @@
     3.4 +(* Author: Tobias Nipkow *)
     3.5 +
     3.6 +section {* Three-Way Comparison *}
     3.7 +
     3.8 +theory Cmp
     3.9 +imports Main
    3.10 +begin
    3.11 +
    3.12 +datatype cmp = LT | EQ | GT
    3.13 +
    3.14 +class cmp = linorder +
    3.15 +fixes cmp :: "'a \<Rightarrow> 'a \<Rightarrow> cmp"
    3.16 +assumes LT[simp]: "cmp x y = LT \<longleftrightarrow> x < y"
    3.17 +assumes EQ[simp]: "cmp x y = EQ \<longleftrightarrow> x = y"
    3.18 +assumes GT[simp]: "cmp x y = GT \<longleftrightarrow> x > y"
    3.19 +
    3.20 +lemma case_cmp_if[simp]: "(case c of EQ \<Rightarrow> e | LT \<Rightarrow> l | GT \<Rightarrow> g) =
    3.21 +  (if c = LT then l else if c = GT then g else e)"
    3.22 +by(simp split: cmp.split)
    3.23 +
    3.24 +end
     4.1 --- a/src/HOL/Data_Structures/RBT_Map.thy	Wed Nov 04 15:07:23 2015 +0100
     4.2 +++ b/src/HOL/Data_Structures/RBT_Map.thy	Thu Nov 05 08:27:14 2015 +0100
     4.3 @@ -8,25 +8,26 @@
     4.4    Lookup2
     4.5  begin
     4.6  
     4.7 -fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
     4.8 +fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
     4.9  "update x y Leaf = R Leaf (x,y) Leaf" |
    4.10 -"update x y (B l (a,b) r) =
    4.11 -  (if x < a then bal (update x y l) (a,b) r else
    4.12 -   if x > a then bal l (a,b) (update x y r)
    4.13 -   else B l (x,y) r)" |
    4.14 -"update x y (R l (a,b) r) =
    4.15 -  (if x < a then R (update x y l) (a,b) r else
    4.16 -   if x > a then R l (a,b) (update x y r)
    4.17 -   else R l (x,y) r)"
    4.18 +"update x y (B l (a,b) r) = (case cmp x a of
    4.19 +  LT \<Rightarrow> bal (update x y l) (a,b) r |
    4.20 +  GT \<Rightarrow> bal l (a,b) (update x y r) |
    4.21 +  EQ \<Rightarrow> B l (x,y) r)" |
    4.22 +"update x y (R l (a,b) r) = (case cmp x a of
    4.23 +  LT \<Rightarrow> R (update x y l) (a,b) r |
    4.24 +  GT \<Rightarrow> R l (a,b) (update x y r) |
    4.25 +  EQ \<Rightarrow> R l (x,y) r)"
    4.26  
    4.27 -fun delete :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    4.28 -and deleteL :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    4.29 -and deleteR :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    4.30 +fun delete :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    4.31 +and deleteL :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    4.32 +and deleteR :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    4.33  where
    4.34  "delete x Leaf = Leaf" |
    4.35 -"delete x (Node c t1 (a,b) t2) = 
    4.36 -  (if x < a then deleteL x t1 (a,b) t2 else
    4.37 -   if x > a then deleteR x t1 (a,b) t2 else combine t1 t2)" |
    4.38 +"delete x (Node c t1 (a,b) t2) = (case cmp x a of
    4.39 +  LT \<Rightarrow> deleteL x t1 (a,b) t2 |
    4.40 +  GT \<Rightarrow> deleteR x t1 (a,b) t2 |
    4.41 +  EQ \<Rightarrow> combine t1 t2)" |
    4.42  "deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    4.43  "deleteL x t1 a t2 = R (delete x t1) a t2" |
    4.44  "deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    4.45 @@ -50,7 +51,6 @@
    4.46  by(induction x t1 and x t1 a t2 and x t1 a t2 rule: delete_deleteL_deleteR.induct)
    4.47    (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
    4.48  
    4.49 -
    4.50  interpretation Map_by_Ordered
    4.51  where empty = Leaf and lookup = lookup and update = update and delete = delete
    4.52  and inorder = inorder and wf = "\<lambda>_. True"
     5.1 --- a/src/HOL/Data_Structures/RBT_Set.thy	Wed Nov 04 15:07:23 2015 +0100
     5.2 +++ b/src/HOL/Data_Structures/RBT_Set.thy	Thu Nov 05 08:27:14 2015 +0100
     5.3 @@ -5,26 +5,30 @@
     5.4  theory RBT_Set
     5.5  imports
     5.6    RBT
     5.7 +  Cmp
     5.8    Isin2
     5.9  begin
    5.10  
    5.11 -fun insert :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    5.12 +fun insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    5.13  "insert x Leaf = R Leaf x Leaf" |
    5.14 -"insert x (B l a r) =
    5.15 -  (if x < a then bal (insert x l) a r else
    5.16 -   if x > a then bal l a (insert x r) else B l a r)" |
    5.17 -"insert x (R l a r) =
    5.18 -  (if x < a then R (insert x l) a r
    5.19 -   else if x > a then R l a (insert x r) else R l a r)"
    5.20 +"insert x (B l a r) = (case cmp x a of
    5.21 +  LT \<Rightarrow> bal (insert x l) a r |
    5.22 +  GT \<Rightarrow> bal l a (insert x r) |
    5.23 +  EQ \<Rightarrow> B l a r)" |
    5.24 +"insert x (R l a r) = (case cmp x a of
    5.25 +  LT \<Rightarrow> R (insert x l) a r |
    5.26 +  GT \<Rightarrow> R l a (insert x r) |
    5.27 +  EQ \<Rightarrow> R l a r)"
    5.28  
    5.29 -fun delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    5.30 -and deleteL :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    5.31 -and deleteR :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    5.32 +fun delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    5.33 +and deleteL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    5.34 +and deleteR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    5.35  where
    5.36  "delete x Leaf = Leaf" |
    5.37 -"delete x (Node _ l a r) = 
    5.38 -  (if x < a then deleteL x l a r 
    5.39 -   else if x > a then deleteR x l a r else combine l r)" |
    5.40 +"delete x (Node _ l a r) = (case cmp x a of
    5.41 +  LT \<Rightarrow> deleteL x l a r |
    5.42 +  GT \<Rightarrow> deleteR x l a r |
    5.43 +  EQ \<Rightarrow> combine l r)" |
    5.44  "deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    5.45  "deleteL x l a r = R (delete x l) a r" |
    5.46  "deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    5.47 @@ -66,6 +70,7 @@
    5.48  by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct)
    5.49    (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
    5.50  
    5.51 +
    5.52  interpretation Set_by_Ordered
    5.53  where empty = Leaf and isin = isin and insert = insert and delete = delete
    5.54  and inorder = inorder and wf = "\<lambda>_. True"
     6.1 --- a/src/HOL/Data_Structures/Splay_Map.thy	Wed Nov 04 15:07:23 2015 +0100
     6.2 +++ b/src/HOL/Data_Structures/Splay_Map.thy	Thu Nov 05 08:27:14 2015 +0100
     6.3 @@ -42,35 +42,30 @@
     6.4  termination splay
     6.5  by lexicographic_order
     6.6  
     6.7 -lemma splay_code: "splay x t = (case t of Leaf \<Rightarrow> Leaf |
     6.8 -  Node al a ar \<Rightarrow>
     6.9 -  (if x = fst a then t else
    6.10 -   if x < fst a then
    6.11 -     case al of
    6.12 -       Leaf \<Rightarrow> t |
    6.13 -       Node bl b br \<Rightarrow>
    6.14 -         (if x = fst b then Node bl b (Node br a ar) else
    6.15 -          if x < fst b then
    6.16 -            if bl = Leaf then Node bl b (Node br a ar)
    6.17 -            else case splay x bl of
    6.18 -                   Node bll y blr \<Rightarrow> Node bll y (Node blr b (Node br a ar))
    6.19 -          else
    6.20 -          if br = Leaf then Node bl b (Node br a ar)
    6.21 -          else case splay x br of
    6.22 -                 Node brl y brr \<Rightarrow> Node (Node bl b brl) y (Node brr a ar))
    6.23 -   else
    6.24 -   case ar of
    6.25 -     Leaf \<Rightarrow> t |
    6.26 -     Node bl b br \<Rightarrow>
    6.27 -       (if x = fst b then Node (Node al a bl) b br else
    6.28 -        if x < fst b then
    6.29 -          if bl = Leaf then Node (Node al a bl) b br
    6.30 -          else case splay x bl of
    6.31 -                 Node bll y blr \<Rightarrow> Node (Node al a bll) y (Node blr b br)
    6.32 -        else if br=Leaf then Node (Node al a bl) b br
    6.33 -             else case splay x br of
    6.34 -                    Node bll y blr \<Rightarrow> Node (Node (Node al a bl) b bll) y blr)))"
    6.35 -by(auto split: tree.split)
    6.36 +lemma splay_code: "splay (x::_::cmp) t = (case t of Leaf \<Rightarrow> Leaf |
    6.37 +  Node al a ar \<Rightarrow> (case cmp x (fst a) of
    6.38 +    EQ \<Rightarrow> t |
    6.39 +    LT \<Rightarrow> (case al of
    6.40 +      Leaf \<Rightarrow> t |
    6.41 +      Node bl b br \<Rightarrow> (case cmp x (fst b) of
    6.42 +        EQ \<Rightarrow> Node bl b (Node br a ar) |
    6.43 +        LT \<Rightarrow> if bl = Leaf then Node bl b (Node br a ar)
    6.44 +              else case splay x bl of
    6.45 +                Node bll y blr \<Rightarrow> Node bll y (Node blr b (Node br a ar)) |
    6.46 +        GT \<Rightarrow> if br = Leaf then Node bl b (Node br a ar)
    6.47 +              else case splay x br of
    6.48 +                Node brl y brr \<Rightarrow> Node (Node bl b brl) y (Node brr a ar))) |
    6.49 +    GT \<Rightarrow> (case ar of
    6.50 +      Leaf \<Rightarrow> t |
    6.51 +      Node bl b br \<Rightarrow> (case cmp x (fst b) of
    6.52 +        EQ \<Rightarrow> Node (Node al a bl) b br |
    6.53 +        LT \<Rightarrow> if bl = Leaf then Node (Node al a bl) b br
    6.54 +              else case splay x bl of
    6.55 +                Node bll y blr \<Rightarrow> Node (Node al a bll) y (Node blr b br) |
    6.56 +        GT \<Rightarrow> if br=Leaf then Node (Node al a bl) b br
    6.57 +              else case splay x br of
    6.58 +                Node bll y blr \<Rightarrow> Node (Node (Node al a bl) b bll) y blr))))"
    6.59 +by(auto cong: case_tree_cong split: tree.split)
    6.60  
    6.61  definition lookup :: "('a*'b)tree \<Rightarrow> 'a::linorder \<Rightarrow> 'b option" where "lookup t x =
    6.62    (case splay x t of Leaf \<Rightarrow> None | Node _ (a,b) _ \<Rightarrow> if x=a then Some b else None)"
     7.1 --- a/src/HOL/Data_Structures/Splay_Set.thy	Wed Nov 04 15:07:23 2015 +0100
     7.2 +++ b/src/HOL/Data_Structures/Splay_Set.thy	Thu Nov 05 08:27:14 2015 +0100
     7.3 @@ -1,6 +1,6 @@
     7.4  (*
     7.5  Author: Tobias Nipkow
     7.6 -Function defs follows AFP entry Splay_Tree, proofs are new.
     7.7 +Function defs follow AFP entry Splay_Tree, proofs are new.
     7.8  *)
     7.9  
    7.10  section "Splay Tree Implementation of Sets"
    7.11 @@ -9,6 +9,7 @@
    7.12  imports
    7.13    "~~/src/HOL/Library/Tree"
    7.14    Set_by_Ordered
    7.15 +  Cmp
    7.16  begin
    7.17  
    7.18  function splay :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
    7.19 @@ -45,35 +46,35 @@
    7.20  termination splay
    7.21  by lexicographic_order
    7.22  
    7.23 -lemma splay_code: "splay x t = (case t of Leaf \<Rightarrow> Leaf |
    7.24 -  Node al a ar \<Rightarrow>
    7.25 -  (if x=a then t else
    7.26 -   if x < a then
    7.27 -     case al of
    7.28 -       Leaf \<Rightarrow> t |
    7.29 -       Node bl b br \<Rightarrow>
    7.30 -         (if x=b then Node bl b (Node br a ar) else
    7.31 -          if x < b then
    7.32 -            if bl = Leaf then Node bl b (Node br a ar)
    7.33 -            else case splay x bl of
    7.34 -                   Node bll y blr \<Rightarrow> Node bll y (Node blr b (Node br a ar))
    7.35 -          else
    7.36 -          if br = Leaf then Node bl b (Node br a ar)
    7.37 -          else case splay x br of
    7.38 -                 Node brl y brr \<Rightarrow> Node (Node bl b brl) y (Node brr a ar))
    7.39 -   else
    7.40 -   case ar of
    7.41 -     Leaf \<Rightarrow> t |
    7.42 -     Node bl b br \<Rightarrow>
    7.43 -       (if x=b then Node (Node al a bl) b br else
    7.44 -        if x < b then
    7.45 -          if bl = Leaf then Node (Node al a bl) b br
    7.46 -          else case splay x bl of
    7.47 -                 Node bll y blr \<Rightarrow> Node (Node al a bll) y (Node blr b br)
    7.48 -        else if br=Leaf then Node (Node al a bl) b br
    7.49 -             else case splay x br of
    7.50 -                    Node bll y blr \<Rightarrow> Node (Node (Node al a bl) b bll) y blr)))"
    7.51 -by(auto split: tree.split)
    7.52 +(* no idea why this speeds things up below *)
    7.53 +lemma case_tree_cong:
    7.54 +  "\<lbrakk> x = x'; y = y'; z = z' \<rbrakk> \<Longrightarrow> case_tree x y z = case_tree x' y' z'"
    7.55 +by auto
    7.56 +
    7.57 +lemma splay_code: "splay (x::_::cmp) t = (case t of Leaf \<Rightarrow> Leaf |
    7.58 +  Node al a ar \<Rightarrow> (case cmp x a of
    7.59 +    EQ \<Rightarrow> t |
    7.60 +    LT \<Rightarrow> (case al of
    7.61 +      Leaf \<Rightarrow> t |
    7.62 +      Node bl b br \<Rightarrow> (case cmp x b of
    7.63 +        EQ \<Rightarrow> Node bl b (Node br a ar) |
    7.64 +        LT \<Rightarrow> if bl = Leaf then Node bl b (Node br a ar)
    7.65 +              else case splay x bl of
    7.66 +                Node bll y blr \<Rightarrow> Node bll y (Node blr b (Node br a ar)) |
    7.67 +        GT \<Rightarrow> if br = Leaf then Node bl b (Node br a ar)
    7.68 +              else case splay x br of
    7.69 +                Node brl y brr \<Rightarrow> Node (Node bl b brl) y (Node brr a ar))) |
    7.70 +    GT \<Rightarrow> (case ar of
    7.71 +      Leaf \<Rightarrow> t |
    7.72 +      Node bl b br \<Rightarrow> (case cmp x b of
    7.73 +        EQ \<Rightarrow> Node (Node al a bl) b br |
    7.74 +        LT \<Rightarrow> if bl = Leaf then Node (Node al a bl) b br
    7.75 +              else case splay x bl of
    7.76 +                Node bll y blr \<Rightarrow> Node (Node al a bll) y (Node blr b br) |
    7.77 +        GT \<Rightarrow> if br=Leaf then Node (Node al a bl) b br
    7.78 +              else case splay x br of
    7.79 +                Node bll y blr \<Rightarrow> Node (Node (Node al a bl) b bll) y blr))))"
    7.80 +by(auto cong: case_tree_cong split: tree.split)
    7.81  
    7.82  definition is_root :: "'a \<Rightarrow> 'a tree \<Rightarrow> bool" where
    7.83  "is_root a t = (case t of Leaf \<Rightarrow> False | Node _ x _ \<Rightarrow> x = a)"
     8.1 --- a/src/HOL/Data_Structures/Tree234_Map.thy	Wed Nov 04 15:07:23 2015 +0100
     8.2 +++ b/src/HOL/Data_Structures/Tree234_Map.thy	Thu Nov 05 08:27:14 2015 +0100
     8.3 @@ -10,118 +10,105 @@
     8.4  
     8.5  subsection \<open>Map operations on 2-3-4 trees\<close>
     8.6  
     8.7 -fun lookup :: "('a::linorder * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
     8.8 +fun lookup :: "('a::cmp * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
     8.9  "lookup Leaf x = None" |
    8.10 -"lookup (Node2 l (a,b) r) x =
    8.11 -  (if x < a then lookup l x else
    8.12 -  if a < x then lookup r x else Some b)" |
    8.13 -"lookup (Node3 l (a1,b1) m (a2,b2) r) x =
    8.14 -  (if x < a1 then lookup l x else
    8.15 -   if x = a1 then Some b1 else
    8.16 -   if x < a2 then lookup m x else
    8.17 -   if x = a2 then Some b2
    8.18 -   else lookup r x)" |
    8.19 -"lookup (Node4 l (a1,b1) m (a2,b2) n (a3,b3) r) x =
    8.20 -  (if x < a2 then
    8.21 -     if x = a1 then Some b1 else
    8.22 -     if x < a1 then lookup l x else lookup m x
    8.23 -   else
    8.24 -     if x = a2 then Some b2 else
    8.25 -     if x = a3 then Some b3 else
    8.26 -     if x < a3 then lookup n x
    8.27 -     else lookup r x)"
    8.28 +"lookup (Node2 l (a,b) r) x = (case cmp x a of
    8.29 +  LT \<Rightarrow> lookup l x |
    8.30 +  GT \<Rightarrow> lookup r x |
    8.31 +  EQ \<Rightarrow> Some b)" |
    8.32 +"lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of
    8.33 +  LT \<Rightarrow> lookup l x |
    8.34 +  EQ \<Rightarrow> Some b1 |
    8.35 +  GT \<Rightarrow> (case cmp x a2 of
    8.36 +          LT \<Rightarrow> lookup m x |
    8.37 +          EQ \<Rightarrow> Some b2 |
    8.38 +          GT \<Rightarrow> lookup r x))" |
    8.39 +"lookup (Node4 t1 (a1,b1) t2 (a2,b2) t3 (a3,b3) t4) x = (case cmp x a2 of
    8.40 +  LT \<Rightarrow> (case cmp x a1 of
    8.41 +           LT \<Rightarrow> lookup t1 x | EQ \<Rightarrow> Some b1 | GT \<Rightarrow> lookup t2 x) |
    8.42 +  EQ \<Rightarrow> Some b2 |
    8.43 +  GT \<Rightarrow> (case cmp x a3 of
    8.44 +           LT \<Rightarrow> lookup t3 x | EQ \<Rightarrow> Some b3 | GT \<Rightarrow> lookup t4 x))"
    8.45  
    8.46 -fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
    8.47 +fun upd :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
    8.48  "upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
    8.49 -"upd x y (Node2 l ab r) =
    8.50 -   (if x < fst ab then
    8.51 -        (case upd x y l of
    8.52 +"upd x y (Node2 l ab r) = (case cmp x (fst ab) of
    8.53 +   LT \<Rightarrow> (case upd x y l of
    8.54             T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
    8.55 -         | Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 ab r))
    8.56 -    else if x = fst ab then T\<^sub>i (Node2 l (x,y) r)
    8.57 -    else
    8.58 -        (case upd x y r of
    8.59 +         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r)) |
    8.60 +   EQ \<Rightarrow> T\<^sub>i (Node2 l (x,y) r) |
    8.61 +   GT \<Rightarrow> (case upd x y r of
    8.62             T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
    8.63 -         | Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l ab r1 q r2)))" |
    8.64 -"upd x y (Node3 l ab1 m ab2 r) =
    8.65 -   (if x < fst ab1 then
    8.66 -        (case upd x y l of
    8.67 +         | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" |
    8.68 +"upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
    8.69 +   LT \<Rightarrow> (case upd x y l of
    8.70             T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
    8.71 -         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node2 m ab2 r))
    8.72 -    else if x = fst ab1 then T\<^sub>i (Node3 l (x,y) m ab2 r)
    8.73 -    else if x < fst ab2 then
    8.74 -             (case upd x y m of
    8.75 -                T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
    8.76 -              | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node2 m2 ab2 r))
    8.77 -         else if x = fst ab2 then T\<^sub>i (Node3 l ab1 m (x,y) r)
    8.78 -         else
    8.79 -             (case upd x y r of
    8.80 -                T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
    8.81 -              | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 q r2)))" |
    8.82 -"upd x y (Node4 l ab1 m ab2 n ab3 r) =
    8.83 -   (if x < fst ab2 then
    8.84 -      if x < fst ab1 then
    8.85 -        (case upd x y l of
    8.86 -           T\<^sub>i l' => T\<^sub>i (Node4 l' ab1 m ab2 n ab3 r)
    8.87 -         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node3 m ab2 n ab3 r))
    8.88 -      else
    8.89 -      if x = fst ab1 then T\<^sub>i (Node4 l (x,y) m ab2 n ab3 r)
    8.90 -      else
    8.91 -        (case upd x y m of
    8.92 -           T\<^sub>i m' => T\<^sub>i (Node4 l ab1 m' ab2 n ab3 r)
    8.93 -         | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node3 m2 ab2 n ab3 r))
    8.94 -    else
    8.95 -    if x = fst ab2 then T\<^sub>i (Node4 l ab1 m (x,y) n ab3 r) else
    8.96 -    if x < fst ab3 then
    8.97 -      (case upd x y n of
    8.98 -         T\<^sub>i n' => T\<^sub>i (Node4 l ab1 m ab2 n' ab3 r)
    8.99 -       | Up\<^sub>i n1 q n2 => Up\<^sub>i (Node2 l ab1 m) ab2(*q*) (Node3 n1 q n2 ab3 r))
   8.100 -    else
   8.101 -    if x = fst ab3 then T\<^sub>i (Node4 l ab1 m ab2 n (x,y) r)
   8.102 -    else
   8.103 -      (case upd x y r of
   8.104 -         T\<^sub>i r' => T\<^sub>i (Node4 l ab1 m ab2 n ab3 r')
   8.105 -       | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node3 n ab3 r1 q r2)))"
   8.106 +         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) |
   8.107 +   EQ \<Rightarrow> T\<^sub>i (Node3 l (x,y) m ab2 r) |
   8.108 +   GT \<Rightarrow> (case cmp x (fst ab2) of
   8.109 +           LT \<Rightarrow> (case upd x y m of
   8.110 +                   T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
   8.111 +                 | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) |
   8.112 +           EQ \<Rightarrow> T\<^sub>i (Node3 l ab1 m (x,y) r) |
   8.113 +           GT \<Rightarrow> (case upd x y r of
   8.114 +                   T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
   8.115 +                 | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))" |
   8.116 +"upd x y (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
   8.117 +   LT \<Rightarrow> (case cmp x (fst ab1) of
   8.118 +            LT \<Rightarrow> (case upd x y t1 of
   8.119 +                     T\<^sub>i t1' => T\<^sub>i (Node4 t1' ab1 t2 ab2 t3 ab3 t4)
   8.120 +                  | Up\<^sub>i t11 q t12 => Up\<^sub>i (Node2 t11 q t12) ab1 (Node3 t2 ab2 t3 ab3 t4)) |
   8.121 +            EQ \<Rightarrow> T\<^sub>i (Node4 t1 (x,y) t2 ab2 t3 ab3 t4) |
   8.122 +            GT \<Rightarrow> (case upd x y t2 of
   8.123 +                    T\<^sub>i t2' => T\<^sub>i (Node4 t1 ab1 t2' ab2 t3 ab3 t4)
   8.124 +                  | Up\<^sub>i t21 q t22 => Up\<^sub>i (Node2 t1 ab1 t21) q (Node3 t22 ab2 t3 ab3 t4))) |
   8.125 +   EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 (x,y) t3 ab3 t4) |
   8.126 +   GT \<Rightarrow> (case cmp x (fst ab3) of
   8.127 +            LT \<Rightarrow> (case upd x y t3 of
   8.128 +                    T\<^sub>i t3' \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3' ab3 t4)
   8.129 +                  | Up\<^sub>i t31 q t32 => Up\<^sub>i (Node2 t1 ab1 t2) ab2(*q*) (Node3 t31 q t32 ab3 t4)) |
   8.130 +            EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 (x,y) t4) |
   8.131 +            GT \<Rightarrow> (case upd x y t4 of
   8.132 +                    T\<^sub>i t4' => T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 ab3 t4')
   8.133 +                  | Up\<^sub>i t41 q t42 => Up\<^sub>i (Node2 t1 ab1 t2) ab2 (Node3 t3 ab3 t41 q t42))))"
   8.134 +
   8.135 +definition update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   8.136 +"update x y t = tree\<^sub>i(upd x y t)"
   8.137  
   8.138 -definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   8.139 -"update a b t = tree\<^sub>i(upd a b t)"
   8.140 -
   8.141 -fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d"
   8.142 -where
   8.143 -"del k Leaf = T\<^sub>d Leaf" |
   8.144 -"del k (Node2 Leaf p Leaf) = (if k=fst p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" |
   8.145 -"del k (Node3 Leaf p Leaf q Leaf) =
   8.146 -  T\<^sub>d(if k=fst p then Node2 Leaf q Leaf else
   8.147 -     if k=fst q then Node2 Leaf p Leaf
   8.148 -     else Node3 Leaf p Leaf q Leaf)" |
   8.149 -"del k (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
   8.150 -  T\<^sub>d(if k=fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
   8.151 -     if k=fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
   8.152 -     if k=fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
   8.153 +fun del :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d" where
   8.154 +"del x Leaf = T\<^sub>d Leaf" |
   8.155 +"del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" |
   8.156 +"del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf
   8.157 +  else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
   8.158 +"del x (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
   8.159 +  T\<^sub>d(if x = fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
   8.160 +     if x = fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
   8.161 +     if x = fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
   8.162       else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" |
   8.163 -"del k (Node2 l a r) =
   8.164 -  (if k<fst a then node21 (del k l) a r else
   8.165 -   if k > fst a then node22 l a (del k r)
   8.166 -   else let (a',t) = del_min r in node22 l a' t)" |
   8.167 -"del k (Node3 l a m b r) =
   8.168 -  (if k<fst a then node31 (del k l) a m b r else
   8.169 -   if k = fst a then let (a',m') = del_min m in node32 l a' m' b r else
   8.170 -   if k < fst b then node32 l a (del k m) b r else
   8.171 -   if k = fst b then let (b',r') = del_min r in node33 l a m b' r'
   8.172 -   else node33 l a m b (del k r))" |
   8.173 -"del x (Node4 l ab1 m ab2 n ab3 r) =
   8.174 -  (if x < fst ab2 then
   8.175 -     if x < fst ab1 then node41 (del x l) ab1 m ab2 n ab3 r else
   8.176 -     if x = fst ab1 then let (ab',m') = del_min m in node42 l ab' m' ab2 n ab3 r
   8.177 -     else node42 l ab1 (del x m) ab2 n ab3 r
   8.178 -   else
   8.179 -     if x = fst ab2 then let (ab',n') = del_min n in node43 l ab1 m ab' n' ab3 r else
   8.180 -     if x < fst ab3 then node43 l ab1 m ab2 (del x n) ab3 r else
   8.181 -     if x = fst ab3 then let (ab',r') = del_min r in node44 l ab1 m ab2 n ab' r'
   8.182 -     else node44 l ab1 m ab2 n ab3 (del x r))"
   8.183 +"del x (Node2 l ab1 r) = (case cmp x (fst ab1) of
   8.184 +  LT \<Rightarrow> node21 (del x l) ab1 r |
   8.185 +  GT \<Rightarrow> node22 l ab1 (del x r) |
   8.186 +  EQ \<Rightarrow> let (ab1',t) = del_min r in node22 l ab1' t)" |
   8.187 +"del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
   8.188 +  LT \<Rightarrow> node31 (del x l) ab1 m ab2 r |
   8.189 +  EQ \<Rightarrow> let (ab1',m') = del_min m in node32 l ab1' m' ab2 r |
   8.190 +  GT \<Rightarrow> (case cmp x (fst ab2) of
   8.191 +           LT \<Rightarrow> node32 l ab1 (del x m) ab2 r |
   8.192 +           EQ \<Rightarrow> let (ab2',r') = del_min r in node33 l ab1 m ab2' r' |
   8.193 +           GT \<Rightarrow> node33 l ab1 m ab2 (del x r)))" |
   8.194 +"del x (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
   8.195 +  LT \<Rightarrow> (case cmp x (fst ab1) of
   8.196 +           LT \<Rightarrow> node41 (del x t1) ab1 t2 ab2 t3 ab3 t4 |
   8.197 +           EQ \<Rightarrow> let (ab',t2') = del_min t2 in node42 t1 ab' t2' ab2 t3 ab3 t4 |
   8.198 +           GT \<Rightarrow> node42 t1 ab1 (del x t2) ab2 t3 ab3 t4) |
   8.199 +  EQ \<Rightarrow> let (ab',t3') = del_min t3 in node43 t1 ab1 t2 ab' t3' ab3 t4 |
   8.200 +  GT \<Rightarrow> (case cmp x (fst ab3) of
   8.201 +          LT \<Rightarrow> node43 t1 ab1 t2 ab2 (del x t3) ab3 t4 |
   8.202 +          EQ \<Rightarrow> let (ab',t4') = del_min t4 in node44 t1 ab1 t2 ab2 t3 ab' t4' |
   8.203 +          GT \<Rightarrow> node44 t1 ab1 t2 ab2 t3 ab3 (del x t4)))"
   8.204  
   8.205 -definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   8.206 -"delete k t = tree\<^sub>d(del k t)"
   8.207 +definition delete :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   8.208 +"delete x t = tree\<^sub>d(del x t)"
   8.209  
   8.210  
   8.211  subsection "Functional correctness"
   8.212 @@ -144,7 +131,7 @@
   8.213    inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
   8.214  by(induction t rule: del.induct)
   8.215    ((auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)[1])+
   8.216 -(* 290 secs (2015) *)
   8.217 +(* 200 secs (2015) *)
   8.218  
   8.219  lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
   8.220    inorder(delete x t) = del_list x (inorder t)"
   8.221 @@ -154,7 +141,7 @@
   8.222  subsection \<open>Balancedness\<close>
   8.223  
   8.224  lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t"
   8.225 -by (induct t) (auto, auto split: up\<^sub>i.split) (* 33 secs (2015) *)
   8.226 +by (induct t) (auto, auto split: up\<^sub>i.split) (* 20 secs (2015) *)
   8.227  
   8.228  lemma bal_update: "bal t \<Longrightarrow> bal (update x y t)"
   8.229  by (simp add: update_def bal_upd)
   8.230 @@ -163,11 +150,12 @@
   8.231  lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
   8.232  by(induction x t rule: del.induct)
   8.233    (auto simp add: heights height_del_min split: prod.split)
   8.234 +(* 20 secs (2015) *)
   8.235  
   8.236  lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
   8.237  by(induction x t rule: del.induct)
   8.238    (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
   8.239 -(* 110 secs (2015) *)
   8.240 +(* 100 secs (2015) *)
   8.241  
   8.242  corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
   8.243  by(simp add: delete_def bal_tree\<^sub>d_del)
     9.1 --- a/src/HOL/Data_Structures/Tree234_Set.thy	Wed Nov 04 15:07:23 2015 +0100
     9.2 +++ b/src/HOL/Data_Structures/Tree234_Set.thy	Thu Nov 05 08:27:14 2015 +0100
     9.3 @@ -5,19 +5,29 @@
     9.4  theory Tree234_Set
     9.5  imports
     9.6    Tree234
     9.7 +  Cmp
     9.8    "../Data_Structures/Set_by_Ordered"
     9.9  begin
    9.10  
    9.11  subsection \<open>Set operations on 2-3-4 trees\<close>
    9.12  
    9.13 -fun isin :: "'a::linorder tree234 \<Rightarrow> 'a \<Rightarrow> bool" where
    9.14 +fun isin :: "'a::cmp tree234 \<Rightarrow> 'a \<Rightarrow> bool" where
    9.15  "isin Leaf x = False" |
    9.16 -"isin (Node2 l a r) x = (x < a \<and> isin l x \<or> x=a \<or> isin r x)" |
    9.17 +"isin (Node2 l a r) x =
    9.18 +  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)" |
    9.19  "isin (Node3 l a m b r) x =
    9.20 -  (x < a \<and> isin l x \<or> x = a \<or> x < b \<and> isin m x \<or> x = b \<or> isin r x)" |
    9.21 -"isin (Node4 l a m b n c r) x =
    9.22 -  (x < b \<and> (x < a \<and> isin l x \<or> x = a \<or> isin m x) \<or> x = b \<or>
    9.23 -   x > b \<and> (x < c \<and> isin n x \<or> x=c \<or> isin r x))"
    9.24 +  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> (case cmp x b of
    9.25 +   LT \<Rightarrow> isin m x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x))" |
    9.26 +"isin (Node4 t1 a t2 b t3 c t4) x = (case cmp x b of
    9.27 +  LT \<Rightarrow> (case cmp x a of
    9.28 +          LT \<Rightarrow> isin t1 x |
    9.29 +          EQ \<Rightarrow> True |
    9.30 +          GT \<Rightarrow> isin t2 x) |
    9.31 +  EQ \<Rightarrow> True |
    9.32 +  GT \<Rightarrow> (case cmp x c of
    9.33 +          LT \<Rightarrow> isin t3 x |
    9.34 +          EQ \<Rightarrow> True |
    9.35 +          GT \<Rightarrow> isin t4 x))"
    9.36  
    9.37  datatype 'a up\<^sub>i = T\<^sub>i "'a tree234" | Up\<^sub>i "'a tree234" 'a "'a tree234"
    9.38  
    9.39 @@ -25,33 +35,31 @@
    9.40  "tree\<^sub>i (T\<^sub>i t) = t" |
    9.41  "tree\<^sub>i (Up\<^sub>i l p r) = Node2 l p r"
    9.42  
    9.43 -fun ins :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>i" where
    9.44 -"ins a Leaf = Up\<^sub>i Leaf a Leaf" |
    9.45 -"ins a (Node2 l x r) =
    9.46 -   (if a < x then
    9.47 -        (case ins a l of
    9.48 -           T\<^sub>i l' => T\<^sub>i (Node2 l' x r)
    9.49 -         | Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 x r))
    9.50 -    else if a=x then T\<^sub>i (Node2 l x r)
    9.51 -    else
    9.52 -        (case ins a r of
    9.53 -           T\<^sub>i r' => T\<^sub>i (Node2 l x r')
    9.54 -         | Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l x r1 q r2)))" |
    9.55 -"ins a (Node3 l x1 m x2 r) =
    9.56 -   (if a < x1 then
    9.57 -        (case ins a l of
    9.58 -           T\<^sub>i l' => T\<^sub>i (Node3 l' x1 m x2 r)
    9.59 -         | Up\<^sub>i l1 q l2 => T\<^sub>i (Node4 l1 q l2 x1 m x2 r))
    9.60 -    else if a=x1 then T\<^sub>i (Node3 l x1 m x2 r)
    9.61 -    else if a < x2 then
    9.62 -             (case ins a m of
    9.63 -                T\<^sub>i m' => T\<^sub>i (Node3 l x1 m' x2 r)
    9.64 -              | Up\<^sub>i m1 q m2 => T\<^sub>i (Node4 l x1 m1 q m2 x2 r))
    9.65 -         else if a=x2 then T\<^sub>i (Node3 l x1 m x2 r)
    9.66 -         else
    9.67 -             (case ins a r of
    9.68 -                T\<^sub>i r' => T\<^sub>i (Node3 l x1 m x2 r')
    9.69 -              | Up\<^sub>i r1 q r2 => T\<^sub>i (Node4 l x1 m x2 r1 q r2)))" |
    9.70 +fun ins :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>i" where
    9.71 +"ins x Leaf = Up\<^sub>i Leaf x Leaf" |
    9.72 +"ins x (Node2 l a r) =
    9.73 +   (case cmp x a of
    9.74 +      LT \<Rightarrow> (case ins x l of
    9.75 +              T\<^sub>i l' => T\<^sub>i (Node2 l' a r)
    9.76 +            | Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)) |
    9.77 +      EQ \<Rightarrow> T\<^sub>i (Node2 l x r) |
    9.78 +      GT \<Rightarrow> (case ins x r of
    9.79 +              T\<^sub>i r' => T\<^sub>i (Node2 l a r')
    9.80 +            | Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2)))" |
    9.81 +"ins x (Node3 l a m b r) =
    9.82 +   (case cmp x a of
    9.83 +      LT \<Rightarrow> (case ins x l of
    9.84 +              T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r)
    9.85 +            | Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)) |
    9.86 +      EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
    9.87 +      GT \<Rightarrow> (case cmp x b of
    9.88 +               GT \<Rightarrow> (case ins x r of
    9.89 +                       T\<^sub>i r' => T\<^sub>i (Node3 l a m b r')
    9.90 +                     | Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)) |
    9.91 +               EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
    9.92 +               LT \<Rightarrow> (case ins x m of
    9.93 +                       T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r)
    9.94 +                     | Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))))" |
    9.95  "ins a (Node4 l x1 m x2 n x3 r) =
    9.96     (if a < x2 then
    9.97        if a < x1 then
    9.98 @@ -75,8 +83,8 @@
    9.99  
   9.100  hide_const insert
   9.101  
   9.102 -definition insert :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
   9.103 -"insert a t = tree\<^sub>i(ins a t)"
   9.104 +definition insert :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
   9.105 +"insert x t = tree\<^sub>i(ins x t)"
   9.106  
   9.107  datatype 'a up\<^sub>d = T\<^sub>d "'a tree234" | Up\<^sub>d "'a tree234"
   9.108  
   9.109 @@ -146,7 +154,7 @@
   9.110  "del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))" |
   9.111  "del_min (Node4 l a m b n c r) = (let (x,l') = del_min l in (x, node41 l' a m b n c r))"
   9.112  
   9.113 -fun del :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
   9.114 +fun del :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
   9.115  "del k Leaf = T\<^sub>d Leaf" |
   9.116  "del k (Node2 Leaf p Leaf) = (if k=p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" |
   9.117  "del k (Node3 Leaf p Leaf q Leaf) = T\<^sub>d(if k=p then Node2 Leaf q Leaf
   9.118 @@ -156,36 +164,38 @@
   9.119       if k=b then Node3 Leaf a Leaf c Leaf else
   9.120       if k=c then Node3 Leaf a Leaf b Leaf
   9.121       else Node4 Leaf a Leaf b Leaf c Leaf)" |
   9.122 -"del k (Node2 l a r) = (if k<a then node21 (del k l) a r else
   9.123 -  if k > a then node22 l a (del k r) else
   9.124 -  let (a',t) = del_min r in node22 l a' t)" |
   9.125 -"del k (Node3 l a m b r) = (if k<a then node31 (del k l) a m b r else
   9.126 -  if k = a then let (a',m') = del_min m in node32 l a' m' b r else
   9.127 -  if k < b then node32 l a (del k m) b r else
   9.128 -  if k = b then let (b',r') = del_min r in node33 l a m b' r'
   9.129 -  else node33 l a m b (del k r))" |
   9.130 -"del k (Node4 l a m b n c r) =
   9.131 -  (if k < b then
   9.132 -     if k < a then node41 (del k l) a m b n c r else
   9.133 -     if k = a then let (a',m') = del_min m in node42 l a' m' b n c r
   9.134 -     else node42 l a (del k m) b n c r
   9.135 -   else
   9.136 -     if k = b then let (b',n') = del_min n in node43 l a m b' n' c r else
   9.137 -     if k < c then node43 l a m b (del k n) c r else
   9.138 -     if k = c then let (c',r') = del_min r in node44 l a m b n c' r'
   9.139 -     else node44 l a m b n c (del k r))"
   9.140 +"del k (Node2 l a r) = (case cmp k a of
   9.141 +  LT \<Rightarrow> node21 (del k l) a r |
   9.142 +  GT \<Rightarrow> node22 l a (del k r) |
   9.143 +  EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" |
   9.144 +"del k (Node3 l a m b r) = (case cmp k a of
   9.145 +  LT \<Rightarrow> node31 (del k l) a m b r |
   9.146 +  EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r |
   9.147 +  GT \<Rightarrow> (case cmp k b of
   9.148 +           LT \<Rightarrow> node32 l a (del k m) b r |
   9.149 +           EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' |
   9.150 +           GT \<Rightarrow> node33 l a m b (del k r)))" |
   9.151 +"del k (Node4 l a m b n c r) = (case cmp k b of
   9.152 +  LT \<Rightarrow> (case cmp k a of
   9.153 +          LT \<Rightarrow> node41 (del k l) a m b n c r |
   9.154 +          EQ \<Rightarrow> let (a',m') = del_min m in node42 l a' m' b n c r |
   9.155 +          GT \<Rightarrow> node42 l a (del k m) b n c r) |
   9.156 +  EQ \<Rightarrow> let (b',n') = del_min n in node43 l a m b' n' c r |
   9.157 +  GT \<Rightarrow> (case cmp k c of
   9.158 +           LT \<Rightarrow> node43 l a m b (del k n) c r |
   9.159 +           EQ \<Rightarrow> let (c',r') = del_min r in node44 l a m b n c' r' |
   9.160 +           GT \<Rightarrow> node44 l a m b n c (del k r)))"
   9.161  
   9.162 -definition delete :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
   9.163 -"delete k t = tree\<^sub>d(del k t)"
   9.164 +definition delete :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
   9.165 +"delete x t = tree\<^sub>d(del x t)"
   9.166  
   9.167  
   9.168  subsection "Functional correctness"
   9.169  
   9.170 -
   9.171  subsubsection \<open>Functional correctness of isin:\<close>
   9.172  
   9.173  lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
   9.174 -by (induction t) (auto simp: elems_simps1)
   9.175 +by (induction t) (auto simp: elems_simps1 ball_Un)
   9.176  
   9.177  lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
   9.178  by (induction t) (auto simp: elems_simps2)
   9.179 @@ -252,12 +262,9 @@
   9.180  
   9.181  lemma inorder_del: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
   9.182    inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
   9.183 -apply(induction t rule: del.induct)
   9.184 -apply(simp_all add: del_list_simps inorder_nodes)
   9.185 -apply(auto simp: del_list_simps;
   9.186 -      auto simp: inorder_nodes del_list_simps del_minD split: prod.splits)+
   9.187 -(* takes 285 s (2015); the last line alone would do it but takes hours *)
   9.188 -done
   9.189 +by(induction t rule: del.induct)
   9.190 +  (auto simp: inorder_nodes del_list_simps del_minD split: prod.splits)
   9.191 +  (* 150 secs (2015) *)
   9.192  
   9.193  lemma inorder_delete: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
   9.194    inorder(delete x t) = del_list x (inorder t)"
   9.195 @@ -282,7 +289,7 @@
   9.196  end
   9.197  
   9.198  lemma bal_ins: "bal t \<Longrightarrow> bal (tree\<^sub>i(ins a t)) \<and> height(ins a t) = height t"
   9.199 -by (induct t) (auto, auto split: up\<^sub>i.split) (* 29 secs (2015) *)
   9.200 +by (induct t) (auto, auto split: up\<^sub>i.split) (* 20 secs (2015) *)
   9.201  
   9.202  
   9.203  text{* Now an alternative proof (by Brian Huffman) that runs faster because
   9.204 @@ -344,9 +351,7 @@
   9.205  "full\<^sub>i n (Up\<^sub>i l p r) \<longleftrightarrow> full n l \<and> full n r"
   9.206  
   9.207  lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (ins a t)"
   9.208 -apply (induct rule: full.induct)
   9.209 -apply (auto, auto split: up\<^sub>i.split)
   9.210 -done
   9.211 +by (induct rule: full.induct) (auto, auto split: up\<^sub>i.split)
   9.212  
   9.213  text {* The @{const insert} operation preserves balance. *}
   9.214  
   9.215 @@ -482,13 +487,12 @@
   9.216  
   9.217  lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
   9.218  by(induction x t rule: del.induct)
   9.219 -  ((auto simp: bals bal_del_min height_del height_del_min split: prod.split)[1])+
   9.220 -(* 64 secs (2015) *)
   9.221 +  (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
   9.222 +(* 60 secs (2015) *)
   9.223  
   9.224  corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
   9.225  by(simp add: delete_def bal_tree\<^sub>d_del)
   9.226  
   9.227 -
   9.228  subsection \<open>Overall Correctness\<close>
   9.229  
   9.230  interpretation Set_by_Ordered
    10.1 --- a/src/HOL/Data_Structures/Tree23_Map.thy	Wed Nov 04 15:07:23 2015 +0100
    10.2 +++ b/src/HOL/Data_Structures/Tree23_Map.thy	Thu Nov 05 08:27:14 2015 +0100
    10.3 @@ -8,65 +8,65 @@
    10.4    Map_by_Ordered
    10.5  begin
    10.6  
    10.7 -fun lookup :: "('a::linorder * 'b) tree23 \<Rightarrow> 'a \<Rightarrow> 'b option" where
    10.8 +fun lookup :: "('a::cmp * 'b) tree23 \<Rightarrow> 'a \<Rightarrow> 'b option" where
    10.9  "lookup Leaf x = None" |
   10.10 -"lookup (Node2 l (a,b) r) x =
   10.11 -  (if x < a then lookup l x else
   10.12 -  if a < x then lookup r x else Some b)" |
   10.13 -"lookup (Node3 l (a1,b1) m (a2,b2) r) x =
   10.14 -  (if x < a1 then lookup l x else
   10.15 -   if x = a1 then Some b1 else
   10.16 -   if x < a2 then lookup m x else
   10.17 -   if x = a2 then Some b2
   10.18 -   else lookup r x)"
   10.19 +"lookup (Node2 l (a,b) r) x = (case cmp x a of
   10.20 +  LT \<Rightarrow> lookup l x |
   10.21 +  GT \<Rightarrow> lookup r x |
   10.22 +  EQ \<Rightarrow> Some b)" |
   10.23 +"lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of
   10.24 +  LT \<Rightarrow> lookup l x |
   10.25 +  EQ \<Rightarrow> Some b1 |
   10.26 +  GT \<Rightarrow> (case cmp x a2 of
   10.27 +          LT \<Rightarrow> lookup m x |
   10.28 +          EQ \<Rightarrow> Some b2 |
   10.29 +          GT \<Rightarrow> lookup r x))"
   10.30  
   10.31 -fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>i" where
   10.32 +fun upd :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>i" where
   10.33  "upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
   10.34 -"upd x y (Node2 l ab r) =
   10.35 -   (if x < fst ab then
   10.36 -        (case upd x y l of
   10.37 +"upd x y (Node2 l ab r) = (case cmp x (fst ab) of
   10.38 +   LT \<Rightarrow> (case upd x y l of
   10.39             T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
   10.40 -         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r))
   10.41 -    else if x = fst ab then T\<^sub>i (Node2 l (x,y) r)
   10.42 -    else
   10.43 -        (case upd x y r of
   10.44 +         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r)) |
   10.45 +   EQ \<Rightarrow> T\<^sub>i (Node2 l (x,y) r) |
   10.46 +   GT \<Rightarrow> (case upd x y r of
   10.47             T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
   10.48           | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" |
   10.49 -"upd x y (Node3 l ab1 m ab2 r) =
   10.50 -   (if x < fst ab1 then
   10.51 -        (case upd x y l of
   10.52 +"upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
   10.53 +   LT \<Rightarrow> (case upd x y l of
   10.54             T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
   10.55 -         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r))
   10.56 -    else if x = fst ab1 then T\<^sub>i (Node3 l (x,y) m ab2 r)
   10.57 -    else if x < fst ab2 then
   10.58 -             (case upd x y m of
   10.59 -                T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
   10.60 -              | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r))
   10.61 -         else if x = fst ab2 then T\<^sub>i (Node3 l ab1 m (x,y) r)
   10.62 -         else
   10.63 -             (case upd x y r of
   10.64 -                T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
   10.65 -              | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2)))"
   10.66 +         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) |
   10.67 +   EQ \<Rightarrow> T\<^sub>i (Node3 l (x,y) m ab2 r) |
   10.68 +   GT \<Rightarrow> (case cmp x (fst ab2) of
   10.69 +           LT \<Rightarrow> (case upd x y m of
   10.70 +                   T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
   10.71 +                 | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) |
   10.72 +           EQ \<Rightarrow> T\<^sub>i (Node3 l ab1 m (x,y) r) |
   10.73 +           GT \<Rightarrow> (case upd x y r of
   10.74 +                   T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
   10.75 +                 | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))"
   10.76  
   10.77 -definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
   10.78 +definition update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
   10.79  "update a b t = tree\<^sub>i(upd a b t)"
   10.80  
   10.81 -fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>d"
   10.82 -where
   10.83 +fun del :: "'a::cmp \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>d" where
   10.84  "del x Leaf = T\<^sub>d Leaf" |
   10.85  "del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" |
   10.86  "del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf
   10.87    else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
   10.88 -"del x (Node2 l ab1 r) = (if x<fst ab1 then node21 (del x l) ab1 r else
   10.89 -  if x > fst ab1 then node22 l ab1 (del x r) else
   10.90 -  let (ab1',t) = del_min r in node22 l ab1' t)" |
   10.91 -"del x (Node3 l ab1 m ab2 r) = (if x<fst ab1 then node31 (del x l) ab1 m ab2 r else
   10.92 -  if x = fst ab1 then let (ab1',m') = del_min m in node32 l ab1' m' ab2 r else
   10.93 -  if x < fst ab2 then node32 l ab1 (del x m) ab2 r else
   10.94 -  if x = fst ab2 then let (ab2',r') = del_min r in node33 l ab1 m ab2' r'
   10.95 -  else node33 l ab1 m ab2 (del x r))"
   10.96 +"del x (Node2 l ab1 r) = (case cmp x (fst ab1) of
   10.97 +  LT \<Rightarrow> node21 (del x l) ab1 r |
   10.98 +  GT \<Rightarrow> node22 l ab1 (del x r) |
   10.99 +  EQ \<Rightarrow> let (ab1',t) = del_min r in node22 l ab1' t)" |
  10.100 +"del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
  10.101 +  LT \<Rightarrow> node31 (del x l) ab1 m ab2 r |
  10.102 +  EQ \<Rightarrow> let (ab1',m') = del_min m in node32 l ab1' m' ab2 r |
  10.103 +  GT \<Rightarrow> (case cmp x (fst ab2) of
  10.104 +           LT \<Rightarrow> node32 l ab1 (del x m) ab2 r |
  10.105 +           EQ \<Rightarrow> let (ab2',r') = del_min r in node33 l ab1 m ab2' r' |
  10.106 +           GT \<Rightarrow> node33 l ab1 m ab2 (del x r)))"
  10.107  
  10.108 -definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
  10.109 +definition delete :: "'a::cmp \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
  10.110  "delete x t = tree\<^sub>d(del x t)"
  10.111  
  10.112  
  10.113 @@ -98,7 +98,7 @@
  10.114  subsection \<open>Balancedness\<close>
  10.115  
  10.116  lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd a b t)) \<and> height(upd a b t) = height t"
  10.117 -by (induct t) (auto split: up\<^sub>i.split)(* 30 secs in 2015 *)
  10.118 +by (induct t) (auto split: up\<^sub>i.split)(* 16 secs in 2015 *)
  10.119  
  10.120  corollary bal_update: "bal t \<Longrightarrow> bal (update a b t)"
  10.121  by (simp add: update_def bal_upd)
    11.1 --- a/src/HOL/Data_Structures/Tree23_Set.thy	Wed Nov 04 15:07:23 2015 +0100
    11.2 +++ b/src/HOL/Data_Structures/Tree23_Set.thy	Thu Nov 05 08:27:14 2015 +0100
    11.3 @@ -5,14 +5,17 @@
    11.4  theory Tree23_Set
    11.5  imports
    11.6    Tree23
    11.7 +  Cmp
    11.8    Set_by_Ordered
    11.9  begin
   11.10  
   11.11 -fun isin :: "'a::linorder tree23 \<Rightarrow> 'a \<Rightarrow> bool" where
   11.12 +fun isin :: "'a::cmp tree23 \<Rightarrow> 'a \<Rightarrow> bool" where
   11.13  "isin Leaf x = False" |
   11.14 -"isin (Node2 l a r) x = (x < a \<and> isin l x \<or> x=a \<or> isin r x)" |
   11.15 +"isin (Node2 l a r) x =
   11.16 +  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)" |
   11.17  "isin (Node3 l a m b r) x =
   11.18 -  (x < a \<and> isin l x \<or> x > b \<and> isin r x \<or> x = a \<or> x = b \<or> isin m x)"
   11.19 +  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> (case cmp x b of
   11.20 +   LT \<Rightarrow> isin m x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x))"
   11.21  
   11.22  datatype 'a up\<^sub>i = T\<^sub>i "'a tree23" | Up\<^sub>i "'a tree23" 'a "'a tree23"
   11.23  
   11.24 @@ -20,38 +23,35 @@
   11.25  "tree\<^sub>i (T\<^sub>i t) = t" |
   11.26  "tree\<^sub>i (Up\<^sub>i l p r) = Node2 l p r"
   11.27  
   11.28 -fun ins :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>i" where
   11.29 +fun ins :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>i" where
   11.30  "ins x Leaf = Up\<^sub>i Leaf x Leaf" |
   11.31  "ins x (Node2 l a r) =
   11.32 -   (if x < a then
   11.33 -      case ins x l of
   11.34 -         T\<^sub>i l' => T\<^sub>i (Node2 l' a r)
   11.35 -       | Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)
   11.36 -    else if x=a then T\<^sub>i (Node2 l x r)
   11.37 -    else
   11.38 -      case ins x r of
   11.39 -        T\<^sub>i r' => T\<^sub>i (Node2 l a r')
   11.40 -      | Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2))" |
   11.41 +   (case cmp x a of
   11.42 +      LT \<Rightarrow> (case ins x l of
   11.43 +              T\<^sub>i l' => T\<^sub>i (Node2 l' a r)
   11.44 +            | Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)) |
   11.45 +      EQ \<Rightarrow> T\<^sub>i (Node2 l x r) |
   11.46 +      GT \<Rightarrow> (case ins x r of
   11.47 +              T\<^sub>i r' => T\<^sub>i (Node2 l a r')
   11.48 +            | Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2)))" |
   11.49  "ins x (Node3 l a m b r) =
   11.50 -   (if x < a then
   11.51 -      case ins x l of
   11.52 -        T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r)
   11.53 -      | Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)
   11.54 -    else
   11.55 -    if x > b then
   11.56 -      case ins x r of
   11.57 -        T\<^sub>i r' => T\<^sub>i (Node3 l a m b r')
   11.58 -      | Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)
   11.59 -    else
   11.60 -    if x=a \<or> x = b then T\<^sub>i (Node3 l a m b r)
   11.61 -    else
   11.62 -      case ins x m of
   11.63 -        T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r)
   11.64 -      | Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))"
   11.65 +   (case cmp x a of
   11.66 +      LT \<Rightarrow> (case ins x l of
   11.67 +              T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r)
   11.68 +            | Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)) |
   11.69 +      EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
   11.70 +      GT \<Rightarrow> (case cmp x b of
   11.71 +               GT \<Rightarrow> (case ins x r of
   11.72 +                       T\<^sub>i r' => T\<^sub>i (Node3 l a m b r')
   11.73 +                     | Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)) |
   11.74 +               EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
   11.75 +               LT \<Rightarrow> (case ins x m of
   11.76 +                       T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r)
   11.77 +                     | Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))))"
   11.78  
   11.79  hide_const insert
   11.80  
   11.81 -definition insert :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
   11.82 +definition insert :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
   11.83  "insert x t = tree\<^sub>i(ins x t)"
   11.84  
   11.85  datatype 'a up\<^sub>d = T\<^sub>d "'a tree23" | Up\<^sub>d "'a tree23"
   11.86 @@ -93,32 +93,34 @@
   11.87  "del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" |
   11.88  "del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))"
   11.89  
   11.90 -fun del :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d"
   11.91 +fun del :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d"
   11.92  where
   11.93  "del x Leaf = T\<^sub>d Leaf" |
   11.94  "del x (Node2 Leaf a Leaf) = (if x = a then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf a Leaf))" |
   11.95  "del x (Node3 Leaf a Leaf b Leaf) = T\<^sub>d(if x = a then Node2 Leaf b Leaf
   11.96    else if x = b then Node2 Leaf a Leaf else Node3 Leaf a Leaf b Leaf)" |
   11.97 -"del x (Node2 l a r) = (if x<a then node21 (del x l) a r else
   11.98 -  if x > a then node22 l a (del x r) else
   11.99 -  let (a',t) = del_min r in node22 l a' t)" |
  11.100 -"del x (Node3 l a m b r) = (if x<a then node31 (del x l) a m b r else
  11.101 -  if x = a then let (a',m') = del_min m in node32 l a' m' b r else
  11.102 -  if x < b then node32 l a (del x m) b r else
  11.103 -  if x = b then let (b',r') = del_min r in node33 l a m b' r'
  11.104 -  else node33 l a m b (del x r))"
  11.105 +"del x (Node2 l a r) = (case cmp x a of
  11.106 +  LT \<Rightarrow> node21 (del x l) a r |
  11.107 +  GT \<Rightarrow> node22 l a (del x r) |
  11.108 +  EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" |
  11.109 +"del x (Node3 l a m b r) = (case cmp x a of
  11.110 +  LT \<Rightarrow> node31 (del x l) a m b r |
  11.111 +  EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r |
  11.112 +  GT \<Rightarrow> (case cmp x b of
  11.113 +          LT \<Rightarrow> node32 l a (del x m) b r |
  11.114 +          EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' |
  11.115 +          GT \<Rightarrow> node33 l a m b (del x r)))"
  11.116  
  11.117 -definition delete :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
  11.118 +definition delete :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
  11.119  "delete x t = tree\<^sub>d(del x t)"
  11.120  
  11.121  
  11.122  subsection "Functional Correctness"
  11.123  
  11.124 -
  11.125  subsubsection "Proofs for isin"
  11.126  
  11.127  lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
  11.128 -by (induction t) (auto simp: elems_simps1)
  11.129 +by (induction t) (auto simp: elems_simps1 ball_Un)
  11.130  
  11.131  lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
  11.132  by (induction t) (auto simp: elems_simps2)
  11.133 @@ -128,7 +130,7 @@
  11.134  
  11.135  lemma inorder_ins:
  11.136    "sorted(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(ins x t)) = ins_list x (inorder t)"
  11.137 -by(induction t) (auto simp: ins_list_simps split: up\<^sub>i.splits) (* 38 secs in 2015 *)
  11.138 +by(induction t) (auto simp: ins_list_simps split: up\<^sub>i.splits)
  11.139  
  11.140  lemma inorder_insert:
  11.141    "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
  11.142 @@ -195,7 +197,7 @@
  11.143  end
  11.144  
  11.145  lemma bal_ins: "bal t \<Longrightarrow> bal (tree\<^sub>i(ins a t)) \<and> height(ins a t) = height t"
  11.146 -by (induct t) (auto split: up\<^sub>i.split) (* 87 secs in 2015 *)
  11.147 +by (induct t) (auto split: up\<^sub>i.split) (* 15 secs in 2015 *)
  11.148  
  11.149  text{* Now an alternative proof (by Brian Huffman) that runs faster because
  11.150  two properties (balance and height) are combined in one predicate. *}
    12.1 --- a/src/HOL/Data_Structures/Tree_Map.thy	Wed Nov 04 15:07:23 2015 +0100
    12.2 +++ b/src/HOL/Data_Structures/Tree_Map.thy	Thu Nov 05 08:27:14 2015 +0100
    12.3 @@ -8,23 +8,24 @@
    12.4    Map_by_Ordered
    12.5  begin
    12.6  
    12.7 -fun lookup :: "('a::linorder*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
    12.8 +fun lookup :: "('a::cmp*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
    12.9  "lookup Leaf x = None" |
   12.10 -"lookup (Node l (a,b) r) x = (if x < a then lookup l x else
   12.11 -  if x > a then lookup r x else Some b)"
   12.12 +"lookup (Node l (a,b) r) x =
   12.13 +  (case cmp x a of LT \<Rightarrow> lookup l x | GT \<Rightarrow> lookup r x | EQ \<Rightarrow> Some b)"
   12.14  
   12.15 -fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
   12.16 +fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
   12.17  "update x y Leaf = Node Leaf (x,y) Leaf" |
   12.18 -"update x y (Node l (a,b) r) =
   12.19 -   (if x < a then Node (update x y l) (a,b) r
   12.20 -    else if x = a then Node l (x,y) r
   12.21 -    else Node l (a,b) (update x y r))"
   12.22 +"update x y (Node l (a,b) r) = (case cmp x a of
   12.23 +   LT \<Rightarrow> Node (update x y l) (a,b) r |
   12.24 +   EQ \<Rightarrow> Node l (x,y) r |
   12.25 +   GT \<Rightarrow> Node l (a,b) (update x y r))"
   12.26  
   12.27 -fun delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
   12.28 +fun delete :: "'a::cmp \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
   12.29  "delete x Leaf = Leaf" |
   12.30 -"delete x (Node l (a,b) r) = (if x < a then Node (delete x l) (a,b) r else
   12.31 -  if x > a then Node l (a,b) (delete x r) else
   12.32 -  if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
   12.33 +"delete x (Node l (a,b) r) = (case cmp x a of
   12.34 +  LT \<Rightarrow> Node (delete x l) (a,b) r |
   12.35 +  GT \<Rightarrow> Node l (a,b) (delete x r) |
   12.36 +  EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
   12.37  
   12.38  
   12.39  subsection "Functional Correctness Proofs"
   12.40 @@ -49,7 +50,6 @@
   12.41    "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   12.42  by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
   12.43  
   12.44 -
   12.45  interpretation Map_by_Ordered
   12.46  where empty = Leaf and lookup = lookup and update = update and delete = delete
   12.47  and inorder = inorder and wf = "\<lambda>_. True"
    13.1 --- a/src/HOL/Data_Structures/Tree_Set.thy	Wed Nov 04 15:07:23 2015 +0100
    13.2 +++ b/src/HOL/Data_Structures/Tree_Set.thy	Thu Nov 05 08:27:14 2015 +0100
    13.3 @@ -5,31 +5,34 @@
    13.4  theory Tree_Set
    13.5  imports
    13.6    "~~/src/HOL/Library/Tree"
    13.7 +  Cmp
    13.8    Set_by_Ordered
    13.9  begin
   13.10  
   13.11 -fun isin :: "'a::linorder tree \<Rightarrow> 'a \<Rightarrow> bool" where
   13.12 +fun isin :: "'a::cmp tree \<Rightarrow> 'a \<Rightarrow> bool" where
   13.13  "isin Leaf x = False" |
   13.14 -"isin (Node l a r) x = (x < a \<and> isin l x \<or> x=a \<or> isin r x)"
   13.15 +"isin (Node l a r) x =
   13.16 +  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)"
   13.17  
   13.18  hide_const (open) insert
   13.19  
   13.20 -fun insert :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
   13.21 +fun insert :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
   13.22  "insert x Leaf = Node Leaf x Leaf" |
   13.23 -"insert x (Node l a r) =
   13.24 -   (if x < a then Node (insert x l) a r else
   13.25 -    if x = a then Node l a r
   13.26 -    else Node l a (insert x r))"
   13.27 +"insert x (Node l a r) = (case cmp x a of
   13.28 +      LT \<Rightarrow> Node (insert x l) a r |
   13.29 +      EQ \<Rightarrow> Node l a r |
   13.30 +      GT \<Rightarrow> Node l a (insert x r))"
   13.31  
   13.32  fun del_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
   13.33  "del_min (Node Leaf a r) = (a, r)" |
   13.34  "del_min (Node l a r) = (let (x,l') = del_min l in (x, Node l' a r))"
   13.35  
   13.36 -fun delete :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
   13.37 +fun delete :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
   13.38  "delete x Leaf = Leaf" |
   13.39 -"delete x (Node l a r) = (if x < a then Node (delete x l) a r else
   13.40 -  if x > a then Node l a (delete x r) else
   13.41 -  if r = Leaf then l else let (a',r') = del_min r in Node l a' r')"
   13.42 +"delete x (Node l a r) = (case cmp x a of
   13.43 +  LT \<Rightarrow>  Node (delete x l) a r |
   13.44 +  GT \<Rightarrow>  Node l a (delete x r) |
   13.45 +  EQ \<Rightarrow> if r = Leaf then l else let (a',r') = del_min r in Node l a' r')"
   13.46  
   13.47  
   13.48  subsection "Functional Correctness Proofs"
   13.49 @@ -56,7 +59,6 @@
   13.50    "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   13.51  by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
   13.52  
   13.53 -
   13.54  interpretation Set_by_Ordered
   13.55  where empty = Leaf and isin = isin and insert = insert and delete = delete
   13.56  and inorder = inorder and wf = "\<lambda>_. True"