| author | paulson | 
| Mon, 07 Sep 2009 13:19:09 +0100 | |
| changeset 32532 | a0a54a51b15b | 
| parent 32378 | 89b19ab3b35c | 
| child 33314 | 53d49370f7af | 
| permissions | -rw-r--r-- | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31136diff
changeset | 1 | (* Title: HOL/Tools/typecopy.ML | 
| 20426 | 2 | Author: Florian Haftmann, TU Muenchen | 
| 3 | ||
| 20855 | 4 | Introducing copies of types using trivial typedefs; datatype-like abstraction. | 
| 20426 | 5 | *) | 
| 6 | ||
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31136diff
changeset | 7 | signature TYPECOPY = | 
| 20426 | 8 | sig | 
| 31735 | 9 |   type info = { vs: (string * sort) list, constr: string, typ: typ,
 | 
| 10 | inject: thm, proj: string * typ, proj_def: thm } | |
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 11 | val typecopy: binding * string list -> typ -> (binding * binding) option | 
| 20483 
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
 haftmann parents: 
20426diff
changeset | 12 | -> theory -> (string * info) * theory | 
| 25506 | 13 | val get_info: theory -> string -> info option | 
| 25489 | 14 | val interpretation: (string -> theory -> theory) -> theory -> theory | 
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 15 | val add_default_code: string -> theory -> theory | 
| 20426 | 16 | val setup: theory -> theory | 
| 17 | end; | |
| 18 | ||
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31136diff
changeset | 19 | structure Typecopy: TYPECOPY = | 
| 20426 | 20 | struct | 
| 21 | ||
| 22 | (* theory data *) | |
| 23 | ||
| 24 | type info = {
 | |
| 25 | vs: (string * sort) list, | |
| 26 | constr: string, | |
| 27 | typ: typ, | |
| 28 | inject: thm, | |
| 29 | proj: string * typ, | |
| 30 | proj_def: thm | |
| 31 | }; | |
| 32 | ||
| 33 | structure TypecopyData = TheoryDataFun | |
| 22846 | 34 | ( | 
| 24626 
85eceef2edc7
introduced generic concepts for theory interpretators
 haftmann parents: 
24624diff
changeset | 35 | type T = info Symtab.table; | 
| 
85eceef2edc7
introduced generic concepts for theory interpretators
 haftmann parents: 
24624diff
changeset | 36 | val empty = Symtab.empty; | 
| 20426 | 37 | val copy = I; | 
| 38 | val extend = I; | |
| 24626 
85eceef2edc7
introduced generic concepts for theory interpretators
 haftmann parents: 
24624diff
changeset | 39 | fun merge _ = Symtab.merge (K true); | 
| 22846 | 40 | ); | 
| 41 | ||
| 25506 | 42 | val get_info = Symtab.lookup o TypecopyData.get; | 
| 20426 | 43 | |
| 44 | ||
| 25489 | 45 | (* interpretation of type copies *) | 
| 20426 | 46 | |
| 24711 | 47 | structure TypecopyInterpretation = InterpretationFun(type T = string val eq = op =); | 
| 25489 | 48 | val interpretation = TypecopyInterpretation.interpretation; | 
| 20426 | 49 | |
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 50 | |
| 31735 | 51 | (* introducing typecopies *) | 
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 52 | |
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 53 | fun typecopy (raw_tyco, raw_vs) raw_ty constr_proj thy = | 
| 20426 | 54 | let | 
| 28664 | 55 | val ty = Sign.certify_typ thy raw_ty; | 
| 29272 | 56 | val vs = | 
| 57 | AList.make (the_default HOLogic.typeS o AList.lookup (op =) (Term.add_tfreesT ty [])) raw_vs; | |
| 20426 | 58 | val tac = Tactic.rtac UNIV_witness 1; | 
| 20483 
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
 haftmann parents: 
20426diff
changeset | 59 |     fun add_info tyco ( { abs_type = ty_abs, rep_type = ty_rep, Abs_name = c_abs,
 | 
| 20426 | 60 | Rep_name = c_rep, Abs_inject = inject, | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31136diff
changeset | 61 | Abs_inverse = inverse, ... } : Typedef.info ) thy = | 
| 20426 | 62 | let | 
| 63 | val exists_thm = | |
| 64 | UNIV_I | |
| 65 | |> Drule.instantiate' [SOME (ctyp_of thy (Logic.varifyT ty_rep))] []; | |
| 66 | val inject' = inject OF [exists_thm, exists_thm]; | |
| 67 | val proj_def = inverse OF [exists_thm]; | |
| 68 |           val info = {
 | |
| 69 | vs = vs, | |
| 70 | constr = c_abs, | |
| 71 | typ = ty_rep, | |
| 72 | inject = inject', | |
| 73 | proj = (c_rep, ty_abs --> ty_rep), | |
| 74 | proj_def = proj_def | |
| 75 | }; | |
| 76 | in | |
| 77 | thy | |
| 24626 
85eceef2edc7
introduced generic concepts for theory interpretators
 haftmann parents: 
