src/Provers/Arith/fast_lin_arith.ML
author nipkow
Fri Nov 27 16:54:59 1998 +0100 (1998-11-27 ago)
changeset 5982 aeb97860d352
child 6056 b21813d1b701
permissions -rw-r--r--
Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
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@5982
     6
A generic linear arithmetic package. At the moment only used for nat.
nipkow@5982
     7
The two tactics provided:
nipkow@5982
     8
    lin_arith_tac:         int -> tactic
nipkow@5982
     9
cut_lin_arith_tac: thms -> int -> tactic
nipkow@5982
    10
Only take premises and conclusions into account
nipkow@5982
    11
that are already (negated) (in)equations.
nipkow@5982
    12
*)
nipkow@5982
    13
nipkow@5982
    14
(*** Data needed for setting up the linear arithmetic package ***)
nipkow@5982
    15
nipkow@5982
    16
signature LIN_ARITH_DATA =
nipkow@5982
    17
sig
nipkow@5982
    18
  val add_mono_thms:    thm list
nipkow@5982
    19
                            (* [| i rel1 j; m rel2 n |] ==> i + m rel3 j + n *)
nipkow@5982
    20
  val conjI:		thm
nipkow@5982
    21
  val ccontr:           thm (* (~ P ==> False) ==> P *)
nipkow@5982
    22
  val lessD:            thm (* m < n ==> Suc m <= n *)
nipkow@5982
    23
  val nat_neqE:         thm (* [| m ~= n; m < n ==> P; n < m ==> P |] ==> P *)
nipkow@5982
    24
  val notI:             thm (* (P ==> False) ==> ~ P *)
nipkow@5982
    25
  val not_leD:          thm (* ~(m <= n) ==> Suc n <= m *)
nipkow@5982
    26
  val not_lessD:        thm (* ~(m < n) ==> n < m *)
nipkow@5982
    27
  val sym:		thm (* x = y ==> y = x *)
nipkow@5982
    28
  val decomp: term ->
nipkow@5982
    29
             ((term * int)list * int * string * (term * int)list * int)option
nipkow@5982
    30
  val simp: thm -> thm
nipkow@5982
    31
end;
nipkow@5982
    32
