wenzelm@24092
|
1 |
(* Title: HOL/Tools/lin_arith.ML
|
wenzelm@29288
|
2 |
Author: Tjark Weber and Tobias Nipkow, TU Muenchen
|
wenzelm@24092
|
3 |
|
wenzelm@24092
|
4 |
HOL setup for linear arithmetic (see Provers/Arith/fast_lin_arith.ML).
|
wenzelm@24092
|
5 |
*)
|
wenzelm@24092
|
6 |
|
wenzelm@24092
|
7 |
signature LIN_ARITH =
|
wenzelm@24092
|
8 |
sig
|
wenzelm@35230
|
9 |
val pre_tac: simpset -> int -> tactic
|
haftmann@31101
|
10 |
val simple_tac: Proof.context -> int -> tactic
|
haftmann@31101
|
11 |
val tac: Proof.context -> int -> tactic
|
haftmann@31101
|
12 |
val simproc: simpset -> term -> thm option
|
haftmann@31100
|
13 |
val add_inj_thms: thm list -> Context.generic -> Context.generic
|
haftmann@31100
|
14 |
val add_lessD: thm -> Context.generic -> Context.generic
|
haftmann@31100
|
15 |
val add_simps: thm list -> Context.generic -> Context.generic
|
haftmann@31100
|
16 |
val add_simprocs: simproc list -> Context.generic -> Context.generic
|
haftmann@31100
|
17 |
val add_inj_const: string * typ -> Context.generic -> Context.generic
|
haftmann@31082
|
18 |
val add_discrete_type: string -> Context.generic -> Context.generic
|
boehmes@31510
|
19 |
val set_number_of: (theory -> typ -> int -> cterm) -> Context.generic ->
|
boehmes@31510
|
20 |
Context.generic
|
haftmann@31082
|
21 |
val setup: Context.generic -> Context.generic
|
haftmann@31100
|
22 |
val global_setup: theory -> theory
|
haftmann@31082
|
23 |
val split_limit: int Config.T
|
haftmann@31082
|
24 |
val neq_limit: int Config.T
|
wenzelm@32740
|
25 |
val warning_count: int Unsynchronized.ref
|
wenzelm@32740
|
26 |
val trace: bool Unsynchronized.ref
|
wenzelm@24092
|
27 |
end;
|
wenzelm@24092
|
28 |
|
haftmann@30671
|
29 |
structure Lin_Arith: LIN_ARITH =
|
wenzelm@24092
|
30 |
struct
|
wenzelm@24092
|
31 |
|
wenzelm@24092
|
32 |
(* Parameters data for general linear arithmetic functor *)
|
wenzelm@24092
|
33 |
|
wenzelm@24092
|
34 |
structure LA_Logic: LIN_ARITH_LOGIC =
|
wenzelm@24092
|
35 |
struct
|
wenzelm@24092
|
36 |
|
wenzelm@24092
|
37 |
val ccontr = ccontr;
|
wenzelm@24092
|
38 |
val conjI = conjI;
|
wenzelm@24092
|
39 |
val notI = notI;
|
wenzelm@24092
|
40 |
val sym = sym;
|
boehmes@31510
|
41 |
val trueI = TrueI;
|
wenzelm@24092
|
42 |
val not_lessD = @{thm linorder_not_less} RS iffD1;
|
wenzelm@24092
|
43 |
val not_leD = @{thm linorder_not_le} RS iffD1;
|
wenzelm@24092
|
44 |
|
wenzelm@35410
|
45 |
fun mk_Eq thm = thm RS @{thm Eq_FalseI} handle THM _ => thm RS @{thm Eq_TrueI};
|
wenzelm@24092
|
46 |
|
wenzelm@24092
|
47 |
val mk_Trueprop = HOLogic.mk_Trueprop;
|
wenzelm@24092
|
48 |
|
wenzelm@24092
|
49 |
fun atomize thm = case Thm.prop_of thm of
|
haftmann@31100
|
50 |
Const ("Trueprop", _) $ (Const (@{const_name "op &"}, _) $ _ $ _) =>
|
haftmann@31100
|
51 |
atomize (thm RS conjunct1) @ atomize (thm RS conjunct2)
|
wenzelm@24092
|
52 |
| _ => [thm];
|
wenzelm@24092
|
53 |
|
haftmann@32603
|
54 |
fun neg_prop ((TP as Const("Trueprop", _)) $ (Const (@{const_name Not}, _) $ t)) = TP $ t
|
haftmann@31100
|
55 |
| neg_prop ((TP as Const("Trueprop", _)) $ t) = TP $ (HOLogic.Not $t)
|
wenzelm@24092
|
56 |
| neg_prop t = raise TERM ("neg_prop", [t]);
|
wenzelm@24092
|
57 |
|
wenzelm@24092
|
58 |
fun is_False thm =
|
wenzelm@24092
|
59 |
let val _ $ t = Thm.prop_of thm
|
haftmann@31100
|
60 |
in t = HOLogic.false_const end;
|
wenzelm@24092
|
61 |
|
haftmann@30671
|
62 |
fun is_nat t = (fastype_of1 t = HOLogic.natT);
|
wenzelm@24092
|
63 |
|
haftmann@31100
|
64 |
fun mk_nat_thm thy t =
|
haftmann@31100
|
65 |
let
|
haftmann@31100
|
66 |
val cn = cterm_of thy (Var (("n", 0), HOLogic.natT))
|
haftmann@31100
|
67 |
and ct = cterm_of thy t
|
haftmann@31100
|
68 |
in instantiate ([], [(cn, ct)]) @{thm le0} end;
|
wenzelm@24092
|
69 |
|
webertj@32369
|
70 |
end; (* LA_Logic *)
|
wenzelm@24092
|
71 |
|
wenzelm@24092
|
72 |
|
wenzelm@24092
|
73 |
(* arith context data *)
|
wenzelm@24092
|
74 |
|
wenzelm@33519
|
75 |
structure Lin_Arith_Data = Generic_Data
|
wenzelm@24092
|
76 |
(
|
wenzelm@24092
|
77 |
type T = {splits: thm list,
|
wenzelm@24092
|
78 |
inj_consts: (string * typ) list,
|
haftmann@30671
|
79 |
discrete: string list};
|
haftmann@30671
|
80 |
val empty = {splits = [], inj_consts = [], discrete = []};
|
wenzelm@24092
|
81 |
val extend = I;
|
wenzelm@33519
|
82 |
fun merge
|
haftmann@30671
|
83 |
({splits= splits1, inj_consts= inj_consts1, discrete= discrete1},
|
haftmann@30671
|
84 |
{splits= splits2, inj_consts= inj_consts2, discrete= discrete2}) : T =
|
wenzelm@33520
|
85 |
{splits = Thm.merge_thms (splits1, splits2),
|
wenzelm@24092
|
86 |
inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
|
haftmann@30671
|
87 |
discrete = Library.merge (op =) (discrete1, discrete2)};
|
wenzelm@24092
|
88 |
);
|
wenzelm@24092
|
89 |
|
haftmann@31100
|
90 |
val get_arith_data = Lin_Arith_Data.get o Context.Proof;
|
wenzelm@24092
|
91 |
|
haftmann@31100
|
92 |
fun add_split thm = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
|
haftmann@31100
|
93 |
{splits = update Thm.eq_thm_prop thm splits,
|
haftmann@31100
|
94 |
inj_consts = inj_consts, discrete = discrete});
|
wenzelm@24092
|
95 |
|
haftmann@31100
|
96 |
fun add_discrete_type d = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
|
wenzelm@24092
|
97 |
{splits = splits, inj_consts = inj_consts,
|
haftmann@30671
|
98 |
discrete = update (op =) d discrete});
|
wenzelm@24092
|
99 |
|
haftmann@31100
|
100 |
fun add_inj_const c = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
|
wenzelm@24112
|
101 |
{splits = splits, inj_consts = update (op =) c inj_consts,
|
haftmann@30671
|
102 |
discrete = discrete});
|
wenzelm@24092
|
103 |
|
haftmann@31100
|
104 |
val (split_limit, setup_split_limit) = Attrib.config_int "linarith_split_limit" 9;
|
haftmann@31100
|
105 |
val (neq_limit, setup_neq_limit) = Attrib.config_int "linarith_neq_limit" 9;
|
wenzelm@24092
|
106 |
|
wenzelm@24092
|
107 |
|
haftmann@31100
|
108 |
structure LA_Data =
|
wenzelm@24092
|
109 |
struct
|
wenzelm@24092
|
110 |
|
haftmann@31082
|
111 |
val fast_arith_neq_limit = neq_limit;
|
wenzelm@24092
|
112 |
|
wenzelm@24092
|
113 |
|
wenzelm@24092
|
114 |
(* Decomposition of terms *)
|
wenzelm@24092
|
115 |
|
wenzelm@24092
|
116 |
(*internal representation of linear (in-)equations*)
|
wenzelm@26942
|
117 |
type decomp =
|
webertj@24328
|
118 |
((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
|
wenzelm@24092
|
119 |
|
wenzelm@24092
|
120 |
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
|
wenzelm@24092
|
121 |
| nT _ = false;
|
wenzelm@24092
|
122 |
|
wenzelm@24092
|
123 |
fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
|
wenzelm@24092
|
124 |
(term * Rat.rat) list * Rat.rat =
|
nipkow@29548
|
125 |
case AList.lookup Pattern.aeconv p t of
|
webertj@24328
|
126 |
NONE => ((t, m) :: p, i)
|
nipkow@29548
|
127 |
| SOME n => (AList.update Pattern.aeconv (t, Rat.add n m) p, i);
|
wenzelm@24092
|
128 |
|
webertj@24328
|
129 |
(* decompose nested multiplications, bracketing them to the right and combining
|
webertj@24328
|
130 |
all their coefficients
|
wenzelm@24092
|
131 |
|
webertj@24328
|
132 |
inj_consts: list of constants to be ignored when encountered
|
webertj@24328
|
133 |
(e.g. arithmetic type conversions that preserve value)
|
wenzelm@24092
|
134 |
|
webertj@24328
|
135 |
m: multiplicity associated with the entire product
|
wenzelm@24092
|
136 |
|
webertj@24328
|
137 |
returns either (SOME term, associated multiplicity) or (NONE, constant)
|
webertj@24328
|
138 |
*)
|
wenzelm@24092
|
139 |
fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
|
wenzelm@24092
|
140 |
let
|
haftmann@35267
|
141 |
fun demult ((mC as Const (@{const_name Groups.times}, _)) $ s $ t, m) =
|
haftmann@35267
|
142 |
(case s of Const (@{const_name Groups.times}, _) $ s1 $ s2 =>
|
webertj@24328
|
143 |
(* bracketing to the right: '(s1 * s2) * t' becomes 's1 * (s2 * t)' *)
|
wenzelm@24092
|
144 |
demult (mC $ s1 $ (mC $ s2 $ t), m)
|
webertj@24328
|
145 |
| _ =>
|
webertj@24328
|
146 |
(* product 's * t', where either factor can be 'NONE' *)
|
webertj@24328
|
147 |
(case demult (s, m) of
|
webertj@24328
|
148 |
(SOME s', m') =>
|
webertj@24328
|
149 |
(case demult (t, m') of
|
webertj@24328
|
150 |
(SOME t', m'') => (SOME (mC $ s' $ t'), m'')
|
webertj@24328
|
151 |
| (NONE, m'') => (SOME s', m''))
|
webertj@24328
|
152 |
| (NONE, m') => demult (t, m')))
|
haftmann@35084
|
153 |
| demult ((mC as Const (@{const_name Rings.divide}, _)) $ s $ t, m) =
|
webertj@24328
|
154 |
(* FIXME: Shouldn't we simplify nested quotients, e.g. '(s/t)/u' could
|
webertj@24328
|
155 |
become 's/(t*u)', and '(s*t)/u' could become 's*(t/u)' ? Note that
|
webertj@24328
|
156 |
if we choose to do so here, the simpset used by arith must be able to
|
webertj@24328
|
157 |
perform the same simplifications. *)
|
webertj@24328
|
158 |
(* FIXME: Currently we treat the numerator as atomic unless the
|
webertj@24328
|
159 |
denominator can be reduced to a numeric constant. It might be better
|
webertj@24328
|
160 |
to demult the numerator in any case, and invent a new term of the form
|
webertj@24328
|
161 |
'1 / t' if the numerator can be reduced, but the denominator cannot. *)
|
webertj@24328
|
162 |
(* FIXME: Currently we even treat the whole fraction as atomic unless the
|
webertj@24328
|
163 |
denominator can be reduced to a numeric constant. It might be better
|
webertj@25015
|
164 |
to use the partially reduced denominator (i.e. 's / (2*t)' could be
|
webertj@24328
|
165 |
demult'ed to 's / t' with multiplicity .5). This would require a
|
webertj@24328
|
166 |
very simple change only below, but it breaks existing proofs. *)
|
webertj@24328
|
167 |
(* quotient 's / t', where the denominator t can be NONE *)
|
webertj@24328
|
168 |
(* Note: will raise Rat.DIVZERO iff m' is Rat.zero *)
|
webertj@24328
|
169 |
(case demult (t, Rat.one) of
|
webertj@24328
|
170 |
(SOME _, _) => (SOME (mC $ s $ t), m)
|
webertj@24328
|
171 |
| (NONE, m') => apsnd (Rat.mult (Rat.inv m')) (demult (s, m)))
|
webertj@24328
|
172 |
(* terms that evaluate to numeric constants *)
|
haftmann@35267
|
173 |
| demult (Const (@{const_name Groups.uminus}, _) $ t, m) = demult (t, Rat.neg m)
|
haftmann@35267
|
174 |
| demult (Const (@{const_name Groups.zero}, _), m) = (NONE, Rat.zero)
|
haftmann@35267
|
175 |
| demult (Const (@{const_name Groups.one}, _), m) = (NONE, m)
|
webertj@24328
|
176 |
(*Warning: in rare cases number_of encloses a non-numeral,
|
webertj@24328
|
177 |
in which case dest_numeral raises TERM; hence all the handles below.
|
webertj@24328
|
178 |
Same for Suc-terms that turn out not to be numerals -
|
webertj@24328
|
179 |
although the simplifier should eliminate those anyway ...*)
|
haftmann@25919
|
180 |
| demult (t as Const ("Int.number_class.number_of", _) $ n, m) =
|
webertj@24328
|
181 |
((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
|
webertj@24328
|
182 |
handle TERM _ => (SOME t, m))
|
webertj@24328
|
183 |
| demult (t as Const (@{const_name Suc}, _) $ _, m) =
|
webertj@24328
|
184 |
((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat t)))
|
webertj@24328
|
185 |
handle TERM _ => (SOME t, m))
|
webertj@24328
|
186 |
(* injection constants are ignored *)
|
wenzelm@24092
|
187 |
| demult (t as Const f $ x, m) =
|
webertj@24328
|
188 |
if member (op =) inj_consts f then demult (x, m) else (SOME t, m)
|
webertj@24328
|
189 |
(* everything else is considered atomic *)
|
wenzelm@24092
|
190 |
| demult (atom, m) = (SOME atom, m)
|
wenzelm@24092
|
191 |
in demult end;
|
wenzelm@24092
|
192 |
|
wenzelm@24092
|
193 |
fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
|
wenzelm@24092
|
194 |
((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
|
wenzelm@24092
|
195 |
let
|
webertj@24328
|
196 |
(* Turns a term 'all' and associated multiplicity 'm' into a list 'p' of
|
webertj@24328
|
197 |
summands and associated multiplicities, plus a constant 'i' (with implicit
|
webertj@24328
|
198 |
multiplicity 1) *)
|
haftmann@35267
|
199 |
fun poly (Const (@{const_name Groups.plus}, _) $ s $ t,
|
wenzelm@24092
|
200 |
m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
|
haftmann@35267
|
201 |
| poly (all as Const (@{const_name Groups.minus}, T) $ s $ t, m, pi) =
|
wenzelm@24092
|
202 |
if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
|
haftmann@35267
|
203 |
| poly (all as Const (@{const_name Groups.uminus}, T) $ t, m, pi) =
|
wenzelm@24092
|
204 |
if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
|
haftmann@35267
|
205 |
| poly (Const (@{const_name Groups.zero}, _), _, pi) =
|
wenzelm@24092
|
206 |
pi
|
haftmann@35267
|
207 |
| poly (Const (@{const_name Groups.one}, _), m, (p, i)) =
|
wenzelm@24092
|
208 |
(p, Rat.add i m)
|
wenzelm@24092
|
209 |
| poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
|
wenzelm@24092
|
210 |
poly (t, m, (p, Rat.add i m))
|
haftmann@35267
|
211 |
| poly (all as Const (@{const_name Groups.times}, _) $ _ $ _, m, pi as (p, i)) =
|
wenzelm@24092
|
212 |
(case demult inj_consts (all, m) of
|
wenzelm@24092
|
213 |
(NONE, m') => (p, Rat.add i m')
|
wenzelm@24092
|
214 |
| (SOME u, m') => add_atom u m' pi)
|
haftmann@35084
|
215 |
| poly (all as Const (@{const_name Rings.divide}, _) $ _ $ _, m, pi as (p, i)) =
|
wenzelm@24092
|
216 |
(case demult inj_consts (all, m) of
|
wenzelm@24092
|
217 |
(NONE, m') => (p, Rat.add i m')
|
wenzelm@24092
|
218 |
| (SOME u, m') => add_atom u m' pi)
|
haftmann@25919
|
219 |
| poly (all as Const ("Int.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
|
wenzelm@24092
|
220 |
(let val k = HOLogic.dest_numeral t
|
wenzelm@24092
|
221 |
val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k
|
wenzelm@24092
|
222 |
in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end
|
wenzelm@24092
|
223 |
handle TERM _ => add_atom all m pi)
|
wenzelm@24092
|
224 |
| poly (all as Const f $ x, m, pi) =
|
wenzelm@24092
|
225 |
if f mem inj_consts then poly (x, m, pi) else add_atom all m pi
|
wenzelm@24092
|
226 |
| poly (all, m, pi) =
|
wenzelm@24092
|
227 |
add_atom all m pi
|
wenzelm@24092
|
228 |
val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
|
wenzelm@24092
|
229 |
val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
|
wenzelm@24092
|
230 |
in
|
wenzelm@24092
|
231 |
case rel of
|
haftmann@35092
|
232 |
@{const_name Orderings.less} => SOME (p, i, "<", q, j)
|
haftmann@35092
|
233 |
| @{const_name Orderings.less_eq} => SOME (p, i, "<=", q, j)
|
wenzelm@24092
|
234 |
| "op =" => SOME (p, i, "=", q, j)
|
wenzelm@24092
|
235 |
| _ => NONE
|
webertj@24328
|
236 |
end handle Rat.DIVZERO => NONE;
|
wenzelm@24092
|
237 |
|
wenzelm@24271
|
238 |
fun of_lin_arith_sort thy U =
|
haftmann@35050
|
239 |
Sign.of_sort thy (U, @{sort Rings.linordered_idom});
|
wenzelm@24092
|
240 |
|
haftmann@31101
|
241 |
fun allows_lin_arith thy (discrete : string list) (U as Type (D, [])) : bool * bool =
|
haftmann@31101
|
242 |
if of_lin_arith_sort thy U then (true, member (op =) discrete D)
|
haftmann@31101
|
243 |
else if member (op =) discrete D then (true, true) else (false, false)
|
haftmann@31101
|
244 |
| allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
|
wenzelm@24092
|
245 |
|
wenzelm@26942
|
246 |
fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decomp option =
|
wenzelm@24092
|
247 |
case T of
|
wenzelm@24092
|
248 |
Type ("fun", [U, _]) =>
|
wenzelm@24092
|
249 |
(case allows_lin_arith thy discrete U of
|
wenzelm@24092
|
250 |
(true, d) =>
|
wenzelm@24092
|
251 |
(case decomp0 inj_consts xxx of
|
wenzelm@24092
|
252 |
NONE => NONE
|
wenzelm@24092
|
253 |
| SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
|
wenzelm@24092
|
254 |
| (false, _) =>
|
wenzelm@24092
|
255 |
NONE)
|
wenzelm@24092
|
256 |
| _ => NONE;
|
wenzelm@24092
|
257 |
|
wenzelm@24092
|
258 |
fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
|
wenzelm@24092
|
259 |
| negate NONE = NONE;
|
wenzelm@24092
|
260 |
|
wenzelm@24092
|
261 |
fun decomp_negation data
|
wenzelm@26942
|
262 |
((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decomp option =
|
wenzelm@24092
|
263 |
decomp_typecheck data (T, (rel, lhs, rhs))
|
wenzelm@24092
|
264 |
| decomp_negation data ((Const ("Trueprop", _)) $
|
wenzelm@24092
|
265 |
(Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
|
wenzelm@24092
|
266 |
negate (decomp_typecheck data (T, (rel, lhs, rhs)))
|
wenzelm@24092
|
267 |
| decomp_negation data _ =
|
wenzelm@24092
|
268 |
NONE;
|
wenzelm@24092
|
269 |
|
wenzelm@26942
|
270 |
fun decomp ctxt : term -> decomp option =
|
wenzelm@24092
|
271 |
let
|
wenzelm@24092
|
272 |
val thy = ProofContext.theory_of ctxt
|
wenzelm@24092
|
273 |
val {discrete, inj_consts, ...} = get_arith_data ctxt
|
wenzelm@24092
|
274 |
in decomp_negation (thy, discrete, inj_consts) end;
|
wenzelm@24092
|
275 |
|
wenzelm@24092
|
276 |
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _)) = nT T
|
wenzelm@24092
|
277 |
| domain_is_nat (_ $ (Const ("Not", _) $ (Const (_, T) $ _ $ _))) = nT T
|
wenzelm@24092
|
278 |
| domain_is_nat _ = false;
|
wenzelm@24092
|
279 |
|
wenzelm@24092
|
280 |
|
wenzelm@24092
|
281 |
(*---------------------------------------------------------------------------*)
|
webertj@32369
|
282 |
(* the following code performs splitting of certain constants (e.g., min, *)
|
wenzelm@24092
|
283 |
(* max) in a linear arithmetic problem; similar to what split_tac later does *)
|
wenzelm@24092
|
284 |
(* to the proof state *)
|
wenzelm@24092
|
285 |
(*---------------------------------------------------------------------------*)
|
wenzelm@24092
|
286 |
|
wenzelm@24092
|
287 |
(* checks if splitting with 'thm' is implemented *)
|
wenzelm@24092
|
288 |
|
wenzelm@32111
|
289 |
fun is_split_thm thm =
|
wenzelm@24092
|
290 |
case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
|
wenzelm@24092
|
291 |
(* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
|
wenzelm@24092
|
292 |
case head_of lhs of
|
wenzelm@24092
|
293 |
Const (a, _) => member (op =) [@{const_name Orderings.max},
|
wenzelm@24092
|
294 |
@{const_name Orderings.min},
|
haftmann@35092
|
295 |
@{const_name Groups.abs},
|
haftmann@35267
|
296 |
@{const_name Groups.minus},
|
haftmann@34961
|
297 |
"Int.nat" (*DYNAMIC BINDING!*),
|
haftmann@34961
|
298 |
"Divides.div_class.mod" (*DYNAMIC BINDING!*),
|
haftmann@34961
|
299 |
"Divides.div_class.div" (*DYNAMIC BINDING!*)] a
|
wenzelm@24092
|
300 |
| _ => (warning ("Lin. Arith.: wrong format for split rule " ^
|
wenzelm@32111
|
301 |
Display.string_of_thm_without_context thm);
|
wenzelm@24092
|
302 |
false))
|
wenzelm@24092
|
303 |
| _ => (warning ("Lin. Arith.: wrong format for split rule " ^
|
wenzelm@32111
|
304 |
Display.string_of_thm_without_context thm);
|
wenzelm@24092
|
305 |
false);
|
wenzelm@24092
|
306 |
|
wenzelm@24092
|
307 |
(* substitute new for occurrences of old in a term, incrementing bound *)
|
wenzelm@24092
|
308 |
(* variables as needed when substituting inside an abstraction *)
|
wenzelm@24092
|
309 |
|
wenzelm@24092
|
310 |
fun subst_term ([] : (term * term) list) (t : term) = t
|
wenzelm@24092
|
311 |
| subst_term pairs t =
|
nipkow@29528
|
312 |
(case AList.lookup Pattern.aeconv pairs t of
|
wenzelm@24092
|
313 |
SOME new =>
|
wenzelm@24092
|
314 |
new
|
wenzelm@24092
|
315 |
| NONE =>
|
wenzelm@24092
|
316 |
(case t of Abs (a, T, body) =>
|
wenzelm@24092
|
317 |
let val pairs' = map (pairself (incr_boundvars 1)) pairs
|
wenzelm@24092
|
318 |
in Abs (a, T, subst_term pairs' body) end
|
wenzelm@24092
|
319 |
| t1 $ t2 =>
|
wenzelm@24092
|
320 |
subst_term pairs t1 $ subst_term pairs t2
|
wenzelm@24092
|
321 |
| _ => t));
|
wenzelm@24092
|
322 |
|
wenzelm@24092
|
323 |
(* approximates the effect of one application of split_tac (followed by NNF *)
|
wenzelm@24092
|
324 |
(* normalization) on the subgoal represented by '(Ts, terms)'; returns a *)
|
wenzelm@24092
|
325 |
(* list of new subgoals (each again represented by a typ list for bound *)
|
wenzelm@24092
|
326 |
(* variables and a term list for premises), or NONE if split_tac would fail *)
|
wenzelm@24092
|
327 |
(* on the subgoal *)
|
wenzelm@24092
|
328 |
|
wenzelm@24092
|
329 |
(* FIXME: currently only the effect of certain split theorems is reproduced *)
|
wenzelm@24092
|
330 |
(* (which is why we need 'is_split_thm'). A more canonical *)
|
wenzelm@24092
|
331 |
(* implementation should analyze the right-hand side of the split *)
|
wenzelm@24092
|
332 |
(* theorem that can be applied, and modify the subgoal accordingly. *)
|
wenzelm@24092
|
333 |
(* Or even better, the splitter should be extended to provide *)
|
wenzelm@24092
|
334 |
(* splitting on terms as well as splitting on theorems (where the *)
|
wenzelm@24092
|
335 |
(* former can have a faster implementation as it does not need to be *)
|
wenzelm@24092
|
336 |
(* proof-producing). *)
|
wenzelm@24092
|
337 |
|
wenzelm@24092
|
338 |
fun split_once_items ctxt (Ts : typ list, terms : term list) :
|
wenzelm@24092
|
339 |
(typ list * term list) list option =
|
wenzelm@24092
|
340 |
let
|
wenzelm@24092
|
341 |
val thy = ProofContext.theory_of ctxt
|
wenzelm@24092
|
342 |
(* takes a list [t1, ..., tn] to the term *)
|
wenzelm@24092
|
343 |
(* tn' --> ... --> t1' --> False , *)
|
wenzelm@24092
|
344 |
(* where ti' = HOLogic.dest_Trueprop ti *)
|
webertj@32369
|
345 |
fun REPEAT_DETERM_etac_rev_mp tms =
|
webertj@32369
|
346 |
fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop tms)
|
webertj@32369
|
347 |
HOLogic.false_const
|
webertj@32369
|
348 |
val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
|
webertj@32369
|
349 |
val cmap = Splitter.cmap_of_split_thms split_thms
|
webertj@32369
|
350 |
val goal_tm = REPEAT_DETERM_etac_rev_mp terms
|
webertj@32369
|
351 |
val splits = Splitter.split_posns cmap thy Ts goal_tm
|
haftmann@31082
|
352 |
val split_limit = Config.get ctxt split_limit
|
wenzelm@24092
|
353 |
in
|
webertj@32369
|
354 |
if length splits > split_limit then (
|
webertj@32369
|
355 |
tracing ("linarith_split_limit exceeded (current value is " ^
|
webertj@32369
|
356 |
string_of_int split_limit ^ ")");
|
webertj@32369
|
357 |
NONE
|
webertj@32369
|
358 |
) else case splits of
|
webertj@32369
|
359 |
[] =>
|
wenzelm@24092
|
360 |
(* split_tac would fail: no possible split *)
|
wenzelm@24092
|
361 |
NONE
|
webertj@32369
|
362 |
| (_, _::_, _, _, _) :: _ =>
|
webertj@32369
|
363 |
(* disallow a split that involves non-locally bound variables (except *)
|
webertj@32369
|
364 |
(* when bound by outermost meta-quantifiers) *)
|
webertj@32369
|
365 |
NONE
|
webertj@32369
|
366 |
| (_, [], _, split_type, split_term) :: _ =>
|
webertj@32369
|
367 |
(* ignore all but the first possible split *)
|
webertj@32369
|
368 |
(case strip_comb split_term of
|
wenzelm@24092
|
369 |
(* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
|
wenzelm@24092
|
370 |
(Const (@{const_name Orderings.max}, _), [t1, t2]) =>
|
wenzelm@24092
|
371 |
let
|
wenzelm@24092
|
372 |
val rev_terms = rev terms
|
wenzelm@24092
|
373 |
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
|
wenzelm@24092
|
374 |
val terms2 = map (subst_term [(split_term, t2)]) rev_terms
|
haftmann@35092
|
375 |
val t1_leq_t2 = Const (@{const_name Orderings.less_eq},
|
wenzelm@24092
|
376 |
split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
|
wenzelm@24092
|
377 |
val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
|
wenzelm@24092
|
378 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
wenzelm@24092
|
379 |
val subgoal1 = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
|
wenzelm@24092
|
380 |
val subgoal2 = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
|
wenzelm@24092
|
381 |
in
|
wenzelm@24092
|
382 |
SOME [(Ts, subgoal1), (Ts, subgoal2)]
|
wenzelm@24092
|
383 |
end
|
wenzelm@24092
|
384 |
(* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
|
wenzelm@24092
|
385 |
| (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
|
wenzelm@24092
|
386 |
let
|
wenzelm@24092
|
387 |
val rev_terms = rev terms
|
wenzelm@24092
|
388 |
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
|
wenzelm@24092
|
389 |
val terms2 = map (subst_term [(split_term, t2)]) rev_terms
|
haftmann@35092
|
390 |
val t1_leq_t2 = Const (@{const_name Orderings.less_eq},
|
wenzelm@24092
|
391 |
split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
|
wenzelm@24092
|
392 |
val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
|
wenzelm@24092
|
393 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
wenzelm@24092
|
394 |
val subgoal1 = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
|
wenzelm@24092
|
395 |
val subgoal2 = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
|
wenzelm@24092
|
396 |
in
|
wenzelm@24092
|
397 |
SOME [(Ts, subgoal1), (Ts, subgoal2)]
|
wenzelm@24092
|
398 |
end
|
wenzelm@24092
|
399 |
(* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
|
haftmann@35092
|
400 |
| (Const (@{const_name Groups.abs}, _), [t1]) =>
|
wenzelm@24092
|
401 |
let
|
wenzelm@24092
|
402 |
val rev_terms = rev terms
|
wenzelm@24092
|
403 |
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
|
haftmann@35267
|
404 |
val terms2 = map (subst_term [(split_term, Const (@{const_name Groups.uminus},
|
wenzelm@24092
|
405 |
split_type --> split_type) $ t1)]) rev_terms
|
haftmann@35267
|
406 |
val zero = Const (@{const_name Groups.zero}, split_type)
|
haftmann@35092
|
407 |
val zero_leq_t1 = Const (@{const_name Orderings.less_eq},
|
wenzelm@24092
|
408 |
split_type --> split_type --> HOLogic.boolT) $ zero $ t1
|
haftmann@35092
|
409 |
val t1_lt_zero = Const (@{const_name Orderings.less},
|
wenzelm@24092
|
410 |
split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
|
wenzelm@24092
|
411 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
wenzelm@24092
|
412 |
val subgoal1 = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
|
wenzelm@24092
|
413 |
val subgoal2 = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
|
wenzelm@24092
|
414 |
in
|
wenzelm@24092
|
415 |
SOME [(Ts, subgoal1), (Ts, subgoal2)]
|
wenzelm@24092
|
416 |
end
|
wenzelm@24092
|
417 |
(* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
|
haftmann@35267
|
418 |
| (Const (@{const_name Groups.minus}, _), [t1, t2]) =>
|
wenzelm@24092
|
419 |
let
|
wenzelm@24092
|
420 |
(* "d" in the above theorem becomes a new bound variable after NNF *)
|
wenzelm@24092
|
421 |
(* transformation, therefore some adjustment of indices is necessary *)
|
wenzelm@24092
|
422 |
val rev_terms = rev terms
|
haftmann@35267
|
423 |
val zero = Const (@{const_name Groups.zero}, split_type)
|
wenzelm@24092
|
424 |
val d = Bound 0
|
wenzelm@24092
|
425 |
val terms1 = map (subst_term [(split_term, zero)]) rev_terms
|
wenzelm@24092
|
426 |
val terms2 = map (subst_term [(incr_boundvars 1 split_term, d)])
|
wenzelm@24092
|
427 |
(map (incr_boundvars 1) rev_terms)
|
wenzelm@24092
|
428 |
val t1' = incr_boundvars 1 t1
|
wenzelm@24092
|
429 |
val t2' = incr_boundvars 1 t2
|
haftmann@35092
|
430 |
val t1_lt_t2 = Const (@{const_name Orderings.less},
|
wenzelm@24092
|
431 |
split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
|
wenzelm@24092
|
432 |
val t1_eq_t2_plus_d = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
|
haftmann@35267
|
433 |
(Const (@{const_name Groups.plus},
|
wenzelm@24092
|
434 |
split_type --> split_type --> split_type) $ t2' $ d)
|
wenzelm@24092
|
435 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
wenzelm@24092
|
436 |
val subgoal1 = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
|
wenzelm@24092
|
437 |
val subgoal2 = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
|
wenzelm@24092
|
438 |
in
|
wenzelm@24092
|
439 |
SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
|
wenzelm@24092
|
440 |
end
|
webertj@33723
|
441 |
(* ?P (nat ?i) = ((ALL n. ?i = of_nat n --> ?P n) & (?i < 0 --> ?P 0)) *)
|
haftmann@25919
|
442 |
| (Const ("Int.nat", _), [t1]) =>
|
wenzelm@24092
|
443 |
let
|
wenzelm@24092
|
444 |
val rev_terms = rev terms
|
haftmann@35267
|
445 |
val zero_int = Const (@{const_name Groups.zero}, HOLogic.intT)
|
haftmann@35267
|
446 |
val zero_nat = Const (@{const_name Groups.zero}, HOLogic.natT)
|
wenzelm@24092
|
447 |
val n = Bound 0
|
wenzelm@24092
|
448 |
val terms1 = map (subst_term [(incr_boundvars 1 split_term, n)])
|
wenzelm@24092
|
449 |
(map (incr_boundvars 1) rev_terms)
|
wenzelm@24092
|
450 |
val terms2 = map (subst_term [(split_term, zero_nat)]) rev_terms
|
wenzelm@24092
|
451 |
val t1' = incr_boundvars 1 t1
|
webertj@33723
|
452 |
val t1_eq_nat_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
|
haftmann@24196
|
453 |
(Const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) $ n)
|
haftmann@35092
|
454 |
val t1_lt_zero = Const (@{const_name Orderings.less},
|
wenzelm@24092
|
455 |
HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
|
wenzelm@24092
|
456 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
webertj@33723
|
457 |
val subgoal1 = (HOLogic.mk_Trueprop t1_eq_nat_n) :: terms1 @ [not_false]
|
wenzelm@24092
|
458 |
val subgoal2 = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
|
wenzelm@24092
|
459 |
in
|
wenzelm@24092
|
460 |
SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
|
wenzelm@24092
|
461 |
end
|
webertj@33716
|
462 |
(* ?P ((?n::nat) mod (number_of ?k)) =
|
wenzelm@24092
|
463 |
((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) -->
|
wenzelm@24092
|
464 |
(ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *)
|
wenzelm@24092
|
465 |
| (Const ("Divides.div_class.mod", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
|
wenzelm@24092
|
466 |
let
|
wenzelm@24092
|
467 |
val rev_terms = rev terms
|
haftmann@35267
|
468 |
val zero = Const (@{const_name Groups.zero}, split_type)
|
wenzelm@24092
|
469 |
val i = Bound 1
|
wenzelm@24092
|
470 |
val j = Bound 0
|
wenzelm@24092
|
471 |
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
|
wenzelm@24092
|
472 |
val terms2 = map (subst_term [(incr_boundvars 2 split_term, j)])
|
wenzelm@24092
|
473 |
(map (incr_boundvars 2) rev_terms)
|
wenzelm@24092
|
474 |
val t1' = incr_boundvars 2 t1
|
wenzelm@24092
|
475 |
val t2' = incr_boundvars 2 t2
|
wenzelm@24092
|
476 |
val t2_eq_zero = Const ("op =",
|
wenzelm@24092
|
477 |
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
|
wenzelm@24092
|
478 |
val t2_neq_zero = HOLogic.mk_not (Const ("op =",
|
wenzelm@24092
|
479 |
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
|
haftmann@35092
|
480 |
val j_lt_t2 = Const (@{const_name Orderings.less},
|
wenzelm@24092
|
481 |
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
|
wenzelm@24092
|
482 |
val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
|
haftmann@35267
|
483 |
(Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
|
haftmann@35267
|
484 |
(Const (@{const_name Groups.times},
|
wenzelm@24092
|
485 |
split_type --> split_type --> split_type) $ t2' $ i) $ j)
|
wenzelm@24092
|
486 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
wenzelm@24092
|
487 |
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
|
wenzelm@24092
|
488 |
val subgoal2 = (map HOLogic.mk_Trueprop
|
wenzelm@24092
|
489 |
[t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
|
wenzelm@24092
|
490 |
@ terms2 @ [not_false]
|
wenzelm@24092
|
491 |
in
|
wenzelm@24092
|
492 |
SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
|
wenzelm@24092
|
493 |
end
|
webertj@33716
|
494 |
(* ?P ((?n::nat) div (number_of ?k)) =
|
wenzelm@24092
|
495 |
((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) -->
|
wenzelm@24092
|
496 |
(ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *)
|
wenzelm@24092
|
497 |
| (Const ("Divides.div_class.div", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
|
wenzelm@24092
|
498 |
let
|
wenzelm@24092
|
499 |
val rev_terms = rev terms
|
haftmann@35267
|
500 |
val zero = Const (@{const_name Groups.zero}, split_type)
|
wenzelm@24092
|
501 |
val i = Bound 1
|
wenzelm@24092
|
502 |
val j = Bound 0
|
wenzelm@24092
|
503 |
val terms1 = map (subst_term [(split_term, zero)]) rev_terms
|
wenzelm@24092
|
504 |
val terms2 = map (subst_term [(incr_boundvars 2 split_term, i)])
|
wenzelm@24092
|
505 |
(map (incr_boundvars 2) rev_terms)
|
wenzelm@24092
|
506 |
val t1' = incr_boundvars 2 t1
|
wenzelm@24092
|
507 |
val t2' = incr_boundvars 2 t2
|
wenzelm@24092
|
508 |
val t2_eq_zero = Const ("op =",
|
wenzelm@24092
|
509 |
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
|
wenzelm@24092
|
510 |
val t2_neq_zero = HOLogic.mk_not (Const ("op =",
|
wenzelm@24092
|
511 |
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
|
haftmann@35092
|
512 |
val j_lt_t2 = Const (@{const_name Orderings.less},
|
wenzelm@24092
|
513 |
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
|
wenzelm@24092
|
514 |
val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
|
haftmann@35267
|
515 |
(Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
|
haftmann@35267
|
516 |
(Const (@{const_name Groups.times},
|
wenzelm@24092
|
517 |
split_type --> split_type --> split_type) $ t2' $ i) $ j)
|
wenzelm@24092
|
518 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
wenzelm@24092
|
519 |
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
|
wenzelm@24092
|
520 |
val subgoal2 = (map HOLogic.mk_Trueprop
|
wenzelm@24092
|
521 |
[t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
|
wenzelm@24092
|
522 |
@ terms2 @ [not_false]
|
wenzelm@24092
|
523 |
in
|
wenzelm@24092
|
524 |
SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
|
wenzelm@24092
|
525 |
end
|
webertj@33716
|
526 |
(* ?P ((?n::int) mod (number_of ?k)) =
|
webertj@33723
|
527 |
((number_of ?k = 0 --> ?P ?n) &
|
webertj@33723
|
528 |
(0 < number_of ?k -->
|
webertj@33723
|
529 |
(ALL i j.
|
webertj@33723
|
530 |
0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) &
|
webertj@33723
|
531 |
(number_of ?k < 0 -->
|
webertj@33723
|
532 |
(ALL i j.
|
webertj@33723
|
533 |
number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *)
|
wenzelm@24092
|
534 |
| (Const ("Divides.div_class.mod",
|
webertj@33723
|
535 |
Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
|
wenzelm@24092
|
536 |
let
|
wenzelm@24092
|
537 |
val rev_terms = rev terms
|
haftmann@35267
|
538 |
val zero = Const (@{const_name Groups.zero}, split_type)
|
wenzelm@24092
|
539 |
val i = Bound 1
|
wenzelm@24092
|
540 |
val j = Bound 0
|
wenzelm@24092
|
541 |
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
|
wenzelm@24092
|
542 |
val terms2_3 = map (subst_term [(incr_boundvars 2 split_term, j)])
|
wenzelm@24092
|
543 |
(map (incr_boundvars 2) rev_terms)
|
wenzelm@24092
|
544 |
val t1' = incr_boundvars 2 t1
|
webertj@33723
|
545 |
val t2' = incr_boundvars 2 t2
|
webertj@33723
|
546 |
val t2_eq_zero = Const ("op =",
|
webertj@33723
|
547 |
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
|
haftmann@35092
|
548 |
val zero_lt_t2 = Const (@{const_name Orderings.less},
|
webertj@33723
|
549 |
split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
|
haftmann@35092
|
550 |
val t2_lt_zero = Const (@{const_name Orderings.less},
|
webertj@33723
|
551 |
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
|
haftmann@35092
|
552 |
val zero_leq_j = Const (@{const_name Orderings.less_eq},
|
wenzelm@24092
|
553 |
split_type --> split_type --> HOLogic.boolT) $ zero $ j
|
haftmann@35092
|
554 |
val j_leq_zero = Const (@{const_name Orderings.less_eq},
|
webertj@33723
|
555 |
split_type --> split_type --> HOLogic.boolT) $ j $ zero
|
haftmann@35092
|
556 |
val j_lt_t2 = Const (@{const_name Orderings.less},
|
wenzelm@24092
|
557 |
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
|
haftmann@35092
|
558 |
val t2_lt_j = Const (@{const_name Orderings.less},
|
webertj@33723
|
559 |
split_type --> split_type--> HOLogic.boolT) $ t2' $ j
|
wenzelm@24092
|
560 |
val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
|
haftmann@35267
|
561 |
(Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
|
haftmann@35267
|
562 |
(Const (@{const_name Groups.times},
|
wenzelm@24092
|
563 |
split_type --> split_type --> split_type) $ t2' $ i) $ j)
|
wenzelm@24092
|
564 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
webertj@33723
|
565 |
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
|
webertj@33723
|
566 |
val subgoal2 = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
|
wenzelm@24092
|
567 |
@ hd terms2_3
|
wenzelm@24092
|
568 |
:: (if tl terms2_3 = [] then [not_false] else [])
|
wenzelm@24092
|
569 |
@ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
|
wenzelm@24092
|
570 |
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
|
webertj@33723
|
571 |
val subgoal3 = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
|
wenzelm@24092
|
572 |
@ hd terms2_3
|
wenzelm@24092
|
573 |
:: (if tl terms2_3 = [] then [not_false] else [])
|
wenzelm@24092
|
574 |
@ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
|
wenzelm@24092
|
575 |
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
|
wenzelm@24092
|
576 |
val Ts' = split_type :: split_type :: Ts
|
wenzelm@24092
|
577 |
in
|
wenzelm@24092
|
578 |
SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
|
wenzelm@24092
|
579 |
end
|
webertj@33716
|
580 |
(* ?P ((?n::int) div (number_of ?k)) =
|
webertj@33723
|
581 |
((number_of ?k = 0 --> ?P 0) &
|
webertj@33723
|
582 |
(0 < number_of ?k -->
|
webertj@33723
|
583 |
(ALL i j.
|
webertj@33723
|
584 |
0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P i)) &
|
webertj@33723
|
585 |
(number_of ?k < 0 -->
|
webertj@33723
|
586 |
(ALL i j.
|
webertj@33723
|
587 |
number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P i))) *)
|
wenzelm@24092
|
588 |
| (Const ("Divides.div_class.div",
|
webertj@33723
|
589 |
Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
|
wenzelm@24092
|
590 |
let
|
wenzelm@24092
|
591 |
val rev_terms = rev terms
|
haftmann@35267
|
592 |
val zero = Const (@{const_name Groups.zero}, split_type)
|
wenzelm@24092
|
593 |
val i = Bound 1
|
wenzelm@24092
|
594 |
val j = Bound 0
|
wenzelm@24092
|
595 |
val terms1 = map (subst_term [(split_term, zero)]) rev_terms
|
wenzelm@24092
|
596 |
val terms2_3 = map (subst_term [(incr_boundvars 2 split_term, i)])
|
wenzelm@24092
|
597 |
(map (incr_boundvars 2) rev_terms)
|
wenzelm@24092
|
598 |
val t1' = incr_boundvars 2 t1
|
webertj@33723
|
599 |
val t2' = incr_boundvars 2 t2
|
webertj@33723
|
600 |
val t2_eq_zero = Const ("op =",
|
webertj@33723
|
601 |
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
|
haftmann@35092
|
602 |
val zero_lt_t2 = Const (@{const_name Orderings.less},
|
webertj@33723
|
603 |
split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
|
haftmann@35092
|
604 |
val t2_lt_zero = Const (@{const_name Orderings.less},
|
webertj@33723
|
605 |
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
|
haftmann@35092
|
606 |
val zero_leq_j = Const (@{const_name Orderings.less_eq},
|
wenzelm@24092
|
607 |
split_type --> split_type --> HOLogic.boolT) $ zero $ j
|
haftmann@35092
|
608 |
val j_leq_zero = Const (@{const_name Orderings.less_eq},
|
webertj@33723
|
609 |
split_type --> split_type --> HOLogic.boolT) $ j $ zero
|
haftmann@35092
|
610 |
val j_lt_t2 = Const (@{const_name Orderings.less},
|
wenzelm@24092
|
611 |
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
|
haftmann@35092
|
612 |
val t2_lt_j = Const (@{const_name Orderings.less},
|
webertj@33723
|
613 |
split_type --> split_type--> HOLogic.boolT) $ t2' $ j
|
webertj@33723
|
614 |
val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
|
haftmann@35267
|
615 |
(Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
|
haftmann@35267
|
616 |
(Const (@{const_name Groups.times},
|
wenzelm@24092
|
617 |
split_type --> split_type --> split_type) $ t2' $ i) $ j)
|
wenzelm@24092
|
618 |
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
|
webertj@33723
|
619 |
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
|
webertj@33723
|
620 |
val subgoal2 = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
|
webertj@33723
|
621 |
@ hd terms2_3
|
webertj@33723
|
622 |
:: (if tl terms2_3 = [] then [not_false] else [])
|
webertj@33723
|
623 |
@ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
|
webertj@33723
|
624 |
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
|
webertj@33723
|
625 |
val subgoal3 = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
|
webertj@33723
|
626 |
@ hd terms2_3
|
webertj@33723
|
627 |
:: (if tl terms2_3 = [] then [not_false] else [])
|
webertj@33723
|
628 |
@ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
|
webertj@33723
|
629 |
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
|
wenzelm@24092
|
630 |
val Ts' = split_type :: split_type :: Ts
|
wenzelm@24092
|
631 |
in
|
wenzelm@24092
|
632 |
SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
|
wenzelm@24092
|
633 |
end
|
wenzelm@24092
|
634 |
(* this will only happen if a split theorem can be applied for which no *)
|
wenzelm@24092
|
635 |
(* code exists above -- in which case either the split theorem should be *)
|
wenzelm@24092
|
636 |
(* implemented above, or 'is_split_thm' should be modified to filter it *)
|
wenzelm@24092
|
637 |
(* out *)
|
wenzelm@24092
|
638 |
| (t, ts) => (
|
wenzelm@24920
|
639 |
warning ("Lin. Arith.: split rule for " ^ Syntax.string_of_term ctxt t ^
|
webertj@32369
|
640 |
" (with " ^ string_of_int (length ts) ^
|
webertj@32369
|
641 |
" argument(s)) not implemented; proof reconstruction is likely to fail");
|
wenzelm@24092
|
642 |
NONE
|
wenzelm@24092
|
643 |
))
|
webertj@32369
|
644 |
end; (* split_once_items *)
|
wenzelm@24092
|
645 |
|
wenzelm@24092
|
646 |
(* remove terms that do not satisfy 'p'; change the order of the remaining *)
|
wenzelm@24092
|
647 |
(* terms in the same way as filter_prems_tac does *)
|
wenzelm@24092
|
648 |
|
wenzelm@24092
|
649 |
fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
|
wenzelm@24092
|
650 |
let
|
wenzelm@33346
|
651 |
fun filter_prems t (left, right) =
|
wenzelm@33346
|
652 |
if p t then (left, right @ [t]) else (left @ right, [])
|
wenzelm@33346
|
653 |
val (left, right) = fold filter_prems terms ([], [])
|
wenzelm@24092
|
654 |
in
|
wenzelm@24092
|
655 |
right @ left
|
wenzelm@24092
|
656 |
end;
|
wenzelm@24092
|
657 |
|
wenzelm@24092
|
658 |
(* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a *)
|
wenzelm@24092
|
659 |
(* subgoal that has 'terms' as premises *)
|
wenzelm@24092
|
660 |
|
wenzelm@24092
|
661 |
fun negated_term_occurs_positively (terms : term list) : bool =
|
wenzelm@24092
|
662 |
List.exists
|
webertj@32369
|
663 |
(fn (Trueprop $ (Const ("Not", _) $ t)) =>
|
webertj@32369
|
664 |
member Pattern.aeconv terms (Trueprop $ t)
|
webertj@32369
|
665 |
| _ => false)
|
wenzelm@24092
|
666 |
terms;
|
wenzelm@24092
|
667 |
|
wenzelm@24092
|
668 |
fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
|
wenzelm@24092
|
669 |
let
|
wenzelm@24092
|
670 |
(* repeatedly split (including newly emerging subgoals) until no further *)
|
wenzelm@24092
|
671 |
(* splitting is possible *)
|
webertj@32369
|
672 |
fun split_loop ([] : (typ list * term list) list) =
|
webertj@32369
|
673 |
([] : (typ list * term list) list)
|
webertj@32369
|
674 |
| split_loop (subgoal::subgoals) =
|
webertj@32369
|
675 |
(case split_once_items ctxt subgoal of
|
webertj@32369
|
676 |
SOME new_subgoals => split_loop (new_subgoals @ subgoals)
|
webertj@32369
|
677 |
| NONE => subgoal :: split_loop subgoals)
|
wenzelm@33035
|
678 |
fun is_relevant t = is_some (decomp ctxt t)
|
wenzelm@24092
|
679 |
(* filter_prems_tac is_relevant: *)
|
wenzelm@24092
|
680 |
val relevant_terms = filter_prems_tac_items is_relevant terms
|
wenzelm@24092
|
681 |
(* split_tac, NNF normalization: *)
|
wenzelm@24092
|
682 |
val split_goals = split_loop [(Ts, relevant_terms)]
|
wenzelm@24092
|
683 |
(* necessary because split_once_tac may normalize terms: *)
|
webertj@32369
|
684 |
val beta_eta_norm = map (apsnd (map (Envir.eta_contract o Envir.beta_norm)))
|
webertj@32369
|
685 |
split_goals
|
wenzelm@24092
|
686 |
(* TRY (etac notE) THEN eq_assume_tac: *)
|
wenzelm@33325
|
687 |
val result = filter_out (negated_term_occurs_positively o snd)
|
webertj@32369
|
688 |
beta_eta_norm
|
wenzelm@24092
|
689 |
in
|
wenzelm@24092
|
690 |
result
|
wenzelm@24092
|
691 |
end;
|
wenzelm@24092
|
692 |
|
wenzelm@24092
|
693 |
(* takes the i-th subgoal [| A1; ...; An |] ==> B to *)
|
wenzelm@24092
|
694 |
(* An --> ... --> A1 --> B, performs splitting with the given 'split_thms' *)
|
wenzelm@24092
|
695 |
(* (resulting in a different subgoal P), takes P to ~P ==> False, *)
|
wenzelm@24092
|
696 |
(* performs NNF-normalization of ~P, and eliminates conjunctions, *)
|
wenzelm@24092
|
697 |
(* disjunctions and existential quantifiers from the premises, possibly (in *)
|
wenzelm@24092
|
698 |
(* the case of disjunctions) resulting in several new subgoals, each of the *)
|
wenzelm@24092
|
699 |
(* general form [| Q1; ...; Qm |] ==> False. Fails if more than *)
|
haftmann@31082
|
700 |
(* !split_limit splits are possible. *)
|
wenzelm@24092
|
701 |
|
wenzelm@24092
|
702 |
local
|
wenzelm@24092
|
703 |
val nnf_simpset =
|
wenzelm@24092
|
704 |
empty_ss setmkeqTrue mk_eq_True
|
wenzelm@24092
|
705 |
setmksimps (mksimps mksimps_pairs)
|
wenzelm@35410
|
706 |
addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
|
wenzelm@35410
|
707 |
@{thm de_Morgan_conj}, not_all, not_ex, not_not]
|
wenzelm@35230
|
708 |
fun prem_nnf_tac ss = full_simp_tac (Simplifier.inherit_context ss nnf_simpset)
|
wenzelm@24092
|
709 |
in
|
wenzelm@24092
|
710 |
|
wenzelm@35230
|
711 |
fun split_once_tac ss split_thms =
|
wenzelm@24092
|
712 |
let
|
wenzelm@35230
|
713 |
val ctxt = Simplifier.the_context ss
|
wenzelm@24092
|
714 |
val thy = ProofContext.theory_of ctxt
|
wenzelm@24092
|
715 |
val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
|
wenzelm@24092
|
716 |
let
|
wenzelm@24092
|
717 |
val Ts = rev (map snd (Logic.strip_params subgoal))
|
wenzelm@24092
|
718 |
val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
|
wenzelm@24092
|
719 |
val cmap = Splitter.cmap_of_split_thms split_thms
|
wenzelm@24092
|
720 |
val splits = Splitter.split_posns cmap thy Ts concl
|
wenzelm@24092
|
721 |
in
|
webertj@32369
|
722 |
if null splits orelse length splits > Config.get ctxt split_limit then
|
webertj@32369
|
723 |
no_tac
|
webertj@32369
|
724 |
else if null (#2 (hd splits)) then
|
webertj@32369
|
725 |
split_tac split_thms i
|
webertj@32369
|
726 |
else
|
webertj@32369
|
727 |
(* disallow a split that involves non-locally bound variables *)
|
webertj@32369
|
728 |
(* (except when bound by outermost meta-quantifiers) *)
|
webertj@32369
|
729 |
no_tac
|
wenzelm@24092
|
730 |
end)
|
wenzelm@24092
|
731 |
in
|
wenzelm@24092
|
732 |
EVERY' [
|
wenzelm@24092
|
733 |
REPEAT_DETERM o etac rev_mp,
|
wenzelm@24092
|
734 |
cond_split_tac,
|
wenzelm@24092
|
735 |
rtac ccontr,
|
wenzelm@35230
|
736 |
prem_nnf_tac ss,
|
wenzelm@24092
|
737 |
TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE))
|
wenzelm@24092
|
738 |
]
|
wenzelm@24092
|
739 |
end;
|
wenzelm@24092
|
740 |
|
wenzelm@24092
|
741 |
end; (* local *)
|
wenzelm@24092
|
742 |
|
wenzelm@24092
|
743 |
(* remove irrelevant premises, then split the i-th subgoal (and all new *)
|
wenzelm@24092
|
744 |
(* subgoals) by using 'split_once_tac' repeatedly. Beta-eta-normalize new *)
|
wenzelm@24092
|
745 |
(* subgoals and finally attempt to solve them by finding an immediate *)
|
webertj@32369
|
746 |
(* contradiction (i.e., a term and its negation) in their premises. *)
|
wenzelm@24092
|
747 |
|
wenzelm@35230
|
748 |
fun pre_tac ss i =
|
wenzelm@24092
|
749 |
let
|
wenzelm@35230
|
750 |
val ctxt = Simplifier.the_context ss;
|
wenzelm@24092
|
751 |
val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
|
wenzelm@33035
|
752 |
fun is_relevant t = is_some (decomp ctxt t)
|
wenzelm@24092
|
753 |
in
|
wenzelm@24092
|
754 |
DETERM (
|
wenzelm@24092
|
755 |
TRY (filter_prems_tac is_relevant i)
|
wenzelm@24092
|
756 |
THEN (
|
wenzelm@35230
|
757 |
(TRY o REPEAT_ALL_NEW (split_once_tac ss split_thms))
|
wenzelm@24092
|
758 |
THEN_ALL_NEW
|
wenzelm@24092
|
759 |
(CONVERSION Drule.beta_eta_conversion
|
wenzelm@24092
|
760 |
THEN'
|
wenzelm@24092
|
761 |
(TRY o (etac notE THEN' eq_assume_tac)))
|
wenzelm@24092
|
762 |
) i
|
wenzelm@24092
|
763 |
)
|
wenzelm@24092
|
764 |
end;
|
wenzelm@24092
|
765 |
|
haftmann@31100
|
766 |
end; (* LA_Data *)
|
wenzelm@24092
|
767 |
|
wenzelm@24092
|
768 |
|
haftmann@31100
|
769 |
val pre_tac = LA_Data.pre_tac;
|
wenzelm@24092
|
770 |
|
haftmann@31100
|
771 |
structure Fast_Arith = Fast_Lin_Arith(structure LA_Logic = LA_Logic and LA_Data = LA_Data);
|
wenzelm@24092
|
772 |
|
boehmes@31510
|
773 |
fun map_inj_thms f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
|
haftmann@31100
|
774 |
{add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = f inj_thms,
|
boehmes@31510
|
775 |
lessD = lessD, neqE = neqE, simpset = simpset, number_of = number_of};
|
haftmann@31100
|
776 |
|
boehmes@31510
|
777 |
fun map_lessD f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
|
haftmann@31100
|
778 |
{add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
|
boehmes@31510
|
779 |
lessD = f lessD, neqE = neqE, simpset = simpset, number_of = number_of};
|
haftmann@31100
|
780 |
|
boehmes@31510
|
781 |
fun map_simpset f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
|
haftmann@31100
|
782 |
{add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
|
boehmes@31510
|
783 |
lessD = lessD, neqE = neqE, simpset = f simpset, number_of = number_of};
|
boehmes@31510
|
784 |
|
boehmes@31510
|
785 |
fun map_number_of f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
|
boehmes@31510
|
786 |
{add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
|
boehmes@31510
|
787 |
lessD = lessD, neqE = neqE, simpset = simpset, number_of = f number_of};
|
haftmann@31100
|
788 |
|
haftmann@31100
|
789 |
fun add_inj_thms thms = Fast_Arith.map_data (map_inj_thms (append thms));
|
haftmann@31100
|
790 |
fun add_lessD thm = Fast_Arith.map_data (map_lessD (fn thms => thms @ [thm]));
|
haftmann@31100
|
791 |
fun add_simps thms = Fast_Arith.map_data (map_simpset (fn simpset => simpset addsimps thms));
|
haftmann@31100
|
792 |
fun add_simprocs procs = Fast_Arith.map_data (map_simpset (fn simpset => simpset addsimprocs procs));
|
haftmann@31100
|
793 |
|
boehmes@31510
|
794 |
fun set_number_of f = Fast_Arith.map_data (map_number_of (K (serial (), f)))
|
boehmes@31510
|
795 |
|
boehmes@31510
|
796 |
|
haftmann@31101
|
797 |
fun simple_tac ctxt = Fast_Arith.lin_arith_tac ctxt false;
|
haftmann@31101
|
798 |
val lin_arith_tac = Fast_Arith.lin_arith_tac;
|
haftmann@31082
|
799 |
val trace = Fast_Arith.trace;
|
wenzelm@27017
|
800 |
val warning_count = Fast_Arith.warning_count;
|
wenzelm@24092
|
801 |
|
wenzelm@24092
|
802 |
(* reduce contradictory <= to False.
|
wenzelm@24092
|
803 |
Most of the work is done by the cancel tactics. *)
|
wenzelm@24092
|
804 |
|
wenzelm@24092
|
805 |
val init_arith_data =
|
boehmes@31510
|
806 |
Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, number_of, ...} =>
|
haftmann@35028
|
807 |
{add_mono_thms = @{thms add_mono_thms_linordered_semiring} @ @{thms add_mono_thms_linordered_field} @ add_mono_thms,
|
boehmes@31510
|
808 |
mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} ::
|
boehmes@31510
|
809 |
@{lemma "a = b ==> c*a = c*b" by (rule arg_cong)} :: mult_mono_thms,
|
wenzelm@24092
|
810 |
inj_thms = inj_thms,
|
haftmann@31082
|
811 |
lessD = lessD @ [@{thm "Suc_leI"}],
|
haftmann@35028
|
812 |
neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_linordered_idom}],
|
wenzelm@24092
|
813 |
simpset = HOL_basic_ss
|
boehmes@31510
|
814 |
addsimps @{thms ring_distribs}
|
boehmes@31510
|
815 |
addsimps [@{thm if_True}, @{thm if_False}]
|
wenzelm@24092
|
816 |
addsimps
|
haftmann@35050
|
817 |
[@{thm add_0_left},
|
haftmann@35050
|
818 |
@{thm add_0_right},
|
wenzelm@24092
|
819 |
@{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
|
wenzelm@24092
|
820 |
@{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
|
wenzelm@24092
|
821 |
@{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
|
wenzelm@24092
|
822 |
@{thm "not_one_less_zero"}]
|
wenzelm@24092
|
823 |
addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv]
|
wenzelm@24092
|
824 |
(*abel_cancel helps it work in abstract algebraic domains*)
|
haftmann@31082
|
825 |
addsimprocs Nat_Arith.nat_cancel_sums_add
|
wenzelm@35410
|
826 |
addcongs [@{thm if_weak_cong}],
|
boehmes@31510
|
827 |
number_of = number_of}) #>
|
haftmann@31082
|
828 |
add_discrete_type @{type_name nat};
|
wenzelm@24092
|
829 |
|
nipkow@29786
|
830 |
fun add_arith_facts ss =
|
haftmann@30671
|
831 |
add_prems (Arith_Data.get_arith_facts (MetaSimplifier.the_context ss)) ss;
|
nipkow@29786
|
832 |
|
haftmann@31101
|
833 |
val simproc = add_arith_facts #> Fast_Arith.lin_arith_simproc;
|
wenzelm@24092
|
834 |
|
wenzelm@24092
|
835 |
|
haftmann@26110
|
836 |
(* generic refutation procedure *)
|
haftmann@26110
|
837 |
|
haftmann@26110
|
838 |
(* parameters:
|
haftmann@26110
|
839 |
|
haftmann@26110
|
840 |
test: term -> bool
|
haftmann@26110
|
841 |
tests if a term is at all relevant to the refutation proof;
|
haftmann@26110
|
842 |
if not, then it can be discarded. Can improve performance,
|
haftmann@26110
|
843 |
esp. if disjunctions can be discarded (no case distinction needed!).
|
haftmann@26110
|
844 |
|
haftmann@26110
|
845 |
prep_tac: int -> tactic
|
haftmann@26110
|
846 |
A preparation tactic to be applied to the goal once all relevant premises
|
haftmann@26110
|
847 |
have been moved to the conclusion.
|
haftmann@26110
|
848 |
|
haftmann@26110
|
849 |
ref_tac: int -> tactic
|
haftmann@26110
|
850 |
the actual refutation tactic. Should be able to deal with goals
|
haftmann@26110
|
851 |
[| A1; ...; An |] ==> False
|
haftmann@26110
|
852 |
where the Ai are atomic, i.e. no top-level &, | or EX
|
haftmann@26110
|
853 |
*)
|
haftmann@26110
|
854 |
|
haftmann@26110
|
855 |
local
|
haftmann@26110
|
856 |
val nnf_simpset =
|
haftmann@26110
|
857 |
empty_ss setmkeqTrue mk_eq_True
|
haftmann@26110
|
858 |
setmksimps (mksimps mksimps_pairs)
|
haftmann@26110
|
859 |
addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
|
haftmann@26110
|
860 |
@{thm de_Morgan_conj}, @{thm not_all}, @{thm not_ex}, @{thm not_not}];
|
haftmann@26110
|
861 |
fun prem_nnf_tac i st =
|
wenzelm@35232
|
862 |
full_simp_tac (Simplifier.global_context (Thm.theory_of_thm st) nnf_simpset) i st;
|
haftmann@26110
|
863 |
in
|
haftmann@26110
|
864 |
fun refute_tac test prep_tac ref_tac =
|
haftmann@26110
|
865 |
let val refute_prems_tac =
|
haftmann@26110
|
866 |
REPEAT_DETERM
|
haftmann@26110
|
867 |
(eresolve_tac [@{thm conjE}, @{thm exE}] 1 ORELSE
|
haftmann@26110
|
868 |
filter_prems_tac test 1 ORELSE
|
haftmann@26110
|
869 |
etac @{thm disjE} 1) THEN
|
haftmann@26110
|
870 |
(DETERM (etac @{thm notE} 1 THEN eq_assume_tac 1) ORELSE
|
haftmann@26110
|
871 |
ref_tac 1);
|
haftmann@26110
|
872 |
in EVERY'[TRY o filter_prems_tac test,
|
haftmann@26110
|
873 |
REPEAT_DETERM o etac @{thm rev_mp}, prep_tac, rtac @{thm ccontr}, prem_nnf_tac,
|
haftmann@26110
|
874 |
SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
|
haftmann@26110
|
875 |
end;
|
haftmann@26110
|
876 |
end;
|
haftmann@26110
|
877 |
|
haftmann@26110
|
878 |
|
wenzelm@24092
|
879 |
(* arith proof method *)
|
wenzelm@24092
|
880 |
|
wenzelm@24092
|
881 |
local
|
wenzelm@24092
|
882 |
|
haftmann@31101
|
883 |
fun raw_tac ctxt ex =
|
wenzelm@33035
|
884 |
(* FIXME: K true should be replaced by a sensible test (perhaps "is_some o
|
wenzelm@24092
|
885 |
decomp sg"? -- but note that the test is applied to terms already before
|
wenzelm@24092
|
886 |
they are split/normalized) to speed things up in case there are lots of
|
wenzelm@24092
|
887 |
irrelevant terms involved; elimination of min/max can be optimized:
|
wenzelm@24092
|
888 |
(max m n + k <= r) = (m+k <= r & n+k <= r)
|
wenzelm@24092
|
889 |
(l <= min m n + k) = (l <= m+k & l <= n+k)
|
wenzelm@24092
|
890 |
*)
|
wenzelm@24092
|
891 |
refute_tac (K true)
|
webertj@33723
|
892 |
(* Splitting is also done inside simple_tac, but not completely -- *)
|
webertj@33723
|
893 |
(* split_tac may use split theorems that have not been implemented in *)
|
webertj@33723
|
894 |
(* simple_tac (cf. pre_decomp and split_once_items above), and *)
|
webertj@33723
|
895 |
(* split_limit may trigger. *)
|
webertj@33723
|
896 |
(* Therefore splitting outside of simple_tac may allow us to prove *)
|
webertj@33723
|
897 |
(* some goals that simple_tac alone would fail on. *)
|
wenzelm@24092
|
898 |
(REPEAT_DETERM o split_tac (#splits (get_arith_data ctxt)))
|
haftmann@31101
|
899 |
(lin_arith_tac ctxt ex);
|
wenzelm@24092
|
900 |
|
wenzelm@24092
|
901 |
in
|
wenzelm@24092
|
902 |
|
haftmann@31101
|
903 |
fun gen_tac ex ctxt = FIRST' [simple_tac ctxt,
|
wenzelm@35625
|
904 |
Object_Logic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_tac ctxt ex];
|
wenzelm@24092
|
905 |
|
haftmann@31101
|
906 |
val tac = gen_tac true;
|
wenzelm@24092
|
907 |
|
wenzelm@24092
|
908 |
end;
|
wenzelm@24092
|
909 |
|
wenzelm@24092
|
910 |
|
wenzelm@24092
|
911 |
(* context setup *)
|
wenzelm@24092
|
912 |
|
wenzelm@24092
|
913 |
val setup =
|
wenzelm@24092
|
914 |
init_arith_data #>
|
haftmann@31101
|
915 |
Simplifier.map_ss (fn ss => ss addsimprocs [Simplifier.simproc (@{theory}) "fast_nat_arith"
|
haftmann@31101
|
916 |
["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] (K simproc)]
|
haftmann@31101
|
917 |
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
|
haftmann@31101
|
918 |
useful to detect inconsistencies among the premises for subgoals which are
|
haftmann@31101
|
919 |
*not* themselves (in)equalities, because the latter activate
|
haftmann@31101
|
920 |
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
|
haftmann@31101
|
921 |
solver all the time rather than add the additional check. *)
|
nipkow@29787
|
922 |
addSolver (mk_solver' "lin_arith"
|
haftmann@31100
|
923 |
(add_arith_facts #> Fast_Arith.cut_lin_arith_tac)))
|
haftmann@31100
|
924 |
|
haftmann@31100
|
925 |
val global_setup =
|
haftmann@31100
|
926 |
setup_split_limit #> setup_neq_limit #>
|
haftmann@31100
|
927 |
Attrib.setup @{binding arith_split} (Scan.succeed (Thm.declaration_attribute add_split))
|
haftmann@31100
|
928 |
"declaration of split rules for arithmetic procedure" #>
|
haftmann@31100
|
929 |
Method.setup @{binding linarith}
|
wenzelm@33596
|
930 |
(Scan.succeed (fn ctxt =>
|
haftmann@31100
|
931 |
METHOD (fn facts =>
|
wenzelm@33596
|
932 |
HEADGOAL (Method.insert_tac (Arith_Data.get_arith_facts ctxt @ facts)
|
haftmann@31101
|
933 |
THEN' tac ctxt)))) "linear arithmetic" #>
|
haftmann@31101
|
934 |
Arith_Data.add_tactic "linear arithmetic" gen_tac;
|
wenzelm@24092
|
935 |
|
wenzelm@24092
|
936 |
end;
|