(* Author: Andreas Lochbihler and Tobias Nipkow *) section "Trie" theory Trie imports "HOL-Library.AList" begin (* A truly generic Trie would be parameterized by the data type for mappings in each node. For this purpose the mapping type needs to become a bnf. import "HOL-Library.Mapping" lift_definition all_mapping :: "('b ⇒ bool) ⇒ ('a, 'b) mapping ⇒ bool" is "λp m. ∀a b. m a = Some b ⟶ p b" . lemma all_mapping_cong [fundef_cong]: "m = n ⟹ (⋀x y. Mapping.lookup n x = Some y ⟹ p y = q y) ⟹ all_mapping p m = all_mapping q n" by transfer auto lift_definition set_mapping :: "('a,'b)mapping ⇒ 'b set" is "λf. Union(set_option ` range f)" . lift_definition rel_mapping :: "('b ⇒ 'c ⇒ bool) ⇒ ('a,'b)mapping ⇒ ('a,'c)mapping ⇒ bool" is "λr. rel_fun (=) (rel_option r)" . bnf "('a,'b)mapping" map: "Mapping.map id" sets: set_mapping bd: "BNF_Cardinal_Arithmetic.csum (card_of (UNIV::'a set)) natLeq" wits: Mapping.empty rel: rel_mapping proof- case goal1 show ?case by(rule Mapping.map.id) next case goal2 show ?case by transfer (simp add: fun_eq_iff option.map_comp) next case goal3 thus ?case by transfer (auto simp: fun_eq_iff intro: option.map_cong) next case goal4 thus ?case by transfer (auto simp: fun_eq_iff) next case goal5 thus ?case by (simp add: Field_card_of Field_natLeq card_of_card_order_on csum_def) next case goal6 thus ?case by (simp add: cinfinite_csum natLeq_cinfinite) next case goal7 thus ?case apply transfer apply(rule ordLeq_transitive[OF comp_single_set_bd[where fset=set_option and gset=range, of natLeq "BNF_Cardinal_Arithmetic.csum (card_of (UNIV::'d set)) natLeq"]]) apply (rule natLeq_Card_order) apply(rename_tac opt) apply(case_tac opt) apply (simp add: card_of_empty1 natLeq_Well_order) using Card_order_singl_ordLeq Field_natLeq natLeq_Card_order apply fastforce apply(rule ordLeq_transitive[OF card_of_image]) apply (simp add: card_of_Card_order ordLeq_csum1) apply(rule cprod_cinfinite_bound) apply(rule ordLeq_refl) apply (rule Card_order_csum) apply (simp add: natLeq_Card_order ordLeq_csum2) apply (rule Card_order_csum) apply (rule natLeq_Card_order) using Cinfinite_csum natLeq_Card_order natLeq_cinfinite by blast next case goal8 thus ?case apply(rule predicate2I) apply transfer by(auto simp: option.rel_compp rel_fun_def) next case goal9 show ?case unfolding OO_Grp_alt fun_eq_iff apply transfer apply safe apply(rename_tac f g) apply(rule_tac x = "λx. case (f x, g x) of (Some u,Some v) ⇒ Some(u,v) | _ ⇒ None" in exI) apply(auto simp: fun_eq_iff rel_fun_def elim!: Option.option.rel_cases split: option.splits) apply (metis option.rel_inject(2)) apply (metis rel_option_None1) apply(auto simp add: option.rel_map subset_eq intro!: rel_option_reflI split: prod.splits) done next case goal10 thus ?case by transfer simp qed *)