File ‹~~/src/Provers/Arith/cancel_numeral_factor.ML›
signature CANCEL_NUMERAL_FACTOR_DATA =
sig
val mk_bal: term * term -> term
val dest_bal: term -> term * term
val mk_coeff: int * term -> term
val dest_coeff: term -> int * term
val cancel: thm
val neg_exchanges: bool
val prove_conv: tactic list -> Proof.context -> thm list -> term * term -> thm option
val trans_tac: Proof.context -> thm option -> tactic
val norm_tac: Proof.context -> tactic
val numeral_simp_tac: Proof.context -> tactic
val simplify_meta_eq: Proof.context -> thm -> thm
end;
functor CancelNumeralFactorFun(Data: CANCEL_NUMERAL_FACTOR_DATA):
sig val proc: Simplifier.proc end =
struct
fun proc ctxt ct =
let
val prems = Simplifier.prems_of ctxt;
val t = Thm.term_of ct;
val (t', ctxt') = yield_singleton (Variable.import_terms true) t ctxt
val (t1,t2) = Data.dest_bal t'
val (m1, t1') = Data.dest_coeff t1
and (m2, t2') = Data.dest_coeff t2
val d =
if (m1<0 andalso m2<=0) orelse
(m1<=0 andalso m2<0) then ~ (abs (Integer.gcd m1 m2)) else abs (Integer.gcd m1 m2)
val _ = if d=1 then
raise TERM("cancel_numeral_factor", [])
else ()
fun newshape (i,t) = Data.mk_coeff(d, Data.mk_coeff(i,t))
val n1 = m1 div d and n2 = m2 div d
val rhs = if d<0 andalso Data.neg_exchanges
then Data.mk_bal (Data.mk_coeff(n2,t2'), Data.mk_coeff(n1,t1'))
else Data.mk_bal (Data.mk_coeff(n1,t1'), Data.mk_coeff(n2,t2'))
val reshape =
Data.prove_conv [Data.norm_tac ctxt'] ctxt' prems
(t', Data.mk_bal (newshape(n1,t1'), newshape(n2,t2')))
in
Data.prove_conv
[Data.trans_tac ctxt' reshape, resolve_tac ctxt' [Data.cancel] 1,
Data.numeral_simp_tac ctxt'] ctxt' prems (t', rhs)
|> Option.map
(Data.simplify_meta_eq ctxt' #>
singleton (Variable.export ctxt' ctxt))
end
handle TERM _ => NONE
| TYPE _ => NONE;
end;