src/Provers/Arith/fast_lin_arith.ML
author nipkow
Thu, 21 Dec 2000 16:18:40 +0100
changeset 10717 c09d4ebfec83
parent 10691 4ea37fba9c02
child 12109 bd6eb9194a5d
permissions -rw-r--r--
rational arithemtic
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
     1
(*  Title:      Provers/Arith/fast_lin_arith.ML
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
     2
    ID:         $Id$
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
     3
    Author:     Tobias Nipkow
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
     4
    Copyright   1998  TU Munich
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
     5
6062
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
     6
A generic linear arithmetic package.
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
     7
It provides two tactics
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
     8
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
     9
    lin_arith_tac:         int -> tactic
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    10
cut_lin_arith_tac: thms -> int -> tactic
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    11
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    12
and a simplification procedure
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    13
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    14
    lin_arith_prover: Sign.sg -> thm list -> term -> thm option
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    15
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    16
Only take premises and conclusions into account that are already (negated)
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    17
(in)equations. lin_arith_prover tries to prove or disprove the term.
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    18
*)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    19
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
    20
(* Debugging: set Fast_Arith.trace *)
7582
2650c9c2ab7f Restructured lin.arith.package.
nipkow
parents: 7570
diff changeset
    21
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    22
(*** Data needed for setting up the linear arithmetic package ***)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    23
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    24
signature LIN_ARITH_LOGIC =
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    25
sig
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    26
  val conjI:		thm
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    27
  val ccontr:           thm (* (~ P ==> False) ==> P *)
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    28
  val neqE:             thm (* [| m ~= n; m < n ==> P; n < m ==> P |] ==> P *)
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    29
  val notI:             thm (* (P ==> False) ==> ~ P *)
6110
15c2b571225b Simplified interface.
nipkow
parents: 6102
diff changeset
    30
  val not_lessD:        thm (* ~(m < n) ==> n <= m *)
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    31
  val not_leD:          thm (* ~(m <= n) ==> n < m *)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    32
  val sym:		thm (* x = y ==> y = x *)
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    33
  val mk_Eq: thm -> thm
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    34
  val mk_Trueprop: term -> term
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    35
  val neg_prop: term -> term
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    36
  val is_False: thm -> bool
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    37
  val is_nat: typ list * term -> bool
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    38
  val mk_nat_thm: Sign.sg -> term -> thm
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    39
end;
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    40
(*
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    41
mk_Eq(~in) = `in == False'
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    42
mk_Eq(in) = `in == True'
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    43
where `in' is an (in)equality.
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    44
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    45
neg_prop(t) = neg if t is wrapped up in Trueprop and
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    46
  nt is the (logically) negated version of t, where the negation
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    47
  of a negative term is the term itself (no double negation!);
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    48
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    49
is_nat(parameter-types,t) =  t:nat
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    50
mk_nat_thm(t) = "0 <= t"
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    51
*)
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    52
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    53
signature LIN_ARITH_DATA =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    54
sig
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    55
  val decomp:
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
    56
    Sign.sg -> term -> ((term*rat)list * rat * string * (term*rat)list * rat * bool)option
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
    57
  val number_of: int * typ -> term
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    58
end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    59
(*
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
    60
decomp(`x Rel y') should yield (p,i,Rel,q,j,d)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    61
   where Rel is one of "<", "~<", "<=", "~<=" and "=" and
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    62
         p/q is the decomposition of the sum terms x/y into a list
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
    63
         of summand * multiplicity pairs and a constant summand and
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
    64
         d indicates if the domain is discrete.
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    65
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    66
ss must reduce contradictory <= to False.
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    67
   It should also cancel common summands to keep <= reduced;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    68
   otherwise <= can grow to massive proportions.
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    69
*)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    70
6062
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    71
signature FAST_LIN_ARITH =
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    72
sig
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    73
  val setup: (theory -> theory) list
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
    74
  val map_data: ({add_mono_thms: thm list, mult_mono_thms: (thm*cterm)list, inj_thms: thm list,
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
    75
                 lessD: thm list, simpset: Simplifier.simpset}
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
    76
                 -> {add_mono_thms: thm list, mult_mono_thms: (thm*cterm)list, inj_thms: thm list,
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
    77
                     lessD: thm list, simpset: Simplifier.simpset})
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
    78
                -> theory -> theory
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
    79
  val trace           : bool ref
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
    80
  val lin_arith_prover: Sign.sg -> thm list -> term -> thm option
