src/HOL/arith_data.ML
author wenzelm
Wed Nov 26 16:44:25 1997 +0100 (1997-11-26)
changeset 4295 81132a38996a
child 4309 c98f44948d86
permissions -rw-r--r--
Setup various arithmetic proof procedures.
     1 (*  Title:      HOL/arith_data.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
     4 
     5 Setup various arithmetic proof procedures.
     6 *)
     7 
     8 signature ARITH_DATA =
     9 sig
    10   val nat_cancel: simproc list
    11 end;
    12 
    13 structure ArithData (* FIXME :ARITH_DATA *) =
    14 struct
    15 
    16 
    17 (** abstract syntax of structure nat: 0, Suc, + **)
    18 
    19 (* mk_sum *)
    20 
    21 val mk_plus = HOLogic.mk_binop "op +";
    22 
    23 fun mk_sum [] = HOLogic.zero
    24   | mk_sum [t] = t
    25   | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
    26 
    27 
    28 (* dest_sum *)
    29 
    30 val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT;
    31 val one = HOLogic.mk_nat 1;
    32 
    33 fun dest_sum tm =
    34   if HOLogic.is_zero tm then []
    35   else
    36     (case try HOLogic.dest_Suc tm of
    37       Some t => one :: dest_sum t
    38     | None =>
    39         (case try dest_plus tm of
    40           Some (t, u) => dest_sum t @ dest_sum u
    41         | None => [tm]));
    42 
    43 
    44 (** generic proof tools **)
    45 
    46 (* prove conversions *)
    47 
    48 val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq;
    49 
    50 fun prove_conv expand_tac norm_tac sg (t, u) =
    51   mk_meta_eq (prove_goalw_cterm [] (cterm_of sg (mk_eqv (t, u)))
    52     (K [expand_tac, norm_tac]))
    53   handle ERROR => error ("The error(s) above occurred while trying to prove " ^
    54     (string_of_cterm (cterm_of sg (mk_eqv (t, u)))));
    55 
    56 val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s"
    57   (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]);
    58 
    59 
    60 (* rewriting *)
    61 
    62 fun simp_all rules = ALLGOALS (simp_tac (HOL_ss addsimps rules));
    63 
    64 val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right];
    65 val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right];
    66 
    67 
    68 
    69 (** cancel common summands **)
    70 
    71 structure Sum =
    72 struct
    73   val mk_sum = mk_sum;
    74   val dest_sum = dest_sum;
    75   val prove_conv = prove_conv;
    76   val norm_tac = simp_all add_rules THEN simp_all add_ac;
    77 end;
    78 
    79 fun gen_uncancel_tac rule ct =
    80   rtac (instantiate' [] [None, Some ct] (rule RS subst_equals)) 1;
    81 
    82 
    83 (* nat eq *)
    84 
    85 structure EqCancelSums = CancelSumsFun
    86 (struct
    87   open Sum;
    88   val mk_bal = HOLogic.mk_eq;
    89   val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
    90   val uncancel_tac = gen_uncancel_tac add_left_cancel;
    91 end);
    92 
    93 
    94 (* nat less *)
    95 
    96 structure LessCancelSums = CancelSumsFun
    97 (struct
    98   open Sum;
    99   val mk_bal = HOLogic.mk_binrel "op <";
   100   val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
   101   val uncancel_tac = gen_uncancel_tac add_left_cancel_less;
   102 end);
   103 
   104 
   105 (* nat le *)
   106 
   107 structure LeCancelSums = CancelSumsFun
   108 (struct
   109   open Sum;
   110   val mk_bal = HOLogic.mk_binrel "op <=";
   111   val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
   112   val uncancel_tac = gen_uncancel_tac add_left_cancel_le;
   113 end);
   114 
   115 
   116 (* nat diff *)
   117 
   118 (* FIXME *)
   119 
   120 
   121 
   122 (** cancel common factor **)
   123 
   124 structure Factor =
   125 struct
   126   val mk_sum = mk_sum;
   127   val dest_sum = dest_sum;
   128   val prove_conv = prove_conv;
   129   val norm_tac = simp_all (add_rules @ mult_rules) THEN simp_all add_ac;
   130 end;
   131 
   132 fun mk_cnat n = cterm_of (sign_of Nat.thy) (HOLogic.mk_nat n);
   133 
   134 fun gen_multiply_tac rule k =
   135   if k > 0 then
   136     rtac (instantiate' [] [None, Some (mk_cnat (k - 1))] (rule RS subst_equals)) 1
   137   else no_tac;
   138 
   139 
   140 (* nat eq *)
   141 
   142 structure EqCancelFactor = CancelFactorFun
   143 (struct
   144   open Factor;
   145   val mk_bal = HOLogic.mk_eq;
   146   val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
   147   val multiply_tac = gen_multiply_tac Suc_mult_cancel1;
   148 end);
   149 
   150 
   151 (* nat less *)
   152 
   153 structure LessCancelFactor = CancelFactorFun
   154 (struct
   155   open Factor;
   156   val mk_bal = HOLogic.mk_binrel "op <";
   157   val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
   158   val multiply_tac = gen_multiply_tac Suc_mult_less_cancel1;
   159 end);
   160 
   161 
   162 (* nat le *)
   163 
   164 structure LeCancelFactor = CancelFactorFun
   165 (struct
   166   open Factor;
   167   val mk_bal = HOLogic.mk_binrel "op <=";
   168   val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
   169   val multiply_tac = gen_multiply_tac Suc_mult_le_cancel1;
   170 end);
   171 
   172 
   173 
   174 (** prepare nat_cancel simprocs **)
   175 
   176 fun prep_pat s = Thm.read_cterm (sign_of Arith.thy) (s, HOLogic.termTVar);
   177 val prep_pats = map prep_pat;
   178 
   179 fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
   180 
   181 val eq_pats = prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"];
   182 val less_pats = prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"];
   183 val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"];
   184 
   185 val nat_cancel = map prep_simproc
   186   [("nateq_cancel_factor", eq_pats, EqCancelFactor.proc),
   187    ("natless_cancel_factor", less_pats, LessCancelFactor.proc),
   188    ("natle_cancel_factor", le_pats, LeCancelFactor.proc),
   189    ("nateq_cancel_sums", eq_pats, EqCancelSums.proc),
   190    ("natless_cancel_sums", less_pats, LessCancelSums.proc),
   191    ("natle_cancel_sums", le_pats, LeCancelSums.proc)];
   192 
   193 
   194 end;