| author | wenzelm | 
| Wed, 07 Aug 2024 15:11:54 +0200 | |
| changeset 80662 | ad9647592a81 | 
| parent 70323 | 2943934a169d | 
| child 80672 | 28e8d402a9ba | 
| permissions | -rw-r--r-- | 
| 47698 | 1 | (* Title: HOL/Tools/Lifting/lifting_util.ML | 
| 2 | Author: Ondrej Kuncar | |
| 3 | ||
| 4 | General-purpose functions used by the Lifting package. | |
| 5 | *) | |
| 6 | ||
| 7 | signature LIFTING_UTIL = | |
| 8 | sig | |
| 9 | val MRSL: thm list * thm -> thm | |
| 47777 
f29e7dcd7c40
use a quot_map theorem attribute instead of the complicated map attribute
 kuncar parents: 
47699diff
changeset | 10 | val dest_Quotient: term -> term * term * term * term | 
| 47951 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 11 | |
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 12 | val quot_thm_rel: thm -> term | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 13 | val quot_thm_abs: thm -> term | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 14 | val quot_thm_rep: thm -> term | 
| 50226 | 15 | val quot_thm_crel: thm -> term | 
| 47951 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 16 | val quot_thm_rty_qty: thm -> typ * typ | 
| 60229 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 17 | val Quotient_conv: conv -> conv -> conv -> conv -> conv | 
| 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 18 | val Quotient_R_conv: conv -> conv | 
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 19 | |
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 20 | val undisch: thm -> thm | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 21 | val undisch_all: thm -> thm | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 22 | val is_fun_type: typ -> bool | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 23 | val get_args: int -> term -> term list | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 24 | val strip_args: int -> term -> term | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 25 | val all_args_conv: conv -> conv | 
| 55454 | 26 | val same_type_constrs: typ * typ -> bool | 
| 27 | val Targs: typ -> typ list | |
| 28 | val Tname: typ -> string | |
| 55945 | 29 | val is_rel_fun: term -> bool | 
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 30 | val relation_types: typ -> typ * typ | 
| 55731 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 31 |   val map_interrupt: ('a -> 'b option) -> 'a list -> 'b list option
 | 
