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