src/HOL/int_factor_simprocs.ML
author wenzelm
Thu, 31 May 2007 18:16:52 +0200
changeset 23164 69e55066dbca
child 23398 0b5a400c7595
permissions -rw-r--r--
moved Integ files to canonical place;
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_factor_simprocs.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
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     4
    Copyright   2000  University of Cambridge
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     5
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     6
Factor cancellation simprocs for the integers (and for fields).
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     7
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     8
This file can't be combined with int_arith1 because it requires IntDiv.thy.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     9
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    10
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    11
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    12
(*To quote from Provers/Arith/cancel_numeral_factor.ML:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    13
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    14
Cancels common coefficients in balanced expressions:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    15
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    16
     u*#m ~~ u'*#m'  ==  #n*u ~~ #n'*u'
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    17
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    18
where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    19
and d = gcd(m,m') and n=m/d and n'=m'/d.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    20
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    21
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    22
val rel_number_of = [eq_number_of_eq, less_number_of_eq_neg, le_number_of_eq];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    23
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    24
(** Factor cancellation theorems for integer division (div, not /) **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    25
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    26
Goal "!!k::int. k~=0 ==> (k*m) div (k*n) = (m div n)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    27
by (stac @{thm zdiv_zmult_zmult1} 1);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    28
by Auto_tac;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    29
qed "int_mult_div_cancel1";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    30
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    31
(*For ExtractCommonTermFun, cancelling common factors*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    32
Goal "(k*m) div (k*n) = (if k = (0::int) then 0 else m div n)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    33
by (simp_tac (simpset() addsimps [int_mult_div_cancel1]) 1);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    34
qed "int_mult_div_cancel_disj";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    35
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    36
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    37
local
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    38
  open Int_Numeral_Simprocs
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    39
in
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    40
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    41
structure CancelNumeralFactorCommon =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    42
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    43
  val mk_coeff          = mk_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    44
  val dest_coeff        = dest_coeff 1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    45
  val trans_tac         = fn _ => trans_tac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    46
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    47
  val norm_ss1 = HOL_ss addsimps minus_from_mult_simps @ mult_1s
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    48
  val norm_ss2 = HOL_ss addsimps simps @ mult_minus_simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    49
  val norm_ss3 = HOL_ss addsimps mult_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    50
  fun norm_tac ss =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    51
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    52
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    53
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    54
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    55
  val numeral_simp_ss = HOL_ss addsimps rel_number_of @ simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    56
  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
    57
  val simplify_meta_eq = Int_Numeral_Simprocs.simplify_meta_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    58
    [@{thm add_0}, @{thm add_0_right}, @{thm mult_zero_left},
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    59
      @{thm mult_zero_right}, @{thm mult_num1}, @{thm mult_1_right}];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    60
  end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    61
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    62
(*Version for integer division*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    63
structure IntDivCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    64
 (open CancelNumeralFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    65
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    66
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    67
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.intT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    68
  val cancel = int_mult_div_cancel1 RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    69
  val neg_exchanges = false
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    70
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    71
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    72
(*Version for fields*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    73
structure DivideCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    74
 (open CancelNumeralFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    75
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    76
  val mk_bal   = HOLogic.mk_binop @{const_name HOL.divide}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    77
  val dest_bal = HOLogic.dest_bin @{const_name HOL.divide} Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    78
  val cancel = @{thm mult_divide_cancel_left} RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    79
  val neg_exchanges = false
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    80
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    81
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    82
structure EqCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    83
 (open CancelNumeralFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    84
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    85
  val mk_bal   = HOLogic.mk_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    86
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    87
  val cancel = @{thm mult_cancel_left} RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    88
  val neg_exchanges = false
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    89
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    90
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    91
structure LessCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    92
 (open CancelNumeralFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    93
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    94
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    95
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    96
  val cancel = @{thm mult_less_cancel_left} RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    97
  val neg_exchanges = true
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    98
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    99
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   100
structure LeCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   101
 (open CancelNumeralFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   102
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   103
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   104
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   105
  val cancel = @{thm mult_le_cancel_left} RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   106
  val neg_exchanges = true
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   107
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   108
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   109
val cancel_numeral_factors =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   110
  map Int_Numeral_Base_Simprocs.prep_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   111
   [("ring_eq_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   112
     ["(l::'a::{idom,number_ring}) * m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   113
      "(l::'a::{idom,number_ring}) = m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   114
     K EqCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   115
    ("ring_less_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   116
     ["(l::'a::{ordered_idom,number_ring}) * m < n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   117
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   118
     K LessCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   119
    ("ring_le_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   120
     ["(l::'a::{ordered_idom,number_ring}) * m <= n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   121
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   122
     K LeCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   123
    ("int_div_cancel_numeral_factors",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   124
     ["((l::int) * m) div n", "(l::int) div (m * n)"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   125
     K IntDivCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   126
    ("divide_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   127
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   128
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   129
      "((number_of v)::'a::{division_by_zero,field,number_ring}) / (number_of w)"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   130
     K DivideCancelNumeralFactor.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
(* referenced by rat_arith.ML *)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   133
val field_cancel_numeral_factors =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   134
  map Int_Numeral_Base_Simprocs.prep_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   135
   [("field_eq_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   136
     ["(l::'a::{field,number_ring}) * m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   137
      "(l::'a::{field,number_ring}) = m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   138
     K EqCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   139
    ("field_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   140
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   141
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   142
      "((number_of v)::'a::{division_by_zero,field,number_ring}) / (number_of w)"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   143
     K DivideCancelNumeralFactor.proc)]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   144
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   145
end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   146
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   147
Addsimprocs cancel_numeral_factors;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   148
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   149
(*examples:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   150
print_depth 22;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   151
set timing;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   152
set trace_simp;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   153
fun test s = (Goal s; by (Simp_tac 1));
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   154
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   155
test "9*x = 12 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   156
test "(9*x) div (12 * (y::int)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   157
test "9*x < 12 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   158
test "9*x <= 12 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   159
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   160
test "-99*x = 132 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   161
test "(-99*x) div (132 * (y::int)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   162
test "-99*x < 132 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   163
test "-99*x <= 132 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   165
test "999*x = -396 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   166
test "(999*x) div (-396 * (y::int)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   167
test "999*x < -396 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   168
test "999*x <= -396 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   169
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   170
test "-99*x = -81 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   171
test "(-99*x) div (-81 * (y::int)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   172
test "-99*x <= -81 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   173
test "-99*x < -81 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   174
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   175
test "-2 * x = -1 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   176
test "-2 * x = -(y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   177
test "(-2 * x) div (-1 * (y::int)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   178
test "-2 * x < -(y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   179
test "-2 * x <= -1 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   180
test "-x < -23 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   181
test "-x <= -23 * (y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   182
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   183
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   184
(*And the same examples for fields such as rat or real:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   185
test "0 <= (y::rat) * -2";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   186
test "9*x = 12 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   187
test "(9*x) / (12 * (y::rat)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   188
test "9*x < 12 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   189
test "9*x <= 12 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   190
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   191
test "-99*x = 132 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   192
test "(-99*x) / (132 * (y::rat)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   193
test "-99*x < 132 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   194
test "-99*x <= 132 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   195
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   196
test "999*x = -396 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   197
test "(999*x) / (-396 * (y::rat)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   198
test "999*x < -396 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   199
test "999*x <= -396 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   200
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   201
test  "(- ((2::rat) * x) <= 2 * y)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   202
test "-99*x = -81 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   203
test "(-99*x) / (-81 * (y::rat)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   204
test "-99*x <= -81 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   205
test "-99*x < -81 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   206
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   207
test "-2 * x = -1 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   208
test "-2 * x = -(y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   209
test "(-2 * x) / (-1 * (y::rat)) = z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   210
test "-2 * x < -(y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   211
test "-2 * x <= -1 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   212
test "-x < -23 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   213
test "-x <= -23 * (y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   214
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   215
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   216
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   217
(** Declarations for ExtractCommonTerm **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   218
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   219
local
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   220
  open Int_Numeral_Simprocs
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   221
in
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   222
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   223
(*Find first term that matches u*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   224
fun find_first_t past u []         = raise TERM ("find_first_t", [])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   225
  | find_first_t past u (t::terms) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   226
        if u aconv t then (rev past @ terms)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   227
        else find_first_t (t::past) u terms
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   228
        handle TERM _ => find_first_t (t::past) u terms;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   229
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   230
(** Final simplification for the CancelFactor simprocs **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   231
val simplify_one = Int_Numeral_Simprocs.simplify_meta_eq  
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   232
  [@{thm mult_1_left}, mult_1_right, @{thm zdiv_1}, numeral_1_eq_1];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   233
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   234
fun cancel_simplify_meta_eq cancel_th ss th =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   235
    simplify_one ss (([th, cancel_th]) MRS trans);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   236
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   237
(*At present, long_mk_prod creates Numeral1, so this requires the axclass
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   238
  number_ring*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   239
structure CancelFactorCommon =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   240
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   241
  val mk_sum            = long_mk_prod
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   242
  val dest_sum          = dest_prod
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   243
  val mk_coeff          = mk_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   244
  val dest_coeff        = dest_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   245
  val find_first        = find_first_t []
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   246
  val trans_tac         = fn _ => trans_tac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   247
  val norm_ss = HOL_ss addsimps mult_1s @ mult_ac
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   248
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   249
  end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   250
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   251
(*mult_cancel_left requires a ring with no zero divisors.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   252
structure EqCancelFactor = ExtractCommonTermFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   253
 (open CancelFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   254
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   255
  val mk_bal   = HOLogic.mk_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   256
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   257
  val simplify_meta_eq  = cancel_simplify_meta_eq @{thm mult_cancel_left}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   258
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   259
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   260
(*int_mult_div_cancel_disj is for integer division (div).*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   261
structure IntDivCancelFactor = ExtractCommonTermFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   262
 (open CancelFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   263
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   264
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   265
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.intT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   266
  val simplify_meta_eq  = cancel_simplify_meta_eq int_mult_div_cancel_disj
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   267
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   268
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   269
(*Version for all fields, including unordered ones (type complex).*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   270
structure DivideCancelFactor = ExtractCommonTermFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   271
 (open CancelFactorCommon
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   272
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   273
  val mk_bal   = HOLogic.mk_binop @{const_name HOL.divide}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   274
  val dest_bal = HOLogic.dest_bin @{const_name HOL.divide} Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   275
  val simplify_meta_eq  = cancel_simplify_meta_eq @{thm mult_divide_cancel_eq_if}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   276
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   277
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   278
(*The number_ring class is necessary because the simprocs refer to the
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   279
  binary number 1.  FIXME: probably they could use 1 instead.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   280
val cancel_factors =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   281
  map Int_Numeral_Base_Simprocs.prep_simproc
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   282
   [("ring_eq_cancel_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   283
     ["(l::'a::{idom,number_ring}) * m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   284
      "(l::'a::{idom,number_ring}) = m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   285
    K EqCancelFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   286
    ("int_div_cancel_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   287
     ["((l::int) * m) div n", "(l::int) div (m * n)"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   288
     K IntDivCancelFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   289
    ("divide_cancel_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   290
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   291
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   292
     K DivideCancelFactor.proc)];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   293
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   294
end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   295
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   296
Addsimprocs cancel_factors;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   297
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   298
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   299
(*examples:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   300
print_depth 22;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   301
set timing;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   302
set trace_simp;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   303
fun test s = (Goal s; by (Asm_simp_tac 1));
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   304
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   305
test "x*k = k*(y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   306
test "k = k*(y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   307
test "a*(b*c) = (b::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   308
test "a*(b*c) = d*(b::int)*(x*a)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   309
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   310
test "(x*k) div (k*(y::int)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   311
test "(k) div (k*(y::int)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   312
test "(a*(b*c)) div ((b::int)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   313
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   314
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   315
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   316
(*And the same examples for fields such as rat or real:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   317
print_depth 22;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   318
set timing;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   319
set trace_simp;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   320
fun test s = (Goal s; by (Asm_simp_tac 1));
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   321
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   322
test "x*k = k*(y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   323
test "k = k*(y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   324
test "a*(b*c) = (b::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   325
test "a*(b*c) = d*(b::rat)*(x*a)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   326
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   327
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   328
test "(x*k) / (k*(y::rat)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   329
test "(k) / (k*(y::rat)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   330
test "(a*(b*c)) / ((b::rat)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   331
test "(a*(b*c)) / (d*(b::rat)*(x*a)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   332
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   333
(*FIXME: what do we do about this?*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   334
test "a*(b*c)/(y*z) = d*(b::rat)*(x*a)/z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   335
*)