6062
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    81
  val     lin_arith_tac:             int -> tactic
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    82
  val cut_lin_arith_tac: thm list -> int -> tactic
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    83
end;
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    84
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    85
functor Fast_Lin_Arith(structure LA_Logic:LIN_ARITH_LOGIC 
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    86
                       and       LA_Data:LIN_ARITH_DATA) : FAST_LIN_ARITH =
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    87
struct
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    88
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    89
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    90
(** theory data **)
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    91
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    92
(* data kind 'Provers/fast_lin_arith' *)
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    93
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    94
structure DataArgs =
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    95
struct
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    96
  val name = "Provers/fast_lin_arith";
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
    97
  type T = {add_mono_thms: thm list, mult_mono_thms: (thm*cterm)list, inj_thms: thm list,
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
    98
            lessD: thm list, simpset: Simplifier.simpset};
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    99
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   100
  val empty = {add_mono_thms = [], mult_mono_thms = [], inj_thms = [],
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   101
               lessD = [], simpset = Simplifier.empty_ss};
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   102
  val copy = I;
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   103
  val prep_ext = I;
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   104
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   105
  fun merge ({add_mono_thms= add_mono_thms1, mult_mono_thms= mult_mono_thms1, inj_thms= inj_thms1,
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   106
              lessD = lessD1, simpset = simpset1},
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   107
             {add_mono_thms= add_mono_thms2, mult_mono_thms= mult_mono_thms2, inj_thms= inj_thms2,
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   108
              lessD = lessD2, simpset = simpset2}) =
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   109
    {add_mono_thms = Drule.merge_rules (add_mono_thms1, add_mono_thms2),
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   110
     mult_mono_thms= generic_merge (eq_thm o pairself fst) I I mult_mono_thms1 mult_mono_thms2,
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   111
     inj_thms = Drule.merge_rules (inj_thms1, inj_thms2),
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   112
     lessD = Drule.merge_rules (lessD1, lessD2),
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   113
     simpset = Simplifier.merge_ss (simpset1, simpset2)};
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   114
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   115
  fun print _ _ = ();
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   116
end;
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   117
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   118
structure Data = TheoryDataFun(DataArgs);
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   119
val map_data = Data.map;
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   120
val setup = [Data.init];
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   121
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   122
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   123
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   124
(*** A fast decision procedure ***)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   125
(*** Code ported from HOL Light ***)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   126
(* possible optimizations:
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   127
   use (var,coeff) rep or vector rep  tp save space;
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   128
   treat non-negative atoms separately rather than adding 0 <= atom
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   129
*)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   130
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   131
val trace = ref false;
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   132
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   133
datatype lineq_type = Eq | Le | Lt;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   134
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   135
datatype injust = Asm of int
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   136
                | Nat of int (* index of atom *)
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   137
                | LessD of injust
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   138
                | NotLessD of injust
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   139
                | NotLeD of injust
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   140
                | NotLeDD of injust
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   141
                | Multiplied of int * injust
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   142
                | Multiplied2 of int * injust
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   143
                | Added of injust * injust;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   144
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   145
datatype lineq = Lineq of int * lineq_type * int list * injust;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   146
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   147
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   148
(* Calculate new (in)equality type after addition.                           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   149
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   150
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   151
fun find_add_type(Eq,x) = x
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   152
  | find_add_type(x,Eq) = x
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   153
  | find_add_type(_,Lt) = Lt
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   154
  | find_add_type(Lt,_) = Lt
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   155
  | find_add_type(Le,Le) = Le;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   156
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   157
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   158
(* Multiply out an (in)equation.                                             *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   159
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   160
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   161
fun multiply_ineq n (i as Lineq(k,ty,l,just)) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   162
  if n = 1 then i
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   163
  else if n = 0 andalso ty = Lt then sys_error "multiply_ineq"
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   164
  else if n < 0 andalso (ty=Le orelse ty=Lt) then sys_error "multiply_ineq"
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   165
  else Lineq(n * k,ty,map (apl(n,op * )) l,Multiplied(n,just));
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   166
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   167
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   168
(* Add together (in)equations.                                               *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   169
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   170
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   171
fun add_ineq (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   172
  let val l = map2 (op +) (l1,l2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   173
  in Lineq(k1+k2,find_add_type(ty1,ty2),l,Added(just1,just2)) end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   174
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   175
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   176
(* Elimination of variable between a single pair of (in)equations.           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   177
(* If they're both inequalities, 1st coefficient must be +ve, 2nd -ve.       *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   178
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   179
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   180
fun el 0 (h::_) = h
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   181
  | el n (_::t) = el (n - 1) t
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   182
  | el _ _  = sys_error "el";
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   183
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   184
fun elim_var v (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   185
  let val c1 = el v l1 and c2 = el v l2
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   186
      val m = lcm(abs c1,abs c2)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   187
      val m1 = m div (abs c1) and m2 = m div (abs c2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   188
      val (n1,n2) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   189
        if (c1 >= 0) = (c2 >= 0)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   190
        then if ty1 = Eq then (~m1,m2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   191
             else if ty2 = Eq then (m1,~m2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   192
                  else sys_error "elim_var"
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   193
        else (m1,m2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   194
      val (p1,p2) = if ty1=Eq andalso ty2=Eq andalso (n1 = ~1 orelse n2 = ~1)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   195
                    then (~n1,~n2) else (n1,n2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   196
  in add_ineq (multiply_ineq n1 i1) (multiply_ineq n2 i2) end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   197
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   198
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   199
(* The main refutation-finding code.                                         *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   200
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   201
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   202
fun is_trivial (Lineq(_,_,l,_)) = forall (fn i => i=0) l;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   203
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   204
fun is_answer (ans as Lineq(k,ty,l,_)) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   205
  case ty  of Eq => k <> 0 | Le => k > 0 | Lt => k >= 0;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   206
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   207
fun calc_blowup l =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   208
  let val (p,n) = partition (apl(0,op<)) (filter (apl(0,op<>)) l)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   209
  in (length p) * (length n) end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   210
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   211
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   212
(* Main elimination code:                                                    *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   213
(*                                                                           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   214
(* (1) Looks for immediate solutions (false assertions with no variables).   *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   215
(*                                                                           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   216
(* (2) If there are any equations, picks a variable with the lowest absolute *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   217
(* coefficient in any of them, and uses it to eliminate.                     *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   218
(*                                                                           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   219
(* (3) Otherwise, chooses a variable in the inequality to minimize the       *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   220
(* blowup (number of consequences generated) and eliminates it.              *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   221
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   222
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   223
fun allpairs f xs ys =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   224
  flat(map (fn x => map (fn y => f x y) ys) xs);
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   225
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   226
fun extract_first p =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   227
  let fun extract xs (y::ys) = if p y then (Some y,xs@ys)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   228
                               else extract (y::xs) ys
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   229
        | extract xs []      = (None,xs)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   230
  in extract [] end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   231
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   232
fun print_ineqs ineqs =
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   233
  if !trace then
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   234
     writeln(cat_lines(""::map (fn Lineq(c,t,l,_) =>
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   235
       string_of_int c ^
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   236
       (case t of Eq => " =  " | Lt=> " <  " | Le => " <= ") ^
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   237
       commas(map string_of_int l)) ineqs))
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   238
  else ();
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   239
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   240
fun elim ineqs =
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   241
  let val dummy = print_ineqs ineqs;
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   242
      val (triv,nontriv) = partition is_trivial ineqs in
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   243
  if not(null triv)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   244
  then case find_first is_answer triv of
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   245
         None => elim nontriv | some => some
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   246
  else
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   247
  if null nontriv then None else
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   248
  let val (eqs,noneqs) = partition (fn (Lineq(_,ty,_,_)) => ty=Eq) nontriv in
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   249
  if not(null eqs) then
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   250
     let val clist = foldl (fn (cs,Lineq(_,_,l,_)) => l union cs) ([],eqs)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   251
         val sclist = sort (fn (x,y) => int_ord(abs(x),abs(y)))
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   252
                           (filter (fn i => i<>0) clist)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   253
         val c = hd sclist
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   254
         val (Some(eq as Lineq(_,_,ceq,_)),othereqs) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   255
               extract_first (fn Lineq(_,_,l,_) => c mem l) eqs
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   256
         val v = find_index (fn k => k=c) ceq
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   257
         val (ioth,roth) = partition (fn (Lineq(_,_,l,_)) => el v l = 0)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   258
                                     (othereqs @ noneqs)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   259
         val others = map (elim_var v eq) roth @ ioth
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   260
     in elim others end
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   261
  else
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   262
  let val lists = map (fn (Lineq(_,_,l,_)) => l) noneqs
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   263
      val numlist = 0 upto (length(hd lists) - 1)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   264
      val coeffs = map (fn i => map (el i) lists) numlist
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   265
      val blows = map calc_blowup coeffs
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   266
      val iblows = blows ~~ numlist
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   267
      val nziblows = filter (fn (i,_) => i<>0) iblows
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   268
  in if null nziblows then None else
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   269
     let val (c,v) = hd(sort (fn (x,y) => int_ord(fst(x),fst(y))) nziblows)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   270
         val (no,yes) = partition (fn (Lineq(_,_,l,_)) => el v l = 0) ineqs
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   271
         val (pos,neg) = partition(fn (Lineq(_,_,l,_)) => el v l > 0) yes
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   272
     in elim (no @ allpairs (elim_var v) pos neg) end
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   273
  end
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   274
  end
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   275
  end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   276
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   277
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   278
(* Translate back a proof.                                                   *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   279
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   280
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   281
fun trace_thm msg th = 
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   282
    if !trace then (writeln msg; prth th) else th;
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   283
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   284
fun trace_msg msg = 
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   285
    if !trace then writeln msg else ();
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   286
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   287
(* FIXME OPTIMIZE!!!!
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   288
   Addition/Multiplication need i*t representation rather than t+t+...
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   289
   Get rid of Mulitplied(2). For Nat LA_Data.number_of should return Suc^n
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   290
   because Numerals are not known early enough.
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   291
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   292
Simplification may detect a contradiction 'prematurely' due to type
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   293
information: n+1 <= 0 is simplified to False and does not need to be crossed
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   294
with 0 <= n.
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   295
*)
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   296
local
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   297
 exception FalseE of thm
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   298
in
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   299
fun mkthm sg asms just =
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   300
  let val {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset} = Data.get_sg sg;
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   301
      val atoms = foldl (fn (ats,(lhs,_,_,rhs,_,_)) =>
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   302
                            map fst lhs  union  (map fst rhs  union  ats))
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   303
                        ([], mapfilter (LA_Data.decomp sg o concl_of) asms)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   304
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   305
      fun add2 thm1 thm2 =
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   306
        let val conj = thm1 RS (thm2 RS LA_Logic.conjI)
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   307
        in get_first (fn th => Some(conj RS th) handle _ => None) add_mono_thms
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   308
        end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   309
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   310
      fun try_add [] _ = None
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   311
        | try_add (thm1::thm1s) thm2 = case add2 thm1 thm2 of
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   312
             None => try_add thm1s thm2 | some => some;
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   313
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   314
      fun addthms thm1 thm2 =
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   315
        case add2 thm1 thm2 of
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   316
          None => (case try_add ([thm1] RL inj_thms) thm2 of
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   317
                     None => the(try_add ([thm2] RL inj_thms) thm1)
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   318
                   | Some thm => thm)
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   319
        | Some thm => thm;
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   320
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   321
      fun multn(n,thm) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   322
        let fun mul(i,th) = if i=1 then th else mul(i-1, addthms thm th)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   323
        in if n < 0 then mul(~n,thm) RS LA_Logic.sym else mul(n,thm) end;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   324
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   325
      fun multn2(n,thm) =
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   326
        let val Some(mth,cv) =
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   327
              get_first (fn (th,cv) => Some(thm RS th,cv) handle _ => None) mult_mono_thms
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   328
            val ct = cterm_of sg (LA_Data.number_of(n,#T(rep_cterm cv)))
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   329
        in instantiate ([],[(cv,ct)]) mth end
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   330
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   331
      fun simp thm =
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   332
        let val thm' = full_simplify simpset thm
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   333
        in if LA_Logic.is_False thm' then raise FalseE thm' else thm' end
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   334
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   335
      fun mk(Asm i) = trace_thm "Asm" (nth_elem(i,asms))
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   336
        | mk(Nat(i)) = (trace_msg "Nat";
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   337
			LA_Logic.mk_nat_thm sg (nth_elem(i,atoms)))
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   338
        | mk(LessD(j)) = trace_thm "L" (hd([mk j] RL lessD))
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   339
        | mk(NotLeD(j)) = trace_thm "NLe" (mk j RS LA_Logic.not_leD)
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   340
        | mk(NotLeDD(j)) = trace_thm "NLeD" (hd([mk j RS LA_Logic.not_leD] RL lessD))
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   341
        | mk(NotLessD(j)) = trace_thm "NL" (mk j RS LA_Logic.not_lessD)
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   342
        | mk(Added(j1,j2)) = simp (trace_thm "+" (addthms (mk j1) (mk j2)))
10717
c09d4ebfec83 rational arithemtic
nipkow
parents: 10691
diff changeset
   343
        | mk(Multiplied(n,j)) = (trace_msg("*"^string_of_int n); trace_thm "*" (multn(n,mk j)))
c09d4ebfec83 rational arithemtic
nipkow
parents: 10691
diff changeset
   344
        | mk(Multiplied2(n,j)) = simp (trace_msg("**"^string_of_int n); trace_thm "**" (multn2(n,mk j)))
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   345
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   346
  in trace_msg "mkthm";
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   347
     simplify simpset (mk just) handle FalseE thm => thm end
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   348
end;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   349
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   350
fun coeff poly atom = case assoc(poly,atom) of None => 0 | Some i => i;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   351
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   352
fun lcms is = foldl lcm (1,is);
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   353
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   354
fun integ(rlhs,r,rel,rrhs,s,d) =
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   355
let val (rn,rd) = rep_rat r and (sn,sd) = rep_rat s
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   356
    val m = lcms(map (abs o snd o rep_rat) (r :: s :: map snd rlhs @ map snd rrhs))
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   357
    fun mult(t,r) = let val (i,j) = rep_rat r in (t,i * (m div j)) end
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   358
in (m,(map mult rlhs, rn * (m div rd), rel, map mult rrhs, sn * (m div sd), d)) end
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   359
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   360
fun mklineq atoms =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   361
  let val n = length atoms in
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   362
    fn (item,k) =>
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   363
    let val (m,(lhs,i,rel,rhs,j,discrete)) = integ item
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   364
        val lhsa = map (coeff lhs) atoms
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   365
        and rhsa = map (coeff rhs) atoms
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   366
        val diff = map2 (op -) (rhsa,lhsa)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   367
        val c = i-j
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   368
        val just = Asm k
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   369
        fun lineq(c,le,cs,j) = Some(Lineq(c,le,cs, if m=1 then j else Multiplied2(m,j)))
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   370
    in case rel of
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   371
        "<="   => lineq(c,Le,diff,just)
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   372
       | "~<=" => if discrete
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   373
                  then lineq(1-c,Le,map (op ~) diff,NotLeDD(just))
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   374
                  else lineq(~c,Lt,map (op ~) diff,NotLeD(just))
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   375
       | "<"   => if discrete
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   376
                  then lineq(c+1,Le,diff,LessD(just))
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   377
                  else lineq(c,Lt,diff,just)
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   378
       | "~<"  => lineq(~c,Le,map (op~) diff,NotLessD(just))
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   379
       | "="   => lineq(c,Eq,diff,just)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   380
       | "~="  => None
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   381
       | _     => sys_error("mklineq" ^ rel)   
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   382
    end
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   383
  end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   384
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   385
fun mknat pTs ixs (atom,i) =
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   386
  if LA_Logic.is_nat(pTs,atom)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   387
  then let val l = map (fn j => if j=i then 1 else 0) ixs
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   388
       in Some(Lineq(0,Le,l,Nat(i))) end
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   389
  else None
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   390
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   391
fun abstract pTs items =
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   392
  let val atoms = foldl (fn (ats,((lhs,_,_,rhs,_,_),_)) =>
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   393
                            (map fst lhs) union ((map fst rhs) union ats))
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   394
                        ([],items)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   395
      val ixs = 0 upto (length(atoms)-1)
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   396
      val iatoms = atoms ~~ ixs
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   397
  in mapfilter (mklineq atoms) items @ mapfilter (mknat pTs ixs) iatoms end;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   398
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   399
(* Ordinary refutation *)
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   400
fun refute1(pTs,items) =
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   401
  (case elim (abstract pTs items) of
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   402
       None => []
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   403
     | Some(Lineq(_,_,_,j)) => [j]);
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   404
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   405
fun refute1_tac(i,just) =
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   406
  fn state =>
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   407
    let val sg = #sign(rep_thm state)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   408
    in resolve_tac [LA_Logic.notI,LA_Logic.ccontr] i THEN
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   409
       METAHYPS (fn asms => rtac (mkthm sg asms just) 1) i
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   410
    end
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   411
    state;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   412
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   413
(* Double refutation caused by equality in conclusion *)
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   414
fun refute2(pTs,items, (rhs,i,_,lhs,j,d), nHs) =
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   415
  (case elim (abstract pTs (items@[((rhs,i,"<",lhs,j,d),nHs)])) of
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   416
    None => []
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   417
  | Some(Lineq(_,_,_,j1)) =>
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   418
      (case elim (abstract pTs (items@[((lhs,j,"<",rhs,i,d),nHs)])) of
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   419
        None => []
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   420
      | Some(Lineq(_,_,_,j2)) => [j1,j2]));
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   421
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   422
fun refute2_tac(i,just1,just2) =
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   423
  fn state => 
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   424
    let val sg = #sign(rep_thm state)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   425
    in rtac LA_Logic.ccontr i THEN rotate_tac ~1 i THEN etac LA_Logic.neqE i THEN
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   426
       METAHYPS (fn asms => rtac (mkthm sg asms just1) 1) i THEN
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   427
       METAHYPS (fn asms => rtac (mkthm sg asms just2) 1) i
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   428
    end
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   429
    state;
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   430
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   431
fun prove sg (pTs,Hs,concl) =
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   432
let val nHs = length Hs
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   433
    val ixHs = Hs ~~ (0 upto (nHs-1))
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   434
    val Hitems = mapfilter (fn (h,i) => case LA_Data.decomp sg h of
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   435
                                 None => None | Some(it) => Some(it,i)) ixHs
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   436
in case LA_Data.decomp sg concl of
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   437
     None => if null Hitems then [] else refute1(pTs,Hitems)
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   438
   | Some(citem as (r,i,rel,l,j,d)) =>
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   439
       if rel = "="
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   440
       then refute2(pTs,Hitems,citem,nHs)
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   441
       else let val neg::rel0 = explode rel
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   442
                val nrel = if neg = "~" then implode rel0 else "~"^rel
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   443
            in refute1(pTs, Hitems@[((r,i,nrel,l,j,d),nHs)]) end
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   444
end;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   445
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   446
(*
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   447
Fast but very incomplete decider. Only premises and conclusions
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   448
that are already (negated) (in)equations are taken into account.
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   449
*)
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   450
fun lin_arith_tac i st = SUBGOAL (fn (A,n) =>
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   451
  let val pTs = rev(map snd (Logic.strip_params A))
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   452
      val Hs = Logic.strip_assums_hyp A
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   453
      val concl = Logic.strip_assums_concl A
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   454
  in case prove (Thm.sign_of_thm st) (pTs,Hs,concl) of
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   455
       [j] => refute1_tac(n,j)
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   456
     | [j1,j2] => refute2_tac(n,j1,j2)
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   457
     | _ => no_tac
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   458
  end) i st;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   459
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   460
fun cut_lin_arith_tac thms i = cut_facts_tac thms i THEN lin_arith_tac i;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   461
6079
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   462
fun prover1(just,sg,thms,concl,pos) =
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   463
let val nconcl = LA_Logic.neg_prop concl
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   464
    val cnconcl = cterm_of sg nconcl
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   465
    val Fthm = mkthm sg (thms @ [assume cnconcl]) just
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   466
    val contr = if pos then LA_Logic.ccontr else LA_Logic.notI
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   467
in Some(LA_Logic.mk_Eq ((implies_intr cnconcl Fthm) COMP contr)) end
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   468
handle _ => None;
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   469
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   470
(* handle thm with equality conclusion *)
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   471
fun prover2(just1,just2,sg,thms,concl) =
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   472
let val nconcl = LA_Logic.neg_prop concl (* m ~= n *)
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   473
    val cnconcl = cterm_of sg nconcl
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   474
    val neqthm = assume cnconcl
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   475
    val casethm = neqthm COMP LA_Logic.neqE (* [|m<n ==> R; n<m ==> R|] ==> R *)
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   476
    val [lessimp1,lessimp2] = prems_of casethm
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   477
    val less1 = fst(Logic.dest_implies lessimp1) (* m<n *)
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   478
    and less2 = fst(Logic.dest_implies lessimp2) (* n<m *)
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   479
    val cless1 = cterm_of sg less1 and cless2 = cterm_of sg less2
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   480
    val thm1 = mkthm sg (thms @ [assume cless1]) just1
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   481
    and thm2 = mkthm sg (thms @ [assume cless2]) just2
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   482
    val dthm1 = implies_intr cless1 thm1 and dthm2 = implies_intr cless2 thm2
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   483
    val thm = dthm2 COMP (dthm1 COMP casethm)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   484
in Some(LA_Logic.mk_Eq ((implies_intr cnconcl thm) COMP LA_Logic.ccontr)) end
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   485
handle _ => None;
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   486
6079
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   487
(* PRE: concl is not negated! *)
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   488
fun lin_arith_prover sg thms concl =
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   489
let val Hs = map (#prop o rep_thm) thms
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   490
    val Tconcl = LA_Logic.mk_Trueprop concl
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   491
in case prove sg ([],Hs,Tconcl) of
6079
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   492
     [j] => prover1(j,sg,thms,Tconcl,true)
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   493
   | [j1,j2] => prover2(j1,j2,sg,thms,Tconcl)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   494
   | _ => let val nTconcl = LA_Logic.neg_prop Tconcl
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   495
          in case prove sg ([],Hs,nTconcl) of
6079
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   496
               [j] => prover1(j,sg,thms,nTconcl,false)
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   497
               (* [_,_] impossible because of negation *)
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   498
             | _ => None
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   499
          end
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   500
end;
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   501
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   502
end;