src/ZF/arith_data.ML
author wenzelm
Tue Jul 31 19:40:22 2007 +0200 (2007-07-31)
changeset 24091 109f19a13872
parent 20342 4392003fcbfa
child 24630 351a308ab58d
permissions -rw-r--r--
added Tools/lin_arith.ML;
paulson@9548
     1
(*  Title:      ZF/arith_data.ML
paulson@9548
     2
    ID:         $Id$
paulson@9548
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@9548
     4
    Copyright   2000  University of Cambridge
paulson@9548
     5
paulson@9548
     6
Arithmetic simplification: cancellation of common terms
paulson@9548
     7
*)
paulson@9548
     8
paulson@9548
     9
signature ARITH_DATA =
paulson@9548
    10
sig
paulson@9570
    11
  (*the main outcome*)
paulson@9548
    12
  val nat_cancel: simproc list
paulson@9570
    13
  (*tools for use in similar applications*)
paulson@9570
    14
  val gen_trans_tac: thm -> thm option -> tactic
wenzelm@20113
    15
  val prove_conv: string -> tactic list -> Proof.context -> thm list -> term * term -> thm option
wenzelm@16973
    16
  val simplify_meta_eq: thm list -> simpset -> thm -> thm
paulson@9874
    17
  (*debugging*)
paulson@9874
    18
  structure EqCancelNumeralsData   : CANCEL_NUMERALS_DATA
paulson@9874
    19
  structure LessCancelNumeralsData : CANCEL_NUMERALS_DATA
paulson@9874
    20
  structure DiffCancelNumeralsData : CANCEL_NUMERALS_DATA
paulson@9548
    21
end;
paulson@9548
    22
paulson@9570
    23
paulson@9548
    24
structure ArithData: ARITH_DATA =
paulson@9548
    25
struct
paulson@9548
    26
paulson@9548
    27
val iT = Ind_Syntax.iT;
paulson@9548
    28
paulson@9548
    29
val zero = Const("0", iT);
paulson@9548
    30
val succ = Const("succ", iT --> iT);
paulson@9548
    31
fun mk_succ t = succ $ t;
paulson@9548
    32
val one = mk_succ zero;
paulson@9548
    33
paulson@9570
    34
val mk_plus = FOLogic.mk_binop "Arith.add";
paulson@9548
    35
paulson@9548
    36
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
paulson@9548
    37
fun mk_sum []        = zero
paulson@9548
    38
  | mk_sum [t,u]     = mk_plus (t, u)
paulson@9548
    39
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@9548
    40
paulson@9548
    41
(*this version ALWAYS includes a trailing zero*)
paulson@9548
    42
fun long_mk_sum []        = zero
paulson@9548
    43
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@9548
    44
paulson@9548
    45
val dest_plus = FOLogic.dest_bin "Arith.add" iT;
paulson@9548
    46
paulson@9548
    47
(* dest_sum *)
paulson@9548
    48
paulson@9548
    49
fun dest_sum (Const("0",_)) = []
paulson@9548
    50
  | dest_sum (Const("succ",_) $ t) = one :: dest_sum t
paulson@9548
    51
  | dest_sum (Const("Arith.add",_) $ t $ u) = dest_sum t @ dest_sum u
paulson@9548
    52
  | dest_sum tm = [tm];
paulson@9548
    53
paulson@9548
    54
(*Apply the given rewrite (if present) just once*)
skalberg@15531
    55
fun gen_trans_tac th2 NONE      = all_tac
skalberg@15531
    56
  | gen_trans_tac th2 (SOME th) = ALLGOALS (rtac (th RS th2));
paulson@9548
    57
paulson@9548
    58
(*Use <-> or = depending on the type of t*)
paulson@9548
    59
fun mk_eq_iff(t,u) =
paulson@9548
    60
  if fastype_of t = iT then FOLogic.mk_eq(t,u)
paulson@9548
    61
                       else FOLogic.mk_iff(t,u);
paulson@9548
    62
paulson@9874
    63
(*We remove equality assumptions because they confuse the simplifier and
paulson@9874
    64
  because only type-checking assumptions are necessary.*)
wenzelm@13462
    65
