src/Provers/Arith/fast_lin_arith.ML
author nipkow
Mon Sep 06 17:37:35 2004 +0200 (2004-09-06)
changeset 15184 d2c19aea17bc
parent 15027 d23887300b96
child 15531 08c8dad8e399
permissions -rw-r--r--
made mult_mono_thms generic.
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
wenzelm@15027
    14
    lin_arith_prover: Sign.sg -> simpset -> 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
paulson@9073
    20
(* Debugging: set Fast_Arith.trace *)
nipkow@7582
    21
nipkow@5982
    22
(*** Data needed for setting up the linear arithmetic package ***)
nipkow@5982
    23
nipkow@6102
    24
signature LIN_ARITH_LOGIC =
nipkow@6102
    25
sig
nipkow@6102
    26
  val conjI:		thm
nipkow@6102
    27
  val ccontr:           thm (* (~ P ==> False) ==> P *)
nipkow@6102
    28
  val neqE:             thm (* [| m ~= n; m < n ==> P; n < m ==> P |] ==> P *)
nipkow@6102
    29
  val notI:             thm (* (P ==> False) ==> ~ P *)
nipkow@6110
    30
  val not_lessD:        thm (* ~(m < n) ==> n <= m *)
nipkow@6128
    31
  val not_leD:          thm (* ~(m <= n) ==> n < m *)
nipkow@6102
    32
  val sym:		thm (* x = y ==> y = x *)
nipkow@6102
    33
  val mk_Eq: thm -> thm
nipkow@6102
    34
  val mk_Trueprop: term -> term
nipkow@6102
    35
  val neg_prop: term -> term
nipkow@6102
    36
  val is_False: thm -> bool
nipkow@6128
    37
  val is_nat: typ list * term -> bool
nipkow@6128
    38
  val mk_nat_thm: Sign.sg -> term -> thm
nipkow@6102
    39
end;
nipkow@6102
    40
(*
nipkow@6102
    41
mk_Eq(~in) = `in == False'
nipkow@6102
    42
mk_Eq(in) = `in == True'
nipkow@6102
    43
where `in' is an (in)equality.
nipkow@6102
    44
nipkow@6102
    45
neg_prop(t) = neg if t is wrapped up in Trueprop and
nipkow@6102
    46
  nt is the (logically) negated version of t, where the negation
nipkow@6102
    47
  of a negative term is the term itself (no double negation!);
nipkow@6128
    48
nipkow@6128
    49
is_nat(parameter-types,t) =  t:nat
nipkow@6128
    50
mk_nat_thm(t) = "0 <= t"
nipkow@6102
    51
*)
nipkow@6102
    52
nipkow@5982
    53
signature LIN_ARITH_DATA =
nipkow@5982
    54
sig
nipkow@6128
    55
  val decomp:
nipkow@10691
    56
    Sign.sg -> term -> ((term*rat)list * rat * string * (term*rat)list * rat * bool)option
nipkow@10691
    57
  val number_of: int * typ -> term
nipkow@5982
    58
end;
nipkow@5982
    59
(*
nipkow@7551
    60
decomp(`x Rel y') should yield (p,i,Rel,q,j,d)
nipkow@5982
    61
   where Rel is one of "<", "~<", "<=", "~<=" and "=" and
nipkow@5982
    62
         p/q is the decomposition of the sum terms x/y into a list
nipkow@7551
    63
         of summand * multiplicity pairs and a constant summand and
nipkow@7551
    64
         d indicates if the domain is discrete.
nipkow@5982
    65
wenzelm@9420
    66
ss must reduce contradictory <= to False.
nipkow@5982
    67
   It should also cancel common summands to keep <= reduced;
nipkow@5982
    68
   otherwise <= can grow to massive proportions.
nipkow@5982
    69
*)
nipkow@5982
    70
nipkow@6062
    71
signature FAST_LIN_ARITH =
nipkow@6062
    72
sig
wenzelm@9420
    73
  val setup: (theory -> theory) list
nipkow@15184
    74
  val map_data: ({add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
nipkow@10575
    75
                 lessD: thm list, simpset: Simplifier.simpset}
nipkow@15184
    76
                 -> {add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
nipkow@10575
    77
                     lessD: thm list, simpset: Simplifier.simpset})
nipkow@10575
    78
                -> theory -> theory
paulson@9073
    79
  val trace           : bool ref
nipkow@14510
    80
  val fast_arith_neq_limit: int ref
wenzelm@15027
    81
  val lin_arith_prover: Sign.sg -> simpset -> term -> thm option
nipkow@13498
    82
  val     lin_arith_tac:     bool -> int -> tactic
nipkow@6062
    83
  val cut_lin_arith_tac: thm list -> int -> tactic
nipkow@6062
    84
end;
nipkow@6062
    85
nipkow@6102
    86
functor Fast_Lin_Arith(structure LA_Logic:LIN_ARITH_LOGIC 
nipkow@6102
    87
                       and       LA_Data:LIN_ARITH_DATA) : FAST_LIN_ARITH =
nipkow@5982
    88
struct
nipkow@5982
    89
wenzelm@9420
    90
wenzelm@9420
    91
(** theory data **)
wenzelm@9420
    92
wenzelm@9420
    93
(* data kind 'Provers/fast_lin_arith' *)
wenzelm@9420
    94
wenzelm@9420
    95
structure DataArgs =
wenzelm@9420
    96
struct
wenzelm@9420
    97
  val name = "Provers/fast_lin_arith";
nipkow@15184
    98
  type T = {add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
nipkow@10575
    99
            lessD: thm list, simpset: Simplifier.simpset};
wenzelm@9420
   100
nipkow@10691
   101
  val empty = {add_mono_thms = [], mult_mono_thms = [], inj_thms = [],
nipkow@10575
   102
               lessD = [], simpset = Simplifier.empty_ss};
wenzelm@9420
   103
  val copy = I;
wenzelm@9420
   104
  val prep_ext = I;
wenzelm@9420
   105
nipkow@10691
   106
  fun merge ({add_mono_thms= add_mono_thms1, mult_mono_thms= mult_mono_thms1, inj_thms= inj_thms1,
nipkow@10575
   107
              lessD = lessD1, simpset = simpset1},
nipkow@10691
   108
             {add_mono_thms= add_mono_thms2, mult_mono_thms= mult_mono_thms2, inj_thms= inj_thms2,
nipkow@10575
   109
              lessD = lessD2, simpset = simpset2}) =
