src/HOL/int_arith1.ML
author haftmann
Tue, 10 Jul 2007 17:30:50 +0200
changeset 23709 fd31da8f752a
parent 23400 a64b39e5809b
child 23881 851c74f1bb69
permissions -rw-r--r--
moved lfp_induct2 here
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/int_arith1.ML
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     3
    Authors:    Larry Paulson and Tobias Nipkow
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     4
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     5
Simprocs and decision procedure for linear arithmetic.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     6
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     7
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     8
(** Misc ML bindings **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     9
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    10
val succ_Pls = thm "succ_Pls";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    11
val succ_Min = thm "succ_Min";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    12
val succ_1 = thm "succ_1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    13
val succ_0 = thm "succ_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    14
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    15
val pred_Pls = thm "pred_Pls";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    16
val pred_Min = thm "pred_Min";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    17
val pred_1 = thm "pred_1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    18
val pred_0 = thm "pred_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    19
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    20
val minus_Pls = thm "minus_Pls";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    21
val minus_Min = thm "minus_Min";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    22
val minus_1 = thm "minus_1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    23
val minus_0 = thm "minus_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    24
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    25
val add_Pls = thm "add_Pls";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    26
val add_Min = thm "add_Min";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    27
val add_BIT_11 = thm "add_BIT_11";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    28
val add_BIT_10 = thm "add_BIT_10";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    29
val add_BIT_0 = thm "add_BIT_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    30
val add_Pls_right = thm "add_Pls_right";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    31
val add_Min_right = thm "add_Min_right";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    32
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    33
val mult_Pls = thm "mult_Pls";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    34
val mult_Min = thm "mult_Min";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    35
val mult_num1 = thm "mult_num1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    36
val mult_num0 = thm "mult_num0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    37
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    38
val neg_def = thm "neg_def";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    39
val iszero_def = thm "iszero_def";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    40
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    41
val number_of_succ = thm "number_of_succ";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    42
val number_of_pred = thm "number_of_pred";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    43
val number_of_minus = thm "number_of_minus";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    44
val number_of_add = thm "number_of_add";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    45
val diff_number_of_eq = thm "diff_number_of_eq";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    46
val number_of_mult = thm "number_of_mult";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    47
val double_number_of_BIT = thm "double_number_of_BIT";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    48
val numeral_0_eq_0 = thm "numeral_0_eq_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    49
val numeral_1_eq_1 = thm "numeral_1_eq_1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    50
val numeral_m1_eq_minus_1 = thm "numeral_m1_eq_minus_1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    51
val mult_minus1 = thm "mult_minus1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    52
val mult_minus1_right = thm "mult_minus1_right";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    53
val minus_number_of_mult = thm "minus_number_of_mult";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    54
val zero_less_nat_eq = thm "zero_less_nat_eq";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    55
val eq_number_of_eq = thm "eq_number_of_eq";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    56
val iszero_number_of_Pls = thm "iszero_number_of_Pls";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    57
val nonzero_number_of_Min = thm "nonzero_number_of_Min";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    58
val iszero_number_of_BIT = thm "iszero_number_of_BIT";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    59
val iszero_number_of_0 = thm "iszero_number_of_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    60
val iszero_number_of_1 = thm "iszero_number_of_1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    61
val less_number_of_eq_neg = thm "less_number_of_eq_neg";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    62
val le_number_of_eq = thm "le_number_of_eq";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    63
val not_neg_number_of_Pls = thm "not_neg_number_of_Pls";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    64
val neg_number_of_Min = thm "neg_number_of_Min";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    65
val neg_number_of_BIT = thm "neg_number_of_BIT";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    66
val le_number_of_eq_not_less = thm "le_number_of_eq_not_less";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    67
val abs_number_of = thm "abs_number_of";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    68
val number_of_reorient = thm "number_of_reorient";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    69
val add_number_of_left = thm "add_number_of_left";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    70
val mult_number_of_left = thm "mult_number_of_left";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    71
val add_number_of_diff1 = thm "add_number_of_diff1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    72
val add_number_of_diff2 = thm "add_number_of_diff2";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    73
val less_iff_diff_less_0 = thm "less_iff_diff_less_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    74
val eq_iff_diff_eq_0 = thm "eq_iff_diff_eq_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    75
val le_iff_diff_le_0 = thm "le_iff_diff_le_0";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    76
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    77
val arith_extra_simps = thms "arith_extra_simps";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    78
val arith_simps = thms "arith_simps";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    79
val rel_simps = thms "rel_simps";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    80
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    81
val zless_imp_add1_zle = thm "zless_imp_add1_zle";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    82
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    83
val combine_common_factor = thm "combine_common_factor";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    84
val eq_add_iff1 = thm "eq_add_iff1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    85
val eq_add_iff2 = thm "eq_add_iff2";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    86
val less_add_iff1 = thm "less_add_iff1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    87
val less_add_iff2 = thm "less_add_iff2";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    88
val le_add_iff1 = thm "le_add_iff1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    89
val le_add_iff2 = thm "le_add_iff2";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    90
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    91
val arith_special = thms "arith_special";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    92
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    93
structure Int_Numeral_Base_Simprocs =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    94
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    95
  fun prove_conv tacs ctxt (_: thm list) (t, u) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    96
    if t aconv u then NONE
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    97
    else
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    98
      let val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    99
      in SOME (Goal.prove ctxt [] [] eq (K (EVERY tacs))) end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   100
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   101
  fun prove_conv_nohyps tacs sg = prove_conv tacs sg [];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   102
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   103
  fun prep_simproc (name, pats, proc) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   104
    Simplifier.simproc (the_context()) name pats proc;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   105
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   106
  fun is_numeral (Const(@{const_name Numeral.number_of}, _) $ w) = true
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   107
    | is_numeral _ = false
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   108
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   109
  fun simplify_meta_eq f_number_of_eq f_eq =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   110
      mk_meta_eq ([f_eq, f_number_of_eq] MRS trans)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   111
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   112
  (*reorientation simprules using ==, for the following simproc*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   113
  val meta_zero_reorient = zero_reorient RS eq_reflection
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   114
  val meta_one_reorient = one_reorient RS eq_reflection
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   115
  val meta_number_of_reorient = number_of_reorient RS eq_reflection
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   116
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   117
  (*reorientation simplification procedure: reorients (polymorphic) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   118
    0 = x, 1 = x, nnn = x provided x isn't 0, 1 or a numeral.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   119
  fun reorient_proc sg _ (_ $ t $ u) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   120
    case u of
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   121
	Const(@{const_name HOL.zero}, _) => NONE
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   122
      | Const(@{const_name HOL.one}, _) => NONE
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   123
      | Const(@{const_name Numeral.number_of}, _) $ _ => NONE
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   124
      | _ => SOME (case t of
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   125
		  Const(@{const_name HOL.zero}, _) => meta_zero_reorient
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   126
		| Const(@{const_name HOL.one}, _) => meta_one_reorient
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   127
		| Const(@{const_name Numeral.number_of}, _) $ _ => meta_number_of_reorient)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   128
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   129
  val reorient_simproc = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   130
      prep_simproc ("reorient_simproc", ["0=x", "1=x", "number_of w = x"], reorient_proc)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   131
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   132
  end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   133
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   134
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   135
Addsimprocs [Int_Numeral_Base_Simprocs.reorient_simproc];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   136
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   137
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   138
structure Int_Numeral_Simprocs =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   139
struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   140
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   141
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic in Int_Numeral_Base_Simprocs
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   142
  isn't complicated by the abstract 0 and 1.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   143
val numeral_syms = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   144
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   145
(** New term ordering so that AC-rewriting brings numerals to the front **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   146
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   147
(*Order integers by absolute value and then by sign. The standard integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   148
  ordering is not well-founded.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   149
fun num_ord (i,j) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   150
      (case IntInf.compare (IntInf.abs i, IntInf.abs j) of
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   151
            EQUAL => int_ord (IntInf.sign i, IntInf.sign j) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   152
          | ord => ord);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   153
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   154
(*This resembles Term.term_ord, but it puts binary numerals before other
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   155
  non-atomic terms.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   156
local open Term 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   157
in 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   158
fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   159
      (case numterm_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   160
  | numterm_ord
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   161
     (Const(@{const_name Numeral.number_of}, _) $ v, Const(@{const_name Numeral.number_of}, _) $ w) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   162
     num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   163
  | numterm_ord (Const(@{const_name Numeral.number_of}, _) $ _, _) = LESS
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   164
  | numterm_ord (_, Const(@{const_name Numeral.number_of}, _) $ _) = GREATER
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   165
  | numterm_ord (t, u) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   166
      (case int_ord (size_of_term t, size_of_term u) of
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   167
        EQUAL =>
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   168
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   169
            (case hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   170
          end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   171
      | ord => ord)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   172
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   173
end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   174
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   175
fun numtermless tu = (numterm_ord tu = LESS);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   176
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   177
(*Defined in this file, but perhaps needed only for Int_Numeral_Base_Simprocs of type nat.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   178
val num_ss = HOL_ss settermless numtermless;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   179
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   180
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   181
(** Utilities **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   182
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   183
fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   184
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   185
fun find_first_numeral past (t::terms) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   186
        ((snd (HOLogic.dest_number t), rev past @ terms)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   187
         handle TERM _ => find_first_numeral (t::past) terms)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   188
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   189
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   190
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   191
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   192
fun mk_minus t = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   193
  let val T = Term.fastype_of t
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   194
  in Const (@{const_name HOL.uminus}, T --> T) $ t end;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   195
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   196
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   197
fun mk_sum T []        = mk_number T 0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   198
  | mk_sum T [t,u]     = mk_plus (t, u)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   199
  | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   200
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   201
(*this version ALWAYS includes a trailing zero*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   202
fun long_mk_sum T []        = mk_number T 0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   203
  | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   204
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   205
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} Term.dummyT;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   206
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   207
(*decompose additions AND subtractions as a sum*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   208
fun dest_summing (pos, Const (@{const_name HOL.plus}, _) $ t $ u, ts) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   209
        dest_summing (pos, t, dest_summing (pos, u, ts))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   210
  | dest_summing (pos, Const (@{const_name HOL.minus}, _) $ t $ u, ts) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   211
        dest_summing (pos, t, dest_summing (not pos, u, ts))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   212
  | dest_summing (pos, t, ts) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   213
        if pos then t::ts else mk_minus t :: ts;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   214
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   215
fun dest_sum t = dest_summing (true, t, []);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   216
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   217
val mk_diff = HOLogic.mk_binop @{const_name HOL.minus};
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   218
val dest_diff = HOLogic.dest_bin @{const_name HOL.minus} Term.dummyT;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   219
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   220
val mk_times = HOLogic.mk_binop @{const_name HOL.times};
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   221
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   222
fun one_of T = Const(@{const_name HOL.one},T);
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   223
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   224
(* build product with trailing 1 rather than Numeral 1 in order to avoid the
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   225
   unnecessary restriction to type class number_ring
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   226
   which is not required for cancellation of common factors in divisions.
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   227
*)
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   228
fun mk_prod T = 
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   229
  let val one = one_of T
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   230
  fun mk [] = one
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   231
    | mk [t] = t
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   232
    | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   233
  in mk end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   234
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   235
(*This version ALWAYS includes a trailing one*)
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   236
fun long_mk_prod T []        = one_of T
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   237
  | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   238
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   239
val dest_times = HOLogic.dest_bin @{const_name HOL.times} Term.dummyT;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   240
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   241
fun dest_prod t =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   242
      let val (t,u) = dest_times t
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   243
      in dest_prod t @ dest_prod u end
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   244
      handle TERM _ => [t];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   245
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   246
(*DON'T do the obvious simplifications; that would create special cases*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   247
fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   248
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   249
(*Express t as a product of (possibly) a numeral with other sorted terms*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   250
fun dest_coeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_coeff (~sign) t
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   251
  | dest_coeff sign t =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   252
    let val ts = sort Term.term_ord (dest_prod t)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   253
        val (n, ts') = find_first_numeral [] ts
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   254
                          handle TERM _ => (1, ts)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   255
    in (sign*n, mk_prod (Term.fastype_of t) ts') end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   256
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   257
(*Find first coefficient-term THAT MATCHES u*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   258
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   259
  | find_first_coeff past u (t::terms) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   260
        let val (n,u') = dest_coeff 1 t
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   261
        in if u aconv u' then (n, rev past @ terms)
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   262
                         else find_first_coeff (t::past) u terms
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   263
        end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   264
        handle TERM _ => find_first_coeff (t::past) u terms;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   265
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   266
(*Fractions as pairs of ints. Can't use Rat.rat because the representation
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   267
  needs to preserve negative values in the denominator.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   268
fun mk_frac (p, q : IntInf.int) = if q = 0 then raise Div else (p, q);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   269
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   270
(*Don't reduce fractions; sums must be proved by rule add_frac_eq.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   271
  Fractions are reduced later by the cancel_numeral_factor simproc.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   272
fun add_frac ((p1 : IntInf.int, q1 : IntInf.int), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   273
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   274
val mk_divide = HOLogic.mk_binop @{const_name HOL.divide};
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   275
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   276
(*Build term (p / q) * t*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   277
fun mk_fcoeff ((p, q), t) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   278
  let val T = Term.fastype_of t
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   279
  in mk_times (mk_divide (mk_number T p, mk_number T q), t) end;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   280
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   281
(*Express t as a product of a fraction with other sorted terms*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   282
fun dest_fcoeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_fcoeff (~sign) t
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   283
  | dest_fcoeff sign (Const (@{const_name HOL.divide}, _) $ t $ u) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   284
    let val (p, t') = dest_coeff sign t
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   285
        val (q, u') = dest_coeff 1 u
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   286
    in (mk_frac (p, q), mk_divide (t', u')) end
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   287
  | dest_fcoeff sign t =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   288
    let val (p, t') = dest_coeff sign t
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   289
        val T = Term.fastype_of t
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   290
    in (mk_frac (p, 1), mk_divide (t', one_of T)) end;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   291
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   292
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   293
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   294
val add_0s =  thms "add_0s";
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23365
diff changeset
   295
val mult_1s = thms "mult_1s" @ [thm"mult_1_left", thm"mult_1_right", thm"divide_1"];
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   296
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   297
(*Simplify inverse Numeral1, a/Numeral1*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   298
val inverse_1s = [@{thm inverse_numeral_1}];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   299
val divide_1s = [@{thm divide_numeral_1}];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   300
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   301
(*To perform binary arithmetic.  The "left" rewriting handles patterns
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   302
  created by the Int_Numeral_Base_Simprocs, such as 3 * (5 * x). *)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   303
val simps = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   304
                 add_number_of_left, mult_number_of_left] @
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   305
                arith_simps @ rel_simps;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   306
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   307
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   308
  during re-arrangement*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   309
val non_add_simps =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   310
  subtract Thm.eq_thm [add_number_of_left, number_of_add RS sym] simps;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   311
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   312
(*To evaluate binary negations of coefficients*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   313
val minus_simps = [numeral_m1_eq_minus_1 RS sym, number_of_minus RS sym,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   314
                   minus_1, minus_0, minus_Pls, minus_Min,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   315
                   pred_1, pred_0, pred_Pls, pred_Min];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   316
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   317
(*To let us treat subtraction as addition*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   318
val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   319
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   320
(*To let us treat division as multiplication*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   321
val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   322
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   323
(*push the unary minus down: - x * y = x * - y *)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   324
val minus_mult_eq_1_to_2 =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   325
    [@{thm minus_mult_left} RS sym, @{thm minus_mult_right}] MRS trans |> standard;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   326
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   327
(*to extract again any uncancelled minuses*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   328
val minus_from_mult_simps =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   329
    [@{thm minus_minus}, @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   330
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   331
(*combine unary minus with numeric literals, however nested within a product*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   332
val mult_minus_simps =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   333
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   334
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   335
(*Apply the given rewrite (if present) just once*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   336
fun trans_tac NONE      = all_tac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   337
  | trans_tac (SOME th) = ALLGOALS (rtac (th RS trans));
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   338
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   339
fun simplify_meta_eq rules =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   340
  let val ss0 = HOL_basic_ss addeqcongs [eq_cong2] addsimps rules
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   341
  in fn ss => simplify (Simplifier.inherit_context ss ss0) o mk_meta_eq end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   342
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   343
structure CancelNumeralsCommon =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   344
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   345
  val mk_sum            = mk_sum
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   346
  val dest_sum          = dest_sum
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   347
  val mk_coeff          = mk_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   348
  val dest_coeff        = dest_coeff 1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   349
  val find_first_coeff  = find_first_coeff []
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   350
  val trans_tac         = fn _ => trans_tac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   351
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   352
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   353
    diff_simps @ minus_simps @ add_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   354
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   355
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ add_ac @ mult_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   356
  fun norm_tac ss =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   357
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   358
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   359
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   360
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   361
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   362
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   363
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   364
  end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   365
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   366
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   367
structure EqCancelNumerals = CancelNumeralsFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   368
 (open CancelNumeralsCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   369
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   370
  val mk_bal   = HOLogic.mk_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   371
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   372
  val bal_add1 = eq_add_iff1 RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   373
  val bal_add2 = eq_add_iff2 RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   374
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   375
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   376
structure LessCancelNumerals = CancelNumeralsFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   377
 (open CancelNumeralsCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   378
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   379
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   380
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   381
  val bal_add1 = less_add_iff1 RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   382
  val bal_add2 = less_add_iff2 RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   383
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   384
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   385
structure LeCancelNumerals = CancelNumeralsFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   386
 (open CancelNumeralsCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   387
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   388
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   389
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   390
  val bal_add1 = le_add_iff1 RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   391
  val bal_add2 = le_add_iff2 RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   392
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   393
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   394
val cancel_numerals =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   395
  map Int_Numeral_Base_Simprocs.prep_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   396
   [("inteq_cancel_numerals",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   397
     ["(l::'a::number_ring) + m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   398
      "(l::'a::number_ring) = m + n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   399
      "(l::'a::number_ring) - m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   400
      "(l::'a::number_ring) = m - n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   401
      "(l::'a::number_ring) * m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   402
      "(l::'a::number_ring) = m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   403
     K EqCancelNumerals.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   404
    ("intless_cancel_numerals",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   405
     ["(l::'a::{ordered_idom,number_ring}) + m < n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   406
      "(l::'a::{ordered_idom,number_ring}) < m + n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   407
      "(l::'a::{ordered_idom,number_ring}) - m < n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   408
      "(l::'a::{ordered_idom,number_ring}) < m - n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   409
      "(l::'a::{ordered_idom,number_ring}) * m < n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   410
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   411
     K LessCancelNumerals.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   412
    ("intle_cancel_numerals",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   413
     ["(l::'a::{ordered_idom,number_ring}) + m <= n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   414
      "(l::'a::{ordered_idom,number_ring}) <= m + n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   415
      "(l::'a::{ordered_idom,number_ring}) - m <= n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   416
      "(l::'a::{ordered_idom,number_ring}) <= m - n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   417
      "(l::'a::{ordered_idom,number_ring}) * m <= n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   418
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   419
     K LeCancelNumerals.proc)];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   420
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   421
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   422
structure CombineNumeralsData =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   423
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   424
  type coeff            = IntInf.int
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   425
  val iszero            = (fn x : IntInf.int => x = 0)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   426
  val add               = IntInf.+
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   427
  val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   428
  val dest_sum          = dest_sum
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   429
  val mk_coeff          = mk_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   430
  val dest_coeff        = dest_coeff 1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   431
  val left_distrib      = combine_common_factor RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   432
  val prove_conv        = Int_Numeral_Base_Simprocs.prove_conv_nohyps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   433
  val trans_tac         = fn _ => trans_tac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   434
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   435
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   436
    diff_simps @ minus_simps @ add_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   437
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   438
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ add_ac @ mult_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   439
  fun norm_tac ss =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   440
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   441
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   442
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   443
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   444
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   445
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   446
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   447
  end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   448
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   449
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   450
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   451
(*Version for fields, where coefficients can be fractions*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   452
structure FieldCombineNumeralsData =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   453
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   454
  type coeff            = IntInf.int * IntInf.int
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   455
  val iszero            = (fn (p : IntInf.int, q) => p = 0)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   456
  val add               = add_frac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   457
  val mk_sum            = long_mk_sum
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   458
  val dest_sum          = dest_sum
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   459
  val mk_coeff          = mk_fcoeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   460
  val dest_coeff        = dest_fcoeff 1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   461
  val left_distrib      = combine_common_factor RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   462
  val prove_conv        = Int_Numeral_Base_Simprocs.prove_conv_nohyps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   463
  val trans_tac         = fn _ => trans_tac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   464
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   465
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   466
    inverse_1s @ divide_simps @ diff_simps @ minus_simps @ add_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   467
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   468
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ add_ac @ mult_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   469
  fun norm_tac ss =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   470
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   471
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   472
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   473
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   474
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   475
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   476
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s @ divide_1s)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   477
  end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   478
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   479
structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   480
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   481
val combine_numerals =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   482
  Int_Numeral_Base_Simprocs.prep_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   483
    ("int_combine_numerals", 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   484
     ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   485
     K CombineNumerals.proc);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   486
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   487
val field_combine_numerals =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   488
  Int_Numeral_Base_Simprocs.prep_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   489
    ("field_combine_numerals", 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   490
     ["(i::'a::{number_ring,field,division_by_zero}) + j",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   491
      "(i::'a::{number_ring,field,division_by_zero}) - j"], 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   492
     K FieldCombineNumerals.proc);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   493
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   494
end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   495
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   496
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   497
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   498
Addsimprocs [Int_Numeral_Simprocs.field_combine_numerals];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   499
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   500
(*examples:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   501
print_depth 22;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   502
set timing;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   503
set trace_simp;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   504
fun test s = (Goal s, by (Simp_tac 1));
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   505
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   506
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   507
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   508
test "2*u = (u::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   509
test "(i + j + 12 + (k::int)) - 15 = y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   510
test "(i + j + 12 + (k::int)) - 5 = y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   511
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   512
test "y - b < (b::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   513
test "y - (3*b + c) < (b::int) - 2*c";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   514
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   515
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   516
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   517
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   518
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   519
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   520
test "(i + j + 12 + (k::int)) = u + 15 + y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   521
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   522
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   523
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)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   524
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   525
test "a + -(b+c) + b = (d::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   526
test "a + -(b+c) - b = (d::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   527
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   528
(*negative numerals*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   529
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   530
test "(i + j + -3 + (k::int)) < u + 5 + y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   531
test "(i + j + 3 + (k::int)) < u + -6 + y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   532
test "(i + j + -12 + (k::int)) - 15 = y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   533
test "(i + j + 12 + (k::int)) - -15 = y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   534
test "(i + j + -12 + (k::int)) - -15 = y";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   535
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   536
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   537
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   538
(** Constant folding for multiplication in semirings **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   539
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   540
(*We do not need folding for addition: combine_numerals does the same thing*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   541
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   542
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   543
struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   544
  val assoc_ss = HOL_ss addsimps mult_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   545
  val eq_reflection = eq_reflection
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   546
end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   547
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   548
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   549
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   550
val assoc_fold_simproc =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   551
  Int_Numeral_Base_Simprocs.prep_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   552
   ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   553
    K Semiring_Times_Assoc.proc);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   554
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   555
Addsimprocs [assoc_fold_simproc];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   556
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   557
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   558
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   559
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   560
(*** decision procedure for linear arithmetic ***)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   561
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   562
(*---------------------------------------------------------------------------*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   563
(* Linear arithmetic                                                         *)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   564
(*---------------------------------------------------------------------------*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   565
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   566
(*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   567
Instantiation of the generic linear arithmetic package for int.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   568
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   569
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   570
(* Update parameters of arithmetic prover *)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   571
local
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   572
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   573
(* reduce contradictory <= to False *)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   574
val add_rules =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   575
    simp_thms @ arith_simps @ rel_simps @ arith_special @
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   576
    [@{thm neg_le_iff_le}, @{thm numeral_0_eq_0}, @{thm numeral_1_eq_1},
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   577
     @{thm minus_zero}, @{thm diff_minus}, @{thm left_minus}, @{thm right_minus},
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   578
     @{thm mult_zero_left}, @{thm mult_zero_right}, @{thm mult_num1}, @{thm mult_1_right},
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   579
     @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   580
     @{thm minus_add_distrib}, @{thm minus_minus}, @{thm mult_assoc},
23365
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23164
diff changeset
   581
     @{thm of_nat_0}, @{thm of_nat_1}, @{thm of_nat_Suc}, @{thm of_nat_add},
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23164
diff changeset
   582
     @{thm of_nat_mult}, @{thm of_int_0}, @{thm of_int_1}, @{thm of_int_add},
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23164
diff changeset
   583
     @{thm of_int_mult}]
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   584
23365
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23164
diff changeset
   585
val nat_inj_thms = [@{thm zle_int} RS iffD2, @{thm int_int_eq} RS iffD2]
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   586
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   587
val Int_Numeral_Base_Simprocs = assoc_fold_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   588
  :: Int_Numeral_Simprocs.combine_numerals
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   589
  :: Int_Numeral_Simprocs.cancel_numerals;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   590
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   591
in
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   592
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   593
val int_arith_setup =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   594
  Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   595
   {add_mono_thms = add_mono_thms,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   596
    mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} :: mult_mono_thms,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   597
    inj_thms = nat_inj_thms @ inj_thms,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   598
    lessD = lessD @ [zless_imp_add1_zle],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   599
    neqE = neqE,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   600
    simpset = simpset addsimps add_rules
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   601
                      addsimprocs Int_Numeral_Base_Simprocs
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   602
                      addcongs [if_weak_cong]}) #>
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   603
  arith_inj_const ("Nat.of_nat", HOLogic.natT --> HOLogic.intT) #>
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   604
  arith_discrete "IntDef.int"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   605
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   606
end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   607
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   608
val fast_int_arith_simproc =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   609
  Simplifier.simproc @{theory}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   610
  "fast_int_arith" 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   611
     ["(m::'a::{ordered_idom,number_ring}) < n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   612
      "(m::'a::{ordered_idom,number_ring}) <= n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   613
      "(m::'a::{ordered_idom,number_ring}) = n"] Fast_Arith.lin_arith_prover;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   614
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   615
Addsimprocs [fast_int_arith_simproc];