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: wenzelm@17223: #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@15965: val mk_coeff: IntInf.int * term -> term paulson@15965: val dest_coeff: term -> IntInf.int * term paulson@15965: val find_first_coeff: term -> term list -> IntInf.int * term list paulson@8760: (*rules*) paulson@8760: val bal_add1: thm paulson@8760: val bal_add2: thm paulson@8736: (*proof tools*) wenzelm@20114: val prove_conv: tactic list -> Proof.context -> thm list -> term * term -> thm option wenzelm@16973: val trans_tac: simpset -> thm option -> tactic (*applies the initial lemma*) wenzelm@16973: val norm_tac: simpset -> tactic (*proves the initial lemma*) wenzelm@16973: val numeral_simp_tac: simpset -> tactic (*proves the final theorem*) wenzelm@16973: val simplify_meta_eq: simpset -> 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@20044: val proc: simpset -> term -> thm option wenzelm@17223: end paulson@8760: = paulson@8736: struct paulson@8736: paulson@8779: (*For t = #n*u then put u in the table*) wenzelm@17223: fun update_by_coeff t = wenzelm@17412: Termtab.update (#2 (Data.dest_coeff t), ()); 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) = wenzelm@17223: let val tab2 = fold update_by_coeff terms2 Termtab.empty wenzelm@17223: fun seek [] = raise TERM("find_common", []) wenzelm@17223: | seek (t::terms) = wenzelm@17223: let val (_,u) = Data.dest_coeff t wenzelm@17223: in if Termtab.defined tab2 u then u else seek terms end paulson@8760: in seek terms1 end; paulson@8736: paulson@8736: (*the simplification procedure*) wenzelm@20044: fun proc ss t = wenzelm@15027: let wenzelm@20114: val ctxt = Simplifier.the_context ss wenzelm@20114: val prems = prems_of_ss ss wenzelm@20114: val ([t'], ctxt') = Variable.import_terms true [t] ctxt wenzelm@20114: val export = singleton (Variable.export ctxt' ctxt) wenzelm@20114: wenzelm@20114: val (t1,t2) = Data.dest_bal t' wenzelm@20114: val terms1 = Data.dest_sum t1 wenzelm@20114: and terms2 = Data.dest_sum t2 wenzelm@20114: wenzelm@20114: val u = find_common (terms1, terms2) wenzelm@20114: val (n1, terms1') = Data.find_first_coeff u terms1 wenzelm@20114: and (n2, terms2') = Data.find_first_coeff u terms2 wenzelm@20114: and T = Term.fastype_of u wenzelm@20114: wenzelm@20114: fun newshape (i,terms) = Data.mk_sum T (Data.mk_coeff(i,u)::terms) wenzelm@20114: val reshape = (*Move i*u to the front and put j*u into standard form wenzelm@17223: i + #m + j + k == #m + i + (j + k) *) wenzelm@20114: if n1=0 orelse n2=0 then (*trivial, so do nothing*) wenzelm@20114: raise TERM("cancel_numerals", []) wenzelm@20114: else Data.prove_conv [Data.norm_tac ss] ctxt prems wenzelm@20114: (t', Data.mk_bal (newshape(n1,terms1'), newshape(n2,terms2'))) paulson@8736: in wenzelm@20114: Option.map (export o Data.simplify_meta_eq ss) wenzelm@20114: (if n2 <= n1 then wenzelm@20114: Data.prove_conv wenzelm@20114: [Data.trans_tac ss reshape, rtac Data.bal_add1 1, wenzelm@20114: Data.numeral_simp_tac ss] ctxt prems wenzelm@20114: (t', Data.mk_bal (newshape(n1-n2,terms1'), Data.mk_sum T terms2')) wenzelm@20114: else wenzelm@20114: Data.prove_conv wenzelm@20114: [Data.trans_tac ss reshape, rtac Data.bal_add2 1, wenzelm@20114: Data.numeral_simp_tac ss] ctxt prems wenzelm@20114: (t', Data.mk_bal (Data.mk_sum T terms1', newshape(n2-n1,terms2')))) paulson@8736: end wenzelm@20114: (* FIXME avoid handling of generic exceptions *) skalberg@15531: handle TERM _ => NONE skalberg@15531: | TYPE _ => NONE; (*Typically (if thy doesn't include Numeral) wenzelm@17223: Undeclared type constructor "Numeral.bin"*) paulson@8736: paulson@8736: end;