wenzelm@9420
   110
    {add_mono_thms = Drule.merge_rules (add_mono_thms1, add_mono_thms2),
nipkow@15184
   111
     mult_mono_thms = Drule.merge_rules (mult_mono_thms1, mult_mono_thms2),
nipkow@10575
   112
     inj_thms = Drule.merge_rules (inj_thms1, inj_thms2),
nipkow@10575
   113
     lessD = Drule.merge_rules (lessD1, lessD2),
nipkow@10575
   114
     simpset = Simplifier.merge_ss (simpset1, simpset2)};
wenzelm@9420
   115
wenzelm@9420
   116
  fun print _ _ = ();
wenzelm@9420
   117
end;
wenzelm@9420
   118
wenzelm@9420
   119
structure Data = TheoryDataFun(DataArgs);
wenzelm@9420
   120
val map_data = Data.map;
wenzelm@9420
   121
val setup = [Data.init];
wenzelm@9420
   122
wenzelm@9420
   123
wenzelm@9420
   124
nipkow@5982
   125
(*** A fast decision procedure ***)
nipkow@5982
   126
(*** Code ported from HOL Light ***)
nipkow@6056
   127
(* possible optimizations:
nipkow@6056
   128
   use (var,coeff) rep or vector rep  tp save space;
nipkow@6056
   129
   treat non-negative atoms separately rather than adding 0 <= atom
nipkow@6056
   130
*)
nipkow@5982
   131
paulson@9073
   132
val trace = ref false;
paulson@9073
   133
nipkow@5982
   134
datatype lineq_type = Eq | Le | Lt;
nipkow@5982
   135
nipkow@6056
   136
datatype injust = Asm of int
nipkow@6056
   137
                | Nat of int (* index of atom *)
nipkow@6128
   138
                | LessD of injust
nipkow@6128
   139
                | NotLessD of injust
nipkow@6128
   140
                | NotLeD of injust
nipkow@7551
   141
                | NotLeDD of injust
nipkow@5982
   142
                | Multiplied of int * injust
nipkow@10691
   143
                | Multiplied2 of int * injust
nipkow@5982
   144
                | Added of injust * injust;
nipkow@5982
   145
nipkow@5982
   146
datatype lineq = Lineq of int * lineq_type * int list * injust;
nipkow@5982
   147
nipkow@13498
   148
fun el 0 (h::_) = h
nipkow@13498
   149
  | el n (_::t) = el (n - 1) t
nipkow@13498
   150
  | el _ _  = sys_error "el";
nipkow@13498
   151
nipkow@13498
   152
(* ------------------------------------------------------------------------- *)
nipkow@13498
   153
(* Finding a (counter) example from the trace of a failed elimination        *)
nipkow@13498
   154
(* ------------------------------------------------------------------------- *)
nipkow@13498
   155
(* Examples are represented as rational numbers,                             *)
nipkow@13498
   156
(* Dont blame John Harrison for this code - it is entirely mine. TN          *)
nipkow@13498
   157
nipkow@13498
   158
exception NoEx;
nipkow@13498
   159
nipkow@14372
   160
(* Coding: (i,true,cs) means i <= cs and (i,false,cs) means i < cs.
nipkow@14372
   161
   In general, true means the bound is included, false means it is excluded.
nipkow@14372
   162
   Need to know if it is a lower or upper bound for unambiguous interpretation!
nipkow@14372
   163
*)
nipkow@14372
   164
nipkow@14372
   165
fun elim_eqns(ineqs,Lineq(i,Le,cs,_)) = (i,true,cs)::ineqs
nipkow@14372
   166
  | elim_eqns(ineqs,Lineq(i,Eq,cs,_)) = (i,true,cs)::(~i,true,map ~ cs)::ineqs
nipkow@14372
   167
  | elim_eqns(ineqs,Lineq(i,Lt,cs,_)) = (i,false,cs)::ineqs;
nipkow@13498
   168
nipkow@13498
   169
val rat0 = rat_of_int 0;
nipkow@13498
   170
nipkow@13498
   171
(* PRE: ex[v] must be 0! *)
nipkow@14372
   172
fun eval (ex:rat list) v (a:int,le,cs:int list) =
nipkow@13498
   173
  let val rs = map rat_of_int cs
nipkow@13498
   174
      val rsum = foldl ratadd (rat0,map ratmul (rs ~~ ex))
nipkow@14372
   175
  in (ratmul(ratadd(rat_of_int a,ratneg rsum), ratinv(el v rs)), le) end;
nipkow@14372
   176
(* If el v rs < 0, le should be negated.
nipkow@14372
   177
   Instead this swap is taken into account in ratrelmin2.
nipkow@14372
   178
*)
nipkow@13498
   179
nipkow@13498
   180
fun ratge0 r = fst(rep_rat r) >= 0;
nipkow@14372
   181
fun ratle(r,s) = ratge0(ratadd(s,ratneg r));
nipkow@13498
   182
nipkow@14372
   183
fun ratrelmin2(x as (r,ler),y as (s,les)) =
nipkow@14372
   184
  if r=s then (r, (not ler) andalso (not les)) else if ratle(r,s) then x else y;
nipkow@14372
   185
fun ratrelmax2(x as (r,ler),y as (s,les)) =
nipkow@14372
   186
  if r=s then (r,ler andalso les) else if ratle(r,s) then y else x;
nipkow@13498
   187
nipkow@14372
   188
val ratrelmin = foldr1 ratrelmin2;
nipkow@14372
   189
val ratrelmax = foldr1 ratrelmax2;
nipkow@13498
   190
nipkow@13498
   191
fun ratroundup r = let val (p,q) = rep_rat r
nipkow@13498
   192
                   in if q=1 then r else rat_of_int((p div q) + 1) end
nipkow@13498
   193
nipkow@13498
   194
fun ratrounddown r = let val (p,q) = rep_rat r
nipkow@13498
   195
                     in if q=1 then r else rat_of_int((p div q) - 1) end
nipkow@13498
   196
nipkow@14372
   197
fun ratexact up (r,exact) =
nipkow@14372
   198
  if exact then r else
nipkow@14372
   199
  let val (p,q) = rep_rat r
nipkow@14372
   200
      val nth = ratinv(rat_of_int q)
nipkow@14372
   201
  in ratadd(r,if up then nth else ratneg nth) end;
nipkow@14372
   202
nipkow@14372
   203
fun ratmiddle(r,s) = ratmul(ratadd(r,s),ratinv(rat_of_int 2));
nipkow@14372
   204
nipkow@14372
   205
fun choose2 d ((lb,exactl),(ub,exactu)) =
nipkow@14372
   206
  if ratle(lb,rat0) andalso (lb <> rat0 orelse exactl) andalso