24624diff
changeset | 78 | |> (TypecopyData.map o Symtab.update_new) (tyco, info) | 
| 24711 | 79 | |> TypecopyInterpretation.data tyco | 
| 20483 
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
 haftmann parents: 
20426diff
changeset | 80 | |> pair (tyco, info) | 
| 20426 | 81 | end | 
| 82 | in | |
| 83 | thy | |
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31136diff
changeset | 84 | |> Typedef.add_typedef false (SOME raw_tyco) (raw_tyco, map fst vs, NoSyn) | 
| 26475 
3cc1e48d0ce1
eliminated quiet_mode ref of some packages (avoid CRITICAL setmp!);
 wenzelm parents: 
25864diff
changeset | 85 | (HOLogic.mk_UNIV ty) (Option.map swap constr_proj) tac | 
| 20483 
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
 haftmann parents: 
20426diff
changeset | 86 | |-> (fn (tyco, info) => add_info tyco info) | 
| 20426 | 87 | end; | 
| 88 | ||
| 89 | ||
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 90 | (* default code setup *) | 
| 20835 | 91 | |
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 92 | fun add_default_code tyco thy = | 
| 25489 | 93 | let | 
| 32378 | 94 |     val SOME { constr = c, proj = (proj, _), proj_def = proj_eq, vs = vs,
 | 
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 95 | typ = ty_rep, ... } = get_info thy tyco; | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31136diff
changeset | 96 |     val SOME { Rep_inject = proj_inject, ... } = Typedef.get_info thy tyco;
 | 
| 32378 | 97 | val constr = (c, Logic.unvarifyT (Sign.the_const_type thy c)); | 
| 28423 | 98 | val ty = Type (tyco, map TFree vs); | 
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 99 | val proj = Const (proj, ty --> ty_rep); | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 100 |     val (t_x, t_y) = (Free ("x", ty), Free ("y", ty));
 | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 101 |     val eq_lhs = Const (@{const_name eq_class.eq}, ty --> ty --> HOLogic.boolT)
 | 
| 28664 | 102 | $ t_x $ t_y; | 
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 103 | val eq_rhs = HOLogic.mk_eq (proj $ t_x, proj $ t_y); | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 104 | val eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq) (eq_lhs, eq_rhs); | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 105 | fun tac eq_thm = Class.intro_classes_tac [] | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 106 | THEN (Simplifier.rewrite_goals_tac | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 107 |         (map Simpdata.mk_eq [eq_thm, @{thm eq}, proj_inject]))
 | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 108 |           THEN ALLGOALS (rtac @{thm refl});
 | 
| 28664 | 109 |     fun mk_eq_refl thy = @{thm HOL.eq_refl}
 | 
| 110 | |> Thm.instantiate | |
| 111 |          ([pairself (Thm.ctyp_of thy) (TVar (("'a", 0), @{sort eq}), Logic.varifyT ty)], [])
 | |
| 112 | |> AxClass.unoverload thy; | |
| 25489 | 113 | in | 
| 25506 | 114 | thy | 
| 28664 | 115 | |> Code.add_datatype [constr] | 
| 116 | |> Code.add_eqn proj_eq | |
| 28423 | 117 | |> TheoryTarget.instantiation ([tyco], vs, [HOLogic.class_eq]) | 
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 118 | |> `(fn lthy => Syntax.check_term lthy eq) | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 119 | |-> (fn eq => Specification.definition | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 120 | (NONE, (Attrib.empty_binding, eq))) | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 121 | |-> (fn (_, (_, eq_thm)) => | 
| 28664 | 122 | Class.prove_instantiation_exit_result Morphism.thm | 
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 123 | (fn _ => fn eq_thm => tac eq_thm) eq_thm) | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 124 | |-> (fn eq_thm => Code.add_eqn eq_thm) | 
| 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 125 | |> (fn thy => Code.add_nbe_eqn (mk_eq_refl thy) thy) | 
| 25489 | 126 | end; | 
| 20426 | 127 | |
| 25489 | 128 | val setup = | 
| 129 | TypecopyInterpretation.init | |
| 31136 
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
 haftmann parents: 
31090diff
changeset | 130 | #> interpretation add_default_code | 
| 20426 | 131 | |
| 22846 | 132 | end; |