| author | berghofe | 
| Wed, 11 Jul 2007 11:04:39 +0200 | |
| changeset 23740 | d7f18c837ce7 | 
| parent 23530 | 438c5d2db482 | 
| child 23881 | 851c74f1bb69 | 
| permissions | -rw-r--r-- | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 1 | (* Title: HOL/arith_data.ML | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 2 | ID: $Id$ | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 3 | Author: Markus Wenzel, Stefan Berghofer and Tobias Nipkow | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 4 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 5 | Various arithmetic proof procedures. | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 6 | *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 7 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 8 | (*---------------------------------------------------------------------------*) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 9 | (* 1. Cancellation of common terms *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 10 | (*---------------------------------------------------------------------------*) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 11 | |
| 13517 | 12 | structure NatArithUtils = | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 13 | struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 14 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 15 | (** abstract syntax of structure nat: 0, Suc, + **) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 16 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 17 | (* mk_sum, mk_norm_sum *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 18 | |
| 22997 | 19 | val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 20 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 21 | fun mk_sum [] = HOLogic.zero | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 22 | | mk_sum [t] = t | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 23 | | mk_sum (t :: ts) = mk_plus (t, mk_sum ts); | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 24 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 25 | (*normal form of sums: Suc (... (Suc (a + (b + ...))))*) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 26 | fun mk_norm_sum ts = | 
| 21621 | 27 | let val (ones, sums) = List.partition (equal HOLogic.Suc_zero) ts in | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 28 | funpow (length ones) HOLogic.mk_Suc (mk_sum sums) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 29 | end; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 30 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 31 | (* dest_sum *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 32 | |
| 22997 | 33 | val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} HOLogic.natT;
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 34 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 35 | fun dest_sum tm = | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 36 | if HOLogic.is_zero tm then [] | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 37 | else | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 38 | (case try HOLogic.dest_Suc tm of | 
| 21621 | 39 | SOME t => HOLogic.Suc_zero :: dest_sum t | 
| 15531 | 40 | | NONE => | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 41 | (case try dest_plus tm of | 
| 15531 | 42 | SOME (t, u) => dest_sum t @ dest_sum u | 
| 43 | | NONE => [tm])); | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 44 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 45 | (** generic proof tools **) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 46 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 47 | (* prove conversions *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 48 | |
| 20044 
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
 wenzelm parents: 
19823diff
changeset | 49 | fun prove_conv expand_tac norm_tac ss tu = (* FIXME avoid standard *) | 
| 
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
 wenzelm parents: 
19823diff
changeset | 50 | mk_meta_eq (standard (Goal.prove (Simplifier.the_context ss) [] [] | 
| 
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
 wenzelm parents: 
19823diff
changeset | 51 | (HOLogic.mk_Trueprop (HOLogic.mk_eq tu)) | 
| 17989 | 52 | (K (EVERY [expand_tac, norm_tac ss])))); | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 53 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 54 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 55 | (* rewriting *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 56 | |
| 18328 | 57 | fun simp_all_tac rules = | 
| 58 | let val ss0 = HOL_ss addsimps rules | |
| 59 | in fn ss => ALLGOALS (simp_tac (Simplifier.inherit_context ss ss0)) end; | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 60 | |
| 13517 | 61 | fun prep_simproc (name, pats, proc) = | 
| 16834 | 62 | Simplifier.simproc (the_context ()) name pats proc; | 
| 13517 | 63 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 64 | end; (* NatArithUtils *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 65 | |
| 13517 | 66 | |
| 67 | signature ARITH_DATA = | |
| 68 | sig | |
| 69 | val nat_cancel_sums_add: simproc list | |
| 70 | val nat_cancel_sums: simproc list | |
| 71 | end; | |
| 72 | ||
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 73 | |
| 13517 | 74 | structure ArithData: ARITH_DATA = | 
| 75 | struct | |
| 76 | ||
| 77 | open NatArithUtils; | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 78 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 79 | (** cancel common summands **) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 80 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 81 | structure Sum = | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 82 | struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 83 | val mk_sum = mk_norm_sum; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 84 | val dest_sum = dest_sum; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 85 | val prove_conv = prove_conv; | 
| 22838 | 86 |   val norm_tac1 = simp_all_tac [@{thm "add_Suc"}, @{thm "add_Suc_right"},
 | 
| 87 |     @{thm "add_0"}, @{thm "add_0_right"}];
 | |
| 22548 | 88 |   val norm_tac2 = simp_all_tac @{thms add_ac};
 | 
| 18328 | 89 | fun norm_tac ss = norm_tac1 ss THEN norm_tac2 ss; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 90 | end; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 91 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 92 | fun gen_uncancel_tac rule ct = | 
| 22838 | 93 |   rtac (instantiate' [] [NONE, SOME ct] (rule RS @{thm subst_equals})) 1;
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 94 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 95 | (* nat eq *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 96 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 97 | structure EqCancelSums = CancelSumsFun | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 98 | (struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 99 | open Sum; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 100 | val mk_bal = HOLogic.mk_eq; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 101 | val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT; | 
| 22838 | 102 |   val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel"};
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 103 | end); | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 104 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 105 | (* nat less *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 106 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 107 | structure LessCancelSums = CancelSumsFun | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 108 | (struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 109 | open Sum; | 
| 22997 | 110 |   val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less};
 | 
| 111 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT;
 | |
| 22838 | 112 |   val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel_less"};
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 113 | end); | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 114 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 115 | (* nat le *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 116 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 117 | structure LeCancelSums = CancelSumsFun | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 118 | (struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 119 | open Sum; | 
| 22997 | 120 |   val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq};
 | 
| 121 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT;
 | |
| 22838 | 122 |   val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel_le"};
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 123 | end); | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 124 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 125 | (* nat diff *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 126 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 127 | structure DiffCancelSums = CancelSumsFun | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 128 | (struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 129 | open Sum; | 
| 22997 | 130 |   val mk_bal = HOLogic.mk_binop @{const_name HOL.minus};
 | 
| 131 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.minus} HOLogic.natT;
 | |
| 22838 | 132 |   val uncancel_tac = gen_uncancel_tac @{thm "diff_cancel"};
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 133 | end); | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 134 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 135 | (** prepare nat_cancel simprocs **) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 136 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 137 | val nat_cancel_sums_add = map prep_simproc | 
| 13462 | 138 |   [("nateq_cancel_sums",
 | 
| 20268 | 139 | ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"], | 
| 140 | K EqCancelSums.proc), | |
| 13462 | 141 |    ("natless_cancel_sums",
 | 
| 20268 | 142 | ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"], | 
| 143 | K LessCancelSums.proc), | |
| 13462 | 144 |    ("natle_cancel_sums",
 | 
| 20268 | 145 | ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"], | 
| 146 | K LeCancelSums.proc)]; | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 147 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 148 | val nat_cancel_sums = nat_cancel_sums_add @ | 
| 13462 | 149 |   [prep_simproc ("natdiff_cancel_sums",
 | 
| 20268 | 150 | ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"], | 
| 151 | K DiffCancelSums.proc)]; | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 152 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 153 | end; (* ArithData *) | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 154 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 155 | open ArithData; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 156 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 157 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 158 | (*---------------------------------------------------------------------------*) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 159 | (* 2. Linear arithmetic *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 160 | (*---------------------------------------------------------------------------*) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 161 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 162 | (* Parameters data for general linear arithmetic functor *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 163 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 164 | structure LA_Logic: LIN_ARITH_LOGIC = | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 165 | struct | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 166 | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 167 | val ccontr = ccontr; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 168 | val conjI = conjI; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 169 | val notI = notI; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 170 | val sym = sym; | 
| 22548 | 171 | val not_lessD = @{thm linorder_not_less} RS iffD1;
 | 
| 172 | val not_leD = @{thm linorder_not_le} RS iffD1;
 | |
| 21243 | 173 | val le0 = thm "le0"; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 174 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 175 | fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI); | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 176 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 177 | val mk_Trueprop = HOLogic.mk_Trueprop; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 178 | |
| 16733 
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
 nipkow parents: 
16485diff
changeset | 179 | fun atomize thm = case #prop(rep_thm thm) of | 
| 
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
 nipkow parents: 
16485diff
changeset | 180 |     Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
 | 
| 
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
 nipkow parents: 
16485diff
changeset | 181 | atomize(thm RS conjunct1) @ atomize(thm RS conjunct2) | 
| 
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
 nipkow parents: 
16485diff
changeset | 182 | | _ => [thm]; | 
| 
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
 nipkow parents: 
16485diff
changeset | 183 | |
| 23190 | 184 | fun neg_prop ((TP as Const("Trueprop",_)) $ (Const("Not",_) $ t)) = TP $ t
 | 
| 185 |   | neg_prop ((TP as Const("Trueprop",_)) $ t) = TP $ (HOLogic.Not $t)
 | |
| 186 |   | neg_prop t = raise TERM ("neg_prop", [t]);
 | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 187 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 188 | fun is_False thm = | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 189 | let val _ $ t = #prop(rep_thm thm) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 190 |   in t = Const("False",HOLogic.boolT) end;
 | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 191 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 192 | fun is_nat(t) = fastype_of1 t = HOLogic.natT; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 193 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 194 | fun mk_nat_thm sg t = | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 195 |   let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
 | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 196 | in instantiate ([],[(cn,ct)]) le0 end; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 197 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 198 | end; (* LA_Logic *) | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 199 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 200 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 201 | (* arith theory data *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 202 | |
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 203 | datatype arithtactic = ArithTactic of {name: string, tactic: int -> tactic, id: stamp};
 | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 204 | |
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 205 | fun mk_arith_tactic name tactic = ArithTactic {name = name, tactic = tactic, id = stamp ()};
 | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 206 | |
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 207 | fun eq_arith_tactic (ArithTactic {id = id1, ...}, ArithTactic {id = id2, ...}) = (id1 = id2);
 | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 208 | |
| 16424 | 209 | structure ArithTheoryData = TheoryDataFun | 
| 22846 | 210 | ( | 
| 20268 | 211 |   type T = {splits: thm list,
 | 
| 212 | inj_consts: (string * typ) list, | |
| 213 | discrete: string list, | |
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 214 | tactics: arithtactic list}; | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 215 |   val empty = {splits = [], inj_consts = [], discrete = [], tactics = []};
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 216 | val copy = I; | 
| 16424 | 217 | val extend = I; | 
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 218 |   fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, tactics= tactics1},
 | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 219 |              {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, tactics= tactics2}) =
 | 
| 22634 | 220 |    {splits = Library.merge Thm.eq_thm_prop (splits1, splits2),
 | 
| 221 | inj_consts = Library.merge (op =) (inj_consts1, inj_consts2), | |
| 222 | discrete = Library.merge (op =) (discrete1, discrete2), | |
| 223 | tactics = Library.merge eq_arith_tactic (tactics1, tactics2)}; | |
| 22846 | 224 | ); | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 225 | |
| 18728 | 226 | val arith_split_add = Thm.declaration_attribute (fn thm => | 
| 20897 | 227 |   Context.mapping (ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
 | 
| 22634 | 228 |     {splits= insert Thm.eq_thm_prop thm splits, inj_consts= inj_consts, discrete= discrete, tactics= tactics})) I);
 | 
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 229 | |
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 230 | fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
 | 
| 22634 | 231 |   {splits = splits, inj_consts = inj_consts, discrete = insert (op =) d discrete, tactics= tactics});
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 232 | |
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 233 | fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
 | 
| 22634 | 234 |   {splits = splits, inj_consts = insert (op =) c inj_consts, discrete = discrete, tactics= tactics});
 | 
| 10574 
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
 nipkow parents: 
10516diff
changeset | 235 | |
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 236 | fun arith_tactic_add tac = ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
 | 
| 22634 | 237 |   {splits= splits, inj_consts= inj_consts, discrete= discrete, tactics= insert eq_arith_tactic tac tactics});
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 238 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 239 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 240 | signature HOL_LIN_ARITH_DATA = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 241 | sig | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 242 | include LIN_ARITH_DATA | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 243 | val fast_arith_split_limit : int ref | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 244 | end; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 245 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 246 | structure LA_Data_Ref: HOL_LIN_ARITH_DATA = | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 247 | struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 248 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 249 | (* internal representation of linear (in-)equations *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 250 | type decompT = ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool); | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 251 | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 252 | (* Decomposition of terms *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 253 | |
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 254 | fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
 | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 255 | | nT _ = false; | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 256 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 257 | fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) : | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 258 | (term * Rat.rat) list * Rat.rat = | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 259 | case AList.lookup (op =) p t of NONE => ((t, m) :: p, i) | 
| 22947 | 260 | | SOME n => (AList.update (op =) (t, Rat.add n m) p, i); | 
| 10693 | 261 | |
| 262 | exception Zero; | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 263 | |
| 21820 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 264 | fun rat_of_term (numt, dent) = | 
| 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 265 | let | 
| 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 266 | val num = HOLogic.dest_numeral numt | 
| 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 267 | val den = HOLogic.dest_numeral dent | 
| 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 268 | in | 
| 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 269 | if den = 0 then raise Zero else Rat.rat_of_quotient (num, den) | 
| 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 270 | end; | 
| 10718 | 271 | |
| 272 | (* Warning: in rare cases number_of encloses a non-numeral, | |
| 21820 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 273 | in which case dest_numeral raises TERM; hence all the handles below. | 
| 11334 
a16eaf2a1edd
Allow Suc-numerals as coefficients in lin-arith formulae
 nipkow parents: 
10906diff
changeset | 274 | Same for Suc-terms that turn out not to be numerals - | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 275 | although the simplifier should eliminate those anyway ... | 
| 10718 | 276 | *) | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 277 | fun number_of_Sucs (Const ("Suc", _) $ n) : int =
 | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 278 | number_of_Sucs n + 1 | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 279 | | number_of_Sucs t = | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 280 |       if HOLogic.is_zero t then 0 else raise TERM ("number_of_Sucs", []);
 | 
| 10718 | 281 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 282 | (* decompose nested multiplications, bracketing them to the right and combining | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 283 | all their coefficients | 
| 10718 | 284 | *) | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 285 | fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat = | 
| 13499 | 286 | let | 
| 22997 | 287 |   fun demult ((mC as Const (@{const_name HOL.times}, _)) $ s $ t, m) = (
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 288 | (case s of | 
| 22997 | 289 |       Const ("Numeral.number_class.number_of", _) $ n =>
 | 
| 22947 | 290 | demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n))) | 
| 22997 | 291 |     | Const (@{const_name HOL.uminus}, _) $ (Const ("Numeral.number_class.number_of", _) $ n) =>
 | 
| 22947 | 292 | demult (t, Rat.mult m (Rat.rat_of_int (~(HOLogic.dest_numeral n)))) | 
| 22997 | 293 |     | Const (@{const_name Suc}, _) $ _ =>
 | 
| 22947 | 294 | demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat s))) | 
| 22997 | 295 |     | Const (@{const_name HOL.times}, _) $ s1 $ s2 =>
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 296 | demult (mC $ s1 $ (mC $ s2 $ t), m) | 
| 22997 | 297 |     | Const (@{const_name HOL.divide}, _) $ numt $ (Const ("Numeral.number_class.number_of", _) $ dent) =>
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 298 | let | 
| 21820 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 299 | val den = HOLogic.dest_numeral dent | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 300 | in | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 301 | if den = 0 then | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 302 | raise Zero | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 303 | else | 
| 22947 | 304 | demult (mC $ numt $ t, Rat.mult m (Rat.inv (Rat.rat_of_int den))) | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 305 | end | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 306 | | _ => | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 307 | atomult (mC, s, t, m) | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 308 | ) handle TERM _ => atomult (mC, s, t, m) | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 309 | ) | 
| 22997 | 310 |     | demult (atom as Const(@{const_name HOL.divide}, _) $ t $ (Const ("Numeral.number_class.number_of", _) $ dent), m) =
 | 
