src/ZF/arith_data.ML
changeset 9548 15bee2731e43
child 9570 e16e168984e1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ZF/arith_data.ML	Mon Aug 07 10:29:54 2000 +0200
     1.3 @@ -0,0 +1,256 @@
     1.4 +(*  Title:      ZF/arith_data.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   2000  University of Cambridge
     1.8 +
     1.9 +Arithmetic simplification: cancellation of common terms
    1.10 +*)
    1.11 +
    1.12 +signature ARITH_DATA =
    1.13 +sig
    1.14 +  val nat_cancel: simproc list
    1.15 +end;
    1.16 +
    1.17 +structure ArithData: ARITH_DATA =
    1.18 +struct
    1.19 +
    1.20 +val iT = Ind_Syntax.iT;
    1.21 +
    1.22 +val zero = Const("0", iT);
    1.23 +val succ = Const("succ", iT --> iT);
    1.24 +fun mk_succ t = succ $ t;
    1.25 +val one = mk_succ zero;
    1.26 +
    1.27 +(*Not FOLogic.mk_binop, since it calls fastype_of, which can fail*)
    1.28 +fun mk_binop_i  c (t,u) = Const (c, [iT,iT] ---> iT) $ t $ u;
    1.29 +fun mk_binrel_i c (t,u) = Const (c, [iT,iT] ---> oT) $ t $ u;
    1.30 +
    1.31 +val mk_plus = mk_binop_i "Arith.add";
    1.32 +
    1.33 +(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
    1.34 +fun mk_sum []        = zero
    1.35 +  | mk_sum [t,u]     = mk_plus (t, u)
    1.36 +  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
    1.37 +
    1.38 +(*this version ALWAYS includes a trailing zero*)
    1.39 +fun long_mk_sum []        = zero
    1.40 +  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
    1.41 +
    1.42 +val dest_plus = FOLogic.dest_bin "Arith.add" iT;
    1.43 +
    1.44 +(* dest_sum *)
    1.45 +
    1.46 +fun dest_sum (Const("0",_)) = []
    1.47 +  | dest_sum (Const("succ",_) $ t) = one :: dest_sum t
    1.48 +  | dest_sum (Const("Arith.add",_) $ t $ u) = dest_sum t @ dest_sum u
    1.49 +  | dest_sum tm = [tm];
    1.50 +
    1.51 +(*Apply the given rewrite (if present) just once*)
    1.52 +fun gen_trans_tac th2 None      = all_tac
    1.53 +  | gen_trans_tac th2 (Some th) = ALLGOALS (rtac (th RS th2));
    1.54 +
    1.55 +(*Use <-> or = depending on the type of t*)
    1.56 +fun mk_eq_iff(t,u) =
    1.57 +  if fastype_of t = iT then FOLogic.mk_eq(t,u)
    1.58 +                       else FOLogic.mk_iff(t,u);
    1.59 +
    1.60 +
    1.61 +fun add_chyps chyps ct = Drule.list_implies (map cprop_of chyps, ct);
    1.62 +
    1.63 +fun prove_conv name tacs sg hyps (t,u) =
    1.64 +  if t aconv u then None
    1.65 +  else
    1.66 +  let val ct = add_chyps hyps
    1.67 +                  (cterm_of sg (FOLogic.mk_Trueprop (mk_eq_iff(t, u))))
    1.68 +  in Some
    1.69 +      (hyps MRS 
    1.70 +       (prove_goalw_cterm_nocheck [] ct 
    1.71 +	(fn prems => cut_facts_tac prems 1 :: tacs)))
    1.72 +      handle ERROR => 
    1.73 +	(warning 
    1.74 +	 ("Cancellation failed: no typing information? (" ^ name ^ ")"); 
    1.75 +	 None)
    1.76 +  end;
    1.77 +
    1.78 +fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
    1.79 +fun prep_pat s = Thm.read_cterm (Theory.sign_of (the_context ()))
    1.80 +                      (s, TypeInfer.anyT ["logic"]);
    1.81 +val prep_pats = map prep_pat;
    1.82 +
    1.83 +
    1.84 +(*** Use CancelNumerals simproc without binary numerals, 
    1.85 +     just for cancellation ***)
    1.86 +
    1.87 +val mk_times = mk_binop_i "Arith.mult";
    1.88 +
    1.89 +fun mk_prod [] = one
    1.90 +  | mk_prod [t] = t
    1.91 +  | mk_prod (t :: ts) = if t = one then mk_prod ts
    1.92 +                        else mk_times (t, mk_prod ts);
    1.93 +
    1.94 +val dest_times = FOLogic.dest_bin "Arith.mult" iT;
    1.95 +
    1.96 +fun dest_prod t =
    1.97 +      let val (t,u) = dest_times t
    1.98 +      in  dest_prod t @ dest_prod u  end
    1.99 +      handle TERM _ => [t];
   1.100 +
   1.101 +(*Dummy version: the only arguments are 0 and 1*)
   1.102 +fun mk_coeff (0, t) = zero
   1.103 +  | mk_coeff (1, t) = t
   1.104 +  | mk_coeff _       = raise TERM("mk_coeff", []);
   1.105 +
   1.106 +(*Dummy version: the "coefficient" is always 1.
   1.107 +  In the result, the factors are sorted terms*)
   1.108 +fun dest_coeff t = (1, mk_prod (sort Term.term_ord (dest_prod t)));
   1.109 +
   1.110 +(*Find first coefficient-term THAT MATCHES u*)
   1.111 +fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
   1.112 +  | find_first_coeff past u (t::terms) =
   1.113 +        let val (n,u') = dest_coeff t
   1.114 +        in  if u aconv u' then (n, rev past @ terms)
   1.115 +                          else find_first_coeff (t::past) u terms
   1.116 +        end
   1.117 +        handle TERM _ => find_first_coeff (t::past) u terms;
   1.118 +
   1.119 +
   1.120 +(*Simplify #1*n and n*#1 to n*)
   1.121 +val add_0s = [add_0_natify, add_0_right_natify];
   1.122 +val add_succs = [add_succ, add_succ_right];
   1.123 +val mult_1s = [mult_1_natify, mult_1_right_natify];
   1.124 +val tc_rules = [natify_in_nat, add_type, diff_type, mult_type];
   1.125 +val natifys = [natify_0, natify_ident, add_natify1, add_natify2,
   1.126 +               add_natify1, add_natify2, diff_natify1, diff_natify2];
   1.127 +
   1.128 +(*Final simplification: cancel + and **)
   1.129 +fun simplify_meta_eq rules =
   1.130 +    mk_meta_eq o
   1.131 +    simplify (FOL_ss addeqcongs[eq_cong2,iff_cong2] 
   1.132 +                     delsimps iff_simps (*these could erase the whole rule!*)
   1.133 +		     addsimps rules)
   1.134 +
   1.135 +val final_rules = add_0s @ mult_1s @ [mult_0, mult_0_right];
   1.136 +
   1.137 +structure CancelNumeralsCommon =
   1.138 +  struct
   1.139 +  val mk_sum            = mk_sum
   1.140 +  val dest_sum          = dest_sum
   1.141 +  val mk_coeff          = mk_coeff
   1.142 +  val dest_coeff        = dest_coeff
   1.143 +  val find_first_coeff  = find_first_coeff []
   1.144 +  val norm_tac_ss1 = ZF_ss addsimps add_0s@add_succs@mult_1s@add_ac
   1.145 +  val norm_tac_ss2 = ZF_ss addsimps add_ac@mult_ac@tc_rules@natifys
   1.146 +  val norm_tac = ALLGOALS (asm_simp_tac norm_tac_ss1)
   1.147 +                 THEN ALLGOALS (asm_simp_tac norm_tac_ss2)
   1.148 +  val numeral_simp_tac_ss = ZF_ss addsimps add_0s@tc_rules@natifys
   1.149 +  val numeral_simp_tac  = ALLGOALS (asm_simp_tac numeral_simp_tac_ss)
   1.150 +  val simplify_meta_eq  = simplify_meta_eq final_rules
   1.151 +  end;
   1.152 +
   1.153 +
   1.154 +structure EqCancelNumerals = CancelNumeralsFun
   1.155 + (open CancelNumeralsCommon
   1.156 +  val prove_conv = prove_conv "nateq_cancel_numerals"
   1.157 +  val mk_bal   = FOLogic.mk_eq
   1.158 +  val dest_bal = FOLogic.dest_bin "op =" iT
   1.159 +  val bal_add1 = eq_add_iff RS iff_trans
   1.160 +  val bal_add2 = eq_add_iff RS iff_trans
   1.161 +  val trans_tac = gen_trans_tac iff_trans
   1.162 +);
   1.163 +
   1.164 +structure LessCancelNumerals = CancelNumeralsFun
   1.165 + (open CancelNumeralsCommon
   1.166 +  val prove_conv = prove_conv "natless_cancel_numerals"
   1.167 +  val mk_bal   = mk_binrel_i "Ordinal.op <"
   1.168 +  val dest_bal = FOLogic.dest_bin "Ordinal.op <" iT
   1.169 +  val bal_add1 = less_add_iff RS iff_trans
   1.170 +  val bal_add2 = less_add_iff RS iff_trans
   1.171 +  val trans_tac = gen_trans_tac iff_trans
   1.172 +);
   1.173 +
   1.174 +structure DiffCancelNumerals = CancelNumeralsFun
   1.175 + (open CancelNumeralsCommon
   1.176 +  val prove_conv = prove_conv "natdiff_cancel_numerals"
   1.177 +  val mk_bal   = mk_binop_i "Arith.diff"
   1.178 +  val dest_bal = FOLogic.dest_bin "Arith.diff" iT
   1.179 +  val bal_add1 = diff_add_eq RS trans
   1.180 +  val bal_add2 = diff_add_eq RS trans
   1.181 +  val trans_tac = gen_trans_tac trans
   1.182 +);
   1.183 +
   1.184 +
   1.185 +val nat_cancel =
   1.186 +      map prep_simproc
   1.187 +       [("nateq_cancel_numerals",
   1.188 +	 prep_pats ["l #+ m = n", "l = m #+ n",
   1.189 +		    "l #* m = n", "l = m #* n",
   1.190 +		    "succ(m) = n", "m = succ(n)"],
   1.191 +	 EqCancelNumerals.proc),
   1.192 +	("natless_cancel_numerals",
   1.193 +	 prep_pats ["l #+ m < n", "l < m #+ n",
   1.194 +		    "l #* m < n", "l < m #* n",
   1.195 +		    "succ(m) < n", "m < succ(n)"],
   1.196 +	 LessCancelNumerals.proc),
   1.197 +	("natdiff_cancel_numerals",
   1.198 +	 prep_pats ["(l #+ m) #- n", "l #- (m #+ n)",
   1.199 +		    "(l #* m) #- n", "l #- (m #* n)",
   1.200 +		    "succ(m) #- n", "m #- succ(n)"],
   1.201 +	 DiffCancelNumerals.proc)];
   1.202 +
   1.203 +end;
   1.204 +
   1.205 +(*examples:
   1.206 +print_depth 22;
   1.207 +set timing;
   1.208 +set trace_simp;
   1.209 +fun test s = (Goal s; by (Asm_simp_tac 1));
   1.210 +
   1.211 +test "x #+ y = x #+ z";
   1.212 +test "y #+ x = x #+ z";
   1.213 +test "x #+ y #+ z = x #+ z";
   1.214 +test "y #+ (z #+ x) = z #+ x";
   1.215 +test "x #+ y #+ z = (z #+ y) #+ (x #+ w)";
   1.216 +test "x#*y #+ z = (z #+ y) #+ (y#*x #+ w)";
   1.217 +
   1.218 +test "x #+ succ(y) = x #+ z";
   1.219 +test "x #+ succ(y) = succ(z #+ x)";
   1.220 +test "succ(x) #+ succ(y) #+ z = succ(z #+ y) #+ succ(x #+ w)";
   1.221 +
   1.222 +test "(x #+ y) #- (x #+ z) = w";
   1.223 +test "(y #+ x) #- (x #+ z) = dd";
   1.224 +test "(x #+ y #+ z) #- (x #+ z) = dd";
   1.225 +test "(y #+ (z #+ x)) #- (z #+ x) = dd";
   1.226 +test "(x #+ y #+ z) #- ((z #+ y) #+ (x #+ w)) = dd";
   1.227 +test "(x#*y #+ z) #- ((z #+ y) #+ (y#*x #+ w)) = dd";
   1.228 +
   1.229 +(*BAD occurrence of natify*)
   1.230 +test "(x #+ succ(y)) #- (x #+ z) = dd";
   1.231 +
   1.232 +test "x #* y2 #+ y #* x2 = y #* x2 #+ x #* y2";
   1.233 +
   1.234 +test "(x #+ succ(y)) #- (succ(z #+ x)) = dd";
   1.235 +test "(succ(x) #+ succ(y) #+ z) #- (succ(z #+ y) #+ succ(x #+ w)) = dd";
   1.236 +
   1.237 +(*use of typing information*)
   1.238 +test "x : nat ==> x #+ y = x";
   1.239 +test "x : nat --> x #+ y = x";
   1.240 +test "x : nat ==> x #+ y < x";
   1.241 +test "x : nat ==> x < y#+x";
   1.242 +
   1.243 +(*fails: no typing information isn't visible*)
   1.244 +test "x #+ y = x";
   1.245 +
   1.246 +test "x #+ y < x #+ z";
   1.247 +test "y #+ x < x #+ z";
   1.248 +test "x #+ y #+ z < x #+ z";
   1.249 +test "y #+ z #+ x < x #+ z";
   1.250 +test "y #+ (z #+ x) < z #+ x";
   1.251 +test "x #+ y #+ z < (z #+ y) #+ (x #+ w)";
   1.252 +test "x#*y #+ z < (z #+ y) #+ (y#*x #+ w)";
   1.253 +
   1.254 +test "x #+ succ(y) < x #+ z";
   1.255 +test "x #+ succ(y) < succ(z #+ x)";
   1.256 +test "succ(x) #+ succ(y) #+ z < succ(z #+ y) #+ succ(x #+ w)";
   1.257 +
   1.258 +test "x #+ succ(y) le succ(z #+ x)";
   1.259 +*)