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