(*
nipkow@5982
    33
decomp(`x Rel y') should yield (p,i,Rel,q,j)
nipkow@5982
    34
   where Rel is one of "<", "~<", "<=", "~<=" and "=" and
nipkow@5982
    35
         p/q is the decomposition of the sum terms x/y into a list
nipkow@5982
    36
         of summand * multiplicity pairs and a constant summand.
nipkow@5982
    37
nipkow@5982
    38
simp must reduce contradictory <= to False.
nipkow@5982
    39
   It should also cancel common summands to keep <= reduced;
nipkow@5982
    40
   otherwise <= can grow to massive proportions.
nipkow@5982
    41
*)
nipkow@5982
    42
nipkow@5982
    43
functor Fast_Lin_Arith(LA_Data:LIN_ARITH_DATA) =
nipkow@5982
    44
struct
nipkow@5982
    45
nipkow@5982
    46
(*** A fast decision procedure ***)
nipkow@5982
    47
(*** Code ported from HOL Light ***)
nipkow@5982
    48
(* possible optimizations: eliminate eqns first; use (var,coeff) rep *)
nipkow@5982
    49
nipkow@5982
    50
datatype lineq_type = Eq | Le | Lt;
nipkow@5982
    51
nipkow@5982
    52
datatype injust = Given of int
nipkow@5982
    53
                | Fwd of injust * thm
nipkow@5982
    54
                | Multiplied of int * injust
nipkow@5982
    55
                | Added of injust * injust;
nipkow@5982
    56
nipkow@5982
    57
datatype lineq = Lineq of int * lineq_type * int list * injust;
nipkow@5982
    58
nipkow@5982
    59
(* ------------------------------------------------------------------------- *)
nipkow@5982
    60
(* Calculate new (in)equality type after addition.                           *)
nipkow@5982
    61
(* ------------------------------------------------------------------------- *)
nipkow@5982
    62
nipkow@5982
    63
fun find_add_type(Eq,x) = x
nipkow@5982
    64
  | find_add_type(x,Eq) = x
nipkow@5982
    65
  | find_add_type(_,Lt) = Lt
nipkow@5982
    66
  | find_add_type(Lt,_) = Lt
nipkow@5982
    67
  | find_add_type(Le,Le) = Le;
nipkow@5982
    68
nipkow@5982
    69
(* ------------------------------------------------------------------------- *)
nipkow@5982
    70
(* Multiply out an (in)equation.                                             *)
nipkow@5982
    71
(* ------------------------------------------------------------------------- *)
nipkow@5982
    72
nipkow@5982
    73
fun multiply_ineq n (i as Lineq(k,ty,l,just)) =
nipkow@5982
    74
  if n = 1 then i
nipkow@5982
    75
  else if n = 0 andalso ty = Lt then sys_error "multiply_ineq"
nipkow@5982
    76
  else if n < 0 andalso (ty=Le orelse ty=Lt) then sys_error "multiply_ineq"
nipkow@5982
    77
  else Lineq(n * k,ty,map (apl(n,op * )) l,Multiplied(n,just));
nipkow@5982
    78
nipkow@5982
    79
(* ------------------------------------------------------------------------- *)
nipkow@5982
    80
(* Add together (in)equations.                                               *)
nipkow@5982
    81
(* ------------------------------------------------------------------------- *)
nipkow@5982
    82
nipkow@5982
    83
fun add_ineq (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
nipkow@5982
    84
  let val l = map2 (op +) (l1,l2)
nipkow@5982
    85
  in Lineq(k1+k2,find_add_type(ty1,ty2),l,Added(just1,just2)) end;
nipkow@5982
    86
nipkow@5982
    87
(* ------------------------------------------------------------------------- *)
nipkow@5982
    88
(* Elimination of variable between a single pair of (in)equations.           *)
nipkow@5982
    89
(* If they're both inequalities, 1st coefficient must be +ve, 2nd -ve.       *)
nipkow@5982
    90
(* ------------------------------------------------------------------------- *)
nipkow@5982
    91
nipkow@5982
    92
fun gcd x y =
nipkow@5982
    93
  let fun gxd x y =
nipkow@5982
    94
    if y = 0 then x else gxd y (x mod y)
nipkow@5982
    95
  in if x < y then gxd y x else gxd x y end;
nipkow@5982
    96
nipkow@5982
    97
fun lcm x y = (x * y) div gcd x y;
nipkow@5982
    98
nipkow@5982
    99
fun el 0 (h::_) = h
nipkow@5982
   100
  | el n (_::t) = el (n - 1) t
nipkow@5982
   101
  | el _ _  = sys_error "el";
nipkow@5982
   102
nipkow@5982
   103
fun elim_var v (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
nipkow@5982
   104
  let val c1 = el v l1 and c2 = el v l2
nipkow@5982
   105
      val m = lcm (abs c1) (abs c2)
nipkow@5982
   106
      val m1 = m div (abs c1) and m2 = m div (abs c2)
nipkow@5982
   107
      val (n1,n2) =
nipkow@5982
   108
        if (c1 >= 0) = (c2 >= 0)
nipkow@5982
   109
        then if ty1 = Eq then (~m1,m2)
nipkow@5982
   110
             else if ty2 = Eq then (m1,~m2)
nipkow@5982
   111
                  else sys_error "elim_var"
nipkow@5982
   112
        else (m1,m2)
nipkow@5982
   113
      val (p1,p2) = if ty1=Eq andalso ty2=Eq andalso (n1 = ~1 orelse n2 = ~1)
nipkow@5982
   114
                    then (~n1,~n2) else (n1,n2)
nipkow@5982
   115
  in add_ineq (multiply_ineq n1 i1) (multiply_ineq n2 i2) end;
nipkow@5982
   116
nipkow@5982
   117
(* ------------------------------------------------------------------------- *)
nipkow@5982
   118
(* The main refutation-finding code.                                         *)
nipkow@5982
   119
(* ------------------------------------------------------------------------- *)
nipkow@5982
   120
nipkow@5982
   121
fun is_trivial (Lineq(_,_,l,_)) = forall (fn i => i=0) l;
nipkow@5982
   122
nipkow@5982
   123
fun is_answer (ans as Lineq(k,ty,l,_)) =
nipkow@5982
   124
  case ty  of Eq => k <> 0 | Le => k > 0 | Lt => k >= 0;
nipkow@5982
   125
nipkow@5982
   126
fun calc_blowup l =
nipkow@5982
   127
  let val (p,n) = partition (apl(0,op<)) (filter (apl(0,op<>)) l)
nipkow@5982
   128
  in (length p) * (length n) end;
nipkow@5982
   129
nipkow@5982
   130
(* ------------------------------------------------------------------------- *)
nipkow@5982
   131
(* Main elimination code:                                                    *)
nipkow@5982
   132
(*                                                                           *)
nipkow@5982
   133
(* (1) Looks for immediate solutions (false assertions with no variables).   *)
nipkow@5982
   134
(*                                                                           *)
nipkow@5982
   135
(* (2) If there are any equations, picks a variable with the lowest absolute *)
nipkow@5982
   136
(* coefficient in any of them, and uses it to eliminate.                     *)
nipkow@5982
   137
(*                                                                           *)
nipkow@5982
   138
(* (3) Otherwise, chooses a variable in the inequality to minimize the       *)
nipkow@5982
   139
(* blowup (number of consequences generated) and eliminates it.              *)
nipkow@5982
   140
(* ------------------------------------------------------------------------- *)
nipkow@5982
   141
nipkow@5982
   142
fun allpairs f xs ys =
nipkow@5982
   143
  flat(map (fn x => map (fn y => f x y) ys) xs);
nipkow@5982
   144
nipkow@5982
   145
fun extract_first p =
nipkow@5982
   146
  let fun extract xs (y::ys) = if p y then (Some y,xs@ys)
nipkow@5982
   147
                               else extract (y::xs) ys
nipkow@5982
   148
        | extract xs []      = (None,xs)
nipkow@5982
   149
  in extract [] end;
nipkow@5982
   150
nipkow@5982
   151
fun elim ineqs =
nipkow@5982
   152
  let val (triv,nontriv) = partition is_trivial ineqs in
nipkow@5982
   153
  if not(null triv)
nipkow@5982
   154
  then case find_first is_answer triv of
nipkow@5982
   155
         None => elim nontriv | some => some
nipkow@5982
   156
  else
nipkow@5982
   157
  if null nontriv then None else
nipkow@5982
   158
  let val (eqs,noneqs) = partition (fn (Lineq(_,ty,_,_)) => ty=Eq) nontriv in
nipkow@5982
   159
  if not(null eqs) then
nipkow@5982
   160
     let val clist = foldl (fn (cs,Lineq(_,_,l,_)) => l union cs) ([],eqs)
nipkow@5982
   161
         val sclist = sort (fn (x,y) => int_ord(abs(x),abs(y)))
nipkow@5982
   162
                           (filter (fn i => i<>0) clist)
nipkow@5982
   163
         val c = hd sclist
nipkow@5982
   164
         val (Some(eq as Lineq(_,_,ceq,_)),othereqs) =
nipkow@5982
   165
               extract_first (fn Lineq(_,_,l,_) => c mem l) eqs
nipkow@5982
   166
         val v = find_index (fn k => k=c) ceq
nipkow@5982
   167
         val (ioth,roth) = partition (fn (Lineq(_,_,l,_)) => el v l = 0)
nipkow@5982
   168
                                     (othereqs @ noneqs)
nipkow@5982
   169
         val others = map (elim_var v eq) roth @ ioth
nipkow@5982
   170
     in elim others end
nipkow@5982
   171
  else
nipkow@5982
   172
  let val lists = map (fn (Lineq(_,_,l,_)) => l) noneqs
nipkow@5982
   173
      val numlist = 0 upto (length(hd lists) - 1)
nipkow@5982
   174
      val coeffs = map (fn i => map (el i) lists) numlist
nipkow@5982
   175
      val blows = map calc_blowup coeffs
nipkow@5982
   176
      val iblows = blows ~~ numlist
nipkow@5982
   177
      val nziblows = filter (fn (i,_) => i<>0) iblows
nipkow@5982
   178
  in if null nziblows then None else
nipkow@5982
   179
     let val (c,v) = hd(sort (fn (x,y) => int_ord(fst(x),fst(y))) nziblows)
nipkow@5982
   180
         val (no,yes) = partition (fn (Lineq(_,_,l,_)) => el v l = 0) ineqs
nipkow@5982
   181
         val (pos,neg) = partition(fn (Lineq(_,_,l,_)) => el v l > 0) yes
nipkow@5982
   182
     in elim (no @ allpairs (elim_var v) pos neg) end
nipkow@5982
   183
  end
nipkow@5982
   184
  end
nipkow@5982
   185
  end;
nipkow@5982
   186
nipkow@5982
   187
(* ------------------------------------------------------------------------- *)
nipkow@5982
   188
(* Translate back a proof.                                                   *)
nipkow@5982
   189
(* ------------------------------------------------------------------------- *)
nipkow@5982
   190
nipkow@5982
   191
(* FIXME OPTIMIZE!!!! *)
nipkow@5982
   192
fun mkproof asms just =
nipkow@5982
   193
  let fun addthms thm1 thm2 =
nipkow@5982
   194
        let val conj = thm1 RS (thm2 RS LA_Data.conjI)
nipkow@5982
   195
        in the(get_first (fn th => Some(conj RS th) handle _ => None)
nipkow@5982
   196
                         LA_Data.add_mono_thms)
nipkow@5982
   197
        end;
nipkow@5982
   198
nipkow@5982
   199
      fun multn(n,thm) =
nipkow@5982
   200
        let fun mul(i,th) = if i=1 then th else mul(i-1, addthms thm th)
nipkow@5982
   201
        in if n < 0 then mul(~n,thm) RS LA_Data.sym else mul(n,thm) end;
nipkow@5982
   202
nipkow@5982
   203
      fun mk(Given i) = nth_elem(i,asms)
nipkow@5982
   204
        | mk(Fwd(j,thm)) = mk j RS thm
nipkow@5982
   205
        | mk(Added(j1,j2)) = LA_Data.simp(addthms (mk j1) (mk j2))
nipkow@5982
   206
        | mk(Multiplied(n,j)) = multn(n,mk j)
nipkow@5982
   207
nipkow@5982
   208
  in LA_Data.simp(mk just) end;
nipkow@5982
   209
nipkow@5982
   210
nipkow@5982
   211
fun coeff poly atom = case assoc(poly,atom) of None => 0 | Some i => i;
nipkow@5982
   212
nipkow@5982
   213
fun mklineq atoms =
nipkow@5982
   214
  let val n = length atoms in
nipkow@5982
   215
    fn ((lhs,i,rel,rhs,j),k) =>
nipkow@5982
   216
    let val lhsa = map (coeff lhs) atoms
nipkow@5982
   217
        and rhsa = map (coeff rhs) atoms
nipkow@5982
   218
        val diff = map2 (op -) (rhsa,lhsa)
nipkow@5982
   219
        val c = i-j
nipkow@5982
   220
        val just = Given k
nipkow@5982
   221
    in case rel of
nipkow@5982
   222
        "<="   => Some(Lineq(c,Le,diff,just))
nipkow@5982
   223
       | "~<=" => Some(Lineq(1-c,Le,map (op ~) diff,Fwd(just,LA_Data.not_leD)))
nipkow@5982
   224
       | "<"   => Some(Lineq(c+1,Le,diff,Fwd(just,LA_Data.lessD)))
nipkow@5982
   225
       | "~<"  => Some(Lineq(~c,Le,map (op~) diff,Fwd(just,LA_Data.not_lessD)))
nipkow@5982
   226
       | "="   => Some(Lineq(c,Eq,diff,just))
nipkow@5982
   227
       | "~="  => None
nipkow@5982
   228
       | _     => sys_error("mklineq" ^ rel)   
nipkow@5982
   229
    end
nipkow@5982
   230
  end;
nipkow@5982
   231
nipkow@5982
   232
fun abstract items =
nipkow@5982
   233
  let val atoms = foldl (fn (ats,((lhs,_,_,rhs,_),_)) =>
nipkow@5982
   234
                            (map fst lhs) union ((map fst rhs) union ats))
nipkow@5982
   235
                        ([],items)
nipkow@5982
   236
  in mapfilter (mklineq atoms) items end;
nipkow@5982
   237
nipkow@5982
   238
(* Ordinary refutation *)
nipkow@5982
   239
fun refute1_tac items =
nipkow@5982
   240
  let val lineqs = abstract items
nipkow@5982
   241
  in case elim lineqs of
nipkow@5982
   242
       None => K no_tac
nipkow@5982
   243
     | Some(Lineq(_,_,_,j)) =>
nipkow@5982
   244
         resolve_tac [LA_Data.notI,LA_Data.ccontr] THEN'
nipkow@5982
   245
         METAHYPS (fn asms => rtac (mkproof asms j) 1)
nipkow@5982
   246
  end;
nipkow@5982
   247
nipkow@5982
   248
(* Double refutation caused by equality in conclusion *)
nipkow@5982
   249
fun refute2_tac items (rhs,i,_,lhs,j) nHs =
nipkow@5982
   250
  (case elim (abstract(items@[((rhs,i,"<",lhs,j),nHs)])) of
nipkow@5982
   251
    None => K no_tac
nipkow@5982
   252
  | Some(Lineq(_,_,_,j1)) =>
nipkow@5982
   253
      (case elim (abstract(items@[((lhs,j,"<",rhs,i),nHs)])) of
nipkow@5982
   254
        None => K no_tac
nipkow@5982
   255
      | Some(Lineq(_,_,_,j2)) =>
nipkow@5982
   256
          rtac LA_Data.ccontr THEN' etac LA_Data.nat_neqE THEN'
nipkow@5982
   257
          METAHYPS (fn asms => rtac (mkproof asms j1) 1) THEN'
nipkow@5982
   258
          METAHYPS (fn asms => rtac (mkproof asms j2) 1) ));
nipkow@5982
   259
nipkow@5982
   260
(*
nipkow@5982
   261
Fast but very incomplete decider. Only premises and conclusions
nipkow@5982
   262
that are already (negated) (in)equations are taken into account.
nipkow@5982
   263
*)
nipkow@5982
   264
val lin_arith_tac = SUBGOAL (fn (A,n) =>
nipkow@5982
   265
  let val Hs = Logic.strip_assums_hyp A
nipkow@5982
   266
      val nHs = length Hs
nipkow@5982
   267
      val His = Hs ~~ (0 upto (nHs-1))
nipkow@5982
   268
      val Hitems = mapfilter (fn (h,i) => case LA_Data.decomp h of
nipkow@5982
   269
                                 None => None | Some(it) => Some(it,i)) His
nipkow@5982
   270
  in case LA_Data.decomp(Logic.strip_assums_concl A) of
nipkow@5982
   271
       None => if null Hitems then no_tac else refute1_tac Hitems n
nipkow@5982
   272
     | Some(citem as (r,i,rel,l,j)) =>
nipkow@5982
   273
         if rel = "="
nipkow@5982
   274
         then refute2_tac Hitems citem nHs n
nipkow@5982
   275
         else let val neg::rel0 = explode rel
nipkow@5982
   276
                  val nrel = if neg = "~" then implode rel0 else "~"^rel
nipkow@5982
   277
              in refute1_tac (Hitems@[((r,i,nrel,l,j),nHs)]) n end
nipkow@5982
   278
  end);
nipkow@5982
   279
nipkow@5982
   280
fun cut_lin_arith_tac thms i = cut_facts_tac thms i THEN lin_arith_tac i;
nipkow@5982
   281
nipkow@5982
   282
end;