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


9 
val add_lift_def:


10 
(binding * mixfix) > typ > term > thm > local_theory > local_theory


11 


12 
val lift_def_cmd:


13 
(binding * string option * mixfix) * string > local_theory > Proof.state


14 


15 
val can_generate_code_cert: thm > bool


16 
end;


17 


18 
structure Lifting_Def: LIFTING_DEF =


19 
struct


20 


21 
(** Interface and Syntax Setup **)


22 


23 
(* Generation of the code certificate from the rsp theorem *)


24 


25 
infix 0 MRSL


26 


27 
fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm


28 


29 
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)


30 
 get_body_types (U, V) = (U, V)


31 


32 
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)


33 
 get_binder_types _ = []


34 


35 
fun force_rty_type ctxt rty rhs =


36 
let


37 
val thy = Proof_Context.theory_of ctxt


38 
val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs


39 
val rty_schematic = fastype_of rhs_schematic


40 
val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty


41 
in


42 
Envir.subst_term_types match rhs_schematic


43 
end


44 


45 
fun unabs_def ctxt def =


46 
let


47 
val (_, rhs) = Thm.dest_equals (cprop_of def)


48 
fun dest_abs (Abs (var_name, T, _)) = (var_name, T)


49 
 dest_abs tm = raise TERM("get_abs_var",[tm])


50 
val (var_name, T) = dest_abs (term_of rhs)


51 
val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt


52 
val thy = Proof_Context.theory_of ctxt'


53 
val refl_thm = Thm.reflexive (cterm_of thy (Free (hd new_var_names, T)))


54 
in


55 
Thm.combination def refl_thm >


56 
singleton (Proof_Context.export ctxt' ctxt)


57 
end


58 


59 
fun unabs_all_def ctxt def =


60 
let


61 
val (_, rhs) = Thm.dest_equals (cprop_of def)


62 
val xs = strip_abs_vars (term_of rhs)


63 
in


64 
fold (K (unabs_def ctxt)) xs def


65 
end


66 


67 
val map_fun_unfolded =


68 
@{thm map_fun_def[abs_def]} >


69 
unabs_def @{context} >


70 
unabs_def @{context} >


71 
Local_Defs.unfold @{context} [@{thm comp_def}]


72 


73 
fun unfold_fun_maps ctm =


74 
let


75 
fun unfold_conv ctm =


76 
case (Thm.term_of ctm) of


77 
Const (@{const_name "map_fun"}, _) $ _ $ _ =>


78 
(Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm


79 
 _ => Conv.all_conv ctm


80 
val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)


81 
in


82 
(Conv.arg_conv (Conv.fun_conv unfold_conv then_conv try_beta_conv)) ctm


83 
end


84 


85 
fun prove_rel ctxt rsp_thm (rty, qty) =


86 
let


87 
val ty_args = get_binder_types (rty, qty)


88 
fun disch_arg args_ty thm =


89 
let


90 
val quot_thm = Lifting_Term.prove_quot_theorem ctxt args_ty


91 
in


92 
[quot_thm, thm] MRSL @{thm apply_rsp''}


93 
end


94 
in


95 
fold disch_arg ty_args rsp_thm


96 
end


97 


98 
exception CODE_CERT_GEN of string


99 


100 
fun simplify_code_eq ctxt def_thm =


101 
Local_Defs.unfold ctxt [@{thm o_def}, @{thm map_fun_def}, @{thm id_def}] def_thm


102 


103 
fun can_generate_code_cert quot_thm =


104 
case Lifting_Term.quot_thm_rel quot_thm of


105 
Const (@{const_name HOL.eq}, _) => true


106 
 Const (@{const_name invariant}, _) $ _ => true


107 
 _ => false


108 


109 
fun generate_code_cert ctxt def_thm rsp_thm (rty, qty) =


110 
let


111 
val thy = Proof_Context.theory_of ctxt


112 
val quot_thm = Lifting_Term.prove_quot_theorem ctxt (get_body_types (rty, qty))


113 
val fun_rel = prove_rel ctxt rsp_thm (rty, qty)


114 
val abs_rep_thm = [quot_thm, fun_rel] MRSL @{thm Quotient_rep_abs}


115 
val abs_rep_eq =


116 
case (HOLogic.dest_Trueprop o prop_of) fun_rel of


117 
Const (@{const_name HOL.eq}, _) $ _ $ _ => abs_rep_thm


118 
 Const (@{const_name invariant}, _) $ _ $ _ $ _ => abs_rep_thm RS @{thm invariant_to_eq}


119 
 _ => raise CODE_CERT_GEN "relation is neither equality nor invariant"


120 
val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm


121 
val unabs_def = unabs_all_def ctxt unfolded_def


122 
val rep = (cterm_of thy o Lifting_Term.quot_thm_rep) quot_thm


123 
val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq}


