datatype clause_info = ClauseInfo of
{no: int,
qglr : ((string * typ) list * term list * term * term),
cdata : clause_context,
tree: Function_Context_Tree.ctx_tree,
lGI: thm,
RCs: rec_call_info list}
(* Theory dependencies. *) val acc_induct_rule = @{thm accp_induct_rule}
val ex1_implies_ex = @{thm Fun_Def.fundef_ex1_existence} val ex1_implies_un = @{thm Fun_Def.fundef_ex1_uniqueness} val ex1_implies_iff = @{thm Fun_Def.fundef_ex1_iff}
val acc_downward = @{thm accp_downward} val accI = @{thm accp.accI}
fun find_calls tree = let fun add_Ri (fixes,assumes) (_ $ arg) _ (_, xs) =
([], (fixes, assumes, arg) :: xs)
| add_Ri _ _ _ _ = raiseMatch in
rev (Function_Context_Tree.traverse_tree add_Ri tree []) end
(** building proof obligations *)
fun mk_compat_proof_obligations domT ranT fvar f glrs = let fun mk_impl ((qs, gs, lhs, rhs),(qs', gs', lhs', rhs')) = let val shift = incr_boundvars (length qs') in
Logic.mk_implies
(HOLogic.mk_Trueprop (HOLogic.eq_const domT $ shift lhs $ lhs'),
HOLogic.mk_Trueprop (HOLogic.eq_const ranT $ shift rhs $ rhs'))
|> fold_rev (curry Logic.mk_implies) (map shift gs @ gs')
|> fold_rev (fn (n,T) => fn b => Logic.all_const T $ Abs(n,T,b)) (qs @ qs')
|> curry abstract_over fvar
|> curry subst_bound f end in map mk_impl (unordered_pairs glrs) end
(* lowlevel term function. FIXME: remove *) fun abstract_over_list vs body = let fun abs lev v tm = if v aconv tm then Bound lev else
(case tm of
Abs (a, T, t) => Abs (a, T, abs (lev + 1) v t)
| t $ u => abs lev v t $ abs lev v u
| t => t) in
fold_index (fn (i, v) => fn t => abs i v t) vs body end
fun mk_clause_info globals G f no cdata qglr tree RCs GIntro_thm RIntro_thms = let val Globals {h, ...} = globals
(* Instantiate the GIntro thm with "f" and import into the clause context. *) val lGI = GIntro_thm
|> Thm.forall_elim (Thm.cterm_of ctxt f)
|> fold Thm.forall_elim cqs
|> fold Thm.elim_implies ags
fun mk_call_info (rcfix, rcassm, rcarg) RI = let val llRI = RI
|> fold Thm.forall_elim cqs
|> fold (Thm.forall_elim o Thm.cterm_of ctxt o Free) rcfix
|> fold Thm.elim_implies ags
|> fold Thm.elim_implies rcassm
val h_assum =
HOLogic.mk_Trueprop (G $ rcarg $ (h $ rcarg))
|> fold_rev (curry Logic.mk_implies o Thm.prop_of) rcassm
|> fold_rev (Logic.all o Free) rcfix
|> Pattern.rewrite_term (Proof_Context.theory_of ctxt) [(f, h)] []
|> abstract_over_list (rev qs) in
RCInfo {RIvs=rcfix, rcarg=rcarg, CCas=rcassm, llRI=llRI, h_assum=h_assum} end
val RC_infos = map2 mk_call_info RCs RIntro_thms in
ClauseInfo {no=no, cdata=cdata, qglr=qglr, lGI=lGI, RCs=RC_infos,
tree=tree} end
fun store_compat_thms 0 thms = []
| store_compat_thms n thms = let val (thms1, thms2) = chop n thms in
(thms1 :: store_compat_thms (n - 1) thms2) end
(* expects i <= j *) fun lookup_compat_thm i j cts =
nth (nth cts (i - 1)) (j - i)
(* Returns "Gsi, Gsj, lhs_i = lhs_j |-- rhs_j_f = rhs_i_f" *) (* if j < i, then turn around *) fun get_compat_thm ctxt cts i j ctxi ctxj = let val ClauseContext {cqs=cqsi,ags=agsi,lhs=lhsi,...} = ctxi val ClauseContext {cqs=cqsj,ags=agsj,lhs=lhsj,...} = ctxj
(* Generates the replacement lemma in fully quantified form. *) fun mk_replacement_lemma ctxt h ih_elim clause = let val ClauseInfo {cdata=ClauseContext {qs, cqs, ags, case_hyp, ...}, RCs, tree, ...} = clause
local open Conv in val ih_conv = arg1_conv o arg_conv o arg_conv end
val ih_elim_case =
Conv.fconv_rule (ih_conv (K (case_hyp RS eq_reflection))) ih_elim
fun elim_implies_eta A AB =
Thm.bicompose (SOME ctxt) {flatten = false, match = true, incremented = false}
(false, A, 0) 1 AB
|> Seq.list_of |> the_single
val function_value =
existence
|> Thm.implies_intr ihyp
|> Thm.implies_intr (Thm.cprop_of case_hyp)
|> Thm.forall_intr (Thm.cterm_of ctxt x)
|> Thm.forall_elim (Thm.cterm_of ctxt lhs)
|> curry (op RS) refl in
(exactly_one, function_value) end
fun prove_stuff ctxt globals G0 f R0 clauses complete compat compat_store G_elim f_def = let val Globals {h, domT, ranT, x = x0, ...} = globals
val G = Thm.cterm_of ctxt G0 val R = Thm.cterm_of ctxt R0
val x = Thm.cterm_of ctxt x0
val A = Thm.ctyp_of ctxt domT val B = Thm.ctyp_of ctxt ranT val C = Thm.ctyp_of_cterm x
val ihyp =
\<^instantiate>\<open>'a = A and 'b = B and'c = C and x and R and G in cprop \<open>\<And>z::'a. R z x \ \!y::'b. G z y\<close> for x :: 'c\
val acc_R_x =
\<^instantiate>\<open>'c = C and R and x in cprop \<open>Wellfounded.accp R x\<close> for x :: 'c\
val ihyp_thm = Thm.assume ihyp |> Thm.forall_elim_vars 0 val ih_intro = ihyp_thm RS (f_def RS ex1_implies_ex) val ih_elim = ihyp_thm RS (f_def RS ex1_implies_un)
|> Thm.instantiate' [] [NONE, SOME (Thm.cterm_of ctxt h)]
val _ = trace_msg (K "Proving Replacement lemmas...") val repLemmas = map (mk_replacement_lemma ctxt h ih_elim) clauses
val _ = trace_msg (K "Proving cases for unique existence...") val (ex1s, values) =
split_list
(map
(mk_uniqueness_case ctxt globals G0 f ihyp ih_intro G_elim compat_store clauses repLemmas)
clauses)
val _ = trace_msg (K "Proving: Graph is a function") val graph_is_function = complete
|> Thm.forall_elim_vars 0
|> fold (curry op COMP) ex1s
|> Thm.implies_intr ihyp
|> Thm.implies_intr acc_R_x
|> Thm.forall_intr x
|> (fn it => Drule.compose (it, 2, acc_induct_rule)) (* "EX! y. (?x,y):G" *)
|> (fn it =>
fold (Thm.forall_intr o Thm.cterm_of ctxt o Var)
(Term.add_vars (Thm.prop_of it) []) it)
val goalstate = Conjunction.intr graph_is_function complete
|> Thm.close_derivation \<^here>
|> Goal.protect 0
|> fold_rev (Thm.implies_intr o Thm.cprop_of) compat
|> Thm.implies_intr (Thm.cprop_of complete) in
(goalstate, values) end
fun requantify orig_intro thm = let val (qs, t) = dest_all_all orig_intro val frees = Variable.add_frees lthy t [] |> remove (op =) (Binding.name_of R, T) val vars = Term.add_vars (Thm.prop_of thm) [] val varmap = AList.lookup (op =) (frees ~~ map fst vars)
#> the_default ("", 0) in
fold_rev (fn Free (n, T) =>
forall_intr_rename (n, Thm.cterm_of lthy (Var (varmap (n, T), T)))) qs thm end in
((Rdef, map2 requantify intrs intrs_gen, Thm.forall_intr_vars elim_gen, induct), lthy) end
fun define_graph (G_binding, G_type) fvar clauses RCss lthy = let val G = Free (Binding.name_of G_binding, G_type) fun mk_GIntro (ClauseContext {qs, gs, lhs, rhs, ...}) RCs = let fun mk_h_assm (rcfix, rcassm, rcarg) =
HOLogic.mk_Trueprop (G $ rcarg $ (fvar $ rcarg))
|> fold_rev (curry Logic.mk_implies o Thm.prop_of) rcassm
|> fold_rev (Logic.all o Free) rcfix in
HOLogic.mk_Trueprop (G $ lhs $ rhs)
|> fold_rev (curry Logic.mk_implies o mk_h_assm) RCs
|> fold_rev (curry Logic.mk_implies) gs
|> fold_rev Logic.all (fvar :: qs) end
val G_intros = map2 mk_GIntro clauses RCss in inductive_def ((G_binding, G_type), NoSyn) G_intros lthy end
fun define_function defname (fname, mixfix) domT ranT G default lthy = let val f_def_binding =
Thm.make_def_binding (Config.get lthy function_internals)
(derived_name_suffix defname "_sumC") val f_def =
\<^instantiate>\<open>'a = domT and 'b = ranT and d = default and G in term \<open>\<lambda>x::'a. THE_default (d x) (\y::'b. G x y)\<close>\<close>
|> Syntax.check_term lthy in
lthy |> Local_Theory.define
((Binding.map_name (suffix "C") fname, mixfix), ((f_def_binding, []), f_def)) end
fun define_recursion_relation (R_binding, R_type) qglrs clauses RCss lthy = let val R = Free (Binding.name_of R_binding, R_type) fun mk_RIntro (ClauseContext {qs, gs, lhs, ...}, (oqs, _, _, _)) (rcfix, rcassm, rcarg) =
HOLogic.mk_Trueprop (R $ rcarg $ lhs)
|> fold_rev (curry Logic.mk_implies o Thm.prop_of) rcassm
|> fold_rev (curry Logic.mk_implies) gs
|> fold_rev (Logic.all o Free) rcfix
|> fold_rev mk_forall_rename (map fst oqs ~~ qs) (* "!!qs xs. CS ==> G => (r, lhs) : R" *)
val R_intross = map2 (map o mk_RIntro) (clauses ~~ qglrs) RCss
val ((R, RIntro_thms, R_elim, _), lthy) =
inductive_def ((R_binding, R_type), NoSyn) (flat R_intross) lthy in
((R, Library.unflat R_intross RIntro_thms, R_elim), lthy) end
fun fix_globals domT ranT fvar ctxt = let val ([h, y, x, z, a, D, P, Pbool], ctxt') = Variable.variant_fixes
["h_fd", "y_fd", "x_fd", "z_fd", "a_fd", "D_fd", "P_fd", "Pb_fd"] ctxt in
(Globals {h = Free (h, domT --> ranT),
y = Free (y, ranT),
x = Free (x, domT),
z = Free (z, domT),
a = Free (a, domT),
D = Free (D, domT --> boolT),
P = Free (P, domT --> boolT),
Pbool = Free (Pbool, boolT),
fvar = fvar,
domT = domT,
ranT = ranT},
ctxt') end
fun inst_RC ctxt fvar f (rcfix, rcassm, rcarg) = let fun inst_term t = subst_bound(f, abstract_over (fvar, t)) in
(rcfix, map (Thm.assume o Thm.cterm_of ctxt o inst_term o Thm.prop_of) rcassm, inst_term rcarg) end
(********************************************************** * PROVING THE RULES
**********************************************************)
fun mk_psimps ctxt globals R clauses valthms f_iff graph_is_function = let val Globals {domT, z, ...} = globals
fun mk_psimp
(ClauseInfo {qglr = (oqs, _, _, _), cdata = ClauseContext {cqs, lhs, ags, ...}, ...}) valthm = let val lhs_acc =
Thm.cterm_of ctxt (HOLogic.mk_Trueprop (mk_acc domT R $ lhs)) (* "acc R lhs" *) val z_smaller =
Thm.cterm_of ctxt (HOLogic.mk_Trueprop (R $ z $ lhs)) (* "R z lhs" *) in
((Thm.assume z_smaller) RS ((Thm.assume lhs_acc) RS acc_downward))
|> (fn it => it COMP graph_is_function)
|> Thm.implies_intr z_smaller
|> Thm.forall_intr (Thm.cterm_of ctxt z)
|> (fn it => it COMP valthm)
|> Thm.implies_intr lhs_acc
|> asm_simplify (put_simpset HOL_basic_ss ctxt |> Simplifier.add_simp f_iff)
|> fold_rev (Thm.implies_intr o Thm.cprop_of) ags
|> fold_rev forall_intr_rename (map fst oqs ~~ cqs) end in
map2 mk_psimp clauses valthms end
(** Induction rule **)
val acc_subset_induct = @{thm predicate1I} RS @{thm accp_subset_induct}
fun mk_partial_induct_rule ctxt globals R complete_thm clauses = let val Globals {domT, x, z, a, P, D, ...} = globals val acc_R = mk_acc domT R
val x_D = Thm.assume (Thm.cterm_of ctxt (HOLogic.mk_Trueprop (D $ x))) val a_D = Thm.cterm_of ctxt (HOLogic.mk_Trueprop (D $ a))
val D_subset =
Thm.cterm_of ctxt (Logic.all x
(Logic.mk_implies (HOLogic.mk_Trueprop (D $ x), HOLogic.mk_Trueprop (acc_R $ x))))
val D_dcl = (* "!!x z. [| x: D; (z,x):R |] ==> z:D" *)
Logic.all x (Logic.all z (Logic.mk_implies (HOLogic.mk_Trueprop (D $ x),
Logic.mk_implies (HOLogic.mk_Trueprop (R $ z $ x),
HOLogic.mk_Trueprop (D $ z)))))
|> Thm.cterm_of ctxt
fun prove_case clause = let val ClauseInfo {cdata = ClauseContext {ctxt = ctxt1, qs, cqs, ags, gs, lhs, case_hyp, ...},
RCs, qglr = (oqs, _, _, _), ...} = clause
val case_hyp_conv = K (case_hyp RS eq_reflection)
local open Conv in val lhs_D = fconv_rule (arg_conv (arg_conv (case_hyp_conv))) x_D val sih =
fconv_rule
(binder_conv (K (arg1_conv (arg_conv (arg_conv case_hyp_conv)))) ctxt1) aihyp end
fun mk_Prec (RCInfo {llRI, RIvs, CCas, rcarg, ...}) = sih
|> Thm.forall_elim (Thm.cterm_of ctxt rcarg)
|> Thm.elim_implies llRI
|> fold_rev (Thm.implies_intr o Thm.cprop_of) CCas
|> fold_rev (Thm.forall_intr o Thm.cterm_of ctxt o Free) RIvs
val P_recs = map mk_Prec RCs (* [P rec1, P rec2, ... ] *)
fun step (fixes, assumes) (_ $ arg) u (sub,(hyps,thms)) = let val used = (u @ sub)
|> map (fn (ctx, thm) => Function_Context_Tree.export_thm ctxt ctx thm)
valsub' = sub @ [(([],[]), acc)] in
(sub', (hyp :: hyps, ethm :: thms)) end
| step _ _ _ _ = raiseMatch in
Function_Context_Tree.traverse_tree step tree end
fun mk_nest_term_rule ctxt globals R0 R_cases clauses = let val Globals { domT, x = x0, z = z0, ... } = globals
val ([Rn], ctxt') = Variable.variant_fixes ["R"] ctxt
val R = Thm.cterm_of ctxt' R0 val R' = Thm.cterm_of ctxt' (Free (Rn, Thm.typ_of_cterm R)) val Rrel = Thm.cterm_of ctxt' (Free (Rn, HOLogic.mk_setT (HOLogic.mk_prodT (domT, domT))))
val x = Thm.cterm_of ctxt' x0 val z = Thm.cterm_of ctxt' z0
val A = Thm.ctyp_of ctxt' domT val B = Thm.ctyp_of_cterm x
val acc_R_z =
\<^instantiate>\<open>'a = A and R and z in cterm \<open>Wellfounded.accp R z\<close> for z :: 'a\
val inrel_R =
\<^instantiate>\<open>'a = A and Rrel in cterm \<open>in_rel Rrel\<close> for Rrel :: \<open>('a \ 'a) set\<close>\<close>
val wfR' = \<^instantiate>\'a = A and R' in cprop \wfP R'\<close>\<close>
val ihyp =
\<^instantiate>\<open>'a = A and 'b = B and x and R' and R in cprop \<open>\<And>z::'a. R' z x \<Longrightarrow> Wellfounded.accp R z\<close> for x :: 'b\
val ihyp_a = Thm.assume ihyp |> Thm.forall_elim_vars 0
val R_z_x =
\<^instantiate>\<open>'a = A and 'b = B and R and x and z in cprop \<open>R z x\<close> for x :: 'a and z :: 'b\<close>
val (hyps, cases) =
fold (mk_nest_term_case ctxt' globals (Thm.term_of R') ihyp_a) clauses ([], []) in
R_cases
|> Thm.forall_elim z
|> Thm.forall_elim x
|> Thm.forall_elim acc_R_z
|> curry op COMP (Thm.assume R_z_x)
|> fold_rev (curry op COMP) cases
|> Thm.implies_intr R_z_x
|> Thm.forall_intr z
|> (fn it => it COMP accI)
|> Thm.implies_intr ihyp
|> Thm.forall_intr x
|> (fn it => Drule.compose (it, 2, wf_induct_rule))
|> curry op RS (Thm.assume wfR')
|> Thm.forall_intr_vars
|> (fn it => it COMP allI)
|> fold Thm.implies_intr hyps
|> Thm.implies_intr wfR'
|> Thm.forall_intr R'
|> Thm.forall_elim inrel_R
|> curry op RS wf_in_rel
|> full_simplify (put_simpset HOL_basic_ss ctxt' |> Simplifier.add_simp in_rel_def)
|> Thm.forall_intr_name ("R", Rrel) end
fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy = let val FunctionConfig {domintros, default=default_opt, ...} = config
val default_str = the_default "%x. HOL.undefined"(* FIXME proper term!? *) default_opt val fvar = (Binding.name_of fname, fT) val domT = domain_type fT val ranT = range_type fT
val default = Syntax.parse_term lthy default_str
|> Type.constraint fT |> Syntax.check_term lthy
val (globals, ctxt') = fix_globals domT ranT (Free fvar) lthy
val Globals { x, h, ... } = globals
val clauses = map (mk_clause_context x ctxt') abstract_qglrs
val n = length abstract_qglrs
fun build_tree (ClauseContext { ctxt, rhs, ...}) =
Function_Context_Tree.mk_tree (Free fvar) h ctxt rhs
val trees = map build_tree clauses val RCss = map find_calls trees
val compat =
mk_compat_proof_obligations domT ranT (Free fvar) f abstract_qglrs
|> map (Thm.cterm_of lthy #> Thm.assume)
val compat_store = store_compat_thms n compat
val (goalstate, values) = PROFILE "prove_stuff"
(prove_stuff lthy globals G f R xclauses complete compat
compat_store G_elim) f_defthm
fun mk_partial_rules newctxt provedgoal = let val (graph_is_function, complete_thm) =
provedgoal
|> Conjunction.elim
|> apfst (Thm.forall_elim_vars 0)
val f_iff = graph_is_function RS (f_defthm RS ex1_implies_iff)
val psimps = PROFILE "Proving simplification rules"
(mk_psimps newctxt globals R xclauses values f_iff) graph_is_function
val simple_pinduct = PROFILE "Proving partial induction rule"
(mk_partial_induct_rule newctxt globals R complete_thm) xclauses
val total_intro = PROFILE "Proving nested termination rule"
(mk_nest_term_rule newctxt globals R R_elim) xclauses
val dom_intros = if domintros then SOME (PROFILE "Proving domain introduction rules"
(map (mk_domain_intro lthy globals R R_elim)) xclauses) else NONE in
FunctionResult {fs=[f], G=G, R=R, dom=dom,
cases=[complete_thm], psimps=psimps, pelims=[],
simple_pinducts=[simple_pinduct],
termination=total_intro, domintros=dom_intros} end in
((f, goalstate, mk_partial_rules), lthy) end
end
¤ Dauer der Verarbeitung: 0.21 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.