paulson@8760: (* Title: Provers/Arith/cancel_numerals.ML paulson@8736: ID: $Id$ paulson@8736: Author: Lawrence C Paulson, Cambridge University Computer Laboratory paulson@8736: Copyright 2000 University of Cambridge paulson@8736: paulson@8760: Cancel common coefficients in balanced expressions: paulson@8736: paulson@8760: i + #m*u + j ~~ i' + #m'*u + j' == #(m-m')*u + i + j ~~ i' + j' paulson@8736: paulson@8736: where ~~ is an appropriate balancing operation (e.g. =, <=, <, -). paulson@8760: paulson@8760: It works by (a) massaging both sides to bring the selected term to the front: paulson@8760: paulson@8760: #m*u + (i + j) ~~ #m'*u + (i' + j') paulson@8760: paulson@8760: (b) then using bal_add1 or bal_add2 to reach paulson@8760: paulson@8760: #(m-m')*u + i + j ~~ i' + j' (if m'<=m) paulson@8760: paulson@8760: or paulson@8760: paulson@8760: i + j ~~ #(m'-m)*u + i' + j' (otherwise) paulson@8736: *) paulson@8736: paulson@8736: signature CANCEL_NUMERALS_DATA = paulson@8736: sig paulson@8736: (*abstract syntax*) paulson@14387: val mk_sum: typ -> term list -> term paulson@8736: val dest_sum: term -> term list paulson@8736: val mk_bal: term * term -> term paulson@8736: val dest_bal: term -> term * term paulson@8760: val mk_coeff: int * term -> term paulson@8760: val dest_coeff: term -> int * term paulson@8760: val find_first_coeff: term -> term list -> int * term list paulson@8760: (*rules*) paulson@8760: val bal_add1: thm paulson@8760: val bal_add2: thm paulson@8736: (*proof tools*) paulson@9546: val prove_conv: tactic list -> Sign.sg -> wenzelm@13484: thm list -> string list -> term * term -> thm option paulson@8799: val trans_tac: thm option -> tactic (*applies the initial lemma*) paulson@8799: val norm_tac: tactic (*proves the initial lemma*) paulson@8799: val numeral_simp_tac: tactic (*proves the final theorem*) paulson@8799: val simplify_meta_eq: thm -> thm (*simplifies the final theorem*) paulson@8736: end; paulson@8736: paulson@8736: paulson@8760: functor CancelNumeralsFun(Data: CANCEL_NUMERALS_DATA): paulson@8760: sig wenzelm@15027: val proc: Sign.sg -> simpset -> term -> thm option paulson@8760: end paulson@8760: = paulson@8736: struct paulson@8736: paulson@8779: (*For t = #n*u then put u in the table*) paulson@8760: fun update_by_coeff (tab, t) = paulson@8779: Termtab.update ((#2 (Data.dest_coeff t), ()), tab); paulson@8760: paulson@8760: (*a left-to-right scan of terms1, seeking a term of the form #n*u, where paulson@8760: #m*u is in terms2 for some m*) paulson@8760: fun find_common (terms1,terms2) = skalberg@15570: let val tab2 = Library.foldl update_by_coeff (Termtab.empty, terms2) paulson@8760: fun seek [] = raise TERM("find_common", []) paulson@8760: | seek (t::terms) = paulson@8760: let val (_,u) = Data.dest_coeff t skalberg@15570: in if isSome (Termtab.lookup (tab2, u)) then u paulson@8760: else seek terms paulson@8760: end paulson@8760: in seek terms1 end; paulson@8736: paulson@8736: (*the simplification procedure*) wenzelm@15027: fun proc sg ss t = wenzelm@15027: let wenzelm@15027: val hyps = prems_of_ss ss; wenzelm@15027: (*first freeze any Vars in the term to prevent flex-flex problems*) wenzelm@13484: val (t', xs) = Term.adhoc_freeze_vars t; paulson@9191: val (t1,t2) = Data.dest_bal t' paulson@8760: val terms1 = Data.dest_sum t1 paulson@8760: and terms2 = Data.dest_sum t2 paulson@8760: val u = find_common (terms1,terms2) paulson@8760: val (n1, terms1') = Data.find_first_coeff u terms1 paulson@8760: and (n2, terms2') = Data.find_first_coeff u terms2 paulson@14387: and T = Term.fastype_of u paulson@14387: fun newshape (i,terms) = Data.mk_sum T (Data.mk_coeff(i,u)::terms) paulson@8779: val reshape = (*Move i*u to the front and put j*u into standard form paulson@8779: i + #m + j + k == #m + i + (j + k) *) paulson@8772: if n1=0 orelse n2=0 then (*trivial, so do nothing*) paulson@8772: raise TERM("cancel_numerals", []) wenzelm@13484: else Data.prove_conv [Data.norm_tac] sg hyps xs paulson@9191: (t', paulson@8772: Data.mk_bal (newshape(n1,terms1'), paulson@8779: newshape(n2,terms2'))) paulson@8736: in skalberg@15570: Option.map Data.simplify_meta_eq paulson@8799: (if n2<=n1 then paulson@8799: Data.prove_conv paulson@8799: [Data.trans_tac reshape, rtac Data.bal_add1 1, wenzelm@13484: Data.numeral_simp_tac] sg hyps xs paulson@9191: (t', Data.mk_bal (newshape(n1-n2,terms1'), paulson@14387: Data.mk_sum T terms2')) paulson@8799: else paulson@8799: Data.prove_conv paulson@8799: [Data.trans_tac reshape, rtac Data.bal_add2 1, wenzelm@13484: Data.numeral_simp_tac] sg hyps xs paulson@14387: (t', Data.mk_bal (Data.mk_sum T terms1', paulson@9191: newshape(n2-n1,terms2')))) paulson@8736: end skalberg@15531: handle TERM _ => NONE skalberg@15531: | TYPE _ => NONE; (*Typically (if thy doesn't include Numeral) paulson@8779: Undeclared type constructor "Numeral.bin"*) paulson@8736: paulson@8736: end;