author | kuncar |
Fri, 05 Dec 2014 14:14:36 +0100 | |
changeset 60225 | eb4e322734bf |
parent 60224 | e759afe46a8c |
child 60227 | eacf75e4da95 |
permissions | -rw-r--r-- |
47308 | 1 |
(* Title: HOL/Tools/Lifting/lifting_def.ML |
2 |
Author: Ondrej Kuncar |
|
3 |
||
4 |
Definitions for constants on quotient types. |
|
5 |
*) |
|
6 |
||
7 |
signature LIFTING_DEF = |
|
8 |
sig |
|
60219 | 9 |
type lift_def |
10 |
val rty_of_lift_def: lift_def -> typ |
|
11 |
val qty_of_lift_def: lift_def -> typ |
|
12 |
val rhs_of_lift_def: lift_def -> term |
|
13 |
val lift_const_of_lift_def: lift_def -> term |
|
14 |
val def_thm_of_lift_def: lift_def -> thm |
|
15 |
val rsp_thm_of_lift_def: lift_def -> thm |
|
16 |
val abs_eq_of_lift_def: lift_def -> thm |
|
17 |
val rep_eq_of_lift_def: lift_def -> thm option |
|
18 |
val transfer_rules_of_lift_def: lift_def -> thm list |
|
60224 | 19 |
val morph_lift_def: morphism -> lift_def -> lift_def |
20 |
val inst_of_lift_def: Proof.context -> typ -> lift_def -> lift_def |
|
21 |
val mk_lift_const_of_lift_def: typ -> lift_def -> term |
|
60219 | 22 |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
23 |
type config = { notes: bool } |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
24 |
val default_config: config |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
25 |
|
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
26 |
val generate_parametric_transfer_rule: |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
27 |
Proof.context -> thm -> thm -> thm |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
28 |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
29 |
val add_lift_def: |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
30 |
config -> binding * mixfix -> typ -> term -> thm -> thm list -> local_theory -> |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
31 |
lift_def * local_theory |
60218 | 32 |
|
33 |
val lift_def: |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
34 |
config -> binding * mixfix -> typ -> term -> (Proof.context -> tactic) -> thm list -> |
60219 | 35 |
local_theory -> lift_def * local_theory |
47308 | 36 |
|
37 |
val lift_def_cmd: |
|
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
57664
diff
changeset
|
38 |
(binding * string option * mixfix) * string * (Facts.ref * Token.src list) list -> |
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
57664
diff
changeset
|
39 |
local_theory -> Proof.state |
47308 | 40 |
|
41 |
val can_generate_code_cert: thm -> bool |
|
53651 | 42 |
end |
47308 | 43 |
|
44 |
structure Lifting_Def: LIFTING_DEF = |
|
45 |
struct |
|
46 |
||
47698 | 47 |
open Lifting_Util |
47308 | 48 |
|
49 |
infix 0 MRSL |
|
50 |
||
60219 | 51 |
datatype lift_def = LIFT_DEF of { |
52 |
rty: typ, |
|
53 |
qty: typ, |
|
54 |
rhs: term, |
|
55 |
lift_const: term, |
|
56 |
def_thm: thm, |
|
57 |
rsp_thm: thm, |
|
58 |
abs_eq: thm, |
|
59 |
rep_eq: thm option, |
|
60 |
transfer_rules: thm list |
|
61 |
}; |
|
62 |
||
63 |
fun rep_lift_def (LIFT_DEF lift_def) = lift_def; |
|
64 |
val rty_of_lift_def = #rty o rep_lift_def; |
|
65 |
val qty_of_lift_def = #qty o rep_lift_def; |
|
66 |
val rhs_of_lift_def = #rhs o rep_lift_def; |
|
67 |
val lift_const_of_lift_def = #lift_const o rep_lift_def; |
|
68 |
val def_thm_of_lift_def = #def_thm o rep_lift_def; |
|
69 |
val rsp_thm_of_lift_def = #rsp_thm o rep_lift_def; |
|
70 |
val abs_eq_of_lift_def = #abs_eq o rep_lift_def; |
|
71 |
val rep_eq_of_lift_def = #rep_eq o rep_lift_def; |
|
72 |
val transfer_rules_of_lift_def = #transfer_rules o rep_lift_def; |
|
73 |
||
74 |
fun mk_lift_def rty qty rhs lift_const def_thm rsp_thm abs_eq rep_eq transfer_rules = |
|
75 |
LIFT_DEF {rty = rty, qty = qty, |
|
76 |
rhs = rhs, lift_const = lift_const, |
|
77 |
def_thm = def_thm, rsp_thm = rsp_thm, abs_eq = abs_eq, rep_eq = rep_eq, |
|
78 |
transfer_rules = transfer_rules }; |
|
79 |
||
60224 | 80 |
fun map_lift_def f1 f2 f3 f4 f5 f6 f7 f8 f9 |
81 |
(LIFT_DEF {rty = rty, qty = qty, rhs = rhs, lift_const = lift_const, |
|
82 |
def_thm = def_thm, rsp_thm = rsp_thm, abs_eq = abs_eq, rep_eq = rep_eq, |
|
83 |
transfer_rules = transfer_rules }) = |
|
84 |
LIFT_DEF {rty = f1 rty, qty = f2 qty, rhs = f3 rhs, lift_const = f4 lift_const, |
|
85 |
def_thm = f5 def_thm, rsp_thm = f6 rsp_thm, abs_eq = f7 abs_eq, rep_eq = f8 rep_eq, |
|
86 |
transfer_rules = f9 transfer_rules } |
|
87 |
||
88 |
fun morph_lift_def phi = |
|
89 |
let |
|
90 |
val mtyp = Morphism.typ phi |
|
91 |
val mterm = Morphism.term phi |
|
92 |
val mthm = Morphism.thm phi |
|
93 |
in |
|
94 |
map_lift_def mtyp mtyp mterm mterm mthm mthm mthm (Option.map mthm) (map mthm) |
|
95 |
end |
|
96 |
||
97 |
fun mk_inst_of_lift_def qty lift_def = Vartab.empty |> Type.raw_match (qty_of_lift_def lift_def, qty) |
|
98 |
||
99 |
fun mk_lift_const_of_lift_def qty lift_def = Envir.subst_term_types (mk_inst_of_lift_def qty lift_def) |
|
100 |
(lift_const_of_lift_def lift_def) |
|
101 |
||
102 |
fun inst_of_lift_def ctxt qty lift_def = mk_inst_of_lift_def qty lift_def |
|
103 |
|> instT_morphism ctxt |> (fn phi => morph_lift_def phi lift_def) |
|
104 |
||
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
105 |
(* Config *) |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
106 |
|
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
107 |
type config = { notes: bool }; |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
108 |
val default_config = { notes = true }; |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
109 |
|
51994 | 110 |
(* Reflexivity prover *) |
111 |
||
55609 | 112 |
fun mono_eq_prover ctxt prop = |
51994 | 113 |
let |
56518
beb3b6851665
left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
kuncar
parents:
56245
diff
changeset
|
114 |
val refl_rules = Lifting_Info.get_reflexivity_rules ctxt |
beb3b6851665
left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
kuncar
parents:
56245
diff
changeset
|
115 |
val transfer_rules = Transfer.get_transfer_raw ctxt |
beb3b6851665
left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
kuncar
parents:
56245
diff
changeset
|
116 |
|
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59458
diff
changeset
|
117 |
fun main_tac i = (REPEAT_ALL_NEW (DETERM o resolve_tac ctxt refl_rules) THEN_ALL_NEW |
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59458
diff
changeset
|
118 |
(REPEAT_ALL_NEW (DETERM o resolve_tac ctxt transfer_rules))) i |
51994 | 119 |
in |
56540 | 120 |
SOME (Goal.prove ctxt [] [] prop (K (main_tac 1))) |
55609 | 121 |
handle ERROR _ => NONE |
51994 | 122 |
end |
56518
beb3b6851665
left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
kuncar
parents:
56245
diff
changeset
|
123 |
|
57642
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
124 |
fun try_prove_refl_rel ctxt rel = |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
125 |
let |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
126 |
fun mk_ge_eq x = |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
127 |
let |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
128 |
val T = fastype_of x |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
129 |
in |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
130 |
Const (@{const_name "less_eq"}, T --> T --> HOLogic.boolT) $ |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
131 |
(Const (@{const_name HOL.eq}, T)) $ x |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
132 |
end; |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
133 |
val goal = HOLogic.mk_Trueprop (mk_ge_eq rel); |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
134 |
in |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
135 |
mono_eq_prover ctxt goal |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
136 |
end; |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
137 |
|
51994 | 138 |
fun try_prove_reflexivity ctxt prop = |
55609 | 139 |
let |
59630 | 140 |
val cprop = Thm.cterm_of ctxt prop |
55609 | 141 |
val rule = @{thm ge_eq_refl} |
59582 | 142 |
val concl_pat = Drule.strip_imp_concl (Thm.cprop_of rule) |
55609 | 143 |
val insts = Thm.first_order_match (concl_pat, cprop) |
144 |
val rule = Drule.instantiate_normalize insts rule |
|
59582 | 145 |
val prop = hd (Thm.prems_of rule) |
55609 | 146 |
in |
147 |
case mono_eq_prover ctxt prop of |
|
55610
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
148 |
SOME thm => SOME (thm RS rule) |
55609 | 149 |
| NONE => NONE |
150 |
end |
|
51994 | 151 |
|
54335
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
152 |
(* |
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
153 |
Generates a parametrized transfer rule. |
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
154 |
transfer_rule - of the form T t f |
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
155 |
parametric_transfer_rule - of the form par_R t' t |
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
156 |
|
54947 | 157 |
Result: par_T t' f, after substituing op= for relations in par_R that relate |
54335
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
158 |
a type constructor to the same type constructor, it is a merge of (par_R' OO T) t' f |
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
159 |
using Lifting_Term.merge_transfer_relations |
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
160 |
*) |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
161 |
|
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
162 |
fun generate_parametric_transfer_rule ctxt transfer_rule parametric_transfer_rule = |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
163 |
let |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
164 |
fun preprocess ctxt thm = |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
165 |
let |
59582 | 166 |
val tm = (strip_args 2 o HOLogic.dest_Trueprop o Thm.concl_of) thm; |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
167 |
val param_rel = (snd o dest_comb o fst o dest_comb) tm; |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
168 |
val free_vars = Term.add_vars param_rel []; |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
169 |
|
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
170 |
fun make_subst (var as (_, typ)) subst = |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
171 |
let |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
172 |
val [rty, rty'] = binder_types typ |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
173 |
in |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
174 |
if (Term.is_TVar rty andalso is_Type rty') then |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
175 |
(Var var, HOLogic.eq_const rty')::subst |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
176 |
else |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
177 |
subst |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
178 |
end; |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
179 |
|
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
180 |
val subst = fold make_subst free_vars []; |
59630 | 181 |
val csubst = map (apply2 (Thm.cterm_of ctxt)) subst; |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
182 |
val inst_thm = Drule.cterm_instantiate csubst thm; |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
183 |
in |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
184 |
Conv.fconv_rule |
59582 | 185 |
((Conv.concl_conv (Thm.nprems_of inst_thm) o |
186 |
HOLogic.Trueprop_conv o Conv.fun2_conv o Conv.arg1_conv) |
|
54742
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents:
54335
diff
changeset
|
187 |
(Raw_Simplifier.rewrite ctxt false (Transfer.get_sym_relator_eq ctxt))) inst_thm |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
188 |
end |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
189 |
|
59630 | 190 |
fun inst_relcomppI ctxt ant1 ant2 = |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
191 |
let |
59582 | 192 |
val t1 = (HOLogic.dest_Trueprop o Thm.concl_of) ant1 |
193 |
val t2 = (HOLogic.dest_Trueprop o Thm.prop_of) ant2 |
|
59630 | 194 |
val fun1 = Thm.cterm_of ctxt (strip_args 2 t1) |
195 |
val args1 = map (Thm.cterm_of ctxt) (get_args 2 t1) |
|
196 |
val fun2 = Thm.cterm_of ctxt (strip_args 2 t2) |
|
197 |
val args2 = map (Thm.cterm_of ctxt) (get_args 1 t2) |
|
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
198 |
val relcomppI = Drule.incr_indexes2 ant1 ant2 @{thm relcomppI} |
59582 | 199 |
val vars = (rev (Term.add_vars (Thm.prop_of relcomppI) [])) |
59630 | 200 |
val subst = map (apfst (Thm.cterm_of ctxt o Var)) (vars ~~ ([fun1] @ args1 @ [fun2] @ args2)) |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
201 |
in |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
202 |
Drule.cterm_instantiate subst relcomppI |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
203 |
end |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
204 |
|
54995 | 205 |
fun zip_transfer_rules ctxt thm = |
206 |
let |
|
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
207 |
fun mk_POS ty = Const (@{const_name POS}, ty --> ty --> HOLogic.boolT) |
59582 | 208 |
val rel = (Thm.dest_fun2 o Thm.dest_arg o Thm.cprop_of) thm |
59586 | 209 |
val typ = Thm.typ_of_cterm rel |
59630 | 210 |
val POS_const = Thm.cterm_of ctxt (mk_POS typ) |
211 |
val var = Thm.cterm_of ctxt (Var (("X", Thm.maxidx_of_cterm rel + 1), typ)) |
|
59582 | 212 |
val goal = |
59630 | 213 |
Thm.apply (Thm.cterm_of ctxt HOLogic.Trueprop) (Thm.apply (Thm.apply POS_const rel) var) |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
214 |
in |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
215 |
[Lifting_Term.merge_transfer_relations ctxt goal, thm] MRSL @{thm POS_apply} |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
216 |
end |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
217 |
|
59630 | 218 |
val thm = |
219 |
inst_relcomppI ctxt parametric_transfer_rule transfer_rule |
|
220 |
OF [parametric_transfer_rule, transfer_rule] |
|
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
221 |
val preprocessed_thm = preprocess ctxt thm |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
222 |
val orig_ctxt = ctxt |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
223 |
val (fixed_thm, ctxt) = yield_singleton (apfst snd oo Variable.import true) preprocessed_thm ctxt |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
224 |
val assms = cprems_of fixed_thm |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
225 |
val add_transfer_rule = Thm.attribute_declaration Transfer.transfer_add |
54995 | 226 |
val (prems, ctxt) = fold_map Thm.assume_hyps assms ctxt |
227 |
val ctxt = Context.proof_map (fold add_transfer_rule prems) ctxt |
|
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
228 |
val zipped_thm = |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
229 |
fixed_thm |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
230 |
|> undisch_all |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
231 |
|> zip_transfer_rules ctxt |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
232 |
|> implies_intr_list assms |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
233 |
|> singleton (Variable.export ctxt orig_ctxt) |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
234 |
in |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
235 |
zipped_thm |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
236 |
end |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
237 |
|
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
238 |
fun print_generate_transfer_info msg = |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
239 |
let |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
240 |
val error_msg = cat_lines |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
241 |
["Generation of a parametric transfer rule failed.", |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
242 |
(Pretty.string_of (Pretty.block |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
243 |
[Pretty.str "Reason:", Pretty.brk 2, msg]))] |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
244 |
in |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
245 |
error error_msg |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
246 |
end |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
247 |
|
53951
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
248 |
fun map_ter _ x [] = x |
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
249 |
| map_ter f _ xs = map f xs |
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
250 |
|
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
251 |
fun generate_transfer_rules lthy quot_thm rsp_thm def_thm par_thms = |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
252 |
let |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
253 |
val transfer_rule = |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
254 |
([quot_thm, rsp_thm, def_thm] MRSL @{thm Quotient_to_transfer}) |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
255 |
|> Lifting_Term.parametrize_transfer_rule lthy |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
256 |
in |
53951
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
257 |
(map_ter (generate_parametric_transfer_rule lthy transfer_rule) [transfer_rule] par_thms |
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
258 |
handle Lifting_Term.MERGE_TRANSFER_REL msg => (print_generate_transfer_info msg; [transfer_rule])) |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
259 |
end |
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
260 |
|
47698 | 261 |
(* Generation of the code certificate from the rsp theorem *) |
47308 | 262 |
|
263 |
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V) |
|
264 |
| get_body_types (U, V) = (U, V) |
|
265 |
||
266 |
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W) |
|
267 |
| get_binder_types _ = [] |
|
268 |
||
55945 | 269 |
fun get_binder_types_by_rel (Const (@{const_name "rel_fun"}, _) $ _ $ S) (Type ("fun", [T, U]), Type ("fun", [V, W])) = |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
270 |
(T, V) :: get_binder_types_by_rel S (U, W) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
271 |
| get_binder_types_by_rel _ _ = [] |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
272 |
|
55945 | 273 |
fun get_body_type_by_rel (Const (@{const_name "rel_fun"}, _) $ _ $ S) (Type ("fun", [_, U]), Type ("fun", [_, V])) = |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
274 |
get_body_type_by_rel S (U, V) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
275 |
| get_body_type_by_rel _ (U, V) = (U, V) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
276 |
|
57642
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
277 |
fun get_binder_rels (Const (@{const_name "rel_fun"}, _) $ R $ S) = R :: get_binder_rels S |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
278 |
| get_binder_rels _ = [] |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
279 |
|
47308 | 280 |
fun force_rty_type ctxt rty rhs = |
281 |
let |
|
282 |
val thy = Proof_Context.theory_of ctxt |
|
283 |
val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs |
|
284 |
val rty_schematic = fastype_of rhs_schematic |
|
285 |
val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty |
|
286 |
in |
|
287 |
Envir.subst_term_types match rhs_schematic |
|
288 |
end |
|
289 |
||
290 |
fun unabs_def ctxt def = |
|
291 |
let |
|
59582 | 292 |
val (_, rhs) = Thm.dest_equals (Thm.cprop_of def) |
47308 | 293 |
fun dest_abs (Abs (var_name, T, _)) = (var_name, T) |
294 |
| dest_abs tm = raise TERM("get_abs_var",[tm]) |
|
59582 | 295 |
val (var_name, T) = dest_abs (Thm.term_of rhs) |
47308 | 296 |
val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt |
59630 | 297 |
val refl_thm = Thm.reflexive (Thm.cterm_of ctxt' (Free (hd new_var_names, T))) |
47308 | 298 |
in |
299 |
Thm.combination def refl_thm |> |
|
300 |
singleton (Proof_Context.export ctxt' ctxt) |
|
301 |
end |
|
302 |
||
303 |
fun unabs_all_def ctxt def = |
|
304 |
let |
|
59582 | 305 |
val (_, rhs) = Thm.dest_equals (Thm.cprop_of def) |
306 |
val xs = strip_abs_vars (Thm.term_of rhs) |
|
47308 | 307 |
in |
308 |
fold (K (unabs_def ctxt)) xs def |
|
309 |
end |
|
310 |
||
311 |
val map_fun_unfolded = |
|
312 |
@{thm map_fun_def[abs_def]} |> |
|
313 |
unabs_def @{context} |> |
|
314 |
unabs_def @{context} |> |
|
315 |
Local_Defs.unfold @{context} [@{thm comp_def}] |
|
316 |
||
317 |
fun unfold_fun_maps ctm = |
|
318 |
let |
|
319 |
fun unfold_conv ctm = |
|
320 |
case (Thm.term_of ctm) of |
|
321 |
Const (@{const_name "map_fun"}, _) $ _ $ _ => |
|
322 |
(Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm |
|
323 |
| _ => Conv.all_conv ctm |
|
324 |
in |
|
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
325 |
(Conv.fun_conv unfold_conv) ctm |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
326 |
end |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
327 |
|
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
328 |
fun unfold_fun_maps_beta ctm = |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
329 |
let val try_beta_conv = Conv.try_conv (Thm.beta_conversion false) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
330 |
in |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
331 |
(unfold_fun_maps then_conv try_beta_conv) ctm |
47308 | 332 |
end |
333 |
||
334 |
fun prove_rel ctxt rsp_thm (rty, qty) = |
|
335 |
let |
|
336 |
val ty_args = get_binder_types (rty, qty) |
|
337 |
fun disch_arg args_ty thm = |
|
338 |
let |
|
47504
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
339 |
val quot_thm = Lifting_Term.prove_quot_thm ctxt args_ty |
47308 | 340 |
in |
341 |
[quot_thm, thm] MRSL @{thm apply_rsp''} |
|
342 |
end |
|
343 |
in |
|
344 |
fold disch_arg ty_args rsp_thm |
|
345 |
end |
|
346 |
||
347 |
exception CODE_CERT_GEN of string |
|
348 |
||
349 |
fun simplify_code_eq ctxt def_thm = |
|
48024 | 350 |
Local_Defs.unfold ctxt [@{thm o_apply}, @{thm map_fun_def}, @{thm id_apply}] def_thm |
47308 | 351 |
|
47852
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
352 |
(* |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
353 |
quot_thm - quotient theorem (Quotient R Abs Rep T). |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
354 |
returns: whether the Lifting package is capable to generate code for the abstract type |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
355 |
represented by quot_thm |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
356 |
*) |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
357 |
|
47308 | 358 |
fun can_generate_code_cert quot_thm = |
47951
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
kuncar
parents:
47937
diff
changeset
|
359 |
case quot_thm_rel quot_thm of |
47308 | 360 |
Const (@{const_name HOL.eq}, _) => true |
56519
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
361 |
| Const (@{const_name eq_onp}, _) $ _ => true |
47308 | 362 |
| _ => false |
363 |
||
55610
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
364 |
fun generate_rep_eq ctxt def_thm rsp_thm (rty, qty) = |
47308 | 365 |
let |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
366 |
val unfolded_def = Conv.fconv_rule (Conv.arg_conv unfold_fun_maps_beta) def_thm |
47308 | 367 |
val unabs_def = unabs_all_def ctxt unfolded_def |
55610
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
368 |
in |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
369 |
if body_type rty = body_type qty then |
55723 | 370 |
SOME (simplify_code_eq ctxt (unabs_def RS @{thm meta_eq_to_obj_eq})) |
55610
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
371 |
else |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
372 |
let |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
373 |
val quot_thm = Lifting_Term.prove_quot_thm ctxt (get_body_types (rty, qty)) |
55945 | 374 |
val rel_fun = prove_rel ctxt rsp_thm (rty, qty) |
375 |
val rep_abs_thm = [quot_thm, rel_fun] MRSL @{thm Quotient_rep_abs_eq} |
|
55610
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
376 |
in |
59582 | 377 |
case mono_eq_prover ctxt (hd (Thm.prems_of rep_abs_thm)) of |
55610
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
378 |
SOME mono_eq_thm => |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
379 |
let |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
380 |
val rep_abs_eq = mono_eq_thm RS rep_abs_thm |
59630 | 381 |
val rep = Thm.cterm_of ctxt (quot_thm_rep quot_thm) |
55610
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
382 |
val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq} |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
383 |
val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong} |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
384 |
val code_cert = [repped_eq, rep_abs_eq] MRSL trans |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
385 |
in |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
386 |
SOME (simplify_code_eq ctxt code_cert) |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
387 |
end |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
388 |
| NONE => NONE |
9066b603dff6
refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents:
55609
diff
changeset
|
389 |
end |
47308 | 390 |
end |
391 |
||
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
392 |
fun generate_abs_eq ctxt def_thm rsp_thm quot_thm = |
47308 | 393 |
let |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
394 |
val abs_eq_with_assms = |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
395 |
let |
47951
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
kuncar
parents:
47937
diff
changeset
|
396 |
val (rty, qty) = quot_thm_rty_qty quot_thm |
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
kuncar
parents:
47937
diff
changeset
|
397 |
val rel = quot_thm_rel quot_thm |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
398 |
val ty_args = get_binder_types_by_rel rel (rty, qty) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
399 |
val body_type = get_body_type_by_rel rel (rty, qty) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
400 |
val quot_ret_thm = Lifting_Term.prove_quot_thm ctxt body_type |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
401 |
|
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
402 |
val rep_abs_folded_unmapped_thm = |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
403 |
let |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
404 |
val rep_id = [quot_thm, def_thm] MRSL @{thm Quotient_Rep_eq} |
59582 | 405 |
val ctm = Thm.dest_equals_lhs (Thm.cprop_of rep_id) |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
406 |
val unfolded_maps_eq = unfold_fun_maps ctm |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
407 |
val t1 = [quot_thm, def_thm, rsp_thm] MRSL @{thm Quotient_rep_abs_fold_unmap} |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
408 |
val prems_pat = (hd o Drule.cprems_of) t1 |
59582 | 409 |
val insts = Thm.first_order_match (prems_pat, Thm.cprop_of unfolded_maps_eq) |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
410 |
in |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
411 |
unfolded_maps_eq RS (Drule.instantiate_normalize insts t1) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
412 |
end |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
413 |
in |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
414 |
rep_abs_folded_unmapped_thm |
55945 | 415 |
|> fold (fn _ => fn thm => thm RS @{thm rel_funD2}) ty_args |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
416 |
|> (fn x => x RS (@{thm Quotient_rel_abs2} OF [quot_ret_thm])) |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
417 |
end |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
418 |
|
57642
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
419 |
val prem_rels = get_binder_rels (quot_thm_rel quot_thm); |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
420 |
val proved_assms = prem_rels |> map (try_prove_refl_rel ctxt) |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
421 |
|> map_index (apfst (fn x => x + 1)) |> filter (is_some o snd) |> map (apsnd the) |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
422 |
|> map (apsnd (fn assm => assm RS @{thm ge_eq_refl})) |
5bc43a73d768
prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents:
56540
diff
changeset
|
423 |
val abs_eq = fold_rev (fn (i, assm) => fn thm => assm RSN (i, thm)) proved_assms abs_eq_with_assms |
47308 | 424 |
in |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
425 |
simplify_code_eq ctxt abs_eq |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
426 |
end |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
427 |
|
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
428 |
|
55724 | 429 |
fun register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy = |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
430 |
let |
55724 | 431 |
fun no_abstr (t $ u) = no_abstr t andalso no_abstr u |
432 |
| no_abstr (Abs (_, _, t)) = no_abstr t |
|
433 |
| no_abstr (Const (name, _)) = not (Code.is_abstr thy name) |
|
434 |
| no_abstr _ = true |
|
435 |
fun is_valid_eq eqn = can (Code.assert_eqn thy) (mk_meta_eq eqn, true) |
|
59582 | 436 |
andalso no_abstr (Thm.prop_of eqn) |
55724 | 437 |
fun is_valid_abs_eq abs_eq = can (Code.assert_abs_eqn thy NONE) (mk_meta_eq abs_eq) |
47308 | 438 |
|
439 |
in |
|
55724 | 440 |
if is_valid_eq abs_eq_thm then |
441 |
Code.add_default_eqn abs_eq_thm thy |
|
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
442 |
else |
55724 | 443 |
let |
444 |
val (rty_body, qty_body) = get_body_types (rty, qty) |
|
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
445 |
in |
55724 | 446 |
if rty_body = qty_body then |
447 |
Code.add_default_eqn (the opt_rep_eq_thm) thy |
|
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
448 |
else |
55724 | 449 |
if is_some opt_rep_eq_thm andalso is_valid_abs_eq (the opt_rep_eq_thm) |
450 |
then |
|
59458 | 451 |
Code.add_abs_default_eqn (the opt_rep_eq_thm) thy |
55724 | 452 |
else |
453 |
thy |
|
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
454 |
end |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
455 |
end |
47308 | 456 |
|
55724 | 457 |
local |
59630 | 458 |
fun encode_code_eq ctxt abs_eq opt_rep_eq (rty, qty) = |
55724 | 459 |
let |
59630 | 460 |
fun mk_type typ = typ |> Logic.mk_type |> Thm.cterm_of ctxt |> Drule.mk_term |
55724 | 461 |
in |
462 |
Conjunction.intr_balanced [abs_eq, (the_default TrueI opt_rep_eq), mk_type rty, mk_type qty] |
|
463 |
end |
|
464 |
||
57664
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
465 |
exception DECODE |
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
466 |
|
55724 | 467 |
fun decode_code_eq thm = |
59582 | 468 |
if Thm.nprems_of thm > 0 then raise DECODE |
57664
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
469 |
else |
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
470 |
let |
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
471 |
val [abs_eq, rep_eq, rty, qty] = Conjunction.elim_balanced 4 thm |
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
472 |
val opt_rep_eq = if Thm.eq_thm_prop (rep_eq, TrueI) then NONE else SOME rep_eq |
59582 | 473 |
fun dest_type typ = typ |> Drule.dest_term |> Thm.term_of |> Logic.dest_type |
57664
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
474 |
in |
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
475 |
(abs_eq, opt_rep_eq, (dest_type rty, dest_type qty)) |
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
476 |
end |
55724 | 477 |
|
478 |
fun register_encoded_code_eq thm thy = |
|
479 |
let |
|
480 |
val (abs_eq_thm, opt_rep_eq_thm, (rty, qty)) = decode_code_eq thm |
|
481 |
in |
|
482 |
register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy |
|
483 |
end |
|
57664
179b9c43fe84
having extra assumptions (typically from a context) means there is no chance to have a valid code equation => skip decoding and registration of the code equations
kuncar
parents:
57663
diff
changeset
|
484 |
handle DECODE => thy |
55724 | 485 |
|
486 |
val register_code_eq_attribute = Thm.declaration_attribute |
|
487 |
(fn thm => Context.mapping (register_encoded_code_eq thm) I) |
|
488 |
val register_code_eq_attrib = Attrib.internal (K register_code_eq_attribute) |
|
57663
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
489 |
|
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
490 |
fun no_no_code ctxt (rty, qty) = |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
491 |
if same_type_constrs (rty, qty) then |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
492 |
forall (no_no_code ctxt) (Targs rty ~~ Targs qty) |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
493 |
else |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
494 |
if is_Type qty then |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
495 |
if Lifting_Info.is_no_code_type ctxt (Tname qty) then false |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
496 |
else |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
497 |
let |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
498 |
val (rty', rtyq) = Lifting_Term.instantiate_rtys ctxt (rty, qty) |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
499 |
val (rty's, rtyqs) = (Targs rty', Targs rtyq) |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
500 |
in |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
501 |
forall (no_no_code ctxt) (rty's ~~ rtyqs) |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
502 |
end |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
503 |
else |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
504 |
true |
55724 | 505 |
in |
506 |
||
507 |
fun register_code_eq abs_eq_thm opt_rep_eq_thm (rty, qty) lthy = |
|
508 |
let |
|
59630 | 509 |
val encoded_code_eq = encode_code_eq lthy abs_eq_thm opt_rep_eq_thm (rty, qty) |
55724 | 510 |
in |
57663
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
511 |
if no_no_code lthy (rty, qty) then |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
512 |
(snd oo Local_Theory.note) ((Binding.empty, [register_code_eq_attrib]), [encoded_code_eq]) lthy |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
513 |
else |
b590fcd03a4a
store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents:
57642
diff
changeset
|
514 |
lthy |
55724 | 515 |
end |
516 |
end |
|
517 |
||
47852
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
518 |
(* |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
519 |
Defines an operation on an abstract type in terms of a corresponding operation |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
520 |
on a representation type. |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
521 |
|
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
522 |
var - a binding and a mixfix of the new constant being defined |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
523 |
qty - an abstract type of the new constant |
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
524 |
rhs - a term representing the new constant on the raw level |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
525 |
rsp_thm - a respectfulness theorem in the internal tagged form (like '(R ===> R ===> R) f f'), |
47852
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
526 |
i.e. "(Lifting_Term.equiv_relation (fastype_of rhs, qty)) $ rhs $ rhs" |
54335
03b10317ba78
update documentation of important public ML functions in Lifting
kuncar
parents:
53951
diff
changeset
|
527 |
par_thms - a parametricity theorem for rhs |
47852
0c3b8d036a5c
documentation of the Lifting package on the ML level & tuned
kuncar
parents:
47699
diff
changeset
|
528 |
*) |
47308 | 529 |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
530 |
fun add_lift_def config var qty rhs rsp_thm par_thms lthy = |
47308 | 531 |
let |
532 |
val rty = fastype_of rhs |
|
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
533 |
val quot_thm = Lifting_Term.prove_quot_thm lthy (rty, qty) |
47951
8c8a03765de7
quot_del attribute, it allows us to deregister quotient types
kuncar
parents:
47937
diff
changeset
|
534 |
val absrep_trm = quot_thm_abs quot_thm |
47308 | 535 |
val rty_forced = (domain_type o fastype_of) absrep_trm |
536 |
val forced_rhs = force_rty_type lthy rty_forced rhs |
|
53207
9745b7d4cc79
prefer Binding.name_of over Binding.print -- the latter leads to funny quotes and markup within the constructed term;
wenzelm
parents:
53205
diff
changeset
|
537 |
val lhs = Free (Binding.name_of (#1 var), qty) |
47308 | 538 |
val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs) |
539 |
val (_, prop') = Local_Defs.cert_def lthy prop |
|
540 |
val (_, newrhs) = Local_Defs.abs_def prop' |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
541 |
val var = (#notes config = false ? apfst Binding.concealed) var |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
542 |
val def_name = if #notes config then Thm.def_binding (#1 var) else Binding.empty |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
543 |
|
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
544 |
val ((lift_const, (_ , def_thm)), lthy) = |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
545 |
Local_Theory.define (var, ((def_name, []), newrhs)) lthy |
47308 | 546 |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
547 |
val transfer_rules = generate_transfer_rules lthy quot_thm rsp_thm def_thm par_thms |
49975
faf4afed009f
transfer package: more flexible handling of equality relations using is_equality predicate
huffman
parents:
49885
diff
changeset
|
548 |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
549 |
val abs_eq_thm = generate_abs_eq lthy def_thm rsp_thm quot_thm |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
550 |
val opt_rep_eq_thm = generate_rep_eq lthy def_thm rsp_thm (rty_forced, qty) |
47351 | 551 |
|
47545 | 552 |
fun qualify defname suffix = Binding.qualified true suffix defname |
47308 | 553 |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
554 |
fun notes names = |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
555 |
let |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
556 |
val lhs_name = (#1 var) |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
557 |
val rsp_thmN = qualify lhs_name "rsp" |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
558 |
val abs_eq_thmN = qualify lhs_name "abs_eq" |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
559 |
val rep_eq_thmN = qualify lhs_name "rep_eq" |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
560 |
val transfer_ruleN = qualify lhs_name "transfer" |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
561 |
val notes = |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
562 |
[(rsp_thmN, [], [rsp_thm]), |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
563 |
(transfer_ruleN, @{attributes [transfer_rule]}, transfer_rules), |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
564 |
(abs_eq_thmN, [], [abs_eq_thm])] |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
565 |
@ (case opt_rep_eq_thm of SOME rep_eq_thm => [(rep_eq_thmN, [], [rep_eq_thm])] | NONE => []) |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
566 |
in |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
567 |
if names then map (fn (name, attrs, thms) => ((name, []), [(thms, attrs)])) notes |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
568 |
else map_filter (fn (_, attrs, thms) => if null attrs then NONE |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
569 |
else SOME ((Binding.empty, []), [(thms, attrs)])) notes |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
570 |
end |
60219 | 571 |
val lift_def = mk_lift_def rty_forced qty newrhs lift_const def_thm rsp_thm abs_eq_thm |
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
572 |
opt_rep_eq_thm transfer_rules |
47308 | 573 |
in |
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
574 |
lthy |
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
575 |
|> Local_Theory.notes (notes (#notes config)) |> snd |
55724 | 576 |
|> register_code_eq abs_eq_thm opt_rep_eq_thm (rty_forced, qty) |
60224 | 577 |
|> ` (fn lthy => morph_lift_def (Local_Theory.target_morphism lthy) lift_def) |
578 |
||> Local_Theory.restore |
|
47308 | 579 |
end |
580 |
||
55731
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
581 |
local |
56519
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
582 |
val eq_onp_assms_tac_fixed_rules = map (Transfer.prep_transfer_domain_thm @{context}) |
55731
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
583 |
[@{thm pcr_Domainp_total}, @{thm pcr_Domainp_par_left_total}, @{thm pcr_Domainp_par}, |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
584 |
@{thm pcr_Domainp}] |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
585 |
in |
47308 | 586 |
fun mk_readable_rsp_thm_eq tm lthy = |
587 |
let |
|
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59586
diff
changeset
|
588 |
val ctm = Thm.cterm_of lthy tm |
47308 | 589 |
|
55731
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
590 |
fun assms_rewr_conv tactic rule ct = |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
591 |
let |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
592 |
fun prove_extra_assms thm = |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
593 |
let |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
594 |
val assms = cprems_of thm |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
595 |
fun finish thm = if Thm.no_prems thm then SOME (Goal.conclude thm) else NONE |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
596 |
fun prove ctm = Option.mapPartial finish (SINGLE tactic (Goal.init ctm)) |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
597 |
in |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
598 |
map_interrupt prove assms |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
599 |
end |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
600 |
|
59582 | 601 |
fun cconl_of thm = Drule.strip_imp_concl (Thm.cprop_of thm) |
55731
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
602 |
fun lhs_of thm = fst (Thm.dest_equals (cconl_of thm)) |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
603 |
fun rhs_of thm = snd (Thm.dest_equals (cconl_of thm)) |
59586 | 604 |
val rule1 = Thm.incr_indexes (Thm.maxidx_of_cterm ct + 1) rule; |
55731
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
605 |
val lhs = lhs_of rule1; |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
606 |
val rule2 = Thm.rename_boundvars (Thm.term_of lhs) (Thm.term_of ct) rule1; |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
607 |
val rule3 = |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
608 |
Thm.instantiate (Thm.match (lhs, ct)) rule2 |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
609 |
handle Pattern.MATCH => raise CTERM ("assms_rewr_conv", [lhs, ct]); |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
610 |
val proved_assms = prove_extra_assms rule3 |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
611 |
in |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
612 |
case proved_assms of |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
613 |
SOME proved_assms => |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
614 |
let |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
615 |
val rule3 = proved_assms MRSL rule3 |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
616 |
val rule4 = |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
617 |
if lhs_of rule3 aconvc ct then rule3 |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
618 |
else |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
619 |
let val ceq = Thm.dest_fun2 (Thm.cprop_of rule3) |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
620 |
in rule3 COMP Thm.trivial (Thm.mk_binop ceq ct (rhs_of rule3)) end |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
621 |
in Thm.transitive rule4 (Thm.beta_conversion true (rhs_of rule4)) end |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
622 |
| NONE => Conv.no_conv ct |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
623 |
end |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
624 |
|
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
625 |
fun assms_rewrs_conv tactic rules = Conv.first_conv (map (assms_rewr_conv tactic) rules) |
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
626 |
|
47308 | 627 |
fun simp_arrows_conv ctm = |
628 |
let |
|
629 |
val unfold_conv = Conv.rewrs_conv |
|
56519
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
630 |
[@{thm rel_fun_eq_eq_onp[THEN eq_reflection]}, |
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
631 |
@{thm rel_fun_eq_onp_rel[THEN eq_reflection]}, |
55945 | 632 |
@{thm rel_fun_eq[THEN eq_reflection]}, |
633 |
@{thm rel_fun_eq_rel[THEN eq_reflection]}, |
|
634 |
@{thm rel_fun_def[THEN eq_reflection]}] |
|
47308 | 635 |
fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2 |
56519
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
636 |
val eq_onp_assms_tac_rules = @{thm left_unique_OO} :: |
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
637 |
eq_onp_assms_tac_fixed_rules @ (Transfer.get_transfer_raw lthy) |
60221 | 638 |
val intro_top_rule = @{thm eq_onp_top_eq_eq[symmetric, THEN eq_reflection]} |
639 |
val kill_tops = Transfer.top_sweep_rewr_conv [@{thm eq_onp_top_eq_eq[THEN eq_reflection]}] |
|
640 |
val eq_onp_assms_tac = (CONVERSION kill_tops THEN' |
|
641 |
TRY o REPEAT_ALL_NEW (resolve_tac lthy eq_onp_assms_tac_rules) |
|
55731
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
642 |
THEN_ALL_NEW (DETERM o Transfer.eq_tac lthy)) 1 |
56519
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
643 |
val relator_eq_onp_conv = Conv.bottom_conv |
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
644 |
(K (Conv.try_conv (assms_rewrs_conv eq_onp_assms_tac |
60221 | 645 |
(intro_top_rule :: Lifting_Info.get_relator_eq_onp_rules lthy)))) lthy |
646 |
then_conv kill_tops |
|
49626 | 647 |
val relator_eq_conv = Conv.bottom_conv |
648 |
(K (Conv.try_conv (Conv.rewrs_conv (Transfer.get_relator_eq lthy)))) lthy |
|
47308 | 649 |
in |
650 |
case (Thm.term_of ctm) of |
|
55945 | 651 |
Const (@{const_name "rel_fun"}, _) $ _ $ _ => |
49626 | 652 |
(binop_conv2 simp_arrows_conv simp_arrows_conv then_conv unfold_conv) ctm |
56519
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
653 |
| _ => (relator_eq_onp_conv then_conv relator_eq_conv) ctm |
47308 | 654 |
end |
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47852
diff
changeset
|
655 |
|
47308 | 656 |
val unfold_ret_val_invs = Conv.bottom_conv |
56524
f4ba736040fa
setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
56519
diff
changeset
|
657 |
(K (Conv.try_conv (Conv.rewr_conv @{thm eq_onp_same_args[THEN eq_reflection]}))) lthy |
55773 | 658 |
val unfold_inv_conv = |
56519
c1048f5bbb45
more appropriate name (Lifting.invariant -> eq_onp)
kuncar
parents:
56518
diff
changeset
|
659 |
Conv.top_sweep_conv (K (Conv.rewr_conv @{thm eq_onp_def[THEN eq_reflection]})) lthy |
56540 | 660 |
val simp_conv = HOLogic.Trueprop_conv (Conv.fun2_conv simp_arrows_conv) |
47308 | 661 |
val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]} |
662 |
val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy |
|
663 |
val beta_conv = Thm.beta_conversion true |
|
664 |
val eq_thm = |
|
55773 | 665 |
(simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs |
666 |
then_conv unfold_inv_conv) ctm |
|
47308 | 667 |
in |
54742
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents:
54335
diff
changeset
|
668 |
Object_Logic.rulify lthy (eq_thm RS Drule.equal_elim_rule2) |
47308 | 669 |
end |
55731
66df76dd2640
rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents:
55724
diff
changeset
|
670 |
end |
47308 | 671 |
|
47607 | 672 |
fun rename_to_tnames ctxt term = |
673 |
let |
|
56245 | 674 |
fun all_typs (Const (@{const_name Pure.all}, _) $ Abs (_, T, t)) = T :: all_typs t |
47607 | 675 |
| all_typs _ = [] |
47308 | 676 |
|
56245 | 677 |
fun rename (Const (@{const_name Pure.all}, T1) $ Abs (_, T2, t)) (new_name :: names) = |
678 |
(Const (@{const_name Pure.all}, T1) $ Abs (new_name, T2, rename t names)) |
|
47607 | 679 |
| rename t _ = t |
680 |
||
681 |
val (fixed_def_t, _) = yield_singleton (Variable.importT_terms) term ctxt |
|
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
58028
diff
changeset
|
682 |
val new_names = Old_Datatype_Prop.make_tnames (all_typs fixed_def_t) |
47607 | 683 |
in |
684 |
rename term new_names |
|
685 |
end |
|
47308 | 686 |
|
56540 | 687 |
(* This is not very cheap way of getting the rules but we have only few active |
688 |
liftings in the current setting *) |
|
689 |
fun get_cr_pcr_eqs ctxt = |
|
690 |
let |
|
691 |
fun collect (data : Lifting_Info.quotient) l = |
|
692 |
if is_some (#pcr_info data) |
|
693 |
then ((Thm.symmetric o safe_mk_meta_eq o #pcr_cr_eq o the o #pcr_info) data :: l) |
|
694 |
else l |
|
695 |
val table = Lifting_Info.get_quotients ctxt |
|
696 |
in |
|
697 |
Symtab.fold (fn (_, data) => fn l => collect data l) table [] |
|
698 |
end |
|
699 |
||
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
700 |
fun prepare_lift_def config var qty rhs par_thms lthy = |
47308 | 701 |
let |
51994 | 702 |
val rsp_rel = Lifting_Term.equiv_relation lthy (fastype_of rhs, qty) |
47308 | 703 |
val rty_forced = (domain_type o fastype_of) rsp_rel; |
51994 | 704 |
val forced_rhs = force_rty_type lthy rty_forced rhs; |
56540 | 705 |
val cr_to_pcr_conv = HOLogic.Trueprop_conv (Conv.fun2_conv |
706 |
(Raw_Simplifier.rewrite lthy false (get_cr_pcr_eqs lthy))) |
|
707 |
val (prsp_tm, rsp_prsp_eq) = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs) |
|
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59586
diff
changeset
|
708 |
|> Thm.cterm_of lthy |
56540 | 709 |
|> cr_to_pcr_conv |
59582 | 710 |
|> `Thm.concl_of |
56540 | 711 |
|>> Logic.dest_equals |
712 |
|>> snd |
|
713 |
val to_rsp = rsp_prsp_eq RS Drule.equal_elim_rule2 |
|
714 |
val opt_proven_rsp_thm = try_prove_reflexivity lthy prsp_tm |
|
51994 | 715 |
|
716 |
fun after_qed internal_rsp_thm lthy = |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
717 |
add_lift_def config var qty rhs (internal_rsp_thm RS to_rsp) par_thms lthy |
47308 | 718 |
in |
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
719 |
case opt_proven_rsp_thm of |
60218 | 720 |
SOME thm => (NONE, K (after_qed thm)) |
51994 | 721 |
| NONE => |
722 |
let |
|
56540 | 723 |
val readable_rsp_thm_eq = mk_readable_rsp_thm_eq prsp_tm lthy |
59582 | 724 |
val (readable_rsp_tm, _) = Logic.dest_implies (Thm.prop_of readable_rsp_thm_eq) |
51994 | 725 |
val readable_rsp_tm_tnames = rename_to_tnames lthy readable_rsp_tm |
726 |
||
727 |
fun after_qed' thm_list lthy = |
|
728 |
let |
|
56540 | 729 |
val internal_rsp_thm = Goal.prove lthy [] [] prsp_tm |
54742
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents:
54335
diff
changeset
|
730 |
(fn {context = ctxt, ...} => |
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents:
54335
diff
changeset
|
731 |
rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac ctxt (hd thm_list) 1) |
51994 | 732 |
in |
733 |
after_qed internal_rsp_thm lthy |
|
734 |
end |
|
735 |
in |
|
60218 | 736 |
(SOME readable_rsp_tm_tnames, after_qed') |
51994 | 737 |
end |
47308 | 738 |
end |
739 |
||
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
740 |
fun lift_def config var qty rhs tac par_thms lthy = |
60218 | 741 |
let |
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
742 |
val (goal, after_qed) = prepare_lift_def config var qty rhs par_thms lthy |
60218 | 743 |
in |
744 |
case goal of |
|
745 |
SOME goal => |
|
746 |
let |
|
747 |
val rsp_thm = Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} => tac ctxt) |
|
748 |
|> Thm.close_derivation |
|
749 |
in |
|
750 |
after_qed [[rsp_thm]] lthy |
|
751 |
end |
|
752 |
| NONE => after_qed [[Drule.dummy_thm]] lthy |
|
753 |
end |
|
754 |
||
755 |
(* |
|
756 |
||
757 |
lifting_definition command. It opens a proof of a corresponding respectfulness |
|
758 |
theorem in a user-friendly, readable form. Then add_lift_def is called internally. |
|
759 |
||
760 |
*) |
|
761 |
||
762 |
fun lift_def_cmd (raw_var, rhs_raw, par_xthms) lthy = |
|
763 |
let |
|
764 |
val ((binding, SOME qty, mx), lthy) = yield_singleton Proof_Context.read_vars raw_var lthy |
|
765 |
val var = (binding, mx) |
|
766 |
val rhs = (Syntax.check_term lthy o Syntax.parse_term lthy) rhs_raw |
|
767 |
val par_thms = Attrib.eval_thms lthy par_xthms |
|
60225
eb4e322734bf
note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents:
60224
diff
changeset
|
768 |
val (goal, after_qed) = prepare_lift_def default_config var qty rhs par_thms lthy |
60218 | 769 |
in |
60219 | 770 |
Proof.theorem NONE (snd oo after_qed) [map (rpair []) (the_list goal)] lthy |
60218 | 771 |
end |
772 |
||
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
773 |
fun quot_thm_err ctxt (rty, qty) pretty_msg = |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
774 |
let |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
775 |
val error_msg = cat_lines |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
776 |
["Lifting failed for the following types:", |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
777 |
Pretty.string_of (Pretty.block |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
778 |
[Pretty.str "Raw type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty]), |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
779 |
Pretty.string_of (Pretty.block |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
780 |
[Pretty.str "Abstract type:", Pretty.brk 2, Syntax.pretty_typ ctxt qty]), |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
781 |
"", |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
782 |
(Pretty.string_of (Pretty.block |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
783 |
[Pretty.str "Reason:", Pretty.brk 2, pretty_msg]))] |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
784 |
in |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
785 |
error error_msg |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
786 |
end |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
787 |
|
47504
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
788 |
fun check_rty_err ctxt (rty_schematic, rty_forced) (raw_var, rhs_raw) = |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
789 |
let |
47504
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
790 |
val (_, ctxt') = yield_singleton Proof_Context.read_vars raw_var ctxt |
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
791 |
val rhs = (Syntax.check_term ctxt' o Syntax.parse_term ctxt') rhs_raw |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
792 |
val error_msg = cat_lines |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
793 |
["Lifting failed for the following term:", |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
794 |
Pretty.string_of (Pretty.block |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
795 |
[Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt rhs]), |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
796 |
Pretty.string_of (Pretty.block |
47504
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
797 |
[Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty_schematic]), |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
798 |
"", |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
799 |
(Pretty.string_of (Pretty.block |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
800 |
[Pretty.str "Reason:", |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
801 |
Pretty.brk 2, |
51957 | 802 |
Pretty.str "The type of the term cannot be instantiated to", |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
803 |
Pretty.brk 1, |
47504
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
804 |
Pretty.quote (Syntax.pretty_typ ctxt rty_forced), |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
805 |
Pretty.str "."]))] |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
806 |
in |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
807 |
error error_msg |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
808 |
end |
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
809 |
|
53951
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
810 |
fun lift_def_cmd_with_err_handling (raw_var, rhs_raw, par_xthms) lthy = |
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
811 |
(lift_def_cmd (raw_var, rhs_raw, par_xthms) lthy |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
812 |
handle Lifting_Term.QUOT_THM (rty, qty, msg) => quot_thm_err lthy (rty, qty) msg) |
47504
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
813 |
handle Lifting_Term.CHECK_RTY (rty_schematic, rty_forced) => |
aa1b8a59017f
go back to the explicit compisition of quotient theorems
kuncar
parents:
47503
diff
changeset
|
814 |
check_rty_err lthy (rty_schematic, rty_forced) (raw_var, rhs_raw) |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
815 |
|
47308 | 816 |
(* parser and command *) |
817 |
val liftdef_parser = |
|
51374
84d01fd733cf
lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents:
51314
diff
changeset
|
818 |
(((Parse.binding -- (@{keyword "::"} |-- (Parse.typ >> SOME) -- Parse.opt_mixfix')) >> Parse.triple2) |
53951
03b74ef6d7c6
allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents:
53651
diff
changeset
|
819 |
--| @{keyword "is"} -- Parse.term -- |
58028
e4250d370657
tuned signature -- define some elementary operations earlier;
wenzelm
parents:
58011
diff
changeset
|
820 |
Scan.optional (@{keyword "parametric"} |-- Parse.!!! Parse.xthms1) []) >> Parse.triple1 |
47308 | 821 |
val _ = |
59936
b8ffc3dc9e24
@{command_spec} is superseded by @{command_keyword};
wenzelm
parents:
59630
diff
changeset
|
822 |
Outer_Syntax.local_theory_to_proof @{command_keyword lift_definition} |
47308 | 823 |
"definition for constants over the quotient type" |
47379
075d22b3a32f
detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents:
47373
diff
changeset
|
824 |
(liftdef_parser >> lift_def_cmd_with_err_handling) |
47308 | 825 |
|
826 |
||
53651 | 827 |
end (* structure *) |