src/Provers/Arith/cancel_numeral_factor.ML
author wenzelm
Sun Mar 07 12:19:47 2010 +0100 (2010-03-07)
changeset 35625 9c818cab0dd0
parent 24630 351a308ab58d
child 35762 af3ff2ba4c54
permissions -rw-r--r--
modernized structure Object_Logic;
     1 (*  Title:      Provers/Arith/cancel_numeral_factor.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   2000  University of Cambridge
     5 
     6 Cancel common coefficients in balanced expressions:
     7 
     8      u*#m ~~ u'*#m'  ==  #n*u ~~ #n'*u'
     9 
    10 where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /)
    11 and d = gcd(m,m') and n=m/d and n'=m'/d.
    12 
    13 It works by (a) massaging both sides to bring gcd(m,m') to the front:
    14 
    15      u*#m ~~ u'*#m'  ==  #d*(#n*u) ~~ #d*(#n'*u')
    16 
    17 (b) then using the rule "cancel" to reach #n*u ~~ #n'*u'.
    18 *)
    19 
    20 signature CANCEL_NUMERAL_FACTOR_DATA =
    21 sig
    22   (*abstract syntax*)
    23   val mk_bal: term * term -> term
    24   val dest_bal: term -> term * term
    25   val mk_coeff: int * term -> term
    26   val dest_coeff: term -> int * term
    27   (*rules*)
    28   val cancel: thm
    29   val neg_exchanges: bool  (*true if a negative coeff swaps the two operands,
    30                              as with < and <= but not = and div*)
    31   (*proof tools*)
    32   val prove_conv: tactic list -> Proof.context -> thm list -> term * term -> thm option
    33   val trans_tac: simpset -> thm option -> tactic (*applies the initial lemma*)
    34   val norm_tac: simpset -> tactic                (*proves the initial lemma*)
    35   val numeral_simp_tac: simpset -> tactic        (*proves the final theorem*)
    36   val simplify_meta_eq: simpset -> thm -> thm    (*simplifies the final theorem*)
    37 end;
    38 
    39 
    40 functor CancelNumeralFactorFun(Data: CANCEL_NUMERAL_FACTOR_DATA):
    41   sig
    42   val proc: simpset -> term -> thm option
    43   end
    44 =
    45 struct
    46 
    47 (*the simplification procedure*)
    48 fun proc ss t =
    49   let
    50     val ctxt = Simplifier.the_context ss;
    51     val prems = prems_of_ss ss;
    52     val ([t'], ctxt') = Variable.import_terms true [t] ctxt
    53     val export = singleton (Variable.export ctxt' ctxt)
    54 
    55     val (t1,t2) = Data.dest_bal t'
    56     val (m1, t1') = Data.dest_coeff t1
    57     and (m2, t2') = Data.dest_coeff t2
    58     val d = (*if both are negative, also divide through by ~1*)
    59       if (m1<0 andalso m2<=0) orelse
    60          (m1<=0 andalso m2<0) then ~ (abs (Integer.gcd m1 m2)) else abs (Integer.gcd m1 m2)
    61     val _ = if d=1 then   (*trivial, so do nothing*)
    62               raise TERM("cancel_numeral_factor", [])
    63             else ()
    64     fun newshape (i,t) = Data.mk_coeff(d, Data.mk_coeff(i,t))
    65     val n1 = m1 div d and n2 = m2 div d
    66     val rhs = if d<0 andalso Data.neg_exchanges
    67               then Data.mk_bal (Data.mk_coeff(n2,t2'), Data.mk_coeff(n1,t1'))
    68               else Data.mk_bal (Data.mk_coeff(n1,t1'), Data.mk_coeff(n2,t2'))
    69     val reshape =  (*Move d to the front and put the rest into standard form
    70                        i * #m * j == #d * (#n * (j * k)) *)
    71       Data.prove_conv [Data.norm_tac ss] ctxt prems
    72         (t', Data.mk_bal (newshape(n1,t1'), newshape(n2,t2')))
    73   in
    74     Option.map (export o Data.simplify_meta_eq ss)
    75       (Data.prove_conv
    76          [Data.trans_tac ss reshape, rtac Data.cancel 1,
    77           Data.numeral_simp_tac ss] ctxt prems (t', rhs))
    78   end
    79   (* FIXME avoid handling of generic exceptions *)
    80   handle TERM _ => NONE
    81        | TYPE _ => NONE;   (*Typically (if thy doesn't include Numeral)
    82                              Undeclared type constructor "Numeral.bin"*)
    83 
    84 end;