Theory Collections.Impl_Bit_Set
section "Bitvector based Sets of Naturals"
theory Impl_Bit_Set
imports
"../../Iterator/Iterator"
"../Intf/Intf_Set"
Native_Word.Code_Target_Integer_Bit
begin
text ‹
Based on the Native-Word library, using bit-operations on arbitrary
precision integers. Fast for sets of small numbers,
direct and fast implementations of equal, union, inter, diff.
Note: On Poly/ML 5.5.1, bit-operations on arbitrary precision integers are
rather inefficient. Use MLton instead, here they are efficiently implemented.
›
type_synonym bitset = integer
definition bs_α :: "bitset ⇒ nat set" where "bs_α s ≡ { n . bit s n}"
context
includes integer.lifting and bit_operations_syntax
begin
definition bs_empty :: "unit ⇒ bitset" where "bs_empty ≡ λ_. 0"
lemma bs_empty_correct: "bs_α (bs_empty ()) = {}"
unfolding bs_α_def bs_empty_def
apply transfer
by auto
definition bs_isEmpty :: "bitset ⇒ bool" where "bs_isEmpty s ≡ s=0"
lemma bs_isEmpty_correct: "bs_isEmpty s ⟷ bs_α s = {}"
unfolding bs_isEmpty_def bs_α_def
by transfer (auto simp: bit_eq_iff)
term set_bit
definition bs_insert :: "nat ⇒ bitset ⇒ bitset" where
"bs_insert i s ≡ set_bit s i True"
lemma bs_insert_correct: "bs_α (bs_insert i s) = insert i (bs_α s)"
unfolding bs_α_def bs_insert_def
by transfer (auto simp add: bit_simps)
definition bs_delete :: "nat ⇒ bitset ⇒ bitset" where
"bs_delete i s ≡ set_bit s i False"
lemma bs_delete_correct: "bs_α (bs_delete i s) = (bs_α s) - {i}"
unfolding bs_α_def bs_delete_def
by transfer (auto simp add: bit_simps split: if_splits)
definition bs_mem :: "nat ⇒ bitset ⇒ bool" where
"bs_mem i s ≡ bit s i"
lemma bs_mem_correct: "bs_mem i s ⟷ i∈bs_α s"
unfolding bs_mem_def bs_α_def by transfer auto
definition bs_eq :: "bitset ⇒ bitset ⇒ bool" where
"bs_eq s1 s2 ≡ (s1=s2)"
lemma bs_eq_correct: "bs_eq s1 s2 ⟷ bs_α s1 = bs_α s2"
unfolding bs_eq_def bs_α_def
including integer.lifting
by transfer (simp add: bit_eq_iff set_eq_iff)
definition bs_subset_eq :: "bitset ⇒ bitset ⇒ bool" where
"bs_subset_eq s1 s2 ≡ s1 AND NOT s2 = 0"
lemma bs_subset_eq_correct: "bs_subset_eq s1 s2 ⟷ bs_α s1 ⊆ bs_α s2"
unfolding bs_α_def bs_subset_eq_def
by transfer (simp add: bit_eq_iff, auto simp add: bit_simps)
definition bs_disjoint :: "bitset ⇒ bitset ⇒ bool" where
"bs_disjoint s1 s2 ≡ s1 AND s2 = 0"
lemma bs_disjoint_correct: "bs_disjoint s1 s2 ⟷ bs_α s1 ∩ bs_α s2 = {}"
unfolding bs_α_def bs_disjoint_def
by transfer (simp add: bit_eq_iff, auto simp add: bit_simps)
definition bs_union :: "bitset ⇒ bitset ⇒ bitset" where
"bs_union s1 s2 = s1 OR s2"
lemma bs_union_correct: "bs_α (bs_union s1 s2) = bs_α s1 ∪ bs_α s2"
unfolding bs_α_def bs_union_def
by transfer (simp add: bit_eq_iff, auto simp add: bit_simps)
definition bs_inter :: "bitset ⇒ bitset ⇒ bitset" where
"bs_inter s1 s2 = s1 AND s2"
lemma bs_inter_correct: "bs_α (bs_inter s1 s2) = bs_α s1 ∩ bs_α s2"
unfolding bs_α_def bs_inter_def
by transfer (simp add: bit_eq_iff, auto simp add: bit_simps)
definition bs_diff :: "bitset ⇒ bitset ⇒ bitset" where
"bs_diff s1 s2 = s1 AND NOT s2"
lemma bs_diff_correct: "bs_α (bs_diff s1 s2) = bs_α s1 - bs_α s2"
unfolding bs_α_def bs_diff_def
by transfer (simp add: bit_eq_iff, auto simp add: bit_simps)
definition bs_UNIV :: "unit ⇒ bitset" where "bs_UNIV ≡ λ_. -1"
lemma bs_UNIV_correct: "bs_α (bs_UNIV ()) = UNIV"
unfolding bs_α_def bs_UNIV_def
by transfer (auto)
definition bs_complement :: "bitset ⇒ bitset" where
"bs_complement s = NOT s"
lemma bs_complement_correct: "bs_α (bs_complement s) = - bs_α s"
unfolding bs_α_def bs_complement_def
by transfer (simp add: bit_eq_iff, auto simp add: bit_simps)
end
lemmas bs_correct[simp] =
bs_empty_correct
bs_isEmpty_correct
bs_insert_correct
bs_delete_correct
bs_mem_correct
bs_eq_correct
bs_subset_eq_correct
bs_disjoint_correct
bs_union_correct
bs_inter_correct
bs_diff_correct
bs_UNIV_correct
bs_complement_correct
subsection ‹Autoref Setup›
definition bs_set_rel_def_internal:
"bs_set_rel Rk ≡
if Rk=nat_rel then br bs_α (λ_. True) else {}"
lemma bs_set_rel_def:
"⟨nat_rel⟩bs_set_rel ≡ br bs_α (λ_. True)"
unfolding bs_set_rel_def_internal relAPP_def by simp
lemmas [autoref_rel_intf] = REL_INTFI[of "bs_set_rel" i_set]
lemma bs_set_rel_sv[relator_props]: "single_valued (⟨nat_rel⟩bs_set_rel)"
unfolding bs_set_rel_def by auto
term bs_empty
lemma [autoref_rules]: "(bs_empty (),{})∈⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_UNIV (),UNIV)∈⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_isEmpty,op_set_isEmpty)∈⟨nat_rel⟩bs_set_rel → bool_rel"
by (auto simp: bs_set_rel_def br_def)
term insert
lemma [autoref_rules]: "(bs_insert,insert)∈nat_rel → ⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
term op_set_delete
lemma [autoref_rules]: "(bs_delete,op_set_delete)∈nat_rel → ⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_mem,(∈))∈nat_rel → ⟨nat_rel⟩bs_set_rel → bool_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_eq,(=))∈⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel → bool_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_subset_eq,(⊆))∈⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel → bool_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_union,(∪))∈⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_inter,(∩))∈⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_diff,(-))∈⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_complement,uminus)∈⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel"
by (auto simp: bs_set_rel_def br_def)
lemma [autoref_rules]: "(bs_disjoint,op_set_disjoint)∈⟨nat_rel⟩bs_set_rel → ⟨nat_rel⟩bs_set_rel → bool_rel"
by (auto simp: bs_set_rel_def br_def)
export_code
bs_empty
bs_isEmpty
bs_insert
bs_delete
bs_mem
bs_eq
bs_subset_eq
bs_disjoint
bs_union
bs_inter
bs_diff
bs_UNIV
bs_complement
in SML
end