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