src/ZF/arith_data.ML
author paulson
Mon Aug 07 10:29:54 2000 +0200 (2000-08-07)
changeset 9548 15bee2731e43
child 9570 e16e168984e1
permissions -rw-r--r--
instantiated Cancel_Numerals for "nat" in ZF
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@9548
    11
  val nat_cancel: simproc list
paulson@9548
    12
end;
paulson@9548
    13
paulson@9548
    14
structure ArithData: ARITH_DATA =
paulson@9548
    15
struct
paulson@9548
    16
paulson@9548
    17
val iT = Ind_Syntax.iT;
paulson@9548
    18
paulson@9548
    19
val zero = Const("0", iT);
paulson@9548
    20
val succ = Const("succ", iT --> iT);
paulson@9548
    21
fun mk_succ t = succ $ t;
paulson@9548
    22
val one = mk_succ zero;
paulson@9548
    23
paulson@9548
    24
(*Not FOLogic.mk_binop, since it calls fastype_of, which can fail*)
paulson@9548
    25
fun mk_binop_i  c (t,u) = Const (c, [iT,iT] ---> iT) $ t $ u;
paulson@9548
    26
fun mk_binrel_i c (t,u) = Const (c, [iT,iT] ---> oT) $ t $ u;
paulson@9548
    27
paulson@9548
    28
val mk_plus = mk_binop_i "Arith.add";
paulson@9548
    29
paulson@9548
    30
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
paulson@9548
    31
fun mk_sum []        = zero
paulson@9548
    32
  | mk_sum [t,u]     = mk_plus (t, u)
paulson@9548
    33
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@9548
    34
paulson@9548
    35
(*this version ALWAYS includes a trailing zero*)
paulson@9548
    36
fun long_mk_sum []        = zero
paulson@9548
    37
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@9548
    38
paulson@9548
    39
val dest_plus = FOLogic.dest_bin "Arith.add" iT;
paulson@9548
    40
paulson@9548
    41
(* dest_sum *)
paulson@9548
    42
paulson@9548
    43
fun dest_sum (Const("0",_)) = []
paulson@9548
    44
  | dest_sum (Const("succ",_) $ t) = one :: dest_sum t
paulson@9548
    45
  | dest_sum (Const("Arith.add",_) $ t $ u) = dest_sum t @ dest_sum u
paulson@9548
    46
  | dest_sum tm = [tm];
paulson@9548
    47
paulson@9548
    48
(*Apply the given rewrite (if present) just once*)
paulson@9548
    49
fun gen_trans_tac th2 None      = all_tac
paulson@9548
    50
  | gen_trans_tac th2 (Some th) = ALLGOALS (rtac (th RS th2));
paulson@9548
    51
paulson@9548
    52
(*Use <-> or = depending on the type of t*)
paulson@9548
    53
fun mk_eq_iff(t,u) =
paulson@9548
    54
  if fastype_of t = iT then FOLogic.mk_eq(t,u)
paulson@9548
    55
                       else FOLogic.mk_iff(t,u);
paulson@9548
    56
paulson@9548
    57
paulson@9548
    58
fun add_chyps chyps ct = Drule.list_implies (map cprop_of chyps, ct);
paulson@9548
    59
paulson@9548
    60
fun prove_conv name tacs sg hyps (t,u) =
paulson@9548
    61
  if t aconv u then None
paulson@9548
    62
  else
paulson@9548
    63
  let val ct = add_chyps hyps
paulson@9548
    64
                  (cterm_of sg (FOLogic.mk_Trueprop (mk_eq_iff(t, u))))
paulson@9548
    65
  in Some
paulson@9548
    66
      (hyps MRS 
paulson@9548
    67
       (prove_goalw_cterm_nocheck [] ct 
paulson@9548
    68
	(fn prems => cut_facts_tac prems 1 :: tacs)))
paulson@9548
    69
      handle ERROR => 
paulson@9548
    70
	(warning 
paulson@9548
    71
	 ("Cancellation failed: no typing information? (" ^ name ^ ")"); 
paulson@9548
    72
	 None)
paulson@9548
    73
  end;
paulson@9548
    74
paulson@9548
    75
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
paulson@9548
    76