nipkow@14372
   207
     ratle(rat0,ub) andalso (ub <> rat0 orelse exactu)
nipkow@14372
   208
  then rat0 else
nipkow@14372
   209
  if not d
nipkow@14372
   210
  then (if ratge0 lb
nipkow@14372
   211
        then if exactl then lb else ratmiddle(lb,ub)
nipkow@14372
   212
        else if exactu then ub else ratmiddle(lb,ub))
nipkow@14372
   213
  else (* discrete domain, both bounds must be exact *)
nipkow@13498
   214
  if ratge0 lb then let val lb' = ratroundup lb
nipkow@13498
   215
                    in if ratle(lb',ub) then lb' else raise NoEx end
nipkow@13498
   216
               else let val ub' = ratrounddown ub
nipkow@13498
   217
                    in if ratle(lb,ub') then ub' else raise NoEx end;
nipkow@13498
   218
nipkow@13498
   219
fun findex1 discr (ex,(v,lineqs)) =
nipkow@13498
   220
  let val nz = filter (fn (Lineq(_,_,cs,_)) => el v cs <> 0) lineqs;
nipkow@13498
   221
      val ineqs = foldl elim_eqns ([],nz)
nipkow@14372
   222
      val (ge,le) = partition (fn (_,_,cs) => el v cs > 0) ineqs
nipkow@14372
   223
      val lb = ratrelmax(map (eval ex v) ge)
nipkow@14372
   224
      val ub = ratrelmin(map (eval ex v) le)
nipkow@13498
   225
  in nth_update (choose2 (nth_elem(v,discr)) (lb,ub)) (v,ex) end;
nipkow@13498
   226
nipkow@13498
   227
fun findex discr = foldl (findex1 discr);
nipkow@13498
   228
nipkow@13498
   229
fun elim1 v x =
nipkow@14372
   230
  map (fn (a,le,bs) => (ratadd(a,ratneg(ratmul(el v bs,x))), le,
nipkow@14372
   231
                        nth_update rat0 (v,bs)));
nipkow@13498
   232
nipkow@14372
   233
fun single_var v (_,_,cs) = (filter_out (equal rat0) cs = [el v cs]);
nipkow@13498
   234
nipkow@13498
   235
(* The base case:
nipkow@13498
   236
   all variables occur only with positive or only with negative coefficients *)
nipkow@13498
   237
fun pick_vars discr (ineqs,ex) =
nipkow@14372
   238
  let val nz = filter_out (fn (_,_,cs) => forall (equal rat0) cs) ineqs
nipkow@14372
   239
  in case nz of [] => ex
nipkow@14372
   240
     | (_,_,cs) :: _ =>
nipkow@14372
   241
       let val v = find_index (not o equal rat0) cs
nipkow@14372
   242
           val d = nth_elem(v,discr)
nipkow@14372
   243
           val pos = ratge0(el v cs)
nipkow@14372
   244
           val sv = filter (single_var v) nz
nipkow@14372
   245
           val minmax =
nipkow@14372
   246
             if pos then if d then ratroundup o fst o ratrelmax
nipkow@14372
   247
                         else ratexact true o ratrelmax
nipkow@14372
   248
                    else if d then ratrounddown o fst o ratrelmin
nipkow@14372
   249
                         else ratexact false o ratrelmin
nipkow@14372
   250
           val bnds = map (fn (a,le,bs) => (ratmul(a,ratinv(el v bs)),le)) sv
nipkow@14372
   251
           val x = minmax((rat0,if pos then true else false)::bnds)
nipkow@14372
   252
           val ineqs' = elim1 v x nz
nipkow@14372
   253
           val ex' = nth_update x (v,ex)
nipkow@14372
   254
       in pick_vars discr (ineqs',ex') end
nipkow@13498
   255
  end;
nipkow@13498
   256
nipkow@13498
   257
fun findex0 discr n lineqs =
nipkow@13498
   258
  let val ineqs = foldl elim_eqns ([],lineqs)
nipkow@14372
   259
      val rineqs = map (fn (a,le,cs) => (rat_of_int a, le, map rat_of_int cs))
nipkow@14372
   260
                       ineqs
nipkow@13498
   261
  in pick_vars discr (rineqs,replicate n rat0) end;
nipkow@13498
   262
nipkow@13498
   263
(* ------------------------------------------------------------------------- *)
nipkow@13498
   264
(* End of counter example finder. The actual decision procedure starts here. *)
nipkow@13498
   265
(* ------------------------------------------------------------------------- *)
nipkow@13498
   266
nipkow@5982
   267
(* ------------------------------------------------------------------------- *)
nipkow@5982
   268
(* Calculate new (in)equality type after addition.                           *)
nipkow@5982
   269
(* ------------------------------------------------------------------------- *)
nipkow@5982
   270
nipkow@5982
   271
fun find_add_type(Eq,x) = x
nipkow@5982
   272
  | find_add_type(x,Eq) = x
nipkow@5982
   273
  | find_add_type(_,Lt) = Lt
nipkow@5982
   274
  | find_add_type(Lt,_) = Lt
nipkow@5982
   275
  | find_add_type(Le,Le) = Le;
nipkow@5982
   276
nipkow@5982
   277
(* ------------------------------------------------------------------------- *)
nipkow@5982
   278
(* Multiply out an (in)equation.                                             *)
nipkow@5982
   279
(* ------------------------------------------------------------------------- *)
nipkow@5982
   280
nipkow@5982
   281
fun multiply_ineq n (i as Lineq(k,ty,l,just)) =
nipkow@5982
   282
  if n = 1 then i
nipkow@5982
   283
  else if n = 0 andalso ty = Lt then sys_error "multiply_ineq"
nipkow@5982
   284
  else if n < 0 andalso (ty=Le orelse ty=Lt) then sys_error "multiply_ineq"
nipkow@5982
   285
  else Lineq(n * k,ty,map (apl(n,op * )) l,Multiplied(n,just));
nipkow@5982
   286
nipkow@5982
   287
(* ------------------------------------------------------------------------- *)
nipkow@5982
   288
(* Add together (in)equations.                                               *)
nipkow@5982
   289
(* ------------------------------------------------------------------------- *)
nipkow@5982
   290
nipkow@5982
   291
fun add_ineq (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
nipkow@5982
   292
  let val l = map2 (op +) (l1,l2)
nipkow@5982
   293
  in Lineq(k1+k2,find_add_type(ty1,ty2),l,Added(just1,just2)) end;
nipkow@5982
   294
nipkow@5982
   295
(* ------------------------------------------------------------------------- *)
nipkow@5982
   296
(* Elimination of variable between a single pair of (in)equations.           *)
nipkow@5982
   297
(* If they're both inequalities, 1st coefficient must be +ve, 2nd -ve.       *)
nipkow@5982
   298
(* ------------------------------------------------------------------------- *)
nipkow@5982
   299
nipkow@5982
   300
fun elim_var v (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
nipkow@5982
   301
  let val c1 = el v l1 and c2 = el v l2
nipkow@10691
   302
      val m = lcm(abs c1,abs c2)
nipkow@5982
   303
      val m1 = m div (abs c1) and m2 = m div (abs c2)
nipkow@5982
   304
      val (n1,n2) =
nipkow@5982
   305
        if (c1 >= 0) = (c2 >= 0)
nipkow@5982
   306
        then if ty1 = Eq then (~m1,m2)
nipkow@5982
   307
             else if ty2 = Eq then (m1,~m2)
nipkow@5982
   308
                  else sys_error "elim_var"
nipkow@5982
   309
        else (m1,m2)
nipkow@5982
   310
      val (p1,p2) = if ty1=Eq andalso ty2=Eq andalso (n1 = ~1 orelse n2 = ~1)
nipkow@5982
   311
                    then (~n1,~n2) else (n1,n2)
nipkow@5982
   312
  in add_ineq (multiply_ineq n1 i1) (multiply_ineq n2 i2) end;
nipkow@5982
   313
nipkow@5982
   314
(* ------------------------------------------------------------------------- *)
nipkow@5982
   315
(* The main refutation-finding code.                                         *)
nipkow@5982
   316
(* ------------------------------------------------------------------------- *)
nipkow@5982
   317
nipkow@5982
   318
fun is_trivial (Lineq(_,_,l,_)) = forall (fn i => i=0) l;
nipkow@5982
   319
nipkow@5982
   320
fun is_answer (ans as Lineq(k,ty,l,_)) =
nipkow@5982
   321
  case ty  of Eq => k <> 0 | Le => k > 0 | Lt => k >= 0;
nipkow@5982
   322
nipkow@5982
   323
fun calc_blowup l =
nipkow@5982
   324
  let val (p,n) = partition (apl(0,op<)) (filter (apl(0,op<>)) l)
nipkow@5982
   325
  in (length p) * (length n) end;
nipkow@5982
   326
nipkow@5982
   327
(* ------------------------------------------------------------------------- *)
nipkow@5982
   328
(* Main elimination code:                                                    *)
nipkow@5982
   329
(*                                                                           *)
nipkow@5982
   330
(* (1) Looks for immediate solutions (false assertions with no variables).   *)
nipkow@5982
   331
(*                                                                           *)
nipkow@5982
   332
(* (2) If there are any equations, picks a variable with the lowest absolute *)
nipkow@5982
   333
(* coefficient in any of them, and uses it to eliminate.                     *)
nipkow@5982
   334
(*                                                                           *)
nipkow@5982
   335
(* (3) Otherwise, chooses a variable in the inequality to minimize the       *)
nipkow@5982
   336
(* blowup (number of consequences generated) and eliminates it.              *)
nipkow@5982
   337
(* ------------------------------------------------------------------------- *)
nipkow@5982
   338
nipkow@5982
   339
fun allpairs f xs ys =
nipkow@5982
   340
  flat(map (fn x => map (fn y => f x y) ys) xs);
nipkow@5982
   341
nipkow@5982
   342
fun extract_first p =
nipkow@5982
   343
  let fun extract xs (y::ys) = if p y then (Some y,xs@ys)
nipkow@5982
   344
                               else extract (y::xs) ys
nipkow@5982
   345
        | extract xs []      = (None,xs)
nipkow@5982
   346
  in extract [] end;
nipkow@5982
   347
nipkow@6056
   348
fun print_ineqs ineqs =
paulson@9073
   349
  if !trace then
wenzelm@12262
   350
     tracing(cat_lines(""::map (fn Lineq(c,t,l,_) =>
paulson@9073
   351
       string_of_int c ^
paulson@9073
   352
       (case t of Eq => " =  " | Lt=> " <  " | Le => " <= ") ^
paulson@9073
   353
       commas(map string_of_int l)) ineqs))
paulson@9073
   354
  else ();
nipkow@6056
   355
nipkow@13498
   356
type history = (int * lineq list) list;
nipkow@13498
   357
datatype result = Success of injust | Failure of history;
nipkow@13498
   358
nipkow@13498
   359
fun elim(ineqs,hist) =
paulson@9073
   360
  let val dummy = print_ineqs ineqs;
nipkow@6056
   361
      val (triv,nontriv) = partition is_trivial ineqs in
nipkow@5982
   362
  if not(null triv)
nipkow@13186
   363
  then case Library.find_first is_answer triv of
nipkow@13498
   364
         None => elim(nontriv,hist)
nipkow@13498
   365
       | Some(Lineq(_,_,_,j)) => Success j
nipkow@5982
   366
  else
nipkow@13498
   367
  if null nontriv then Failure(hist)
nipkow@13498
   368
  else
nipkow@5982
   369
  let val (eqs,noneqs) = partition (fn (Lineq(_,ty,_,_)) => ty=Eq) nontriv in
nipkow@5982
   370
  if not(null eqs) then
nipkow@5982
   371
     let val clist = foldl (fn (cs,Lineq(_,_,l,_)) => l union cs) ([],eqs)
nipkow@5982
   372
         val sclist = sort (fn (x,y) => int_ord(abs(x),abs(y)))
nipkow@5982
   373
                           (filter (fn i => i<>0) clist)
nipkow@5982
   374
         val c = hd sclist
nipkow@5982
   375
         val (Some(eq as Lineq(_,_,ceq,_)),othereqs) =
nipkow@5982
   376
               extract_first (fn Lineq(_,_,l,_) => c mem l) eqs
nipkow@13498
   377
         val v = find_index_eq c ceq
nipkow@5982
   378
         val (ioth,roth) = partition (fn (Lineq(_,_,l,_)) => el v l = 0)
nipkow@5982
   379
                                     (othereqs @ noneqs)
nipkow@5982
   380
         val others = map (elim_var v eq) roth @ ioth
nipkow@13498
   381
     in elim(others,(v,nontriv)::hist) end
nipkow@5982
   382
  else
nipkow@5982
   383
  let val lists = map (fn (Lineq(_,_,l,_)) => l) noneqs
nipkow@5982
   384
      val numlist = 0 upto (length(hd lists) - 1)
nipkow@5982
   385
      val coeffs = map (fn i => map (el i) lists) numlist
nipkow@5982
   386
      val blows = map calc_blowup coeffs
nipkow@5982
   387
      val iblows = blows ~~ numlist
nipkow@5982
   388
      val nziblows = filter (fn (i,_) => i<>0) iblows
nipkow@13498
   389
  in if null nziblows then Failure((~1,nontriv)::hist)
nipkow@13498
   390
     else
nipkow@5982
   391
     let val (c,v) = hd(sort (fn (x,y) => int_ord(fst(x),fst(y))) nziblows)
nipkow@5982
   392
         val (no,yes) = partition (fn (Lineq(_,_,l,_)) => el v l = 0) ineqs
nipkow@5982
   393
         val (pos,neg) = partition(fn (Lineq(_,_,l,_)) => el v l > 0) yes
nipkow@13498
   394
     in elim(no @ allpairs (elim_var v) pos neg, (v,nontriv)::hist) end
nipkow@5982
   395
  end
nipkow@5982
   396
  end
nipkow@5982
   397
  end;
nipkow@5982
   398
nipkow@5982
   399
(* ------------------------------------------------------------------------- *)
nipkow@5982
   400
(* Translate back a proof.                                                   *)
nipkow@5982
   401
(* ------------------------------------------------------------------------- *)
nipkow@5982
   402
paulson@9073
   403
fun trace_thm msg th = 
wenzelm@12262
   404
    if !trace then (tracing msg; tracing (Display.string_of_thm th); th) else th;
paulson@9073
   405
paulson@9073
   406
fun trace_msg msg = 
wenzelm@12262
   407
    if !trace then tracing msg else ();
paulson@9073
   408
nipkow@13498
   409
(* FIXME OPTIMIZE!!!! (partly done already)
nipkow@6056
   410
   Addition/Multiplication need i*t representation rather than t+t+...
nipkow@10691
   411
   Get rid of Mulitplied(2). For Nat LA_Data.number_of should return Suc^n
nipkow@10691
   412
   because Numerals are not known early enough.
nipkow@6056
   413
nipkow@6056
   414
Simplification may detect a contradiction 'prematurely' due to type
nipkow@6056
   415
information: n+1 <= 0 is simplified to False and does not need to be crossed
nipkow@6056
   416
with 0 <= n.
nipkow@6056
   417
*)
nipkow@6056
   418
local
nipkow@6056
   419
 exception FalseE of thm
nipkow@6056
   420
in
nipkow@6074
   421
fun mkthm sg asms just =
nipkow@10691
   422
  let val {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset} = Data.get_sg sg;
wenzelm@9420
   423
      val atoms = foldl (fn (ats,(lhs,_,_,rhs,_,_)) =>
nipkow@6056
   424
                            map fst lhs  union  (map fst rhs  union  ats))
nipkow@13464
   425
                        ([], mapfilter (fn thm => if Thm.no_prems thm
nipkow@13464
   426
                                        then LA_Data.decomp sg (concl_of thm)
nipkow@13464
   427
                                        else None) asms)
nipkow@6056
   428
nipkow@10575
   429
      fun add2 thm1 thm2 =
nipkow@6102
   430
        let val conj = thm1 RS (thm2 RS LA_Logic.conjI)
wenzelm@14821
   431
        in get_first (fn th => Some(conj RS th) handle THM _ => None) add_mono_thms
nipkow@5982
   432
        end;
nipkow@5982
   433
nipkow@10575
   434
      fun try_add [] _ = None
nipkow@10575
   435
        | try_add (thm1::thm1s) thm2 = case add2 thm1 thm2 of
nipkow@10575
   436
             None => try_add thm1s thm2 | some => some;
nipkow@10575
   437
nipkow@10575
   438
      fun addthms thm1 thm2 =
nipkow@10575
   439
        case add2 thm1 thm2 of
nipkow@10575
   440
          None => (case try_add ([thm1] RL inj_thms) thm2 of
nipkow@14360
   441
                     None => ( the(try_add ([thm2] RL inj_thms) thm1)
nipkow@14360
   442
                               handle OPTION =>
nipkow@14360
   443
                               (trace_thm "" thm1; trace_thm "" thm2;
nipkow@14360
   444
                                sys_error "Lin.arith. failed to add thms")
nipkow@14360
   445
                             )
nipkow@10575
   446
                   | Some thm => thm)
nipkow@10575
   447
        | Some thm => thm;
nipkow@10575
   448
nipkow@5982
   449
      fun multn(n,thm) =
nipkow@5982
   450
        let fun mul(i,th) = if i=1 then th else mul(i-1, addthms thm th)
nipkow@6102
   451
        in if n < 0 then mul(~n,thm) RS LA_Logic.sym else mul(n,thm) end;
nipkow@15184
   452
(*
nipkow@10691
   453
      fun multn2(n,thm) =
nipkow@10691
   454
        let val Some(mth,cv) =
wenzelm@14821
   455
              get_first (fn (th,cv) => Some(thm RS th,cv) handle THM _ => None) mult_mono_thms
nipkow@10691
   456
            val ct = cterm_of sg (LA_Data.number_of(n,#T(rep_cterm cv)))
nipkow@10691
   457
        in instantiate ([],[(cv,ct)]) mth end
nipkow@15184
   458
*)
nipkow@15184
   459
      fun multn2(n,thm) =
nipkow@15184
   460
        let val Some(mth) =
nipkow@15184
   461
              get_first (fn th => Some(thm RS th) handle THM _ => None) mult_mono_thms
nipkow@15184
   462
            fun cvar(th,_ $ (_ $ _ $ var)) = cterm_of (#sign(rep_thm th)) var;
nipkow@15184
   463
            val cv = cvar(mth, hd(prems_of mth));
nipkow@15184
   464
            val ct = cterm_of sg (LA_Data.number_of(n,#T(rep_cterm cv)))
nipkow@15184
   465
        in instantiate ([],[(cv,ct)]) mth end
nipkow@10691
   466
nipkow@6056
   467
      fun simp thm =
nipkow@12932
   468
        let val thm' = trace_thm "Simplified:" (full_simplify simpset thm)
nipkow@6102
   469
        in if LA_Logic.is_False thm' then raise FalseE thm' else thm' end
nipkow@6056
   470
paulson@9073
   471
      fun mk(Asm i) = trace_thm "Asm" (nth_elem(i,asms))
nipkow@13464
   472
        | mk(Nat i) = (trace_msg "Nat"; LA_Logic.mk_nat_thm sg (nth_elem(i,atoms)))
wenzelm@9420
   473
        | mk(LessD(j)) = trace_thm "L" (hd([mk j] RL lessD))
paulson@9073
   474
        | mk(NotLeD(j)) = trace_thm "NLe" (mk j RS LA_Logic.not_leD)
wenzelm@9420
   475
        | mk(NotLeDD(j)) = trace_thm "NLeD" (hd([mk j RS LA_Logic.not_leD] RL lessD))
paulson@9073
   476
        | mk(NotLessD(j)) = trace_thm "NL" (mk j RS LA_Logic.not_lessD)
paulson@9073
   477
        | mk(Added(j1,j2)) = simp (trace_thm "+" (addthms (mk j1) (mk j2)))
nipkow@10717
   478
        | mk(Multiplied(n,j)) = (trace_msg("*"^string_of_int n); trace_thm "*" (multn(n,mk j)))
nipkow@10717
   479
        | mk(Multiplied2(n,j)) = simp (trace_msg("**"^string_of_int n); trace_thm "**" (multn2(n,mk j)))
nipkow@5982
   480
paulson@9073
   481
  in trace_msg "mkthm";
nipkow@12932
   482
     let val thm = trace_thm "Final thm:" (mk just)
nipkow@12932
   483
     in let val fls = simplify simpset thm
nipkow@13186
   484
        in trace_thm "After simplification:" fls;
nipkow@13186
   485
           if LA_Logic.is_False fls then fls
nipkow@13186
   486
           else
nipkow@13186
   487
            (tracing "Assumptions:"; seq print_thm asms;
nipkow@13186
   488
             tracing "Proved:"; print_thm fls;
nipkow@13186
   489
             warning "Linear arithmetic should have refuted the assumptions.\n\
nipkow@13186
   490
                     \Please inform Tobias Nipkow (nipkow@in.tum.de).";
nipkow@13186
   491
             fls)
nipkow@12932
   492
        end
nipkow@12932
   493
     end handle FalseE thm => (trace_thm "False reached early:" thm; thm)
nipkow@12932
   494
  end
nipkow@6056
   495
end;
nipkow@5982
   496
nipkow@5982
   497
fun coeff poly atom = case assoc(poly,atom) of None => 0 | Some i => i;
nipkow@5982
   498
nipkow@10691
   499
fun lcms is = foldl lcm (1,is);
nipkow@10691
   500
nipkow@10691
   501
fun integ(rlhs,r,rel,rrhs,s,d) =
nipkow@10691
   502
let val (rn,rd) = rep_rat r and (sn,sd) = rep_rat s
nipkow@10691
   503
    val m = lcms(map (abs o snd o rep_rat) (r :: s :: map snd rlhs @ map snd rrhs))
nipkow@10691
   504
    fun mult(t,r) = let val (i,j) = rep_rat r in (t,i * (m div j)) end
nipkow@12932
   505
in (m,(map mult rlhs, rn*(m div rd), rel, map mult rrhs, sn*(m div sd), d)) end
nipkow@10691
   506
nipkow@13498
   507
fun mklineq n atoms =
nipkow@13498
   508
  fn (item,k) =>
nipkow@13498
   509
  let val (m,(lhs,i,rel,rhs,j,discrete)) = integ item
nipkow@13498
   510
      val lhsa = map (coeff lhs) atoms
nipkow@13498
   511
      and rhsa = map (coeff rhs) atoms
nipkow@13498
   512
      val diff = map2 (op -) (rhsa,lhsa)
nipkow@13498
   513
      val c = i-j
nipkow@13498
   514
      val just = Asm k
nipkow@13498
   515
      fun lineq(c,le,cs,j) = Lineq(c,le,cs, if m=1 then j else Multiplied2(m,j))
nipkow@13498
   516
  in case rel of
nipkow@13498
   517
      "<="   => lineq(c,Le,diff,just)
nipkow@13498
   518
     | "~<=" => if discrete
nipkow@13498
   519
                then lineq(1-c,Le,map (op ~) diff,NotLeDD(just))
nipkow@13498
   520
                else lineq(~c,Lt,map (op ~) diff,NotLeD(just))
nipkow@13498
   521
     | "<"   => if discrete
nipkow@13498
   522
                then lineq(c+1,Le,diff,LessD(just))
nipkow@13498
   523
                else lineq(c,Lt,diff,just)
nipkow@13498
   524
     | "~<"  => lineq(~c,Le,map (op~) diff,NotLessD(just))
nipkow@13498
   525
     | "="   => lineq(c,Eq,diff,just)
nipkow@13498
   526
     | _     => sys_error("mklineq" ^ rel)   
nipkow@5982
   527
  end;
nipkow@5982
   528
nipkow@13498
   529
(* ------------------------------------------------------------------------- *)
nipkow@13498
   530
(* Print (counter) example                                                   *)
nipkow@13498
   531
(* ------------------------------------------------------------------------- *)
nipkow@13498
   532
nipkow@13498
   533
fun print_atom((a,d),r) =
nipkow@13498
   534
  let val (p,q) = rep_rat r
nipkow@13498
   535
      val s = if d then string_of_int p else
nipkow@13498
   536
              if p = 0 then "0"
nipkow@13498
   537
              else string_of_int p ^ "/" ^ string_of_int q
nipkow@13498
   538
  in a ^ " = " ^ s end;
nipkow@13498
   539
nipkow@13498
   540
fun print_ex sds =
nipkow@13498
   541
  tracing o
nipkow@13498
   542
  apl("Counter example:\n",op ^) o
nipkow@13498
   543
  commas o
nipkow@13498
   544
  map print_atom o
nipkow@13498
   545
  apl(sds, op ~~);
nipkow@13498
   546
nipkow@13498
   547
fun trace_ex(sg,params,atoms,discr,n,hist:history) =
nipkow@13498
   548
  if null hist then ()
nipkow@13498
   549
  else let val frees = map Free params;
nipkow@13498
   550
           fun s_of_t t = Sign.string_of_term sg (subst_bounds(frees,t));
nipkow@13498
   551
           val (v,lineqs) :: hist' = hist
nipkow@13498
   552
           val start = if v = ~1 then (findex0 discr n lineqs,hist')
nipkow@13498
   553
                       else (replicate n rat0,hist)
nipkow@13516
   554
       in warning "arith failed - see trace for a counter example";
nipkow@13516
   555
          print_ex ((map s_of_t atoms)~~discr) (findex discr start)
nipkow@13498
   556
          handle NoEx =>
nipkow@13498
   557
  (tracing "The decision procedure failed to prove your proposition\n\
nipkow@13498
   558
           \but could not construct a counter example either.\n\
nipkow@13498
   559
           \Probably the proposition is true but cannot be proved\n\
nipkow@13498
   560
           \by the incomplete decision procedure.")
nipkow@14386
   561
       end;
nipkow@13498
   562
nipkow@6056
   563
fun mknat pTs ixs (atom,i) =
nipkow@6128
   564
  if LA_Logic.is_nat(pTs,atom)
nipkow@6056
   565
  then let val l = map (fn j => if j=i then 1 else 0) ixs
nipkow@6056
   566
       in Some(Lineq(0,Le,l,Nat(i))) end
nipkow@6056
   567
  else None
nipkow@6056
   568
nipkow@13186
   569
(* This code is tricky. It takes a list of premises in the order they occur
nipkow@13186
   570
in the subgoal. Numerical premises are coded as Some(tuple), non-numerical
nipkow@13186
   571
ones as None. Going through the premises, each numeric one is converted into
nipkow@13186
   572
a Lineq. The tricky bit is to convert ~= which is split into two cases < and
nipkow@13498
   573
>. Thus split_items returns a list of equation systems. This may blow up if
nipkow@13186
   574
there are many ~=, but in practice it does not seem to happen. The really
nipkow@13186
   575
tricky bit is to arrange the order of the cases such that they coincide with
nipkow@13186
   576
the order in which the cases are in the end generated by the tactic that
nipkow@13186
   577
applies the generated refutation thms (see function 'refute_tac').
nipkow@13186
   578
nipkow@13186
   579
For variables n of type nat, a constraint 0 <= n is added.
nipkow@13186
   580
*)
nipkow@13464
   581
fun split_items(items) =
nipkow@13464
   582
  let fun elim_neq front _ [] = [rev front]
nipkow@13464
   583
        | elim_neq front n (None::ineqs) = elim_neq front (n+1) ineqs
nipkow@13464
   584
        | elim_neq front n (Some(ineq as (l,i,rel,r,j,d))::ineqs) =
nipkow@13464
   585
          if rel = "~=" then elim_neq front n (ineqs @ [Some(l,i,"<",r,j,d)]) @
nipkow@13464
   586
                             elim_neq front n (ineqs @ [Some(r,j,"<",l,i,d)])
nipkow@13464
   587
          else elim_neq ((ineq,n) :: front) (n+1) ineqs
nipkow@13464
   588
  in elim_neq [] 0 items end;
nipkow@13464
   589
nipkow@13498
   590
fun add_atoms(ats,((lhs,_,_,rhs,_,_),_)) =
nipkow@13498
   591
    (map fst lhs) union ((map fst rhs) union ats)
nipkow@13464
   592
nipkow@13498
   593
fun add_datoms(dats,((lhs,_,_,rhs,_,d),_)) =
nipkow@13498
   594
    (map (pair d o fst) lhs) union ((map (pair d o fst) rhs) union dats)
nipkow@13498
   595
nipkow@13498
   596
fun discr initems = map fst (foldl add_datoms ([],initems));
nipkow@13464
   597
nipkow@13498
   598
fun refutes sg (pTs,params) ex =
nipkow@13498
   599
let
nipkow@13498
   600
  fun refute (initems::initemss) js =
nipkow@13498
   601
    let val atoms = foldl add_atoms ([],initems)
nipkow@13498
   602
        val n = length atoms
nipkow@13498
   603
        val mkleq = mklineq n atoms
nipkow@13498
   604
        val ixs = 0 upto (n-1)
nipkow@13498
   605
        val iatoms = atoms ~~ ixs
nipkow@13498
   606
        val natlineqs = mapfilter (mknat pTs ixs) iatoms
nipkow@13498
   607
        val ineqs = map mkleq initems @ natlineqs
nipkow@13498
   608
    in case elim(ineqs,[]) of
nipkow@13498
   609
         Success(j) =>
nipkow@13498
   610
           (trace_msg "Contradiction!"; refute initemss (js@[j]))
nipkow@13498
   611
       | Failure(hist) =>
nipkow@13498
   612
           (if not ex then ()
nipkow@13498
   613
            else trace_ex(sg,params,atoms,discr initems,n,hist);
nipkow@13498
   614
            None)
nipkow@13498
   615
    end
nipkow@13498
   616
    | refute [] js = Some js
nipkow@13498
   617
in refute end;
nipkow@5982
   618
nipkow@13498
   619
fun refute sg ps ex items = refutes sg ps ex (split_items items) [];
nipkow@13186
   620
nipkow@13186
   621
fun refute_tac(i,justs) =
nipkow@6074
   622
  fn state =>
nipkow@6074
   623
    let val sg = #sign(rep_thm state)
nipkow@13186
   624
        fun just1 j = REPEAT_DETERM(etac LA_Logic.neqE i) THEN
nipkow@13186
   625
                      METAHYPS (fn asms => rtac (mkthm sg asms j) 1) i
nipkow@13186
   626
    in DETERM(resolve_tac [LA_Logic.notI,LA_Logic.ccontr] i) THEN
nipkow@13186
   627
       EVERY(map just1 justs)
nipkow@6074
   628
    end
nipkow@6074
   629
    state;
nipkow@6074
   630
nipkow@14510
   631
fun count P xs = length(filter P xs);
nipkow@14510
   632
nipkow@14510
   633
(* The limit on the number of ~= allowed.
nipkow@14510
   634
   Because each ~= is split into two cases, this can lead to an explosion.
nipkow@14510
   635
*)
nipkow@14510
   636
val fast_arith_neq_limit = ref 9;
nipkow@14510
   637
nipkow@13498
   638
fun prove sg ps ex Hs concl =
nipkow@13186
   639
let val Hitems = map (LA_Data.decomp sg) Hs
nipkow@14510
   640
in if count (fn None => false | Some(_,_,r,_,_,_) => r = "~=") Hitems
nipkow@14510
   641
      > !fast_arith_neq_limit then None
nipkow@14510
   642
   else
nipkow@14510
   643
   case LA_Data.decomp sg concl of
nipkow@13498
   644
     None => refute sg ps ex (Hitems@[None])
nipkow@7551
   645
   | Some(citem as (r,i,rel,l,j,d)) =>
nipkow@13186
   646
       let val neg::rel0 = explode rel
nipkow@13186
   647
           val nrel = if neg = "~" then implode rel0 else "~"^rel
nipkow@13498
   648
       in refute sg ps ex (Hitems @ [Some(r,i,nrel,l,j,d)]) end
nipkow@6074
   649
end;
nipkow@5982
   650
nipkow@5982
   651
(*
nipkow@5982
   652
Fast but very incomplete decider. Only premises and conclusions
nipkow@5982
   653
that are already (negated) (in)equations are taken into account.
nipkow@5982
   654
*)
nipkow@13498
   655
fun lin_arith_tac ex i st = SUBGOAL (fn (A,_) =>
nipkow@13498
   656
  let val params = rev(Logic.strip_params A)
nipkow@13498
   657
      val pTs = map snd params
nipkow@6056
   658
      val Hs = Logic.strip_assums_hyp A
nipkow@6074
   659
      val concl = Logic.strip_assums_concl A
nipkow@12932
   660
  in trace_thm ("Trying to refute subgoal " ^ string_of_int i) st;
nipkow@13498
   661
     case prove (Thm.sign_of_thm st) (pTs,params) ex Hs concl of
nipkow@13464
   662
       None => (trace_msg "Refutation failed."; no_tac)
nipkow@13464
   663
     | Some js => (trace_msg "Refutation succeeded."; refute_tac(i,js))
wenzelm@9420
   664
  end) i st;
nipkow@5982
   665
nipkow@13498
   666
fun cut_lin_arith_tac thms i = cut_facts_tac thms i THEN lin_arith_tac false i;
nipkow@5982
   667
nipkow@13186
   668
(** Forward proof from theorems **)
nipkow@13186
   669
nipkow@13186
   670
(* More tricky code. Needs to arrange the proofs of the multiple cases (due
nipkow@13186
   671
to splits of ~= premises) such that it coincides with the order of the cases
nipkow@13498
   672
generated by function split_items. *)
nipkow@13186
   673
nipkow@13186
   674
datatype splittree = Tip of thm list
nipkow@13186
   675
                   | Spl of thm * cterm * splittree * cterm * splittree
nipkow@13186
   676
nipkow@13186
   677
fun extract imp =
nipkow@13186
   678
let val (Il,r) = Thm.dest_comb imp
nipkow@13186
   679
    val (_,imp1) = Thm.dest_comb Il
nipkow@13186
   680
    val (Ict1,_) = Thm.dest_comb imp1
nipkow@13186
   681
    val (_,ct1) = Thm.dest_comb Ict1
nipkow@13186
   682
    val (Ir,_) = Thm.dest_comb r
nipkow@13186
   683
    val (_,Ict2r) = Thm.dest_comb Ir
nipkow@13186
   684
    val (Ict2,_) = Thm.dest_comb Ict2r
nipkow@13186
   685
    val (_,ct2) = Thm.dest_comb Ict2
nipkow@13186
   686
in (ct1,ct2) end;
nipkow@6074
   687
nipkow@13186
   688
fun splitasms asms =
nipkow@13186
   689
let fun split(asms',[]) = Tip(rev asms')
nipkow@13186
   690
      | split(asms',asm::asms) =
nipkow@13186
   691
      let val spl = asm COMP LA_Logic.neqE
nipkow@13186
   692
          val (ct1,ct2) = extract(cprop_of spl)
nipkow@13186
   693
          val thm1 = assume ct1 and thm2 = assume ct2
nipkow@13186
   694
      in Spl(spl,ct1,split(asms',asms@[thm1]),ct2,split(asms',asms@[thm2])) end
nipkow@13186
   695
      handle THM _ => split(asm::asms', asms)
nipkow@13186
   696
in split([],asms) end;
nipkow@6074
   697
nipkow@13186
   698
fun fwdproof sg (Tip asms) (j::js) = (mkthm sg asms j, js)
nipkow@13186
   699
  | fwdproof sg (Spl(thm,ct1,tree1,ct2,tree2)) js =
nipkow@13186
   700
    let val (thm1,js1) = fwdproof sg tree1 js
nipkow@13186
   701
        val (thm2,js2) = fwdproof sg tree2 js1
nipkow@13186
   702
        val thm1' = implies_intr ct1 thm1
nipkow@13186
   703
        val thm2' = implies_intr ct2 thm2
nipkow@13186
   704
    in (thm2' COMP (thm1' COMP thm), js2) end;
wenzelm@14821
   705
(* needs handle THM _ => None ? *)
nipkow@13186
   706
nipkow@13186
   707
fun prover sg thms Tconcl js pos =
nipkow@13186
   708
let val nTconcl = LA_Logic.neg_prop Tconcl
nipkow@13186
   709
    val cnTconcl = cterm_of sg nTconcl
nipkow@13186
   710
    val nTconclthm = assume cnTconcl
nipkow@13186
   711
    val tree = splitasms (thms @ [nTconclthm])
nipkow@13186
   712
    val (thm,_) = fwdproof sg tree js
nipkow@13186
   713
    val contr = if pos then LA_Logic.ccontr else LA_Logic.notI
nipkow@13186
   714
in Some(LA_Logic.mk_Eq((implies_intr cnTconcl thm) COMP contr)) end
nipkow@13186
   715
(* in case concl contains ?-var, which makes assume fail: *)
nipkow@13186
   716
handle THM _ => None;
nipkow@13186
   717
nipkow@13186
   718
(* PRE: concl is not negated!
nipkow@13186
   719
   This assumption is OK because
nipkow@13186
   720
   1. lin_arith_prover tries both to prove and disprove concl and
nipkow@13186
   721
   2. lin_arith_prover is applied by the simplifier which
nipkow@13186
   722
      dives into terms and will thus try the non-negated concl anyway.
nipkow@13186
   723
*)
wenzelm@15027
   724
fun lin_arith_prover sg ss concl =
wenzelm@15027
   725
let
wenzelm@15027
   726
    val thms = prems_of_ss ss;
wenzelm@15027
   727
    val Hs = map (#prop o rep_thm) thms
nipkow@6102
   728
    val Tconcl = LA_Logic.mk_Trueprop concl
nipkow@13498
   729
in case prove sg ([],[]) false Hs Tconcl of (* concl provable? *)
nipkow@13186
   730
     Some js => prover sg thms Tconcl js true
nipkow@13186
   731
   | None => let val nTconcl = LA_Logic.neg_prop Tconcl
nipkow@13498
   732
          in case prove sg ([],[]) false Hs nTconcl of (* ~concl provable? *)
nipkow@13186
   733
               Some js => prover sg thms nTconcl js false
nipkow@13186
   734
             | None => None
nipkow@6079
   735
          end
nipkow@5982
   736
end;
nipkow@6074
   737
nipkow@6074
   738
end;