wenzelm@4866
|
1 |
(* Title: HOL/Tools/typedef_package.ML
|
wenzelm@4866
|
2 |
ID: $Id$
|
wenzelm@4866
|
3 |
Author: Markus Wenzel, TU Muenchen
|
wenzelm@4866
|
4 |
|
wenzelm@6357
|
5 |
Gordon/HOL-style type definitions.
|
wenzelm@4866
|
6 |
*)
|
wenzelm@4866
|
7 |
|
wenzelm@4866
|
8 |
signature TYPEDEF_PACKAGE =
|
wenzelm@4866
|
9 |
sig
|
wenzelm@5697
|
10 |
val quiet_mode: bool ref
|
wenzelm@5104
|
11 |
val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
|
wenzelm@11822
|
12 |
val add_typedef_x: string -> bstring * string list * mixfix ->
|
wenzelm@4866
|
13 |
string -> string list -> thm list -> tactic option -> theory -> theory
|
wenzelm@11827
|
14 |
val add_typedef: bool -> string option -> bstring * string list * mixfix ->
|
wenzelm@11822
|
15 |
string -> (bstring * bstring) option -> tactic -> theory -> theory *
|
wenzelm@11822
|
16 |
{type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
|
wenzelm@11822
|
17 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
|
wenzelm@11822
|
18 |
Rep_induct: thm, Abs_induct: thm}
|
wenzelm@11827
|
19 |
val add_typedef_i: bool -> string option -> bstring * string list * mixfix ->
|
wenzelm@11822
|
20 |
term -> (bstring * bstring) option -> tactic -> theory -> theory *
|
wenzelm@11822
|
21 |
{type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
|
wenzelm@11822
|
22 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
|
wenzelm@11822
|
23 |
Rep_induct: thm, Abs_induct: thm}
|
wenzelm@13443
|
24 |
val typedef_proof: (bool * string) * (bstring * string list * mixfix) * string
|
wenzelm@13443
|
25 |
* (string * string) option -> bool -> theory -> ProofHistory.T
|
wenzelm@13443
|
26 |
val typedef_proof_i: (bool * string) * (bstring * string list * mixfix) * term
|
wenzelm@13443
|
27 |
* (string * string) option -> bool -> theory -> ProofHistory.T
|
berghofe@15259
|
28 |
val setup: (theory -> theory) list
|
wenzelm@4866
|
29 |
end;
|
wenzelm@4866
|
30 |
|
wenzelm@4866
|
31 |
structure TypedefPackage: TYPEDEF_PACKAGE =
|
wenzelm@4866
|
32 |
struct
|
wenzelm@4866
|
33 |
|
wenzelm@4866
|
34 |
|
wenzelm@10280
|
35 |
(** theory context references **)
|
wenzelm@10280
|
36 |
|
wenzelm@11608
|
37 |
val type_definitionN = "Typedef.type_definition";
|
wenzelm@10280
|
38 |
|
wenzelm@13413
|
39 |
val Rep = thm "type_definition.Rep";
|
wenzelm@13413
|
40 |
val Rep_inverse = thm "type_definition.Rep_inverse";
|
wenzelm@13413
|
41 |
val Abs_inverse = thm "type_definition.Abs_inverse";
|
wenzelm@13413
|
42 |
val Rep_inject = thm "type_definition.Rep_inject";
|
wenzelm@13413
|
43 |
val Abs_inject = thm "type_definition.Abs_inject";
|
wenzelm@13413
|
44 |
val Rep_cases = thm "type_definition.Rep_cases";
|
wenzelm@13413
|
45 |
val Abs_cases = thm "type_definition.Abs_cases";
|
wenzelm@13413
|
46 |
val Rep_induct = thm "type_definition.Rep_induct";
|
wenzelm@13413
|
47 |
val Abs_induct = thm "type_definition.Abs_induct";
|
wenzelm@10280
|
48 |
|
wenzelm@10280
|
49 |
|
wenzelm@10280
|
50 |
|
berghofe@15259
|
51 |
(** theory data **)
|
berghofe@15259
|
52 |
|
berghofe@15259
|
53 |
structure TypedefArgs =
|
berghofe@15259
|
54 |
struct
|
berghofe@15259
|
55 |
val name = "HOL/typedef";
|
berghofe@15259
|
56 |
type T = (typ * typ * string * string) Symtab.table;
|
berghofe@15259
|
57 |
val empty = Symtab.empty;
|
berghofe@15259
|
58 |
val copy = I;
|
berghofe@15259
|
59 |
val prep_ext = I;
|
berghofe@15259
|
60 |
val merge = Symtab.merge op =;
|
berghofe@15259
|
61 |
fun print sg _ = ();
|
berghofe@15259
|
62 |
end;
|
berghofe@15259
|
63 |
|
berghofe@15259
|
64 |
structure TypedefData = TheoryDataFun(TypedefArgs);
|
berghofe@15259
|
65 |
|
berghofe@15259
|
66 |
fun put_typedef newT oldT Abs_name Rep_name thy =
|
berghofe@15259
|
67 |
TypedefData.put (Symtab.update_new
|
berghofe@15259
|
68 |
((fst (dest_Type newT), (newT, oldT, Abs_name, Rep_name)),
|
berghofe@15259
|
69 |
TypedefData.get thy)) thy;
|
berghofe@15259
|
70 |
|
berghofe@15259
|
71 |
|
berghofe@15259
|
72 |
|
wenzelm@5104
|
73 |
(** type declarations **)
|
wenzelm@5104
|
74 |
|
wenzelm@5104
|
75 |
fun add_typedecls decls thy =
|
wenzelm@5104
|
76 |
let
|
wenzelm@5104
|
77 |
val full = Sign.full_name (Theory.sign_of thy);
|
wenzelm@5104
|
78 |
|
wenzelm@5104
|
79 |
fun arity_of (raw_name, args, mx) =
|
wenzelm@12338
|
80 |
(full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.typeS, HOLogic.typeS);
|
wenzelm@5104
|
81 |
in
|
wenzelm@8141
|
82 |
if can (Theory.assert_super HOL.thy) thy then
|
wenzelm@8141
|
83 |
thy
|
wenzelm@8141
|
84 |
|> PureThy.add_typedecls decls
|
wenzelm@8141
|
85 |
|> Theory.add_arities_i (map arity_of decls)
|
wenzelm@8141
|
86 |
else thy |> PureThy.add_typedecls decls
|
wenzelm@5104
|
87 |
end;
|
wenzelm@5104
|
88 |
|
wenzelm@5104
|
89 |
|
wenzelm@5104
|
90 |
|
wenzelm@5104
|
91 |
(** type definitions **)
|
wenzelm@5104
|
92 |
|
wenzelm@5697
|
93 |
(* messages *)
|
wenzelm@5697
|
94 |
|
wenzelm@5697
|
95 |
val quiet_mode = ref false;
|
wenzelm@5697
|
96 |
fun message s = if ! quiet_mode then () else writeln s;
|
wenzelm@5697
|
97 |
|
wenzelm@5697
|
98 |
|
wenzelm@11426
|
99 |
(* prove_nonempty -- tactical version *) (*exception ERROR*)
|
wenzelm@4866
|
100 |
|
wenzelm@11827
|
101 |
fun prove_nonempty thy cset goal (witn1_tac, witn_names, witn_thms, witn2_tac) =
|
wenzelm@6383
|
102 |
let
|
wenzelm@6383
|
103 |
val is_def = Logic.is_equals o #prop o Thm.rep_thm;
|
wenzelm@6383
|
104 |
val thms = PureThy.get_thmss thy witn_names @ witn_thms;
|
wenzelm@4866
|
105 |
val tac =
|
wenzelm@11827
|
106 |
witn1_tac THEN
|
wenzelm@4866
|
107 |
TRY (rewrite_goals_tac (filter is_def thms)) THEN
|
wenzelm@4866
|
108 |
TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
|
wenzelm@11827
|
109 |
if_none witn2_tac (TRY (ALLGOALS (CLASET' blast_tac)));
|
wenzelm@4866
|
110 |
in
|
wenzelm@6383
|
111 |
message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
|
wenzelm@11968
|
112 |
Tactic.prove (Theory.sign_of thy) [] [] goal (K tac)
|
wenzelm@6383
|
113 |
end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset));
|
wenzelm@4866
|
114 |
|
wenzelm@4866
|
115 |
|
wenzelm@6383
|
116 |
(* prepare_typedef *)
|
wenzelm@6383
|
117 |
|
wenzelm@6383
|
118 |
fun read_term sg used s =
|
wenzelm@12338
|
119 |
#1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.typeT));
|
wenzelm@4866
|
120 |
|
wenzelm@6383
|
121 |
fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg;
|
wenzelm@6383
|
122 |
|
wenzelm@6383
|
123 |
fun err_in_typedef name =
|
wenzelm@6383
|
124 |
error ("The error(s) above occurred in typedef " ^ quote name);
|
wenzelm@6383
|
125 |
|
wenzelm@11822
|
126 |
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy =
|
wenzelm@4866
|
127 |
let
|
wenzelm@11608
|
128 |
val _ = Theory.requires thy "Typedef" "typedefs";
|
wenzelm@6383
|
129 |
val sign = Theory.sign_of thy;
|
wenzelm@10280
|
130 |
val full = Sign.full_name sign;
|
wenzelm@4866
|
131 |
|
wenzelm@4866
|
132 |
(*rhs*)
|
wenzelm@10280
|
133 |
val full_name = full name;
|
wenzelm@6383
|
134 |
val cset = prep_term sign vs raw_set;
|
wenzelm@6383
|
135 |
val {T = setT, t = set, ...} = Thm.rep_cterm cset;
|
wenzelm@4866
|
136 |
val rhs_tfrees = term_tfrees set;
|
wenzelm@4866
|
137 |
val oldT = HOLogic.dest_setT setT handle TYPE _ =>
|
wenzelm@4866
|
138 |
error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
|
wenzelm@11426
|
139 |
fun mk_nonempty A =
|
wenzelm@11426
|
140 |
HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)));
|
wenzelm@11426
|
141 |
val goal = mk_nonempty set;
|
wenzelm@11744
|
142 |
val vname = take_suffix Symbol.is_digit (Symbol.explode name)
|
wenzelm@14819
|
143 |
|> apfst implode |> apsnd (#1 o Library.read_int);
|
wenzelm@11744
|
144 |
val goal_pat = mk_nonempty (Var (vname, setT));
|
wenzelm@4866
|
145 |
|
wenzelm@4866
|
146 |
(*lhs*)
|
wenzelm@12338
|
147 |
val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.typeS)) vs;
|
wenzelm@4866
|
148 |
val tname = Syntax.type_name t mx;
|
wenzelm@10280
|
149 |
val full_tname = full tname;
|
wenzelm@10280
|
150 |
val newT = Type (full_tname, map TFree lhs_tfrees);
|
wenzelm@4866
|
151 |
|
wenzelm@11744
|
152 |
val (Rep_name, Abs_name) = if_none opt_morphs ("Rep_" ^ name, "Abs_" ^ name);
|
wenzelm@10280
|
153 |
val setC = Const (full_name, setT);
|
wenzelm@10280
|
154 |
val RepC = Const (full Rep_name, newT --> oldT);
|
wenzelm@10280
|
155 |
val AbsC = Const (full Abs_name, oldT --> newT);
|
wenzelm@4866
|
156 |
val x_new = Free ("x", newT);
|
wenzelm@4866
|
157 |
val y_old = Free ("y", oldT);
|
wenzelm@10280
|
158 |
|
wenzelm@11822
|
159 |
val set' = if def then setC else set;
|
wenzelm@4866
|
160 |
|
wenzelm@10280
|
161 |
val typedef_name = "type_definition_" ^ name;
|
wenzelm@10280
|
162 |
val typedefC =
|
wenzelm@10280
|
163 |
Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT);
|
wenzelm@11426
|
164 |
val typedef_prop =
|
wenzelm@11426
|
165 |
Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set'));
|
wenzelm@4866
|
166 |
|
wenzelm@11822
|
167 |
fun typedef_result (theory, nonempty) =
|
wenzelm@6383
|
168 |
theory
|
berghofe@15259
|
169 |
|> put_typedef newT oldT (full Abs_name) (full Rep_name)
|
wenzelm@6383
|
170 |
|> add_typedecls [(t, vs, mx)]
|
wenzelm@6383
|
171 |
|> Theory.add_consts_i
|
wenzelm@11822
|
172 |
((if def then [(name, setT, NoSyn)] else []) @
|
wenzelm@6383
|
173 |
[(Rep_name, newT --> oldT, NoSyn),
|
wenzelm@6383
|
174 |
(Abs_name, oldT --> newT, NoSyn)])
|
wenzelm@11822
|
175 |
|> (if def then (apsnd (Some o hd) oo (PureThy.add_defs_i false o map Thm.no_attributes))
|
wenzelm@11822
|
176 |
[Logic.mk_defpair (setC, set)]
|
wenzelm@11822
|
177 |
else rpair None)
|
wenzelm@14570
|
178 |
|>>> PureThy.add_axioms_i [((typedef_name, typedef_prop),
|
wenzelm@11727
|
179 |
[apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])]
|
wenzelm@14570
|
180 |
|>> Theory.add_finals_i false [RepC, AbsC]
|
wenzelm@14570
|
181 |
|> (fn (theory', (set_def, [type_definition])) =>
|
wenzelm@11822
|
182 |
let
|
wenzelm@11822
|
183 |
fun make th = Drule.standard (th OF [type_definition]);
|
wenzelm@11822
|
184 |
val (theory'', [Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject,
|
wenzelm@11822
|
185 |
Rep_cases, Abs_cases, Rep_induct, Abs_induct]) =
|
wenzelm@12338
|
186 |
theory'
|
wenzelm@12338
|
187 |
|> Theory.add_path name
|
wenzelm@12338
|
188 |
|> PureThy.add_thms
|
wenzelm@11822
|
189 |
([((Rep_name, make Rep), []),
|
wenzelm@11822
|
190 |
((Rep_name ^ "_inverse", make Rep_inverse), []),
|
wenzelm@11822
|
191 |
((Abs_name ^ "_inverse", make Abs_inverse), []),
|
wenzelm@11822
|
192 |
((Rep_name ^ "_inject", make Rep_inject), []),
|
wenzelm@11822
|
193 |
((Abs_name ^ "_inject", make Abs_inject), []),
|
wenzelm@11822
|
194 |
((Rep_name ^ "_cases", make Rep_cases),
|
wenzelm@11822
|
195 |
[RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]),
|
wenzelm@11822
|
196 |
((Abs_name ^ "_cases", make Abs_cases),
|
wenzelm@11822
|
197 |
[RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]),
|
wenzelm@11822
|
198 |
((Rep_name ^ "_induct", make Rep_induct),
|
wenzelm@11822
|
199 |
[RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]),
|
wenzelm@11822
|
200 |
((Abs_name ^ "_induct", make Abs_induct),
|
wenzelm@12338
|
201 |
[RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])])
|
wenzelm@12338
|
202 |
|>> Theory.parent_path;
|
wenzelm@11822
|
203 |
val result = {type_definition = type_definition, set_def = set_def,
|
wenzelm@11822
|
204 |
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
|
wenzelm@11822
|
205 |
Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
|
wenzelm@11822
|
206 |
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
|
wenzelm@11822
|
207 |
in ((theory'', type_definition), result) end);
|
wenzelm@6383
|
208 |
|
wenzelm@4866
|
209 |
|
wenzelm@4866
|
210 |
(* errors *)
|
wenzelm@4866
|
211 |
|
wenzelm@4866
|
212 |
fun show_names pairs = commas_quote (map fst pairs);
|
wenzelm@4866
|
213 |
|
wenzelm@4866
|
214 |
val illegal_vars =
|
wenzelm@4866
|
215 |
if null (term_vars set) andalso null (term_tvars set) then []
|
wenzelm@4866
|
216 |
else ["Illegal schematic variable(s) on rhs"];
|
wenzelm@4866
|
217 |
|
wenzelm@4866
|
218 |
val dup_lhs_tfrees =
|
wenzelm@4866
|
219 |
(case duplicates lhs_tfrees of [] => []
|
wenzelm@4866
|
220 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
|
wenzelm@4866
|
221 |
|
wenzelm@4866
|
222 |
val extra_rhs_tfrees =
|
wenzelm@4866
|
223 |
(case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
|
wenzelm@4866
|
224 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]);
|
wenzelm@4866
|
225 |
|
wenzelm@4866
|
226 |
val illegal_frees =
|
wenzelm@4866
|
227 |
(case term_frees set of [] => []
|
wenzelm@4866
|
228 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
|
wenzelm@4866
|
229 |
|
wenzelm@4866
|
230 |
val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
|
wenzelm@11426
|
231 |
val _ = if null errs then () else error (cat_lines errs);
|
wenzelm@11426
|
232 |
|
wenzelm@11426
|
233 |
(*test theory errors now!*)
|
wenzelm@11426
|
234 |
val test_thy = Theory.copy thy;
|
wenzelm@11727
|
235 |
val _ = (test_thy,
|
wenzelm@11822
|
236 |
setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_result;
|
wenzelm@11426
|
237 |
|
wenzelm@11822
|
238 |
in (cset, goal, goal_pat, typedef_result) end
|
wenzelm@11426
|
239 |
handle ERROR => err_in_typedef name;
|
wenzelm@4866
|
240 |
|
wenzelm@4866
|
241 |
|
wenzelm@6383
|
242 |
(* add_typedef interfaces *)
|
wenzelm@4866
|
243 |
|
wenzelm@11827
|
244 |
fun gen_typedef prep_term def name typ set opt_morphs tac1 names thms tac2 thy =
|
wenzelm@6383
|
245 |
let
|
wenzelm@11822
|
246 |
val (cset, goal, _, typedef_result) =
|
wenzelm@11822
|
247 |
prepare_typedef prep_term def name typ set opt_morphs thy;
|
wenzelm@11827
|
248 |
val non_empty = prove_nonempty thy cset goal (tac1, names, thms, tac2);
|
wenzelm@11822
|
249 |
val ((thy', _), result) = (thy, non_empty) |> typedef_result;
|
wenzelm@11822
|
250 |
in (thy', result) end;
|
wenzelm@4866
|
251 |
|
wenzelm@11827
|
252 |
fun sane_typedef prep_term def opt_name typ set opt_morphs tac =
|
wenzelm@11827
|
253 |
gen_typedef prep_term def
|
wenzelm@11827
|
254 |
(if_none opt_name (#1 typ)) typ set opt_morphs all_tac [] [] (Some tac);
|
wenzelm@11822
|
255 |
|
wenzelm@11822
|
256 |
fun add_typedef_x name typ set names thms tac =
|
wenzelm@11827
|
257 |
#1 o gen_typedef read_term true name typ set None (Tactic.rtac exI 1) names thms tac;
|
wenzelm@11822
|
258 |
|
wenzelm@11822
|
259 |
val add_typedef = sane_typedef read_term;
|
wenzelm@11822
|
260 |
val add_typedef_i = sane_typedef cert_term;
|
wenzelm@4866
|
261 |
|
wenzelm@4866
|
262 |
|
wenzelm@6383
|
263 |
(* typedef_proof interface *)
|
wenzelm@6383
|
264 |
|
wenzelm@13443
|
265 |
fun gen_typedef_proof prep_term ((def, name), typ, set, opt_morphs) int thy =
|
wenzelm@11822
|
266 |
let
|
wenzelm@11822
|
267 |
val (_, goal, goal_pat, att_result) =
|
wenzelm@13443
|
268 |
prepare_typedef prep_term def name typ set opt_morphs thy;
|
wenzelm@11822
|
269 |
val att = #1 o att_result;
|
wenzelm@12876
|
270 |
in thy |> IsarThy.theorem_i Drule.internalK (("", [att]), (goal, ([goal_pat], []))) int end;
|
wenzelm@6383
|
271 |
|
wenzelm@6383
|
272 |
val typedef_proof = gen_typedef_proof read_term;
|
wenzelm@6383
|
273 |
val typedef_proof_i = gen_typedef_proof cert_term;
|
wenzelm@6357
|
274 |
|
wenzelm@6357
|
275 |
|
wenzelm@6383
|
276 |
|
berghofe@15259
|
277 |
(** trivial code generator **)
|
berghofe@15259
|
278 |
|
berghofe@15259
|
279 |
fun typedef_codegen thy gr dep brack t =
|
berghofe@15259
|
280 |
let
|
berghofe@15259
|
281 |
fun mk_fun s T ts =
|
berghofe@15259
|
282 |
let
|
berghofe@15259
|
283 |
val (gr', _) = Codegen.invoke_tycodegen thy dep false (gr, T);
|
berghofe@15259
|
284 |
val (gr'', ps) =
|
berghofe@15259
|
285 |
foldl_map (Codegen.invoke_codegen thy dep true) (gr', ts);
|
berghofe@15259
|
286 |
val id = Codegen.mk_const_id (sign_of thy) s
|
berghofe@15259
|
287 |
in Some (gr'', Codegen.mk_app brack (Pretty.str id) ps) end;
|
berghofe@15259
|
288 |
fun get_name (Type (tname, _)) = tname
|
berghofe@15259
|
289 |
| get_name _ = "";
|
berghofe@15259
|
290 |
fun lookup f T = if_none (apsome f (Symtab.lookup
|
berghofe@15259
|
291 |
(TypedefData.get thy, get_name T))) ""
|
berghofe@15259
|
292 |
in
|
berghofe@15259
|
293 |
(case strip_comb t of
|
berghofe@15259
|
294 |
(Const (s, Type ("fun", [T, U])), ts) =>
|
berghofe@15259
|
295 |
if lookup #4 T = s andalso
|
berghofe@15259
|
296 |
is_none (Codegen.get_assoc_type thy (get_name T))
|
berghofe@15259
|
297 |
then mk_fun s T ts
|
berghofe@15259
|
298 |
else if lookup #3 U = s andalso
|
berghofe@15259
|
299 |
is_none (Codegen.get_assoc_type thy (get_name U))
|
berghofe@15259
|
300 |
then mk_fun s U ts
|
berghofe@15259
|
301 |
else None
|
berghofe@15259
|
302 |
| _ => None)
|
berghofe@15259
|
303 |
end;
|
berghofe@15259
|
304 |
|
berghofe@15259
|
305 |
fun mk_tyexpr [] s = Pretty.str s
|
berghofe@15259
|
306 |
| mk_tyexpr [p] s = Pretty.block [p, Pretty.str (" " ^ s)]
|
berghofe@15259
|
307 |
| mk_tyexpr ps s = Pretty.list "(" (") " ^ s) ps;
|
berghofe@15259
|
308 |
|
berghofe@15259
|
309 |
fun typedef_tycodegen thy gr dep brack (Type (s, Ts)) =
|
berghofe@15259
|
310 |
(case Symtab.lookup (TypedefData.get thy, s) of
|
berghofe@15259
|
311 |
None => None
|
berghofe@15259
|
312 |
| Some (newT as Type (tname, Us), oldT, Abs_name, Rep_name) =>
|
berghofe@15259
|
313 |
if is_some (Codegen.get_assoc_type thy tname) then None else
|
berghofe@15259
|
314 |
let
|
berghofe@15259
|
315 |
val sg = sign_of thy;
|
berghofe@15259
|
316 |
val Abs_id = Codegen.mk_const_id sg Abs_name;
|
berghofe@15259
|
317 |
val Rep_id = Codegen.mk_const_id sg Rep_name;
|
berghofe@15259
|
318 |
val ty_id = Codegen.mk_type_id sg s;
|
berghofe@15259
|
319 |
val (gr', qs) = foldl_map
|
berghofe@15259
|
320 |
(Codegen.invoke_tycodegen thy dep (length Ts = 1)) (gr, Ts);
|
berghofe@15259
|
321 |
val gr'' = Graph.add_edge (Abs_id, dep) gr' handle Graph.UNDEF _ =>
|
berghofe@15259
|
322 |
let
|
berghofe@15259
|
323 |
val (gr'', p :: ps) = foldl_map
|
berghofe@15259
|
324 |
(Codegen.invoke_tycodegen thy Abs_id false)
|
berghofe@15259
|
325 |
(Graph.add_edge (Abs_id, dep)
|
berghofe@15259
|
326 |
(Graph.new_node (Abs_id, (None, "")) gr'), oldT :: Us);
|
berghofe@15259
|
327 |
val s =
|
berghofe@15259
|
328 |
Pretty.string_of (Pretty.block [Pretty.str "datatype ",
|
berghofe@15259
|
329 |
mk_tyexpr ps ty_id,
|
berghofe@15259
|
330 |
Pretty.str " =", Pretty.brk 1, Pretty.str (Abs_id ^ " of"),
|
berghofe@15259
|
331 |
Pretty.brk 1, p, Pretty.str ";"]) ^ "\n\n" ^
|
berghofe@15259
|
332 |
Pretty.string_of (Pretty.block [Pretty.str ("fun " ^ Rep_id),
|
berghofe@15259
|
333 |
Pretty.brk 1, Pretty.str ("(" ^ Abs_id), Pretty.brk 1,
|
berghofe@15259
|
334 |
Pretty.str "x) = x;"]) ^ "\n\n" ^
|
berghofe@15259
|
335 |
(if "term_of" mem !Codegen.mode then
|
berghofe@15259
|
336 |
Pretty.string_of (Pretty.block [Pretty.str "fun ",
|
berghofe@15259
|
337 |
Codegen.mk_term_of sg false newT, Pretty.brk 1,
|
berghofe@15259
|
338 |
Pretty.str ("(" ^ Abs_id), Pretty.brk 1,
|
berghofe@15259
|
339 |
Pretty.str "x) =", Pretty.brk 1,
|
berghofe@15259
|
340 |
Pretty.block [Pretty.str ("Const (\"" ^ Abs_name ^ "\","),
|
berghofe@15259
|
341 |
Pretty.brk 1, Codegen.mk_type false (oldT --> newT),
|
berghofe@15259
|
342 |
Pretty.str ")"], Pretty.str " $", Pretty.brk 1,
|
berghofe@15259
|
343 |
Codegen.mk_term_of sg false oldT, Pretty.brk 1,
|
berghofe@15259
|
344 |
Pretty.str "x;"]) ^ "\n\n"
|
berghofe@15259
|
345 |
else "") ^
|
berghofe@15259
|
346 |
(if "test" mem !Codegen.mode then
|
berghofe@15259
|
347 |
Pretty.string_of (Pretty.block [Pretty.str "fun ",
|
berghofe@15259
|
348 |
Codegen.mk_gen sg false [] "" newT, Pretty.brk 1,
|
berghofe@15259
|
349 |
Pretty.str "i =", Pretty.brk 1,
|
berghofe@15259
|
350 |
Pretty.block [Pretty.str (Abs_id ^ " ("),
|
berghofe@15259
|
351 |
Codegen.mk_gen sg false [] "" oldT, Pretty.brk 1,
|
berghofe@15259
|
352 |
Pretty.str "i);"]]) ^ "\n\n"
|
berghofe@15259
|
353 |
else "")
|
berghofe@15259
|
354 |
in Graph.map_node Abs_id (K (None, s)) gr'' end
|
berghofe@15259
|
355 |
in
|
berghofe@15259
|
356 |
Some (gr'', mk_tyexpr qs ty_id)
|
berghofe@15259
|
357 |
end)
|
berghofe@15259
|
358 |
| typedef_tycodegen thy gr dep brack _ = None;
|
berghofe@15259
|
359 |
|
berghofe@15259
|
360 |
val setup =
|
berghofe@15259
|
361 |
[TypedefData.init,
|
berghofe@15259
|
362 |
Codegen.add_codegen "typedef" typedef_codegen,
|
berghofe@15259
|
363 |
Codegen.add_tycodegen "typedef" typedef_tycodegen];
|
berghofe@15259
|
364 |
|
berghofe@15259
|
365 |
|
berghofe@15259
|
366 |
|
wenzelm@6383
|
367 |
(** outer syntax **)
|
wenzelm@6383
|
368 |
|
wenzelm@6723
|
369 |
local structure P = OuterParse and K = OuterSyntax.Keyword in
|
wenzelm@6383
|
370 |
|
wenzelm@6357
|
371 |
val typedeclP =
|
wenzelm@12624
|
372 |
OuterSyntax.command "typedecl" "type declaration (HOL)" K.thy_decl
|
wenzelm@12876
|
373 |
(P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) =>
|
wenzelm@6357
|
374 |
Toplevel.theory (add_typedecls [(t, vs, mx)])));
|
wenzelm@6357
|
375 |
|
wenzelm@6723
|
376 |
|
wenzelm@6383
|
377 |
val typedef_proof_decl =
|
wenzelm@13443
|
378 |
Scan.optional (P.$$$ "(" |-- P.!!!
|
wenzelm@13443
|
379 |
(((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, Some s)))
|
wenzelm@13443
|
380 |
--| P.$$$ ")")) (true, None) --
|
wenzelm@11744
|
381 |
(P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
|
wenzelm@12876
|
382 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name));
|
wenzelm@6357
|
383 |
|
wenzelm@13443
|
384 |
fun mk_typedef_proof ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
|
wenzelm@13443
|
385 |
typedef_proof ((def, if_none opt_name (Syntax.type_name t mx)), (t, vs, mx), A, morphs);
|
wenzelm@6357
|
386 |
|
wenzelm@6357
|
387 |
val typedefP =
|
wenzelm@6723
|
388 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
|
wenzelm@6383
|
389 |
(typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof)));
|
wenzelm@6357
|
390 |
|
wenzelm@6723
|
391 |
|
wenzelm@11744
|
392 |
val _ = OuterSyntax.add_keywords ["morphisms"];
|
wenzelm@6357
|
393 |
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
|
wenzelm@6357
|
394 |
|
wenzelm@4866
|
395 |
end;
|
wenzelm@6383
|
396 |
|
wenzelm@6383
|
397 |
end;
|