author nipkow Wed Mar 02 10:01:31 2016 +0100 (2016-03-02) changeset 62496 f187aaf602c4 parent 62495 83db706d7771 child 62497 5b5b704f4811
added invariant proofs to AA trees
     1.1 --- a/src/HOL/Data_Structures/AA_Map.thy	Tue Mar 01 22:49:33 2016 +0100
1.2 +++ b/src/HOL/Data_Structures/AA_Map.thy	Wed Mar 02 10:01:31 2016 +0100
1.3 @@ -1,6 +1,6 @@
1.4  (* Author: Tobias Nipkow *)
1.5
1.6 -section "AA Implementation of Maps"
1.7 +section "AA Tree Implementation of Maps"
1.8
1.9  theory AA_Map
1.10  imports
1.11 @@ -26,21 +26,190 @@
1.12              else let (l',ab') = del_max l in adjust (Node lv l' ab' r)))"
1.13
1.14
1.15 +subsection "Invariance"
1.16 +
1.17 +subsubsection "Proofs for insert"
1.18 +
1.19 +lemma lvl_update_aux:
1.20 +  "lvl (update x y t) = lvl t \<or> lvl (update x y t) = lvl t + 1 \<and> sngl (update x y t)"
1.21 +apply(induction t)
1.22 +apply (auto simp: lvl_skew)
1.23 +apply (metis Suc_eq_plus1 lvl.simps(2) lvl_split lvl_skew)+
1.24 +done
1.25 +
1.26 +lemma lvl_update: obtains
1.27 +  (Same) "lvl (update x y t) = lvl t" |
1.28 +  (Incr) "lvl (update x y t) = lvl t + 1" "sngl (update x y t)"
1.29 +using lvl_update_aux by fastforce
1.30 +
1.31 +declare invar.simps(2)[simp]
1.32 +
1.33 +lemma lvl_update_sngl: "invar t \<Longrightarrow> sngl t \<Longrightarrow> lvl(update x y t) = lvl t"
1.34 +proof (induction t rule: update.induct)
1.35 +  case (2 x y lv t1 a b t2)
1.36 +  consider (LT) "x < a" | (GT) "x > a" | (EQ) "x = a"
1.37 +    using less_linear by blast
1.38 +  thus ?case proof cases
1.39 +    case LT
1.40 +    thus ?thesis using 2 by (auto simp add: skew_case split_case split: tree.splits)
1.41 +  next
1.42 +    case GT
1.43 +    thus ?thesis using 2 proof (cases t1)
1.44 +      case Node
1.45 +      thus ?thesis using 2 GT
1.46 +        apply (auto simp add: skew_case split_case split: tree.splits)
1.47 +        by (metis less_not_refl2 lvl.simps(2) lvl_update_aux n_not_Suc_n sngl.simps(3))+
1.48 +    qed (auto simp add: lvl_0_iff)
1.49 +  qed simp
1.50 +qed simp
1.51 +
1.52 +lemma lvl_update_incr_iff: "(lvl(update a b t) = lvl t + 1) \<longleftrightarrow>
1.53 +  (EX l x r. update a b t = Node (lvl t + 1) l x r \<and> lvl l = lvl r)"
1.54 +apply(cases t)
1.55 +apply(auto simp add: skew_case split_case split: if_splits)
1.56 +apply(auto split: tree.splits if_splits)
1.57 +done
1.58 +
1.59 +lemma invar_update: "invar t \<Longrightarrow> invar(update a b t)"
1.60 +proof(induction t)
1.61 +  case (Node n l xy r)
1.62 +  hence il: "invar l" and ir: "invar r" by auto
1.63 +  obtain x y where [simp]: "xy = (x,y)" by fastforce
1.64 +  note N = Node
1.65 +  let ?t = "Node n l xy r"
1.66 +  have "a < x \<or> a = x \<or> x < a" by auto
1.67 +  moreover
1.68 +  { assume "a < x"
1.69 +    note iil = Node.IH(1)[OF il]
1.70 +    have ?case
1.71 +    proof (cases rule: lvl_update[of a b l])
1.72 +      case (Same) thus ?thesis
1.73 +        using \<open>a<x\<close> invar_NodeL[OF Node.prems iil Same]
1.74 +        by (simp add: skew_invar split_invar del: invar.simps)
1.75 +    next
1.76 +      case (Incr)
1.77 +      then obtain t1 w t2 where ial[simp]: "update a b l = Node n t1 w t2"
1.78 +        using Node.prems by (auto simp: lvl_Suc_iff)
1.79 +      have l12: "lvl t1 = lvl t2"
1.80 +        by (metis Incr(1) ial lvl_update_incr_iff tree.inject)
1.81 +      have "update a b ?t = split(skew(Node n (update a b l) xy r))"
1.83 +      also have "skew(Node n (update a b l) xy r) = Node n t1 w (Node n t2 xy r)"
1.84 +        by(simp)
1.85 +      also have "invar(split \<dots>)"
1.86 +      proof (cases r)
1.87 +        case Leaf
1.88 +        hence "l = Leaf" using Node.prems by(auto simp: lvl_0_iff)
1.89 +        thus ?thesis using Leaf ial by simp
1.90 +      next
1.91 +        case [simp]: (Node m t3 y t4)
1.92 +        show ?thesis (*using N(3) iil l12 by(auto)*)
1.93 +        proof cases
1.94 +          assume "m = n" thus ?thesis using N(3) iil by(auto)
1.95 +        next
1.96 +          assume "m \<noteq> n" thus ?thesis using N(3) iil l12 by(auto)
1.97 +        qed
1.98 +      qed
1.99 +      finally show ?thesis .
1.100 +    qed
1.101 +  }
1.102 +  moreover
1.103 +  { assume "x < a"
1.104 +    note iir = Node.IH(2)[OF ir]
1.105 +    from \<open>invar ?t\<close> have "n = lvl r \<or> n = lvl r + 1" by auto
1.106 +    hence ?case
1.107 +    proof
1.108 +      assume 0: "n = lvl r"
1.109 +      have "update a b ?t = split(skew(Node n l xy (update a b r)))"
1.110 +        using \<open>a>x\<close> by(auto)
1.111 +      also have "skew(Node n l xy (update a b r)) = Node n l xy (update a b r)"
1.112 +        using Node.prems by(simp add: skew_case split: tree.split)
1.113 +      also have "invar(split \<dots>)"
1.114 +      proof -
1.115 +        from lvl_update_sngl[OF ir sngl_if_invar[OF \<open>invar ?t\<close> 0], of a b]
1.116 +        obtain t1 p t2 where iar: "update a b r = Node n t1 p t2"
1.117 +          using Node.prems 0 by (auto simp: lvl_Suc_iff)
1.118 +        from Node.prems iar 0 iir
1.119 +        show ?thesis by (auto simp: split_case split: tree.splits)
1.120 +      qed
1.121 +      finally show ?thesis .
1.122 +    next
1.123 +      assume 1: "n = lvl r + 1"
1.124 +      hence "sngl ?t" by(cases r) auto
1.125 +      show ?thesis
1.126 +      proof (cases rule: lvl_update[of a b r])
1.127 +        case (Same)
1.128 +        show ?thesis using \<open>x<a\<close> il ir invar_NodeR[OF Node.prems 1 iir Same]
1.129 +          by (auto simp add: skew_invar split_invar)
1.130 +      next
1.131 +        case (Incr)
1.132 +        thus ?thesis using invar_NodeR2[OF invar ?t Incr(2) 1 iir] 1 \<open>x < a\<close>
1.133 +          by (auto simp add: skew_invar split_invar split: if_splits)
1.134 +      qed
1.135 +    qed
1.136 +  }
1.137 +  moreover { assume "a = x" hence ?case using Node.prems by auto }
1.138 +  ultimately show ?case by blast
1.139 +qed simp
1.140 +
1.141 +subsubsection "Proofs for delete"
1.142 +
1.143 +declare invar.simps(2)[simp del]
1.144 +
1.145 +theorem post_delete: "invar t \<Longrightarrow> post_del t (delete x t)"
1.146 +proof (induction t)
1.147 +  case (Node lv l ab r)
1.148 +
1.149 +  obtain a b where [simp]: "ab = (a,b)" by fastforce
1.150 +
1.151 +  let ?l' = "delete x l" and ?r' = "delete x r"
1.152 +  let ?t = "Node lv l ab r" let ?t' = "delete x ?t"
1.153 +
1.154 +  from Node.prems have inv_l: "invar l" and inv_r: "invar r" by (auto)
1.155 +
1.156 +  note post_l' = Node.IH(1)[OF inv_l]
1.157 +  note preL = pre_adj_if_postL[OF Node.prems post_l']
1.158 +
1.159 +  note post_r' = Node.IH(2)[OF inv_r]
1.160 +  note preR = pre_adj_if_postR[OF Node.prems post_r']
1.161 +
1.162 +  show ?case
1.163 +  proof (cases rule: linorder_cases[of x a])
1.164 +    case less
1.166 +  next
1.167 +    case greater
1.168 +    thus ?thesis using Node.prems preR by (simp add: post_del_adjR post_r')
1.169 +  next
1.170 +    case equal
1.171 +    show ?thesis
1.172 +    proof cases
1.173 +      assume "l = Leaf" thus ?thesis using equal Node.prems
1.174 +        by(auto simp: post_del_def invar.simps(2))
1.175 +    next
1.176 +      assume "l \<noteq> Leaf" thus ?thesis using equal Node.prems
1.178 +    qed
1.179 +  qed
1.181 +
1.182 +
1.183  subsection {* Functional Correctness Proofs *}
1.184
1.185  theorem inorder_update:
1.186    "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
1.187  by (induct t) (auto simp: upd_list_simps inorder_split inorder_skew)
1.188
1.189 -
1.190  theorem inorder_delete:
1.191 -  "sorted1(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
1.192 +  "\<lbrakk>invar t; sorted1(inorder t)\<rbrakk> \<Longrightarrow>
1.193 +  inorder (delete x t) = del_list x (inorder t)"
1.194  by(induction t)
1.195 -  (auto simp: del_list_simps inorder_adjust del_maxD split: prod.splits)
1.197 +              post_del_max post_delete del_maxD split: prod.splits)
1.198
1.199 -interpretation Map_by_Ordered
1.200 +interpretation I: Map_by_Ordered
1.201  where empty = Leaf and lookup = lookup and update = update and delete = delete
1.202 -and inorder = inorder and inv = "\<lambda>_. True"
1.203 +and inorder = inorder and inv = invar
1.204  proof (standard, goal_cases)
1.205    case 1 show ?case by simp
1.206  next
1.207 @@ -49,6 +218,12 @@
1.208    case 3 thus ?case by(simp add: inorder_update)
1.209  next
1.210    case 4 thus ?case by(simp add: inorder_delete)
1.211 -qed auto
1.212 +next
1.213 +  case 5 thus ?case by(simp)
1.214 +next
1.215 +  case 6 thus ?case by(simp add: invar_update)
1.216 +next
1.217 +  case 7 thus ?case using post_delete by(auto simp: post_del_def)
1.218 +qed
1.219
1.220  end

     2.1 --- a/src/HOL/Data_Structures/AA_Set.thy	Tue Mar 01 22:49:33 2016 +0100
2.2 +++ b/src/HOL/Data_Structures/AA_Set.thy	Wed Mar 02 10:01:31 2016 +0100
2.3 @@ -1,7 +1,5 @@
2.4  (*
2.5 -Author: Tobias Nipkow
2.6 -
2.8 +Author: Tobias Nipkow and Daniel StÃ¼we
2.9  *)
2.10
2.11  section \<open>AA Tree Implementation of Sets\<close>
2.12 @@ -17,13 +15,13 @@
2.13  fun lvl :: "'a aa_tree \<Rightarrow> nat" where
2.14  "lvl Leaf = 0" |
2.15  "lvl (Node lv _ _ _) = lv"
2.16 -(*
2.17 +
2.18  fun invar :: "'a aa_tree \<Rightarrow> bool" where
2.19  "invar Leaf = True" |
2.20  "invar (Node h l a r) =
2.21   (invar l \<and> invar r \<and>
2.22    h = lvl l + 1 \<and> (h = lvl r + 1 \<or> (\<exists>lr b rr. r = Node h lr b rr \<and> h = lvl rr + 1)))"
2.23 -*)
2.24 +
2.25  fun skew :: "'a aa_tree \<Rightarrow> 'a aa_tree" where
2.26  "skew (Node lva (Node lvb t1 b t2) a t3) =
2.27    (if lva = lvb then Node lva t1 b (Node lva t2 a t3) else Node lva (Node lvb t1 b t2) a t3)" |
2.28 @@ -46,11 +44,6 @@
2.29       GT \<Rightarrow> split (skew (Node lv t1 a (insert x t2))) |
2.30       EQ \<Rightarrow> Node lv t1 x t2)"
2.31
2.32 -(* wrong in paper! *)
2.33 -fun del_max :: "'a aa_tree \<Rightarrow> 'a aa_tree * 'a" where
2.34 -"del_max (Node lv l a Leaf) = (l,a)" |
2.35 -"del_max (Node lv l a r) = (let (r',b) = del_max r in (Node lv l a r', b))"
2.36 -
2.37  fun sngl :: "'a aa_tree \<Rightarrow> bool" where
2.38  "sngl Leaf = False" |
2.39  "sngl (Node _ _ _ Leaf) = True" |
2.40 @@ -65,19 +58,28 @@
2.41      if lvl r < lv-1
2.42      then case l of
2.43             Node lva t1 a (Node lvb t2 b t3)
2.44 -             \<Rightarrow> Node (lvb+1) (Node lva t1 a t2) b (Node (lv-1) t3 x r) |
2.45 -           _ \<Rightarrow> t (* unreachable *)
2.46 +             \<Rightarrow> Node (lvb+1) (Node lva t1 a t2) b (Node (lv-1) t3 x r)
2.47      else
2.48      if lvl r < lv then split (Node (lv-1) l x r)
2.49      else
2.50        case r of
2.51 -        Leaf \<Rightarrow> Leaf (* unreachable *) |
2.52          Node lvb t1 b t4 \<Rightarrow>
2.53            (case t1 of
2.54               Node lva t2 a t3
2.55                 \<Rightarrow> Node (lva+1) (Node (lv-1) l x t2) a
2.56 -                    (split (Node (if sngl t1 then lva-1 else lva) t3 b t4))
2.57 -           | _ \<Rightarrow> t (* unreachable *))))"
2.58 +                    (split (Node (if sngl t1 then lva else lva+1) t3 b t4)))))"
2.59 +
2.60 +text{* In the paper, the last case of @{const adjust} is expressed with the help of an
2.61 +incorrect auxiliary function \texttt{nlvl}.
2.62 +
2.63 +Function @{text del_max} below is called \texttt{dellrg} in the paper.
2.64 +The latter is incorrect for two reasons: \texttt{dellrg} is meant to delete the largest
2.65 +element but recurses on the left instead of the right subtree; the invariant
2.66 +is not restored.*}
2.67 +
2.68 +fun del_max :: "'a aa_tree \<Rightarrow> 'a aa_tree * 'a" where
2.69 +"del_max (Node lv l a Leaf) = (l,a)" |
2.70 +"del_max (Node lv l a r) = (let (r',b) = del_max r in (adjust(Node lv l a r'), b))"
2.71
2.72  fun delete :: "'a::cmp \<Rightarrow> 'a aa_tree \<Rightarrow> 'a aa_tree" where
2.73  "delete _ Leaf = Leaf" |
2.74 @@ -88,9 +90,368 @@
2.75       EQ \<Rightarrow> (if l = Leaf then r
2.76              else let (l',b) = del_max l in adjust (Node lv l' b r)))"
2.77
2.79 +"pre_adjust (Node lv l a r) = (invar l \<and> invar r \<and>
2.80 +    ((lv = lvl l + 1 \<and> (lv = lvl r + 1 \<or> lv = lvl r + 2 \<or> lv = lvl r \<and> sngl r)) \<or>
2.81 +     (lv = lvl l + 2 \<and> (lv = lvl r + 1 \<or> lv = lvl r \<and> sngl r))))"
2.82 +
2.84 +
2.85 +subsection "Auxiliary Proofs"
2.86 +
2.87 +lemma split_case: "split t = (case t of
2.88 +  Node lvx a x (Node lvy b y (Node lvz c z d)) \<Rightarrow>
2.89 +   (if lvx = lvy \<and> lvy = lvz
2.90 +    then Node (lvx+1) (Node lvx a x b) y (Node lvx c z d)
2.91 +    else t)
2.92 +  | t \<Rightarrow> t)"
2.93 +by(auto split: tree.split)
2.94 +
2.95 +lemma skew_case: "skew t = (case t of
2.96 +  Node lvx (Node lvy a y b) x c \<Rightarrow>
2.97 +  (if lvx = lvy then Node lvx a y (Node lvx b x c) else t)
2.98 + | t \<Rightarrow> t)"
2.99 +by(auto split: tree.split)
2.100 +
2.101 +lemma lvl_0_iff: "invar t \<Longrightarrow> lvl t = 0 \<longleftrightarrow> t = Leaf"
2.102 +by(cases t) auto
2.103 +
2.104 +lemma lvl_Suc_iff: "lvl t = Suc n \<longleftrightarrow> (\<exists> l a r. t = Node (Suc n) l a r)"
2.105 +by(cases t) auto
2.106 +
2.107 +lemma lvl_skew: "lvl (skew t) = lvl t"
2.108 +by(induction t rule: skew.induct) auto
2.109 +
2.110 +lemma lvl_split: "lvl (split t) = lvl t \<or> lvl (split t) = lvl t + 1 \<and> sngl (split t)"
2.111 +by(induction t rule: split.induct) auto
2.112 +
2.113 +lemma invar_2Nodes:"invar (Node lv l x (Node rlv rl rx rr)) =
2.114 +     (invar l \<and> invar \<langle>rlv, rl, rx, rr\<rangle> \<and> lv = Suc (lvl l) \<and>
2.115 +     (lv = Suc rlv \<or> rlv = lv \<and> lv = Suc (lvl rr)))"
2.116 +by simp
2.117 +
2.118 +lemma invar_NodeLeaf[simp]:
2.119 +  "invar (Node lv l x Leaf) = (invar l \<and> lv = Suc (lvl l) \<and> lv = Suc 0)"
2.120 +by simp
2.121 +
2.122 +lemma sngl_if_invar: "invar (Node n l a r) \<Longrightarrow> n = lvl r \<Longrightarrow> sngl r"
2.123 +by(cases r rule: sngl.cases) clarsimp+
2.124 +
2.125 +
2.126 +subsection "Invariance"
2.127 +
2.128 +subsubsection "Proofs for insert"
2.129 +
2.130 +lemma lvl_insert_aux:
2.131 +  "lvl (insert x t) = lvl t \<or> lvl (insert x t) = lvl t + 1 \<and> sngl (insert x t)"
2.132 +apply(induction t)
2.133 +apply (auto simp: lvl_skew)
2.134 +apply (metis Suc_eq_plus1 lvl.simps(2) lvl_split lvl_skew)+
2.135 +done
2.136 +
2.137 +lemma lvl_insert: obtains
2.138 +  (Same) "lvl (insert x t) = lvl t" |
2.139 +  (Incr) "lvl (insert x t) = lvl t + 1" "sngl (insert x t)"
2.140 +using lvl_insert_aux by blast
2.141 +
2.142 +lemma lvl_insert_sngl: "invar t \<Longrightarrow> sngl t \<Longrightarrow> lvl(insert x t) = lvl t"
2.143 +proof (induction t rule: "insert.induct" )
2.144 +  case (2 x lv t1 a t2)
2.145 +  consider (LT) "x < a" | (GT) "x > a" | (EQ) "x = a"
2.146 +    using less_linear by blast
2.147 +  thus ?case proof cases
2.148 +    case LT
2.149 +    thus ?thesis using 2 by (auto simp add: skew_case split_case split: tree.splits)
2.150 +  next
2.151 +    case GT
2.152 +    thus ?thesis using 2 proof (cases t1)
2.153 +      case Node
2.154 +      thus ?thesis using 2 GT
2.155 +        apply (auto simp add: skew_case split_case split: tree.splits)
2.156 +        by (metis less_not_refl2 lvl.simps(2) lvl_insert_aux n_not_Suc_n sngl.simps(3))+
2.157 +    qed (auto simp add: lvl_0_iff)
2.158 +  qed simp
2.159 +qed simp
2.160 +
2.161 +lemma skew_invar: "invar t \<Longrightarrow> skew t = t"
2.162 +by(induction t rule: skew.induct) auto
2.163 +
2.164 +lemma split_invar: "invar t \<Longrightarrow> split t = t"
2.165 +by(induction t rule: split.induct) clarsimp+
2.166 +
2.167 +lemma invar_NodeL:
2.168 +  "\<lbrakk> invar(Node n l x r); invar l'; lvl l' = lvl l \<rbrakk> \<Longrightarrow> invar(Node n l' x r)"
2.169 +by(auto)
2.170 +
2.171 +lemma invar_NodeR:
2.172 +  "\<lbrakk> invar(Node n l x r); n = lvl r + 1; invar r'; lvl r' = lvl r \<rbrakk> \<Longrightarrow> invar(Node n l x r')"
2.173 +by(auto)
2.174 +
2.175 +lemma invar_NodeR2:
2.176 +  "\<lbrakk> invar(Node n l x r); sngl r'; n = lvl r + 1; invar r'; lvl r' = n \<rbrakk> \<Longrightarrow> invar(Node n l x r')"
2.177 +by(cases r' rule: sngl.cases) clarsimp+
2.178 +
2.179 +
2.180 +lemma lvl_insert_incr_iff: "(lvl(insert a t) = lvl t + 1) \<longleftrightarrow>
2.181 +  (EX l x r. insert a t = Node (lvl t + 1) l x r \<and> lvl l = lvl r)"
2.182 +apply(cases t)
2.183 +apply(auto simp add: skew_case split_case split: if_splits)
2.184 +apply(auto split: tree.splits if_splits)
2.185 +done
2.186 +
2.187 +lemma invar_insert: "invar t \<Longrightarrow> invar(insert a t)"
2.188 +proof(induction t)
2.189 +  case (Node n l x r)
2.190 +  hence il: "invar l" and ir: "invar r" by auto
2.191 +  note N = Node
2.192 +  let ?t = "Node n l x r"
2.193 +  have "a < x \<or> a = x \<or> x < a" by auto
2.194 +  moreover
2.195 +  { assume "a < x"
2.196 +    note iil = Node.IH(1)[OF il]
2.197 +    have ?case
2.198 +    proof (cases rule: lvl_insert[of a l])
2.199 +      case (Same) thus ?thesis
2.200 +        using \<open>a<x\<close> invar_NodeL[OF Node.prems iil Same]
2.201 +        by (simp add: skew_invar split_invar del: invar.simps)
2.202 +    next
2.203 +      case (Incr)
2.204 +      then obtain t1 w t2 where ial[simp]: "insert a l = Node n t1 w t2"
2.205 +        using Node.prems by (auto simp: lvl_Suc_iff)
2.206 +      have l12: "lvl t1 = lvl t2"
2.207 +        by (metis Incr(1) ial lvl_insert_incr_iff tree.inject)
2.208 +      have "insert a ?t = split(skew(Node n (insert a l) x r))"
2.210 +      also have "skew(Node n (insert a l) x r) = Node n t1 w (Node n t2 x r)"
2.211 +        by(simp)
2.212 +      also have "invar(split \<dots>)"
2.213 +      proof (cases r)
2.214 +        case Leaf
2.215 +        hence "l = Leaf" using Node.prems by(auto simp: lvl_0_iff)
2.216 +        thus ?thesis using Leaf ial by simp
2.217 +      next
2.218 +        case [simp]: (Node m t3 y t4)
2.219 +        show ?thesis (*using N(3) iil l12 by(auto)*)
2.220 +        proof cases
2.221 +          assume "m = n" thus ?thesis using N(3) iil by(auto)
2.222 +        next
2.223 +          assume "m \<noteq> n" thus ?thesis using N(3) iil l12 by(auto)
2.224 +        qed
2.225 +      qed
2.226 +      finally show ?thesis .
2.227 +    qed
2.228 +  }
2.229 +  moreover
2.230 +  { assume "x < a"
2.231 +    note iir = Node.IH(2)[OF ir]
2.232 +    from \<open>invar ?t\<close> have "n = lvl r \<or> n = lvl r + 1" by auto
2.233 +    hence ?case
2.234 +    proof
2.235 +      assume 0: "n = lvl r"
2.236 +      have "insert a ?t = split(skew(Node n l x (insert a r)))"
2.237 +        using \<open>a>x\<close> by(auto)
2.238 +      also have "skew(Node n l x (insert a r)) = Node n l x (insert a r)"
2.239 +        using Node.prems by(simp add: skew_case split: tree.split)
2.240 +      also have "invar(split \<dots>)"
2.241 +      proof -
2.242 +        from lvl_insert_sngl[OF ir sngl_if_invar[OF \<open>invar ?t\<close> 0], of a]
2.243 +        obtain t1 y t2 where iar: "insert a r = Node n t1 y t2"
2.244 +          using Node.prems 0 by (auto simp: lvl_Suc_iff)
2.245 +        from Node.prems iar 0 iir
2.246 +        show ?thesis by (auto simp: split_case split: tree.splits)
2.247 +      qed
2.248 +      finally show ?thesis .
2.249 +    next
2.250 +      assume 1: "n = lvl r + 1"
2.251 +      hence "sngl ?t" by(cases r) auto
2.252 +      show ?thesis
2.253 +      proof (cases rule: lvl_insert[of a r])
2.254 +        case (Same)
2.255 +        show ?thesis using \<open>x<a\<close> il ir invar_NodeR[OF Node.prems 1 iir Same]
2.256 +          by (auto simp add: skew_invar split_invar)
2.257 +      next
2.258 +        case (Incr)
2.259 +        thus ?thesis using invar_NodeR2[OF invar ?t Incr(2) 1 iir] 1 \<open>x < a\<close>
2.260 +          by (auto simp add: skew_invar split_invar split: if_splits)
2.261 +      qed
2.262 +    qed
2.263 +  }
2.264 +  moreover { assume "a = x" hence ?case using Node.prems by auto }
2.265 +  ultimately show ?case by blast
2.266 +qed simp
2.267 +
2.268 +
2.269 +subsubsection "Proofs for delete"
2.270 +
2.271 +lemma invarL: "ASSUMPTION(invar \<langle>lv, l, a, r\<rangle>) \<Longrightarrow> invar l"
2.273 +
2.274 +lemma invarR: "ASSUMPTION(invar \<langle>lv, l, a, r\<rangle>) \<Longrightarrow> invar r"
2.276 +
2.277 +lemma sngl_NodeI:
2.278 +  "sngl (Node lv l a r) \<Longrightarrow> sngl (Node lv l' a' r)"
2.279 +by(cases r) (simp_all)
2.280 +
2.281 +
2.282 +declare invarL[simp] invarR[simp]
2.283 +
2.284 +lemma pre_cases:
2.285 +assumes "pre_adjust (Node lv l x r)"
2.286 +obtains
2.287 + (tSngl) "invar l \<and> invar r \<and>
2.288 +    lv = Suc (lvl r) \<and> lvl l = lvl r" |
2.289 + (tDouble) "invar l \<and> invar r \<and>
2.290 +    lv = lvl r \<and> Suc (lvl l) = lvl r \<and> sngl r " |
2.291 + (rDown) "invar l \<and> invar r \<and>
2.292 +    lv = Suc (Suc (lvl r)) \<and>  lv = Suc (lvl l)" |
2.293 + (lDown_tSngl) "invar l \<and> invar r \<and>
2.294 +    lv = Suc (lvl r) \<and> lv = Suc (Suc (lvl l))" |
2.295 + (lDown_tDouble) "invar l \<and> invar r \<and>
2.296 +    lv = lvl r \<and> lv = Suc (Suc (lvl l)) \<and> sngl r"
2.298 +by auto
2.299 +
2.300 +declare invar.simps(2)[simp del] invar_2Nodes[simp add]
2.301 +
2.303 +  assumes pre: "pre_adjust (Node lv l a r)"
2.304 +  shows  "invar(adjust (Node lv l a r))"
2.305 +using pre proof (cases rule: pre_cases)
2.306 +  case (tDouble) thus ?thesis unfolding adjust_def by (cases r) (auto simp: invar.simps(2))
2.307 +next
2.308 +  case (rDown)
2.309 +  from rDown obtain llv ll la lr where l: "l = Node llv ll la lr" by (cases l) auto
2.310 +  from rDown show ?thesis unfolding adjust_def by (auto simp: l invar.simps(2) split: tree.splits)
2.311 +next
2.312 +  case (lDown_tDouble)
2.313 +  from lDown_tDouble obtain rlv rr ra rl where r: "r = Node rlv rl ra rr" by (cases r) auto
2.314 +  from lDown_tDouble and r obtain rrlv rrr rra rrl where
2.315 +    rr :"rr = Node rrlv rrr rra rrl" by (cases rr) auto
2.316 +  from  lDown_tDouble show ?thesis unfolding adjust_def r rr
2.317 +    apply (cases rl) apply (auto simp add: invar.simps(2))
2.318 +    using lDown_tDouble by (auto simp: split_case lvl_0_iff  elim:lvl.elims split: tree.split)
2.319 +qed (auto simp: split_case invar.simps(2) adjust_def split: tree.splits)
2.320 +
2.322 +  assumes "pre_adjust (Node lv l a r)"
2.323 +  shows "lv = lvl (adjust(Node lv l a r)) \<or> lv = lvl (adjust(Node lv l a r)) + 1"
2.324 +using assms(1) proof(cases rule: pre_cases)
2.325 +  case lDown_tSngl thus ?thesis
2.326 +    using lvl_split[of "\<langle>lvl r, l, a, r\<rangle>"] by (auto simp: adjust_def)
2.327 +next
2.328 +  case lDown_tDouble thus ?thesis
2.329 +    by (auto simp: adjust_def invar.simps(2) split: tree.split)
2.330 +qed (auto simp: adjust_def split: tree.splits)
2.331 +
2.333 +  "sngl \<langle>lv, l, a, r\<rangle>" "lv = lvl (adjust \<langle>lv, l, a, r\<rangle>)"
2.334 +  shows "sngl (adjust \<langle>lv, l, a, r\<rangle>)"
2.335 +using assms proof (cases rule: pre_cases)
2.336 +  case rDown
2.337 +  thus ?thesis using assms(2,3) unfolding adjust_def
2.338 +    by (auto simp add: skew_case) (auto split: tree.split)
2.339 +qed (auto simp: adjust_def skew_case split_case split: tree.split)
2.340 +
2.341 +definition "post_del t t' ==
2.342 +  invar t' \<and>
2.343 +  (lvl t' = lvl t \<or> lvl t' + 1 = lvl t) \<and>
2.344 +  (lvl t' = lvl t \<and> sngl t \<longrightarrow> sngl t')"
2.345 +
2.347 +  "invar\<langle>lv, l, a, r\<rangle> \<Longrightarrow> post_del r r' \<Longrightarrow> pre_adjust \<langle>lv, l, a, r'\<rangle>"
2.348 +by(cases "sngl r")
2.349 +  (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims)
2.350 +
2.352 +  "invar\<langle>lv, l, a, r\<rangle> \<Longrightarrow> post_del l l' \<Longrightarrow> pre_adjust \<langle>lv, l', b, r\<rangle>"
2.353 +by(cases "sngl r")
2.354 +  (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims)
2.355 +
2.357 +  "\<lbrakk> invar\<langle>lv, l, a, r\<rangle>; pre_adjust \<langle>lv, l', b, r\<rangle> \<rbrakk>
2.358 +  \<Longrightarrow> post_del \<langle>lv, l, a, r\<rangle> (adjust \<langle>lv, l', b, r\<rangle>)"
2.359 +unfolding post_del_def
2.361 +
2.363 +assumes "invar\<langle>lv, l, a, r\<rangle>" "pre_adjust \<langle>lv, l, a, r'\<rangle>" "post_del r r'"
2.364 +shows "post_del \<langle>lv, l, a, r\<rangle> (adjust \<langle>lv, l, a, r'\<rangle>)"
2.365 +proof(unfold post_del_def, safe del: disjCI)
2.366 +  let ?t = "\<langle>lv, l, a, r\<rangle>"
2.367 +  let ?t' = "adjust \<langle>lv, l, a, r'\<rangle>"
2.368 +  show "invar ?t'" by(rule invar_adjust[OF assms(2)])
2.369 +  show "lvl ?t' = lvl ?t \<or> lvl ?t' + 1 = lvl ?t"
2.370 +    using lvl_adjust[OF assms(2)] by auto
2.371 +  show "sngl ?t'" if as: "lvl ?t' = lvl ?t" "sngl ?t"
2.372 +  proof -
2.373 +    have s: "sngl \<langle>lv, l, a, r'\<rangle>"
2.374 +    proof(cases r')
2.375 +      case Leaf thus ?thesis by simp
2.376 +    next
2.377 +      case Node thus ?thesis using as(2) assms(1,3)
2.378 +      by (cases r) (auto simp: post_del_def)
2.379 +    qed
2.380 +    show ?thesis using as(1) sngl_adjust[OF assms(2) s] by simp
2.381 +  qed
2.382 +qed
2.383 +
2.384 +declare prod.splits[split]
2.385 +
2.386 +theorem post_del_max:
2.387 + "\<lbrakk> invar t; (t', x) = del_max t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow> post_del t t'"
2.388 +proof (induction t arbitrary: t' rule: del_max.induct)
2.389 +  case (2 lv l a lvr rl ra rr)
2.390 +  let ?r =  "\<langle>lvr, rl, ra, rr\<rangle>"
2.391 +  let ?t = "\<langle>lv, l, a, ?r\<rangle>"
2.392 +  from "2.prems"(2) obtain r' where r': "(r', x) = del_max ?r"
2.393 +    and [simp]: "t' = adjust \<langle>lv, l, a, r'\<rangle>" by auto
2.394 +  from  "2.IH"[OF _ r'] \<open>invar ?t\<close> have post: "post_del ?r r'" by simp
2.395 +  note preR = pre_adj_if_postR[OF \<open>invar ?t\<close> post]
2.397 +qed (auto simp: post_del_def)
2.398 +
2.399 +theorem post_delete: "invar t \<Longrightarrow> post_del t (delete x t)"
2.400 +proof (induction t)
2.401 +  case (Node lv l a r)
2.402 +
2.403 +  let ?l' = "delete x l" and ?r' = "delete x r"
2.404 +  let ?t = "Node lv l a r" let ?t' = "delete x ?t"
2.405 +
2.406 +  from Node.prems have inv_l: "invar l" and inv_r: "invar r" by (auto)
2.407 +
2.408 +  note post_l' = Node.IH(1)[OF inv_l]
2.409 +  note preL = pre_adj_if_postL[OF Node.prems post_l']
2.410 +
2.411 +  note post_r' = Node.IH(2)[OF inv_r]
2.412 +  note preR = pre_adj_if_postR[OF Node.prems post_r']
2.413 +
2.414 +  show ?case
2.415 +  proof (cases rule: linorder_cases[of x a])
2.416 +    case less
2.418 +  next
2.419 +    case greater
2.420 +    thus ?thesis using Node.prems by (simp add: post_del_adjR preR post_r')
2.421 +  next
2.422 +    case equal
2.423 +    show ?thesis
2.424 +    proof cases
2.425 +      assume "l = Leaf" thus ?thesis using equal Node.prems
2.426 +        by(auto simp: post_del_def invar.simps(2))
2.427 +    next
2.428 +      assume "l \<noteq> Leaf" thus ?thesis using equal
2.430 +    qed
2.431 +  qed
2.433 +
2.434 +declare invar_2Nodes[simp del]
2.435 +
2.436
2.437  subsection "Functional Correctness"
2.438
2.439 +
2.440  subsubsection "Proofs for insert"
2.441
2.442  lemma inorder_split: "inorder(split t) = inorder t"
2.443 @@ -103,28 +464,28 @@
2.444    "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
2.445  by(induction t) (auto simp: ins_list_simps inorder_split inorder_skew)
2.446
2.447 +
2.448  subsubsection "Proofs for delete"
2.449
2.451 +by(induction t)
2.453 +     split: tree.splits)
2.454 +
2.455  lemma del_maxD:
2.456 -  "\<lbrakk> del_max t = (t',x); t \<noteq> Leaf \<rbrakk> \<Longrightarrow> inorder t' @ [x] = inorder t"
2.457 +  "\<lbrakk> del_max t = (t',x); t \<noteq> Leaf; invar t \<rbrakk> \<Longrightarrow> inorder t' @ [x] = inorder t"
2.458  by(induction t arbitrary: t' rule: del_max.induct)
2.459 -  (auto simp: sorted_lems split: prod.splits)
2.460 -
2.461 -lemma inorder_adjust: "t \<noteq> Leaf \<Longrightarrow> inorder(adjust t) = inorder t"
2.462 -by(induction t)
2.463 -  (auto simp: adjust_def inorder_skew inorder_split split: tree.splits)
2.465
2.466  lemma inorder_delete:
2.467 -  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
2.468 +  "invar t \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
2.469  by(induction t)
2.470 -  (auto simp: del_list_simps inorder_adjust del_maxD split: prod.splits)
2.471 -
2.473 +              post_del_max post_delete del_maxD split: prod.splits)
2.474
2.475 -subsection "Overall correctness"
2.476 -
2.477 -interpretation Set_by_Ordered
2.478 +interpretation I: Set_by_Ordered
2.479  where empty = Leaf and isin = isin and insert = insert and delete = delete
2.480 -and inorder = inorder and inv = "\<lambda>_. True"
2.481 +and inorder = inorder and inv = invar
2.482  proof (standard, goal_cases)
2.483    case 1 show ?case by simp
2.484  next
2.485 @@ -133,6 +494,12 @@
2.486    case 3 thus ?case by(simp add: inorder_insert)
2.487  next
2.488    case 4 thus ?case by(simp add: inorder_delete)
2.489 -qed auto
2.490 +next
2.491 +  case 5 thus ?case by(simp)
2.492 +next
2.493 +  case 6 thus ?case by(simp add: invar_insert)
2.494 +next
2.495 +  case 7 thus ?case using post_delete by(auto simp: post_del_def)
2.496 +qed
2.497
2.498  end

     3.1 --- a/src/HOL/Data_Structures/document/root.bib	Tue Mar 01 22:49:33 2016 +0100
3.2 +++ b/src/HOL/Data_Structures/document/root.bib	Wed Mar 02 10:01:31 2016 +0100
3.3 @@ -1,3 +1,8 @@
3.5 +title={Balanced search trees made simple},pages={60--71},year=1993,
3.6 +booktitle={Algorithms and Data Structures (WADS '93)},
3.7 +series={LNCS},volume={709},publisher={Springer}}
3.8 +
3.9  @article{Hinze-bro12,author={Ralf Hinze},
3.10  title={Purely Functional 1-2 Brother Trees},
3.11  journal={J. Functional Programming},
3.12 @@ -14,6 +19,10 @@
3.13  @misc{Kahrs-html,author={Stefan Kahrs},title={Red Black Trees},
3.14  note={\url{http://www.cs.ukc.ac.uk/people/staff/smk/redblack/rb.html}}}
3.15
3.16 +@unpublished{Nipkow16,author={Tobias Nipkow},
3.17 +title={Automatic Functional Correctness Proofs for Functional Search Trees},
3.18 +year=2016,month=feb,note={\url{http://www.in.tum.de/~nipkow/pubs/trees.html}}}
3.19 +
3.20  @book{Okasaki,author={Chris Okasaki},title="Purely Functional Data Structures",
3.21  publisher="Cambridge University Press",year=1998}
3.22
3.23 @@ -25,6 +34,11 @@
3.24  title={1-2 Brother Trees or {AVL} Trees Revisited},journal={Comput. J.},
3.25  volume=23,number=3,pages={248--255},year=1980}
3.26
3.27 +@inproceedings{Ragde14,author={Prabhakar Ragde},
3.28 +title={Simple Balanced Binary Search Trees},pages={78--87},year=2014,
3.29 +booktitle={Trends in Functional Programming in Education},
3.30 +series={EPTCS},volume=170,editor={Caldwell and H\"olzenspies and Achten}}
3.31 +
3.33  title={Balanced Trees with Removals: An Exercise in Rewriting and Proof},
3.34  journal={Sci. Comput. Program.},volume=18,number=2,pages={181--204},year=1992}

     4.1 --- a/src/HOL/Data_Structures/document/root.tex	Tue Mar 01 22:49:33 2016 +0100
4.2 +++ b/src/HOL/Data_Structures/document/root.tex	Wed Mar 02 10:01:31 2016 +0100
4.3 @@ -27,6 +27,8 @@
4.4  A collection of verified functional data structures. The emphasis is on
4.5  conciseness of algorithms and succinctness of proofs, more in the style
4.6  of a textbook than a library of efficient algorithms.
4.7 +
4.8 +For more details see \cite{Nipkow16}.
4.9  \end{abstract}
4.10
4.11  \setcounter{tocdepth}{1}
4.12 @@ -52,6 +54,11 @@
4.13  They were invented by Ottmann and Six~\cite{OttmannS76,OttmannW-CJ80}.
4.14  The functional version is due to Hinze~\cite{Hinze-bro12}.
4.15
4.16 +\paragraph{AA trees}
`