src/Provers/Arith/cancel_numerals.ML
author wenzelm
Sat Jul 08 12:54:30 2006 +0200 (2006-07-08)
changeset 20044 92cc2f4c7335
parent 17412 e26cb20ef0cc
child 20114 a1bb4bc68ff3
permissions -rw-r--r--
simprocs: no theory argument -- use simpset context instead;
     1 (*  Title:      Provers/Arith/cancel_numerals.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      i + #m*u + j ~~ i' + #m'*u + j'  ==  #(m-m')*u + i + j ~~ i' + j'
     9 
    10 where ~~ is an appropriate balancing operation (e.g. =, <=, <, -).
    11 
    12 It works by (a) massaging both sides to bring the selected term to the front:
    13 
    14      #m*u + (i + j) ~~ #m'*u + (i' + j')
    15 
    16 (b) then using bal_add1 or bal_add2 to reach
    17 
    18      #(m-m')*u + i + j ~~ i' + j'       (if m'<=m)
    19 
    20 or
    21 
    22      i + j ~~ #(m'-m)*u + i' + j'       (otherwise)
    23 *)
    24 
    25 signature CANCEL_NUMERALS_DATA =
    26 sig
    27   (*abstract syntax*)
    28   val mk_sum: typ -> term list -> term
    29   val dest_sum: term -> term list
    30   val mk_bal: term * term -> term
    31   val dest_bal: term -> term * term
    32   val mk_coeff: IntInf.int * term -> term
    33   val dest_coeff: term -> IntInf.int * term
    34   val find_first_coeff: term -> term list -> IntInf.int * term list
    35   (*rules*)
    36   val bal_add1: thm
    37   val bal_add2: thm
    38   (*proof tools*)
    39   val prove_conv: tactic list -> Proof.context ->
    40                   thm list -> string list -> term * term -> thm option
    41   val trans_tac: simpset -> thm option -> tactic (*applies the initial lemma*)
    42   val norm_tac: simpset -> tactic                (*proves the initial lemma*)
    43   val numeral_simp_tac: simpset -> tactic        (*proves the final theorem*)
    44   val simplify_meta_eq: simpset -> thm -> thm    (*simplifies the final theorem*)
    45 end;
    46 
    47 
    48 functor CancelNumeralsFun(Data: CANCEL_NUMERALS_DATA):
    49   sig
    50   val proc: simpset -> term -> thm option
    51   end
    52 =
    53 struct
    54 
    55 (*For t = #n*u then put u in the table*)
    56 fun update_by_coeff t =
    57   Termtab.update (#2 (Data.dest_coeff t), ());
    58 
    59 (*a left-to-right scan of terms1, seeking a term of the form #n*u, where
    60   #m*u is in terms2 for some m*)
    61 fun find_common (terms1,terms2) =
    62   let val tab2 = fold update_by_coeff terms2 Termtab.empty
    63       fun seek [] = raise TERM("find_common", [])
    64         | seek (t::terms) =
    65               let val (_,u) = Data.dest_coeff t
    66               in if Termtab.defined tab2 u then u else seek terms end
    67   in  seek terms1 end;
    68 
    69 (*the simplification procedure*)
    70 fun proc ss t =
    71   let
    72       val ctxt = Simplifier.the_context ss;
    73       val hyps = prems_of_ss ss;
    74       (*first freeze any Vars in the term to prevent flex-flex problems*)
    75       val (t', xs) = Term.adhoc_freeze_vars t;
    76       val (t1,t2) = Data.dest_bal t'
    77       val terms1 = Data.dest_sum t1
    78       and terms2 = Data.dest_sum t2
    79       val u = find_common (terms1,terms2)
    80       val (n1, terms1') = Data.find_first_coeff u terms1
    81       and (n2, terms2') = Data.find_first_coeff u terms2
    82       and T = Term.fastype_of u
    83       fun newshape (i,terms) = Data.mk_sum T (Data.mk_coeff(i,u)::terms)
    84       val reshape =  (*Move i*u to the front and put j*u into standard form
    85                        i + #m + j + k == #m + i + (j + k) *)
    86             if n1=0 orelse n2=0 then   (*trivial, so do nothing*)
    87                 raise TERM("cancel_numerals", [])
    88             else Data.prove_conv [Data.norm_tac ss] ctxt hyps xs
    89                         (t',
    90                          Data.mk_bal (newshape(n1,terms1'),
    91                                       newshape(n2,terms2')))
    92   in
    93       Option.map (Data.simplify_meta_eq ss)
    94        (if n2<=n1 then
    95             Data.prove_conv
    96                [Data.trans_tac ss reshape, rtac Data.bal_add1 1,
    97                 Data.numeral_simp_tac ss] ctxt hyps xs
    98                (t', Data.mk_bal (newshape(n1-n2,terms1'),
    99                                  Data.mk_sum T terms2'))
   100         else
   101             Data.prove_conv
   102                [Data.trans_tac ss reshape, rtac Data.bal_add2 1,
   103                 Data.numeral_simp_tac ss] ctxt hyps xs
   104                (t', Data.mk_bal (Data.mk_sum T terms1',
   105                                  newshape(n2-n1,terms2'))))
   106   end
   107   handle TERM _ => NONE
   108        | TYPE _ => NONE;   (*Typically (if thy doesn't include Numeral)
   109                              Undeclared type constructor "Numeral.bin"*)
   110 
   111 end;