author | wenzelm |
Thu, 23 Jul 2009 23:12:21 +0200 | |
changeset 32155 | e2bf2f73b0c8 |
parent 31735 | a00292a5587d |
child 32378 | 89b19ab3b35c |
permissions | -rw-r--r-- |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31136
diff
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:
31136
diff
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:
31090
diff
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:
20426
diff
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:
31090
diff
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:
31136
diff
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:
24624
diff
changeset
|
35 |
type T = info Symtab.table; |
85eceef2edc7
introduced generic concepts for theory interpretators
haftmann
parents:
24624
diff
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:
24624
diff
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:
31090
diff
changeset
|
50 |
|
31735 | 51 |
(* introducing typecopies *) |
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
52 |
|
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
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:
20426
diff
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:
31136
diff
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:
24624
diff
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:
20426
diff
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:
31136
diff
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:
25864
diff
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:
20426
diff
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:
31090
diff
changeset
|
90 |
(* default code setup *) |
20835 | 91 |
|
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
92 |
fun add_default_code tyco thy = |
25489 | 93 |
let |
28664 | 94 |
val SOME { constr = constr_name, proj = (proj, _), proj_def = proj_eq, vs = raw_vs, |
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
95 |
typ = ty_rep, ... } = get_info thy tyco; |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31136
diff
changeset
|
96 |
val SOME { Rep_inject = proj_inject, ... } = Typedef.get_info thy tyco; |
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
97 |
val constr = (constr_name, Logic.unvarifyT (Sign.the_const_type thy constr_name)); |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
98 |
val vs = (map dest_TFree o snd o dest_Type) (Type (tyco, map TFree raw_vs)); |
28423 | 99 |
val ty = Type (tyco, map TFree vs); |
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
100 |
val proj = Const (proj, ty --> ty_rep); |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
101 |
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:
31090
diff
changeset
|
102 |
val eq_lhs = Const (@{const_name eq_class.eq}, ty --> ty --> HOLogic.boolT) |
28664 | 103 |
$ t_x $ t_y; |
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
104 |
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:
31090
diff
changeset
|
105 |
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:
31090
diff
changeset
|
106 |
fun tac eq_thm = Class.intro_classes_tac [] |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
107 |
THEN (Simplifier.rewrite_goals_tac |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
108 |
(map Simpdata.mk_eq [eq_thm, @{thm eq}, proj_inject])) |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
109 |
THEN ALLGOALS (rtac @{thm refl}); |
28664 | 110 |
fun mk_eq_refl thy = @{thm HOL.eq_refl} |
111 |
|> Thm.instantiate |
|
112 |
([pairself (Thm.ctyp_of thy) (TVar (("'a", 0), @{sort eq}), Logic.varifyT ty)], []) |
|
113 |
|> AxClass.unoverload thy; |
|
25489 | 114 |
in |
25506 | 115 |
thy |
28664 | 116 |
|> Code.add_datatype [constr] |
117 |
|> Code.add_eqn proj_eq |
|
28423 | 118 |
|> TheoryTarget.instantiation ([tyco], vs, [HOLogic.class_eq]) |
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
119 |
|> `(fn lthy => Syntax.check_term lthy eq) |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
120 |
|-> (fn eq => Specification.definition |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
121 |
(NONE, (Attrib.empty_binding, eq))) |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
122 |
|-> (fn (_, (_, eq_thm)) => |
28664 | 123 |
Class.prove_instantiation_exit_result Morphism.thm |
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
124 |
(fn _ => fn eq_thm => tac eq_thm) eq_thm) |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
125 |
|-> (fn eq_thm => Code.add_eqn eq_thm) |
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
126 |
|> (fn thy => Code.add_nbe_eqn (mk_eq_refl thy) thy) |
25489 | 127 |
end; |
20426 | 128 |
|
25489 | 129 |
val setup = |
130 |
TypecopyInterpretation.init |
|
31136
85d04515abb3
tuned and generalized construction of code equations for eq; tuned interface
haftmann
parents:
31090
diff
changeset
|
131 |
#> interpretation add_default_code |
20426 | 132 |
|
22846 | 133 |
end; |