4866
|
1 |
(* Title: HOL/Tools/typedef_package.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Gordon/HOL style type definitions.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature TYPEDEF_PACKAGE =
|
|
9 |
sig
|
|
10 |
val prove_nonempty: cterm -> thm list -> tactic option -> thm
|
|
11 |
val add_typedef: string -> bstring * string list * mixfix ->
|
|
12 |
string -> string list -> thm list -> tactic option -> theory -> theory
|
|
13 |
val add_typedef_i: string -> bstring * string list * mixfix ->
|
|
14 |
term -> string list -> thm list -> tactic option -> theory -> theory
|
|
15 |
end;
|
|
16 |
|
|
17 |
structure TypedefPackage: TYPEDEF_PACKAGE =
|
|
18 |
struct
|
|
19 |
|
|
20 |
|
|
21 |
(* prove non-emptyness of a set *) (*exception ERROR*)
|
|
22 |
|
|
23 |
val is_def = Logic.is_equals o #prop o rep_thm;
|
|
24 |
|
|
25 |
fun prove_nonempty cset thms usr_tac =
|
|
26 |
let
|
|
27 |
val {T = setT, t = set, maxidx, sign} = rep_cterm cset;
|
|
28 |
val T = HOLogic.dest_setT setT;
|
|
29 |
val goal = cterm_of sign
|
|
30 |
(HOLogic.mk_Trueprop (HOLogic.mk_mem (Var (("x", maxidx + 1), T), set)));
|
|
31 |
val tac =
|
|
32 |
TRY (rewrite_goals_tac (filter is_def thms)) THEN
|
|
33 |
TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
|
|
34 |
if_none usr_tac (TRY (ALLGOALS (CLASET' blast_tac)));
|
|
35 |
in
|
|
36 |
prove_goalw_cterm [] goal (K [tac])
|
|
37 |
end
|
|
38 |
handle ERROR =>
|
|
39 |
error ("Failed to prove nonemptiness of " ^ quote (string_of_cterm cset));
|
|
40 |
|
|
41 |
|
|
42 |
(* gen_add_typedef *)
|
|
43 |
|
|
44 |
fun gen_add_typedef prep_term name (t, vs, mx) raw_set axms thms usr_tac thy =
|
|
45 |
let
|
4970
|
46 |
val _ = Theory.requires thy "Set" "typedefs";
|
4866
|
47 |
val sign = sign_of thy;
|
|
48 |
val full_name = Sign.full_name sign;
|
|
49 |
|
|
50 |
(*rhs*)
|
|
51 |
val cset = prep_term sign raw_set;
|
|
52 |
val {T = setT, t = set, ...} = rep_cterm cset;
|
|
53 |
val rhs_tfrees = term_tfrees set;
|
|
54 |
val oldT = HOLogic.dest_setT setT handle TYPE _ =>
|
|
55 |
error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
|
|
56 |
|
|
57 |
(*lhs*)
|
|
58 |
val lhs_tfrees =
|
|
59 |
map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs;
|
|
60 |
|
|
61 |
val tname = Syntax.type_name t mx;
|
|
62 |
val newT = Type (full_name tname, map TFree lhs_tfrees);
|
|
63 |
|
|
64 |
val Rep_name = "Rep_" ^ name;
|
|
65 |
val Abs_name = "Abs_" ^ name;
|
|
66 |
val setC = Const (full_name name, setT);
|
|
67 |
val RepC = Const (full_name Rep_name, newT --> oldT);
|
|
68 |
val AbsC = Const (full_name Abs_name, oldT --> newT);
|
|
69 |
val x_new = Free ("x", newT);
|
|
70 |
val y_old = Free ("y", oldT);
|
|
71 |
|
|
72 |
(*axioms*)
|
|
73 |
val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, setC));
|
|
74 |
val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new));
|
|
75 |
val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, setC)),
|
|
76 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old)));
|
|
77 |
|
|
78 |
|
|
79 |
(* errors *)
|
|
80 |
|
|
81 |
fun show_names pairs = commas_quote (map fst pairs);
|
|
82 |
|
|
83 |
val illegal_vars =
|
|
84 |
if null (term_vars set) andalso null (term_tvars set) then []
|
|
85 |
else ["Illegal schematic variable(s) on rhs"];
|
|
86 |
|
|
87 |
val dup_lhs_tfrees =
|
|
88 |
(case duplicates lhs_tfrees of [] => []
|
|
89 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
|
|
90 |
|
|
91 |
val extra_rhs_tfrees =
|
|
92 |
(case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
|
|
93 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]);
|
|
94 |
|
|
95 |
val illegal_frees =
|
|
96 |
(case term_frees set of [] => []
|
|
97 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
|
|
98 |
|
|
99 |
val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
|
|
100 |
in
|
|
101 |
if null errs then ()
|
|
102 |
else error (cat_lines errs);
|
|
103 |
|
|
104 |
writeln("Proving nonemptiness of " ^ quote name ^ " ...");
|
|
105 |
prove_nonempty cset (map (get_axiom thy) axms @ thms) usr_tac;
|
|
106 |
|
|
107 |
thy
|
4932
|
108 |
|> PureThy.add_typedecls [(t, vs, mx)]
|
4866
|
109 |
|> Theory.add_arities_i
|
4932
|
110 |
[(full_name tname, replicate (length vs) HOLogic.termS, HOLogic.termS)]
|
4866
|
111 |
|> Theory.add_consts_i
|
|
112 |
[(name, setT, NoSyn),
|
|
113 |
(Rep_name, newT --> oldT, NoSyn),
|
|
114 |
(Abs_name, oldT --> newT, NoSyn)]
|
|
115 |
|> (PureThy.add_defs_i o map Attribute.none)
|
|
116 |
[(name ^ "_def", Logic.mk_equals (setC, set))]
|
|
117 |
|> (PureThy.add_axioms_i o map Attribute.none)
|
|
118 |
[(Rep_name, rep_type),
|
|
119 |
(Rep_name ^ "_inverse", rep_type_inv),
|
|
120 |
(Abs_name ^ "_inverse", abs_type_inv)]
|
|
121 |
end
|
|
122 |
handle ERROR =>
|
|
123 |
error ("The error(s) above occurred in typedef " ^ quote name);
|
|
124 |
|
|
125 |
|
|
126 |
(* external interfaces *)
|
|
127 |
|
|
128 |
fun read_term sg str =
|
|
129 |
read_cterm sg (str, HOLogic.termTVar);
|
|
130 |
|
|
131 |
fun cert_term sg tm =
|
|
132 |
cterm_of sg tm handle TERM (msg, _) => error msg;
|
|
133 |
|
|
134 |
val add_typedef = gen_add_typedef read_term;
|
|
135 |
val add_typedef_i = gen_add_typedef cert_term;
|
|
136 |
|
|
137 |
|
|
138 |
end;
|