added splay trees
authornipkow
Fri Oct 30 20:01:05 2015 +0100 (2015-10-30)
changeset 6152587244a9cfe40
parent 61524 f2e51e704a96
child 61530 aa1ece0bce62
added splay trees
src/HOL/Data_Structures/Splay_Map.thy
src/HOL/Data_Structures/Splay_Set.thy
src/HOL/Data_Structures/document/root.bib
src/HOL/Data_Structures/document/root.tex
src/HOL/ROOT
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Data_Structures/Splay_Map.thy	Fri Oct 30 20:01:05 2015 +0100
     1.3 @@ -0,0 +1,180 @@
     1.4 +(* Author: Tobias Nipkow *)
     1.5 +
     1.6 +section "Splay Tree Implementation of Maps"
     1.7 +
     1.8 +theory Splay_Map
     1.9 +imports
    1.10 +  Splay_Set
    1.11 +  Map_by_Ordered
    1.12 +begin
    1.13 +
    1.14 +function splay :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
    1.15 +"splay x Leaf = Leaf" |
    1.16 +"x = fst a \<Longrightarrow> splay x (Node t1 a t2) = Node t1 a t2" |
    1.17 +"x = fst a \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node (Node t1 a t2) b t3) = Node t1 a (Node t2 b t3)" |
    1.18 +"x < fst a \<Longrightarrow> splay x (Node Leaf a t) = Node Leaf a t" |
    1.19 +"x < fst a \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node (Node Leaf a t1) b t2) = Node Leaf a (Node t1 b t2)" |
    1.20 +"x < fst a \<Longrightarrow> x < fst b \<Longrightarrow> t1 \<noteq> Leaf \<Longrightarrow>
    1.21 + splay x (Node (Node t1 a t2) b t3) =
    1.22 + (case splay x t1 of Node t11 y t12 \<Rightarrow> Node t11 y (Node t12 a (Node t2 b t3)))" |
    1.23 +"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node (Node t1 a Leaf) b t2) = Node t1 a (Node Leaf b t2)" |
    1.24 +"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow> t2 \<noteq> Leaf \<Longrightarrow>
    1.25 + splay x (Node (Node t1 a t2) b t3) =
    1.26 + (case splay x t2 of Node t21 y t22 \<Rightarrow> Node (Node t1 a t21) y (Node t22 b t3))" |
    1.27 +"fst a < x \<Longrightarrow> x = fst b \<Longrightarrow> splay x (Node t1 a (Node t2 b t3)) = Node (Node t1 a t2) b t3" |
    1.28 +"fst a < x \<Longrightarrow> splay x (Node t a Leaf) = Node t a Leaf" |
    1.29 +"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow>  t2 \<noteq> Leaf \<Longrightarrow>
    1.30 + splay x (Node t1 a (Node t2 b t3)) =
    1.31 + (case splay x t2 of Node t21 y t22 \<Rightarrow> Node (Node t1 a t21) y (Node t22 b t3))" |
    1.32 +"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node t1 a (Node Leaf b t2)) = Node (Node t1 a Leaf) b t2" |
    1.33 +"fst a < x \<Longrightarrow> fst b < x \<Longrightarrow> splay x (Node t1 a (Node t2 b Leaf)) = Node (Node t1 a t2) b Leaf" |
    1.34 +"fst a < x \<Longrightarrow> fst b < x \<Longrightarrow> t3 \<noteq> Leaf \<Longrightarrow>
    1.35 + splay x (Node t1 a (Node t2 b t3)) =
    1.36 + (case splay x t3 of Node t31 y t32 \<Rightarrow> Node (Node (Node t1 a t2) b t31) y t32)"
    1.37 +apply(atomize_elim)
    1.38 +apply(auto)
    1.39 +(* 1 subgoal *)
    1.40 +apply (subst (asm) neq_Leaf_iff)
    1.41 +apply(auto)
    1.42 +apply (metis tree.exhaust surj_pair less_linear)+
    1.43 +done
    1.44 +
    1.45 +termination splay
    1.46 +by lexicographic_order
    1.47 +
    1.48 +lemma splay_code: "splay x t = (case t of Leaf \<Rightarrow> Leaf |
    1.49 +  Node al a ar \<Rightarrow>
    1.50 +  (if x = fst a then t else
    1.51 +   if x < fst a then
    1.52 +     case al of
    1.53 +       Leaf \<Rightarrow> t |
    1.54 +       Node bl b br \<Rightarrow>
    1.55 +         (if x = fst b then Node bl b (Node br a ar) else
    1.56 +          if x < fst b then
    1.57 +            if bl = Leaf then Node bl b (Node br a ar)
    1.58 +            else case splay x bl of
    1.59 +                   Node bll y blr \<Rightarrow> Node bll y (Node blr b (Node br a ar))
    1.60 +          else
    1.61 +          if br = Leaf then Node bl b (Node br a ar)
    1.62 +          else case splay x br of
    1.63 +                 Node brl y brr \<Rightarrow> Node (Node bl b brl) y (Node brr a ar))
    1.64 +   else
    1.65 +   case ar of
    1.66 +     Leaf \<Rightarrow> t |
    1.67 +     Node bl b br \<Rightarrow>
    1.68 +       (if x = fst b then Node (Node al a bl) b br else
    1.69 +        if x < fst b then
    1.70 +          if bl = Leaf then Node (Node al a bl) b br
    1.71 +          else case splay x bl of
    1.72 +                 Node bll y blr \<Rightarrow> Node (Node al a bll) y (Node blr b br)
    1.73 +        else if br=Leaf then Node (Node al a bl) b br
    1.74 +             else case splay x br of
    1.75 +                    Node bll y blr \<Rightarrow> Node (Node (Node al a bl) b bll) y blr)))"
    1.76 +by(auto split: tree.split)
    1.77 +
    1.78 +definition lookup :: "('a*'b)tree \<Rightarrow> 'a::linorder \<Rightarrow> 'b option" where "lookup t x =
    1.79 +  (case splay x t of Leaf \<Rightarrow> None | Node _ (a,b) _ \<Rightarrow> if x=a then Some b else None)"
    1.80 +
    1.81 +hide_const (open) insert
    1.82 +
    1.83 +fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
    1.84 +"update x y t =  (if t = Leaf then Node Leaf (x,y) Leaf
    1.85 +  else case splay x t of
    1.86 +    Node l a r \<Rightarrow> if x = fst a then Node l (x,y) r
    1.87 +      else if x < fst a then Node l (x,y) (Node Leaf a r) else Node (Node l a Leaf) (x,y) r)"
    1.88 +
    1.89 +definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
    1.90 +"delete x t = (if t = Leaf then Leaf
    1.91 +  else case splay x t of Node l a r \<Rightarrow>
    1.92 +    if x = fst a
    1.93 +    then if l = Leaf then r else case splay_max l of Node l' m r' \<Rightarrow> Node l' m r
    1.94 +    else Node l a r)"
    1.95 +
    1.96 +
    1.97 +subsection "Functional Correctness Proofs"
    1.98 +
    1.99 +lemma splay_Leaf_iff: "(splay x t = Leaf) = (t = Leaf)"
   1.100 +by(induction x t rule: splay.induct) (auto split: tree.splits)
   1.101 +
   1.102 +
   1.103 +subsubsection "Proofs for lookup"
   1.104 +
   1.105 +lemma splay_map_of_inorder:
   1.106 +  "splay x t = Node l a r \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
   1.107 +   map_of (inorder t) x = (if x = fst a then Some(snd a) else None)"
   1.108 +by(induction x t arbitrary: l a r rule: splay.induct)
   1.109 +  (auto simp: map_of_simps splay_Leaf_iff split: tree.splits)
   1.110 +
   1.111 +lemma lookup_eq:
   1.112 +  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
   1.113 +by(auto simp: lookup_def splay_Leaf_iff splay_map_of_inorder split: tree.split)
   1.114 +
   1.115 +
   1.116 +subsubsection "Proofs for update"
   1.117 +
   1.118 +lemma inorder_splay: "inorder(splay x t) = inorder t"
   1.119 +by(induction x t rule: splay.induct)
   1.120 +  (auto simp: neq_Leaf_iff split: tree.split)
   1.121 +
   1.122 +lemma sorted_splay:
   1.123 +  "sorted1(inorder t) \<Longrightarrow> splay x t = Node l a r \<Longrightarrow>
   1.124 +  sorted(map fst (inorder l) @ x # map fst (inorder r))"
   1.125 +unfolding inorder_splay[of x t, symmetric]
   1.126 +by(induction x t arbitrary: l a r rule: splay.induct)
   1.127 +  (auto simp: sorted_lems sorted_Cons_le sorted_snoc_le splay_Leaf_iff split: tree.splits)
   1.128 +
   1.129 +(* more upd_list lemmas; unify with basic set? *)
   1.130 +
   1.131 +lemma upd_list_Cons:
   1.132 +  "sorted1 ((x,y) # xs) \<Longrightarrow> upd_list x y xs = (x,y) # xs"
   1.133 +by (induction xs) auto
   1.134 +
   1.135 +lemma upd_list_snoc:
   1.136 +  "sorted1 (xs @ [(x,y)]) \<Longrightarrow> upd_list x y xs = xs @ [(x,y)]"
   1.137 +by(induction xs) (auto simp add: sorted_mid_iff2)
   1.138 +
   1.139 +lemma inorder_update:
   1.140 +  "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
   1.141 +using inorder_splay[of x t, symmetric] sorted_splay[of t x]
   1.142 +by(auto simp: upd_list_simps upd_list_Cons upd_list_snoc neq_Leaf_iff split: tree.split)
   1.143 +
   1.144 +
   1.145 +subsubsection "Proofs for delete"
   1.146 +
   1.147 +(* more del_simp lemmas; unify with basic set? *)
   1.148 +
   1.149 +lemma del_list_notin_Cons: "sorted (x # map fst xs) \<Longrightarrow> del_list x xs = xs"
   1.150 +by(induction xs)(auto simp: sorted_Cons_iff)
   1.151 +
   1.152 +lemma del_list_sorted_app:
   1.153 +  "sorted(map fst xs @ [x]) \<Longrightarrow> del_list x (xs @ ys) = xs @ del_list x ys"
   1.154 +by (induction xs) (auto simp: sorted_mid_iff2)
   1.155 +
   1.156 +lemma inorder_splay_maxD:
   1.157 +  "splay_max t = Node l a r \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
   1.158 +  inorder l @ [a] = inorder t \<and> r = Leaf"
   1.159 +by(induction t arbitrary: l a r rule: splay_max.induct)
   1.160 +  (auto simp: sorted_lems splay_max_Leaf_iff split: tree.splits if_splits)
   1.161 +
   1.162 +lemma inorder_delete:
   1.163 +  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   1.164 +using inorder_splay[of x t, symmetric] sorted_splay[of t x]
   1.165 +by (auto simp: del_list_simps del_list_sorted_app delete_def
   1.166 +  del_list_notin_Cons inorder_splay_maxD splay_Leaf_iff splay_max_Leaf_iff
   1.167 +  split: tree.splits)
   1.168 +
   1.169 +
   1.170 +subsubsection "Overall Correctness"
   1.171 +
   1.172 +interpretation Map_by_Ordered
   1.173 +where empty = Leaf and lookup = lookup and update = update
   1.174 +and delete = delete and inorder = inorder and wf = "\<lambda>_. True"
   1.175 +proof (standard, goal_cases)
   1.176 +  case 2 thus ?case by(simp add: lookup_eq)
   1.177 +next
   1.178 +  case 3 thus ?case by(simp add: inorder_update del: update.simps)
   1.179 +next
   1.180 +  case 4 thus ?case by(simp add: inorder_delete)
   1.181 +qed auto
   1.182 +
   1.183 +end
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Data_Structures/Splay_Set.thy	Fri Oct 30 20:01:05 2015 +0100
     2.3 @@ -0,0 +1,209 @@
     2.4 +(*
     2.5 +Author: Tobias Nipkow
     2.6 +Function defs follows AFP entry Splay_Tree, proofs are new.
     2.7 +*)
     2.8 +
     2.9 +section "Splay Tree Implementation of Sets"
    2.10 +
    2.11 +theory Splay_Set
    2.12 +imports
    2.13 +  "~~/src/HOL/Library/Tree"
    2.14 +  Set_by_Ordered
    2.15 +begin
    2.16 +
    2.17 +function splay :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
    2.18 +"splay a Leaf = Leaf" |
    2.19 +"splay a (Node t1 a t2) = Node t1 a t2" |
    2.20 +"a<b \<Longrightarrow> splay a (Node (Node t1 a t2) b t3) = Node t1 a (Node t2 b t3)" |
    2.21 +"x<a \<Longrightarrow> splay x (Node Leaf a t) = Node Leaf a t" |
    2.22 +"x<a \<Longrightarrow> x<b \<Longrightarrow> splay x (Node (Node Leaf a t1) b t2) = Node Leaf a (Node t1 b t2)" |
    2.23 +"x<a \<Longrightarrow> x<b \<Longrightarrow> t1 \<noteq> Leaf \<Longrightarrow>
    2.24 + splay x (Node (Node t1 a t2) b t3) =
    2.25 + (case splay x t1 of Node t11 y t12 \<Rightarrow> Node t11 y (Node t12 a (Node t2 b t3)))" |
    2.26 +"a<x \<Longrightarrow> x<b \<Longrightarrow> splay x (Node (Node t1 a Leaf) b t2) = Node t1 a (Node Leaf b t2)" |
    2.27 +"a<x \<Longrightarrow> x<b \<Longrightarrow> t2 \<noteq> Leaf \<Longrightarrow>
    2.28 + splay x (Node (Node t1 a t2) b t3) =
    2.29 + (case splay x t2 of Node t21 y t22 \<Rightarrow> Node (Node t1 a t21) y (Node t22 b t3))" |
    2.30 +"a<b \<Longrightarrow> splay b (Node t1 a (Node t2 b t3)) = Node (Node t1 a t2) b t3" |
    2.31 +"a<x \<Longrightarrow> splay x (Node t a Leaf) = Node t a Leaf" |
    2.32 +"a<x \<Longrightarrow> x<b \<Longrightarrow>  t2 \<noteq> Leaf \<Longrightarrow>
    2.33 + splay x (Node t1 a (Node t2 b t3)) =
    2.34 + (case splay x t2 of Node t21 y t22 \<Rightarrow> Node (Node t1 a t21) y (Node t22 b t3))" |
    2.35 +"a<x \<Longrightarrow> x<b \<Longrightarrow> splay x (Node t1 a (Node Leaf b t2)) = Node (Node t1 a Leaf) b t2" |
    2.36 +"a<x \<Longrightarrow> b<x \<Longrightarrow> splay x (Node t1 a (Node t2 b Leaf)) = Node (Node t1 a t2) b Leaf" |
    2.37 +"a<x \<Longrightarrow> b<x \<Longrightarrow>  t3 \<noteq> Leaf \<Longrightarrow>
    2.38 + splay x (Node t1 a (Node t2 b t3)) =
    2.39 + (case splay x t3 of Node t31 y t32 \<Rightarrow> Node (Node (Node t1 a t2) b t31) y t32)"
    2.40 +apply(atomize_elim)
    2.41 +apply(auto)
    2.42 +(* 1 subgoal *)
    2.43 +apply (subst (asm) neq_Leaf_iff)
    2.44 +apply(auto)
    2.45 +apply (metis tree.exhaust less_linear)+
    2.46 +done
    2.47 +
    2.48 +termination splay
    2.49 +by lexicographic_order
    2.50 +
    2.51 +lemma splay_code: "splay x t = (case t of Leaf \<Rightarrow> Leaf |
    2.52 +  Node al a ar \<Rightarrow>
    2.53 +  (if x=a then t else
    2.54 +   if x < a then
    2.55 +     case al of
    2.56 +       Leaf \<Rightarrow> t |
    2.57 +       Node bl b br \<Rightarrow>
    2.58 +         (if x=b then Node bl b (Node br a ar) else
    2.59 +          if x < b then
    2.60 +            if bl = Leaf then Node bl b (Node br a ar)
    2.61 +            else case splay x bl of
    2.62 +                   Node bll y blr \<Rightarrow> Node bll y (Node blr b (Node br a ar))
    2.63 +          else
    2.64 +          if br = Leaf then Node bl b (Node br a ar)
    2.65 +          else case splay x br of
    2.66 +                 Node brl y brr \<Rightarrow> Node (Node bl b brl) y (Node brr a ar))
    2.67 +   else
    2.68 +   case ar of
    2.69 +     Leaf \<Rightarrow> t |
    2.70 +     Node bl b br \<Rightarrow>
    2.71 +       (if x=b then Node (Node al a bl) b br else
    2.72 +        if x < b then
    2.73 +          if bl = Leaf then Node (Node al a bl) b br
    2.74 +          else case splay x bl of
    2.75 +                 Node bll y blr \<Rightarrow> Node (Node al a bll) y (Node blr b br)
    2.76 +        else if br=Leaf then Node (Node al a bl) b br
    2.77 +             else case splay x br of
    2.78 +                    Node bll y blr \<Rightarrow> Node (Node (Node al a bl) b bll) y blr)))"
    2.79 +by(auto split: tree.split)
    2.80 +
    2.81 +definition is_root :: "'a \<Rightarrow> 'a tree \<Rightarrow> bool" where
    2.82 +"is_root a t = (case t of Leaf \<Rightarrow> False | Node _ x _ \<Rightarrow> x = a)"
    2.83 +
    2.84 +definition "isin t x = is_root x (splay x t)"
    2.85 +
    2.86 +hide_const (open) insert
    2.87 +
    2.88 +fun insert :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
    2.89 +"insert x t =  (if t = Leaf then Node Leaf x Leaf
    2.90 +  else case splay x t of
    2.91 +    Node l a r \<Rightarrow> if x = a then Node l a r
    2.92 +      else if x < a then Node l x (Node Leaf a r) else Node (Node l a Leaf) x r)"
    2.93 +
    2.94 +
    2.95 +fun splay_max :: "'a tree \<Rightarrow> 'a tree" where
    2.96 +"splay_max Leaf = Leaf" |
    2.97 +"splay_max (Node l b Leaf) = Node l b Leaf" |
    2.98 +"splay_max (Node l b (Node rl c rr)) =
    2.99 +  (if rr = Leaf then Node (Node l b rl) c Leaf
   2.100 +   else case splay_max rr of
   2.101 +     Node rrl m rrr \<Rightarrow> Node (Node (Node l b rl) c rrl) m rrr)"
   2.102 +
   2.103 +
   2.104 +definition delete :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
   2.105 +"delete x t = (if t = Leaf then Leaf
   2.106 +  else case splay x t of Node l a r \<Rightarrow>
   2.107 +    if x = a
   2.108 +    then if l = Leaf then r else case splay_max l of Node l' m r' \<Rightarrow> Node l' m r
   2.109 +    else Node l a r)"
   2.110 +
   2.111 +
   2.112 +subsection "Functional Correctness Proofs"
   2.113 +
   2.114 +lemma splay_Leaf_iff: "(splay a t = Leaf) = (t = Leaf)"
   2.115 +by(induction a t rule: splay.induct) (auto split: tree.splits)
   2.116 +
   2.117 +lemma splay_max_Leaf_iff: "(splay_max t = Leaf) = (t = Leaf)"
   2.118 +by(induction t rule: splay_max.induct)(auto split: tree.splits)
   2.119 +
   2.120 +
   2.121 +subsubsection "Proofs for isin"
   2.122 +
   2.123 +lemma
   2.124 +  "splay x t = Node l a r \<Longrightarrow> sorted(inorder t) \<Longrightarrow>
   2.125 +  x \<in> elems (inorder t) \<longleftrightarrow> x=a"
   2.126 +by(induction x t arbitrary: l a r rule: splay.induct)
   2.127 +  (auto simp: elems_simps1 splay_Leaf_iff ball_Un split: tree.splits)
   2.128 +
   2.129 +lemma splay_elemsD:
   2.130 +  "splay x t = Node l a r \<Longrightarrow> sorted(inorder t) \<Longrightarrow>
   2.131 +  x \<in> elems (inorder t) \<longleftrightarrow> x=a"
   2.132 +by(induction x t arbitrary: l a r rule: splay.induct)
   2.133 +  (auto simp: elems_simps2 splay_Leaf_iff split: tree.splits)
   2.134 +
   2.135 +lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
   2.136 +by (auto simp: isin_def is_root_def splay_elemsD splay_Leaf_iff split: tree.splits)
   2.137 +
   2.138 +
   2.139 +subsubsection "Proofs for insert"
   2.140 +
   2.141 +(* more sorted lemmas; unify with basic set? *)
   2.142 +
   2.143 +lemma sorted_snoc_le:
   2.144 +  "ASSUMPTION(sorted(xs @ [x])) \<Longrightarrow> x \<le> y \<Longrightarrow> sorted (xs @ [y])"
   2.145 +by (auto simp add: Sorted_Less.sorted_snoc_iff ASSUMPTION_def)
   2.146 +
   2.147 +lemma sorted_Cons_le:
   2.148 +  "ASSUMPTION(sorted(x # xs)) \<Longrightarrow> y \<le> x \<Longrightarrow> sorted (y # xs)"
   2.149 +by (auto simp add: Sorted_Less.sorted_Cons_iff ASSUMPTION_def)
   2.150 +
   2.151 +lemma inorder_splay: "inorder(splay x t) = inorder t"
   2.152 +by(induction x t rule: splay.induct)
   2.153 +  (auto simp: neq_Leaf_iff split: tree.split)
   2.154 +
   2.155 +lemma sorted_splay:
   2.156 +  "sorted(inorder t) \<Longrightarrow> splay x t = Node l a r \<Longrightarrow>
   2.157 +  sorted(inorder l @ x # inorder r)"
   2.158 +unfolding inorder_splay[of x t, symmetric]
   2.159 +by(induction x t arbitrary: l a r rule: splay.induct)
   2.160 +  (auto simp: sorted_lems sorted_Cons_le sorted_snoc_le splay_Leaf_iff split: tree.splits)
   2.161 +
   2.162 +lemma ins_list_Cons: "sorted (x # xs) \<Longrightarrow> ins_list x xs = x # xs"
   2.163 +by (induction xs) auto
   2.164 +
   2.165 +lemma ins_list_snoc: "sorted (xs @ [x]) \<Longrightarrow> ins_list x xs = xs @ [x]"
   2.166 +by(induction xs) (auto simp add: sorted_mid_iff2)
   2.167 +
   2.168 +lemma inorder_insert:
   2.169 +  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
   2.170 +using inorder_splay[of x t, symmetric] sorted_splay[of t x]
   2.171 +by(auto simp: ins_list_simps ins_list_Cons ins_list_snoc neq_Leaf_iff split: tree.split)
   2.172 +
   2.173 +
   2.174 +subsubsection "Proofs for delete"
   2.175 +
   2.176 +(* more del_simp lemmas; unify with basic set? *)
   2.177 +
   2.178 +lemma del_list_notin_Cons: "sorted (x # xs) \<Longrightarrow> del_list x xs = xs"
   2.179 +by(induction xs)(auto simp: sorted_Cons_iff)
   2.180 +
   2.181 +lemma del_list_sorted_app:
   2.182 +  "sorted(xs @ [x]) \<Longrightarrow> del_list x (xs @ ys) = xs @ del_list x ys"
   2.183 +by (induction xs) (auto simp: sorted_mid_iff2)
   2.184 +
   2.185 +lemma inorder_splay_maxD:
   2.186 +  "splay_max t = Node l a r \<Longrightarrow> sorted(inorder t) \<Longrightarrow>
   2.187 +  inorder l @ [a] = inorder t \<and> r = Leaf"
   2.188 +by(induction t arbitrary: l a r rule: splay_max.induct)
   2.189 +  (auto simp: sorted_lems splay_max_Leaf_iff split: tree.splits if_splits)
   2.190 +
   2.191 +lemma inorder_delete:
   2.192 +  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   2.193 +using inorder_splay[of x t, symmetric] sorted_splay[of t x]
   2.194 +by (auto simp: del_list_simps del_list_sorted_app delete_def
   2.195 +  del_list_notin_Cons inorder_splay_maxD splay_Leaf_iff splay_max_Leaf_iff
   2.196 +  split: tree.splits)
   2.197 +
   2.198 +
   2.199 +subsubsection "Overall Correctness"
   2.200 +
   2.201 +interpretation Set_by_Ordered
   2.202 +where empty = Leaf and isin = isin and insert = insert
   2.203 +and delete = delete and inorder = inorder and wf = "\<lambda>_. True"
   2.204 +proof (standard, goal_cases)
   2.205 +  case 2 thus ?case by(simp add: isin_set)
   2.206 +next
   2.207 +  case 3 thus ?case by(simp add: inorder_insert del: insert.simps)
   2.208 +next
   2.209 +  case 4 thus ?case by(simp add: inorder_delete)
   2.210 +qed auto
   2.211 +
   2.212 +end
     3.1 --- a/src/HOL/Data_Structures/document/root.bib	Thu Oct 29 15:40:52 2015 +0100
     3.2 +++ b/src/HOL/Data_Structures/document/root.bib	Fri Oct 30 20:01:05 2015 +0100
     3.3 @@ -6,3 +6,10 @@
     3.4  
     3.5  @book{Okasaki,author={Chris Okasaki},title="Purely Functional Data Structures",
     3.6  publisher="Cambridge University Press",year=1998}
     3.7 +
     3.8 +@article{Schoenmakers-IPL93,author="Berry Schoenmakers",
     3.9 +title="A Systematic Analysis of Splaying",journal={Information Processing Letters},volume=45,pages={41-50},year=1993}
    3.10 +
    3.11 +@article{SleatorT-JACM85,author={Daniel D. Sleator and Robert E. Tarjan},
    3.12 +title={Self-adjusting Binary Search Trees},journal={J. ACM},
    3.13 +volume=32,number=3,pages={652-686},year=1985}
     4.1 --- a/src/HOL/Data_Structures/document/root.tex	Thu Oct 29 15:40:52 2015 +0100
     4.2 +++ b/src/HOL/Data_Structures/document/root.tex	Fri Oct 30 20:01:05 2015 +0100
     4.3 @@ -44,6 +44,10 @@
     4.4  \paragraph{2-3 trees}
     4.5  The function definitions are based on the teaching material by Franklyn Turbak.
     4.6  
     4.7 +\paragraph{Splay trees}
     4.8 +They were invented by Sleator and Tarjan \cite{SleatorT-JACM85}.
     4.9 +Our formalisation follows Schoenmakers \cite{Schoenmakers-IPL93}.
    4.10 +
    4.11  \bibliographystyle{abbrv}
    4.12  \bibliography{root}
    4.13  
     5.1 --- a/src/HOL/ROOT	Thu Oct 29 15:40:52 2015 +0100
     5.2 +++ b/src/HOL/ROOT	Fri Oct 30 20:01:05 2015 +0100
     5.3 @@ -174,12 +174,12 @@
     5.4    theories [document = false]
     5.5      "Less_False"
     5.6    theories
     5.7 -    Tree_Set
     5.8      Tree_Map
     5.9      AVL_Map
    5.10      RBT_Map
    5.11      Tree23_Map
    5.12      Tree234_Map
    5.13 +    Splay_Map
    5.14    document_files "root.tex" "root.bib"
    5.15  
    5.16  session "HOL-Import" in Import = HOL +