generalized datatype code generation code so that it works with old-style and new-style (co)datatypes (as long as they are not local)
1 (* Title: HOL/Tools/ctr_sugar_code.ML
2 Author: Jasmin Blanchette, TU Muenchen
3 Author: Dmitriy Traytel, TU Muenchen
4 Author: Stefan Berghofer, TU Muenchen
5 Author: Florian Haftmann, TU Muenchen
8 Code generation for freely generated types.
11 signature CTR_SUGAR_CODE =
13 val add_ctr_code: string -> typ list -> (string * typ) list -> thm list -> thm list -> thm list ->
17 structure Ctr_Sugar_Code : CTR_SUGAR_CODE =
26 fun mk_case_certificate thy raw_thms =
28 val thms as thm1 :: _ = raw_thms
29 |> Conjunction.intr_balanced
30 |> Thm.unvarify_global
31 |> Conjunction.elim_balanced (length raw_thms)
32 |> map Simpdata.mk_meta_eq
33 |> map Drule.zero_var_indexes;
34 val params = Term.add_free_names (Thm.prop_of thm1) [];
36 |> Thm.prop_of |> Logic.dest_equals |> fst |> Term.strip_comb
37 ||> fst o split_last |> list_comb;
38 val lhs = Free (singleton (Name.variant_list params) "case", Term.fastype_of rhs);
39 val assum = Thm.cterm_of thy (Logic.mk_equals (lhs, rhs));
42 |> Conjunction.intr_balanced
43 |> rewrite_rule [Thm.symmetric (Thm.assume assum)]
44 |> Thm.implies_intr assum
45 |> Thm.generalize ([], params) 0
46 |> Axclass.unoverload thy
50 fun mk_free_ctr_equations fcT ctrs inject_thms distinct_thms thy =
52 fun mk_fcT_eq (t, u) = Const (@{const_name HOL.equal}, fcT --> fcT --> HOLogic.boolT) $ t $ u;
53 fun true_eq tu = HOLogic.mk_eq (mk_fcT_eq tu, @{term True});
54 fun false_eq tu = HOLogic.mk_eq (mk_fcT_eq tu, @{term False});
56 val monomorphic_prop_of = prop_of o Thm.unvarify_global o Drule.zero_var_indexes;
58 fun massage_inject (tp $ (eqv $ (_ $ t $ u) $ rhs)) = tp $ (eqv $ mk_fcT_eq (t, u) $ rhs);
59 fun massage_distinct (tp $ (_ $ (_ $ t $ u))) = [tp $ false_eq (t, u), tp $ false_eq (u, t)];
61 val triv_inject_goals =
62 map_filter (fn c as (_, T) =>
63 if T = fcT then SOME (HOLogic.mk_Trueprop (true_eq (Const c, Const c))) else NONE)
65 val inject_goals = map (massage_inject o monomorphic_prop_of) inject_thms;
66 val distinct_goals = maps (massage_distinct o monomorphic_prop_of) distinct_thms;
67 val refl_goal = HOLogic.mk_Trueprop (true_eq (Free ("x", fcT), Free ("x", fcT)));
70 Simplifier.global_context thy HOL_basic_ss
71 addsimps (map Simpdata.mk_eq (@{thms equal eq_True} @ inject_thms @ distinct_thms));
74 Goal.prove_sorry_global thy [] [] goal (K (ALLGOALS (simp_tac simp_ctxt)))
77 (map prove (triv_inject_goals @ inject_goals @ distinct_goals), prove refl_goal)
80 fun add_equality fcT fcT_name As ctrs inject_thms distinct_thms =
84 fun mk_side const_name =
85 Const (const_name, fcT --> fcT --> HOLogic.boolT) $ Free ("x", fcT) $ Free ("y", fcT);
87 mk_Trueprop_eq (mk_side @{const_name HOL.equal}, mk_side @{const_name HOL.eq})
88 |> Syntax.check_term lthy;
89 val ((_, (_, raw_def)), lthy') =
90 Specification.definition (NONE, (Attrib.empty_binding, spec)) lthy;
91 val ctxt_thy = Proof_Context.init_global (Proof_Context.theory_of lthy); (* FIXME? *)
92 val def = singleton (Proof_Context.export lthy' ctxt_thy) raw_def;
97 fun tac thms = Class.intro_classes_tac [] THEN ALLGOALS (Proof_Context.fact_tac thms);
100 Binding.qualify true (Long_Name.base_name fcT_name) o Binding.qualify true eqN o Binding.name;
102 Class.instantiation ([fcT_name], map dest_TFree As, [HOLogic.class_equal])
104 #-> Class.prove_instantiation_exit_result (map o Morphism.thm) (K tac) o single
105 #-> fold Code.del_eqn
106 #> `(mk_free_ctr_equations fcT ctrs inject_thms distinct_thms)
107 #-> (fn (thms, thm) => Global_Theory.note_thmss Thm.lemmaK
108 [((qualify reflN, [Code.add_nbe_default_eqn_attribute]), [([thm], [])]),
109 ((qualify simpsN, [Code.add_default_eqn_attribute]), [(rev thms, [])])])
113 fun add_ctr_code fcT_name As ctrs inject_thms distinct_thms case_thms thy =
115 val fcT = Type (fcT_name, As);
116 val unover_ctrs = map (fn ctr as (_, fcT) => (Axclass.unoverload_const thy ctr, fcT)) ctrs;
118 if can (Code.constrset_of_consts thy) unover_ctrs then
120 |> Code.add_datatype ctrs
121 |> fold_rev Code.add_default_eqn case_thms
122 |> Code.add_case (mk_case_certificate thy case_thms)
123 |> not (Sorts.has_instance (Sign.classes_of thy) fcT_name [HOLogic.class_equal])
124 ? add_equality fcT fcT_name As ctrs inject_thms distinct_thms