src/HOL/Integ/int_arith1.ML
author wenzelm
Wed, 04 Apr 2007 00:11:03 +0200
changeset 22578 b0eb5652f210
parent 22548 6ce4bddf3bcb
child 22997 d4f3b015b50b
permissions -rw-r--r--
removed obsolete sign_of/sign_of_thm;
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/Integ/int_arith1.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
    Authors:    Larry Paulson and Tobias Nipkow
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     4
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     5
Simprocs and decision procedure for linear arithmetic.
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     6
*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
     7
14329
ff3210fe968f re-organized some hyperreal and real lemmas
paulson
parents: 14273
diff changeset
     8
(** Misc ML bindings **)
ff3210fe968f re-organized some hyperreal and real lemmas
paulson
parents: 14273
diff changeset
     9
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    10
val succ_Pls = thm "succ_Pls";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    11
val succ_Min = thm "succ_Min";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    12
val succ_1 = thm "succ_1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    13
val succ_0 = thm "succ_0";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    14
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    15
val pred_Pls = thm "pred_Pls";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    16
val pred_Min = thm "pred_Min";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    17
val pred_1 = thm "pred_1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    18
val pred_0 = thm "pred_0";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    19
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    20
val minus_Pls = thm "minus_Pls";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    21
val minus_Min = thm "minus_Min";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    22
val minus_1 = thm "minus_1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    23
val minus_0 = thm "minus_0";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    24
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    25
val add_Pls = thm "add_Pls";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    26
val add_Min = thm "add_Min";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    27
val add_BIT_11 = thm "add_BIT_11";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    28
val add_BIT_10 = thm "add_BIT_10";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    29
val add_BIT_0 = thm "add_BIT_0";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    30
val add_Pls_right = thm "add_Pls_right";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    31
val add_Min_right = thm "add_Min_right";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    32
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    33
val mult_Pls = thm "mult_Pls";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    34
val mult_Min = thm "mult_Min";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    35
val mult_num1 = thm "mult_num1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    36
val mult_num0 = thm "mult_num0";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    37
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    38
val neg_def = thm "neg_def";
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    39
val iszero_def = thm "iszero_def";
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
    40
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    41
val number_of_succ = thm "number_of_succ";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    42
val number_of_pred = thm "number_of_pred";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    43
val number_of_minus = thm "number_of_minus";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    44
val number_of_add = thm "number_of_add";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    45
val diff_number_of_eq = thm "diff_number_of_eq";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    46
val number_of_mult = thm "number_of_mult";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    47
val double_number_of_BIT = thm "double_number_of_BIT";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    48
val numeral_0_eq_0 = thm "numeral_0_eq_0";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    49
val numeral_1_eq_1 = thm "numeral_1_eq_1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    50
val numeral_m1_eq_minus_1 = thm "numeral_m1_eq_minus_1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    51
val mult_minus1 = thm "mult_minus1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    52
val mult_minus1_right = thm "mult_minus1_right";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    53
val minus_number_of_mult = thm "minus_number_of_mult";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    54
val zero_less_nat_eq = thm "zero_less_nat_eq";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    55
val eq_number_of_eq = thm "eq_number_of_eq";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    56
val iszero_number_of_Pls = thm "iszero_number_of_Pls";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    57
val nonzero_number_of_Min = thm "nonzero_number_of_Min";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    58
val iszero_number_of_BIT = thm "iszero_number_of_BIT";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    59
val iszero_number_of_0 = thm "iszero_number_of_0";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    60
val iszero_number_of_1 = thm "iszero_number_of_1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    61
val less_number_of_eq_neg = thm "less_number_of_eq_neg";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    62
val le_number_of_eq = thm "le_number_of_eq";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    63
val not_neg_number_of_Pls = thm "not_neg_number_of_Pls";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    64
val neg_number_of_Min = thm "neg_number_of_Min";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    65
val neg_number_of_BIT = thm "neg_number_of_BIT";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    66
val le_number_of_eq_not_less = thm "le_number_of_eq_not_less";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    67
val abs_number_of = thm "abs_number_of";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    68
val number_of_reorient = thm "number_of_reorient";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    69
val add_number_of_left = thm "add_number_of_left";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    70
val mult_number_of_left = thm "mult_number_of_left";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    71
val add_number_of_diff1 = thm "add_number_of_diff1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    72
val add_number_of_diff2 = thm "add_number_of_diff2";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    73
val less_iff_diff_less_0 = thm "less_iff_diff_less_0";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    74
val eq_iff_diff_eq_0 = thm "eq_iff_diff_eq_0";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    75
val le_iff_diff_le_0 = thm "le_iff_diff_le_0";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    76
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    77
val arith_extra_simps = thms "arith_extra_simps";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    78
val arith_simps = thms "arith_simps";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    79
val rel_simps = thms "rel_simps";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    80
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
    81
