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