| 60229 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 32 | val conceal_naming_result: (local_theory -> 'a * local_theory) -> local_theory -> 'a * local_theory | 
| 53651 | 33 | end | 
| 47698 | 34 | |
| 35 | ||
| 36 | structure Lifting_Util: LIFTING_UTIL = | |
| 37 | struct | |
| 38 | ||
| 39 | infix 0 MRSL | |
| 40 | ||
| 41 | fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm | |
| 42 | ||
| 69593 | 43 | fun dest_Quotient (Const (\<^const_name>\<open>Quotient\<close>, _) $ rel $ abs $ rep $ cr) | 
| 47777 
f29e7dcd7c40
use a quot_map theorem attribute instead of the complicated map attribute
 kuncar parents: 
47699diff
changeset | 44 | = (rel, abs, rep, cr) | 
| 
f29e7dcd7c40
use a quot_map theorem attribute instead of the complicated map attribute
 kuncar parents: 
47699diff
changeset | 45 |   | dest_Quotient t = raise TERM ("dest_Quotient", [t])
 | 
| 
f29e7dcd7c40
use a quot_map theorem attribute instead of the complicated map attribute
 kuncar parents: 
47699diff
changeset | 46 | |
| 47951 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 47 | (* | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 48 | quot_thm_rel, quot_thm_abs, quot_thm_rep and quot_thm_rty_qty - simple functions | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 49 | for destructing quotient theorems (Quotient R Abs Rep T). | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 50 | *) | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 51 | |
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 52 | fun quot_thm_rel quot_thm = | 
| 59582 | 53 | case (dest_Quotient o HOLogic.dest_Trueprop o Thm.prop_of) quot_thm of | 
| 47951 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 54 | (rel, _, _, _) => rel | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 55 | |
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 56 | fun quot_thm_abs quot_thm = | 
| 59582 | 57 | case (dest_Quotient o HOLogic.dest_Trueprop o Thm.prop_of) quot_thm of | 
| 47951 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 58 | (_, abs, _, _) => abs | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 59 | |
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 60 | fun quot_thm_rep quot_thm = | 
| 59582 | 61 | case (dest_Quotient o HOLogic.dest_Trueprop o Thm.prop_of) quot_thm of | 
| 47951 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 62 | (_, _, rep, _) => rep | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 63 | |
| 50226 | 64 | fun quot_thm_crel quot_thm = | 
| 59582 | 65 | case (dest_Quotient o HOLogic.dest_Trueprop o Thm.prop_of) quot_thm of | 
| 50226 | 66 | (_, _, _, crel) => crel | 
| 67 | ||
| 47951 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 68 | fun quot_thm_rty_qty quot_thm = | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 69 | let | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 70 | val abs = quot_thm_abs quot_thm | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 71 | val abs_type = fastype_of abs | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 72 | in | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 73 | (domain_type abs_type, range_type abs_type) | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 74 | end | 
| 
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
 kuncar parents: 
47777diff
changeset | 75 | |
| 60229 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 76 | fun Quotient_conv R_conv Abs_conv Rep_conv T_conv = Conv.combination_conv (Conv.combination_conv | 
| 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 77 | (Conv.combination_conv (Conv.arg_conv R_conv) Abs_conv) Rep_conv) T_conv; | 
| 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 78 | |
| 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 79 | fun Quotient_R_conv R_conv = Quotient_conv R_conv Conv.all_conv Conv.all_conv Conv.all_conv; | 
| 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 80 | |
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 81 | fun undisch thm = | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 82 | let | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 83 | val assm = Thm.cprem_of thm 1 | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 84 | in | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 85 | Thm.implies_elim thm (Thm.assume assm) | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 86 | end | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 87 | |
| 59582 | 88 | fun undisch_all thm = funpow (Thm.nprems_of thm) undisch thm | 
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 89 | |
| 69593 | 90 | fun is_fun_type (Type (\<^type_name>\<open>fun\<close>, _)) = true | 
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 91 | | is_fun_type _ = false | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 92 | |
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 93 | fun get_args n = rev o fst o funpow_yield n (swap o dest_comb) | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 94 | |
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 95 | fun strip_args n = funpow n (fst o dest_comb) | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 96 | |
| 55456 | 97 | fun all_args_conv conv ctm = Conv.try_conv (Conv.combination_conv (all_args_conv conv) conv) ctm | 
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 98 | |
| 55454 | 99 | fun same_type_constrs (Type (r, _), Type (q, _)) = (r = q) | 
| 100 | | same_type_constrs _ = false | |
| 101 | ||
| 102 | fun Targs (Type (_, args)) = args | |
| 103 | | Targs _ = [] | |
| 104 | ||
| 105 | fun Tname (Type (name, _)) = name | |
| 106 | | Tname _ = "" | |
| 107 | ||
| 69593 | 108 | fun is_rel_fun (Const (\<^const_name>\<open>rel_fun\<close>, _) $ _ $ _) = true | 
| 55945 | 109 | | is_rel_fun _ = false | 
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 110 | |
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 111 | fun relation_types typ = | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 112 | case strip_type typ of | 
| 69593 | 113 | ([typ1, typ2], \<^typ>\<open>bool\<close>) => (typ1, typ2) | 
| 51374 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 114 | | _ => error "relation_types: not a relation" | 
| 
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
 kuncar parents: 
51314diff
changeset | 115 | |
| 55731 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 116 | fun map_interrupt f l = | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 117 | let | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 118 | fun map_interrupt' _ [] l = SOME (rev l) | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 119 | | map_interrupt' f (x::xs) l = (case f x of | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 120 | NONE => NONE | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 121 | | SOME v => map_interrupt' f xs (v::l)) | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 122 | in | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 123 | map_interrupt' f l [] | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 124 | end | 
| 
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
 kuncar parents: 
55456diff
changeset | 125 | |
| 60229 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 126 | fun conceal_naming_result f lthy = | 
| 70320 
59258a3192bf
misc tuning and clarification, notably wrt. flow of context;
 wenzelm parents: 
69593diff
changeset | 127 | lthy |> Proof_Context.concealed |> f ||> Proof_Context.restore_naming lthy; | 
| 60229 
4cd6462c1fda
Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
 kuncar parents: 
60223diff
changeset | 128 | |
| 53651 | 129 | end |