fun is_eq_thm th =
paulson@9874
    66
    can FOLogic.dest_eq (FOLogic.dest_Trueprop (#prop (rep_thm th)));
paulson@9649
    67
paulson@9548
    68
fun add_chyps chyps ct = Drule.list_implies (map cprop_of chyps, ct);
paulson@9548
    69
wenzelm@20113
    70
fun prove_conv name tacs ctxt prems (t,u) =
skalberg@15531
    71
  if t aconv u then NONE
paulson@9548
    72
  else
wenzelm@20113
    73
  let val prems' = List.filter (not o is_eq_thm) prems
wenzelm@20113
    74
      val goal = Logic.list_implies (map (#prop o Thm.rep_thm) prems',
wenzelm@12134
    75
        FOLogic.mk_Trueprop (mk_eq_iff (t, u)));
wenzelm@20113
    76
  in SOME (prems' MRS Goal.prove ctxt [] [] goal (K (EVERY tacs)))
wenzelm@18678
    77
      handle ERROR msg =>
skalberg@15531
    78
        (warning (msg ^ "\nCancellation failed: no typing information? (" ^ name ^ ")"); NONE)
paulson@9548
    79
  end;
paulson@9548
    80
wenzelm@13462
    81
fun prep_simproc (name, pats, proc) =
wenzelm@20342
    82
  Simplifier.simproc (the_context ()) name pats proc;
paulson@9548
    83
paulson@9548
    84
wenzelm@13462
    85
(*** Use CancelNumerals simproc without binary numerals,
paulson@9548
    86
     just for cancellation ***)
paulson@9548
    87
paulson@9570
    88
val mk_times = FOLogic.mk_binop "Arith.mult";
paulson@9548
    89
paulson@9548
    90
fun mk_prod [] = one
paulson@9548
    91
  | mk_prod [t] = t
paulson@9548
    92
  | mk_prod (t :: ts) = if t = one then mk_prod ts
paulson@9548
    93
                        else mk_times (t, mk_prod ts);
paulson@9548
    94
paulson@9548
    95
val dest_times = FOLogic.dest_bin "Arith.mult" iT;
paulson@9548
    96
paulson@9548
    97
fun dest_prod t =
paulson@9548
    98
      let val (t,u) = dest_times t
paulson@9548
    99
      in  dest_prod t @ dest_prod u  end
paulson@9548
   100
      handle TERM _ => [t];
paulson@9548
   101
paulson@9548
   102
(*Dummy version: the only arguments are 0 and 1*)
paulson@15965
   103
fun mk_coeff (0: IntInf.int, t) = zero
paulson@9548
   104
  | mk_coeff (1, t) = t
paulson@9548
   105
  | mk_coeff _       = raise TERM("mk_coeff", []);
paulson@9548
   106
paulson@9548
   107
(*Dummy version: the "coefficient" is always 1.
paulson@9548
   108
  In the result, the factors are sorted terms*)
paulson@15965
   109
fun dest_coeff t = (1 : IntInf.int, mk_prod (sort Term.term_ord (dest_prod t)));
paulson@9548
   110
paulson@9548
   111
(*Find first coefficient-term THAT MATCHES u*)
paulson@9548
   112
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
paulson@9548
   113
  | find_first_coeff past u (t::terms) =
paulson@9548
   114
        let val (n,u') = dest_coeff t
paulson@9548
   115
        in  if u aconv u' then (n, rev past @ terms)
paulson@9548
   116
                          else find_first_coeff (t::past) u terms
paulson@9548
   117
        end
paulson@9548
   118
        handle TERM _ => find_first_coeff (t::past) u terms;
paulson@9548
   119
paulson@9548
   120
paulson@9548
   121
(*Simplify #1*n and n*#1 to n*)
paulson@9548
   122
val add_0s = [add_0_natify, add_0_right_natify];
paulson@9548
   123
val add_succs = [add_succ, add_succ_right];
paulson@9548
   124
val mult_1s = [mult_1_natify, mult_1_right_natify];
paulson@9548
   125
val tc_rules = [natify_in_nat, add_type, diff_type, mult_type];
paulson@9548
   126
val natifys = [natify_0, natify_ident, add_natify1, add_natify2,
paulson@9570
   127
               diff_natify1, diff_natify2];
paulson@9548
   128
paulson@9548
   129
(*Final simplification: cancel + and **)
wenzelm@18328
   130
fun simplify_meta_eq rules =
wenzelm@18328
   131
  let val ss0 =
wenzelm@18328
   132
    FOL_ss addeqcongs [eq_cong2, iff_cong2]
wenzelm@18328
   133
      delsimps iff_simps (*these could erase the whole rule!*)
wenzelm@18328
   134
      addsimps rules
wenzelm@18328
   135
  in fn ss => mk_meta_eq o simplify (Simplifier.inherit_context ss ss0) end;
paulson@9548
   136
paulson@9548
   137
val final_rules = add_0s @ mult_1s @ [mult_0, mult_0_right];
paulson@9548
   138
paulson@9548
   139
structure CancelNumeralsCommon =
paulson@9548
   140
  struct
paulson@14387
   141
  val mk_sum            = (fn T:typ => mk_sum)
paulson@9548
   142
  val dest_sum          = dest_sum
paulson@9548
   143
  val mk_coeff          = mk_coeff
paulson@9548
   144
  val dest_coeff        = dest_coeff
paulson@9548
   145
  val find_first_coeff  = find_first_coeff []
wenzelm@18328
   146
wenzelm@18328
   147
  val norm_ss1 = ZF_ss addsimps add_0s @ add_succs @ mult_1s @ add_ac
wenzelm@18328
   148
  val norm_ss2 = ZF_ss addsimps add_0s @ mult_1s @ add_ac @ mult_ac @ tc_rules @ natifys
wenzelm@16973
   149
  fun norm_tac ss =
wenzelm@18328
   150
    ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@18328
   151
    THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@18328
   152
  val numeral_simp_ss = ZF_ss addsimps add_0s @ tc_rules @ natifys
wenzelm@16973
   153
  fun numeral_simp_tac ss =
wenzelm@18328
   154
    ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
paulson@9548
   155
  val simplify_meta_eq  = simplify_meta_eq final_rules
paulson@9548
   156
  end;
paulson@9548
   157
paulson@9874
   158
(** The functor argumnets are declared as separate structures
paulson@9874
   159
    so that they can be exported to ease debugging. **)
paulson@9548
   160
wenzelm@13462
   161
structure EqCancelNumeralsData =
paulson@9874
   162
  struct
paulson@9874
   163
  open CancelNumeralsCommon
paulson@9548
   164
  val prove_conv = prove_conv "nateq_cancel_numerals"
paulson@9548
   165
  val mk_bal   = FOLogic.mk_eq
paulson@9649
   166
  val dest_bal = FOLogic.dest_eq
paulson@9548
   167
  val bal_add1 = eq_add_iff RS iff_trans
paulson@9548
   168
  val bal_add2 = eq_add_iff RS iff_trans
wenzelm@16973
   169
  fun trans_tac _ = gen_trans_tac iff_trans
paulson@9874
   170
  end;
paulson@9874
   171
paulson@9874
   172
structure EqCancelNumerals = CancelNumeralsFun(EqCancelNumeralsData);
paulson@9548
   173
wenzelm@13462
   174
structure LessCancelNumeralsData =
paulson@9874
   175
  struct
paulson@9874
   176
  open CancelNumeralsCommon
paulson@9548
   177
  val prove_conv = prove_conv "natless_cancel_numerals"
paulson@13155
   178
  val mk_bal   = FOLogic.mk_binrel "Ordinal.lt"
paulson@13155
   179
  val dest_bal = FOLogic.dest_bin "Ordinal.lt" iT
paulson@9548
   180
  val bal_add1 = less_add_iff RS iff_trans
paulson@9548
   181
  val bal_add2 = less_add_iff RS iff_trans
wenzelm@16973
   182
  fun trans_tac _ = gen_trans_tac iff_trans
paulson@9874
   183
  end;
paulson@9874
   184
paulson@9874
   185
structure LessCancelNumerals = CancelNumeralsFun(LessCancelNumeralsData);
paulson@9548
   186
wenzelm@13462
   187
structure DiffCancelNumeralsData =
paulson@9874
   188
  struct
paulson@9874
   189
  open CancelNumeralsCommon
paulson@9548
   190
  val prove_conv = prove_conv "natdiff_cancel_numerals"
paulson@9570
   191
  val mk_bal   = FOLogic.mk_binop "Arith.diff"
paulson@9548
   192
  val dest_bal = FOLogic.dest_bin "Arith.diff" iT
paulson@9548
   193
  val bal_add1 = diff_add_eq RS trans
paulson@9548
   194
  val bal_add2 = diff_add_eq RS trans
wenzelm@16973
   195
  fun trans_tac _ = gen_trans_tac trans
paulson@9874
   196
  end;
paulson@9874
   197
paulson@9874
   198
structure DiffCancelNumerals = CancelNumeralsFun(DiffCancelNumeralsData);
paulson@9548
   199
paulson@9548
   200
paulson@9548
   201
val nat_cancel =
wenzelm@13462
   202
  map prep_simproc
wenzelm@13462
   203
   [("nateq_cancel_numerals",
wenzelm@13462
   204
     ["l #+ m = n", "l = m #+ n",
wenzelm@13462
   205
      "l #* m = n", "l = m #* n",
wenzelm@13462
   206
      "succ(m) = n", "m = succ(n)"],
wenzelm@20044
   207
     (K EqCancelNumerals.proc)),
wenzelm@13462
   208
    ("natless_cancel_numerals",
wenzelm@13462
   209
     ["l #+ m < n", "l < m #+ n",
wenzelm@13462
   210
      "l #* m < n", "l < m #* n",
wenzelm@13462
   211
      "succ(m) < n", "m < succ(n)"],
wenzelm@20044
   212
     (K LessCancelNumerals.proc)),
wenzelm@13462
   213
    ("natdiff_cancel_numerals",
wenzelm@13462
   214
     ["(l #+ m) #- n", "l #- (m #+ n)",
wenzelm@13462
   215
      "(l #* m) #- n", "l #- (m #* n)",
wenzelm@13462
   216
      "succ(m) #- n", "m #- succ(n)"],
wenzelm@20044
   217
     (K DiffCancelNumerals.proc))];
paulson@9548
   218
paulson@9548
   219
end;
paulson@9548
   220
paulson@13259
   221
Addsimprocs ArithData.nat_cancel;
paulson@13259
   222
paulson@13259
   223
paulson@9548
   224
(*examples:
paulson@9548
   225
print_depth 22;
paulson@9548
   226
set timing;
paulson@9548
   227
set trace_simp;
paulson@9548
   228
fun test s = (Goal s; by (Asm_simp_tac 1));
paulson@9548
   229
paulson@9548
   230
test "x #+ y = x #+ z";
paulson@9548
   231
test "y #+ x = x #+ z";
paulson@9548
   232
test "x #+ y #+ z = x #+ z";
paulson@9548
   233
test "y #+ (z #+ x) = z #+ x";
paulson@9548
   234
test "x #+ y #+ z = (z #+ y) #+ (x #+ w)";
paulson@9548
   235
test "x#*y #+ z = (z #+ y) #+ (y#*x #+ w)";
paulson@9548
   236
paulson@9548
   237
test "x #+ succ(y) = x #+ z";
paulson@9548
   238
test "x #+ succ(y) = succ(z #+ x)";
paulson@9548
   239
test "succ(x) #+ succ(y) #+ z = succ(z #+ y) #+ succ(x #+ w)";
paulson@9548
   240
paulson@9548
   241
test "(x #+ y) #- (x #+ z) = w";
paulson@9548
   242
test "(y #+ x) #- (x #+ z) = dd";
paulson@9548
   243
test "(x #+ y #+ z) #- (x #+ z) = dd";
paulson@9548
   244
test "(y #+ (z #+ x)) #- (z #+ x) = dd";
paulson@9548
   245
test "(x #+ y #+ z) #- ((z #+ y) #+ (x #+ w)) = dd";
paulson@9548
   246
test "(x#*y #+ z) #- ((z #+ y) #+ (y#*x #+ w)) = dd";
paulson@9548
   247
paulson@9548
   248
(*BAD occurrence of natify*)
paulson@9548
   249
test "(x #+ succ(y)) #- (x #+ z) = dd";
paulson@9548
   250
paulson@9548
   251
test "x #* y2 #+ y #* x2 = y #* x2 #+ x #* y2";
paulson@9548
   252
paulson@9548
   253
test "(x #+ succ(y)) #- (succ(z #+ x)) = dd";
paulson@9548
   254
test "(succ(x) #+ succ(y) #+ z) #- (succ(z #+ y) #+ succ(x #+ w)) = dd";
paulson@9548
   255
paulson@9548
   256
(*use of typing information*)
paulson@9548
   257
test "x : nat ==> x #+ y = x";
paulson@9548
   258
test "x : nat --> x #+ y = x";
paulson@9548
   259
test "x : nat ==> x #+ y < x";
paulson@9548
   260
test "x : nat ==> x < y#+x";
paulson@13126
   261
test "x : nat ==> x le succ(x)";
paulson@9548
   262
paulson@9548
   263
(*fails: no typing information isn't visible*)
paulson@9548
   264
test "x #+ y = x";
paulson@9548
   265
paulson@9548
   266
test "x #+ y < x #+ z";
paulson@9548
   267
test "y #+ x < x #+ z";
paulson@9548
   268
test "x #+ y #+ z < x #+ z";
paulson@9548
   269
test "y #+ z #+ x < x #+ z";
paulson@9548
   270
test "y #+ (z #+ x) < z #+ x";
paulson@9548
   271
test "x #+ y #+ z < (z #+ y) #+ (x #+ w)";
paulson@9548
   272
test "x#*y #+ z < (z #+ y) #+ (y#*x #+ w)";
paulson@9548
   273
paulson@9548
   274
test "x #+ succ(y) < x #+ z";
paulson@9548
   275
test "x #+ succ(y) < succ(z #+ x)";
paulson@9548
   276
test "succ(x) #+ succ(y) #+ z < succ(z #+ y) #+ succ(x #+ w)";
paulson@9548
   277
paulson@9548
   278
test "x #+ succ(y) le succ(z #+ x)";
paulson@9548
   279
*)