nipkow@5982
|
1 |
(* Title: Provers/Arith/fast_lin_arith.ML
|
nipkow@5982
|
2 |
ID: $Id$
|
nipkow@5982
|
3 |
Author: Tobias Nipkow
|
nipkow@5982
|
4 |
Copyright 1998 TU Munich
|
nipkow@5982
|
5 |
|
nipkow@6062
|
6 |
A generic linear arithmetic package.
|
nipkow@6102
|
7 |
It provides two tactics
|
nipkow@6102
|
8 |
|
nipkow@5982
|
9 |
lin_arith_tac: int -> tactic
|
nipkow@5982
|
10 |
cut_lin_arith_tac: thms -> int -> tactic
|
nipkow@6102
|
11 |
|
nipkow@6102
|
12 |
and a simplification procedure
|
nipkow@6102
|
13 |
|
nipkow@6102
|
14 |
lin_arith_prover: Sign.sg -> thm list -> term -> thm option
|
nipkow@6102
|
15 |
|
nipkow@6102
|
16 |
Only take premises and conclusions into account that are already (negated)
|
nipkow@6102
|
17 |
(in)equations. lin_arith_prover tries to prove or disprove the term.
|
nipkow@5982
|
18 |
*)
|
nipkow@5982
|
19 |
|
nipkow@5982
|
20 |
(*** Data needed for setting up the linear arithmetic package ***)
|
nipkow@5982
|
21 |
|
nipkow@6102
|
22 |
signature LIN_ARITH_LOGIC =
|
nipkow@6102
|
23 |
sig
|
nipkow@6102
|
24 |
val conjI: thm
|
nipkow@6102
|
25 |
val ccontr: thm (* (~ P ==> False) ==> P *)
|
nipkow@6102
|
26 |
val neqE: thm (* [| m ~= n; m < n ==> P; n < m ==> P |] ==> P *)
|
nipkow@6102
|
27 |
val notI: thm (* (P ==> False) ==> ~ P *)
|
nipkow@6110
|
28 |
val not_lessD: thm (* ~(m < n) ==> n <= m *)
|
nipkow@6128
|
29 |
val not_leD: thm (* ~(m <= n) ==> n < m *)
|
nipkow@6102
|
30 |
val sym: thm (* x = y ==> y = x *)
|
nipkow@6102
|
31 |
val mk_Eq: thm -> thm
|
nipkow@6102
|
32 |
val mk_Trueprop: term -> term
|
nipkow@6102
|
33 |
val neg_prop: term -> term
|
nipkow@6102
|
34 |
val is_False: thm -> bool
|
nipkow@6128
|
35 |
val is_nat: typ list * term -> bool
|
nipkow@6128
|
36 |
val mk_nat_thm: Sign.sg -> term -> thm
|
nipkow@6102
|
37 |
end;
|
nipkow@6102
|
38 |
(*
|
nipkow@6102
|
39 |
mk_Eq(~in) = `in == False'
|
nipkow@6102
|
40 |
mk_Eq(in) = `in == True'
|
nipkow@6102
|
41 |
where `in' is an (in)equality.
|
nipkow@6102
|
42 |
|
nipkow@6102
|
43 |
neg_prop(t) = neg if t is wrapped up in Trueprop and
|
nipkow@6102
|
44 |
nt is the (logically) negated version of t, where the negation
|
nipkow@6102
|
45 |
of a negative term is the term itself (no double negation!);
|
nipkow@6128
|
46 |
|
nipkow@6128
|
47 |
is_nat(parameter-types,t) = t:nat
|
nipkow@6128
|
48 |
mk_nat_thm(t) = "0 <= t"
|
nipkow@6102
|
49 |
*)
|
nipkow@6102
|
50 |
|
nipkow@5982
|
51 |
signature LIN_ARITH_DATA =
|
nipkow@5982
|
52 |
sig
|
nipkow@6128
|
53 |
val add_mono_thms: thm list ref
|
nipkow@5982
|
54 |
(* [| i rel1 j; m rel2 n |] ==> i + m rel3 j + n *)
|
nipkow@6128
|
55 |
val lessD: thm list ref (* m < n ==> m+1 <= n *)
|
nipkow@6128
|
56 |
val decomp:
|
nipkow@7551
|
57 |
term ->
|
nipkow@7551
|
58 |
((term * int)list * int * string * (term * int)list * int * bool)option
|
nipkow@6128
|
59 |
val simp: (thm -> thm) ref
|
nipkow@5982
|
60 |
end;
|
nipkow@5982
|
61 |
(*
|
nipkow@7551
|
62 |
decomp(`x Rel y') should yield (p,i,Rel,q,j,d)
|
nipkow@5982
|
63 |
where Rel is one of "<", "~<", "<=", "~<=" and "=" and
|
nipkow@5982
|
64 |
p/q is the decomposition of the sum terms x/y into a list
|
nipkow@7551
|
65 |
of summand * multiplicity pairs and a constant summand and
|
nipkow@7551
|
66 |
d indicates if the domain is discrete.
|
nipkow@5982
|
67 |
|
nipkow@5982
|
68 |
simp must reduce contradictory <= to False.
|
nipkow@5982
|
69 |
It should also cancel common summands to keep <= reduced;
|
nipkow@5982
|
70 |
otherwise <= can grow to massive proportions.
|
nipkow@5982
|
71 |
*)
|
nipkow@5982
|
72 |
|
nipkow@6062
|
73 |
signature FAST_LIN_ARITH =
|
nipkow@6062
|
74 |
sig
|
nipkow@6074
|
75 |
val lin_arith_prover: Sign.sg -> thm list -> term -> thm option
|
nipkow@6062
|
76 |
val lin_arith_tac: int -> tactic
|
nipkow@6062
|
77 |
val cut_lin_arith_tac: thm list -> int -> tactic
|
nipkow@6062
|
78 |
end;
|
nipkow@6062
|
79 |
|
nipkow@6102
|
80 |
functor Fast_Lin_Arith(structure LA_Logic:LIN_ARITH_LOGIC
|
nipkow@6102
|
81 |
and LA_Data:LIN_ARITH_DATA) : FAST_LIN_ARITH =
|
nipkow@5982
|
82 |
struct
|
nipkow@5982
|
83 |
|
nipkow@5982
|
84 |
(*** A fast decision procedure ***)
|
nipkow@5982
|
85 |
(*** Code ported from HOL Light ***)
|
nipkow@6056
|
86 |
(* possible optimizations:
|
nipkow@6056
|
87 |
use (var,coeff) rep or vector rep tp save space;
|
nipkow@6056
|
88 |
treat non-negative atoms separately rather than adding 0 <= atom
|
nipkow@6056
|
89 |
*)
|
nipkow@5982
|
90 |
|
nipkow@5982
|
91 |
datatype lineq_type = Eq | Le | Lt;
|
nipkow@5982
|
92 |
|
nipkow@6056
|
93 |
datatype injust = Asm of int
|
nipkow@6056
|
94 |
| Nat of int (* index of atom *)
|
nipkow@6128
|
95 |
| LessD of injust
|
nipkow@6128
|
96 |
| NotLessD of injust
|
nipkow@6128
|
97 |
| NotLeD of injust
|
nipkow@7551
|
98 |
| NotLeDD of injust
|
nipkow@5982
|
99 |
| Multiplied of int * injust
|
nipkow@5982
|
100 |
| Added of injust * injust;
|
nipkow@5982
|
101 |
|
nipkow@5982
|
102 |
datatype lineq = Lineq of int * lineq_type * int list * injust;
|
nipkow@5982
|
103 |
|
nipkow@5982
|
104 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
105 |
(* Calculate new (in)equality type after addition. *)
|
nipkow@5982
|
106 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
107 |
|
nipkow@5982
|
108 |
fun find_add_type(Eq,x) = x
|
nipkow@5982
|
109 |
| find_add_type(x,Eq) = x
|
nipkow@5982
|
110 |
| find_add_type(_,Lt) = Lt
|
nipkow@5982
|
111 |
| find_add_type(Lt,_) = Lt
|
nipkow@5982
|
112 |
| find_add_type(Le,Le) = Le;
|
nipkow@5982
|
113 |
|
nipkow@5982
|
114 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
115 |
(* Multiply out an (in)equation. *)
|
nipkow@5982
|
116 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
117 |
|
nipkow@5982
|
118 |
fun multiply_ineq n (i as Lineq(k,ty,l,just)) =
|
nipkow@5982
|
119 |
if n = 1 then i
|
nipkow@5982
|
120 |
else if n = 0 andalso ty = Lt then sys_error "multiply_ineq"
|
nipkow@5982
|
121 |
else if n < 0 andalso (ty=Le orelse ty=Lt) then sys_error "multiply_ineq"
|
nipkow@5982
|
122 |
else Lineq(n * k,ty,map (apl(n,op * )) l,Multiplied(n,just));
|
nipkow@5982
|
123 |
|
nipkow@5982
|
124 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
125 |
(* Add together (in)equations. *)
|
nipkow@5982
|
126 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
127 |
|
nipkow@5982
|
128 |
fun add_ineq (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
|
nipkow@5982
|
129 |
let val l = map2 (op +) (l1,l2)
|
nipkow@5982
|
130 |
in Lineq(k1+k2,find_add_type(ty1,ty2),l,Added(just1,just2)) end;
|
nipkow@5982
|
131 |
|
nipkow@5982
|
132 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
133 |
(* Elimination of variable between a single pair of (in)equations. *)
|
nipkow@5982
|
134 |
(* If they're both inequalities, 1st coefficient must be +ve, 2nd -ve. *)
|
nipkow@5982
|
135 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
136 |
|
nipkow@5982
|
137 |
fun gcd x y =
|
nipkow@5982
|
138 |
let fun gxd x y =
|
nipkow@5982
|
139 |
if y = 0 then x else gxd y (x mod y)
|
nipkow@5982
|
140 |
in if x < y then gxd y x else gxd x y end;
|
nipkow@5982
|
141 |
|
nipkow@5982
|
142 |
fun lcm x y = (x * y) div gcd x y;
|
nipkow@5982
|
143 |
|
nipkow@5982
|
144 |
fun el 0 (h::_) = h
|
nipkow@5982
|
145 |
| el n (_::t) = el (n - 1) t
|
nipkow@5982
|
146 |
| el _ _ = sys_error "el";
|
nipkow@5982
|
147 |
|
nipkow@5982
|
148 |
fun elim_var v (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
|
nipkow@5982
|
149 |
let val c1 = el v l1 and c2 = el v l2
|
nipkow@5982
|
150 |
val m = lcm (abs c1) (abs c2)
|
nipkow@5982
|
151 |
val m1 = m div (abs c1) and m2 = m div (abs c2)
|
nipkow@5982
|
152 |
val (n1,n2) =
|
nipkow@5982
|
153 |
if (c1 >= 0) = (c2 >= 0)
|
nipkow@5982
|
154 |
then if ty1 = Eq then (~m1,m2)
|
nipkow@5982
|
155 |
else if ty2 = Eq then (m1,~m2)
|
nipkow@5982
|
156 |
else sys_error "elim_var"
|
nipkow@5982
|
157 |
else (m1,m2)
|
nipkow@5982
|
158 |
val (p1,p2) = if ty1=Eq andalso ty2=Eq andalso (n1 = ~1 orelse n2 = ~1)
|
nipkow@5982
|
159 |
then (~n1,~n2) else (n1,n2)
|
nipkow@5982
|
160 |
in add_ineq (multiply_ineq n1 i1) (multiply_ineq n2 i2) end;
|
nipkow@5982
|
161 |
|
nipkow@5982
|
162 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
163 |
(* The main refutation-finding code. *)
|
nipkow@5982
|
164 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
165 |
|
nipkow@5982
|
166 |
fun is_trivial (Lineq(_,_,l,_)) = forall (fn i => i=0) l;
|
nipkow@5982
|
167 |
|
nipkow@5982
|
168 |
fun is_answer (ans as Lineq(k,ty,l,_)) =
|
nipkow@5982
|
169 |
case ty of Eq => k <> 0 | Le => k > 0 | Lt => k >= 0;
|
nipkow@5982
|
170 |
|
nipkow@5982
|
171 |
fun calc_blowup l =
|
nipkow@5982
|
172 |
let val (p,n) = partition (apl(0,op<)) (filter (apl(0,op<>)) l)
|
nipkow@5982
|
173 |
in (length p) * (length n) end;
|
nipkow@5982
|
174 |
|
nipkow@5982
|
175 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
176 |
(* Main elimination code: *)
|
nipkow@5982
|
177 |
(* *)
|
nipkow@5982
|
178 |
(* (1) Looks for immediate solutions (false assertions with no variables). *)
|
nipkow@5982
|
179 |
(* *)
|
nipkow@5982
|
180 |
(* (2) If there are any equations, picks a variable with the lowest absolute *)
|
nipkow@5982
|
181 |
(* coefficient in any of them, and uses it to eliminate. *)
|
nipkow@5982
|
182 |
(* *)
|
nipkow@5982
|
183 |
(* (3) Otherwise, chooses a variable in the inequality to minimize the *)
|
nipkow@5982
|
184 |
(* blowup (number of consequences generated) and eliminates it. *)
|
nipkow@5982
|
185 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
186 |
|
nipkow@5982
|
187 |
fun allpairs f xs ys =
|
nipkow@5982
|
188 |
flat(map (fn x => map (fn y => f x y) ys) xs);
|
nipkow@5982
|
189 |
|
nipkow@5982
|
190 |
fun extract_first p =
|
nipkow@5982
|
191 |
let fun extract xs (y::ys) = if p y then (Some y,xs@ys)
|
nipkow@5982
|
192 |
else extract (y::xs) ys
|
nipkow@5982
|
193 |
| extract xs [] = (None,xs)
|
nipkow@5982
|
194 |
in extract [] end;
|
nipkow@5982
|
195 |
|
nipkow@7551
|
196 |
|
nipkow@6056
|
197 |
fun print_ineqs ineqs =
|
nipkow@6056
|
198 |
writeln(cat_lines(""::map (fn Lineq(c,t,l,_) =>
|
nipkow@6056
|
199 |
string_of_int c ^
|
nipkow@6056
|
200 |
(case t of Eq => " = " | Lt=> " < " | Le => " <= ") ^
|
nipkow@6056
|
201 |
commas(map string_of_int l)) ineqs));
|
nipkow@7551
|
202 |
|
nipkow@6056
|
203 |
|
nipkow@5982
|
204 |
fun elim ineqs =
|
nipkow@6056
|
205 |
let (*val dummy = print_ineqs ineqs;*)
|
nipkow@6056
|
206 |
val (triv,nontriv) = partition is_trivial ineqs in
|
nipkow@5982
|
207 |
if not(null triv)
|
nipkow@5982
|
208 |
then case find_first is_answer triv of
|
nipkow@5982
|
209 |
None => elim nontriv | some => some
|
nipkow@5982
|
210 |
else
|
nipkow@5982
|
211 |
if null nontriv then None else
|
nipkow@5982
|
212 |
let val (eqs,noneqs) = partition (fn (Lineq(_,ty,_,_)) => ty=Eq) nontriv in
|
nipkow@5982
|
213 |
if not(null eqs) then
|
nipkow@5982
|
214 |
let val clist = foldl (fn (cs,Lineq(_,_,l,_)) => l union cs) ([],eqs)
|
nipkow@5982
|
215 |
val sclist = sort (fn (x,y) => int_ord(abs(x),abs(y)))
|
nipkow@5982
|
216 |
(filter (fn i => i<>0) clist)
|
nipkow@5982
|
217 |
val c = hd sclist
|
nipkow@5982
|
218 |
val (Some(eq as Lineq(_,_,ceq,_)),othereqs) =
|
nipkow@5982
|
219 |
extract_first (fn Lineq(_,_,l,_) => c mem l) eqs
|
nipkow@5982
|
220 |
val v = find_index (fn k => k=c) ceq
|
nipkow@5982
|
221 |
val (ioth,roth) = partition (fn (Lineq(_,_,l,_)) => el v l = 0)
|
nipkow@5982
|
222 |
(othereqs @ noneqs)
|
nipkow@5982
|
223 |
val others = map (elim_var v eq) roth @ ioth
|
nipkow@5982
|
224 |
in elim others end
|
nipkow@5982
|
225 |
else
|
nipkow@5982
|
226 |
let val lists = map (fn (Lineq(_,_,l,_)) => l) noneqs
|
nipkow@5982
|
227 |
val numlist = 0 upto (length(hd lists) - 1)
|
nipkow@5982
|
228 |
val coeffs = map (fn i => map (el i) lists) numlist
|
nipkow@5982
|
229 |
val blows = map calc_blowup coeffs
|
nipkow@5982
|
230 |
val iblows = blows ~~ numlist
|
nipkow@5982
|
231 |
val nziblows = filter (fn (i,_) => i<>0) iblows
|
nipkow@5982
|
232 |
in if null nziblows then None else
|
nipkow@5982
|
233 |
let val (c,v) = hd(sort (fn (x,y) => int_ord(fst(x),fst(y))) nziblows)
|
nipkow@5982
|
234 |
val (no,yes) = partition (fn (Lineq(_,_,l,_)) => el v l = 0) ineqs
|
nipkow@5982
|
235 |
val (pos,neg) = partition(fn (Lineq(_,_,l,_)) => el v l > 0) yes
|
nipkow@5982
|
236 |
in elim (no @ allpairs (elim_var v) pos neg) end
|
nipkow@5982
|
237 |
end
|
nipkow@5982
|
238 |
end
|
nipkow@5982
|
239 |
end;
|
nipkow@5982
|
240 |
|
nipkow@5982
|
241 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
242 |
(* Translate back a proof. *)
|
nipkow@5982
|
243 |
(* ------------------------------------------------------------------------- *)
|
nipkow@5982
|
244 |
|
nipkow@6056
|
245 |
(* FIXME OPTIMIZE!!!!
|
nipkow@6056
|
246 |
Addition/Multiplication need i*t representation rather than t+t+...
|
nipkow@6056
|
247 |
|
nipkow@6056
|
248 |
Simplification may detect a contradiction 'prematurely' due to type
|
nipkow@6056
|
249 |
information: n+1 <= 0 is simplified to False and does not need to be crossed
|
nipkow@6056
|
250 |
with 0 <= n.
|
nipkow@6056
|
251 |
*)
|
nipkow@6056
|
252 |
local
|
nipkow@6056
|
253 |
exception FalseE of thm
|
nipkow@6056
|
254 |
in
|
nipkow@6074
|
255 |
fun mkthm sg asms just =
|
nipkow@7551
|
256 |
let val atoms = foldl (fn (ats,(lhs,_,_,rhs,_,_)) =>
|
nipkow@6056
|
257 |
map fst lhs union (map fst rhs union ats))
|
nipkow@7551
|
258 |
([], mapfilter (LA_Data.decomp o concl_of) asms)
|
nipkow@6056
|
259 |
|
nipkow@6056
|
260 |
fun addthms thm1 thm2 =
|
nipkow@6102
|
261 |
let val conj = thm1 RS (thm2 RS LA_Logic.conjI)
|
nipkow@5982
|
262 |
in the(get_first (fn th => Some(conj RS th) handle _ => None)
|
nipkow@6128
|
263 |
(!LA_Data.add_mono_thms))
|
nipkow@5982
|
264 |
end;
|
nipkow@5982
|
265 |
|
nipkow@5982
|
266 |
fun multn(n,thm) =
|
nipkow@5982
|
267 |
let fun mul(i,th) = if i=1 then th else mul(i-1, addthms thm th)
|
nipkow@6102
|
268 |
in if n < 0 then mul(~n,thm) RS LA_Logic.sym else mul(n,thm) end;
|
nipkow@5982
|
269 |
|
nipkow@6056
|
270 |
fun simp thm =
|
nipkow@6128
|
271 |
let val thm' = !LA_Data.simp thm
|
nipkow@6102
|
272 |
in if LA_Logic.is_False thm' then raise FalseE thm' else thm' end
|
nipkow@6056
|
273 |
|
nipkow@7551
|
274 |
fun mk(Asm i) = ((*writeln"Asm";prth*)(nth_elem(i,asms)))
|
nipkow@7552
|
275 |
| mk(Nat(i)) = ((*writeln"N";*)LA_Logic.mk_nat_thm sg (nth_elem(i,atoms)))
|
nipkow@7551
|
276 |
| mk(LessD(j)) = ((*writeln"L";prth*)(hd([mk j] RL !LA_Data.lessD)))
|
nipkow@7551
|
277 |
| mk(NotLeD(j)) = ((*writeln"NLe";prth*)(mk j RS LA_Logic.not_leD))
|
nipkow@7551
|
278 |
| mk(NotLeDD(j)) = ((*writeln"NLeD";prth*)(hd([mk j RS LA_Logic.not_leD] RL !LA_Data.lessD)))
|
nipkow@7551
|
279 |
| mk(NotLessD(j)) = ((*writeln"NL";prth*)(mk j RS LA_Logic.not_lessD))
|
nipkow@7570
|
280 |
| mk(Added(j1,j2)) = ((*writeln"+";prth*)(simp((*prth*)(addthms (mk j1) (mk j2)))))
|
nipkow@7552
|
281 |
| mk(Multiplied(n,j)) = ((*writeln"*";*)multn(n,mk j))
|
nipkow@5982
|
282 |
|
nipkow@7551
|
283 |
in (*writeln"mkthm";*)!LA_Data.simp(mk just) handle FalseE thm => thm end
|
nipkow@6056
|
284 |
end;
|
nipkow@5982
|
285 |
|
nipkow@5982
|
286 |
fun coeff poly atom = case assoc(poly,atom) of None => 0 | Some i => i;
|
nipkow@5982
|
287 |
|
nipkow@5982
|
288 |
fun mklineq atoms =
|
nipkow@5982
|
289 |
let val n = length atoms in
|
nipkow@7551
|
290 |
fn ((lhs,i,rel,rhs,j,discrete),k) =>
|
nipkow@5982
|
291 |
let val lhsa = map (coeff lhs) atoms
|
nipkow@5982
|
292 |
and rhsa = map (coeff rhs) atoms
|
nipkow@5982
|
293 |
val diff = map2 (op -) (rhsa,lhsa)
|
nipkow@5982
|
294 |
val c = i-j
|
nipkow@6056
|
295 |
val just = Asm k
|
nipkow@5982
|
296 |
in case rel of
|
nipkow@5982
|
297 |
"<=" => Some(Lineq(c,Le,diff,just))
|
nipkow@7551
|
298 |
| "~<=" => if discrete
|
nipkow@7551
|
299 |
then Some(Lineq(1-c,Le,map (op ~) diff,NotLeDD(just)))
|
nipkow@7551
|
300 |
else Some(Lineq(~c,Lt,map (op ~) diff,NotLeD(just)))
|
nipkow@7551
|
301 |
| "<" => if discrete
|
nipkow@7551
|
302 |
then Some(Lineq(c+1,Le,diff,LessD(just)))
|
nipkow@7551
|
303 |
else Some(Lineq(c,Lt,diff,just))
|
nipkow@6128
|
304 |
| "~<" => Some(Lineq(~c,Le,map (op~) diff,NotLessD(just)))
|
nipkow@5982
|
305 |
| "=" => Some(Lineq(c,Eq,diff,just))
|
nipkow@5982
|
306 |
| "~=" => None
|
nipkow@5982
|
307 |
| _ => sys_error("mklineq" ^ rel)
|
nipkow@5982
|
308 |
end
|
nipkow@5982
|
309 |
end;
|
nipkow@5982
|
310 |
|
nipkow@6056
|
311 |
fun mknat pTs ixs (atom,i) =
|
nipkow@6128
|
312 |
if LA_Logic.is_nat(pTs,atom)
|
nipkow@6056
|
313 |
then let val l = map (fn j => if j=i then 1 else 0) ixs
|
nipkow@6056
|
314 |
in Some(Lineq(0,Le,l,Nat(i))) end
|
nipkow@6056
|
315 |
else None
|
nipkow@6056
|
316 |
|
nipkow@6056
|
317 |
fun abstract pTs items =
|
nipkow@7551
|
318 |
let val atoms = foldl (fn (ats,((lhs,_,_,rhs,_,_),_)) =>
|
nipkow@5982
|
319 |
(map fst lhs) union ((map fst rhs) union ats))
|
nipkow@5982
|
320 |
([],items)
|
nipkow@6056
|
321 |
val ixs = 0 upto (length(atoms)-1)
|
nipkow@6056
|
322 |
val iatoms = atoms ~~ ixs
|
nipkow@6056
|
323 |
in mapfilter (mklineq atoms) items @ mapfilter (mknat pTs ixs) iatoms end;
|
nipkow@5982
|
324 |
|
nipkow@5982
|
325 |
(* Ordinary refutation *)
|
nipkow@6074
|
326 |
fun refute1(pTs,items) =
|
nipkow@6074
|
327 |
(case elim (abstract pTs items) of
|
nipkow@6074
|
328 |
None => []
|
nipkow@6074
|
329 |
| Some(Lineq(_,_,_,j)) => [j]);
|
nipkow@6074
|
330 |
|
nipkow@6074
|
331 |
fun refute1_tac(i,just) =
|
nipkow@6074
|
332 |
fn state =>
|
nipkow@6074
|
333 |
let val sg = #sign(rep_thm state)
|
nipkow@6102
|
334 |
in resolve_tac [LA_Logic.notI,LA_Logic.ccontr] i THEN
|
nipkow@6074
|
335 |
METAHYPS (fn asms => rtac (mkthm sg asms just) 1) i
|
nipkow@6074
|
336 |
end
|
nipkow@6074
|
337 |
state;
|
nipkow@5982
|
338 |
|
nipkow@5982
|
339 |
(* Double refutation caused by equality in conclusion *)
|
nipkow@7551
|
340 |
fun refute2(pTs,items, (rhs,i,_,lhs,j,d), nHs) =
|
nipkow@7551
|
341 |
(case elim (abstract pTs (items@[((rhs,i,"<",lhs,j,d),nHs)])) of
|
nipkow@6074
|
342 |
None => []
|
nipkow@5982
|
343 |
| Some(Lineq(_,_,_,j1)) =>
|
nipkow@7551
|
344 |
(case elim (abstract pTs (items@[((lhs,j,"<",rhs,i,d),nHs)])) of
|
nipkow@6074
|
345 |
None => []
|
nipkow@6074
|
346 |
| Some(Lineq(_,_,_,j2)) => [j1,j2]));
|
nipkow@6074
|
347 |
|
nipkow@6074
|
348 |
fun refute2_tac(i,just1,just2) =
|
nipkow@6074
|
349 |
fn state =>
|
nipkow@6074
|
350 |
let val sg = #sign(rep_thm state)
|
nipkow@6102
|
351 |
in rtac LA_Logic.ccontr i THEN rotate_tac ~1 i THEN etac LA_Logic.neqE i THEN
|
nipkow@6074
|
352 |
METAHYPS (fn asms => rtac (mkthm sg asms just1) 1) i THEN
|
nipkow@6074
|
353 |
METAHYPS (fn asms => rtac (mkthm sg asms just2) 1) i
|
nipkow@6074
|
354 |
end
|
nipkow@6074
|
355 |
state;
|
nipkow@6074
|
356 |
|
nipkow@6074
|
357 |
fun prove(pTs,Hs,concl) =
|
nipkow@6074
|
358 |
let val nHs = length Hs
|
nipkow@6074
|
359 |
val ixHs = Hs ~~ (0 upto (nHs-1))
|
nipkow@7551
|
360 |
val Hitems = mapfilter (fn (h,i) => case LA_Data.decomp h of
|
nipkow@6074
|
361 |
None => None | Some(it) => Some(it,i)) ixHs
|
nipkow@7551
|
362 |
in case LA_Data.decomp concl of
|
nipkow@6074
|
363 |
None => if null Hitems then [] else refute1(pTs,Hitems)
|
nipkow@7551
|
364 |
| Some(citem as (r,i,rel,l,j,d)) =>
|
nipkow@6074
|
365 |
if rel = "="
|
nipkow@6074
|
366 |
then refute2(pTs,Hitems,citem,nHs)
|
nipkow@6074
|
367 |
else let val neg::rel0 = explode rel
|
nipkow@6074
|
368 |
val nrel = if neg = "~" then implode rel0 else "~"^rel
|
nipkow@7551
|
369 |
in refute1(pTs, Hitems@[((r,i,nrel,l,j,d),nHs)]) end
|
nipkow@6074
|
370 |
end;
|
nipkow@5982
|
371 |
|
nipkow@5982
|
372 |
(*
|
nipkow@5982
|
373 |
Fast but very incomplete decider. Only premises and conclusions
|
nipkow@5982
|
374 |
that are already (negated) (in)equations are taken into account.
|
nipkow@5982
|
375 |
*)
|
nipkow@5982
|
376 |
val lin_arith_tac = SUBGOAL (fn (A,n) =>
|
nipkow@6056
|
377 |
let val pTs = rev(map snd (Logic.strip_params A))
|
nipkow@6056
|
378 |
val Hs = Logic.strip_assums_hyp A
|
nipkow@6074
|
379 |
val concl = Logic.strip_assums_concl A
|
nipkow@6074
|
380 |
in case prove(pTs,Hs,concl) of
|
nipkow@6074
|
381 |
[j] => refute1_tac(n,j)
|
nipkow@6074
|
382 |
| [j1,j2] => refute2_tac(n,j1,j2)
|
nipkow@6074
|
383 |
| _ => no_tac
|
nipkow@5982
|
384 |
end);
|
nipkow@5982
|
385 |
|
nipkow@5982
|
386 |
fun cut_lin_arith_tac thms i = cut_facts_tac thms i THEN lin_arith_tac i;
|
nipkow@5982
|
387 |
|
nipkow@6079
|
388 |
fun prover1(just,sg,thms,concl,pos) =
|
nipkow@6102
|
389 |
let val nconcl = LA_Logic.neg_prop concl
|
nipkow@6074
|
390 |
val cnconcl = cterm_of sg nconcl
|
nipkow@6074
|
391 |
val Fthm = mkthm sg (thms @ [assume cnconcl]) just
|
nipkow@6102
|
392 |
val contr = if pos then LA_Logic.ccontr else LA_Logic.notI
|
nipkow@6102
|
393 |
in Some(LA_Logic.mk_Eq ((implies_intr cnconcl Fthm) COMP contr)) end
|
nipkow@6074
|
394 |
handle _ => None;
|
nipkow@6074
|
395 |
|
nipkow@6074
|
396 |
(* handle thm with equality conclusion *)
|
nipkow@6074
|
397 |
fun prover2(just1,just2,sg,thms,concl) =
|
nipkow@6102
|
398 |
let val nconcl = LA_Logic.neg_prop concl (* m ~= n *)
|
nipkow@6074
|
399 |
val cnconcl = cterm_of sg nconcl
|
nipkow@6074
|
400 |
val neqthm = assume cnconcl
|
nipkow@6102
|
401 |
val casethm = neqthm COMP LA_Logic.neqE (* [|m<n ==> R; n<m ==> R|] ==> R *)
|
nipkow@6074
|
402 |
val [lessimp1,lessimp2] = prems_of casethm
|
nipkow@6074
|
403 |
val less1 = fst(Logic.dest_implies lessimp1) (* m<n *)
|
nipkow@6074
|
404 |
and less2 = fst(Logic.dest_implies lessimp2) (* n<m *)
|
nipkow@6074
|
405 |
val cless1 = cterm_of sg less1 and cless2 = cterm_of sg less2
|
nipkow@6074
|
406 |
val thm1 = mkthm sg (thms @ [assume cless1]) just1
|
nipkow@6074
|
407 |
and thm2 = mkthm sg (thms @ [assume cless2]) just2
|
nipkow@6074
|
408 |
val dthm1 = implies_intr cless1 thm1 and dthm2 = implies_intr cless2 thm2
|
nipkow@6074
|
409 |
val thm = dthm2 COMP (dthm1 COMP casethm)
|
nipkow@6102
|
410 |
in Some(LA_Logic.mk_Eq ((implies_intr cnconcl thm) COMP LA_Logic.ccontr)) end
|
nipkow@6074
|
411 |
handle _ => None;
|
nipkow@6074
|
412 |
|
nipkow@6079
|
413 |
(* PRE: concl is not negated! *)
|
nipkow@6074
|
414 |
fun lin_arith_prover sg thms concl =
|
nipkow@6074
|
415 |
let val Hs = map (#prop o rep_thm) thms
|
nipkow@6102
|
416 |
val Tconcl = LA_Logic.mk_Trueprop concl
|
nipkow@6074
|
417 |
in case prove([],Hs,Tconcl) of
|
nipkow@6079
|
418 |
[j] => prover1(j,sg,thms,Tconcl,true)
|
nipkow@6074
|
419 |
| [j1,j2] => prover2(j1,j2,sg,thms,Tconcl)
|
nipkow@6102
|
420 |
| _ => let val nTconcl = LA_Logic.neg_prop Tconcl
|
nipkow@6079
|
421 |
in case prove([],Hs,nTconcl) of
|
nipkow@6079
|
422 |
[j] => prover1(j,sg,thms,nTconcl,false)
|
nipkow@6079
|
423 |
(* [_,_] impossible because of negation *)
|
nipkow@6079
|
424 |
| _ => None
|
nipkow@6079
|
425 |
end
|
nipkow@5982
|
426 |
end;
|
nipkow@6074
|
427 |
|
nipkow@6074
|
428 |
end;
|