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