src/Provers/Arith/cancel_numerals.ML
author wenzelm
Wed Jul 12 21:19:17 2006 +0200 (2006-07-12)
changeset 20114 a1bb4bc68ff3
parent 20044 92cc2f4c7335
child 24630 351a308ab58d
permissions -rw-r--r--
prove_conv: Variable.import_terms instead of Term.addhoc_freeze_vars;
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@20114
    39
  val prove_conv: tactic list -> Proof.context -> thm list -> term * term -> thm option
wenzelm@16973
    40
  val trans_tac: simpset -> thm option -> tactic (*applies the initial lemma*)
wenzelm@16973
    41
  val norm_tac: simpset -> tactic                (*proves the initial lemma*)
wenzelm@16973
    42
  val numeral_simp_tac: simpset -> tactic        (*proves the final theorem*)
wenzelm@16973
    43
  val simplify_meta_eq: simpset -> thm -> thm    (*simplifies the final theorem*)
paulson@8736
    44
end;
paulson@8736
    45
paulson@8736
    46
paulson@8760
    47
functor CancelNumeralsFun(Data: CANCEL_NUMERALS_DATA):
paulson@8760
    48
  sig
wenzelm@20044
    49
  val proc: simpset -> term -> thm option
wenzelm@17223
    50
  end
paulson@8760
    51
=
paulson@8736
    52
struct
paulson@8736
    53
paulson@8779
    54
(*For t = #n*u then put u in the table*)
wenzelm@17223
    55
fun update_by_coeff t =
wenzelm@17412
    56
  Termtab.update (#2 (Data.dest_coeff t), ());
paulson@8760
    57
paulson@8760
    58
(*a left-to-right scan of terms1, seeking a term of the form #n*u, where
paulson@8760
    59
  #m*u is in terms2 for some m*)
paulson@8760
    60
fun find_common (terms1,terms2) =
wenzelm@17223
    61
  let val tab2 = fold update_by_coeff terms2 Termtab.empty
wenzelm@17223
    62
      fun seek [] = raise TERM("find_common", [])
wenzelm@17223
    63
        | seek (t::terms) =
wenzelm@17223
    64
              let val (_,u) = Data.dest_coeff t
wenzelm@17223
    65
              in if Termtab.defined tab2 u then u else seek terms end
paulson@8760
    66
  in  seek terms1 end;
paulson@8736
    67
paulson@8736
    68
(*the simplification procedure*)
wenzelm@20044
    69
fun proc ss t =
wenzelm@15027
    70
  let
wenzelm@20114
    71
    val ctxt = Simplifier.the_context ss
wenzelm@20114
    72
    val prems = prems_of_ss ss
wenzelm@20114
    73
    val ([t'], ctxt') = Variable.import_terms true [t] ctxt
wenzelm@20114
    74
    val export = singleton (Variable.export ctxt' ctxt)
wenzelm@20114
    75
wenzelm@20114
    76
    val (t1,t2) = Data.dest_bal t'
wenzelm@20114
    77
    val terms1 = Data.dest_sum t1
wenzelm@20114
    78
    and terms2 = Data.dest_sum t2
wenzelm@20114
    79
wenzelm@20114
    80
    val u = find_common (terms1, terms2)
wenzelm@20114
    81
    val (n1, terms1') = Data.find_first_coeff u terms1
wenzelm@20114
    82
    and (n2, terms2') = Data.find_first_coeff u terms2
wenzelm@20114
    83
    and T = Term.fastype_of u
wenzelm@20114
    84
wenzelm@20114
    85
    fun newshape (i,terms) = Data.mk_sum T (Data.mk_coeff(i,u)::terms)
wenzelm@20114
    86
    val reshape =  (*Move i*u to the front and put j*u into standard form
wenzelm@17223
    87
                       i + #m + j + k == #m + i + (j + k) *)
wenzelm@20114
    88
        if n1=0 orelse n2=0 then   (*trivial, so do nothing*)
wenzelm@20114
    89
          raise TERM("cancel_numerals", [])
wenzelm@20114
    90
        else Data.prove_conv [Data.norm_tac ss] ctxt prems
wenzelm@20114
    91
          (t', Data.mk_bal (newshape(n1,terms1'), newshape(n2,terms2')))
paulson@8736
    92
  in
wenzelm@20114
    93
    Option.map (export o Data.simplify_meta_eq ss)
wenzelm@20114
    94
      (if n2 <= n1 then
wenzelm@20114
    95
         Data.prove_conv
wenzelm@20114
    96
           [Data.trans_tac ss reshape, rtac Data.bal_add1 1,
wenzelm@20114
    97
            Data.numeral_simp_tac ss] ctxt prems
wenzelm@20114
    98
           (t', Data.mk_bal (newshape(n1-n2,terms1'), Data.mk_sum T terms2'))
wenzelm@20114
    99
       else
wenzelm@20114
   100
         Data.prove_conv
wenzelm@20114
   101
           [Data.trans_tac ss reshape, rtac Data.bal_add2 1,
wenzelm@20114
   102
            Data.numeral_simp_tac ss] ctxt prems
wenzelm@20114
   103
           (t', Data.mk_bal (Data.mk_sum T terms1', newshape(n2-n1,terms2'))))
paulson@8736
   104
  end
wenzelm@20114
   105
  (* FIXME avoid handling of generic exceptions *)
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;