author | wenzelm |
Wed, 25 Jun 2025 16:35:25 +0200 | |
changeset 82768 | 8f866fd6fae1 |
parent 81962 | e506e636c724 |
permissions | -rw-r--r-- |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
1 |
(* Title: HOL/Import/import_rule.ML |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
2 |
Author: Cezary Kaliszyk, University of Innsbruck |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
3 |
Author: Alexander Krauss, QAware GmbH |
81933 | 4 |
Author: Makarius |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
5 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
6 |
Importer proof rules and processing of lines and files. |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
7 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
8 |
Based on earlier code by Steven Obua and Sebastian Skalberg. |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
9 |
*) |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
10 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
11 |
signature IMPORT_RULE = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
12 |
sig |
81926 | 13 |
val trace : bool Config.T |
81847 | 14 |
val import_file : Path.T -> theory -> theory |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
15 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
16 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
17 |
structure Import_Rule: IMPORT_RULE = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
18 |
struct |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
19 |
|
81926 | 20 |
(* tracing *) |
21 |
||
22 |
val trace = Attrib.setup_config_bool \<^binding>\<open>import_trace\<close> (K false) |
|
23 |
||
24 |
type name = {hol: string, isabelle: string} |
|
25 |
||
26 |
fun print_name {hol, isabelle} = |
|
27 |
if hol = isabelle then quote hol |
|
28 |
else quote hol ^ " = " ^ quote isabelle |
|
29 |
||
30 |
fun print_item kind name = |
|
31 |
Markup.markup Markup.keyword1 kind ^ " " ^ print_name name |
|
32 |
||
33 |
fun tracing_item thy kind name = |
|
34 |
if Config.get_global thy trace then tracing (print_item kind name) else () |
|
35 |
||
36 |
||
37 |
||
81909 | 38 |
(** primitive rules of HOL Light **) |
39 |
||
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
40 |
fun to_obj_eq th = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
41 |
let |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
42 |
val (t, u) = Thm.dest_equals (Thm.cprop_of th) |
81866 | 43 |
val A = Thm.ctyp_of_cterm t |
81961 | 44 |
val rl = \<^instantiate>\<open>(no_beta) 'a = A and t and u in lemma \<open>t \<equiv> u \<Longrightarrow> t = u\<close> by simp\<close> |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
45 |
in |
81866 | 46 |
Thm.implies_elim rl th |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
47 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
48 |
|
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
49 |
fun to_meta_eq th = |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
50 |
let |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
51 |
val (t, u) = Thm.dest_binop (HOLogic.dest_judgment (Thm.cprop_of th)) |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
52 |
val A = Thm.ctyp_of_cterm t |
81961 | 53 |
val rl = \<^instantiate>\<open>(no_beta) 'a = A and t and u in lemma \<open>t = u \<Longrightarrow> t \<equiv> u\<close> by simp\<close> |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
54 |
in |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
55 |
Thm.implies_elim rl th |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
56 |
end |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
57 |
|
81962 | 58 |
|
59 |
(* basic logic *) |
|
60 |
||
61 |
fun refl t = |
|
62 |
\<^instantiate>\<open>(no_beta) 'a = \<open>Thm.ctyp_of_cterm t\<close> and t in lemma \<open>t = t\<close> by (fact refl)\<close> |
|
63 |
||
64 |
fun trans th1 th2 = |
|
65 |
Thm.transitive (to_meta_eq th1) (to_meta_eq th2) |> to_obj_eq |
|
66 |
||
67 |
fun mk_comb th1 th2 = |
|
68 |
Thm.combination (to_meta_eq th1) (to_meta_eq th2) |> to_obj_eq |
|
69 |
||
70 |
fun abs x th = |
|
71 |
to_meta_eq th |> Thm.abstract_rule (Term.term_name (Thm.term_of x)) x |> to_obj_eq |
|
72 |
||
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
73 |
fun beta t = Thm.beta_conversion false t |> to_obj_eq |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
74 |
|
81962 | 75 |
val assume = Thm.assume_cterm o HOLogic.mk_judgment |
76 |
||
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
77 |
fun eq_mp th1 th2 = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
78 |
let |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
79 |
val (Q, P) = Thm.dest_binop (HOLogic.dest_judgment (Thm.cprop_of th1)) |
81961 | 80 |
val rl = \<^instantiate>\<open>(no_beta) P and Q in lemma \<open>Q = P \<Longrightarrow> Q \<Longrightarrow> P\<close> by (fact iffD1)\<close> |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
81 |
in |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
82 |
Thm.implies_elim (Thm.implies_elim rl th1) th2 |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
83 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
84 |
|
81962 | 85 |
fun deduct_antisym_rule th1 th2 = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
86 |
let |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
87 |
val Q = Thm.cprop_of th1 |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
88 |
val P = Thm.cprop_of th2 |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
89 |
val th1' = Thm.implies_intr P th1 |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
90 |
val th2' = Thm.implies_intr Q th2 |
81944 | 91 |
val rl = |
81961 | 92 |
\<^instantiate>\<open>(no_beta) |
93 |
P = \<open>HOLogic.dest_judgment P\<close> and |
|
94 |
Q = \<open>HOLogic.dest_judgment Q\<close> |
|
95 |
in lemma \<open>(P \<Longrightarrow> Q) \<Longrightarrow> (Q \<Longrightarrow> P) \<Longrightarrow> Q = P\<close> by (rule iffI)\<close> |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
96 |
in |
81961 | 97 |
Thm.implies_elim (Thm.implies_elim rl th1') th2' |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
98 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
99 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
100 |
fun conj1 th = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
101 |
let |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
102 |
val (P, Q) = Thm.dest_binop (HOLogic.dest_judgment (Thm.cprop_of th)) |
81961 | 103 |
val rl = \<^instantiate>\<open>(no_beta) P and Q in lemma \<open>P \<and> Q \<Longrightarrow> P\<close> by (fact conjunct1)\<close> |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
104 |
in |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
105 |
Thm.implies_elim rl th |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
106 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
107 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
108 |
fun conj2 th = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
109 |
let |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
110 |
val (P, Q) = Thm.dest_binop (HOLogic.dest_judgment (Thm.cprop_of th)) |
81961 | 111 |
val rl = \<^instantiate>\<open>(no_beta) P and Q in lemma \<open>P \<and> Q \<Longrightarrow> Q\<close> by (fact conjunct2)\<close> |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
112 |
in |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
113 |
Thm.implies_elim rl th |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
114 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
115 |
|
81909 | 116 |
|
117 |
(* instantiation *) |
|
118 |
||
81852 | 119 |
fun freezeT thy th = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
120 |
let |
81857 | 121 |
fun add (v as ((a, _), S)) tvars = |
122 |
if TVars.defined tvars v then tvars |
|
123 |
else TVars.add (v, Thm.global_ctyp_of thy (TFree (a, S))) tvars |
|
124 |
val tyinst = |
|
125 |
TVars.build (Thm.prop_of th |> (fold_types o fold_atyps) (fn TVar v => add v | _ => I)) |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
126 |
in |
81857 | 127 |
Thm.instantiate (tyinst, Vars.empty) th |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
128 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
129 |
|
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
130 |
fun freeze' th = |
81858
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
131 |
let |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
132 |
val vars = Vars.build (th |> Thm.add_vars) |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
133 |
val inst = vars |> Vars.map (fn _ => fn v => |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
134 |
let |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
135 |
val Var ((x, _), _) = Thm.term_of v |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
136 |
val ty = Thm.ctyp_of_cterm v |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
137 |
in Thm.free (x, ty) end) |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
138 |
in |
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
139 |
Thm.instantiate (TVars.empty, inst) th |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
140 |
end |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
141 |
|
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
142 |
fun freeze thy = freezeT thy #> freeze'; |
81858
81f3adce1eda
minor performance tuning: more elementary operations;
wenzelm
parents:
81857
diff
changeset
|
143 |
|
81937 | 144 |
fun inst_type theta = |
81909 | 145 |
let |
146 |
val tyinst = |
|
81937 | 147 |
TFrees.build (theta |> fold (fn (a, b) => |
81909 | 148 |
TFrees.add (Term.dest_TFree (Thm.typ_of a), b))) |
149 |
in |
|
150 |
Thm.instantiate_frees (tyinst, Frees.empty) |
|
151 |
end |
|
152 |
||
81937 | 153 |
fun inst theta th = |
81909 | 154 |
let |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
155 |
val inst = |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
156 |
Frees.build (theta |> fold (fn (a, b) => |
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
157 |
Frees.add (Term.dest_Free (Thm.term_of a), b))) |
81909 | 158 |
in |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
159 |
Thm.instantiate_frees (TFrees.empty, inst) th |
81909 | 160 |
end |
161 |
||
162 |
||
163 |
(* constant definitions *) |
|
164 |
||
81926 | 165 |
fun def' (name as {isabelle = c, ...}) rhs thy = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
166 |
let |
81926 | 167 |
val _ = tracing_item thy "const" name; |
81840 | 168 |
val b = Binding.name c |
81852 | 169 |
val ty = type_of rhs |
170 |
val thy1 = Sign.add_consts [(b, ty, NoSyn)] thy |
|
171 |
val eq = Logic.mk_equals (Const (Sign.full_name thy1 b, ty), rhs) |
|
172 |
val (th, thy2) = Global_Theory.add_def (Binding.suffix_name "_hldef" b, eq) thy1 |
|
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
173 |
val def_thm = freezeT thy1 th |> to_obj_eq |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
174 |
in |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
175 |
(def_thm, thy2) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
176 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
177 |
|
81835 | 178 |
fun mdef thy name = |
81866 | 179 |
(case Import_Data.get_const_def thy name of |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
180 |
SOME th => th |
81866 | 181 |
| NONE => error ("Constant mapped, but no definition: " ^ quote name)) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
182 |
|
81926 | 183 |
fun def (name as {isabelle = c, ...}) rhs thy = |
81861 | 184 |
if is_some (Import_Data.get_const_def thy c) then |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
185 |
(warning ("Const mapped, but def provided: " ^ quote c); (freeze thy (mdef thy c), thy)) |
81926 | 186 |
else def' name (Thm.term_of rhs) thy |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
187 |
|
81909 | 188 |
|
189 |
(* type definitions *) |
|
190 |
||
81861 | 191 |
fun typedef_hol2hollight A B rep abs pred a r = |
81961 | 192 |
\<^instantiate>\<open>(no_beta) 'a = A and 'b = B and Rep = rep and Abs = abs and P = pred and a and r |
193 |
in lemma "type_definition Rep Abs (Collect P) \<Longrightarrow> Abs (Rep a) = a \<and> P r = (Rep (Abs r) = r)" |
|
81829 | 194 |
by (metis type_definition.Rep_inverse type_definition.Abs_inverse |
81961 | 195 |
type_definition.Rep mem_Collect_eq)\<close> |
81829 | 196 |
|
81861 | 197 |
fun typedef_hollight th = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
198 |
let |
81866 | 199 |
val ((rep, abs), P) = |
81943 | 200 |
Thm.dest_comb (HOLogic.dest_judgment (Thm.cprop_of th)) |
81866 | 201 |
|>> (Thm.dest_comb #>> Thm.dest_arg) |
202 |
||> Thm.dest_arg |
|
203 |
val [A, B] = Thm.dest_ctyp (Thm.ctyp_of_cterm rep) |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
204 |
in |
81866 | 205 |
typedef_hol2hollight A B rep abs P (Thm.free ("a", A)) (Thm.free ("r", B)) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
206 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
207 |
|
81950 | 208 |
fun tydef' (name as {isabelle = tycname, ...}) abs_name rep_name P t witness thy = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
209 |
let |
81926 | 210 |
val _ = tracing_item thy "type" name; |
211 |
||
81950 | 212 |
val T = Thm.ctyp_of_cterm t |
81949 | 213 |
val nonempty = |
81961 | 214 |
\<^instantiate>\<open>(no_beta) 'a = T and P and t |
215 |
in lemma "P t \<Longrightarrow> \<exists>x. x \<in> Collect P" by auto\<close> |
|
81950 | 216 |
|> Thm.elim_implies witness |
217 |
val \<^Const_>\<open>Trueprop for \<^Const_>\<open>Ex _ for \<open>Abs (_, _, \<^Const_>\<open>Set.member _ for _ set\<close>)\<close>\<close>\<close> = |
|
218 |
Thm.concl_of nonempty |
|
219 |
||
220 |
val tfrees = Term.add_tfrees set [] |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
221 |
val tnames = sort_strings (map fst tfrees) |
61110 | 222 |
val typedef_bindings = |
62513
702085ca8564
take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents:
62436
diff
changeset
|
223 |
{Rep_name = Binding.name rep_name, |
702085ca8564
take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents:
62436
diff
changeset
|
224 |
Abs_name = Binding.name abs_name, |
702085ca8564
take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents:
62436
diff
changeset
|
225 |
type_definition_name = Binding.name ("type_definition_" ^ tycname)} |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
226 |
val ((_, typedef_info), thy') = |
81947 | 227 |
Typedef.add_typedef_global {overloaded = false} |
81950 | 228 |
(Binding.name tycname, map (rpair dummyS) tnames, NoSyn) set |
81949 | 229 |
(SOME typedef_bindings) (fn ctxt => resolve_tac ctxt [nonempty] 1) thy |
81855 | 230 |
val aty = Thm.global_ctyp_of thy' (#abs_type (#1 typedef_info)) |
60648 | 231 |
val th = freezeT thy' (#type_definition (#2 typedef_info)) |
81946
ee680c69de38
misc tuning: prefer specific variants of Thm.dest_comb;
wenzelm
parents:
81944
diff
changeset
|
232 |
val (rep, abs) = Thm.dest_binop (Thm.dest_fun (HOLogic.dest_judgment (Thm.cprop_of th))) |
81861 | 233 |
val [A, B] = Thm.dest_ctyp (Thm.ctyp_of_cterm rep) |
81950 | 234 |
val typedef_th = typedef_hol2hollight A B rep abs P (Thm.free ("a", aty)) (Thm.free ("r", T)) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
235 |
in |
81855 | 236 |
(typedef_th OF [#type_definition (#2 typedef_info)], thy') |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
237 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
238 |
|
81835 | 239 |
fun mtydef thy name = |
81866 | 240 |
(case Import_Data.get_typ_def thy name of |
81948
0e2f019477e2
more direct emulation of HOL Light inferences: prefer Pure rules over HOL thms;
wenzelm
parents:
81947
diff
changeset
|
241 |
SOME th => Thm.implies_elim (typedef_hollight th) th |
81866 | 242 |
| NONE => error ("Type mapped, but no tydef thm registered: " ^ quote name)) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
243 |
|
81926 | 244 |
fun tydef (name as {hol = tycname, ...}) abs_name rep_name P t td_th thy = |
81866 | 245 |
if is_some (Import_Data.get_typ_def thy tycname) then |
246 |
(warning ("Type mapped but proofs provided: " ^ quote tycname); (mtydef thy tycname, thy)) |
|
81926 | 247 |
else tydef' name abs_name rep_name P t td_th thy |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
248 |
|
81909 | 249 |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
250 |
|
81909 | 251 |
(** importer **) |
252 |
||
253 |
(* basic entities *) |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
254 |
|
81926 | 255 |
fun make_name hol = |
256 |
{hol = hol, isabelle = String.translate (fn #"." => "dot" | c => Char.toString c) hol} |
|
81831 | 257 |
|
81932 | 258 |
fun make_bound a = |
259 |
(case try (unprefix "_") a of |
|
260 |
SOME b => if forall_string Symbol.is_ascii_digit b then "u" else b |
|
261 |
| NONE => a); |
|
262 |
||
81926 | 263 |
fun make_free x ty = Thm.free (#isabelle (make_name x), ty); |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
264 |
|
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
265 |
fun make_tfree thy a = |
81831 | 266 |
let val b = "'" ^ String.translate (fn #"?" => "t" | c => Char.toString c) a |
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
267 |
in Thm.global_ctyp_of thy (TFree (b, \<^sort>\<open>type\<close>)) end |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
268 |
|
81908 | 269 |
fun make_type thy c args = |
81837 | 270 |
let |
271 |
val d = |
|
272 |
(case Import_Data.get_typ_map thy c of |
|
273 |
SOME d => d |
|
81926 | 274 |
| NONE => Sign.full_bname thy (#isabelle (make_name c))) |
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
275 |
val T = Thm.global_ctyp_of thy (Type (d, replicate (length args) dummyT)) |
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
276 |
in Thm.make_ctyp T args end |
81837 | 277 |
|
81908 | 278 |
fun make_const thy c ty = |
81831 | 279 |
let |
280 |
val d = |
|
81835 | 281 |
(case Import_Data.get_const_map thy c of |
81831 | 282 |
SOME d => d |
81926 | 283 |
| NONE => Sign.full_bname thy (#isabelle (make_name c))) |
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
284 |
in Thm.global_cterm_of thy (Const (d, Thm.typ_of ty)) end |
81831 | 285 |
|
81942
da3c3948a39c
clarified signature: more uniform cterm operations, without context;
wenzelm
parents:
81938
diff
changeset
|
286 |
val make_thm = Skip_Proof.make_thm_cterm o HOLogic.mk_judgment |
81908 | 287 |
|
81854 | 288 |
|
81909 | 289 |
(* import file *) |
290 |
||
291 |
local |
|
292 |
||
81854 | 293 |
datatype state = |
294 |
State of theory * (ctyp Inttab.table * int) * (cterm Inttab.table * int) * (thm Inttab.table * int) |
|
295 |
||
296 |
fun init_state thy = State (thy, (Inttab.empty, 0), (Inttab.empty, 0), (Inttab.empty, 0)) |
|
297 |
||
81913 | 298 |
fun get (tab, reg) s = |
81854 | 299 |
(case Int.fromString s of |
81859
6cc57bd46179
clarified exceptions and messages: use "error" only for user-errors, not system failures;
wenzelm
parents:
81858
diff
changeset
|
300 |
NONE => raise Fail "get: not a number" |
81854 | 301 |
| SOME i => |
302 |
(case Inttab.lookup tab (Int.abs i) of |
|
81859
6cc57bd46179
clarified exceptions and messages: use "error" only for user-errors, not system failures;
wenzelm
parents:
81858
diff
changeset
|
303 |
NONE => raise Fail "get: lookup failed" |
81913 | 304 |
| SOME res => (res, (if i < 0 then Inttab.delete (Int.abs i) tab else tab, reg)))) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
305 |
|
81854 | 306 |
fun get_theory (State (thy, _, _, _)) = thy; |
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
307 |
val theory = `get_theory; |
81913 | 308 |
fun theory_op f (State (thy, a, b, c)) = let val (y, thy') = f thy in (y, State (thy', a, b, c)) end; |
81854 | 309 |
|
310 |
fun typ i (State (thy, a, b, c)) = let val (i, a') = get a i in (i, State (thy, a', b, c)) end |
|
311 |
fun term i (State (thy, a, b, c)) = let val (i, b') = get b i in (i, State (thy, a, b', c)) end |
|
312 |
fun thm i (State (thy, a, b, c)) = let val (i, c') = get c i in (i, State (thy, a, b, c')) end |
|
313 |
||
81908 | 314 |
val typs = fold_map typ |
315 |
val terms = fold_map term |
|
316 |
||
81913 | 317 |
fun set (tab, reg) res = (Inttab.update_new (reg + 1, res) tab, reg + 1) |
81854 | 318 |
fun set_typ ty (State (thy, a, b, c)) = State (thy, set a ty, b, c) |
319 |
fun set_term tm (State (thy, a, b, c)) = State (thy, a, set b tm, c) |
|
320 |
fun set_thm th (State (thy, a, b, c)) = State (thy, a, b, set c th) |
|
321 |
||
81911 | 322 |
fun stored_thm name (State (thy, a, b, c)) = |
323 |
let val th = freeze thy (Global_Theory.get_thm thy name) |
|
324 |
in State (thy, a, b, set c th) end |
|
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
325 |
|
81913 | 326 |
fun store_thm name (State (thy, a, b, c as (tab, reg))) = |
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
327 |
let |
81926 | 328 |
val _ = tracing_item thy "thm" name; |
329 |
||
81912 | 330 |
val th = |
81913 | 331 |
(case Inttab.lookup tab reg of |
332 |
NONE => raise Fail "store_thm: lookup failed" |
|
81912 | 333 |
| SOME th0 => Drule.export_without_context_open th0) |
334 |
||
335 |
val tvars = TVars.build (Thm.fold_terms {hyps = false} TVars.add_tvars th); |
|
336 |
val names = Name.invent_global_types (TVars.size tvars) |
|
337 |
val tyinst = |
|
338 |
TVars.build (fold2 |
|
339 |
(fn v as ((_, i), S) => fn b => TVars.add (v, Thm.global_ctyp_of thy (TVar ((b, i), S)))) |
|
340 |
(TVars.list_set tvars) names) |
|
341 |
||
342 |
val th' = Thm.instantiate (tyinst, Vars.empty) th |
|
81926 | 343 |
val thy' = #2 (Global_Theory.add_thm ((Binding.name (#isabelle name), th'), []) thy) |
81906
016e27e10758
misc tuning and clarification: prefer state operations, avoid redundant ctyp_of/cterm_of;
wenzelm
parents:
81905
diff
changeset
|
344 |
in State (thy', a, b, c) end |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
345 |
|
81849 | 346 |
fun pair_list (x :: y :: zs) = ((x, y) :: pair_list zs) |
347 |
| pair_list [] = [] |
|
81859
6cc57bd46179
clarified exceptions and messages: use "error" only for user-errors, not system failures;
wenzelm
parents:
81858
diff
changeset
|
348 |
| pair_list _ = raise Fail "pair_list: odd list length" |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
349 |
|
81847 | 350 |
fun parse_line s = |
351 |
(case String.tokens (fn x => x = #"\n" orelse x = #" ") s of |
|
81859
6cc57bd46179
clarified exceptions and messages: use "error" only for user-errors, not system failures;
wenzelm
parents:
81858
diff
changeset
|
352 |
[] => raise Fail "parse_line: empty" |
81847 | 353 |
| cmd :: args => |
354 |
(case String.explode cmd of |
|
81859
6cc57bd46179
clarified exceptions and messages: use "error" only for user-errors, not system failures;
wenzelm
parents:
81858
diff
changeset
|
355 |
[] => raise Fail "parse_line: empty command" |
81847 | 356 |
| c :: cs => (c, String.implode cs :: args))) |
357 |
||
81907 | 358 |
fun command (#"R", [t]) = term t #>> refl #-> set_thm |
359 |
| command (#"B", [t]) = term t #>> beta #-> set_thm |
|
360 |
| command (#"1", [th]) = thm th #>> conj1 #-> set_thm |
|
361 |
| command (#"2", [th]) = thm th #>> conj2 #-> set_thm |
|
81962 | 362 |
| command (#"H", [t]) = term t #>> assume #-> set_thm |
81908 | 363 |
| command (#"A", [_, t]) = term t #>> make_thm #-> set_thm |
81962 | 364 |
| command (#"C", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry mk_comb #-> set_thm |
81907 | 365 |
| command (#"T", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry trans #-> set_thm |
366 |
| command (#"E", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry eq_mp #-> set_thm |
|
81962 | 367 |
| command (#"D", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry deduct_antisym_rule #-> set_thm |
81907 | 368 |
| command (#"L", [t, th]) = term t ##>> thm th #>> uncurry abs #-> set_thm |
81911 | 369 |
| command (#"M", [name]) = stored_thm name |
81907 | 370 |
| command (#"Q", args) = |
81910 | 371 |
split_last args |> (fn (tys, th) => thm th #-> (fn th => typs tys #-> (fn tys => |
81908 | 372 |
set_thm (inst_type (pair_list tys) th)))) |
81907 | 373 |
| command (#"S", args) = |
81910 | 374 |
split_last args |> (fn (ts, th) => thm th #-> (fn th => terms ts #-> (fn ts => |
81908 | 375 |
set_thm (inst (pair_list ts) th)))) |
81907 | 376 |
| command (#"F", [name, t]) = |
377 |
term t #-> (fn t => theory_op (def (make_name name) t) #-> set_thm) |
|
378 |
| command (#"F", [name]) = theory #-> (fn thy => set_thm (mdef thy name)) |
|
379 |
| command (#"Y", [name, abs, rep, t1, t2, th]) = |
|
380 |
thm th #-> (fn th => term t1 #-> (fn t1 => term t2 #-> (fn t2 => |
|
81926 | 381 |
theory_op (tydef (make_name name) abs rep t1 t2 th) #-> set_thm))) |
81907 | 382 |
| command (#"Y", [name, _, _]) = theory #-> (fn thy => set_thm (mtydef thy name)) |
81913 | 383 |
| command (#"t", [a]) = theory #-> (fn thy => set_typ (make_tfree thy a)) |
81908 | 384 |
| command (#"a", c :: tys) = theory #-> (fn thy => typs tys #>> make_type thy c #-> set_typ) |
385 |
| command (#"v", [x, ty]) = typ ty #>> make_free x #-> set_term |
|
386 |
| command (#"c", [c, ty]) = theory #-> (fn thy => typ ty #>> make_const thy c #-> set_term) |
|
387 |
| command (#"f", [t, u]) = term t #-> (fn t => term u #-> (fn u => set_term (Thm.apply t u))) |
|
81932 | 388 |
| command (#"l", [x, t]) = |
389 |
term x #-> (fn x => term t #-> (fn t => |
|
390 |
set_term (Thm.lambda_name (make_bound (#1 (dest_Free (Thm.term_of x))), x) t))) |
|
81926 | 391 |
| command (#"+", [name]) = store_thm (make_name name) |
81907 | 392 |
| command (c, _) = raise Fail ("process: unknown command: " ^ String.str c) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
393 |
|
81909 | 394 |
in |
395 |
||
81847 | 396 |
fun import_file path0 thy = |
81846
5a7bf0f038e2
more robust import_file path: proper master_directory;
wenzelm
parents:
81844
diff
changeset
|
397 |
let |
5a7bf0f038e2
more robust import_file path: proper master_directory;
wenzelm
parents:
81844
diff
changeset
|
398 |
val path = File.absolute_path (Resources.master_directory thy + path0) |
5a7bf0f038e2
more robust import_file path: proper master_directory;
wenzelm
parents:
81844
diff
changeset
|
399 |
val lines = |
5a7bf0f038e2
more robust import_file path: proper master_directory;
wenzelm
parents:
81844
diff
changeset
|
400 |
if Path.is_zst path then Bytes.read path |> Zstd.uncompress |> Bytes.trim_split_lines |
5a7bf0f038e2
more robust import_file path: proper master_directory;
wenzelm
parents:
81844
diff
changeset
|
401 |
else File.read_lines path |
81908 | 402 |
in init_state thy |> fold (parse_line #> command) lines |> get_theory end |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
403 |
|
81847 | 404 |
val _ = |
405 |
Outer_Syntax.command \<^command_keyword>\<open>import_file\<close> "import recorded proofs from HOL Light" |
|
406 |
(Parse.path >> (fn name => Toplevel.theory (fn thy => import_file (Path.explode name) thy))) |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
407 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
408 |
end |
81909 | 409 |
|
410 |
end |