author | wenzelm |
Tue, 12 Jan 1999 13:54:51 +0100 | |
changeset 6092 | d9db67970c73 |
parent 5697 | e816c4f1a396 |
child 6357 | 12448b8f92fb |
permissions | -rw-r--r-- |
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 |
|
5697 | 10 |
val quiet_mode: bool ref |
5104 | 11 |
val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory |
4866 | 12 |
val prove_nonempty: cterm -> thm list -> tactic option -> thm |
13 |
val add_typedef: string -> bstring * string list * mixfix -> |
|
14 |
string -> string list -> thm list -> tactic option -> theory -> theory |
|
15 |
val add_typedef_i: string -> bstring * string list * mixfix -> |
|
16 |
term -> string list -> thm list -> tactic option -> theory -> theory |
|
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
17 |
val add_typedef_i_no_def: string -> bstring * string list * mixfix -> |
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
18 |
term -> string list -> thm list -> tactic option -> theory -> theory |
4866 | 19 |
end; |
20 |
||
21 |
structure TypedefPackage: TYPEDEF_PACKAGE = |
|
22 |
struct |
|
23 |
||
24 |
||
5104 | 25 |
(** type declarations **) |
26 |
||
27 |
fun add_typedecls decls thy = |
|
28 |
let |
|
29 |
val full = Sign.full_name (Theory.sign_of thy); |
|
30 |
||
31 |
fun arity_of (raw_name, args, mx) = |
|
32 |
(full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS); |
|
33 |
in |
|
34 |
thy |
|
35 |
|> PureThy.add_typedecls decls |
|
36 |
|> Theory.add_arities_i (map arity_of decls) |
|
37 |
end; |
|
38 |
||
39 |
||
40 |
||
41 |
(** type definitions **) |
|
42 |
||
5697 | 43 |
(* messages *) |
44 |
||
45 |
val quiet_mode = ref false; |
|
46 |
fun message s = if ! quiet_mode then () else writeln s; |
|
47 |
||
48 |
||
4866 | 49 |
(* prove non-emptyness of a set *) (*exception ERROR*) |
50 |
||
51 |
val is_def = Logic.is_equals o #prop o rep_thm; |
|
52 |
||
53 |
fun prove_nonempty cset thms usr_tac = |
|
54 |
let |
|
55 |
val {T = setT, t = set, maxidx, sign} = rep_cterm cset; |
|
56 |
val T = HOLogic.dest_setT setT; |
|
57 |
val goal = cterm_of sign |
|
58 |
(HOLogic.mk_Trueprop (HOLogic.mk_mem (Var (("x", maxidx + 1), T), set))); |
|
59 |
val tac = |
|
60 |
TRY (rewrite_goals_tac (filter is_def thms)) THEN |
|
61 |
TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN |
|
62 |
if_none usr_tac (TRY (ALLGOALS (CLASET' blast_tac))); |
|
63 |
in |
|
64 |
prove_goalw_cterm [] goal (K [tac]) |
|
65 |
end |
|
66 |
handle ERROR => |
|
67 |
error ("Failed to prove nonemptiness of " ^ quote (string_of_cterm cset)); |
|
68 |
||
69 |
||
70 |
(* gen_add_typedef *) |
|
71 |
||
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
72 |
fun gen_add_typedef prep_term no_def name (t, vs, mx) raw_set axms thms usr_tac thy = |
4866 | 73 |
let |
4970 | 74 |
val _ = Theory.requires thy "Set" "typedefs"; |
4866 | 75 |
val sign = sign_of thy; |
76 |
val full_name = Sign.full_name sign; |
|
77 |
||
78 |
(*rhs*) |
|
79 |
val cset = prep_term sign raw_set; |
|
80 |
val {T = setT, t = set, ...} = rep_cterm cset; |
|
81 |
val rhs_tfrees = term_tfrees set; |
|
82 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
|
83 |
error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT)); |
|
84 |
||
85 |
(*lhs*) |
|
86 |
val lhs_tfrees = |
|
87 |
map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs; |
|
88 |
||
89 |
val tname = Syntax.type_name t mx; |
|
90 |
val newT = Type (full_name tname, map TFree lhs_tfrees); |
|
91 |
||
92 |
val Rep_name = "Rep_" ^ name; |
|
93 |
val Abs_name = "Abs_" ^ name; |
|
94 |
val setC = Const (full_name name, setT); |
|
95 |
val RepC = Const (full_name Rep_name, newT --> oldT); |
|
96 |
val AbsC = Const (full_name Abs_name, oldT --> newT); |
|
97 |
val x_new = Free ("x", newT); |
|
98 |
val y_old = Free ("y", oldT); |
|
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
99 |
val set' = if no_def then set else setC; |
4866 | 100 |
|
101 |
(*axioms*) |
|
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
102 |
val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, set')); |
4866 | 103 |
val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new)); |
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
104 |
val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, set')), |
4866 | 105 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old))); |
106 |
||
107 |
||
108 |
(* errors *) |
|
109 |
||
110 |
fun show_names pairs = commas_quote (map fst pairs); |
|
111 |
||
112 |
val illegal_vars = |
|
113 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
114 |
else ["Illegal schematic variable(s) on rhs"]; |
|
115 |
||
116 |
val dup_lhs_tfrees = |
|
117 |
(case duplicates lhs_tfrees of [] => [] |
|
118 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
|
119 |
||
120 |
val extra_rhs_tfrees = |
|
121 |
(case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => [] |
|
122 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
|
123 |
||
124 |
val illegal_frees = |
|
125 |
(case term_frees set of [] => [] |
|
126 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
127 |
||
128 |
val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees; |
|
129 |
in |
|
130 |
if null errs then () |
|
131 |
else error (cat_lines errs); |
|
132 |
||
5697 | 133 |
message ("Proving nonemptiness of " ^ quote name ^ " ..."); |
4866 | 134 |
prove_nonempty cset (map (get_axiom thy) axms @ thms) usr_tac; |
135 |
||
136 |
thy |
|
4932 | 137 |
|> PureThy.add_typedecls [(t, vs, mx)] |
4866 | 138 |
|> Theory.add_arities_i |
4932 | 139 |
[(full_name tname, replicate (length vs) HOLogic.termS, HOLogic.termS)] |
4866 | 140 |
|> Theory.add_consts_i |
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
141 |
((if no_def then [] else [(name, setT, NoSyn)]) @ |
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
142 |
[(Rep_name, newT --> oldT, NoSyn), |
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
143 |
(Abs_name, oldT --> newT, NoSyn)]) |
6092 | 144 |
|> (if no_def then I else (PureThy.add_defs_i o map Thm.no_attributes) |
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
145 |
[(name ^ "_def", Logic.mk_equals (setC, set))]) |
6092 | 146 |
|> (PureThy.add_axioms_i o map Thm.no_attributes) |
4866 | 147 |
[(Rep_name, rep_type), |
148 |
(Rep_name ^ "_inverse", rep_type_inv), |
|
149 |
(Abs_name ^ "_inverse", abs_type_inv)] |
|
150 |
end |
|
151 |
handle ERROR => |
|
152 |
error ("The error(s) above occurred in typedef " ^ quote name); |
|
153 |
||
154 |
||
155 |
(* external interfaces *) |
|
156 |
||
157 |
fun read_term sg str = |
|
158 |
read_cterm sg (str, HOLogic.termTVar); |
|
159 |
||
160 |
fun cert_term sg tm = |
|
161 |
cterm_of sg tm handle TERM (msg, _) => error msg; |
|
162 |
||
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
163 |
val add_typedef = gen_add_typedef read_term false; |
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
164 |
val add_typedef_i = gen_add_typedef cert_term false; |
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
165 |
val add_typedef_i_no_def = gen_add_typedef cert_term true; |
4866 | 166 |
|
167 |
||
168 |
end; |