kuncar@48153
|
1 |
(* Title: HOL/Tools/Lifting/lifting_info.ML
|
kuncar@48153
|
2 |
Author: Ondrej Kuncar
|
kuncar@48153
|
3 |
|
kuncar@48153
|
4 |
Context data for the lifting package.
|
kuncar@48153
|
5 |
*)
|
kuncar@48153
|
6 |
|
kuncar@48153
|
7 |
signature LIFTING_INFO =
|
kuncar@48153
|
8 |
sig
|
kuncar@54356
|
9 |
type quot_map = {rel_quot_thm: thm}
|
kuncar@54356
|
10 |
val lookup_quot_maps: Proof.context -> string -> quot_map option
|
kuncar@54356
|
11 |
val print_quot_maps: Proof.context -> unit
|
kuncar@54356
|
12 |
|
kuncar@54356
|
13 |
type pcr = {pcrel_def: thm, pcr_cr_eq: thm}
|
kuncar@54356
|
14 |
type quotient = {quot_thm: thm, pcr_info: pcr option}
|
kuncar@54788
|
15 |
val pcr_eq: pcr * pcr -> bool
|
kuncar@54788
|
16 |
val quotient_eq: quotient * quotient -> bool
|
kuncar@54356
|
17 |
val transform_quotient: morphism -> quotient -> quotient
|
kuncar@54356
|
18 |
val lookup_quotients: Proof.context -> string -> quotient option
|
kuncar@54356
|
19 |
val update_quotients: string -> quotient -> Context.generic -> Context.generic
|
kuncar@54787
|
20 |
val delete_quotients: thm -> Context.generic -> Context.generic
|
kuncar@48153
|
21 |
val print_quotients: Proof.context -> unit
|
kuncar@48153
|
22 |
|
kuncar@54788
|
23 |
type restore_data = {quotient : quotient, transfer_rules: thm Item_Net.T}
|
kuncar@54788
|
24 |
val lookup_restore_data: Proof.context -> string -> restore_data option
|
kuncar@54788
|
25 |
val init_restore_data: string -> quotient -> Context.generic -> Context.generic
|
kuncar@54788
|
26 |
val add_transfer_rules_in_restore_data: string -> thm Item_Net.T -> Context.generic -> Context.generic
|
kuncar@54788
|
27 |
|
kuncar@48499
|
28 |
val get_invariant_commute_rules: Proof.context -> thm list
|
kuncar@48951
|
29 |
|
kuncar@48997
|
30 |
val get_reflexivity_rules: Proof.context -> thm list
|
kuncar@53131
|
31 |
val add_reflexivity_rule_raw_attribute: attribute
|
kuncar@48997
|
32 |
val add_reflexivity_rule_attribute: attribute
|
kuncar@48499
|
33 |
|
kuncar@52511
|
34 |
type relator_distr_data = {pos_mono_rule: thm, neg_mono_rule: thm,
|
kuncar@52511
|
35 |
pos_distr_rules: thm list, neg_distr_rules: thm list}
|
kuncar@52511
|
36 |
val lookup_relator_distr_data: Proof.context -> string -> relator_distr_data option
|
kuncar@54356
|
37 |
|
kuncar@54356
|
38 |
val get_quot_maps : Proof.context -> quot_map Symtab.table
|
kuncar@54356
|
39 |
val get_quotients : Proof.context -> quotient Symtab.table
|
kuncar@54356
|
40 |
val get_relator_distr_data : Proof.context -> relator_distr_data Symtab.table
|
kuncar@54788
|
41 |
val get_restore_data : Proof.context -> restore_data Symtab.table
|
kuncar@52511
|
42 |
|
kuncar@48153
|
43 |
val setup: theory -> theory
|
kuncar@54788
|
44 |
end
|
kuncar@48153
|
45 |
|
kuncar@48153
|
46 |
structure Lifting_Info: LIFTING_INFO =
|
kuncar@48153
|
47 |
struct
|
kuncar@48153
|
48 |
|
kuncar@48647
|
49 |
open Lifting_Util
|
kuncar@48647
|
50 |
|
kuncar@54356
|
51 |
(** data container **)
|
kuncar@54356
|
52 |
|
kuncar@54356
|
53 |
type quot_map = {rel_quot_thm: thm}
|
kuncar@54356
|
54 |
type pcr = {pcrel_def: thm, pcr_cr_eq: thm}
|
kuncar@54356
|
55 |
type quotient = {quot_thm: thm, pcr_info: pcr option}
|
kuncar@54356
|
56 |
type relator_distr_data = {pos_mono_rule: thm, neg_mono_rule: thm,
|
kuncar@54356
|
57 |
pos_distr_rules: thm list, neg_distr_rules: thm list}
|
kuncar@54788
|
58 |
type restore_data = {quotient : quotient, transfer_rules: thm Item_Net.T}
|
kuncar@54356
|
59 |
|
kuncar@54356
|
60 |
structure Data = Generic_Data
|
kuncar@54356
|
61 |
(
|
kuncar@54356
|
62 |
type T =
|
kuncar@54356
|
63 |
{ quot_maps : quot_map Symtab.table,
|
kuncar@54356
|
64 |
quotients : quotient Symtab.table,
|
kuncar@54356
|
65 |
reflexivity_rules : thm Item_Net.T,
|
kuncar@54788
|
66 |
relator_distr_data : relator_distr_data Symtab.table,
|
kuncar@54788
|
67 |
restore_data : restore_data Symtab.table
|
kuncar@54356
|
68 |
}
|
kuncar@54356
|
69 |
val empty =
|
kuncar@54356
|
70 |
{ quot_maps = Symtab.empty,
|
kuncar@54356
|
71 |
quotients = Symtab.empty,
|
kuncar@54356
|
72 |
reflexivity_rules = Thm.full_rules,
|
kuncar@54788
|
73 |
relator_distr_data = Symtab.empty,
|
kuncar@54788
|
74 |
restore_data = Symtab.empty
|
kuncar@54356
|
75 |
}
|
kuncar@54356
|
76 |
val extend = I
|
kuncar@54356
|
77 |
fun merge
|
kuncar@54788
|
78 |
( { quot_maps = qm1, quotients = q1, reflexivity_rules = rr1, relator_distr_data = rdd1,
|
kuncar@54788
|
79 |
restore_data = rd1 },
|
kuncar@54788
|
80 |
{ quot_maps = qm2, quotients = q2, reflexivity_rules = rr2, relator_distr_data = rdd2,
|
kuncar@54788
|
81 |
restore_data = rd2 } ) =
|
kuncar@54356
|
82 |
{ quot_maps = Symtab.merge (K true) (qm1, qm2),
|
kuncar@54356
|
83 |
quotients = Symtab.merge (K true) (q1, q2),
|
kuncar@54356
|
84 |
reflexivity_rules = Item_Net.merge (rr1, rr2),
|
kuncar@54788
|
85 |
relator_distr_data = Symtab.merge (K true) (rdd1, rdd2),
|
kuncar@54788
|
86 |
restore_data = Symtab.merge (K true) (rd1, rd2) }
|
kuncar@54356
|
87 |
)
|
kuncar@54356
|
88 |
|
kuncar@54788
|
89 |
fun map_data f1 f2 f3 f4 f5
|
kuncar@54788
|
90 |
{ quot_maps, quotients, reflexivity_rules, relator_distr_data, restore_data } =
|
kuncar@54356
|
91 |
{ quot_maps = f1 quot_maps,
|
kuncar@54356
|
92 |
quotients = f2 quotients,
|
kuncar@54356
|
93 |
reflexivity_rules = f3 reflexivity_rules,
|
kuncar@54788
|
94 |
relator_distr_data = f4 relator_distr_data,
|
kuncar@54788
|
95 |
restore_data = f5 restore_data }
|
kuncar@54356
|
96 |
|
kuncar@54788
|
97 |
fun map_quot_maps f = map_data f I I I I
|
kuncar@54788
|
98 |
fun map_quotients f = map_data I f I I I
|
kuncar@54788
|
99 |
fun map_reflexivity_rules f = map_data I I f I I
|
kuncar@54788
|
100 |
fun map_relator_distr_data f = map_data I I I f I
|
kuncar@54788
|
101 |
fun map_restore_data f = map_data I I I I f
|
kuncar@54356
|
102 |
|
kuncar@54356
|
103 |
val get_quot_maps' = #quot_maps o Data.get
|
kuncar@54356
|
104 |
val get_quotients' = #quotients o Data.get
|
kuncar@54356
|
105 |
val get_reflexivity_rules' = #reflexivity_rules o Data.get
|
kuncar@54356
|
106 |
val get_relator_distr_data' = #relator_distr_data o Data.get
|
kuncar@54788
|
107 |
val get_restore_data' = #restore_data o Data.get
|
kuncar@54356
|
108 |
|
kuncar@54356
|
109 |
fun get_quot_maps ctxt = get_quot_maps' (Context.Proof ctxt)
|
kuncar@54356
|
110 |
fun get_quotients ctxt = get_quotients' (Context.Proof ctxt)
|
kuncar@54356
|
111 |
fun get_relator_distr_data ctxt = get_relator_distr_data' (Context.Proof ctxt)
|
kuncar@54788
|
112 |
fun get_restore_data ctxt = get_restore_data' (Context.Proof ctxt)
|
kuncar@48153
|
113 |
|
kuncar@48647
|
114 |
(* info about Quotient map theorems *)
|
kuncar@48153
|
115 |
|
kuncar@54356
|
116 |
val lookup_quot_maps = Symtab.lookup o get_quot_maps
|
kuncar@48153
|
117 |
|
kuncar@48655
|
118 |
fun quot_map_thm_sanity_check rel_quot_thm ctxt =
|
kuncar@48655
|
119 |
let
|
kuncar@48655
|
120 |
fun quot_term_absT ctxt quot_term =
|
kuncar@48655
|
121 |
let
|
kuncar@48655
|
122 |
val (_, abs, _, _) = (dest_Quotient o HOLogic.dest_Trueprop) quot_term
|
kuncar@48655
|
123 |
handle TERM (_, [t]) => error (Pretty.string_of (Pretty.block
|
kuncar@48655
|
124 |
[Pretty.str "The Quotient map theorem is not in the right form.",
|
kuncar@48655
|
125 |
Pretty.brk 1,
|
kuncar@48655
|
126 |
Pretty.str "The following term is not the Quotient predicate:",
|
kuncar@48655
|
127 |
Pretty.brk 1,
|
kuncar@48655
|
128 |
Syntax.pretty_term ctxt t]))
|
kuncar@48655
|
129 |
in
|
kuncar@48655
|
130 |
fastype_of abs
|
kuncar@48655
|
131 |
end
|
kuncar@48655
|
132 |
|
kuncar@48655
|
133 |
val ((_, [rel_quot_thm_fixed]), ctxt') = Variable.importT [rel_quot_thm] ctxt
|
kuncar@48655
|
134 |
val rel_quot_thm_prop = prop_of rel_quot_thm_fixed
|
kuncar@48655
|
135 |
val rel_quot_thm_concl = Logic.strip_imp_concl rel_quot_thm_prop
|
kuncar@48655
|
136 |
val rel_quot_thm_prems = Logic.strip_imp_prems rel_quot_thm_prop;
|
kuncar@48655
|
137 |
val concl_absT = quot_term_absT ctxt' rel_quot_thm_concl
|
kuncar@48655
|
138 |
val concl_tfrees = Term.add_tfree_namesT (concl_absT) []
|
kuncar@48655
|
139 |
val prems_tfrees = fold (fn typ => fn list => Term.add_tfree_namesT (quot_term_absT ctxt' typ) list)
|
kuncar@48655
|
140 |
rel_quot_thm_prems []
|
kuncar@48655
|
141 |
val extra_prem_tfrees =
|
kuncar@48655
|
142 |
case subtract (op =) concl_tfrees prems_tfrees of
|
kuncar@48655
|
143 |
[] => []
|
kuncar@48655
|
144 |
| extras => [Pretty.block ([Pretty.str "Extra type variables in the premises:",
|
kuncar@48655
|
145 |
Pretty.brk 1] @
|
kuncar@48655
|
146 |
((Pretty.commas o map (Pretty.str o quote)) extras) @
|
kuncar@48655
|
147 |
[Pretty.str "."])]
|
kuncar@48655
|
148 |
val errs = extra_prem_tfrees
|
kuncar@48655
|
149 |
in
|
kuncar@48655
|
150 |
if null errs then () else error (cat_lines (["Sanity check of the quotient map theorem failed:",""]
|
kuncar@48655
|
151 |
@ (map Pretty.string_of errs)))
|
kuncar@48655
|
152 |
end
|
kuncar@48655
|
153 |
|
kuncar@48655
|
154 |
|
kuncar@48647
|
155 |
fun add_quot_map rel_quot_thm ctxt =
|
kuncar@48647
|
156 |
let
|
kuncar@48655
|
157 |
val _ = Context.cases (K ()) (quot_map_thm_sanity_check rel_quot_thm) ctxt
|
kuncar@48647
|
158 |
val rel_quot_thm_concl = (Logic.strip_imp_concl o prop_of) rel_quot_thm
|
kuncar@48647
|
159 |
val (_, abs, _, _) = (dest_Quotient o HOLogic.dest_Trueprop) rel_quot_thm_concl
|
kuncar@48647
|
160 |
val relatorT_name = (fst o dest_Type o fst o dest_funT o fastype_of) abs
|
kuncar@48647
|
161 |
val minfo = {rel_quot_thm = rel_quot_thm}
|
kuncar@48647
|
162 |
in
|
kuncar@54356
|
163 |
Data.map (map_quot_maps (Symtab.update (relatorT_name, minfo))) ctxt
|
kuncar@48647
|
164 |
end
|
kuncar@48647
|
165 |
|
kuncar@48966
|
166 |
val quot_map_attribute_setup =
|
kuncar@48647
|
167 |
Attrib.setup @{binding quot_map} (Scan.succeed (Thm.declaration_attribute add_quot_map))
|
kuncar@48647
|
168 |
"declaration of the Quotient map theorem"
|
kuncar@48153
|
169 |
|
kuncar@54356
|
170 |
fun print_quot_maps ctxt =
|
kuncar@48153
|
171 |
let
|
kuncar@48647
|
172 |
fun prt_map (ty_name, {rel_quot_thm}) =
|
kuncar@48153
|
173 |
Pretty.block (separate (Pretty.brk 2)
|
kuncar@48153
|
174 |
[Pretty.str "type:",
|
kuncar@48153
|
175 |
Pretty.str ty_name,
|
kuncar@48153
|
176 |
Pretty.str "quot. theorem:",
|
kuncar@48647
|
177 |
Syntax.pretty_term ctxt (prop_of rel_quot_thm)])
|
kuncar@48153
|
178 |
in
|
kuncar@54356
|
179 |
map prt_map (Symtab.dest (get_quot_maps ctxt))
|
kuncar@48153
|
180 |
|> Pretty.big_list "maps for type constructors:"
|
kuncar@48153
|
181 |
|> Pretty.writeln
|
kuncar@48153
|
182 |
end
|
kuncar@48153
|
183 |
|
kuncar@48153
|
184 |
(* info about quotient types *)
|
kuncar@54788
|
185 |
|
kuncar@54788
|
186 |
fun pcr_eq ({pcrel_def = pcrel_def1, pcr_cr_eq = pcr_cr_eq1},
|
kuncar@54788
|
187 |
{pcrel_def = pcrel_def2, pcr_cr_eq = pcr_cr_eq2}) =
|
kuncar@54788
|
188 |
Thm.eq_thm (pcrel_def1, pcrel_def2) andalso Thm.eq_thm (pcr_cr_eq1, pcr_cr_eq2)
|
kuncar@54788
|
189 |
|
kuncar@54788
|
190 |
fun option_eq _ (NONE,NONE) = true
|
kuncar@54788
|
191 |
| option_eq _ (NONE,_) = false
|
kuncar@54788
|
192 |
| option_eq _ (_,NONE) = false
|
kuncar@54788
|
193 |
| option_eq cmp (SOME x, SOME y) = cmp (x,y);
|
kuncar@54788
|
194 |
|
kuncar@54788
|
195 |
fun quotient_eq ({quot_thm = quot_thm1, pcr_info = pcr_info1},
|
kuncar@54788
|
196 |
{quot_thm = quot_thm2, pcr_info = pcr_info2}) =
|
kuncar@54788
|
197 |
Thm.eq_thm (quot_thm1, quot_thm2) andalso option_eq pcr_eq (pcr_info1, pcr_info2)
|
kuncar@54788
|
198 |
|
kuncar@54356
|
199 |
fun transform_pcr_info phi {pcrel_def, pcr_cr_eq} =
|
kuncar@52511
|
200 |
{pcrel_def = Morphism.thm phi pcrel_def, pcr_cr_eq = Morphism.thm phi pcr_cr_eq}
|
kuncar@52511
|
201 |
|
kuncar@54356
|
202 |
fun transform_quotient phi {quot_thm, pcr_info} =
|
kuncar@54356
|
203 |
{quot_thm = Morphism.thm phi quot_thm, pcr_info = Option.map (transform_pcr_info phi) pcr_info}
|
kuncar@48153
|
204 |
|
kuncar@54356
|
205 |
fun lookup_quotients ctxt type_name = Symtab.lookup (get_quotients ctxt) type_name
|
kuncar@48153
|
206 |
|
kuncar@54356
|
207 |
fun update_quotients type_name qinfo ctxt =
|
kuncar@54356
|
208 |
Data.map (map_quotients (Symtab.update (type_name, qinfo))) ctxt
|
kuncar@48153
|
209 |
|
kuncar@48966
|
210 |
fun delete_quotients quot_thm ctxt =
|
kuncar@48966
|
211 |
let
|
kuncar@48966
|
212 |
val (_, qtyp) = quot_thm_rty_qty quot_thm
|
kuncar@48966
|
213 |
val qty_full_name = (fst o dest_Type) qtyp
|
kuncar@54356
|
214 |
val symtab = get_quotients' ctxt
|
kuncar@54421
|
215 |
fun compare_data (_, data:quotient) = Thm.eq_thm_prop (#quot_thm data, quot_thm)
|
kuncar@48966
|
216 |
in
|
kuncar@54356
|
217 |
if Symtab.member compare_data symtab (qty_full_name, quot_thm)
|
kuncar@54356
|
218 |
then Data.map (map_quotients (Symtab.delete qty_full_name)) ctxt
|
kuncar@54356
|
219 |
else ctxt
|
kuncar@48966
|
220 |
end
|
kuncar@48966
|
221 |
|
kuncar@48153
|
222 |
fun print_quotients ctxt =
|
kuncar@48153
|
223 |
let
|
kuncar@54356
|
224 |
fun prt_quot (qty_name, {quot_thm, pcr_info}: quotient) =
|
kuncar@48153
|
225 |
Pretty.block (separate (Pretty.brk 2)
|
kuncar@48153
|
226 |
[Pretty.str "type:",
|
kuncar@48153
|
227 |
Pretty.str qty_name,
|
kuncar@48153
|
228 |
Pretty.str "quot. thm:",
|
kuncar@51242
|
229 |
Syntax.pretty_term ctxt (prop_of quot_thm),
|
kuncar@51242
|
230 |
Pretty.str "pcrel_def thm:",
|
kuncar@54356
|
231 |
option_fold (Pretty.str "-") ((Syntax.pretty_term ctxt) o prop_of o #pcrel_def) pcr_info,
|
kuncar@52511
|
232 |
Pretty.str "pcr_cr_eq thm:",
|
kuncar@54356
|
233 |
option_fold (Pretty.str "-") ((Syntax.pretty_term ctxt) o prop_of o #pcr_cr_eq) pcr_info])
|
kuncar@48153
|
234 |
in
|
kuncar@54356
|
235 |
map prt_quot (Symtab.dest (get_quotients ctxt))
|
kuncar@48153
|
236 |
|> Pretty.big_list "quotients:"
|
kuncar@48153
|
237 |
|> Pretty.writeln
|
kuncar@48153
|
238 |
end
|
kuncar@48153
|
239 |
|
kuncar@48966
|
240 |
val quot_del_attribute_setup =
|
kuncar@48966
|
241 |
Attrib.setup @{binding quot_del} (Scan.succeed (Thm.declaration_attribute delete_quotients))
|
kuncar@48966
|
242 |
"deletes the Quotient theorem"
|
kuncar@48966
|
243 |
|
kuncar@54788
|
244 |
(* data for restoring Transfer/Lifting context *)
|
kuncar@54788
|
245 |
|
kuncar@54788
|
246 |
fun lookup_restore_data ctxt bundle_name = Symtab.lookup (get_restore_data ctxt) bundle_name
|
kuncar@54788
|
247 |
|
kuncar@54788
|
248 |
fun update_restore_data bundle_name restore_data ctxt =
|
kuncar@54788
|
249 |
Data.map (map_restore_data (Symtab.update (bundle_name, restore_data))) ctxt
|
kuncar@54788
|
250 |
|
kuncar@54788
|
251 |
fun init_restore_data bundle_name qinfo ctxt =
|
kuncar@54788
|
252 |
update_restore_data bundle_name { quotient = qinfo, transfer_rules = Thm.full_rules } ctxt
|
kuncar@54788
|
253 |
|
kuncar@54788
|
254 |
fun add_transfer_rules_in_restore_data bundle_name transfer_rules ctxt =
|
kuncar@54788
|
255 |
case Symtab.lookup (get_restore_data' ctxt) bundle_name of
|
kuncar@54788
|
256 |
SOME restore_data => update_restore_data bundle_name { quotient = #quotient restore_data,
|
kuncar@54788
|
257 |
transfer_rules = Item_Net.merge ((#transfer_rules restore_data), transfer_rules) } ctxt
|
kuncar@54788
|
258 |
| NONE => error ("The restore data " ^ quote bundle_name ^ " is not defined.")
|
kuncar@54788
|
259 |
|
kuncar@54356
|
260 |
(* theorems that a relator of an invariant is an invariant of the corresponding predicate *)
|
kuncar@54356
|
261 |
|
kuncar@48499
|
262 |
structure Invariant_Commute = Named_Thms
|
kuncar@48499
|
263 |
(
|
kuncar@48499
|
264 |
val name = @{binding invariant_commute}
|
kuncar@48499
|
265 |
val description = "theorems that a relator of an invariant is an invariant of the corresponding predicate"
|
kuncar@48499
|
266 |
)
|
kuncar@48499
|
267 |
|
kuncar@48499
|
268 |
fun get_invariant_commute_rules ctxt = map safe_mk_meta_eq (Invariant_Commute.get ctxt)
|
kuncar@48499
|
269 |
|
kuncar@53131
|
270 |
(* info about reflexivity rules *)
|
kuncar@53131
|
271 |
|
kuncar@54356
|
272 |
fun get_reflexivity_rules ctxt = Item_Net.content (get_reflexivity_rules' (Context.Proof ctxt))
|
kuncar@54356
|
273 |
|
kuncar@53131
|
274 |
|
kuncar@53131
|
275 |
(* Conversion to create a reflp' variant of a reflexivity rule *)
|
kuncar@53131
|
276 |
fun safe_reflp_conv ct =
|
kuncar@53131
|
277 |
Conv.try_conv (HOLogic.Trueprop_conv (Conv.rewr_conv (Thm.symmetric @{thm reflp'_def}))) ct
|
kuncar@53131
|
278 |
|
kuncar@53131
|
279 |
fun prep_reflp_conv ct = (
|
kuncar@53131
|
280 |
Conv.implies_conv safe_reflp_conv prep_reflp_conv
|
kuncar@53131
|
281 |
else_conv
|
kuncar@53131
|
282 |
safe_reflp_conv
|
kuncar@53131
|
283 |
else_conv
|
kuncar@53131
|
284 |
Conv.all_conv) ct
|
kuncar@53131
|
285 |
|
kuncar@54356
|
286 |
fun add_reflexivity_rule_raw thm = Data.map (map_reflexivity_rules (Item_Net.update thm))
|
kuncar@53131
|
287 |
val add_reflexivity_rule_raw_attribute = Thm.declaration_attribute add_reflexivity_rule_raw
|
kuncar@53131
|
288 |
|
kuncar@53131
|
289 |
fun add_reflexivity_rule thm = add_reflexivity_rule_raw thm #>
|
kuncar@53131
|
290 |
add_reflexivity_rule_raw (Conv.fconv_rule prep_reflp_conv thm)
|
kuncar@53131
|
291 |
val add_reflexivity_rule_attribute = Thm.declaration_attribute add_reflexivity_rule
|
kuncar@53131
|
292 |
|
kuncar@53131
|
293 |
|
kuncar@53131
|
294 |
val relfexivity_rule_setup =
|
kuncar@53131
|
295 |
let
|
kuncar@53131
|
296 |
val name = @{binding reflexivity_rule}
|
kuncar@54356
|
297 |
fun del_thm_raw thm = Data.map (map_reflexivity_rules (Item_Net.remove thm))
|
kuncar@53131
|
298 |
fun del_thm thm = del_thm_raw thm #>
|
kuncar@53131
|
299 |
del_thm_raw (Conv.fconv_rule prep_reflp_conv thm)
|
kuncar@53131
|
300 |
val del = Thm.declaration_attribute del_thm
|
kuncar@53131
|
301 |
val text = "rules that are used to prove that a relation is reflexive"
|
kuncar@54356
|
302 |
val content = Item_Net.content o get_reflexivity_rules'
|
kuncar@53131
|
303 |
in
|
kuncar@53131
|
304 |
Attrib.setup name (Attrib.add_del add_reflexivity_rule_attribute del) text
|
kuncar@53131
|
305 |
#> Global_Theory.add_thms_dynamic (name, content)
|
kuncar@53131
|
306 |
end
|
kuncar@48951
|
307 |
|
kuncar@52511
|
308 |
(* info about relator distributivity theorems *)
|
kuncar@52511
|
309 |
|
kuncar@54356
|
310 |
fun map_relator_distr_data' f1 f2 f3 f4
|
kuncar@52511
|
311 |
{pos_mono_rule, neg_mono_rule, pos_distr_rules, neg_distr_rules} =
|
kuncar@52511
|
312 |
{pos_mono_rule = f1 pos_mono_rule,
|
kuncar@52511
|
313 |
neg_mono_rule = f2 neg_mono_rule,
|
kuncar@52511
|
314 |
pos_distr_rules = f3 pos_distr_rules,
|
kuncar@52511
|
315 |
neg_distr_rules = f4 neg_distr_rules}
|
kuncar@52511
|
316 |
|
kuncar@54356
|
317 |
fun map_pos_mono_rule f = map_relator_distr_data' f I I I
|
kuncar@54356
|
318 |
fun map_neg_mono_rule f = map_relator_distr_data' I f I I
|
kuncar@54356
|
319 |
fun map_pos_distr_rules f = map_relator_distr_data' I I f I
|
kuncar@54356
|
320 |
fun map_neg_distr_rules f = map_relator_distr_data' I I I f
|
kuncar@52511
|
321 |
|
kuncar@52511
|
322 |
fun introduce_polarities rule =
|
kuncar@52511
|
323 |
let
|
kuncar@52511
|
324 |
val dest_less_eq = HOLogic.dest_bin @{const_name "less_eq"} dummyT
|
kuncar@52511
|
325 |
val prems_pairs = map (dest_less_eq o HOLogic.dest_Trueprop) (prems_of rule)
|
kuncar@52511
|
326 |
val equal_prems = filter op= prems_pairs
|
kuncar@52511
|
327 |
val _ = if null equal_prems then ()
|
kuncar@52511
|
328 |
else error "The rule contains reflexive assumptions."
|
kuncar@52511
|
329 |
val concl_pairs = rule
|
kuncar@52511
|
330 |
|> concl_of
|
kuncar@52511
|
331 |
|> HOLogic.dest_Trueprop
|
kuncar@52511
|
332 |
|> dest_less_eq
|
kuncar@52511
|
333 |
|> pairself (snd o strip_comb)
|
kuncar@52511
|
334 |
|> op~~
|
kuncar@52511
|
335 |
|> filter_out op=
|
kuncar@52511
|
336 |
|
kuncar@52511
|
337 |
val _ = if has_duplicates op= concl_pairs
|
kuncar@52511
|
338 |
then error "The rule contains duplicated variables in the conlusion." else ()
|
kuncar@52511
|
339 |
|
kuncar@52511
|
340 |
fun rewrite_prem prem_pair =
|
wenzelm@52564
|
341 |
if member op= concl_pairs prem_pair
|
kuncar@52511
|
342 |
then HOLogic.Trueprop_conv (Conv.rewr_conv (Thm.symmetric @{thm POS_def}))
|
wenzelm@52564
|
343 |
else if member op= concl_pairs (swap prem_pair)
|
kuncar@52511
|
344 |
then HOLogic.Trueprop_conv (Conv.rewr_conv (Thm.symmetric @{thm NEG_def}))
|
kuncar@52511
|
345 |
else error "The rule contains a non-relevant assumption."
|
kuncar@52511
|
346 |
|
kuncar@52511
|
347 |
fun rewrite_prems [] = Conv.all_conv
|
kuncar@52511
|
348 |
| rewrite_prems (x::xs) = Conv.implies_conv (rewrite_prem x) (rewrite_prems xs)
|
kuncar@52511
|
349 |
|
kuncar@52511
|
350 |
val rewrite_prems_conv = rewrite_prems prems_pairs
|
kuncar@52511
|
351 |
val rewrite_concl_conv =
|
kuncar@52511
|
352 |
Conv.concl_conv ~1 (HOLogic.Trueprop_conv (Conv.rewr_conv (Thm.symmetric @{thm POS_def})))
|
kuncar@52511
|
353 |
in
|
kuncar@52511
|
354 |
(Conv.fconv_rule (rewrite_prems_conv then_conv rewrite_concl_conv)) rule
|
kuncar@52511
|
355 |
end
|
kuncar@52511
|
356 |
handle
|
kuncar@52511
|
357 |
TERM _ => error "The rule has a wrong format."
|
kuncar@52511
|
358 |
| CTERM _ => error "The rule has a wrong format."
|
kuncar@52511
|
359 |
|
kuncar@52511
|
360 |
fun negate_mono_rule mono_rule =
|
kuncar@52511
|
361 |
let
|
kuncar@52511
|
362 |
val rewr_conv = HOLogic.Trueprop_conv (Conv.rewrs_conv [@{thm POS_NEG}, @{thm NEG_POS}])
|
kuncar@52511
|
363 |
in
|
kuncar@52511
|
364 |
Conv.fconv_rule (Conv.prems_conv ~1 rewr_conv then_conv Conv.concl_conv ~1 rewr_conv) mono_rule
|
kuncar@52511
|
365 |
end;
|
kuncar@52511
|
366 |
|
kuncar@52511
|
367 |
fun add_mono_rule mono_rule ctxt =
|
kuncar@52511
|
368 |
let
|
kuncar@52511
|
369 |
val mono_rule = introduce_polarities mono_rule
|
kuncar@52511
|
370 |
val mono_ruleT_name = (fst o dest_Type o fst o relation_types o fst o relation_types o snd o
|
kuncar@52511
|
371 |
dest_Const o head_of o HOLogic.dest_Trueprop o concl_of) mono_rule
|
kuncar@54356
|
372 |
val _ = if Symtab.defined (get_relator_distr_data' ctxt) mono_ruleT_name
|
kuncar@52511
|
373 |
then error ("Monotocity rule for type " ^ quote mono_ruleT_name ^ " is already_defined.")
|
kuncar@52511
|
374 |
else ()
|
kuncar@52511
|
375 |
val neg_mono_rule = negate_mono_rule mono_rule
|
kuncar@52511
|
376 |
val relator_distr_data = {pos_mono_rule = mono_rule, neg_mono_rule = neg_mono_rule,
|
kuncar@52511
|
377 |
pos_distr_rules = [], neg_distr_rules = []}
|
kuncar@52511
|
378 |
in
|
kuncar@54356
|
379 |
Data.map (map_relator_distr_data (Symtab.update (mono_ruleT_name, relator_distr_data))) ctxt
|
kuncar@52511
|
380 |
end;
|
kuncar@52511
|
381 |
|
kuncar@52511
|
382 |
local
|
kuncar@52511
|
383 |
fun add_distr_rule update_entry distr_rule ctxt =
|
kuncar@52511
|
384 |
let
|
kuncar@52511
|
385 |
val distr_ruleT_name = (fst o dest_Type o fst o relation_types o fst o relation_types o snd o
|
kuncar@52511
|
386 |
dest_Const o head_of o HOLogic.dest_Trueprop o concl_of) distr_rule
|
kuncar@52511
|
387 |
in
|
kuncar@54356
|
388 |
if Symtab.defined (get_relator_distr_data' ctxt) distr_ruleT_name then
|
kuncar@54356
|
389 |
Data.map (map_relator_distr_data (Symtab.map_entry distr_ruleT_name (update_entry distr_rule)))
|
kuncar@54356
|
390 |
ctxt
|
kuncar@52511
|
391 |
else error "The monoticity rule is not defined."
|
kuncar@52511
|
392 |
end
|
kuncar@52511
|
393 |
|
kuncar@52511
|
394 |
fun rewrite_concl_conv thm ctm =
|
kuncar@52511
|
395 |
Conv.concl_conv ~1 (HOLogic.Trueprop_conv (Conv.rewr_conv (Thm.symmetric thm))) ctm
|
kuncar@52511
|
396 |
handle CTERM _ => error "The rule has a wrong format."
|
kuncar@52511
|
397 |
|
kuncar@52511
|
398 |
in
|
kuncar@52511
|
399 |
fun add_pos_distr_rule distr_rule ctxt =
|
kuncar@52511
|
400 |
let
|
kuncar@52511
|
401 |
val distr_rule = Conv.fconv_rule (rewrite_concl_conv @{thm POS_def}) distr_rule
|
kuncar@52511
|
402 |
fun update_entry distr_rule data =
|
kuncar@52511
|
403 |
map_pos_distr_rules (cons (@{thm POS_trans} OF [distr_rule, #pos_mono_rule data])) data
|
kuncar@52511
|
404 |
in
|
kuncar@52511
|
405 |
add_distr_rule update_entry distr_rule ctxt
|
kuncar@52511
|
406 |
end
|
kuncar@52511
|
407 |
handle THM _ => error "Combining of the distr. rule and the monotonicity rule together has failed."
|
kuncar@52511
|
408 |
|
kuncar@52511
|
409 |
|
kuncar@52511
|
410 |
fun add_neg_distr_rule distr_rule ctxt =
|
kuncar@52511
|
411 |
let
|
kuncar@52511
|
412 |
val distr_rule = Conv.fconv_rule (rewrite_concl_conv @{thm NEG_def}) distr_rule
|
kuncar@52511
|
413 |
fun update_entry distr_rule data =
|
kuncar@52511
|
414 |
map_neg_distr_rules (cons (@{thm NEG_trans} OF [distr_rule, #neg_mono_rule data])) data
|
kuncar@52511
|
415 |
in
|
kuncar@52511
|
416 |
add_distr_rule update_entry distr_rule ctxt
|
kuncar@52511
|
417 |
end
|
kuncar@52511
|
418 |
handle THM _ => error "Combining of the distr. rule and the monotonicity rule together has failed."
|
kuncar@52511
|
419 |
end
|
kuncar@52511
|
420 |
|
kuncar@52511
|
421 |
local
|
kuncar@52511
|
422 |
val eq_refl2 = sym RS @{thm eq_refl}
|
kuncar@52511
|
423 |
in
|
kuncar@52511
|
424 |
fun add_eq_distr_rule distr_rule ctxt =
|
kuncar@52511
|
425 |
let
|
kuncar@52511
|
426 |
val pos_distr_rule = @{thm eq_refl} OF [distr_rule]
|
kuncar@52511
|
427 |
val neg_distr_rule = eq_refl2 OF [distr_rule]
|
kuncar@52511
|
428 |
in
|
kuncar@52511
|
429 |
ctxt
|
kuncar@52511
|
430 |
|> add_pos_distr_rule pos_distr_rule
|
kuncar@52511
|
431 |
|> add_neg_distr_rule neg_distr_rule
|
kuncar@52511
|
432 |
end
|
kuncar@52511
|
433 |
end;
|
kuncar@52511
|
434 |
|
kuncar@52511
|
435 |
local
|
kuncar@52511
|
436 |
fun sanity_check rule =
|
kuncar@52511
|
437 |
let
|
kuncar@52511
|
438 |
val assms = map (perhaps (try HOLogic.dest_Trueprop)) (prems_of rule)
|
kuncar@52511
|
439 |
val concl = (perhaps (try HOLogic.dest_Trueprop)) (concl_of rule);
|
kuncar@52511
|
440 |
val (lhs, rhs) = case concl of
|
kuncar@52511
|
441 |
Const ("Orderings.ord_class.less_eq", _) $ (lhs as Const ("Relation.relcompp",_) $ _ $ _) $ rhs =>
|
kuncar@52511
|
442 |
(lhs, rhs)
|
kuncar@52511
|
443 |
| Const ("Orderings.ord_class.less_eq", _) $ rhs $ (lhs as Const ("Relation.relcompp",_) $ _ $ _) =>
|
kuncar@52511
|
444 |
(lhs, rhs)
|
kuncar@52511
|
445 |
| Const ("HOL.eq", _) $ (lhs as Const ("Relation.relcompp",_) $ _ $ _) $ rhs => (lhs, rhs)
|
kuncar@52511
|
446 |
| _ => error "The rule has a wrong format."
|
kuncar@52511
|
447 |
|
kuncar@52511
|
448 |
val lhs_vars = Term.add_vars lhs []
|
kuncar@52511
|
449 |
val rhs_vars = Term.add_vars rhs []
|
kuncar@52511
|
450 |
val assms_vars = fold Term.add_vars assms [];
|
kuncar@52511
|
451 |
val _ = if has_duplicates op= lhs_vars then error "Left-hand side has variable duplicates" else ()
|
kuncar@52511
|
452 |
val _ = if subset op= (rhs_vars, lhs_vars) then ()
|
kuncar@52511
|
453 |
else error "Extra variables in the right-hand side of the rule"
|
kuncar@52511
|
454 |
val _ = if subset op= (assms_vars, lhs_vars) then ()
|
kuncar@52511
|
455 |
else error "Extra variables in the assumptions of the rule"
|
kuncar@52511
|
456 |
val rhs_args = (snd o strip_comb) rhs;
|
kuncar@52511
|
457 |
fun check_comp t = case t of
|
kuncar@52511
|
458 |
Const ("Relation.relcompp", _) $ Var (_, _) $ Var (_,_) => ()
|
kuncar@52511
|
459 |
| _ => error "There is an argument on the rhs that is not a composition."
|
kuncar@52511
|
460 |
val _ = map check_comp rhs_args
|
kuncar@52511
|
461 |
in
|
kuncar@52511
|
462 |
()
|
kuncar@52511
|
463 |
end
|
kuncar@52511
|
464 |
in
|
kuncar@52511
|
465 |
|
kuncar@52511
|
466 |
fun add_distr_rule distr_rule ctxt =
|
kuncar@52511
|
467 |
let
|
kuncar@52511
|
468 |
val _ = sanity_check distr_rule
|
kuncar@52511
|
469 |
val concl = (perhaps (try HOLogic.dest_Trueprop)) (concl_of distr_rule)
|
kuncar@52511
|
470 |
in
|
kuncar@52511
|
471 |
case concl of
|
kuncar@52511
|
472 |
Const ("Orderings.ord_class.less_eq", _) $ (Const ("Relation.relcompp",_) $ _ $ _) $ _ =>
|
kuncar@52511
|
473 |
add_pos_distr_rule distr_rule ctxt
|
kuncar@52511
|
474 |
| Const ("Orderings.ord_class.less_eq", _) $ _ $ (Const ("Relation.relcompp",_) $ _ $ _) =>
|
kuncar@52511
|
475 |
add_neg_distr_rule distr_rule ctxt
|
kuncar@52511
|
476 |
| Const ("HOL.eq", _) $ (Const ("Relation.relcompp",_) $ _ $ _) $ _ =>
|
kuncar@52511
|
477 |
add_eq_distr_rule distr_rule ctxt
|
kuncar@52511
|
478 |
end
|
kuncar@52511
|
479 |
end
|
kuncar@52511
|
480 |
|
kuncar@52511
|
481 |
fun get_distr_rules_raw ctxt = Symtab.fold
|
kuncar@52511
|
482 |
(fn (_, {pos_distr_rules, neg_distr_rules, ...}) => fn rules => pos_distr_rules @ neg_distr_rules @ rules)
|
kuncar@54356
|
483 |
(get_relator_distr_data' ctxt) []
|
kuncar@52511
|
484 |
|
kuncar@52511
|
485 |
fun get_mono_rules_raw ctxt = Symtab.fold
|
kuncar@52511
|
486 |
(fn (_, {pos_mono_rule, neg_mono_rule, ...}) => fn rules => [pos_mono_rule, neg_mono_rule] @ rules)
|
kuncar@54356
|
487 |
(get_relator_distr_data' ctxt) []
|
kuncar@52511
|
488 |
|
kuncar@54356
|
489 |
val lookup_relator_distr_data = Symtab.lookup o get_relator_distr_data
|
kuncar@52511
|
490 |
|
kuncar@52511
|
491 |
val relator_distr_attribute_setup =
|
kuncar@52511
|
492 |
Attrib.setup @{binding relator_mono} (Scan.succeed (Thm.declaration_attribute add_mono_rule))
|
kuncar@52511
|
493 |
"declaration of relator's monoticity"
|
kuncar@52511
|
494 |
#> Attrib.setup @{binding relator_distr} (Scan.succeed (Thm.declaration_attribute add_distr_rule))
|
kuncar@52511
|
495 |
"declaration of relator's distributivity over OO"
|
kuncar@52511
|
496 |
#> Global_Theory.add_thms_dynamic
|
kuncar@52511
|
497 |
(@{binding relator_distr_raw}, get_distr_rules_raw)
|
kuncar@52511
|
498 |
#> Global_Theory.add_thms_dynamic
|
kuncar@52511
|
499 |
(@{binding relator_mono_raw}, get_mono_rules_raw)
|
kuncar@52511
|
500 |
|
kuncar@48153
|
501 |
(* theory setup *)
|
kuncar@48153
|
502 |
|
kuncar@48153
|
503 |
val setup =
|
kuncar@48966
|
504 |
quot_map_attribute_setup
|
kuncar@48966
|
505 |
#> quot_del_attribute_setup
|
kuncar@48499
|
506 |
#> Invariant_Commute.setup
|
kuncar@53131
|
507 |
#> relfexivity_rule_setup
|
kuncar@52511
|
508 |
#> relator_distr_attribute_setup
|
kuncar@48153
|
509 |
|
kuncar@48153
|
510 |
(* outer syntax commands *)
|
kuncar@48153
|
511 |
|
kuncar@48153
|
512 |
val _ =
|
kuncar@54356
|
513 |
Outer_Syntax.improper_command @{command_spec "print_quot_maps"} "print quotient map functions"
|
kuncar@54356
|
514 |
(Scan.succeed (Toplevel.keep (print_quot_maps o Toplevel.context_of)))
|
kuncar@48153
|
515 |
|
kuncar@48153
|
516 |
val _ =
|
kuncar@48153
|
517 |
Outer_Syntax.improper_command @{command_spec "print_quotients"} "print quotients"
|
kuncar@48153
|
518 |
(Scan.succeed (Toplevel.keep (print_quotients o Toplevel.context_of)))
|
kuncar@48153
|
519 |
|
kuncar@54788
|
520 |
end
|