124 
val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong}


125 
val code_cert = [repped_eq, abs_rep_eq] MRSL @{thm trans}


126 
in


127 
simplify_code_eq ctxt code_cert


128 
end


129 


130 
fun define_code_cert code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy =


131 
let


132 
val quot_thm = Lifting_Term.prove_quot_theorem lthy (get_body_types (rty, qty))


133 
in


134 
if can_generate_code_cert quot_thm then


135 
let


136 
val code_cert = generate_code_cert lthy def_thm rsp_thm (rty, qty)


137 
val add_abs_eqn_attribute =


138 
Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abs_eqn thm) I)


139 
val add_abs_eqn_attrib = Attrib.internal (K add_abs_eqn_attribute);


140 
in


141 
lthy


142 
> (snd oo Local_Theory.note) ((code_eqn_thm_name, [add_abs_eqn_attrib]), [code_cert])


143 
end


144 
else


145 
lthy


146 
end


147 


148 
fun define_code_eq code_eqn_thm_name def_thm lthy =


149 
let


150 
val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm


151 
val code_eq = unabs_all_def lthy unfolded_def


152 
val simp_code_eq = simplify_code_eq lthy code_eq


153 
in


154 
lthy


155 
> (snd oo Local_Theory.note) ((code_eqn_thm_name, [Code.add_default_eqn_attrib]), [simp_code_eq])


156 
end


157 


158 
fun define_code code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy =


159 
if body_type rty = body_type qty then


160 
define_code_eq code_eqn_thm_name def_thm lthy


161 
else


162 
define_code_cert code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy


163 


164 


165 
fun add_lift_def var qty rhs rsp_thm lthy =


166 
let


167 
val rty = fastype_of rhs


168 
val absrep_trm = Lifting_Term.absrep_fun lthy (rty, qty)


169 
val rty_forced = (domain_type o fastype_of) absrep_trm


170 
val forced_rhs = force_rty_type lthy rty_forced rhs


