src/Provers/Arith/fast_lin_arith.ML
author webertj
Wed, 22 Mar 2006 11:54:54 +0100
changeset 19314 cf1c19eee826
parent 19049 2103a8e14eaa
child 19318 958d5c8a8306
permissions -rw-r--r--
comment for conjI added
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
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
    14
    lin_arith_prover: theory -> simpset -> term -> thm option
6102
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
19314
cf1c19eee826 comment for conjI added
webertj
parents: 19049
diff changeset
    26
  val conjI:		thm (* P ==> Q ==> P & Q *)
6102
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 notI:             thm (* (P ==> False) ==> ~ P *)
6110
15c2b571225b Simplified interface.
nipkow
parents: 6102
diff changeset
    29
  val not_lessD:        thm (* ~(m < n) ==> n <= m *)
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
    30
  val not_leD:          thm (* ~(m <= n) ==> n < m *)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    31
  val sym:		thm (* x = y ==> y = x *)
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    32
  val mk_Eq: thm -> thm
16735
008d089822e3 takes & in premises apart now.
nipkow
parents: 16458
diff changeset
    33
  val atomize: thm -> thm list
6102
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
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
    38
  val mk_nat_thm: theory -> 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:
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
    56
    theory -> term -> ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool) option
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
    57
  val number_of: IntInf.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
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18572
diff changeset
    73
  val setup: theory -> theory
15184
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
    74
  val map_data: ({add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
    75
                 lessD: thm list, neqE: thm list, simpset: Simplifier.simpset}
15184
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
    76
                 -> {add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
    77
                     lessD: thm list, neqE: thm list, simpset: Simplifier.simpset})
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
    78
                -> theory -> theory
19314
cf1c19eee826 comment for conjI added
webertj
parents: 19049
diff changeset
    79
  val trace: bool ref
14510
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
    80
  val fast_arith_neq_limit: int ref
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
    81
  val lin_arith_prover: theory -> simpset -> term -> thm option
17892
62c397c17d18 Simplifier.theory_context;
wenzelm
parents: 17877
diff changeset
    82
  val     lin_arith_tac:    bool -> int -> tactic
17613
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
    83
  val cut_lin_arith_tac: simpset -> int -> tactic
