src/HOL/Integ/nat_simprocs.ML
author haftmann
Sat, 19 May 2007 11:33:30 +0200
changeset 23024 70435ffe077d
parent 22997 d4f3b015b50b
child 23058 c722004c5a22
permissions -rw-r--r--
fixed text
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/nat_simprocs.ML
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     4
    Copyright   2000  University of Cambridge
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     5
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     6
Simprocs for nat numerals.
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     7
*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     8
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     9
structure Nat_Numeral_Simprocs =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    10
struct
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    11
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    12
(*Maps n to #n for n = 0, 1, 2*)
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    13
val numeral_syms =
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14369
diff changeset
    14
       [nat_numeral_0_eq_0 RS sym, nat_numeral_1_eq_1 RS sym, numeral_2_eq_2 RS sym];
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    15
val numeral_sym_ss = HOL_ss addsimps numeral_syms;
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    16
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
    17
fun rename_numerals th =
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    18
    simplify numeral_sym_ss (Thm.transfer (the_context ()) th);
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    19
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    20
(*Utilities*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    21
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21416
diff changeset
    22
fun mk_number n = HOLogic.number_of_const HOLogic.natT $ HOLogic.mk_numeral n;
21873
62d2416728f5 introduced abstract view on number expressions in hologic.ML
haftmann
parents: 21820
diff changeset
    23
fun dest_number t = IntInf.max (0, snd (HOLogic.dest_number t));
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    24
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    25
fun find_first_numeral past (t::terms) =
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21416
diff changeset
    26
        ((dest_number t, t, rev past @ terms)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    27
         handle TERM _ => find_first_numeral (t::past) terms)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    28
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    29
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21416
diff changeset
    30
val zero = mk_number 0;
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
    31
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    32
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    33
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    34
fun mk_sum []        = zero
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    35
  | mk_sum [t,u]     = mk_plus (t, u)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    36
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    37
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    38
(*this version ALWAYS includes a trailing zero*)
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    39
fun long_mk_sum []        = HOLogic.zero
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    40
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    41
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
    42
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} HOLogic.natT;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    43
11377
0f16ad464c62 Simprocs for type "nat" no longer introduce numerals unless they are already
paulson
parents: 11334
diff changeset
    44
0f16ad464c62 Simprocs for type "nat" no longer introduce numerals unless they are already
paulson
parents: 11334
diff changeset
    45
(** Other simproc items **)
0f16ad464c62 Simprocs for type "nat" no longer introduce numerals unless they are already
paulson
parents: 11334
diff changeset
    46
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    47
val trans_tac = Int_Numeral_Simprocs.trans_tac;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    48
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14390
diff changeset
    49
val bin_simps =
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14390
diff changeset
    50
     [nat_numeral_0_eq_0 RS sym, nat_numeral_1_eq_1 RS sym,
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14390
diff changeset
    51
      add_nat_number_of, nat_number_of_add_left, 
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14390
diff changeset
    52
      diff_nat_number_of, le_number_of_eq_not_less,
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14390
diff changeset
    53
      mult_nat_number_of, nat_number_of_mult_left, 
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14390
diff changeset
    54
      less_nat_number_of, 
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
    55
      @{thm Let_number_of}, nat_number_of] @
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
    56
     arith_simps @ rel_simps;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    57
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
    58
fun prep_simproc (name, pats, proc) =
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 22548
diff changeset
    59
  Simplifier.simproc (the_context ()) name pats proc;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    60
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    61
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    62
(*** CancelNumerals simprocs ***)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    63
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21416
diff changeset
    64
val one = mk_number 1;
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
    65
val mk_times = HOLogic.mk_binop @{const_name HOL.times};
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    66
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    67
fun mk_prod [] = one
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    68
  | mk_prod [t] = t
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    69
  | mk_prod (t :: ts) = if t = one then mk_prod ts
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    70
                        else mk_times (t, mk_prod ts);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    71
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
    72
val dest_times = HOLogic.dest_bin @{const_name HOL.times} HOLogic.natT;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    73
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    74
fun dest_prod t =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    75
      let val (t,u) = dest_times t
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    76
      in  dest_prod t @ dest_prod u  end
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    77
      handle TERM _ => [t];
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    78
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    79
(*DON'T do the obvious simplifications; that would create special cases*)
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21416
diff changeset
    80
fun mk_coeff (k,t) = mk_times (mk_number k, t);
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    81
9544
f9202e219a29 added a dummy "thm list" argument to prove_conv for the new interface to
paulson
parents: 9436
diff changeset
    82
(*Express t as a product of (possibly) a numeral with other factors, sorted*)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    83
fun dest_coeff t =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    84
    let val ts = sort Term.term_ord (dest_prod t)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    85
        val (n, _, ts') = find_first_numeral [] ts
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    86
                          handle TERM _ => (1, one, ts)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    87
    in (n, mk_prod ts') end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    88
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    89
(*Find first coefficient-term THAT MATCHES u*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    90
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    91
  | find_first_coeff past u (t::terms) =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    92
        let val (n,u') = dest_coeff t
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    93
        in  if u aconv u' then (n, rev past @ terms)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    94
                          else find_first_coeff (t::past) u terms
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    95
        end
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    96
        handle TERM _ => find_first_coeff (t::past) u terms;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    97
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    98
19298
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
    99
(*Split up a sum into the list of its constituent terms, on the way removing any
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   100
  Sucs and counting them.*)
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   101
fun dest_Suc_sum (Const ("Suc", _) $ t, (k,ts)) = dest_Suc_sum (t, (k+1,ts))
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   102
  | dest_Suc_sum (t, (k,ts)) = 
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   103
      let val (t1,t2) = dest_plus t
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   104
      in  dest_Suc_sum (t1, dest_Suc_sum (t2, (k,ts)))  end
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   105
      handle TERM _ => (k, t::ts);
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   106
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   107
(*Code for testing whether numerals are already used in the goal*)
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   108
fun is_numeral (Const(@{const_name Numeral.number_of}, _) $ w) = true
19298
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   109
  | is_numeral _ = false;
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   110
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   111
fun prod_has_numeral t = exists is_numeral (dest_prod t);
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   112
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   113
(*The Sucs found in the term are converted to a binary numeral. If relaxed is false,
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   114
  an exception is raised unless the original expression contains at least one
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   115
  numeral in a coefficient position.  This prevents nat_combine_numerals from 
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   116
  introducing numerals to goals.*)
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   117
fun dest_Sucs_sum relaxed t = 
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   118
  let val (k,ts) = dest_Suc_sum (t,(0,[]))
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   119
  in
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   120
     if relaxed orelse exists prod_has_numeral ts then 
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   121
       if k=0 then ts
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21416
diff changeset
   122
       else mk_number (IntInf.fromInt k) :: ts
19298
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   123
     else raise TERM("Nat_Numeral_Simprocs.dest_Sucs_sum", [t])
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   124
  end;
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   125
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   126
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
   127
(*Simplify 1*n and n*1 to n*)
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
   128
val add_0s  = map rename_numerals [add_0, add_0_right];
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   129
val mult_1s = map rename_numerals [@{thm nat_mult_1}, @{thm nat_mult_1_right}];
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   130
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11377
diff changeset
   131
(*Final simplification: cancel + and *; replace Numeral0 by 0 and Numeral1 by 1*)
12949
b94843ffc0d1 Fixed the natxx_cancel_numerals simprocs so that they pull out Sucs and
paulson
parents: 12338
diff changeset
   132
b94843ffc0d1 Fixed the natxx_cancel_numerals simprocs so that they pull out Sucs and
paulson
parents: 12338
diff changeset
   133
(*And these help the simproc return False when appropriate, which helps
b94843ffc0d1 Fixed the natxx_cancel_numerals simprocs so that they pull out Sucs and
paulson
parents: 12338
diff changeset
   134
  the arith prover.*)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   135
val contra_rules = [add_Suc, add_Suc_right, Zero_not_Suc, Suc_not_Zero,
12949
b94843ffc0d1 Fixed the natxx_cancel_numerals simprocs so that they pull out Sucs and
paulson
parents: 12338
diff changeset
   136
                    le_0_eq];
b94843ffc0d1 Fixed the natxx_cancel_numerals simprocs so that they pull out Sucs and
paulson
parents: 12338
diff changeset
   137
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   138
val simplify_meta_eq =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   139
    Int_Numeral_Simprocs.simplify_meta_eq
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14369
diff changeset
   140
        ([nat_numeral_0_eq_0, numeral_1_eq_Suc_0, add_0, add_0_right,
12949
b94843ffc0d1 Fixed the natxx_cancel_numerals simprocs so that they pull out Sucs and
paulson
parents: 12338
diff changeset
   141
          mult_0, mult_0_right, mult_1, mult_1_right] @ contra_rules);
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   142
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   143
14474
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   144
(*Like HOL_ss but with an ordering that brings numerals to the front
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   145
  under AC-rewriting.*)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   146
val num_ss = Int_Numeral_Simprocs.num_ss;
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   147
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   148
(*** Applying CancelNumeralsFun ***)
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   149
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   150
structure CancelNumeralsCommon =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   151
  struct
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14369
diff changeset
   152
  val mk_sum            = (fn T:typ => mk_sum)
19298
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   153
  val dest_sum          = dest_Sucs_sum true
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   154
  val mk_coeff          = mk_coeff
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   155
  val dest_coeff        = dest_coeff
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   156
  val find_first_coeff  = find_first_coeff []
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   157
  val trans_tac         = fn _ => trans_tac
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   158
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   159
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   160
    [Suc_eq_add_numeral_1_left] @ add_ac
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   161
  val norm_ss2 = num_ss addsimps bin_simps @ add_ac @ mult_ac
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   162
  fun norm_tac ss = 
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   163
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   164
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   165
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   166
  val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps;
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   167
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss));
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   168
  val simplify_meta_eq  = simplify_meta_eq
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   169
  end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   170
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   171
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   172
structure EqCancelNumerals = CancelNumeralsFun
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   173
 (open CancelNumeralsCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   174
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   175
  val mk_bal   = HOLogic.mk_eq
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   176
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   177
  val bal_add1 = nat_eq_add_iff1 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   178
  val bal_add2 = nat_eq_add_iff2 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   179
);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   180
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   181
structure LessCancelNumerals = CancelNumeralsFun
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   182
 (open CancelNumeralsCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   183
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   184
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   185
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   186
  val bal_add1 = nat_less_add_iff1 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   187
  val bal_add2 = nat_less_add_iff2 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   188
);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   189
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   190
structure LeCancelNumerals = CancelNumeralsFun
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   191
 (open CancelNumeralsCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   192
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   193
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   194
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   195
  val bal_add1 = nat_le_add_iff1 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   196
  val bal_add2 = nat_le_add_iff2 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   197
);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   198
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   199
structure DiffCancelNumerals = CancelNumeralsFun
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   200
 (open CancelNumeralsCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   201
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   202
  val mk_bal   = HOLogic.mk_binop @{const_name HOL.minus}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   203
  val dest_bal = HOLogic.dest_bin @{const_name HOL.minus} HOLogic.natT
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   204
  val bal_add1 = nat_diff_add_eq1 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   205
  val bal_add2 = nat_diff_add_eq2 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   206
);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   207
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   208
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   209
val cancel_numerals =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   210
  map prep_simproc
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   211
   [("nateq_cancel_numerals",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   212
     ["(l::nat) + m = n", "(l::nat) = m + n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   213
      "(l::nat) * m = n", "(l::nat) = m * n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   214
      "Suc m = n", "m = Suc n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   215
     K EqCancelNumerals.proc),
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   216
    ("natless_cancel_numerals",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   217
     ["(l::nat) + m < n", "(l::nat) < m + n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   218
      "(l::nat) * m < n", "(l::nat) < m * n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   219
      "Suc m < n", "m < Suc n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   220
     K LessCancelNumerals.proc),
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   221
    ("natle_cancel_numerals",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   222
     ["(l::nat) + m <= n", "(l::nat) <= m + n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   223
      "(l::nat) * m <= n", "(l::nat) <= m * n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   224
      "Suc m <= n", "m <= Suc n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   225
     K LeCancelNumerals.proc),
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   226
    ("natdiff_cancel_numerals",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   227
     ["((l::nat) + m) - n", "(l::nat) - (m + n)",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   228
      "(l::nat) * m - n", "(l::nat) - m * n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   229
      "Suc m - n", "m - Suc n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   230
     K DiffCancelNumerals.proc)];
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   231
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   232
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   233
(*** Applying CombineNumeralsFun ***)
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   234
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   235
structure CombineNumeralsData =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   236
  struct
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15921
diff changeset
   237
  val add               = IntInf.+
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14369
diff changeset
   238
  val mk_sum            = (fn T:typ => long_mk_sum)  (*to work for 2*x + 3*x *)
19298
741b8138c2b3 Now SML/NJ-friendly (IntInf)
paulson
parents: 19294
diff changeset
   239
  val dest_sum          = dest_Sucs_sum false
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   240
  val mk_coeff          = mk_coeff
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   241
  val dest_coeff        = dest_coeff
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   242
  val left_distrib      = left_add_mult_distrib RS trans
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   243
  val prove_conv        = Int_Numeral_Base_Simprocs.prove_conv_nohyps
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   244
  val trans_tac         = fn _ => trans_tac
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   245
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   246
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @ [Suc_eq_add_numeral_1] @ add_ac
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   247
  val norm_ss2 = num_ss addsimps bin_simps @ add_ac @ mult_ac
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   248
  fun norm_tac ss =
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   249
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   250
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   251
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   252
  val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps;
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   253
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   254
  val simplify_meta_eq  = simplify_meta_eq
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   255
  end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   256
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   257
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   258
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   259
val combine_numerals =
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   260
  prep_simproc ("nat_combine_numerals", ["(i::nat) + j", "Suc (i + j)"], K CombineNumerals.proc);
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   261
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   262
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   263
(*** Applying CancelNumeralFactorFun ***)
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   264
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   265
structure CancelNumeralFactorCommon =
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   266
  struct
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   267
  val mk_coeff          = mk_coeff
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   268
  val dest_coeff        = dest_coeff
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   269
  val trans_tac         = fn _ => trans_tac
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   270
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   271
  val norm_ss1 = num_ss addsimps
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   272
    numeral_syms @ add_0s @ mult_1s @ [Suc_eq_add_numeral_1_left] @ add_ac
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   273
  val norm_ss2 = num_ss addsimps bin_simps @ add_ac @ mult_ac
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   274
  fun norm_tac ss =
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   275
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   276
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   277
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   278
  val numeral_simp_ss = HOL_ss addsimps bin_simps
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   279
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   280
  val simplify_meta_eq  = simplify_meta_eq
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   281
  end
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   282
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   283
structure DivCancelNumeralFactor = CancelNumeralFactorFun
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   284
 (open CancelNumeralFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   285
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   286
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   287
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   288
  val cancel = nat_mult_div_cancel1 RS trans
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   289
  val neg_exchanges = false
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   290
)
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   291
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   292
structure EqCancelNumeralFactor = CancelNumeralFactorFun
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   293
 (open CancelNumeralFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   294
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   295
  val mk_bal   = HOLogic.mk_eq
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   296
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   297
  val cancel = nat_mult_eq_cancel1 RS trans
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   298
  val neg_exchanges = false
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   299
)
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   300
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   301
structure LessCancelNumeralFactor = CancelNumeralFactorFun
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   302
 (open CancelNumeralFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   303
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   304
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   305
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   306
  val cancel = nat_mult_less_cancel1 RS trans
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   307
  val neg_exchanges = true
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   308
)
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   309
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   310
structure LeCancelNumeralFactor = CancelNumeralFactorFun
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   311
 (open CancelNumeralFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   312
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   313
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   314
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   315
  val cancel = nat_mult_le_cancel1 RS trans
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   316
  val neg_exchanges = true
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   317
)
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   318
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   319
val cancel_numeral_factors =
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   320
  map prep_simproc
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   321
   [("nateq_cancel_numeral_factors",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   322
     ["(l::nat) * m = n", "(l::nat) = m * n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   323
     K EqCancelNumeralFactor.proc),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   324
    ("natless_cancel_numeral_factors",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   325
     ["(l::nat) * m < n", "(l::nat) < m * n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   326
     K LessCancelNumeralFactor.proc),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   327
    ("natle_cancel_numeral_factors",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   328
     ["(l::nat) * m <= n", "(l::nat) <= m * n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   329
     K LeCancelNumeralFactor.proc),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   330
    ("natdiv_cancel_numeral_factors",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   331
     ["((l::nat) * m) div n", "(l::nat) div (m * n)"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   332
     K DivCancelNumeralFactor.proc)];
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   333
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   334
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   335
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   336
(*** Applying ExtractCommonTermFun ***)
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   337
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   338
(*this version ALWAYS includes a trailing one*)
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   339
fun long_mk_prod []        = one
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   340
  | long_mk_prod (t :: ts) = mk_times (t, mk_prod ts);
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   341
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   342
(*Find first term that matches u*)
18442
b35d7312c64f resolved shadowing of Library.find_first
haftmann
parents: 18328
diff changeset
   343
fun find_first_t past u []         = raise TERM("find_first_t", [])
b35d7312c64f resolved shadowing of Library.find_first
haftmann
parents: 18328
diff changeset
   344
  | find_first_t past u (t::terms) =
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   345
        if u aconv t then (rev past @ terms)
18442
b35d7312c64f resolved shadowing of Library.find_first
haftmann
parents: 18328
diff changeset
   346
        else find_first_t (t::past) u terms
b35d7312c64f resolved shadowing of Library.find_first
haftmann
parents: 18328
diff changeset
   347
        handle TERM _ => find_first_t (t::past) u terms;
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   348
15271
3c32a26510c4 fixed some awkward problems with nat/int simprocs
paulson
parents: 14474
diff changeset
   349
(** Final simplification for the CancelFactor simprocs **)
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 21873
diff changeset
   350
val simplify_one = Int_Numeral_Simprocs.simplify_meta_eq  
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 21873
diff changeset
   351
  [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_1}, @{thm numeral_1_eq_Suc_0}];
15271
3c32a26510c4 fixed some awkward problems with nat/int simprocs
paulson
parents: 14474
diff changeset
   352
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   353
fun cancel_simplify_meta_eq cancel_th ss th =
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   354
    simplify_one ss (([th, cancel_th]) MRS trans);
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   355
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   356
structure CancelFactorCommon =
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   357
  struct
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14369
diff changeset
   358
  val mk_sum            = (fn T:typ => long_mk_prod)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   359
  val dest_sum          = dest_prod
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   360
  val mk_coeff          = mk_coeff
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   361
  val dest_coeff        = dest_coeff
18442
b35d7312c64f resolved shadowing of Library.find_first
haftmann
parents: 18328
diff changeset
   362
  val find_first        = find_first_t []
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   363
  val trans_tac         = fn _ => trans_tac
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   364
  val norm_ss = HOL_ss addsimps mult_1s @ mult_ac
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17877
diff changeset
   365
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   366
  end;
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   367
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   368
structure EqCancelFactor = ExtractCommonTermFun
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   369
 (open CancelFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   370
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   371
  val mk_bal   = HOLogic.mk_eq
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   372
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   373
  val simplify_meta_eq  = cancel_simplify_meta_eq nat_mult_eq_cancel_disj
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   374
);
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   375
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   376
structure LessCancelFactor = ExtractCommonTermFun
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   377
 (open CancelFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   378
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   379
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   380
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   381
  val simplify_meta_eq  = cancel_simplify_meta_eq nat_mult_less_cancel_disj
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   382
);
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   383
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   384
structure LeCancelFactor = ExtractCommonTermFun
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   385
 (open CancelFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   386
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   387
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   388
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   389
  val simplify_meta_eq  = cancel_simplify_meta_eq nat_mult_le_cancel_disj
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   390
);
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   391
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   392
structure DivideCancelFactor = ExtractCommonTermFun
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   393
 (open CancelFactorCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20044
diff changeset
   394
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   395
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   396
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   397
  val simplify_meta_eq  = cancel_simplify_meta_eq nat_mult_div_cancel_disj
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   398
);
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   399
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   400
val cancel_factor =
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   401
  map prep_simproc
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   402
   [("nat_eq_cancel_factor",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   403
     ["(l::nat) * m = n", "(l::nat) = m * n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   404
     K EqCancelFactor.proc),
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   405
    ("nat_less_cancel_factor",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   406
     ["(l::nat) * m < n", "(l::nat) < m * n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   407
     K LessCancelFactor.proc),
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   408
    ("nat_le_cancel_factor",
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   409
     ["(l::nat) * m <= n", "(l::nat) <= m * n"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   410
     K LeCancelFactor.proc),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   411
    ("nat_divide_cancel_factor",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   412
     ["((l::nat) * m) div n", "(l::nat) div (m * n)"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19481
diff changeset
   413
     K DivideCancelFactor.proc)];
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   414
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   415
end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   416
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   417
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   418
Addsimprocs Nat_Numeral_Simprocs.cancel_numerals;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   419
Addsimprocs [Nat_Numeral_Simprocs.combine_numerals];
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   420
Addsimprocs Nat_Numeral_Simprocs.cancel_numeral_factors;
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   421
Addsimprocs Nat_Numeral_Simprocs.cancel_factor;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   422
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   423
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   424
(*examples:
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   425
print_depth 22;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   426
set timing;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   427
set trace_simp;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   428
fun test s = (Goal s; by (Simp_tac 1));
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   429
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   430
(*cancel_numerals*)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   431
test "l +( 2) + (2) + 2 + (l + 2) + (oo  + 2) = (uu::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   432
test "(2*length xs < 2*length xs + j)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   433
test "(2*length xs < length xs * 2 + j)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   434
test "2*u = (u::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   435
test "2*u = Suc (u)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   436
test "(i + j + 12 + (k::nat)) - 15 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   437
test "(i + j + 12 + (k::nat)) - 5 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   438
test "Suc u - 2 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   439
test "Suc (Suc (Suc u)) - 2 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   440
test "(i + j + 2 + (k::nat)) - 1 = y";
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
   441
test "(i + j + 1 + (k::nat)) - 2 = y";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   442
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   443
test "(2*x + (u*v) + y) - v*3*u = (w::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   444
test "(2*x*u*v + 5 + (u*v)*4 + y) - v*u*4 = (w::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   445
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   446
test "Suc (Suc (2*x*u*v + u*4 + y)) - u = w";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   447
test "Suc ((u*v)*4) - v*3*u = w";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   448
test "Suc (Suc ((u*v)*3)) - v*3*u = w";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   449
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   450
test "(i + j + 12 + (k::nat)) = u + 15 + y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   451
test "(i + j + 32 + (k::nat)) - (u + 15 + y) = zz";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   452
test "(i + j + 12 + (k::nat)) = u + 5 + y";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   453
(*Suc*)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   454
test "(i + j + 12 + k) = Suc (u + y)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   455
test "Suc (Suc (Suc (Suc (Suc (u + y))))) <= ((i + j) + 41 + k)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   456
test "(i + j + 5 + k) < Suc (Suc (Suc (Suc (Suc (u + y)))))";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   457
test "Suc (Suc (Suc (Suc (Suc (u + y))))) - 5 = v";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   458
test "(i + j + 5 + k) = Suc (Suc (Suc (Suc (Suc (Suc (Suc (u + y)))))))";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   459
test "2*y + 3*z + 2*u = Suc (u)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   460
test "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = Suc (u)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   461
test "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = 2*y' + 3*z' + 6*w' + 2*y' + 3*z' + u + (vv::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   462
test "6 + 2*y + 3*z + 4*u = Suc (vv + 2*u + z)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   463
test "(2*n*m) < (3*(m*n)) + (u::nat)";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   464
14474
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   465
test "(Suc (Suc (Suc (Suc (Suc (Suc (case length (f c) of 0 => 0 | Suc k => k)))))) <= Suc 0)";
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   466
 
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   467
test "Suc (Suc (Suc (Suc (Suc (Suc (length l1 + length l2)))))) <= length l1";
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   468
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   469
test "( (Suc (Suc (Suc (Suc (Suc (length (compT P E A ST mxr e) + length l3)))))) <= length (compT P E A ST mxr e))";
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   470
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   471
test "( (Suc (Suc (Suc (Suc (Suc (length (compT P E A ST mxr e) + length (compT P E (A Un \<A> e) ST mxr c))))))) <= length (compT P E A ST mxr e))";
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   472
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14430
diff changeset
   473
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   474
(*negative numerals: FAIL*)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   475
test "(i + j + -23 + (k::nat)) < u + 15 + y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   476
test "(i + j + 3 + (k::nat)) < u + -15 + y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   477
test "(i + j + -12 + (k::nat)) - 15 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   478
test "(i + j + 12 + (k::nat)) - -15 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   479
test "(i + j + -12 + (k::nat)) - -15 = y";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   480
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   481
(*combine_numerals*)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   482
test "k + 3*k = (u::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   483
test "Suc (i + 3) = u";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   484
test "Suc (i + j + 3 + k) = u";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   485
test "k + j + 3*k + j = (u::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   486
test "Suc (j*i + i + k + 5 + 3*k + i*j*4) = (u::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   487
test "(2*n*m) + (3*(m*n)) = (u::nat)";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   488
(*negative numerals: FAIL*)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   489
test "Suc (i + j + -3 + k) = u";
10536
8f34ecae1446 invoking CancelNumeralFactorFun
paulson
parents: 9571
diff changeset
   490
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   491
(*cancel_numeral_factors*)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   492
test "9*x = 12 * (y::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   493
test "(9*x) div (12 * (y::nat)) = z";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   494
test "9*x < 12 * (y::nat)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   495
test "9*x <= 12 * (y::nat)";
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   496
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   497
(*cancel_factor*)
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   498
test "x*k = k*(y::nat)";
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   499
test "k = k*(y::nat)";
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   500
test "a*(b*c) = (b::nat)";
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   501
test "a*(b*c) = d*(b::nat)*(x*a)";
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   502
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   503
test "x*k < k*(y::nat)";
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   504
test "k < k*(y::nat)";
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   505
test "a*(b*c) < (b::nat)";
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   506
test "a*(b*c) < d*(b::nat)*(x*a)";
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   507
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   508
test "x*k <= k*(y::nat)";
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   509
test "k <= k*(y::nat)";
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   510
test "a*(b*c) <= (b::nat)";
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   511
test "a*(b*c) <= d*(b::nat)*(x*a)";
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   512
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   513
test "(x*k) div (k*(y::nat)) = (uu::nat)";
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12949
diff changeset
   514
test "(k) div (k*(y::nat)) = (uu::nat)";
10704
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   515
test "(a*(b*c)) div ((b::nat)) = (uu::nat)";
c1643c077df4 inserting the simproc nat_cancel_factor
paulson
parents: 10693
diff changeset
   516
test "(a*(b*c)) div (d*(b::nat)*(x*a)) = (uu::nat)";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   517
*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   518
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   519
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   520
(*** Prepare linear arithmetic for nat numerals ***)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   521
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   522
local
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   523
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   524
(* reduce contradictory <= to False *)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   525
val add_rules =
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22578
diff changeset
   526
  [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1}, nat_0, nat_1,
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
   527
   add_nat_number_of, diff_nat_number_of, mult_nat_number_of,
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents: 14273
diff changeset
   528
   eq_nat_number_of, less_nat_number_of, le_number_of_eq_not_less,
11296
38a69e5d79fa added mult_Suc laws to lin.arith.simpset.
nipkow
parents: 11259
diff changeset
   529
   le_Suc_number_of,le_number_of_Suc,
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   530
   less_Suc_number_of,less_number_of_Suc,
11296
38a69e5d79fa added mult_Suc laws to lin.arith.simpset.
nipkow
parents: 11259
diff changeset
   531
   Suc_eq_number_of,eq_number_of_Suc,
14369
c50188fe6366 tidying up arithmetic for the hyperreals
paulson
parents: 14365
diff changeset
   532
   mult_Suc, mult_Suc_right,
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   533
   eq_number_of_0, eq_0_number_of, less_0_number_of,
14390
55fe71faadda further tweaks to the numeric theories
paulson
parents: 14387
diff changeset
   534
   of_int_number_of_eq, of_nat_number_of_eq, nat_number_of, if_True, if_False];
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   535
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 21873
diff changeset
   536
val simprocs = Nat_Numeral_Simprocs.combine_numerals
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 21873
diff changeset
   537
  :: Nat_Numeral_Simprocs.cancel_numerals;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   538
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   539
in
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   540
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   541
val nat_simprocs_setup =
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18442
diff changeset
   542
  Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
10693
9e4a0e84d0d6 moved mk_bin from Numerals to HOLogic
nipkow
parents: 10574
diff changeset
   543
   {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
15921
b6e345548913 Fixing a problem with lin.arith.
nipkow
parents: 15271
diff changeset
   544
    inj_thms = inj_thms, lessD = lessD, neqE = neqE,
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   545
    simpset = simpset addsimps add_rules
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18442
diff changeset
   546
                      addsimprocs simprocs});
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   547
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   548
end;