author  nipkow 
Thu, 24 Sep 2020 00:29:51 +0200  
changeset 72513  415220b59d37 
parent 70944  3fb16bed5d6c 
child 72773  8eabaf951e6b 
permissions  rwrr 
62706  1 
(* Author: Tobias Nipkow *) 
2 

3 
section \<open>Leftist Heap\<close> 

4 

5 
theory Leftist_Heap 

66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

6 
imports 
70636  7 
"HOLLibrary.Pattern_Aliases" 
66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

8 
Tree2 
68493  9 
Priority_Queue_Specs 
66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

10 
Complex_Main 
62706  11 
begin 
12 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

13 
fun mset_tree :: "('a*'b) tree \<Rightarrow> 'a multiset" where 
64969  14 
"mset_tree Leaf = {#}"  
70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

15 
"mset_tree (Node l (a, _) r) = {#a#} + mset_tree l + mset_tree r" 
64969  16 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

17 
type_synonym 'a lheap = "('a*nat)tree" 
62706  18 

19 
fun rank :: "'a lheap \<Rightarrow> nat" where 

20 
"rank Leaf = 0"  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

21 
"rank (Node _ _ r) = rank r + 1" 
62706  22 

23 
fun rk :: "'a lheap \<Rightarrow> nat" where 

24 
"rk Leaf = 0"  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

25 
"rk (Node _ (_, n) _) = n" 
62706  26 

67406  27 
text\<open>The invariants:\<close> 
64969  28 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

29 
fun (in linorder) heap :: "('a*'b) tree \<Rightarrow> bool" where 
64969  30 
"heap Leaf = True"  
70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

31 
"heap (Node l (m, _) r) = 
70771  32 
(heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))" 
62706  33 

64973  34 
fun ltree :: "'a lheap \<Rightarrow> bool" where 
35 
"ltree Leaf = True"  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

36 
"ltree (Node l (a, n) r) = 
64973  37 
(n = rank r + 1 \<and> rank l \<ge> rank r \<and> ltree l & ltree r)" 
62706  38 

70771  39 
definition empty :: "'a lheap" where 
40 
"empty = Leaf" 

41 

62706  42 
definition node :: "'a lheap \<Rightarrow> 'a \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where 
43 
"node l a r = 

44 
(let rl = rk l; rr = rk r 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

45 
in if rl \<ge> rr then Node l (a,rr+1) r else Node r (a,rl+1) l)" 
62706  46 

47 
fun get_min :: "'a lheap \<Rightarrow> 'a" where 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

48 
"get_min(Node l (a, n) r) = a" 
62706  49 

66502  50 
text \<open>For function \<open>merge\<close>:\<close> 
51 
unbundle pattern_aliases 

66492  52 

66502  53 
fun merge :: "'a::ord lheap \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where 
70771  54 
"merge Leaf t = t"  
55 
"merge t Leaf = t"  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

56 
"merge (Node l1 (a1, n1) r1 =: t1) (Node l2 (a2, n2) r2 =: t2) = 
66492  57 
(if a1 \<le> a2 then node l1 a1 (merge r1 t2) 
68600  58 
else node l2 a2 (merge t1 r2))" 
62706  59 

70771  60 
text \<open>Termination of @{const merge}: by sum or lexicographic product of the sizes 
61 
of the two arguments. Isabelle uses a lexicographic product.\<close> 

62 

64976  63 
lemma merge_code: "merge t1 t2 = (case (t1,t2) of 
62706  64 
(Leaf, _) \<Rightarrow> t2  
65 
(_, Leaf) \<Rightarrow> t1  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

66 
(Node l1 (a1, n1) r1, Node l2 (a2, n2) r2) \<Rightarrow> 
68600  67 
if a1 \<le> a2 then node l1 a1 (merge r1 t2) else node l2 a2 (merge t1 r2))" 
64976  68 
by(induction t1 t2 rule: merge.induct) (simp_all split: tree.split) 
62706  69 

66524  70 
hide_const (open) insert 
71 

62706  72 
definition insert :: "'a::ord \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where 
70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

73 
"insert x t = merge (Node Leaf (x,1) Leaf) t" 
62706  74 

68021  75 
fun del_min :: "'a::ord lheap \<Rightarrow> 'a lheap" where 
76 
"del_min Leaf = Leaf"  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

77 
"del_min (Node l _ r) = merge l r" 
62706  78 

79 

80 
subsection "Lemmas" 

81 

66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

82 
lemma mset_tree_empty: "mset_tree t = {#} \<longleftrightarrow> t = Leaf" 
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

83 
by(cases t) auto 
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

84 

64973  85 
lemma rk_eq_rank[simp]: "ltree t \<Longrightarrow> rk t = rank t" 
62706  86 
by(cases t) auto 
87 