fun prep_pat s = Thm.read_cterm (Theory.sign_of (the_context ()))
paulson@9548
    77
                      (s, TypeInfer.anyT ["logic"]);
paulson@9548
    78
val prep_pats = map prep_pat;
paulson@9548
    79
paulson@9548
    80
paulson@9548
    81
(*** Use CancelNumerals simproc without binary numerals, 
paulson@9548
    82
     just for cancellation ***)
paulson@9548
    83
paulson@9548
    84
val mk_times = mk_binop_i "Arith.mult";
paulson@9548
    85
paulson@9548
    86
fun mk_prod [] = one
paulson@9548
    87
  | mk_prod [t] = t
paulson@9548
    88
  | mk_prod (t :: ts) = if t = one then mk_prod ts
paulson@9548
    89
                        else mk_times (t, mk_prod ts);
paulson@9548
    90
paulson@9548
    91
val dest_times = FOLogic.dest_bin "Arith.mult" iT;
paulson@9548
    92
paulson@9548
    93
fun dest_prod t =
paulson@9548
    94
      let val (t,u) = dest_times t
paulson@9548
    95
      in  dest_prod t @ dest_prod u  end
paulson@9548
    96
      handle TERM _ => [t];
paulson@9548
    97
paulson@9548
    98
(*Dummy version: the only arguments are 0 and 1*)
paulson@9548
    99
fun mk_coeff (0, t) = zero
paulson@9548
   100
  | mk_coeff (1, t) = t
paulson@9548
   101
  | mk_coeff _       = raise TERM("mk_coeff", []);
paulson@9548
   102
paulson@9548
   103
(*Dummy version: the "coefficient" is always 1.
paulson@9548
   104
  In the result, the factors are sorted terms*)
paulson@9548
   105
fun dest_coeff t = (1, mk_prod (sort Term.term_ord (dest_prod t)));
paulson@9548
   106
paulson@9548
   107
(*Find first coefficient-term THAT MATCHES u*)
paulson@9548
   108
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
paulson@9548
   109
  | find_first_coeff past u (t::terms) =