| 20268 | 311 | (let | 
| 21820 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 312 | val den = HOLogic.dest_numeral dent | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 313 | in | 
| 20268 | 314 | if den = 0 then | 
| 315 | raise Zero | |
| 316 | else | |
| 22947 | 317 | demult (t, Rat.mult m (Rat.inv (Rat.rat_of_int den))) | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 318 | end | 
| 20268 | 319 | handle TERM _ => (SOME atom, m)) | 
| 22997 | 320 |     | demult (Const (@{const_name HOL.zero}, _), m) = (NONE, Rat.zero)
 | 
| 321 |     | demult (Const (@{const_name HOL.one}, _), m) = (NONE, m)
 | |
| 322 |     | demult (t as Const ("Numeral.number_class.number_of", _) $ n, m) =
 | |
| 22947 | 323 | ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n))) | 
| 324 | handle TERM _ => (SOME t, m)) | |
| 22997 | 325 |     | demult (Const (@{const_name HOL.uminus}, _) $ t, m) = demult (t, Rat.neg m)
 | 
| 20268 | 326 | | demult (t as Const f $ x, m) = | 
| 22997 | 327 | (if member (op =) inj_consts f then SOME x else SOME t, m) | 
| 20268 | 328 | | demult (atom, m) = (SOME atom, m) | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 329 | and | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 330 | atomult (mC, atom, t, m) = ( | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 331 | case demult (t, m) of (NONE, m') => (SOME atom, m') | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 332 | | (SOME t', m') => (SOME (mC $ atom $ t'), m') | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 333 | ) | 
| 13499 | 334 | in demult end; | 
| 10718 | 335 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 336 | fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) : | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 337 | ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option = | 
| 10574 
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
 nipkow parents: 