64973  88 
lemma ltree_node: "ltree (node l a r) \<longleftrightarrow> ltree l \<and> ltree r" 
62706  89 
by(auto simp add: node_def) 
90 

64969  91 
lemma heap_node: "heap (node l a r) \<longleftrightarrow> 
70771  92 
heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. a \<le> x)" 
64969  93 
by(auto simp add: node_def) 
94 

70771  95 
lemma set_tree_mset: "set_tree t = set_mset(mset_tree t)" 
96 
by(induction t) auto 

62706  97 

98 
subsection "Functional Correctness" 

99 

72513  100 
lemma mset_merge: "mset_tree (merge t1 t2) = mset_tree t1 + mset_tree t2" 
101 
by (induction t1 t2 rule: merge.induct) (auto simp add: node_def ac_simps) 

62706  102 

64969  103 
lemma mset_insert: "mset_tree (insert x t) = mset_tree t + {#x#}" 
64976  104 
by (auto simp add: insert_def mset_merge) 
62706  105 

72513  106 
lemma get_min: "\<lbrakk> heap t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow> get_min t = Min(set_tree t)" 
107 
by (cases t) (auto simp add: eq_Min_iff) 

64969  108 

72513  109 
lemma mset_del_min: "mset_tree (del_min t) = mset_tree t  {# get_min t #}" 
110 
by (cases t) (auto simp: mset_merge) 

62706  111 

64976  112 
lemma ltree_merge: "\<lbrakk> ltree l; ltree r \<rbrakk> \<Longrightarrow> ltree (merge l r)" 
72513  113 
by(induction l r rule: merge.induct)(auto simp: ltree_node) 
62706  114 

64976  115 
lemma heap_merge: "\<lbrakk> heap l; heap r \<rbrakk> \<Longrightarrow> heap (merge l r)" 
116 
proof(induction l r rule: merge.induct) 

70771  117 
case 3 thus ?case by(auto simp: heap_node mset_merge ball_Un set_tree_mset) 
64969  118 
qed simp_all 
119 

64973  120 
lemma ltree_insert: "ltree t \<Longrightarrow> ltree(insert x t)" 
64976  121 
by(simp add: insert_def ltree_merge del: merge.simps split: tree.split) 
62706  122 

64969  123 
lemma heap_insert: "heap t \<Longrightarrow> heap(insert x t)" 
64976  124 
by(simp add: insert_def heap_merge del: merge.simps split: tree.split) 
64969  125 

68021  126 
lemma ltree_del_min: "ltree t \<Longrightarrow> ltree(del_min t)" 
64976  127 
by(cases t)(auto simp add: ltree_merge simp del: merge.simps) 
62706  128 

68021  129 
lemma heap_del_min: "heap t \<Longrightarrow> heap(del_min t)" 
64976  130 
by(cases t)(auto simp add: heap_merge simp del: merge.simps) 
64969  131 

66564  132 
text \<open>Last step of functional correctness proof: combine all the above lemmas 
133 
to show that leftist heaps satisfy the specification of priority queues with merge.\<close> 

62706  134 

66564  135 
interpretation lheap: Priority_Queue_Merge 
72513  136 
where empty = empty and is_empty = "\<lambda>t. t = Leaf" 
68021  137 
and insert = insert and del_min = del_min 
66564  138 
and get_min = get_min and merge = merge 
72513  139 
and invar = "\<lambda>t. heap t \<and> ltree t" and mset = mset_tree 
62706  140 
proof(standard, goal_cases) 
70771  141 
case 1 show ?case by (simp add: empty_def) 
62706  142 
next 
64975  143 
case (2 q) show ?case by (cases q) auto 
62706  144 
next 
64975  145 
case 3 show ?case by(rule mset_insert) 
146 
next 

68021  147 
case 4 show ?case by(rule mset_del_min) 
62706  148 
next 
70771  149 
case 5 thus ?case by(simp add: get_min mset_tree_empty set_tree_mset) 
66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

150 
next 
70771  151 
case 6 thus ?case by(simp add: empty_def) 
62706  152 
next 
66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

153 
case 7 thus ?case by(simp add: heap_insert ltree_insert) 
64969  154 
next 
68021  155 
case 8 thus ?case by(simp add: heap_del_min ltree_del_min) 
66564  156 
next 
157 
case 9 thus ?case by (simp add: mset_merge) 

158 
next 

159 
case 10 thus ?case by (simp add: heap_merge ltree_merge) 

62706  160 
qed 
161 

162 

163 
subsection "Complexity" 

164 

64973  165 
lemma pow2_rank_size1: "ltree t \<Longrightarrow> 2 ^ rank t \<le> size1 t" 
70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

166 
proof(induction t rule: tree2_induct) 
62706  167 
case Leaf show ?case by simp 
168 
next 