paulson@9548
   110
        let val (n,u') = dest_coeff t
paulson@9548
   111
        in  if u aconv u' then (n, rev past @ terms)
paulson@9548
   112
                          else find_first_coeff (t::past) u terms
paulson@9548
   113
        end
paulson@9548
   114
        handle TERM _ => find_first_coeff (t::past) u terms;
paulson@9548
   115
paulson@9548
   116
paulson@9548
   117
(*Simplify #1*n and n*#1 to n*)
paulson@9548
   118
val add_0s = [add_0_natify, add_0_right_natify];
paulson@9548
   119
val add_succs = [add_succ, add_succ_right];
paulson@9548
   120
val mult_1s = [mult_1_natify, mult_1_right_natify];
paulson@9548
   121
val tc_rules = [natify_in_nat, add_type, diff_type, mult_type];
paulson@9548
   122
val natifys = [natify_0, natify_ident, add_natify1, add_natify2,
paulson@9548
   123
               add_natify1, add_natify2, diff_natify1, diff_natify2];
paulson@9548
   124
paulson@9548
   125
(*Final simplification: cancel + and **)
paulson@9548
   126
fun simplify_meta_eq rules =
paulson@9548
   127
    mk_meta_eq o
paulson@9548
   128
    simplify (FOL_ss addeqcongs[eq_cong2,iff_cong2] 
paulson@9548
   129
                     delsimps iff_simps (*these could erase the whole rule!*)
paulson@9548
   130
		     addsimps rules)
paulson@9548
   131
paulson@9548
   132
val final_rules = add_0s @ mult_1s @ [mult_0, mult_0_right];
paulson@9548
   133
paulson@9548
   134
structure CancelNumeralsCommon =
paulson@9548
   135
  struct
paulson@9548
   136
  val mk_sum            = mk_sum
paulson@9548
   137
  val dest_sum          = dest_sum
paulson@9548
   138
  val mk_coeff          = mk_coeff
paulson@9548
   139
  val dest_coeff        = dest_coeff
paulson@9548
   140
  val find_first_coeff  = find_first_coeff []
paulson@9548
   141
  val norm_tac_ss1 = ZF_ss addsimps add_0s@add_succs@mult_1s@add_ac
paulson@9548
   142
  val norm_tac_ss2 = ZF_ss addsimps add_ac@mult_ac@tc_rules@natifys
paulson@9548
   143
  val norm_tac = ALLGOALS (asm_simp_tac norm_tac_ss1)
paulson@9548
   144
                 THEN ALLGOALS (asm_simp_tac norm_tac_ss2)
paulson@9548
   145
  val numeral_simp_tac_ss = ZF_ss addsimps add_0s@tc_rules@natifys
paulson@9548
   146
  val numeral_simp_tac  = ALLGOALS (asm_simp_tac numeral_simp_tac_ss)
paulson@9548
   147
  val simplify_meta_eq  = simplify_meta_eq final_rules
paulson@9548
   148
  end;
paulson@9548
   149
paulson@9548
   150
paulson@9548
   151
structure EqCancelNumerals = CancelNumeralsFun
paulson@9548
   152
 (open CancelNumeralsCommon
paulson@9548
   153
  val prove_conv = prove_conv "nateq_cancel_numerals"
paulson@9548
   154
  val mk_bal   = FOLogic.mk_eq
paulson@9548
   155
  val dest_bal = FOLogic.dest_bin "op =" iT
paulson@9548
   156
  val bal_add1 = eq_add_iff RS iff_trans
paulson@9548
   157
  val bal_add2 = eq_add_iff RS iff_trans
paulson@9548
   158
  val trans_tac = gen_trans_tac iff_trans
paulson@9548
   159
);
paulson@9548
   160
paulson@9548
   161
structure LessCancelNumerals = CancelNumeralsFun
paulson@9548
   162
 (open CancelNumeralsCommon
paulson@9548
   163
  val prove_conv = prove_conv "natless_cancel_numerals"
paulson@9548
   164
  val mk_bal   = mk_binrel_i "Ordinal.op <"
paulson@9548
   165
  val dest_bal = FOLogic.dest_bin "Ordinal.op <" iT
paulson@9548
   166
  val bal_add1 = less_add_iff RS iff_trans
paulson@9548
   167
  val bal_add2 = less_add_iff RS iff_trans
paulson@9548
   168
  val trans_tac = gen_trans_tac iff_trans
paulson@9548
   169
);
paulson@9548
   170
paulson@9548
   171
structure DiffCancelNumerals = CancelNumeralsFun
paulson@9548
   172
 (open CancelNumeralsCommon
paulson@9548
   173
  val prove_conv = prove_conv "natdiff_cancel_numerals"
paulson@9548
   174
  val mk_bal   = mk_binop_i "Arith.diff"
paulson@9548
   175
  val dest_bal = FOLogic.dest_bin "Arith.diff" iT
paulson@9548
   176
  val bal_add1 = diff_add_eq RS trans
paulson@9548
   177
  val bal_add2 = diff_add_eq RS trans
paulson@9548
   178
  val trans_tac = gen_trans_tac trans
paulson@9548
   179
);
paulson@9548
   180
paulson@9548
   181
paulson@9548
   182
val nat_cancel =
paulson@9548
   183
      map prep_simproc
paulson@9548
   184
       [("nateq_cancel_numerals",
paulson@9548
   185
	 prep_pats ["l #+ m = n", "l = m #+ n",
paulson@9548
   186
		    "l #* m = n", "l = m #* n",
paulson@9548
   187
		    "succ(m) = n", "m = succ(n)"],
paulson@9548
   188
	 EqCancelNumerals.proc),
paulson@9548
   189
	("natless_cancel_numerals",
paulson@9548
   190
	 prep_pats ["l #+ m < n", "l < m #+ n",
paulson@9548
   191
		    "l #* m < n", "l < m #* n",
paulson@9548
   192
		    "succ(m) < n", "m < succ(n)"],
paulson@9548
   193
	 LessCancelNumerals.proc),
paulson@9548
   194
	("natdiff_cancel_numerals",
paulson@9548
   195
	 prep_pats ["(l #+ m) #- n", "l #- (m #+ n)",
paulson@9548
   196
		    "(l #* m) #- n", "l #- (m #* n)",
paulson@9548
   197
		    "succ(m) #- n", "m #- succ(n)"],
paulson@9548
   198
	 DiffCancelNumerals.proc)];
paulson@9548
   199
paulson@9548
   200
end;
paulson@9548
   201
paulson@9548
   202
(*examples:
paulson@9548
   203
print_depth 22;
paulson@9548
   204
set timing;
paulson@9548
   205
set trace_simp;
paulson@9548
   206
fun test s = (Goal s; by (Asm_simp_tac 1));
paulson@9548
   207
paulson@9548
   208
test "x #+ y = x #+ z";
paulson@9548
   209
test "y #+ x = x #+ z";
paulson@9548
   210
test "x #+ y #+ z = x #+ z";
paulson@9548
   211
test "y #+ (z #+ x) = z #+ x";
paulson@9548
   212
test "x #+ y #+ z = (z #+ y) #+ (x #+ w)";
paulson@9548
   213
test "x#*y #+ z = (z #+ y) #+ (y#*x #+ w)";
paulson@9548
   214
paulson@9548
   215
test "x #+ succ(y) = x #+ z";
paulson@9548
   216
test "x #+ succ(y) = succ(z #+ x)";
paulson@9548
   217
test "succ(x) #+ succ(y) #+ z = succ(z #+ y) #+ succ(x #+ w)";
paulson@9548
   218
paulson@9548
   219
test "(x #+ y) #- (x #+ z) = w";
paulson@9548
   220
test "(y #+ x) #- (x #+ z) = dd";
paulson@9548
   221
test "(x #+ y #+ z) #- (x #+ z) = dd";
paulson@9548
   222
test "(y #+ (z #+ x)) #- (z #+ x) = dd";
paulson@9548
   223
test "(x #+ y #+ z) #- ((z #+ y) #+ (x #+ w)) = dd";
paulson@9548
   224
test "(x#*y #+ z) #- ((z #+ y) #+ (y#*x #+ w)) = dd";
paulson@9548
   225
paulson@9548
   226
(*BAD occurrence of natify*)
paulson@9548
   227
test "(x #+ succ(y)) #- (x #+ z) = dd";
paulson@9548
   228
paulson@9548
   229
test "x #* y2 #+ y #* x2 = y #* x2 #+ x #* y2";
paulson@9548
   230
paulson@9548
   231
test "(x #+ succ(y)) #- (succ(z #+ x)) = dd";
paulson@9548
   232
test "(succ(x) #+ succ(y) #+ z) #- (succ(z #+ y) #+ succ(x #+ w)) = dd";
paulson@9548
   233
paulson@9548
   234
(*use of typing information*)
paulson@9548
   235
test "x : nat ==> x #+ y = x";
paulson@9548
   236
test "x : nat --> x #+ y = x";
paulson@9548
   237
test "x : nat ==> x #+ y < x";
paulson@9548
   238
test "x : nat ==> x < y#+x";
paulson@9548
   239
paulson@9548
   240
(*fails: no typing information isn't visible*)
paulson@9548
   241
test "x #+ y = x";
paulson@9548
   242
paulson@9548
   243
test "x #+ y < x #+ z";
paulson@9548
   244
test "y #+ x < x #+ z";
paulson@9548
   245
test "x #+ y #+ z < x #+ z";
paulson@9548
   246
test "y #+ z #+ x < x #+ z";
paulson@9548
   247
test "y #+ (z #+ x) < z #+ x";
paulson@9548
   248
test "x #+ y #+ z < (z #+ y) #+ (x #+ w)";
paulson@9548
   249
test "x#*y #+ z < (z #+ y) #+ (y#*x #+ w)";
paulson@9548
   250
paulson@9548
   251
test "x #+ succ(y) < x #+ z";
paulson@9548
   252
test "x #+ succ(y) < succ(z #+ x)";
paulson@9548
   253
test "succ(x) #+ succ(y) #+ z < succ(z #+ y) #+ succ(x #+ w)";
paulson@9548
   254
paulson@9548
   255
test "x #+ succ(y) le succ(z #+ x)";
paulson@9548
   256
*)