File ‹~~/src/Provers/Arith/extract_common_term.ML›
signature EXTRACT_COMMON_TERM_DATA =
sig
val mk_sum: typ -> term list -> term
val dest_sum: term -> term list
val mk_bal: term * term -> term
val dest_bal: term -> term * term
val find_first: term -> term list -> term list
val mk_eq: term * term -> term
val norm_tac: Proof.context -> tactic
val simplify_meta_eq: Proof.context -> thm -> thm -> thm
val simp_conv: Proof.context -> term -> thm option
end;
functor ExtractCommonTermFun(Data: EXTRACT_COMMON_TERM_DATA):
sig
val proc: Proof.context -> term -> thm option
end
=
struct
fun find_common (terms1,terms2) =
let val tab2 = fold (Termtab.update o rpair ()) terms2 Termtab.empty
fun seek [] = raise TERM("find_common", [])
| seek (u::terms) =
if Termtab.defined tab2 u then u
else seek terms
in seek terms1 end;
fun proc ctxt t =
let
val prems = Simplifier.prems_of ctxt;
val (t', ctxt') = yield_singleton (Variable.import_terms true) t ctxt
val (t1,t2) = Data.dest_bal t'
val terms1 = Data.dest_sum t1
and terms2 = Data.dest_sum t2
val u = find_common (terms1,terms2)
val simp_th =
case Data.simp_conv ctxt' u of NONE => raise TERM("no simp", [])
| SOME th => th
val terms1' = Data.find_first u terms1
and terms2' = Data.find_first u terms2
and T = Term.fastype_of u
val t'' = Data.mk_bal (Data.mk_sum T (u::terms1'), Data.mk_sum T (u::terms2'))
val reshape =
Goal.prove ctxt' [] [] (Data.mk_eq (t', t'')) (K (Data.norm_tac ctxt))
in
SOME (singleton (Variable.export ctxt' ctxt) (Data.simplify_meta_eq ctxt' simp_th reshape))
end
handle TERM _ => NONE
| TYPE _ => NONE;
end;