kuncar@48153
|
1 |
(* Title: HOL/Tools/Lifting/lifting_setup.ML
|
kuncar@48153
|
2 |
Author: Ondrej Kuncar
|
kuncar@48153
|
3 |
|
huffman@48210
|
4 |
Setting up the lifting infrastructure.
|
kuncar@48153
|
5 |
*)
|
kuncar@48153
|
6 |
|
kuncar@48153
|
7 |
signature LIFTING_SETUP =
|
kuncar@48153
|
8 |
sig
|
kuncar@48153
|
9 |
exception SETUP_LIFTING_INFR of string
|
kuncar@48153
|
10 |
|
kuncar@48437
|
11 |
val setup_lifting_infr: bool -> thm -> local_theory -> local_theory
|
kuncar@48392
|
12 |
|
kuncar@48437
|
13 |
val setup_by_quotient: bool -> thm -> thm option -> local_theory -> local_theory
|
kuncar@48153
|
14 |
|
kuncar@48437
|
15 |
val setup_by_typedef_thm: bool -> thm -> local_theory -> local_theory
|
kuncar@48153
|
16 |
end;
|
kuncar@48153
|
17 |
|
huffman@48205
|
18 |
structure Lifting_Setup: LIFTING_SETUP =
|
kuncar@48153
|
19 |
struct
|
kuncar@48153
|
20 |
|
kuncar@48153
|
21 |
infix 0 MRSL
|
kuncar@48153
|
22 |
|
kuncar@48153
|
23 |
fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm
|
kuncar@48153
|
24 |
|
kuncar@48153
|
25 |
exception SETUP_LIFTING_INFR of string
|
kuncar@48153
|
26 |
|
kuncar@48219
|
27 |
fun define_cr_rel rep_fun lthy =
|
kuncar@48153
|
28 |
let
|
kuncar@48219
|
29 |
val (qty, rty) = (dest_funT o fastype_of) rep_fun
|
kuncar@48219
|
30 |
val rep_fun_graph = (HOLogic.eq_const rty) $ Bound 1 $ (rep_fun $ Bound 0)
|
kuncar@48219
|
31 |
val def_term = Abs ("x", rty, Abs ("y", qty, rep_fun_graph));
|
kuncar@48153
|
32 |
val qty_name = (fst o dest_Type) qty
|
kuncar@48153
|
33 |
val cr_rel_name = Binding.prefix_name "cr_" (Binding.qualified_name qty_name)
|
kuncar@48153
|
34 |
val (fixed_def_term, lthy') = yield_singleton (Variable.importT_terms) def_term lthy
|
kuncar@48153
|
35 |
val ((_, (_ , def_thm)), lthy'') =
|
kuncar@48153
|
36 |
Local_Theory.define ((cr_rel_name, NoSyn), ((Thm.def_binding cr_rel_name, []), fixed_def_term)) lthy'
|
kuncar@48153
|
37 |
in
|
kuncar@48153
|
38 |
(def_thm, lthy'')
|
kuncar@48153
|
39 |
end
|
kuncar@48153
|
40 |
|
kuncar@48437
|
41 |
fun define_abs_type no_code quot_thm lthy =
|
kuncar@48437
|
42 |
if not no_code andalso Lifting_Def.can_generate_code_cert quot_thm then
|
kuncar@48153
|
43 |
let
|
kuncar@48153
|
44 |
val abs_type_thm = quot_thm RS @{thm Quotient_abs_rep}
|
kuncar@48153
|
45 |
val add_abstype_attribute =
|
kuncar@48153
|
46 |
Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abstype thm) I)
|
kuncar@48153
|
47 |
val add_abstype_attrib = Attrib.internal (K add_abstype_attribute);
|
kuncar@48153
|
48 |
in
|
kuncar@48153
|
49 |
lthy
|
kuncar@48153
|
50 |
|> (snd oo Local_Theory.note) ((Binding.empty, [add_abstype_attrib]), [abs_type_thm])
|
kuncar@48153
|
51 |
end
|
kuncar@48153
|
52 |
else
|
kuncar@48153
|
53 |
lthy
|
kuncar@48153
|
54 |
|
kuncar@48237
|
55 |
fun quot_thm_sanity_check ctxt quot_thm =
|
kuncar@48237
|
56 |
let
|
kuncar@48237
|
57 |
val ((_, [quot_thm_fixed]), ctxt') = Variable.importT [quot_thm] ctxt
|
kuncar@48237
|
58 |
val (rty, qty) = Lifting_Term.quot_thm_rty_qty quot_thm_fixed
|
kuncar@48237
|
59 |
val rty_tfreesT = Term.add_tfree_namesT rty []
|
kuncar@48237
|
60 |
val qty_tfreesT = Term.add_tfree_namesT qty []
|
kuncar@48237
|
61 |
val extra_rty_tfrees =
|
kuncar@48411
|
62 |
case subtract (op =) qty_tfreesT rty_tfreesT of
|
kuncar@48237
|
63 |
[] => []
|
kuncar@48237
|
64 |
| extras => [Pretty.block ([Pretty.str "Extra variables in the raw type:",
|
kuncar@48237
|
65 |
Pretty.brk 1] @
|
kuncar@48237
|
66 |
((Pretty.commas o map (Pretty.str o quote)) extras) @
|
kuncar@48411
|
67 |
[Pretty.str "."])]
|
kuncar@48237
|
68 |
val not_type_constr =
|
kuncar@48411
|
69 |
case qty of
|
kuncar@48237
|
70 |
Type _ => []
|
kuncar@48237
|
71 |
| _ => [Pretty.block [Pretty.str "The quotient type ",
|
kuncar@48237
|
72 |
Pretty.quote (Syntax.pretty_typ ctxt' qty),
|
kuncar@48237
|
73 |
Pretty.brk 1,
|
kuncar@48411
|
74 |
Pretty.str "is not a type constructor."]]
|
kuncar@48237
|
75 |
val errs = extra_rty_tfrees @ not_type_constr
|
kuncar@48237
|
76 |
in
|
kuncar@48237
|
77 |
if null errs then () else error (cat_lines (["Sanity check of the quotient theorem failed:",""]
|
kuncar@48237
|
78 |
@ (map Pretty.string_of errs)))
|
kuncar@48237
|
79 |
end
|
kuncar@48237
|
80 |
|
kuncar@48437
|
81 |
fun setup_lifting_infr no_code quot_thm lthy =
|
kuncar@48153
|
82 |
let
|
kuncar@48237
|
83 |
val _ = quot_thm_sanity_check lthy quot_thm
|
kuncar@48153
|
84 |
val (_, qtyp) = Lifting_Term.quot_thm_rty_qty quot_thm
|
kuncar@48153
|
85 |
val qty_full_name = (fst o dest_Type) qtyp
|
kuncar@48153
|
86 |
val quotients = { quot_thm = quot_thm }
|
kuncar@48153
|
87 |
fun quot_info phi = Lifting_Info.transform_quotients phi quotients
|
kuncar@48153
|
88 |
in
|
kuncar@48153
|
89 |
lthy
|
kuncar@48153
|
90 |
|> Local_Theory.declaration {syntax = false, pervasive = true}
|
kuncar@48153
|
91 |
(fn phi => Lifting_Info.update_quotients qty_full_name (quot_info phi))
|
kuncar@48437
|
92 |
|> define_abs_type no_code quot_thm
|
kuncar@48153
|
93 |
end
|
kuncar@48153
|
94 |
|
kuncar@48437
|
95 |
fun setup_by_quotient no_code quot_thm maybe_reflp_thm lthy =
|
kuncar@48392
|
96 |
let
|
kuncar@48392
|
97 |
val transfer_attr = Attrib.internal (K Transfer.transfer_add)
|
kuncar@48411
|
98 |
val (_, qty) = Lifting_Term.quot_thm_rty_qty quot_thm
|
huffman@48446
|
99 |
val induct_attr = Attrib.internal (K (Induct.induct_type (fst (dest_Type qty))))
|
kuncar@48411
|
100 |
val qty_name = (Binding.name o fst o dest_Type) qty
|
kuncar@48411
|
101 |
fun qualify suffix = Binding.qualified true suffix qty_name
|
kuncar@48392
|
102 |
val lthy' = case maybe_reflp_thm of
|
kuncar@48392
|
103 |
SOME reflp_thm => lthy
|
kuncar@48411
|
104 |
|> (snd oo Local_Theory.note) ((qualify "bi_total", [transfer_attr]),
|
kuncar@48392
|
105 |
[[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
|
kuncar@48411
|
106 |
|> (snd oo Local_Theory.note) ((qualify "id_abs_transfer", [transfer_attr]),
|
kuncar@48392
|
107 |
[[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
|
huffman@48446
|
108 |
|> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
|
huffman@48446
|
109 |
[[quot_thm, reflp_thm] MRSL @{thm Quotient_total_abs_induct}])
|
kuncar@48392
|
110 |
| NONE => lthy
|
kuncar@48411
|
111 |
|> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]),
|
kuncar@48411
|
112 |
[quot_thm RS @{thm Quotient_All_transfer}])
|
kuncar@48411
|
113 |
|> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]),
|
kuncar@48411
|
114 |
[quot_thm RS @{thm Quotient_Ex_transfer}])
|
kuncar@48411
|
115 |
|> (snd oo Local_Theory.note) ((qualify "forall_transfer", [transfer_attr]),
|
kuncar@48411
|
116 |
[quot_thm RS @{thm Quotient_forall_transfer}])
|
huffman@48446
|
117 |
|> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
|
huffman@48446
|
118 |
[quot_thm RS @{thm Quotient_abs_induct}])
|
kuncar@48392
|
119 |
in
|
kuncar@48392
|
120 |
lthy'
|
kuncar@48411
|
121 |
|> (snd oo Local_Theory.note) ((qualify "right_unique", [transfer_attr]),
|
kuncar@48392
|
122 |
[quot_thm RS @{thm Quotient_right_unique}])
|
kuncar@48411
|
123 |
|> (snd oo Local_Theory.note) ((qualify "right_total", [transfer_attr]),
|
kuncar@48392
|
124 |
[quot_thm RS @{thm Quotient_right_total}])
|
kuncar@48411
|
125 |
|> (snd oo Local_Theory.note) ((qualify "rel_eq_transfer", [transfer_attr]),
|
kuncar@48392
|
126 |
[quot_thm RS @{thm Quotient_rel_eq_transfer}])
|
kuncar@48437
|
127 |
|> setup_lifting_infr no_code quot_thm
|
kuncar@48392
|
128 |
end
|
kuncar@48392
|
129 |
|
kuncar@48437
|
130 |
fun setup_by_typedef_thm no_code typedef_thm lthy =
|
kuncar@48153
|
131 |
let
|
kuncar@48392
|
132 |
val transfer_attr = Attrib.internal (K Transfer.transfer_add)
|
kuncar@48392
|
133 |
val (_ $ rep_fun $ _ $ typedef_set) = (HOLogic.dest_Trueprop o prop_of) typedef_thm
|
kuncar@48392
|
134 |
val (T_def, lthy') = define_cr_rel rep_fun lthy
|
kuncar@48411
|
135 |
|
kuncar@48411
|
136 |
val quot_thm = case typedef_set of
|
kuncar@48392
|
137 |
Const ("Orderings.top_class.top", _) =>
|
kuncar@48392
|
138 |
[typedef_thm, T_def] MRSL @{thm UNIV_typedef_to_Quotient}
|
kuncar@48392
|
139 |
| Const (@{const_name "Collect"}, _) $ Abs (_, _, _) =>
|
kuncar@48392
|
140 |
[typedef_thm, T_def] MRSL @{thm open_typedef_to_Quotient}
|
kuncar@48392
|
141 |
| _ =>
|
kuncar@48411
|
142 |
[typedef_thm, T_def] MRSL @{thm typedef_to_Quotient}
|
kuncar@48153
|
143 |
|
kuncar@48411
|
144 |
val (_, qty) = Lifting_Term.quot_thm_rty_qty quot_thm
|
kuncar@48411
|
145 |
val qty_name = (Binding.name o fst o dest_Type) qty
|
kuncar@48411
|
146 |
fun qualify suffix = Binding.qualified true suffix qty_name
|
kuncar@48411
|
147 |
|
kuncar@48411
|
148 |
val lthy'' = case typedef_set of
|
kuncar@48411
|
149 |
Const ("Orderings.top_class.top", _) =>
|
kuncar@48411
|
150 |
let
|
kuncar@48411
|
151 |
val equivp_thm = typedef_thm RS @{thm UNIV_typedef_to_equivp}
|
kuncar@48411
|
152 |
val reflp_thm = equivp_thm RS @{thm equivp_reflp2}
|
kuncar@48411
|
153 |
in
|
kuncar@48411
|
154 |
lthy'
|
kuncar@48411
|
155 |
|> (snd oo Local_Theory.note) ((qualify "bi_total", [transfer_attr]),
|
kuncar@48411
|
156 |
[[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
|
kuncar@48411
|
157 |
|> (snd oo Local_Theory.note) ((qualify "id_abs_transfer", [transfer_attr]),
|
kuncar@48411
|
158 |
[[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
|
kuncar@48411
|
159 |
end
|
kuncar@48411
|
160 |
| _ => lthy'
|
kuncar@48411
|
161 |
|> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]),
|
kuncar@48411
|
162 |
[[typedef_thm, T_def] MRSL @{thm typedef_All_transfer}])
|
kuncar@48411
|
163 |
|> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]),
|
kuncar@48411
|
164 |
[[typedef_thm, T_def] MRSL @{thm typedef_Ex_transfer}])
|
kuncar@48411
|
165 |
|> (snd oo Local_Theory.note) ((qualify "forall_transfer", [transfer_attr]),
|
kuncar@48411
|
166 |
[[typedef_thm, T_def] MRSL @{thm typedef_forall_transfer}])
|
kuncar@48153
|
167 |
in
|
kuncar@48392
|
168 |
lthy''
|
kuncar@48411
|
169 |
|> (snd oo Local_Theory.note) ((qualify "bi_unique", [transfer_attr]),
|
kuncar@48392
|
170 |
[[typedef_thm, T_def] MRSL @{thm typedef_bi_unique}])
|
kuncar@48411
|
171 |
|> (snd oo Local_Theory.note) ((qualify "rep_transfer", [transfer_attr]),
|
huffman@48406
|
172 |
[[typedef_thm, T_def] MRSL @{thm typedef_rep_transfer}])
|
kuncar@48411
|
173 |
|> (snd oo Local_Theory.note) ((qualify "right_unique", [transfer_attr]),
|
kuncar@48411
|
174 |
[[quot_thm] MRSL @{thm Quotient_right_unique}])
|
kuncar@48411
|
175 |
|> (snd oo Local_Theory.note) ((qualify "right_total", [transfer_attr]),
|
kuncar@48411
|
176 |
[[quot_thm] MRSL @{thm Quotient_right_total}])
|
kuncar@48437
|
177 |
|> setup_lifting_infr no_code quot_thm
|
kuncar@48153
|
178 |
end
|
kuncar@48153
|
179 |
|
kuncar@48437
|
180 |
fun setup_lifting_cmd no_code xthm opt_reflp_xthm lthy =
|
kuncar@48392
|
181 |
let
|
kuncar@48392
|
182 |
val input_thm = singleton (Attrib.eval_thms lthy) xthm
|
kuncar@48392
|
183 |
val input_term = (HOLogic.dest_Trueprop o prop_of) input_thm
|
kuncar@48437
|
184 |
handle TERM _ => error "Unsupported type of a theorem. Only Quotient or type_definition are supported."
|
kuncar@48437
|
185 |
|
kuncar@48437
|
186 |
fun sanity_check_reflp_thm reflp_thm =
|
kuncar@48437
|
187 |
let
|
kuncar@48437
|
188 |
val reflp_tm = (HOLogic.dest_Trueprop o prop_of) reflp_thm
|
kuncar@48437
|
189 |
handle TERM _ => error "Invalid form of the reflexivity theorem. Use \"reflp R\"."
|
kuncar@48437
|
190 |
in
|
kuncar@48437
|
191 |
case reflp_tm of
|
kuncar@48437
|
192 |
Const (@{const_name reflp}, _) $ _ => ()
|
kuncar@48437
|
193 |
| _ => error "Invalid form of the reflexivity theorem. Use \"reflp R\"."
|
kuncar@48437
|
194 |
end
|
kuncar@48437
|
195 |
|
kuncar@48437
|
196 |
fun setup_quotient () =
|
kuncar@48437
|
197 |
case opt_reflp_xthm of
|
kuncar@48437
|
198 |
SOME reflp_xthm =>
|
kuncar@48437
|
199 |
let
|
kuncar@48437
|
200 |
val reflp_thm = singleton (Attrib.eval_thms lthy) reflp_xthm
|
kuncar@48437
|
201 |
val _ = sanity_check_reflp_thm reflp_thm
|
kuncar@48437
|
202 |
in
|
kuncar@48437
|
203 |
setup_by_quotient no_code input_thm (SOME reflp_thm) lthy
|
kuncar@48437
|
204 |
end
|
kuncar@48437
|
205 |
| NONE => setup_by_quotient no_code input_thm NONE lthy
|
kuncar@48437
|
206 |
|
kuncar@48437
|
207 |
fun setup_typedef () =
|
kuncar@48437
|
208 |
case opt_reflp_xthm of
|
kuncar@48437
|
209 |
SOME _ => error "The reflexivity theorem cannot be specified if the type_definition theorem is used."
|
kuncar@48437
|
210 |
| NONE => setup_by_typedef_thm no_code input_thm lthy
|
kuncar@48392
|
211 |
in
|
kuncar@48392
|
212 |
case input_term of
|
kuncar@48437
|
213 |
(Const (@{const_name Quotient}, _) $ _ $ _ $ _ $ _) => setup_quotient ()
|
kuncar@48437
|
214 |
| (Const (@{const_name type_definition}, _) $ _ $ _ $ _) => setup_typedef ()
|
kuncar@48392
|
215 |
| _ => error "Unsupported type of a theorem. Only Quotient or type_definition are supported."
|
kuncar@48392
|
216 |
end
|
kuncar@48153
|
217 |
|
kuncar@48437
|
218 |
val opt_no_code =
|
kuncar@48437
|
219 |
Scan.optional (@{keyword "("} |-- Parse.!!! ((Parse.reserved "no_code" >> K true) --| @{keyword ")"})) false
|
kuncar@48437
|
220 |
|
kuncar@48153
|
221 |
val _ =
|
kuncar@48153
|
222 |
Outer_Syntax.local_theory @{command_spec "setup_lifting"}
|
huffman@48210
|
223 |
"Setup lifting infrastructure"
|
kuncar@48437
|
224 |
(opt_no_code -- Parse_Spec.xthm -- Scan.option Parse_Spec.xthm >>
|
kuncar@48437
|
225 |
(fn ((no_code, xthm), opt_reflp_xthm) => setup_lifting_cmd no_code xthm opt_reflp_xthm))
|
kuncar@48153
|
226 |
end;
|