10516diff
changeset | 338 | let | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 339 | (* Turn term into list of summand * multiplicity plus a constant *) | 
| 22997 | 340 |   fun poly (Const (@{const_name HOL.plus}, _) $ s $ t, m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) =
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 341 | poly (s, m, poly (t, m, pi)) | 
| 22997 | 342 |     | poly (all as Const (@{const_name HOL.minus}, T) $ s $ t, m, pi) =
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 343 | if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi)) | 
| 22997 | 344 |     | poly (all as Const (@{const_name HOL.uminus}, T) $ t, m, pi) =
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 345 | if nT T then add_atom all m pi else poly (t, Rat.neg m, pi) | 
| 22997 | 346 |     | poly (Const (@{const_name HOL.zero}, _), _, pi) =
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 347 | pi | 
| 22997 | 348 |     | poly (Const (@{const_name HOL.one}, _), m, (p, i)) =
 | 
| 22947 | 349 | (p, Rat.add i m) | 
| 22997 | 350 |     | poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
 | 
| 22947 | 351 | poly (t, m, (p, Rat.add i m)) | 
| 22997 | 352 |     | poly (all as Const (@{const_name HOL.times}, _) $ _ $ _, m, pi as (p, i)) =
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 353 | (case demult inj_consts (all, m) of | 
| 22947 | 354 | (NONE, m') => (p, Rat.add i m') | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 355 | | (SOME u, m') => add_atom u m' pi) | 
| 22997 | 356 |     | poly (all as Const (@{const_name HOL.divide}, _) $ _ $ _, m, pi as (p, i)) =
 | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 357 | (case demult inj_consts (all, m) of | 
| 22947 | 358 | (NONE, m') => (p, Rat.add i m') | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 359 | | (SOME u, m') => add_atom u m' pi) | 
| 22997 | 360 |     | poly (all as Const ("Numeral.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
 | 
| 21820 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 361 | (let val k = HOLogic.dest_numeral t | 
| 20859 | 362 | val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k | 
| 22947 | 363 | in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end | 
| 20859 | 364 | handle TERM _ => add_atom all m pi) | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 365 | | poly (all as Const f $ x, m, pi) = | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 366 | if f mem inj_consts then poly (x, m, pi) else add_atom all m pi | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 367 | | poly (all, m, pi) = | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 368 | add_atom all m pi | 
| 22947 | 369 | val (p, i) = poly (lhs, Rat.one, ([], Rat.zero)) | 
| 370 | val (q, j) = poly (rhs, Rat.one, ([], Rat.zero)) | |
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 371 | in | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 372 | case rel of | 
| 22997 | 373 |     @{const_name Orderings.less}    => SOME (p, i, "<", q, j)
 | 
| 374 |   | @{const_name Orderings.less_eq} => SOME (p, i, "<=", q, j)
 | |
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 375 | | "op =" => SOME (p, i, "=", q, j) | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 376 | | _ => NONE | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 377 | end handle Zero => NONE; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 378 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 379 | fun of_lin_arith_sort sg (U : typ) : bool = | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 380 | Type.of_sort (Sign.tsig_of sg) (U, ["Ring_and_Field.ordered_idom"]) | 
| 15121 
1198032bad25
Initial changes to extend arithmetic from individual types to type classes.
 nipkow parents: 
14738diff
changeset | 381 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 382 | fun allows_lin_arith sg (discrete : string list) (U as Type (D, [])) : bool * bool = | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 383 | if of_lin_arith_sort sg U then | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 384 | (true, D mem discrete) | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 385 | else (* special cases *) | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 386 | if D mem discrete then (true, true) else (false, false) | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 387 | | allows_lin_arith sg discrete U = | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 388 | (of_lin_arith_sort sg U, false); | 
| 15121 
1198032bad25
Initial changes to extend arithmetic from individual types to type classes.
 nipkow parents: 
14738diff
changeset | 389 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 390 | fun decomp_typecheck (sg, discrete, inj_consts) (T : typ, xxx) : decompT option = | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 391 | case T of | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 392 |     Type ("fun", [U, _]) =>
 | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 393 | (case allows_lin_arith sg discrete U of | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 394 | (true, d) => | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 395 | (case decomp0 inj_consts xxx of | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 396 | NONE => NONE | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 397 | | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d)) | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 398 | | (false, _) => | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 399 | NONE) | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 400 | | _ => NONE; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 401 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 402 | fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d) | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 403 | | negate NONE = NONE; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 404 | |
| 23190 | 405 | fun decomp_negation data | 
| 406 |   ((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decompT option =
 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 407 | decomp_typecheck data (T, (rel, lhs, rhs)) | 
| 23190 | 408 |   | decomp_negation data ((Const ("Trueprop", _)) $
 | 
| 409 |   (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
 | |
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 410 | negate (decomp_typecheck data (T, (rel, lhs, rhs))) | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 411 | | decomp_negation data _ = | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 412 | NONE; | 
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 413 | |
| 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 414 | fun decomp sg : term -> decompT option = | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 415 | let | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 416 |   val {discrete, inj_consts, ...} = ArithTheoryData.get sg
 | 
| 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 417 | in | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 418 | decomp_negation (sg, discrete, inj_consts) | 
| 20254 
58b71535ed00
lin_arith_prover splits certain operators (e.g. min, max, abs)
 webertj parents: 
20217diff
changeset | 419 | end; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 420 | |
| 20276 
d94dc40673b1
possible disagreement between proof search and proof reconstruction when eliminating inequalities over different types fixed
 webertj parents: 
20271diff
changeset | 421 | fun domain_is_nat (_ $ (Const (_, T) $ _ $ _)) = nT T | 
| 
d94dc40673b1
possible disagreement between proof search and proof reconstruction when eliminating inequalities over different types fixed
 webertj parents: 
20271diff
changeset | 422 |   | domain_is_nat (_ $ (Const ("Not", _) $ (Const (_, T) $ _ $ _))) = nT T
 | 
| 
d94dc40673b1
possible disagreement between proof search and proof reconstruction when eliminating inequalities over different types fixed
 webertj parents: 
20271diff
changeset | 423 | | domain_is_nat _ = false; | 
| 
d94dc40673b1
possible disagreement between proof search and proof reconstruction when eliminating inequalities over different types fixed
 webertj parents: 
20271diff
changeset | 424 | |
| 21820 
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
 haftmann parents: 
21621diff
changeset | 425 | fun number_of (n, T) = HOLogic.mk_number T n; | 
| 10693 | 426 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 427 | (*---------------------------------------------------------------------------*) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 428 | (* code that performs certain goal transformations for linear arithmetic *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 429 | (*---------------------------------------------------------------------------*) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 430 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 431 | (* A "do nothing" variant of pre_decomp and pre_tac: | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 432 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 433 | fun pre_decomp sg Ts termitems = [termitems]; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 434 | fun pre_tac i = all_tac; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 435 | *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 436 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 437 | (*---------------------------------------------------------------------------*) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 438 | (* the following code performs splitting of certain constants (e.g. min, *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 439 | (* max) in a linear arithmetic problem; similar to what split_tac later does *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 440 | (* to the proof state *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 441 | (*---------------------------------------------------------------------------*) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 442 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 443 | val fast_arith_split_limit = ref 9; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 444 | |
| 20268 | 445 | (* checks if splitting with 'thm' is implemented *) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 446 | |
| 20268 | 447 | fun is_split_thm (thm : thm) : bool = | 
| 448 | case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => ( | |
| 449 | (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *) | |
| 450 | case head_of lhs of | |
| 22997 | 451 |       Const (a, _) => member (op =) [@{const_name Orderings.max},
 | 
| 452 |                                     @{const_name Orderings.min},
 | |
| 453 |                                     @{const_name HOL.abs},
 | |
| 454 |                                     @{const_name HOL.minus},
 | |
| 20268 | 455 | "IntDef.nat", | 
| 22997 | 456 | "Divides.div_class.mod", | 
| 457 | "Divides.div_class.div"] a | |
| 20268 | 458 |     | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
 | 
| 459 | Display.string_of_thm thm); | |
| 460 | false)) | |
| 461 |   | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
 | |
| 462 | Display.string_of_thm thm); | |
| 463 | false); | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 464 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 465 | (* substitute new for occurrences of old in a term, incrementing bound *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 466 | (* variables as needed when substituting inside an abstraction *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 467 | |
| 20268 | 468 | fun subst_term ([] : (term * term) list) (t : term) = t | 
| 469 | | subst_term pairs t = | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 470 | (case AList.lookup (op aconv) pairs t of | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 471 | SOME new => | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 472 | new | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 473 | | NONE => | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 474 | (case t of Abs (a, T, body) => | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 475 | let val pairs' = map (pairself (incr_boundvars 1)) pairs | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 476 | in Abs (a, T, subst_term pairs' body) end | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 477 | | t1 $ t2 => | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 478 | subst_term pairs t1 $ subst_term pairs t2 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 479 | | _ => t)); | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 480 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 481 | (* approximates the effect of one application of split_tac (followed by NNF *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 482 | (* normalization) on the subgoal represented by '(Ts, terms)'; returns a *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 483 | (* list of new subgoals (each again represented by a typ list for bound *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 484 | (* variables and a term list for premises), or NONE if split_tac would fail *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 485 | (* on the subgoal *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 486 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 487 | (* FIXME: currently only the effect of certain split theorems is reproduced *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 488 | (* (which is why we need 'is_split_thm'). A more canonical *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 489 | (* implementation should analyze the right-hand side of the split *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 490 | (* theorem that can be applied, and modify the subgoal accordingly. *) | 
| 20268 | 491 | (* Or even better, the splitter should be extended to provide *) | 
| 492 | (* splitting on terms as well as splitting on theorems (where the *) | |
| 493 | (* former can have a faster implementation as it does not need to be *) | |
| 494 | (* proof-producing). *) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 495 | |
| 20268 | 496 | fun split_once_items (sg : theory) (Ts : typ list, terms : term list) : | 
| 497 | (typ list * term list) list option = | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 498 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 499 | (* takes a list [t1, ..., tn] to the term *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 500 | (* tn' --> ... --> t1' --> False , *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 501 | (* where ti' = HOLogic.dest_Trueprop ti *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 502 | (* term list -> term *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 503 | fun REPEAT_DETERM_etac_rev_mp terms' = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 504 | fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop terms') HOLogic.false_const | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 505 | val split_thms = filter is_split_thm (#splits (ArithTheoryData.get sg)) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 506 | val cmap = Splitter.cmap_of_split_thms split_thms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 507 | val splits = Splitter.split_posns cmap sg Ts (REPEAT_DETERM_etac_rev_mp terms) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 508 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 509 | if length splits > !fast_arith_split_limit then ( | 
| 20268 | 510 |     tracing ("fast_arith_split_limit exceeded (current value is " ^
 | 
| 511 | string_of_int (!fast_arith_split_limit) ^ ")"); | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 512 | NONE | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 513 | ) else ( | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 514 | case splits of [] => | 
| 20268 | 515 | (* split_tac would fail: no possible split *) | 
| 516 | NONE | |
| 517 | | ((_, _, _, split_type, split_term) :: _) => ( | |
| 518 | (* ignore all but the first possible split *) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 519 | case strip_comb split_term of | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 520 | (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *) | 
| 22997 | 521 |       (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 522 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 523 | val rev_terms = rev terms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 524 | val terms1 = map (subst_term [(split_term, t1)]) rev_terms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 525 | val terms2 = map (subst_term [(split_term, t2)]) rev_terms | 
| 22997 | 526 |         val t1_leq_t2     = Const (@{const_name Orderings.less_eq},
 | 
| 20268 | 527 | split_type --> split_type --> HOLogic.boolT) $ t1 $ t2 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 528 | val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 529 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 530 | val subgoal1 = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 531 | val subgoal2 = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 532 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 533 | SOME [(Ts, subgoal1), (Ts, subgoal2)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 534 | end | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 535 | (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *) | 
| 22997 | 536 |     | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 537 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 538 | val rev_terms = rev terms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 539 | val terms1 = map (subst_term [(split_term, t1)]) rev_terms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 540 | val terms2 = map (subst_term [(split_term, t2)]) rev_terms | 
| 22997 | 541 |         val t1_leq_t2     = Const (@{const_name Orderings.less_eq},
 | 
| 20268 | 542 | split_type --> split_type --> HOLogic.boolT) $ t1 $ t2 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 543 | val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 544 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 545 | val subgoal1 = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 546 | val subgoal2 = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 547 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 548 | SOME [(Ts, subgoal1), (Ts, subgoal2)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 549 | end | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 550 | (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *) | 
| 22997 | 551 |     | (Const (@{const_name HOL.abs}, _), [t1]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 552 | let | 
| 20268 | 553 | val rev_terms = rev terms | 
| 554 | val terms1 = map (subst_term [(split_term, t1)]) rev_terms | |
| 22997 | 555 |         val terms2      = map (subst_term [(split_term, Const (@{const_name HOL.uminus},
 | 
| 20268 | 556 | split_type --> split_type) $ t1)]) rev_terms | 
| 22997 | 557 |         val zero        = Const (@{const_name HOL.zero}, split_type)
 | 
| 558 |         val zero_leq_t1 = Const (@{const_name Orderings.less_eq},
 | |
| 20268 | 559 | split_type --> split_type --> HOLogic.boolT) $ zero $ t1 | 
| 22997 | 560 |         val t1_lt_zero  = Const (@{const_name Orderings.less},
 | 
| 20268 | 561 | split_type --> split_type --> HOLogic.boolT) $ t1 $ zero | 
| 562 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | |
| 563 | val subgoal1 = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false] | |
| 564 | val subgoal2 = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false] | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 565 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 566 | SOME [(Ts, subgoal1), (Ts, subgoal2)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 567 | end | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 568 | (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *) | 
| 22997 | 569 |     | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 570 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 571 | (* "d" in the above theorem becomes a new bound variable after NNF *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 572 | (* transformation, therefore some adjustment of indices is necessary *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 573 | val rev_terms = rev terms | 
| 22997 | 574 |         val zero            = Const (@{const_name HOL.zero}, split_type)
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 575 | val d = Bound 0 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 576 | val terms1 = map (subst_term [(split_term, zero)]) rev_terms | 
| 20268 | 577 | val terms2 = map (subst_term [(incr_boundvars 1 split_term, d)]) | 
| 578 | (map (incr_boundvars 1) rev_terms) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 579 | val t1' = incr_boundvars 1 t1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 580 | val t2' = incr_boundvars 1 t2 | 
| 22997 | 581 |         val t1_lt_t2        = Const (@{const_name Orderings.less},
 | 
| 20268 | 582 | split_type --> split_type --> HOLogic.boolT) $ t1 $ t2 | 
| 583 |         val t1_eq_t2_plus_d = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
 | |
| 22997 | 584 |                                 (Const (@{const_name HOL.plus},
 | 
| 20268 | 585 | split_type --> split_type --> split_type) $ t2' $ d) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 586 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 587 | val subgoal1 = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 588 | val subgoal2 = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 589 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 590 | SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 591 | end | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 592 | (* ?P (nat ?i) = ((ALL n. ?i = int n --> ?P n) & (?i < 0 --> ?P 0)) *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 593 |     | (Const ("IntDef.nat", _), [t1]) =>
 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 594 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 595 | val rev_terms = rev terms | 
| 22997 | 596 |         val zero_int    = Const (@{const_name HOL.zero}, HOLogic.intT)
 | 
| 597 |         val zero_nat    = Const (@{const_name HOL.zero}, HOLogic.natT)
 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 598 | val n = Bound 0 | 
| 20268 | 599 | val terms1 = map (subst_term [(incr_boundvars 1 split_term, n)]) | 
| 600 | (map (incr_boundvars 1) rev_terms) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 601 | val terms2 = map (subst_term [(split_term, zero_nat)]) rev_terms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 602 | val t1' = incr_boundvars 1 t1 | 
| 20268 | 603 |         val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
 | 
| 23365 | 604 |                             (Const ("Nat.of_nat", HOLogic.natT --> HOLogic.intT) $ n)
 | 
| 22997 | 605 |         val t1_lt_zero  = Const (@{const_name Orderings.less},
 | 
| 20268 | 606 | HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 607 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 608 | val subgoal1 = (HOLogic.mk_Trueprop t1_eq_int_n) :: terms1 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 609 | val subgoal2 = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 610 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 611 | SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 612 | end | 
| 20268 | 613 | (* "?P ((?n::nat) mod (number_of ?k)) = | 
| 614 | ((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) --> | |
| 615 | (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *) | |
| 22997 | 616 |     | (Const ("Divides.div_class.mod", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 617 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 618 | val rev_terms = rev terms | 
| 22997 | 619 |         val zero                    = Const (@{const_name HOL.zero}, split_type)
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 620 | val i = Bound 1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 621 | val j = Bound 0 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 622 | val terms1 = map (subst_term [(split_term, t1)]) rev_terms | 
| 20268 | 623 | val terms2 = map (subst_term [(incr_boundvars 2 split_term, j)]) | 
| 624 | (map (incr_boundvars 2) rev_terms) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 625 | val t1' = incr_boundvars 2 t1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 626 | val t2' = incr_boundvars 2 t2 | 
| 20268 | 627 |         val t2_eq_zero              = Const ("op =",
 | 
| 628 | split_type --> split_type --> HOLogic.boolT) $ t2 $ zero | |
| 629 |         val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
 | |
| 630 | split_type --> split_type --> HOLogic.boolT) $ t2' $ zero) | |
| 22997 | 631 |         val j_lt_t2                 = Const (@{const_name Orderings.less},
 | 
| 20268 | 632 | split_type --> split_type--> HOLogic.boolT) $ j $ t2' | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 633 |         val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
 | 
| 22997 | 634 |                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
 | 
| 635 |                                          (Const (@{const_name HOL.times},
 | |
| 20268 | 636 | split_type --> split_type --> split_type) $ t2' $ i) $ j) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 637 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 638 | val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false] | 
| 20268 | 639 | val subgoal2 = (map HOLogic.mk_Trueprop | 
| 640 | [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j]) | |
| 641 | @ terms2 @ [not_false] | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 642 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 643 | SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 644 | end | 
| 20268 | 645 | (* "?P ((?n::nat) div (number_of ?k)) = | 
| 646 | ((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) --> | |
| 647 | (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *) | |
| 22997 | 648 |     | (Const ("Divides.div_class.div", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 649 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 650 | val rev_terms = rev terms | 
| 22997 | 651 |         val zero                    = Const (@{const_name HOL.zero}, split_type)
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 652 | val i = Bound 1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 653 | val j = Bound 0 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 654 | val terms1 = map (subst_term [(split_term, zero)]) rev_terms | 
| 20268 | 655 | val terms2 = map (subst_term [(incr_boundvars 2 split_term, i)]) | 
| 656 | (map (incr_boundvars 2) rev_terms) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 657 | val t1' = incr_boundvars 2 t1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 658 | val t2' = incr_boundvars 2 t2 | 
| 20268 | 659 |         val t2_eq_zero              = Const ("op =",
 | 
| 660 | split_type --> split_type --> HOLogic.boolT) $ t2 $ zero | |
| 661 |         val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
 | |
| 662 | split_type --> split_type --> HOLogic.boolT) $ t2' $ zero) | |
| 22997 | 663 |         val j_lt_t2                 = Const (@{const_name Orderings.less},
 | 
| 20268 | 664 | split_type --> split_type--> HOLogic.boolT) $ j $ t2' | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 665 |         val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
 | 
| 22997 | 666 |                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
 | 
| 667 |                                          (Const (@{const_name HOL.times},
 | |
| 20268 | 668 | split_type --> split_type --> split_type) $ t2' $ i) $ j) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 669 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 670 | val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false] | 
| 20268 | 671 | val subgoal2 = (map HOLogic.mk_Trueprop | 
| 672 | [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j]) | |
| 673 | @ terms2 @ [not_false] | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 674 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 675 | SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 676 | end | 
| 20268 | 677 | (* "?P ((?n::int) mod (number_of ?k)) = | 
| 678 | ((iszero (number_of ?k) --> ?P ?n) & | |
| 20485 | 679 | (neg (number_of (uminus ?k)) --> | 
| 20268 | 680 | (ALL i j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) & | 
| 681 | (neg (number_of ?k) --> | |
| 682 | (ALL i j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *) | |
| 22997 | 683 |     | (Const ("Divides.div_class.mod",
 | 
| 20268 | 684 |         Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 685 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 686 | val rev_terms = rev terms | 
| 22997 | 687 |         val zero                    = Const (@{const_name HOL.zero}, split_type)
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 688 | val i = Bound 1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 689 | val j = Bound 0 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 690 | val terms1 = map (subst_term [(split_term, t1)]) rev_terms | 
| 20268 | 691 | val terms2_3 = map (subst_term [(incr_boundvars 2 split_term, j)]) | 
| 692 | (map (incr_boundvars 2) rev_terms) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 693 | val t1' = incr_boundvars 2 t1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 694 | val (t2' as (_ $ k')) = incr_boundvars 2 t2 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 695 |         val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 696 |         val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
 | 
| 20268 | 697 | (number_of $ | 
| 22997 | 698 |                                           (Const (@{const_name HOL.uminus},
 | 
| 20485 | 699 | HOLogic.intT --> HOLogic.intT) $ k')) | 
| 22997 | 700 |         val zero_leq_j              = Const (@{const_name Orderings.less_eq},
 | 
| 20268 | 701 | split_type --> split_type --> HOLogic.boolT) $ zero $ j | 
| 22997 | 702 |         val j_lt_t2                 = Const (@{const_name Orderings.less},
 | 
| 20268 | 703 | split_type --> split_type--> HOLogic.boolT) $ j $ t2' | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 704 |         val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
 | 
| 22997 | 705 |                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
 | 
| 706 |                                          (Const (@{const_name HOL.times},
 | |
| 20268 | 707 | split_type --> split_type --> split_type) $ t2' $ i) $ j) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 708 |         val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
 | 
| 22997 | 709 |         val t2_lt_j                 = Const (@{const_name Orderings.less},
 | 
| 20268 | 710 | split_type --> split_type--> HOLogic.boolT) $ t2' $ j | 
| 22997 | 711 |         val j_leq_zero              = Const (@{const_name Orderings.less_eq},
 | 
| 20268 | 712 | split_type --> split_type --> HOLogic.boolT) $ j $ zero | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 713 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 714 | val subgoal1 = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 715 | val subgoal2 = (map HOLogic.mk_Trueprop [neg_minus_k, zero_leq_j]) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 716 | @ hd terms2_3 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 717 | :: (if tl terms2_3 = [] then [not_false] else []) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 718 | @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j]) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 719 | @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false]) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 720 | val subgoal3 = (map HOLogic.mk_Trueprop [neg_t2, t2_lt_j]) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 721 | @ hd terms2_3 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 722 | :: (if tl terms2_3 = [] then [not_false] else []) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 723 | @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j]) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 724 | @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false]) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 725 | val Ts' = split_type :: split_type :: Ts | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 726 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 727 | SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 728 | end | 
| 20268 | 729 | (* "?P ((?n::int) div (number_of ?k)) = | 
| 730 | ((iszero (number_of ?k) --> ?P 0) & | |
| 20485 | 731 | (neg (number_of (uminus ?k)) --> | 
| 20268 | 732 | (ALL i. (EX j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j) --> ?P i)) & | 
| 733 | (neg (number_of ?k) --> | |
| 734 | (ALL i. (EX j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j) --> ?P i))) *) | |
| 22997 | 735 |     | (Const ("Divides.div_class.div",
 | 
| 20268 | 736 |         Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 737 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 738 | val rev_terms = rev terms | 
| 22997 | 739 |         val zero                    = Const (@{const_name HOL.zero}, split_type)
 | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 740 | val i = Bound 1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 741 | val j = Bound 0 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 742 | val terms1 = map (subst_term [(split_term, zero)]) rev_terms | 
| 20268 | 743 | val terms2_3 = map (subst_term [(incr_boundvars 2 split_term, i)]) | 
| 744 | (map (incr_boundvars 2) rev_terms) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 745 | val t1' = incr_boundvars 2 t1 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 746 | val (t2' as (_ $ k')) = incr_boundvars 2 t2 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 747 |         val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 748 |         val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
 | 
| 20268 | 749 | (number_of $ | 
| 22997 | 750 |                                           (Const (@{const_name HOL.uminus},
 | 
| 20485 | 751 | HOLogic.intT --> HOLogic.intT) $ k')) | 
| 22997 | 752 |         val zero_leq_j              = Const (@{const_name Orderings.less_eq},
 | 
| 20268 | 753 | split_type --> split_type --> HOLogic.boolT) $ zero $ j | 
| 22997 | 754 |         val j_lt_t2                 = Const (@{const_name Orderings.less},
 | 
| 20268 | 755 | split_type --> split_type--> HOLogic.boolT) $ j $ t2' | 
| 756 |         val t1_eq_t2_times_i_plus_j = Const ("op =",
 | |
| 757 | split_type --> split_type --> HOLogic.boolT) $ t1' $ | |
| 22997 | 758 |                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
 | 
| 759 |                                          (Const (@{const_name HOL.times},
 | |
| 20268 | 760 | split_type --> split_type --> split_type) $ t2' $ i) $ j) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 761 |         val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
 | 
| 22997 | 762 |         val t2_lt_j                 = Const (@{const_name Orderings.less},
 | 
| 20268 | 763 | split_type --> split_type--> HOLogic.boolT) $ t2' $ j | 
| 22997 | 764 |         val j_leq_zero              = Const (@{const_name Orderings.less_eq},
 | 
| 20268 | 765 | split_type --> split_type --> HOLogic.boolT) $ j $ zero | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 766 | val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 767 | val subgoal1 = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 768 | val subgoal2 = (HOLogic.mk_Trueprop neg_minus_k) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 769 | :: terms2_3 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 770 | @ not_false | 
| 20268 | 771 | :: (map HOLogic.mk_Trueprop | 
| 772 | [zero_leq_j, j_lt_t2, t1_eq_t2_times_i_plus_j]) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 773 | val subgoal3 = (HOLogic.mk_Trueprop neg_t2) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 774 | :: terms2_3 | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 775 | @ not_false | 
| 20268 | 776 | :: (map HOLogic.mk_Trueprop | 
| 777 | [t2_lt_j, j_leq_zero, t1_eq_t2_times_i_plus_j]) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 778 | val Ts' = split_type :: split_type :: Ts | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 779 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 780 | SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 781 | end | 
| 20268 | 782 | (* this will only happen if a split theorem can be applied for which no *) | 
| 783 | (* code exists above -- in which case either the split theorem should be *) | |
| 784 | (* implemented above, or 'is_split_thm' should be modified to filter it *) | |
| 785 | (* out *) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 786 | | (t, ts) => ( | 
| 20268 | 787 |       warning ("Lin. Arith.: split rule for " ^ Sign.string_of_term sg t ^
 | 
| 788 | " (with " ^ Int.toString (length ts) ^ | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 789 | " argument(s)) not implemented; proof reconstruction is likely to fail"); | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 790 | NONE | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 791 | )) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 792 | ) | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 793 | end; | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 794 | |
| 20268 | 795 | (* remove terms that do not satisfy 'p'; change the order of the remaining *) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 796 | (* terms in the same way as filter_prems_tac does *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 797 | |
| 20268 | 798 | fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list = | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 799 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 800 | fun filter_prems (t, (left, right)) = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 801 | if p t then (left, right @ [t]) else (left @ right, []) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 802 | val (left, right) = foldl filter_prems ([], []) terms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 803 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 804 | right @ left | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 805 | end; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 806 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 807 | (* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 808 | (* subgoal that has 'terms' as premises *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 809 | |
| 20268 | 810 | fun negated_term_occurs_positively (terms : term list) : bool = | 
| 811 | List.exists | |
| 812 |     (fn (Trueprop $ (Const ("Not", _) $ t)) => member (op aconv) terms (Trueprop $ t)
 | |
| 813 | | _ => false) | |
| 814 | terms; | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 815 | |
| 20268 | 816 | fun pre_decomp sg (Ts : typ list, terms : term list) : (typ list * term list) list = | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 817 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 818 | (* repeatedly split (including newly emerging subgoals) until no further *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 819 | (* splitting is possible *) | 
| 20271 
e76e77e0d615
fixed a bug in function poly: decomposition of products
 webertj parents: 
20268diff
changeset | 820 | fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list) | 
| 20268 | 821 | | split_loop (subgoal::subgoals) = ( | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 822 | case split_once_items sg subgoal of | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 823 | SOME new_subgoals => split_loop (new_subgoals @ subgoals) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 824 | | NONE => subgoal :: split_loop subgoals | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 825 | ) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 826 | fun is_relevant t = isSome (decomp sg t) | 
| 20268 | 827 | (* filter_prems_tac is_relevant: *) | 
| 828 | val relevant_terms = filter_prems_tac_items is_relevant terms | |
| 829 | (* split_tac, NNF normalization: *) | |
| 830 | val split_goals = split_loop [(Ts, relevant_terms)] | |
| 831 | (* necessary because split_once_tac may normalize terms: *) | |
| 832 | val beta_eta_norm = map (apsnd (map (Envir.eta_contract o Envir.beta_norm))) split_goals | |
| 833 | (* TRY (etac notE) THEN eq_assume_tac: *) | |
| 834 | val result = List.filter (not o negated_term_occurs_positively o snd) beta_eta_norm | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 835 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 836 | result | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 837 | end; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 838 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 839 | (* takes the i-th subgoal [| A1; ...; An |] ==> B to *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 840 | (* An --> ... --> A1 --> B, performs splitting with the given 'split_thms' *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 841 | (* (resulting in a different subgoal P), takes P to ~P ==> False, *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 842 | (* performs NNF-normalization of ~P, and eliminates conjunctions, *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 843 | (* disjunctions and existential quantifiers from the premises, possibly (in *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 844 | (* the case of disjunctions) resulting in several new subgoals, each of the *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 845 | (* general form [| Q1; ...; Qm |] ==> False. Fails if more than *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 846 | (* !fast_arith_split_limit splits are possible. *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 847 | |
| 20850 | 848 | local | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 849 | val nnf_simpset = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 850 | empty_ss setmkeqTrue mk_eq_True | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 851 | setmksimps (mksimps mksimps_pairs) | 
| 20850 | 852 | addsimps [imp_conv_disj, iff_conv_conj_imp, de_Morgan_disj, de_Morgan_conj, | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 853 | not_all, not_ex, not_not] | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 854 | fun prem_nnf_tac i st = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 855 | full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st | 
| 20850 | 856 | in | 
| 857 | ||
| 858 | fun split_once_tac (split_thms : thm list) (i : int) : tactic = | |
| 859 | let | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 860 | fun cond_split_tac i st = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 861 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 862 | val subgoal = Logic.nth_prem (i, Thm.prop_of st) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 863 | val Ts = rev (map snd (Logic.strip_params subgoal)) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 864 | val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 865 | val cmap = Splitter.cmap_of_split_thms split_thms | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 866 | val splits = Splitter.split_posns cmap (theory_of_thm st) Ts concl | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 867 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 868 | if length splits > !fast_arith_split_limit then | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 869 | no_tac st | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 870 | else | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 871 | split_tac split_thms i st | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 872 | end | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 873 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 874 | EVERY' [ | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 875 | REPEAT_DETERM o etac rev_mp, | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 876 | cond_split_tac, | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 877 | rtac ccontr, | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 878 | prem_nnf_tac, | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 879 | TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE)) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 880 | ] i | 
| 20850 | 881 | end | 
| 882 | ||
| 883 | end; (* local *) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 884 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 885 | (* remove irrelevant premises, then split the i-th subgoal (and all new *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 886 | (* subgoals) by using 'split_once_tac' repeatedly. Beta-eta-normalize new *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 887 | (* subgoals and finally attempt to solve them by finding an immediate *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 888 | (* contradiction (i.e. a term and its negation) in their premises. *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 889 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 890 | fun pre_tac i st = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 891 | let | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 892 | val sg = theory_of_thm st | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 893 | val split_thms = filter is_split_thm (#splits (ArithTheoryData.get sg)) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 894 | fun is_relevant t = isSome (decomp sg t) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 895 | in | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 896 | DETERM ( | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 897 | TRY (filter_prems_tac is_relevant i) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 898 | THEN ( | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 899 | (TRY o REPEAT_ALL_NEW (split_once_tac split_thms)) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 900 | THEN_ALL_NEW | 
| 23530 | 901 | (CONVERSION Drule.beta_eta_conversion | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 902 | THEN' | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 903 | (TRY o (etac notE THEN' eq_assume_tac))) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 904 | ) i | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 905 | ) st | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 906 | end; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 907 | |
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 908 | end; (* LA_Data_Ref *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 909 | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 910 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 911 | structure Fast_Arith = | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 912 | Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref); | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 913 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 914 | val fast_arith_tac = Fast_Arith.lin_arith_tac false; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 915 | val fast_ex_arith_tac = Fast_Arith.lin_arith_tac; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 916 | val trace_arith = Fast_Arith.trace; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 917 | val fast_arith_neq_limit = Fast_Arith.fast_arith_neq_limit; | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 918 | val fast_arith_split_limit = LA_Data_Ref.fast_arith_split_limit; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 919 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 920 | (* reduce contradictory <= to False. | 
| 22838 | 921 | Most of the work is done by the cancel tactics. *) | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 922 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 923 | val init_lin_arith_data = | 
| 18708 | 924 |  Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
 | 
| 15121 
1198032bad25
Initial changes to extend arithmetic from individual types to type classes.
 nipkow parents: 
14738diff
changeset | 925 |    {add_mono_thms = add_mono_thms @
 | 
| 22838 | 926 |     @{thms add_mono_thms_ordered_semiring} @ @{thms add_mono_thms_ordered_field},
 | 
| 10693 | 927 | mult_mono_thms = mult_mono_thms, | 
| 10574 
8f98f0301d67
Linear arithmetic now copes with mixed nat/int formulae.
 nipkow parents: 
10516diff
changeset | 928 | inj_thms = inj_thms, | 
| 21243 | 929 | lessD = lessD @ [thm "Suc_leI"], | 
| 22887 | 930 |     neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_ordered_idom}],
 | 
| 22838 | 931 | simpset = HOL_basic_ss | 
| 23085 | 932 |       addsimps [@{thm "monoid_add_class.zero_plus.add_0_left"}, @{thm "monoid_add_class.zero_plus.add_0_right"},
 | 
| 22838 | 933 |         @{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
 | 
| 934 |         @{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
 | |
| 935 |         @{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
 | |
| 936 |         @{thm "not_one_less_zero"}]
 | |
| 937 | addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv] | |
| 938 | (*abel_cancel helps it work in abstract algebraic domains*) | |
| 939 | addsimprocs nat_cancel_sums_add}) #> | |
| 18708 | 940 | arith_discrete "nat"; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 941 | |
| 13462 | 942 | val fast_nat_arith_simproc = | 
| 16834 | 943 | Simplifier.simproc (the_context ()) "fast_nat_arith" | 
| 13462 | 944 | ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 945 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 946 | (* Because of fast_nat_arith_simproc, the arithmetic solver is really only | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 947 | useful to detect inconsistencies among the premises for subgoals which are | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 948 | *not* themselves (in)equalities, because the latter activate | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 949 | fast_nat_arith_simproc anyway. However, it seems cheaper to activate the | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 950 | solver all the time rather than add the additional check. *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 951 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 952 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 953 | (* arith proof method *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 954 | |
| 10516 | 955 | local | 
| 956 | ||
| 13499 | 957 | fun raw_arith_tac ex i st = | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 958 | (* FIXME: K true should be replaced by a sensible test (perhaps "isSome o | 
| 23200 | 959 | decomp sg"? -- but note that the test is applied to terms already before | 
| 960 | they are split/normalized) to speed things up in case there are lots of | |
| 961 | irrelevant terms involved; elimination of min/max can be optimized: | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 962 | (max m n + k <= r) = (m+k <= r & n+k <= r) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 963 | (l <= min m n + k) = (l <= m+k & l <= n+k) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 964 | *) | 
| 13499 | 965 | refute_tac (K true) | 
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 966 | (* Splitting is also done inside fast_arith_tac, but not completely -- *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 967 | (* split_tac may use split theorems that have not been implemented in *) | 
| 20268 | 968 | (* fast_arith_tac (cf. pre_decomp and split_once_items above), and *) | 
| 969 | (* fast_arith_split_limit may trigger. *) | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 970 | (* Therefore splitting outside of fast_arith_tac may allow us to prove *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 971 | (* some goals that fast_arith_tac alone would fail on. *) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 972 | (REPEAT_DETERM o split_tac (#splits (ArithTheoryData.get (Thm.theory_of_thm st)))) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 973 | (fast_ex_arith_tac ex) | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 974 | i st; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 975 | |
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 976 | fun arith_theory_tac i st = | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 977 | let | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 978 | val tactics = #tactics (ArithTheoryData.get (Thm.theory_of_thm st)) | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 979 | in | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 980 |   FIRST' (map (fn ArithTactic {tactic, ...} => tactic) tactics) i st
 | 
| 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 981 | end; | 
| 13877 
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
 berghofe parents: 
13517diff
changeset | 982 | |
| 10516 | 983 | in | 
| 984 | ||
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 985 | val simple_arith_tac = FIRST' [fast_arith_tac, | 
| 23190 | 986 | ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac true]; | 
| 13877 
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
 berghofe parents: 
13517diff
changeset | 987 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 988 | val arith_tac = FIRST' [fast_arith_tac, | 
| 23190 | 989 | ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac true, | 
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 990 | arith_theory_tac]; | 
| 13877 
a6b825ee48d9
Added hook for presburger arithmetic decision procedure.
 berghofe parents: 
13517diff
changeset | 991 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 992 | val silent_arith_tac = FIRST' [fast_arith_tac, | 
| 23190 | 993 | ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac false, | 
| 20412 
40757f475eb0
additional list of tactics that can be added to arith
 webertj parents: 
20280diff
changeset | 994 | arith_theory_tac]; | 
| 10516 | 995 | |
| 20217 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 996 | fun arith_method prems = | 
| 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 webertj parents: 
20044diff
changeset | 997 | Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac)); | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 998 | |
| 10516 | 999 | end; | 
| 1000 | ||
| 15195 | 1001 | (* antisymmetry: | 
| 15197 | 1002 | combines x <= y (or ~(y < x)) and y <= x (or ~(x < y)) into x = y | 
| 15195 | 1003 | |
| 1004 | local | |
| 1005 | val antisym = mk_meta_eq order_antisym | |
| 22548 | 1006 | val not_lessD = @{thm linorder_not_less} RS iffD1
 | 
| 15195 | 1007 | fun prp t thm = (#prop(rep_thm thm) = t) | 
| 1008 | in | |
| 1009 | fun antisym_eq prems thm = | |
| 1010 | let | |
| 1011 | val r = #prop(rep_thm thm); | |
| 1012 | in | |
| 1013 | case r of | |
| 22997 | 1014 |       Tr $ ((c as Const(@{const_name Orderings.less_eq},T)) $ s $ t) =>
 | 
| 15195 | 1015 | let val r' = Tr $ (c $ t $ s) | 
| 1016 | in | |
| 1017 | case Library.find_first (prp r') prems of | |
| 15531 | 1018 | NONE => | 
| 22997 | 1019 |               let val r' = Tr $ (HOLogic.Not $ (Const(@{const_name Orderings.less},T) $ s $ t))
 | 
| 15195 | 1020 | in case Library.find_first (prp r') prems of | 
| 15531 | 1021 | NONE => [] | 
| 1022 | | SOME thm' => [(thm' RS not_lessD) RS (thm RS antisym)] | |
| 15195 | 1023 | end | 
| 15531 | 1024 | | SOME thm' => [thm' RS (thm RS antisym)] | 
| 15195 | 1025 | end | 
| 22997 | 1026 |     | Tr $ (Const("Not",_) $ (Const(@{const_name Orderings.less},T) $ s $ t)) =>
 | 
| 1027 |         let val r' = Tr $ (Const(@{const_name Orderings.less_eq},T) $ s $ t)
 | |
| 15195 | 1028 | in | 
| 1029 | case Library.find_first (prp r') prems of | |
| 15531 | 1030 | NONE => | 
| 22997 | 1031 |               let val r' = Tr $ (HOLogic.Not $ (Const(@{const_name Orderings.less},T) $ t $ s))
 | 
| 15195 | 1032 | in case Library.find_first (prp r') prems of | 
| 15531 | 1033 | NONE => [] | 
| 1034 | | SOME thm' => | |
| 15195 | 1035 | [(thm' RS not_lessD) RS ((thm RS not_lessD) RS antisym)] | 
| 1036 | end | |
| 15531 | 1037 | | SOME thm' => [thm' RS ((thm RS not_lessD) RS antisym)] | 
| 15195 | 1038 | end | 
| 1039 | | _ => [] | |
| 1040 | end | |
| 1041 | handle THM _ => [] | |
| 1042 | end; | |
| 15197 | 1043 | *) | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 1044 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 1045 | (* theory setup *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 1046 | |
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 1047 | val arith_setup = | 
| 18708 | 1048 | init_lin_arith_data #> | 
| 1049 | (fn thy => (Simplifier.change_simpset_of thy (fn ss => ss | |
| 17875 | 1050 | addsimprocs (nat_cancel_sums @ [fast_nat_arith_simproc]) | 
| 18708 | 1051 | addSolver (mk_solver' "lin. arith." Fast_Arith.cut_lin_arith_tac)); thy)) #> | 
| 15221 | 1052 | Method.add_methods | 
| 21879 | 1053 |     [("arith", (arith_method o fst) oo Method.syntax Args.bang_facts,
 | 
| 18708 | 1054 | "decide linear arithmethic")] #> | 
| 18728 | 1055 |   Attrib.add_attributes [("arith_split", Attrib.no_args arith_split_add,
 | 
| 18708 | 1056 | "declaration of split rules for arithmetic procedure")]; |