171 
val lhs = Free (Binding.print (#1 var), qty)


172 
val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs)


173 
val (_, prop') = Local_Defs.cert_def lthy prop


174 
val (_, newrhs) = Local_Defs.abs_def prop'


175 


176 
val ((_, (_ , def_thm)), lthy') =


177 
Local_Theory.define (var, ((Thm.def_binding (#1 var), []), newrhs)) lthy


178 


179 
fun qualify defname suffix = Binding.name suffix


180 
> Binding.qualify true defname


181 


182 
val lhs_name = Binding.name_of (#1 var)


183 
val rsp_thm_name = qualify lhs_name "rsp"


184 
val code_eqn_thm_name = qualify lhs_name "rep_eq"


185 
in


186 
lthy'


187 
> (snd oo Local_Theory.note) ((rsp_thm_name, []), [rsp_thm])


188 
> define_code code_eqn_thm_name def_thm rsp_thm (rty_forced, qty)


189 
end


190 


191 
fun mk_readable_rsp_thm_eq tm lthy =


192 
let


193 
val ctm = cterm_of (Proof_Context.theory_of lthy) tm


194 


195 
fun norm_fun_eq ctm =


196 
let


197 
fun abs_conv2 cv = Conv.abs_conv (K (Conv.abs_conv (K cv) lthy)) lthy


198 
fun erase_quants ctm' =


199 
case (Thm.term_of ctm') of


200 
Const ("HOL.eq", _) $ _ $ _ => Conv.all_conv ctm'


201 
 _ => (Conv.binder_conv (K erase_quants) lthy then_conv


202 
Conv.rewr_conv @{thm fun_eq_iff[symmetric, THEN eq_reflection]}) ctm'


203 
in


204 
(abs_conv2 erase_quants then_conv Thm.eta_conversion) ctm


205 
end


206 


207 
fun simp_arrows_conv ctm =


208 
let


209 
val unfold_conv = Conv.rewrs_conv


210 
[@{thm fun_rel_eq_invariant[THEN eq_reflection]}, @{thm fun_rel_eq_rel[THEN eq_reflection]},


211 
@{thm fun_rel_def[THEN eq_reflection]}]


212 
val left_conv = simp_arrows_conv then_conv Conv.try_conv norm_fun_eq


213 
fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2


214 
in


215 
case (Thm.term_of ctm) of


216 
Const (@{const_name "fun_rel"}, _) $ _ $ _ =>


217 
(binop_conv2 left_conv simp_arrows_conv then_conv unfold_conv) ctm


218 
 _ => Conv.all_conv ctm


219 
end


220 


221 
val unfold_ret_val_invs = Conv.bottom_conv


222 
(K (Conv.try_conv (Conv.rewr_conv @{thm invariant_same_args}))) lthy


223 
val simp_conv = Conv.arg_conv (Conv.fun2_conv simp_arrows_conv)


224 
val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}


225 
val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy


226 
val beta_conv = Thm.beta_conversion true


227 
val eq_thm =


228 
(simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs) ctm


229 
in


230 
Object_Logic.rulify(eq_thm RS Drule.equal_elim_rule2)


231 
end


232 


233 


234 


235 
fun lift_def_cmd (raw_var, rhs_raw) lthy =


236 
let


237 
val ((binding, SOME qty, mx), ctxt) = yield_singleton Proof_Context.read_vars raw_var lthy


238 
val rhs = (Syntax.check_term ctxt o Syntax.parse_term ctxt) rhs_raw


239 


240 
fun try_to_prove_refl thm =


241 
let


242 
val lhs_eq =


243 
thm


244 
> prop_of


245 
> Logic.dest_implies


246 
> fst


247 
> strip_all_body


248 
> try HOLogic.dest_Trueprop


249 
in


250 
case lhs_eq of


251 
SOME (Const ("HOL.eq", _) $ _ $ _) => SOME (@{thm refl} RS thm)


252 
 _ => NONE


253 
end


254 


255 
val rsp_rel = Lifting_Term.equiv_relation lthy (fastype_of rhs, qty)


256 
val rty_forced = (domain_type o fastype_of) rsp_rel;


257 
val forced_rhs = force_rty_type lthy rty_forced rhs;


258 
val internal_rsp_tm = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs)


259 
val readable_rsp_thm_eq = mk_readable_rsp_thm_eq internal_rsp_tm lthy


260 
val maybe_proven_rsp_thm = try_to_prove_refl readable_rsp_thm_eq


261 
val (readable_rsp_tm, _) = Logic.dest_implies (prop_of readable_rsp_thm_eq)


262 


263 
fun after_qed thm_list lthy =


264 
let


265 
val internal_rsp_thm =


266 
case thm_list of


267 
[] => the maybe_proven_rsp_thm


268 
 [[thm]] => Goal.prove ctxt [] [] internal_rsp_tm


269 
(fn _ => rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac [thm] 1)


270 
in


271 
add_lift_def (binding, mx) qty rhs internal_rsp_thm lthy


272 
end


273 


274 
in


275 
case maybe_proven_rsp_thm of


276 
SOME _ => Proof.theorem NONE after_qed [] lthy


277 
 NONE => Proof.theorem NONE after_qed [[(readable_rsp_tm,[])]] lthy


278 
end


279 


280 
(* parser and command *)


281 
val liftdef_parser =


282 
((Parse.binding  (@{keyword "::"}  (Parse.typ >> SOME)  Parse.opt_mixfix')) >> Parse.triple2)


283 
 @{keyword "is"}  Parse.term


284 


285 
val _ =


286 
Outer_Syntax.local_theory_to_proof @{command_spec "lift_definition"}


287 
"definition for constants over the quotient type"


288 
(liftdef_parser >> lift_def_cmd)


289 


290 


291 
end; (* structure *)