val zless_imp_add1_zle = thm "zless_imp_add1_zle";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    82
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    83
val combine_common_factor = thm "combine_common_factor";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    84
val eq_add_iff1 = thm "eq_add_iff1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    85
val eq_add_iff2 = thm "eq_add_iff2";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    86
val less_add_iff1 = thm "less_add_iff1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    87
val less_add_iff2 = thm "less_add_iff2";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    88
val le_add_iff1 = thm "le_add_iff1";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    89
val le_add_iff2 = thm "le_add_iff2";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    90
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    91
val arith_special = thms "arith_special";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
    92
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
    93
structure Int_Numeral_Base_Simprocs =
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
    94
  struct
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20045
diff changeset
    95
  fun prove_conv tacs ctxt (_: thm list) (t, u) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15185
diff changeset
    96
    if t aconv u then NONE
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
    97
    else
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
    98
      let val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20045
diff changeset
    99
      in SOME (Goal.prove ctxt [] [] eq (K (EVERY tacs))) end
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   100
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   101
  fun prove_conv_nohyps tacs sg = prove_conv tacs sg [];
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   102
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   103
  fun prep_simproc (name, pats, proc) =
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 22548
diff changeset
   104
    Simplifier.simproc (the_context()) name pats proc;
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   105
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   106
  fun is_numeral (Const("Numeral.number_of", _) $ w) = true
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   107
    | is_numeral _ = false
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   108
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   109
  fun simplify_meta_eq f_number_of_eq f_eq =
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   110
      mk_meta_eq ([f_eq, f_number_of_eq] MRS trans)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   111
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   112
  (*reorientation simprules using ==, for the following simproc*)
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   113
  val meta_zero_reorient = zero_reorient RS eq_reflection
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   114
  val meta_one_reorient = one_reorient RS eq_reflection
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   115
  val meta_number_of_reorient = number_of_reorient RS eq_reflection
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   116
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   117
  (*reorientation simplification procedure: reorients (polymorphic) 
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   118
    0 = x, 1 = x, nnn = x provided x isn't 0, 1 or a numeral.*)
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   119
  fun reorient_proc sg _ (_ $ t $ u) =
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   120
    case u of
20713
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20485
diff changeset
   121
	Const("HOL.zero", _) => NONE
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20485
diff changeset
   122
      | Const("HOL.one", _) => NONE
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15185
diff changeset
   123
      | Const("Numeral.number_of", _) $ _ => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15185
diff changeset
   124
      | _ => SOME (case t of
20713
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20485
diff changeset
   125
		  Const("HOL.zero", _) => meta_zero_reorient
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20485
diff changeset
   126
		| Const("HOL.one", _) => meta_one_reorient
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   127
		| Const("Numeral.number_of", _) $ _ => meta_number_of_reorient)
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   128
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   129
  val reorient_simproc = 
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   130
      prep_simproc ("reorient_simproc", ["0=x", "1=x", "number_of w = x"], reorient_proc)
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   131
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   132
  end;
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   133
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   134
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   135
Addsimprocs [Int_Numeral_Base_Simprocs.reorient_simproc];
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   136
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   137
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   138
structure Int_Numeral_Simprocs =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   139
struct
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   140
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   141
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic in Int_Numeral_Base_Simprocs
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11713
diff changeset
   142
  isn't complicated by the abstract 0 and 1.*)
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   143
val numeral_syms = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym];
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11713
diff changeset
   144