68414  169 
case (Node l a n r) 
62706  170 
hence "rank r \<le> rank l" by simp 
171 
hence *: "(2::nat) ^ rank r \<le> 2 ^ rank l" by simp 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

172 
have "(2::nat) ^ rank \<langle>l, (a, n), r\<rangle> = 2 ^ rank r + 2 ^ rank r" 
62706  173 
by(simp add: mult_2) 
174 
also have "\<dots> \<le> size1 l + size1 r" 

175 
using Node * by (simp del: power_increasing_iff) 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

176 
also have "\<dots> = size1 \<langle>l, (a, n), r\<rangle>" by simp 
62706  177 
finally show ?case . 
178 
qed 

179 

66492  180 
text\<open>Explicit termination argument: sum of sizes\<close> 
181 

66502  182 
fun t_merge :: "'a::ord lheap \<Rightarrow> 'a lheap \<Rightarrow> nat" where 
70771  183 
"t_merge Leaf t = 1"  
184 
"t_merge t Leaf = 1"  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

185 
"t_merge (Node l1 (a1, n1) r1 =: t1) (Node l2 (a2, n2) r2 =: t2) = 
66492  186 
(if a1 \<le> a2 then 1 + t_merge r1 t2 
68600  187 
else 1 + t_merge t1 r2)" 
62706  188 

189 
definition t_insert :: "'a::ord \<Rightarrow> 'a lheap \<Rightarrow> nat" where 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

190 
"t_insert x t = t_merge (Node Leaf (x, 1) Leaf) t" 
62706  191 

68021  192 
fun t_del_min :: "'a::ord lheap \<Rightarrow> nat" where 
193 
"t_del_min Leaf = 1"  

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

194 
"t_del_min (Node l _ r) = t_merge l r" 
62706  195 

64976  196 
lemma t_merge_rank: "t_merge l r \<le> rank l + rank r + 1" 
197 
proof(induction l r rule: merge.induct) 

68600  198 
case 3 thus ?case by(simp) 
62706  199 
qed simp_all 
200 

64976  201 
corollary t_merge_log: assumes "ltree l" "ltree r" 
202 
shows "t_merge l r \<le> log 2 (size1 l) + log 2 (size1 r) + 1" 

62706  203 
using le_log2_of_power[OF pow2_rank_size1[OF assms(1)]] 
64976  204 
le_log2_of_power[OF pow2_rank_size1[OF assms(2)]] t_merge_rank[of l r] 
62706  205 
by linarith 
206 

64973  207 
corollary t_insert_log: "ltree t \<Longrightarrow> t_insert x t \<le> log 2 (size1 t) + 2" 
70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

208 
using t_merge_log[of "Node Leaf (x, 1) Leaf" t] 
62706  209 
by(simp add: t_insert_def split: tree.split) 
210 

66492  211 
(* FIXME mv ? *) 
62706  212 
lemma ld_ld_1_less: 
66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

213 
assumes "x > 0" "y > 0" shows "log 2 x + log 2 y + 1 < 2 * log 2 (x+y)" 
62706  214 
proof  
66422
8194ed7cf2cb
separate file for priority queue interface; extended Leftist_Heap.
nipkow
parents:
64977
diff
changeset

215 
have "2 powr (log 2 x + log 2 y + 1) = 2*x*y" 
64977  216 
using assms by(simp add: powr_add) 
217 
also have "\<dots> < (x+y)^2" using assms 

62706  218 
by(simp add: numeral_eq_Suc algebra_simps add_pos_pos) 
64977  219 
also have "\<dots> = 2 powr (2 * log 2 (x+y))" 
66492  220 
using assms by(simp add: powr_add log_powr[symmetric]) 
64977  221 
finally show ?thesis by simp 
62706  222 
qed 
223 

68021  224 
corollary t_del_min_log: assumes "ltree t" 
225 
shows "t_del_min t \<le> 2 * log 2 (size1 t) + 1" 

70944
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
70771
diff
changeset

226 
proof(cases t rule: tree2_cases) 
62706  227 
case Leaf thus ?thesis using assms by simp 
228 
next 

68414  229 
case [simp]: (Node t1 _ _ t2) 
68021  230 
have "t_del_min t = t_merge t1 t2" by simp 
62706  231 
also have "\<dots> \<le> log 2 (size1 t1) + log 2 (size1 t2) + 1" 
64976  232 
using \<open>ltree t\<close> by (auto simp: t_merge_log simp del: t_merge.simps) 
62706  233 
also have "\<dots> \<le> 2 * log 2 (size1 t) + 1" 
234 
using ld_ld_1_less[of "size1 t1" "size1 t2"] by (simp) 

235 
finally show ?thesis . 

236 
qed 

237 

238 
end 