6062
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    84
end;
ede9fea461f5 Small mods.
nipkow
parents: 6056
diff changeset
    85
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    86
functor Fast_Lin_Arith(structure LA_Logic:LIN_ARITH_LOGIC 
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
    87
                       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
    88
struct
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
    89
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    90
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    91
(** theory data **)
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    92
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    93
(* data kind 'Provers/fast_lin_arith' *)
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    94
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
    95
structure Data = TheoryDataFun
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
    96
(struct
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
    97
  val name = "Provers/fast_lin_arith";
15184
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
    98
  type T = {add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
    99
            lessD: thm list, neqE: thm list, simpset: Simplifier.simpset};
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   100
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   101
  val empty = {add_mono_thms = [], mult_mono_thms = [], inj_thms = [],
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   102
               lessD = [], neqE = [], simpset = Simplifier.empty_ss};
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   103
  val copy = I;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   104
  val extend = I;
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   105
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   106
  fun merge _
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   107
    ({add_mono_thms= add_mono_thms1, mult_mono_thms= mult_mono_thms1, inj_thms= inj_thms1,
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   108
      lessD = lessD1, neqE=neqE1, simpset = simpset1},
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   109
     {add_mono_thms= add_mono_thms2, mult_mono_thms= mult_mono_thms2, inj_thms= inj_thms2,
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   110
      lessD = lessD2, neqE=neqE2, simpset = simpset2}) =
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   111
    {add_mono_thms = Drule.merge_rules (add_mono_thms1, add_mono_thms2),
15184
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   112
     mult_mono_thms = Drule.merge_rules (mult_mono_thms1, mult_mono_thms2),
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   113
     inj_thms = Drule.merge_rules (inj_thms1, inj_thms2),
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   114
     lessD = Drule.merge_rules (lessD1, lessD2),
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   115
     neqE = Drule.merge_rules (neqE1, neqE2),
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   116
     simpset = Simplifier.merge_ss (simpset1, simpset2)};
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   117
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   118
  fun print _ _ = ();
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   119
end);
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   120
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   121
val map_data = Data.map;
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18572
diff changeset
   122
val setup = Data.init;
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   123
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   124
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   125
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   126
(*** A fast decision procedure ***)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   127
(*** Code ported from HOL Light ***)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   128
(* possible optimizations:
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   129
   use (var,coeff) rep or vector rep  tp save space;
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   130
   treat non-negative atoms separately rather than adding 0 <= atom
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   131
*)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   132
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   133
val trace = ref false;
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   134
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   135
datatype lineq_type = Eq | Le | Lt;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   136
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   137
datatype injust = Asm of int
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   138
                | Nat of int (* index of atom *)
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   139
                | LessD of injust
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   140
                | NotLessD of injust
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   141
                | NotLeD of injust
7551
8e934d1a9ac6 Now distinguishes discrete from non-distrete types.
nipkow
parents: 6128
diff changeset
   142
                | NotLeDD of injust
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   143
                | Multiplied of IntInf.int * injust
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   144
                | Multiplied2 of IntInf.int * injust
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   145
                | Added of injust * injust;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   146
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   147
datatype lineq = Lineq of IntInf.int * lineq_type * IntInf.int list * injust;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   148
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   149
fun el 0 (h::_) = h
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   150
  | el n (_::t) = el (n - 1) t
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   151
  | el _ _  = sys_error "el";
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   152
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   153
(* ------------------------------------------------------------------------- *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   154
(* Finding a (counter) example from the trace of a failed elimination        *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   155
(* ------------------------------------------------------------------------- *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   156
(* Examples are represented as rational numbers,                             *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   157
(* Dont blame John Harrison for this code - it is entirely mine. TN          *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   158
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   159
exception NoEx;
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   160
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   161
(* Coding: (i,true,cs) means i <= cs and (i,false,cs) means i < cs.
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   162
   In general, true means the bound is included, false means it is excluded.
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   163
   Need to know if it is a lower or upper bound for unambiguous interpretation!
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   164
*)
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   165
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   166
fun elim_eqns(ineqs,Lineq(i,Le,cs,_)) = (i,true,cs)::ineqs
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   167
  | elim_eqns(ineqs,Lineq(i,Eq,cs,_)) = (i,true,cs)::(~i,true,map ~ cs)::ineqs
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   168
  | elim_eqns(ineqs,Lineq(i,Lt,cs,_)) = (i,false,cs)::ineqs;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   169
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   170
(* PRE: ex[v] must be 0! *)
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   171
fun eval (ex:Rat.rat list) v (a:IntInf.int,le,cs:IntInf.int list) =
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   172
  let val rs = map Rat.rat_of_intinf cs
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   173
      val rsum = Library.foldl Rat.add (Rat.zero, map Rat.mult (rs ~~ ex))
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   174
  in (Rat.mult (Rat.add(Rat.rat_of_intinf a,Rat.neg rsum), Rat.inv(el v rs)), le) end;
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   175
(* If el v rs < 0, le should be negated.
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   176
   Instead this swap is taken into account in ratrelmin2.
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   177
*)
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   178
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   179
fun ratrelmin2(x as (r,ler),y as (s,les)) =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   180
  if r=s then (r, (not ler) andalso (not les)) else if Rat.le(r,s) then x else y;
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   181
fun ratrelmax2(x as (r,ler),y as (s,les)) =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   182
  if r=s then (r,ler andalso les) else if Rat.le(r,s) then y else x;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   183
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   184
val ratrelmin = foldr1 ratrelmin2;
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   185
val ratrelmax = foldr1 ratrelmax2;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   186
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   187
fun ratexact up (r,exact) =
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   188
  if exact then r else
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   189
  let val (p,q) = Rat.quotient_of_rat r
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   190
      val nth = Rat.inv(Rat.rat_of_intinf q)
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   191
  in Rat.add(r,if up then nth else Rat.neg nth) end;
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   192
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   193
fun ratmiddle(r,s) = Rat.mult(Rat.add(r,s),Rat.inv(Rat.rat_of_int 2));
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   194
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   195
fun choose2 d ((lb,exactl),(ub,exactu)) =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   196
  if Rat.le(lb,Rat.zero) andalso (lb <> Rat.zero orelse exactl) andalso
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   197
     Rat.le(Rat.zero,ub) andalso (ub <> Rat.zero orelse exactu)
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   198
  then Rat.zero else
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   199
  if not d
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   200
  then (if Rat.ge0 lb
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   201
        then if exactl then lb else ratmiddle(lb,ub)
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   202
        else if exactu then ub else ratmiddle(lb,ub))
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   203
  else (* discrete domain, both bounds must be exact *)
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   204
  if Rat.ge0 lb then let val lb' = Rat.roundup lb
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   205
                    in if Rat.le(lb',ub) then lb' else raise NoEx end
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   206
               else let val ub' = Rat.rounddown ub
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   207
                    in if Rat.le(lb,ub') then ub' else raise NoEx end;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   208
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   209
fun findex1 discr (ex,(v,lineqs)) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   210
  let val nz = List.filter (fn (Lineq(_,_,cs,_)) => el v cs <> 0) lineqs;
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   211
      val ineqs = Library.foldl elim_eqns ([],nz)
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   212
      val (ge,le) = List.partition (fn (_,_,cs) => el v cs > 0) ineqs
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   213
      val lb = ratrelmax(map (eval ex v) ge)
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   214
      val ub = ratrelmin(map (eval ex v) le)
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17951
diff changeset
   215
  in nth_update (v, choose2 (nth discr v) (lb, ub)) ex end;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   216
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   217
fun findex discr = Library.foldl (findex1 discr);
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   218
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   219
fun elim1 v x =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   220
  map (fn (a,le,bs) => (Rat.add(a,Rat.neg(Rat.mult(el v bs,x))), le,
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17951
diff changeset
   221
                        nth_update (v, Rat.zero) bs));
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   222
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   223
fun single_var v (_,_,cs) = (filter_out (equal Rat.zero) cs = [el v cs]);
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   224
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   225
(* The base case:
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   226
   all variables occur only with positive or only with negative coefficients *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   227
fun pick_vars discr (ineqs,ex) =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   228
  let val nz = filter_out (fn (_,_,cs) => forall (equal Rat.zero) cs) ineqs
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   229
  in case nz of [] => ex
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   230
     | (_,_,cs) :: _ =>
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   231
       let val v = find_index (not o equal Rat.zero) cs
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17951
diff changeset
   232
           val d = nth discr v
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   233
           val pos = Rat.ge0(el v cs)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   234
           val sv = List.filter (single_var v) nz
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   235
           val minmax =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   236
             if pos then if d then Rat.roundup o fst o ratrelmax
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   237
                         else ratexact true o ratrelmax
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   238
                    else if d then Rat.rounddown o fst o ratrelmin
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   239
                         else ratexact false o ratrelmin
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   240
           val bnds = map (fn (a,le,bs) => (Rat.mult(a,Rat.inv(el v bs)),le)) sv
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   241
           val x = minmax((Rat.zero,if pos then true else false)::bnds)
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   242
           val ineqs' = elim1 v x nz
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17951
diff changeset
   243
           val ex' = nth_update (v, x) ex
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   244
       in pick_vars discr (ineqs',ex') end
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   245
  end;
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   246
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   247
fun findex0 discr n lineqs =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   248
  let val ineqs = Library.foldl elim_eqns ([],lineqs)
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   249
      val rineqs = map (fn (a,le,cs) => (Rat.rat_of_intinf a, le, map Rat.rat_of_intinf cs))
14372
51ddf8963c95 Finally fixed the counterexample finder. Can now deal with < on real.
nipkow
parents: 14360
diff changeset
   250
                       ineqs
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   251
  in pick_vars discr (rineqs,replicate n Rat.zero) end;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   252
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   253
(* ------------------------------------------------------------------------- *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   254
(* End of counter example finder. The actual decision procedure starts here. *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   255
(* ------------------------------------------------------------------------- *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   256
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   257
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   258
(* 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
   259
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   260
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   261
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
   262
  | find_add_type(x,Eq) = x
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   263
  | find_add_type(_,Lt) = Lt
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   264
  | find_add_type(Lt,_) = Lt
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   265
  | find_add_type(Le,Le) = Le;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   266
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   267
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   268
(* Multiply out an (in)equation.                                             *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   269
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   270
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   271
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
   272
  if n = 1 then i
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   273
  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
   274
  else if n < 0 andalso (ty=Le orelse ty=Lt) then sys_error "multiply_ineq"
17524
42d56a6dec6e fixed syntax for sml/nj
paulson
parents: 17515
diff changeset
   275
  else Lineq (n * k, ty, map (curry op* n) l, Multiplied (n, just));
5982
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
(* Add together (in)equations.                                               *)
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
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   281
fun add_ineq (i1 as Lineq(k1,ty1,l1,just1)) (i2 as Lineq(k2,ty2,l2,just2)) =
18330
444f16d232a2 introduced new map2, fold
haftmann
parents: 18011
diff changeset
   282
  let val l = map2 (curry (op +)) l1 l2
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   283
  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
   284
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   285
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   286
(* 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
   287
(* 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
   288
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   289
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   290
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
   291
  let val c1 = el v l1 and c2 = el v l2
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   292
      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
   293
      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
   294
      val (n1,n2) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   295
        if (c1 >= 0) = (c2 >= 0)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   296
        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
   297
             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
   298
                  else sys_error "elim_var"
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   299
        else (m1,m2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   300
      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
   301
                    then (~n1,~n2) else (n1,n2)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   302
  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
   303
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   304
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   305
(* The main refutation-finding code.                                         *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   306
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   307
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   308
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
   309
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   310
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
   311
  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
   312
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   313
fun calc_blowup (l:IntInf.int list) =
17496
26535df536ae slight adaptions to library changes
haftmann
parents: 17325
diff changeset
   314
  let val (p,n) = List.partition (curry (op <) 0) (List.filter (curry (op <>) 0) l)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   315
  in (length p) * (length n) end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   316
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   317
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   318
(* Main elimination code:                                                    *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   319
(*                                                                           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   320
(* (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
   321
(*                                                                           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   322
(* (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
   323
(* 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
   324
(*                                                                           *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   325
(* (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
   326
(* 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
   327
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   328
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   329
fun allpairs f xs ys =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   330
  List.concat(map (fn x => map (fn y => f x y) ys) xs);
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   331
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   332
fun extract_first p =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   333
  let fun extract xs (y::ys) = if p y then (SOME y,xs@ys)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   334
                               else extract (y::xs) ys
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   335
        | extract xs []      = (NONE,xs)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   336
  in extract [] end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   337
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   338
fun print_ineqs ineqs =
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   339
  if !trace then
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 12109
diff changeset
   340
     tracing(cat_lines(""::map (fn Lineq(c,t,l,_) =>
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   341
       IntInf.toString c ^
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   342
       (case t of Eq => " =  " | Lt=> " <  " | Le => " <= ") ^
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   343
       commas(map IntInf.toString l)) ineqs))
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   344
  else ();
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   345
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   346
type history = (int * lineq list) list;
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   347
datatype result = Success of injust | Failure of history;
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   348
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   349
fun elim(ineqs,hist) =
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   350
  let val dummy = print_ineqs ineqs;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   351
      val (triv,nontriv) = List.partition is_trivial ineqs in
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   352
  if not(null triv)
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   353
  then case Library.find_first is_answer triv of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   354
         NONE => elim(nontriv,hist)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   355
       | SOME(Lineq(_,_,_,j)) => Success j
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   356
  else
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   357
  if null nontriv then Failure(hist)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   358
  else
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   359
  let val (eqs,noneqs) = List.partition (fn (Lineq(_,ty,_,_)) => ty=Eq) nontriv in
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   360
  if not(null eqs) then
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   361
     let val clist = Library.foldl (fn (cs,Lineq(_,_,l,_)) => l union cs) ([],eqs)
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   362
         val sclist = sort (fn (x,y) => IntInf.compare(abs(x),abs(y)))
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   363
                           (List.filter (fn i => i<>0) clist)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   364
         val c = hd sclist
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   365
         val (SOME(eq as Lineq(_,_,ceq,_)),othereqs) =
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   366
               extract_first (fn Lineq(_,_,l,_) => c mem l) eqs
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   367
         val v = find_index_eq c ceq
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   368
         val (ioth,roth) = List.partition (fn (Lineq(_,_,l,_)) => el v l = 0)
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   369
                                     (othereqs @ noneqs)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   370
         val others = map (elim_var v eq) roth @ ioth
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   371
     in elim(others,(v,nontriv)::hist) end
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   372
  else
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   373
  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
   374
      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
   375
      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
   376
      val blows = map calc_blowup coeffs
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   377
      val iblows = blows ~~ numlist
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   378
      val nziblows = List.filter (fn (i,_) => i<>0) iblows
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   379
  in if null nziblows then Failure((~1,nontriv)::hist)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   380
     else
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   381
     let val (c,v) = hd(sort (fn (x,y) => int_ord(fst(x),fst(y))) nziblows)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   382
         val (no,yes) = List.partition (fn (Lineq(_,_,l,_)) => el v l = 0) ineqs
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   383
         val (pos,neg) = List.partition(fn (Lineq(_,_,l,_)) => el v l > 0) yes
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   384
     in elim(no @ allpairs (elim_var v) pos neg, (v,nontriv)::hist) end
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   385
  end
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   386
  end
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   387
  end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   388
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   389
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   390
(* Translate back a proof.                                                   *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   391
(* ------------------------------------------------------------------------- *)
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   392
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   393
fun trace_thm msg th = 
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 12109
diff changeset
   394
    if !trace then (tracing msg; tracing (Display.string_of_thm th); th) else th;
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   395
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   396
fun trace_msg msg = 
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 12109
diff changeset
   397
    if !trace then tracing msg else ();
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   398
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   399
(* FIXME OPTIMIZE!!!! (partly done already)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   400
   Addition/Multiplication need i*t representation rather than t+t+...
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   401
   Get rid of Mulitplied(2). For Nat LA_Data.number_of should return Suc^n
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   402
   because Numerals are not known early enough.
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   403
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   404
Simplification may detect a contradiction 'prematurely' due to type
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   405
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
   406
with 0 <= n.
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   407
*)
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   408
local
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   409
 exception FalseE of thm
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   410
in
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   411
fun mkthm (sg, ss) asms just =
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   412
  let val {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset, ...} =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   413
          Data.get sg;
17877
67d5ab1cb0d8 Simplifier.inherit_context instead of Simplifier.inherit_bounds;
wenzelm
parents: 17613
diff changeset
   414
      val simpset' = Simplifier.inherit_context ss simpset;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   415
      val atoms = Library.foldl (fn (ats,(lhs,_,_,rhs,_,_)) =>
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   416
                            map fst lhs  union  (map fst rhs  union  ats))
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   417
                        ([], List.mapPartial (fn thm => if Thm.no_prems thm
13464
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   418
                                        then LA_Data.decomp sg (concl_of thm)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   419
                                        else NONE) asms)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   420
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   421
      fun add2 thm1 thm2 =
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   422
        let val conj = thm1 RS (thm2 RS LA_Logic.conjI)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   423
        in get_first (fn th => SOME(conj RS th) handle THM _ => NONE) add_mono_thms
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   424
        end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   425
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   426
      fun try_add [] _ = NONE
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   427
        | try_add (thm1::thm1s) thm2 = case add2 thm1 thm2 of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   428
             NONE => try_add thm1s thm2 | some => some;
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   429
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   430
      fun addthms thm1 thm2 =
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   431
        case add2 thm1 thm2 of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   432
          NONE => (case try_add ([thm1] RL inj_thms) thm2 of
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   433
                     NONE => ( valOf(try_add ([thm2] RL inj_thms) thm1)
15660
255055554c67 handle Option instead of OPTION;
wenzelm
parents: 15570
diff changeset
   434
                               handle Option =>
14360
e654599b114e Added an exception handler and error msg.
nipkow
parents: 13561
diff changeset
   435
                               (trace_thm "" thm1; trace_thm "" thm2;
e654599b114e Added an exception handler and error msg.
nipkow
parents: 13561
diff changeset
   436
                                sys_error "Lin.arith. failed to add thms")
e654599b114e Added an exception handler and error msg.
nipkow
parents: 13561
diff changeset
   437
                             )
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   438
                   | SOME thm => thm)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   439
        | SOME thm => thm;
10575
c78d26d5c3c1 Now adjusted to mixed terms involving coercions.
nipkow
parents: 9420
diff changeset
   440
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   441
      fun multn(n,thm) =
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   442
        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
   443
        in if n < 0 then mul(~n,thm) RS LA_Logic.sym else mul(n,thm) end;
15184
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   444
(*
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   445
      fun multn2(n,thm) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   446
        let val SOME(mth,cv) =
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   447
              get_first (fn (th,cv) => SOME(thm RS th,cv) handle THM _ => NONE) mult_mono_thms
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   448
            val ct = cterm_of sg (LA_Data.number_of(n,#T(rep_cterm cv)))
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   449
        in instantiate ([],[(cv,ct)]) mth end
15184
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   450
*)
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   451
      fun multn2(n,thm) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   452
        let val SOME(mth) =
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   453
              get_first (fn th => SOME(thm RS th) handle THM _ => NONE) mult_mono_thms
15184
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   454
            fun cvar(th,_ $ (_ $ _ $ var)) = cterm_of (#sign(rep_thm th)) var;
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   455
            val cv = cvar(mth, hd(prems_of mth));
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   456
            val ct = cterm_of sg (LA_Data.number_of(n,#T(rep_cterm cv)))
d2c19aea17bc made mult_mono_thms generic.
nipkow
parents: 15027
diff changeset
   457
        in instantiate ([],[(cv,ct)]) mth end
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   458
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   459
      fun simp thm =
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   460
        let val thm' = trace_thm "Simplified:" (full_simplify simpset' thm)
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   461
        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
   462
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17951
diff changeset
   463
      fun mk(Asm i) = trace_thm "Asm" (nth asms i)
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17951
diff changeset
   464
        | mk(Nat i) = (trace_msg "Nat"; LA_Logic.mk_nat_thm sg (nth atoms i))
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   465
        | mk(LessD(j)) = trace_thm "L" (hd([mk j] RL lessD))
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   466
        | mk(NotLeD(j)) = trace_thm "NLe" (mk j RS LA_Logic.not_leD)
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   467
        | 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
   468
        | mk(NotLessD(j)) = trace_thm "NL" (mk j RS LA_Logic.not_lessD)
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   469
        | mk(Added(j1,j2)) = simp (trace_thm "+" (addthms (mk j1) (mk j2)))
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   470
        | mk(Multiplied(n,j)) = (trace_msg("*"^IntInf.toString n); trace_thm "*" (multn(n,mk j)))
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   471
        | mk(Multiplied2(n,j)) = simp (trace_msg("**"^IntInf.toString 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
   472
9073
40d8dfac96b8 tracing flag for arith_tac
paulson
parents: 8263
diff changeset
   473
  in trace_msg "mkthm";
12932
3bda5306d262 updated debugging output
nipkow
parents: 12311
diff changeset
   474
     let val thm = trace_thm "Final thm:" (mk just)
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   475
     in let val fls = simplify simpset' thm
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   476
        in trace_thm "After simplification:" fls;
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   477
           if LA_Logic.is_False fls then fls
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   478
           else
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   479
            (tracing "Assumptions:"; List.app print_thm asms;
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   480
             tracing "Proved:"; print_thm fls;
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   481
             warning "Linear arithmetic should have refuted the assumptions.\n\
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   482
                     \Please inform Tobias Nipkow (nipkow@in.tum.de).";
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   483
             fls)
12932
3bda5306d262 updated debugging output
nipkow
parents: 12311
diff changeset
   484
        end
3bda5306d262 updated debugging output
nipkow
parents: 12311
diff changeset
   485
     end handle FalseE thm => (trace_thm "False reached early:" thm; thm)
3bda5306d262 updated debugging output
nipkow
parents: 12311
diff changeset
   486
  end
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   487
end;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   488
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   489
fun coeff poly atom : IntInf.int =
17325
d9d50222808e introduced new-style AList operations
haftmann
parents: 17029
diff changeset
   490
  AList.lookup (op =) poly atom |> the_default 0;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   491
16358
2e2a506553a3 IntInf change
nipkow
parents: 15965
diff changeset
   492
fun lcms is = Library.foldl lcm (1, is);
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   493
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   494
fun integ(rlhs,r,rel,rrhs,s,d) =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   495
let val (rn,rd) = Rat.quotient_of_rat r and (sn,sd) = Rat.quotient_of_rat s
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   496
    val m = lcms(map (abs o snd o Rat.quotient_of_rat) (r :: s :: map snd rlhs @ map snd rrhs))
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15922
diff changeset
   497
    fun mult(t,r) = 
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   498
        let val (i,j) = Rat.quotient_of_rat r
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15922
diff changeset
   499
        in (t,i * (m div j)) end
12932
3bda5306d262 updated debugging output
nipkow
parents: 12311
diff changeset
   500
in (m,(map mult rlhs, rn*(m div rd), rel, map mult rrhs, sn*(m div sd), d)) end
10691
4ea37fba9c02 towards rtional arithmetic
nipkow
parents: 10575
diff changeset
   501
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   502
fun mklineq n atoms =
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   503
  fn (item,k) =>
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   504
  let val (m,(lhs,i,rel,rhs,j,discrete)) = integ item
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   505
      val lhsa = map (coeff lhs) atoms
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   506
      and rhsa = map (coeff rhs) atoms
18330
444f16d232a2 introduced new map2, fold
haftmann
parents: 18011
diff changeset
   507
      val diff = map2 (curry (op -)) rhsa lhsa
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   508
      val c = i-j
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   509
      val just = Asm k
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   510
      fun lineq(c,le,cs,j) = Lineq(c,le,cs, if m=1 then j else Multiplied2(m,j))
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   511
  in case rel of
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   512
      "<="   => lineq(c,Le,diff,just)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   513
     | "~<=" => if discrete
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   514
                then lineq(1-c,Le,map (op ~) diff,NotLeDD(just))
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   515
                else lineq(~c,Lt,map (op ~) diff,NotLeD(just))
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   516
     | "<"   => if discrete
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   517
                then lineq(c+1,Le,diff,LessD(just))
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   518
                else lineq(c,Lt,diff,just)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   519
     | "~<"  => lineq(~c,Le,map (op~) diff,NotLessD(just))
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   520
     | "="   => lineq(c,Eq,diff,just)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   521
     | _     => sys_error("mklineq" ^ rel)   
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   522
  end;
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   523
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   524
(* ------------------------------------------------------------------------- *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   525
(* Print (counter) example                                                   *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   526
(* ------------------------------------------------------------------------- *)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   527
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   528
fun print_atom((a,d),r) =
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   529
  let val (p,q) = Rat.quotient_of_rat r
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15922
diff changeset
   530
      val s = if d then IntInf.toString p else
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   531
              if p = 0 then "0"
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15922
diff changeset
   532
              else IntInf.toString p ^ "/" ^ IntInf.toString q
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   533
  in a ^ " = " ^ s end;
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   534
19049
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   535
fun produce_ex sds =
17496
26535df536ae slight adaptions to library changes
haftmann
parents: 17325
diff changeset
   536
  curry (op ~~) sds
26535df536ae slight adaptions to library changes
haftmann
parents: 17325
diff changeset
   537
  #> map print_atom
26535df536ae slight adaptions to library changes
haftmann
parents: 17325
diff changeset
   538
  #> commas
19049
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   539
  #> curry (op ^) "Counter example:\n";
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   540
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   541
fun trace_ex(sg,params,atoms,discr,n,hist:history) =
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   542
  if null hist then ()
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   543
  else let val frees = map Free params;
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   544
           fun s_of_t t = Sign.string_of_term sg (subst_bounds(frees,t));
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   545
           val (v,lineqs) :: hist' = hist
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   546
           val start = if v = ~1 then (findex0 discr n lineqs,hist')
17951
ff954cc338c7 introduced functions from Pure/General/rat.ML
haftmann
parents: 17892
diff changeset
   547
                       else (replicate n Rat.zero,hist)
19049
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   548
           val ex = SOME (produce_ex ((map s_of_t atoms)~~discr) (findex discr start))
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   549
             handle NoEx => NONE;
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   550
       in
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   551
         (case ex of
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   552
           SOME s => (warning "arith failed - see trace for a counter example"; tracing s)
2103a8e14eaa counter example: avoid vacuous trace;
wenzelm
parents: 18708
diff changeset
   553
         | NONE => warning "arith failed")
14386
ad1ffcc90162 Removed dangling exception handler
nipkow
parents: 14372
diff changeset
   554
       end;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   555
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   556
fun mknat pTs ixs (atom,i) =
6128
2acc5d36610c More arith refinements.
nipkow
parents: 6110
diff changeset
   557
  if LA_Logic.is_nat(pTs,atom)
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   558
  then let val l = map (fn j => if j=i then 1 else 0) ixs
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   559
       in SOME(Lineq(0,Le,l,Nat(i))) end
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   560
  else NONE
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   561
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   562
(* This code is tricky. It takes a list of premises in the order they occur
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   563
in the subgoal. Numerical premises are coded as SOME(tuple), non-numerical
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   564
ones as NONE. Going through the premises, each numeric one is converted into
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   565
a Lineq. The tricky bit is to convert ~= which is split into two cases < and
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   566
>. Thus split_items returns a list of equation systems. This may blow up if
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   567
there are many ~=, but in practice it does not seem to happen. The really
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   568
tricky bit is to arrange the order of the cases such that they coincide with
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   569
the order in which the cases are in the end generated by the tactic that
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   570
applies the generated refutation thms (see function 'refute_tac').
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   571
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   572
For variables n of type nat, a constraint 0 <= n is added.
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   573
*)
13464
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   574
fun split_items(items) =
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   575
  let fun elim_neq front _ [] = [rev front]
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   576
        | elim_neq front n (NONE::ineqs) = elim_neq front (n+1) ineqs
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   577
        | elim_neq front n (SOME(ineq as (l,i,rel,r,j,d))::ineqs) =
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   578
          if rel = "~=" then elim_neq front n (ineqs @ [SOME(l,i,"<",r,j,d)]) @
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   579
                             elim_neq front n (ineqs @ [SOME(r,j,"<",l,i,d)])
13464
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   580
          else elim_neq ((ineq,n) :: front) (n+1) ineqs
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   581
  in elim_neq [] 0 items end;
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   582
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   583
fun add_atoms(ats,((lhs,_,_,rhs,_,_),_)) =
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   584
    (map fst lhs) union ((map fst rhs) union ats)
13464
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   585
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   586
fun add_datoms(dats,((lhs,_,_,rhs,_,d),_)) =
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   587
    (map (pair d o fst) lhs) union ((map (pair d o fst) rhs) union dats)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   588
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   589
fun discr initems = map fst (Library.foldl add_datoms ([],initems));
13464
c98321b8d638 Fixed two bugs
nipkow
parents: 13186
diff changeset
   590
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   591
fun refutes sg (pTs,params) ex =
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   592
let
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   593
  fun refute (initems::initemss) js =
19314
cf1c19eee826 comment for conjI added
webertj
parents: 19049
diff changeset
   594
    let val _ = trace_msg ("initems: " ^ makestring initems)  (* TODO *)
cf1c19eee826 comment for conjI added
webertj
parents: 19049
diff changeset
   595
        val atoms = Library.foldl add_atoms ([],initems)
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   596
        val n = length atoms
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   597
        val mkleq = mklineq n atoms
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   598
        val ixs = 0 upto (n-1)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   599
        val iatoms = atoms ~~ ixs
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   600
        val natlineqs = List.mapPartial (mknat pTs ixs) iatoms
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   601
        val ineqs = map mkleq initems @ natlineqs
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   602
    in case elim(ineqs,[]) of
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   603
         Success(j) =>
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   604
           (trace_msg "Contradiction!"; refute initemss (js@[j]))
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   605
       | Failure(hist) =>
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   606
           (if not ex then ()
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   607
            else trace_ex(sg,params,atoms,discr initems,n,hist);
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   608
            NONE)
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   609
    end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   610
    | refute [] js = SOME js
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   611
in refute end;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   612
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   613
fun refute sg ps ex items = refutes sg ps ex (split_items items) [];
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   614
17613
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   615
fun refute_tac ss (i,justs) =
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   616
  fn state =>
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   617
    let val sg = #sign(rep_thm state)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   618
        val {neqE, ...} = Data.get sg;
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   619
        fun just1 j = REPEAT_DETERM(eresolve_tac neqE i) THEN
17613
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   620
          METAHYPS (fn asms => rtac (mkthm (sg, ss) asms j) 1) i
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   621
    in DETERM(resolve_tac [LA_Logic.notI,LA_Logic.ccontr] i) THEN
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   622
       EVERY(map just1 justs)
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   623
    end
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   624
    state;
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   625
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   626
fun count P xs = length(List.filter P xs);
14510
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   627
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   628
(* The limit on the number of ~= allowed.
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   629
   Because each ~= is split into two cases, this can lead to an explosion.
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   630
*)
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   631
val fast_arith_neq_limit = ref 9;
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   632
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   633
fun prove sg ps ex Hs concl =
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   634
let val Hitems = map (LA_Data.decomp sg) Hs
19314
cf1c19eee826 comment for conjI added
webertj
parents: 19049
diff changeset
   635
    val _ = trace_msg ("Hitems: " ^ makestring Hitems)  (* TODO *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   636
in if count (fn NONE => false | SOME(_,_,r,_,_,_) => r = "~=") Hitems
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   637
      > !fast_arith_neq_limit then NONE
14510
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   638
   else
73ea1234bb23 introduced fast_arith_neq_limit
nipkow
parents: 14386
diff changeset
   639
   case LA_Data.decomp sg concl of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   640
     NONE => refute sg ps ex (Hitems@[NONE])
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   641
   | SOME(citem as (r,i,rel,l,j,d)) =>
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   642
       let val neg::rel0 = explode rel
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   643
           val nrel = if neg = "~" then implode rel0 else "~"^rel
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   644
       in refute sg ps ex (Hitems @ [SOME(r,i,nrel,l,j,d)]) end
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   645
end;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   646
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   647
(*
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   648
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
   649
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
   650
*)
17613
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   651
fun simpset_lin_arith_tac ss ex i st = SUBGOAL (fn (A,_) =>
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   652
  let val params = rev(Logic.strip_params A)
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   653
      val pTs = map snd params
6056
b21813d1b701 Version 1 of linear arithmetic for nat.
nipkow
parents: 5982
diff changeset
   654
      val Hs = Logic.strip_assums_hyp A
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   655
      val concl = Logic.strip_assums_concl A
12932
3bda5306d262 updated debugging output
nipkow
parents: 12311
diff changeset
   656
  in trace_thm ("Trying to refute subgoal " ^ string_of_int i) st;
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   657
     case prove (Thm.sign_of_thm st) (pTs,params) ex Hs concl of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   658
       NONE => (trace_msg "Refutation failed."; no_tac)
17613
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   659
     | SOME js => (trace_msg "Refutation succeeded."; refute_tac ss (i,js))
9420
d4e9f60fe25a avoid global references;
wenzelm
parents: 9073
diff changeset
   660
  end) i st;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   661
17892
62c397c17d18 Simplifier.theory_context;
wenzelm
parents: 17877
diff changeset
   662
fun lin_arith_tac ex i st =
62c397c17d18 Simplifier.theory_context;
wenzelm
parents: 17877
diff changeset
   663
  simpset_lin_arith_tac (Simplifier.theory_context (Thm.theory_of_thm st) Simplifier.empty_ss)
62c397c17d18 Simplifier.theory_context;
wenzelm
parents: 17877
diff changeset
   664
    ex i st;
17613
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   665
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   666
fun cut_lin_arith_tac ss i =
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   667
  cut_facts_tac (Simplifier.prems_of_ss ss) i THEN
072c21e31b42 Simplifier.inherit_bounds;
wenzelm
parents: 17524
diff changeset
   668
  simpset_lin_arith_tac ss false i;
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   669
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   670
(** Forward proof from theorems **)
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   671
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   672
(* More tricky code. Needs to arrange the proofs of the multiple cases (due
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   673
to splits of ~= premises) such that it coincides with the order of the cases
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   674
generated by function split_items. *)
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   675
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   676
datatype splittree = Tip of thm list
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   677
                   | Spl of thm * cterm * splittree * cterm * splittree
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   678
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   679
fun extract imp =
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   680
let val (Il,r) = Thm.dest_comb imp
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   681
    val (_,imp1) = Thm.dest_comb Il
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   682
    val (Ict1,_) = Thm.dest_comb imp1
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   683
    val (_,ct1) = Thm.dest_comb Ict1
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   684
    val (Ir,_) = Thm.dest_comb r
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   685
    val (_,Ict2r) = Thm.dest_comb Ir
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   686
    val (Ict2,_) = Thm.dest_comb Ict2r
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   687
    val (_,ct2) = Thm.dest_comb Ict2
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   688
in (ct1,ct2) end;
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   689
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   690
fun splitasms sg asms =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16358
diff changeset
   691
let val {neqE, ...}  = Data.get sg;
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   692
    fun split(asms',[]) = Tip(rev asms')
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   693
      | split(asms',asm::asms) =
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   694
      (case get_first (fn th => SOME(asm COMP th) handle THM _ => NONE) neqE
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   695
       of SOME spl =>
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   696
          let val (ct1,ct2) = extract(cprop_of spl)
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   697
              val thm1 = assume ct1 and thm2 = assume ct2
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   698
          in Spl(spl,ct1,split(asms',asms@[thm1]),ct2,split(asms',asms@[thm2]))
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   699
          end
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   700
       | NONE => split(asm::asms', asms))
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   701
in split([],asms) end;
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   702
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   703
fun fwdproof ctxt (Tip asms) (j::js) = (mkthm ctxt asms j, js)
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   704
  | fwdproof ctxt (Spl(thm,ct1,tree1,ct2,tree2)) js =
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   705
    let val (thm1,js1) = fwdproof ctxt tree1 js
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   706
        val (thm2,js2) = fwdproof ctxt tree2 js1
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   707
        val thm1' = implies_intr ct1 thm1
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   708
        val thm2' = implies_intr ct2 thm2
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   709
    in (thm2' COMP (thm1' COMP thm), js2) end;
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   710
(* needs handle THM _ => NONE ? *)
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   711
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   712
fun prover (ctxt as (sg, _)) thms Tconcl js pos =
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   713
let val nTconcl = LA_Logic.neg_prop Tconcl
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   714
    val cnTconcl = cterm_of sg nTconcl
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   715
    val nTconclthm = assume cnTconcl
15922
7ef183f3fc98 neqE applies even if the type is not one which partakes in linear arithmetic.
nipkow
parents: 15660
diff changeset
   716
    val tree = splitasms sg (thms @ [nTconclthm])
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   717
    val (thm,_) = fwdproof ctxt tree js
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   718
    val contr = if pos then LA_Logic.ccontr else LA_Logic.notI
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   719
in SOME(LA_Logic.mk_Eq((implies_intr cnTconcl thm) COMP contr)) end
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   720
(* in case concl contains ?-var, which makes assume fail: *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   721
handle THM _ => NONE;
13186
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   722
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   723
(* PRE: concl is not negated!
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   724
   This assumption is OK because
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   725
   1. lin_arith_prover tries both to prove and disprove concl and
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   726
   2. lin_arith_prover is applied by the simplifier which
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   727
      dives into terms and will thus try the non-negated concl anyway.
ef8ed6adcb38 Big update. Allows case splitting on ~= now (trying both < and >).
nipkow
parents: 13105
diff changeset
   728
*)
15027
d23887300b96 adapted type of simprocs;
wenzelm
parents: 14821
diff changeset
   729
fun lin_arith_prover sg ss concl =
d23887300b96 adapted type of simprocs;
wenzelm
parents: 14821
diff changeset
   730
let
16735
008d089822e3 takes & in premises apart now.
nipkow
parents: 16458
diff changeset
   731
    val thms = List.concat(map LA_Logic.atomize (prems_of_ss ss));
15027
d23887300b96 adapted type of simprocs;
wenzelm
parents: 14821
diff changeset
   732
    val Hs = map (#prop o rep_thm) thms
6102
b985e2184868 Split argument structure.
nipkow
parents: 6079
diff changeset
   733
    val Tconcl = LA_Logic.mk_Trueprop concl
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   734
in case prove sg ([],[]) false Hs Tconcl of (* concl provable? *)
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   735
     SOME js => prover (sg, ss) thms Tconcl js true
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   736
   | NONE => let val nTconcl = LA_Logic.neg_prop Tconcl
13498
5330f1744817 Added counter example generation.
nipkow
parents: 13464
diff changeset
   737
          in case prove sg ([],[]) false Hs nTconcl of (* ~concl provable? *)
17515
830bc15e692c Simplifier.inherit_bounds;
wenzelm
parents: 17496
diff changeset
   738
               SOME js => prover (sg, ss) thms nTconcl js false
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15184
diff changeset
   739
             | NONE => NONE
6079
4f7975c74cdf More arith simplifications.
nipkow
parents: 6074
diff changeset
   740
          end
5982
aeb97860d352 Replaced the puny nat_transitive.ML by the general fast_lin_arith.ML.
nipkow
parents:
diff changeset
   741
end;
6074
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   742
34242451bc91 Added simproc.
nipkow
parents: 6062
diff changeset
   743
end;