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