14474
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   145
(** New term ordering so that AC-rewriting brings numerals to the front **)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   146
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   147
(*Order integers by absolute value and then by sign. The standard integer
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   148
  ordering is not well-founded.*)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   149
fun num_ord (i,j) =
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15921
diff changeset
   150
      (case IntInf.compare (IntInf.abs i, IntInf.abs j) of
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   151
            EQUAL => int_ord (IntInf.sign i, IntInf.sign j) 
14474
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   152
          | ord => ord);
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   153
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   154
(*This resembles Term.term_ord, but it puts binary numerals before other
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   155
  non-atomic terms.*)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   156
local open Term 
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   157
in 
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   158
fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   159
      (case numterm_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   160
  | numterm_ord
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   161
     (Const("Numeral.number_of", _) $ v, Const("Numeral.number_of", _) $ w) =
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21243
diff changeset
   162
     num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)
14474
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   163
  | numterm_ord (Const("Numeral.number_of", _) $ _, _) = LESS
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   164
  | numterm_ord (_, Const("Numeral.number_of", _) $ _) = GREATER
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   165
  | numterm_ord (t, u) =
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   166
      (case int_ord (size_of_term t, size_of_term u) of
14474
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   167
        EQUAL =>
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   168
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   169
            (case hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   170
          end
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   171
      | ord => ord)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   172
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   173
end;
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   174
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   175
fun numtermless tu = (numterm_ord tu = LESS);
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   176
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   177
(*Defined in this file, but perhaps needed only for Int_Numeral_Base_Simprocs of type nat.*)
14474
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   178
val num_ss = HOL_ss settermless numtermless;
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   179
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   180
00292f6f8d13 New simplification ordering to move numerals together. Fixes a bug in the
paulson
parents: 14390
diff changeset
   181
(** Utilities **)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   182
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21243
diff changeset
   183
fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   184
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   185
fun find_first_numeral past (t::terms) =
21873
62d2416728f5 introduced abstract view on number expressions in hologic.ML
haftmann
parents: 21820
diff changeset
   186
        ((snd (HOLogic.dest_number t), rev past @ terms)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   187
         handle TERM _ => find_first_numeral (t::past) terms)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   188
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   189
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   190
val mk_plus = HOLogic.mk_binop "HOL.plus";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   191
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   192
fun mk_minus t = 
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   193
  let val T = Term.fastype_of t
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   194
  in Const ("HOL.uminus", T --> T) $ t
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   195
  end;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   196
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11713
diff changeset
   197
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21243
diff changeset
   198
fun mk_sum T []        = mk_number T 0
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   199
  | mk_sum T [t,u]     = mk_plus (t, u)
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   200
  | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   201
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   202
(*this version ALWAYS includes a trailing zero*)
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21243
diff changeset
   203
fun long_mk_sum T []        = mk_number T 0
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   204
  | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   205
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   206
val dest_plus = HOLogic.dest_bin "HOL.plus" Term.dummyT;
9436
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
(*decompose additions AND subtractions as a sum*)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   209
fun dest_summing (pos, Const ("HOL.plus", _) $ t $ u, ts) =
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   210
        dest_summing (pos, t, dest_summing (pos, u, ts))
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   211
  | dest_summing (pos, Const ("HOL.minus", _) $ t $ u, ts) =
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   212
        dest_summing (pos, t, dest_summing (not pos, u, ts))
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   213
  | dest_summing (pos, t, ts) =
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   214
        if pos then t::ts else mk_minus t :: ts;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   215
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   216
fun dest_sum t = dest_summing (true, t, []);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   217
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   218
val mk_diff = HOLogic.mk_binop "HOL.minus";
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   219
val dest_diff = HOLogic.dest_bin "HOL.minus" Term.dummyT;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   220
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   221
val mk_times = HOLogic.mk_binop "HOL.times";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   222
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   223
fun mk_prod T = 
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21243
diff changeset
   224
  let val one = mk_number T 1
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   225
  fun mk [] = one
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   226
    | mk [t] = t
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   227
    | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   228
  in mk end;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   229
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   230
(*This version ALWAYS includes a trailing one*)
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21243
diff changeset
   231
fun long_mk_prod T []        = mk_number T 1
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   232
  | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   233
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   234
val dest_times = HOLogic.dest_bin "HOL.times" Term.dummyT;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   235
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   236
fun dest_prod t =
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   237
      let val (t,u) = dest_times t
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   238
      in  dest_prod t @ dest_prod u  end
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   239
      handle TERM _ => [t];
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   240
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   241
(*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: 21243
diff changeset
   242
fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   243
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   244
(*Express t as a product of (possibly) a numeral with other sorted terms*)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 19044
diff changeset
   245
fun dest_coeff sign (Const ("HOL.uminus", _) $ t) = dest_coeff (~sign) t
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   246
  | dest_coeff sign t =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   247
    let val ts = sort Term.term_ord (dest_prod t)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   248
        val (n, ts') = find_first_numeral [] ts
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   249
                          handle TERM _ => (1, ts)
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   250
    in (sign*n, mk_prod (Term.fastype_of t) ts') end;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   251
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   252
(*Find first coefficient-term THAT MATCHES u*)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   253
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   254
  | find_first_coeff past u (t::terms) =
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   255
        let val (n,u') = dest_coeff 1 t
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   256
        in  if u aconv u' then (n, rev past @ terms)
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   257
                          else find_first_coeff (t::past) u terms
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   258
        end
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   259
        handle TERM _ => find_first_coeff (t::past) u terms;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   260
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   261
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11713
diff changeset
   262
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1*)
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   263
val add_0s =  thms "add_0s";
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   264
val mult_1s = thms "mult_1s";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   265
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11713
diff changeset
   266
(*To perform binary arithmetic.  The "left" rewriting handles patterns
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   267
  created by the Int_Numeral_Base_Simprocs, such as 3 * (5 * x). *)
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   268
val simps = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym,
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   269
                 add_number_of_left, mult_number_of_left] @
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   270
                arith_simps @ rel_simps;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   271
14113
7b3513ba0f86 Fixing a simproc bug
paulson
parents: 13499
diff changeset
   272
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
7b3513ba0f86 Fixing a simproc bug
paulson
parents: 13499
diff changeset
   273
  during re-arrangement*)
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   274
val non_add_simps =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   275
  subtract Thm.eq_thm [add_number_of_left, number_of_add RS sym] simps;
14113
7b3513ba0f86 Fixing a simproc bug
paulson
parents: 13499
diff changeset
   276
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   277
(*To evaluate binary negations of coefficients*)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14738
diff changeset
   278
val minus_simps = [numeral_m1_eq_minus_1 RS sym, number_of_minus RS sym,
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   279
                   minus_1, minus_0, minus_Pls, minus_Min,
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   280
                   pred_1, pred_0, pred_Pls, pred_Min];
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   281
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   282
(*To let us treat subtraction as addition*)
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   283
val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   284
10713
69c9fc1d11f2 simproc bug fix: negative literals and large terms
paulson
parents: 10693
diff changeset
   285
(*push the unary minus down: - x * y = x * - y *)
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   286
val minus_mult_eq_1_to_2 =
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   287
    [@{thm minus_mult_left} RS sym, @{thm minus_mult_right}] MRS trans |> standard;
10713
69c9fc1d11f2 simproc bug fix: negative literals and large terms
paulson
parents: 10693
diff changeset
   288
69c9fc1d11f2 simproc bug fix: negative literals and large terms
paulson
parents: 10693
diff changeset
   289
(*to extract again any uncancelled minuses*)
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   290
val minus_from_mult_simps =
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   291
    [@{thm minus_minus}, @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym];
10713
69c9fc1d11f2 simproc bug fix: negative literals and large terms
paulson
parents: 10693
diff changeset
   292
69c9fc1d11f2 simproc bug fix: negative literals and large terms
paulson
parents: 10693
diff changeset
   293
(*combine unary minus with numeric literals, however nested within a product*)
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   294
val mult_minus_simps =
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   295
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
10713
69c9fc1d11f2 simproc bug fix: negative literals and large terms
paulson
parents: 10693
diff changeset
   296
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   297
(*Apply the given rewrite (if present) just once*)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15185
diff changeset
   298
fun trans_tac NONE      = all_tac
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15185
diff changeset
   299
  | trans_tac (SOME th) = ALLGOALS (rtac (th RS trans));
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   300
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   301
fun simplify_meta_eq rules =
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   302
  let val ss0 = HOL_basic_ss addeqcongs [eq_cong2] addsimps rules
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   303
  in fn ss => simplify (Simplifier.inherit_context ss ss0) o mk_meta_eq end
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   304
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   305
structure CancelNumeralsCommon =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   306
  struct
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   307
  val mk_sum            = mk_sum
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   308
  val dest_sum          = dest_sum
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   309
  val mk_coeff          = mk_coeff
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   310
  val dest_coeff        = dest_coeff 1
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   311
  val find_first_coeff  = find_first_coeff []
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   312
  val trans_tac         = fn _ => trans_tac
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   313
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   314
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   315
    diff_simps @ minus_simps @ add_ac
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   316
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   317
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ add_ac @ mult_ac
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   318
  fun norm_tac ss =
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   319
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   320
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   321
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   322
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   323
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   324
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   325
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   326
  end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   327
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   328
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   329
structure EqCancelNumerals = CancelNumeralsFun
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   330
 (open CancelNumeralsCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   331
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   332
  val mk_bal   = HOLogic.mk_eq
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   333
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   334
  val bal_add1 = eq_add_iff1 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   335
  val bal_add2 = eq_add_iff2 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   336
);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   337
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   338
structure LessCancelNumerals = CancelNumeralsFun
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   339
 (open CancelNumeralsCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   340
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
19277
f7602e74d948 renamed op < <= to Orderings.less(_eq)
haftmann
parents: 19233
diff changeset
   341
  val mk_bal   = HOLogic.mk_binrel "Orderings.less"
f7602e74d948 renamed op < <= to Orderings.less(_eq)
haftmann
parents: 19233
diff changeset
   342
  val dest_bal = HOLogic.dest_bin "Orderings.less" Term.dummyT
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   343
  val bal_add1 = less_add_iff1 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   344
  val bal_add2 = less_add_iff2 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   345
);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   346
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   347
structure LeCancelNumerals = CancelNumeralsFun
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   348
 (open CancelNumeralsCommon
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   349
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
19277
f7602e74d948 renamed op < <= to Orderings.less(_eq)
haftmann
parents: 19233
diff changeset
   350
  val mk_bal   = HOLogic.mk_binrel "Orderings.less_eq"
f7602e74d948 renamed op < <= to Orderings.less(_eq)
haftmann
parents: 19233
diff changeset
   351
  val dest_bal = HOLogic.dest_bin "Orderings.less_eq" Term.dummyT
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   352
  val bal_add1 = le_add_iff1 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   353
  val bal_add2 = le_add_iff2 RS trans
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   354
);
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   355
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   356
val cancel_numerals =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   357
  map Int_Numeral_Base_Simprocs.prep_simproc
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   358
   [("inteq_cancel_numerals",
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   359
     ["(l::'a::number_ring) + m = n",
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   360
      "(l::'a::number_ring) = m + n",
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   361
      "(l::'a::number_ring) - m = n",
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   362
      "(l::'a::number_ring) = m - n",
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   363
      "(l::'a::number_ring) * m = n",
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   364
      "(l::'a::number_ring) = m * n"],
20045
e66efbafbf1f simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19617
diff changeset
   365
     K EqCancelNumerals.proc),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   366
    ("intless_cancel_numerals",
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   367
     ["(l::'a::{ordered_idom,number_ring}) + m < n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   368
      "(l::'a::{ordered_idom,number_ring}) < m + n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   369
      "(l::'a::{ordered_idom,number_ring}) - m < n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   370
      "(l::'a::{ordered_idom,number_ring}) < m - n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   371
      "(l::'a::{ordered_idom,number_ring}) * m < n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   372
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
20045
e66efbafbf1f simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19617
diff changeset
   373
     K LessCancelNumerals.proc),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   374
    ("intle_cancel_numerals",
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   375
     ["(l::'a::{ordered_idom,number_ring}) + m <= n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   376
      "(l::'a::{ordered_idom,number_ring}) <= m + n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   377
      "(l::'a::{ordered_idom,number_ring}) - m <= n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   378
      "(l::'a::{ordered_idom,number_ring}) <= m - n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   379
      "(l::'a::{ordered_idom,number_ring}) * m <= n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   380
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
20045
e66efbafbf1f simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19617
diff changeset
   381
     K LeCancelNumerals.proc)];
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   382
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   383
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   384
structure CombineNumeralsData =
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   385
  struct
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15921
diff changeset
   386
  val add               = IntInf.+ 
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   387
  val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   388
  val dest_sum          = dest_sum
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   389
  val mk_coeff          = mk_coeff
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   390
  val dest_coeff        = dest_coeff 1
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   391
  val left_distrib      = combine_common_factor RS trans
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   392
  val prove_conv        = Int_Numeral_Base_Simprocs.prove_conv_nohyps
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   393
  val trans_tac         = fn _ => trans_tac
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   394
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   395
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   396
    diff_simps @ minus_simps @ add_ac
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   397
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   398
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ add_ac @ mult_ac
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   399
  fun norm_tac ss =
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   400
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   401
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   402
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   403
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   404
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   405
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 16473
diff changeset
   406
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   407
  end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   408
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   409
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   410
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   411
val combine_numerals =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   412
  Int_Numeral_Base_Simprocs.prep_simproc
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   413
    ("int_combine_numerals", 
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   414
     ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
20045
e66efbafbf1f simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19617
diff changeset
   415
     K CombineNumerals.proc);
9436
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
end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   418
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   419
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   420
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   421
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   422
(*examples:
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   423
print_depth 22;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   424
set timing;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   425
set trace_simp;
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   426
fun test s = (Goal s, by (Simp_tac 1));
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   427
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   428
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   429
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   430
test "2*u = (u::int)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   431
test "(i + j + 12 + (k::int)) - 15 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   432
test "(i + j + 12 + (k::int)) - 5 = y";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   433
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   434
test "y - b < (b::int)";
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   435
test "y - (3*b + c) < (b::int) - 2*c";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   436
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   437
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   438
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   439
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   440
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   441
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   442
test "(i + j + 12 + (k::int)) = u + 15 + y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   443
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   444
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   445
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::int)";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   446
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   447
test "a + -(b+c) + b = (d::int)";
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   448
test "a + -(b+c) - b = (d::int)";
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   449
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   450
(*negative numerals*)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   451
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   452
test "(i + j + -3 + (k::int)) < u + 5 + y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   453
test "(i + j + 3 + (k::int)) < u + -6 + y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   454
test "(i + j + -12 + (k::int)) - 15 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   455
test "(i + j + 12 + (k::int)) - -15 = y";
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   456
test "(i + j + -12 + (k::int)) - -15 = y";
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   457
*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   458
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   459
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   460
(** Constant folding for multiplication in semirings **)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   461
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   462
(*We do not need folding for addition: combine_numerals does the same thing*)
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   463
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   464
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   465
struct
20045
e66efbafbf1f simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19617
diff changeset
   466
  val assoc_ss = HOL_ss addsimps mult_ac
e66efbafbf1f simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19617
diff changeset
   467
  val eq_reflection = eq_reflection
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   468
end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   469
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   470
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   471
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   472
val assoc_fold_simproc =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   473
  Int_Numeral_Base_Simprocs.prep_simproc
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   474
   ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
20045
e66efbafbf1f simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19617
diff changeset
   475
    K Semiring_Times_Assoc.proc);
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   476
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   477
Addsimprocs [assoc_fold_simproc];
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   478
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   479
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
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   482
(*** decision procedure for linear arithmetic ***)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   483
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   484
(*---------------------------------------------------------------------------*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   485
(* Linear arithmetic                                                         *)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   486
(*---------------------------------------------------------------------------*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   487
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   488
(*
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   489
Instantiation of the generic linear arithmetic package for int.
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   490
*)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   491
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   492
(* Update parameters of arithmetic prover *)
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   493
local
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   494
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   495
(* reduce contradictory <= to False *)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   496
val add_rules =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   497
    simp_thms @ arith_simps @ rel_simps @ arith_special @
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   498
    [@{thm neg_le_iff_le}, @{thm numeral_0_eq_0}, @{thm numeral_1_eq_1},
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   499
     @{thm minus_zero}, @{thm diff_minus}, @{thm left_minus}, @{thm right_minus},
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   500
     @{thm mult_zero_left}, @{thm mult_zero_right}, @{thm mult_num1}, @{thm mult_1_right},
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   501
     @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym,
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   502
     @{thm minus_add_distrib}, @{thm minus_minus}, @{thm mult_assoc},
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   503
     of_nat_0, of_nat_1, of_nat_Suc, of_nat_add, of_nat_mult,
20254
58b71535ed00 lin_arith_prover splits certain operators (e.g. min, max, abs)
webertj
parents: 20113
diff changeset
   504
     of_int_0, of_int_1, of_int_add, of_int_mult, int_eq_of_nat]
58b71535ed00 lin_arith_prover splits certain operators (e.g. min, max, abs)
webertj
parents: 20113
diff changeset
   505
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   506
val nat_inj_thms = [zle_int RS iffD2, int_int_eq RS iffD2]
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   507
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   508
val Int_Numeral_Base_Simprocs = assoc_fold_simproc
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   509
  :: Int_Numeral_Simprocs.combine_numerals
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   510
  :: Int_Numeral_Simprocs.cancel_numerals;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   511
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   512
in
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   513
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   514
val int_arith_setup =
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18328
diff changeset
   515
  Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
14368
2763da611ad9 converted Real/Lubs to Isar script. Converting arithmetic setup
paulson
parents: 14331
diff changeset
   516
   {add_mono_thms = add_mono_thms,
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   517
    mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} :: mult_mono_thms,
20254
58b71535ed00 lin_arith_prover splits certain operators (e.g. min, max, abs)
webertj
parents: 20113
diff changeset
   518
    inj_thms = nat_inj_thms @ inj_thms,
14272
5efbb548107d Tidying of the integer development; towards removing the
paulson
parents: 14271
diff changeset
   519
    lessD = lessD @ [zless_imp_add1_zle],
19044
d4bc0ee9383a got rid of superfluous linorder_neqE-instance for int.
nipkow
parents: 18708
diff changeset
   520
    neqE = neqE,
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   521
    simpset = simpset addsimps add_rules
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20254
diff changeset
   522
                      addsimprocs Int_Numeral_Base_Simprocs
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18328
diff changeset
   523
                      addcongs [if_weak_cong]}) #>
21243
afffe1f72143 removed theory NatArith (now part of Nat);
wenzelm
parents: 20713
diff changeset
   524
  arith_inj_const ("Nat.of_nat", HOLogic.natT --> HOLogic.intT) #>
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18328
diff changeset
   525
  arith_inj_const ("IntDef.int", HOLogic.natT --> HOLogic.intT) #>
20254
58b71535ed00 lin_arith_prover splits certain operators (e.g. min, max, abs)
webertj
parents: 20113
diff changeset
   526
  arith_discrete "IntDef.int"
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   527
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   528
end;
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   529
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 12975
diff changeset
   530
val fast_int_arith_simproc =
22548
6ce4bddf3bcb dropped legacy ML bindings
haftmann
parents: 22192
diff changeset
   531
  Simplifier.simproc @{theory}
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   532
  "fast_int_arith" 
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   533
     ["(m::'a::{ordered_idom,number_ring}) < n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   534
      "(m::'a::{ordered_idom,number_ring}) <= n",
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14474
diff changeset
   535
      "(m::'a::{ordered_idom,number_ring}) = n"] Fast_Arith.lin_arith_prover;
9436
62bb04ab4b01 rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
diff changeset
   536
20254
58b71535ed00 lin_arith_prover splits certain operators (e.g. min, max, abs)
webertj
parents: 20113
diff changeset
   537
Addsimprocs [fast_int_arith_simproc];