theory BTree_ImpSplit imports
BTree_ImpSet
BTree_Split
Imperative_Loops begin
section"Imperative split operations"
text"So far, we have only given a functional specification of a possible split. We will now provide imperative split functions that refine the functional specification. However, rather than tracing the execution of the abstract specification, the imperative versions are implemented using while-loops."
subsection"Linear split"
text"The linear split is the most simple split function for binary trees. It serves a good example on how to use while-loops in Imperative/HOL and how to prove Hoare-Triples about its application using loop invariants."
definition lin_split :: "('a::heap × 'b::{heap,linorder}) pfarray ==> 'b ==> nat Heap" where "lin_split ≡ λ (a,n) p. do {
i ← heap_WHILET (λi. if i<n then do { (_,s) ← Array.nth a i; return (s<p) } else return False) (λi. return (i+1)) 0;
text"To obtain an efficient B-Tree implementation, we prefer a binary split function. To explore the searching procedure and the resulting proof, we first implement the split on singleton arrays."
definition bin'_split :: "'b::{heap,linorder} array_list ==> 'b ==> nat Heap" where "bin'_split ≡ λ(a,n) p. do { (low',high') ← heap_WHILET (λ(low,high). return (low < high)) (λ(low,high). let mid = ((low + high) div 2) in do { s ← Array.nth a mid; if p < s then return (low, mid) else if p > s then return (mid+1, high) else return (mid,mid) }) (0::nat,n); return low' }"
thm sorted_wrt_nth_less
(* alternative: replace (\<forall>j<l. xs!j < p) by (l > 0 \<longrightarrow> xs!(l-1) < p)*) lemma bin'_split_rule: " sorted_less xs ==> < is_pfa c xs (a,n)> bin'_split (a,n) p <λl. is_pfa c xs (a,n) * ↑(l ≤ n ∧ (∀j<l. xs!j < p) ∧ (l<n ⟶ xs!l≥p)) >t" unfolding bin'_split_def
supply R = heap_WHILET_rule''[where
R = "measure (λ(l,h). h-l)" and I = "λ(l,h). is_pfa c xs (a,n) * ↑(l≤h ∧ h ≤ n ∧ (∀j<l. xs!j < p) ∧ (h<n ⟶ xs!h≥p))" and b = "λ(l,h). l<h" and Q="λ(l,h). is_pfa c xs (a,n) * ↑(l ≤ n ∧ (∀j<l. xs!j < p) ∧ (l<n ⟶ xs!l≥p))"
] thm R
apply (sep_auto decon: R simp: less_Suc_eq is_pfa_def) []
subgoal for l' aa l'a aaa ba j proof - assume0: "n ≤ length l'a" assume a: "l'a ! ((aa + n) div 2) < p" moreoverassume"aa < n" ultimatelyhave b: "((aa+n)div 2) < n" by linarith thenhave"(take n l'a) ! ((aa + n) div 2) < p" using a by auto moreoverassume"sorted_less (take n l'a)" ultimatelyhave"∧j. j < (aa+n)div 2 ==> (take n l'a) ! j < (take n l'a) ! ((aa + n) div 2)" using
sorted_wrt_nth_less[where ?P="(<)"and xs="(take n l'a)"and ?j="((aa + n) div 2)"]
a b "0"by auto moreoverfix j assume"j < (aa+n) div 2" ultimatelyshow"l'a ! j < p"using"0" b using‹take n l'a ! ((aa + n) div 2) < p› dual_order.strict_trans by auto qed
subgoal for l' aa b l'a aaa ba j proof - assume t0: "n ≤ length l'a" assume t1: "aa < b" assume a: "l'a ! ((aa + b) div 2) < p" moreoverassume"b ≤ n" ultimatelyhave b: "((aa+b)div 2) < n"using t1 by linarith thenhave"(take n l'a) ! ((aa + b) div 2) < p" using a by auto moreoverassume"sorted_less (take n l'a)" ultimatelyhave"∧j. j < (aa+b)div 2 ==> (take n l'a) ! j < (take n l'a) ! ((aa + b) div 2)" using
sorted_wrt_nth_less[where ?P="(<)"and xs="(take n l'a)"and ?j="((aa + b) div 2)"]
a b t0 by auto moreoverfix j assume"j < (aa+b) div 2" ultimatelyshow"l'a ! j < p"using t0 b using‹take n l'a ! ((aa + b) div 2) < p› dual_order.strict_trans by auto qed apply sep_auto apply (metis le_less nth_take) apply (metis le_less nth_take) apply sep_auto
subgoal for l' aa l'a aaa ba j proof - assume t0: "aa < n" assume t1: " n ≤ length l'a" assume t4: "sorted_less (take n l'a)" assume t5: "j < (aa + n) div 2" have"(aa+n) div 2 < n"using t0 by linarith thenhave"(take n l'a) ! j < (take n l'a) ! ((aa + n) div 2)" using t0 sorted_wrt_nth_less[where xs="take n l'a"and ?j="((aa + n) div 2)"]
t1 t4 t5 by auto thenshow ?thesis using‹(aa + n) div 2 < n› t5 by auto qed
subgoal for l' aa b l'a aaa ba j proof - assume t0: "aa < b" assume t1: " n ≤ length l'a" assume t3: "b ≤ n" assume t4: "sorted_less (take n l'a)" assume t5: "j < (aa + b) div 2" have"(aa+b) div 2 < n"using t3 t0 by linarith thenhave"(take n l'a) ! j < (take n l'a) ! ((aa + b) div 2)" using t0 sorted_wrt_nth_less[where xs="take n l'a"and ?j="((aa + b) div 2)"]
t1 t4 t5 by auto thenshow ?thesis using‹(aa + b) div 2 < n› t5 by auto qed apply (metis nth_take order_mono_setup.refl) apply sep_auto apply (sep_auto simp add: is_pfa_def) done
text"Then, using the same loop invariant, a binary split for B-tree-like arrays is derived in a straightforward manner."
definition bin_split :: "('a::heap × 'b::{heap,linorder}) pfarray ==> 'b ==> nat Heap" where "bin_split ≡ λ(a,n) p. do { (low',high') ← heap_WHILET (λ(low,high). return (low < high)) (λ(low,high). let mid = ((low + high) div 2) in do { (_,s) ← Array.nth a mid; if p < s then return (low, mid) else if p > s then return (mid+1, high) else return (mid,mid) }) (0::nat,n); return low' }"
thm nth_take
lemma nth_take_eq: "take n ls = take n ls' ==> i < n ==> ls!i = ls'!i" by (metis nth_take)
lemma bin_split_rule: " sorted_less (map snd xs) ==> < is_pfa c xs (a,n)> bin_split (a,n) p <λl. is_pfa c xs (a,n) * ↑(l ≤ n ∧ (∀j<l. snd(xs!j) < p) ∧ (l<n ⟶ snd(xs!l)≥p)) >t" (* this works in principle, as demonstrated above *) unfolding bin_split_def
supply R = heap_WHILET_rule''[where
R = "measure (λ(l,h). h-l)" and I = "λ(l,h). is_pfa c xs (a,n) * ↑(l≤h ∧ h ≤ n ∧ (∀j<l. snd (xs!j) < p) ∧ (h<n ⟶ snd (xs!h)≥p))" and b = "λ(l,h). l<h" and Q="λ(l,h). is_pfa c xs (a,n) * ↑(l ≤ n ∧ (∀j<l. snd (xs!j) < p) ∧ (l<n ⟶ snd (xs!l)≥p))"
] thm R
apply (sep_auto decon: R simp: less_Suc_eq is_pfa_def) []
apply(auto dest!: sndI nth_take_eq[of n _ _ "(_ + _) div 2"])[] apply(auto dest!: sndI nth_take_eq[of n _ _ "(_ + _) div 2"])[] apply (sep_auto dest!: sndI )
subgoal for ls i ls' _ _ j using map_snd_sorted_lesseq[of "take n ls'" j "(i + n) div 2"]
less_mult_imp_div_less apply(auto)[] done
subgoal for ls i j ls' _ _ j' using map_snd_sorted_lesseq[of "take n ls'" j' "(i + j) div 2"]
less_mult_imp_div_less apply(auto)[] done apply sep_auto
subgoal for ls i ls' _ _ j using map_snd_sorted_less[of "take n ls'" j "(i + n) div 2"]
less_mult_imp_div_less apply(auto)[] done
subgoal for ls i j ls' _ _ j' using map_snd_sorted_less[of "take n ls'" j' "(i + j) div 2"]
less_mult_imp_div_less apply(auto)[] done apply (metis le_less nth_take_eq) apply sep_auto apply (sep_auto simp add: is_pfa_def) done
subsection"Refinement of an abstract split"
text"We provide a certain abstract split function that is particularly easy to analyse. The idea of this function is due to Peter Lammich."
interpretation btree_abs_search: split abs_split unfolding abs_split_def sym[OF linear_split_alt] by unfold_locales
text‹Any function that yields the heap rule
have obtained for bin\_split and lin\_split also
this abstract split.›
locale imp_split_smeq = fixes split_fun :: "('a::{heap,default,linorder} btnode ref option × 'a) array × nat ==> 'a ==> nat Heap" assumes split_rule: "sorted_less (separators xs) ==> <is_pfa c xs (a, n)> split_fun (a, n) (p::'a) <λr. is_pfa c xs (a, n) * ↑ (r ≤ n ∧ (∀j<r. snd (xs ! j) < p) ∧ (r < n ⟶ p ≤ snd (xs ! r)))>t" begin
lemma abs_split_full: "∀(_,s) ∈ set xs. s < p ==> abs_split xs p = (xs,[])" by (simp add: abs_split_def)
lemma abs_split_split: assumes"n < length xs" and"(∀(_,s) ∈ set (take n xs). s < p)" and" (case (xs!n) of (_,s) ==>¬(s < p))" shows"abs_split xs p = (take n xs, drop n xs)" using assms apply (auto simp add: abs_split_def) apply (metis (mono_tags, lifting) id_take_nth_drop old.prod.case takeWhile_eq_all_conv takeWhile_tail) by (metis (no_types, lifting) Cons_nth_drop_Suc case_prod_conv dropWhile.simps(2) dropWhile_append2 id_take_nth_drop)
lemma split_rule_abs_split: shows "sorted_less (separators ts) ==> < is_pfa c tsi (a,n) * list_assn (A ×a id_assn) ts tsi> split_fun (a,n) p <λi. is_pfa c tsi (a,n) * list_assn (A ×a id_assn) ts tsi * ↑(split_relation ts (abs_split ts p) i)>t" apply(rule hoare_triple_preI) apply (sep_auto heap: split_rule dest!: mod_starD id_assn_list
simp add: list_assn_prod_map split_ismeq) apply(auto simp add: is_pfa_def) proof -
fix h l' assume heap_init: "h ⊨ a ↦a l'" "map snd ts = (map snd (take n l'))" "n ≤ length l'"
show full_thm: "∀j<n. snd (l' ! j) < p ==> split_relation ts (abs_split ts p) n" proof - assume sm_list: "∀j<n. snd (l' ! j) < p" thenhave"∀j < length (map snd (take n l')). ((map snd (take n l'))!j) < p" by simp thenhave"∀j<length (map snd ts). ((map snd ts)!j) < p" using heap_init by simp thenhave"∀(_,s) ∈ set ts. s < p" by (metis case_prod_unfold in_set_conv_nth length_map nth_map) thenhave"abs_split ts p = (ts, [])" using abs_split_full[of ts p] by simp thenshow"split_relation ts (abs_split ts p) n" using split_relation_length by (metis heap_init(2) heap_init(3) length_map length_take min.absorb2)
qed thenshow"∀j<n. snd (l' ! j) < p ==> p ≤ snd (take n l' ! n) ==> split_relation ts (abs_split ts p) n" by simp
show part_thm: "∧x. x < n ==> ∀j<x. snd (l' ! j) < p ==> p ≤ snd (l' ! x) ==> split_relation ts (abs_split ts p) x" proof - fix x assume x_sm_len: "x < n" moreoverassume sm_list: "∀j<x. snd (l' ! j) < p" ultimatelyhave"∀j<x. ((map snd l') ! j) < p" using heap_init by auto thenhave"∀j<x. ((map snd ts)!j) < p" using heap_init x_sm_len by auto moreoverhave x_sm_len_ts: "x < n" using heap_init x_sm_len by auto ultimatelyhave"∀(_,x) ∈ set (take x ts). x < p" by (auto simp add: in_set_conv_nth min.absorb2)+ moreoverassume"p ≤ snd (l' ! x)" thenhave"case l'!x of (_,s) ==>¬(s < p)" by (simp add: case_prod_unfold) thenhave"case ts!x of (_,s) ==>¬(s < p)" using heap_init x_sm_len x_sm_len_ts by (metis (mono_tags, lifting) case_prod_unfold length_map length_take min.absorb2 nth_take snd_map_help(2)) ultimatelyhave"abs_split ts p = (take x ts, drop x ts)" using x_sm_len_ts abs_split_split[of x ts p] heap_init by (metis length_map length_take min.absorb2) thenshow"split_relation ts (abs_split ts p) x" using x_sm_len_ts by (metis append_take_drop_id heap_init(2) heap_init(3) length_map length_take less_imp_le_nat min.absorb2 split_relation_alt) qed qed
text"Obtaining actual code turns out to be slightly more difficult due to the use of locales. However, we successfully obtain the B-tree insertion and membership query with binary search splitting."
global_interpretation btree_imp_binary_split: imp_split_smeq bin_split defines btree_isin = btree_imp_binary_split.isin and btree_ins = btree_imp_binary_split.ins and btree_insert = btree_imp_binary_split.insert and btree_del = btree_imp_binary_split.del and btree_split_max = btree_imp_binary_split.split_max and btree_delete = btree_imp_binary_split.delete and btree_empty = btree_imp_binary_split.empty apply unfold_locales apply(sep_auto heap: bin_split_rule) done
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.