src/HOL/Tools/lin_arith.ML
author boehmes
Mon, 08 Jun 2009 22:29:37 +0200
changeset 31510 e0f2bb4b0021
parent 31101 26c7bb764a38
child 32091 30e2ffbba718
permissions -rw-r--r--
fast_lin_arith uses proper multiplication instead of unfolding to additions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/lin_arith.ML
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 28053
diff changeset
     2
    Author:     Tjark Weber and Tobias Nipkow, TU Muenchen
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     4
HOL setup for linear arithmetic (see Provers/Arith/fast_lin_arith.ML).
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     5
*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     6
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     7
signature LIN_ARITH =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     8
sig
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
     9
  val pre_tac: Proof.context -> int -> tactic
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
    10
  val simple_tac: Proof.context -> int -> tactic
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
    11
  val tac: Proof.context -> int -> tactic
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
    12
  val simproc: simpset -> term -> thm option
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    13
  val add_inj_thms: thm list -> Context.generic -> Context.generic
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    14
  val add_lessD: thm -> Context.generic -> Context.generic
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    15
  val add_simps: thm list -> Context.generic -> Context.generic
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    16
  val add_simprocs: simproc list -> Context.generic -> Context.generic
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
    17
  val add_inj_const: string * typ -> Context.generic -> Context.generic
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    18
  val add_discrete_type: string -> Context.generic -> Context.generic
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
    19
  val set_number_of: (theory -> typ -> int -> cterm) -> Context.generic ->
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
    20
    Context.generic
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
    21
  val setup: Context.generic -> Context.generic
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    22
  val global_setup: theory -> theory
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
    23
  val split_limit: int Config.T
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
    24
  val neq_limit: int Config.T
27017
1e0e8c1adf8c added warning_count for issued reconstruction failure messages;
wenzelm
parents: 26942
diff changeset
    25
  val warning_count: int ref
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
    26
  val trace: bool ref
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    27
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    28
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    29
structure Lin_Arith: LIN_ARITH =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    30
struct
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    31
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    32
(* Parameters data for general linear arithmetic functor *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    33
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    34
structure LA_Logic: LIN_ARITH_LOGIC =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    35
struct
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    36
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    37
val ccontr = ccontr;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    38
val conjI = conjI;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    39
val notI = notI;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    40
val sym = sym;
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
    41
val trueI = TrueI;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    42
val not_lessD = @{thm linorder_not_less} RS iffD1;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    43
val not_leD = @{thm linorder_not_le} RS iffD1;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    44
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    45
fun mk_Eq thm = thm RS Eq_FalseI handle THM _ => thm RS Eq_TrueI;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    46
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    47
val mk_Trueprop = HOLogic.mk_Trueprop;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    48
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    49
fun atomize thm = case Thm.prop_of thm of
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    50
    Const ("Trueprop", _) $ (Const (@{const_name "op &"}, _) $ _ $ _) =>
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    51
    atomize (thm RS conjunct1) @ atomize (thm RS conjunct2)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    52
  | _ => [thm];
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    53
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    54
fun neg_prop ((TP as Const("Trueprop", _)) $ (Const (@{const_name "Not"}, _) $ t)) = TP $ t
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    55
  | neg_prop ((TP as Const("Trueprop", _)) $ t) = TP $ (HOLogic.Not $t)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    56
  | neg_prop t = raise TERM ("neg_prop", [t]);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    57
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    58
fun is_False thm =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    59
  let val _ $ t = Thm.prop_of thm
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    60
  in t = HOLogic.false_const end;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    61
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    62
fun is_nat t = (fastype_of1 t = HOLogic.natT);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    63
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    64
fun mk_nat_thm thy t =
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    65
  let
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    66
    val cn = cterm_of thy (Var (("n", 0), HOLogic.natT))
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    67
    and ct = cterm_of thy t
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    68
  in instantiate ([], [(cn, ct)]) @{thm le0} end;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    69
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    70
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    71
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    72
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    73
(* arith context data *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    74
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    75
structure Lin_Arith_Data = GenericDataFun
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    76
(
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    77
  type T = {splits: thm list,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    78
            inj_consts: (string * typ) list,
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    79
            discrete: string list};
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    80
  val empty = {splits = [], inj_consts = [], discrete = []};
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    81
  val extend = I;
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 28053
diff changeset
    82
  fun merge _
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    83
   ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1},
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    84
    {splits= splits2, inj_consts= inj_consts2, discrete= discrete2}) : T =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    85
   {splits = Library.merge Thm.eq_thm_prop (splits1, splits2),
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    86
    inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    87
    discrete = Library.merge (op =) (discrete1, discrete2)};
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    88
);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    89
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    90
val get_arith_data = Lin_Arith_Data.get o Context.Proof;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    91
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    92
fun add_split thm = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    93
  {splits = update Thm.eq_thm_prop thm splits,
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    94
   inj_consts = inj_consts, discrete = discrete});
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    95
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
    96
fun add_discrete_type d = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    97
  {splits = splits, inj_consts = inj_consts,
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
    98
   discrete = update (op =) d discrete});
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    99
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   100
fun add_inj_const c = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
   101
  {splits = splits, inj_consts = update (op =) c inj_consts,
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
   102
   discrete = discrete});
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   103
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   104
val (split_limit, setup_split_limit) = Attrib.config_int "linarith_split_limit" 9;
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   105
val (neq_limit, setup_neq_limit) = Attrib.config_int "linarith_neq_limit" 9;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   106
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   107
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   108
structure LA_Data =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   109
struct
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   110
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   111
val fast_arith_neq_limit = neq_limit;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   112
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   113
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   114
(* Decomposition of terms *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   115
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   116
(*internal representation of linear (in-)equations*)
26942
87e4208700d1 renamed type decompT to decomp;
wenzelm
parents: 26110
diff changeset
   117
type decomp =
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   118
  ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   119
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   120
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   121
  | nT _                      = false;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   122
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   123
fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   124
             (term * Rat.rat) list * Rat.rat =
29548
02a52ae34b7a bug fixes
nipkow@lapbroy100.local
parents: 29528
diff changeset
   125
  case AList.lookup Pattern.aeconv p t of
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   126
      NONE   => ((t, m) :: p, i)
29548
02a52ae34b7a bug fixes
nipkow@lapbroy100.local
parents: 29528
diff changeset
   127
    | SOME n => (AList.update Pattern.aeconv (t, Rat.add n m) p, i);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   128
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   129
(* decompose nested multiplications, bracketing them to the right and combining
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   130
   all their coefficients
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   131
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   132
   inj_consts: list of constants to be ignored when encountered
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   133
               (e.g. arithmetic type conversions that preserve value)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   134
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   135
   m: multiplicity associated with the entire product
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   136
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   137
   returns either (SOME term, associated multiplicity) or (NONE, constant)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   138
*)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   139
fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   140
let
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   141
  fun demult ((mC as Const (@{const_name HOL.times}, _)) $ s $ t, m) =
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   142
      (case s of Const (@{const_name HOL.times}, _) $ s1 $ s2 =>
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   143
        (* bracketing to the right: '(s1 * s2) * t' becomes 's1 * (s2 * t)' *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   144
        demult (mC $ s1 $ (mC $ s2 $ t), m)
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   145
      | _ =>
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   146
        (* product 's * t', where either factor can be 'NONE' *)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   147
        (case demult (s, m) of
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   148
          (SOME s', m') =>
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   149
            (case demult (t, m') of
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   150
              (SOME t', m'') => (SOME (mC $ s' $ t'), m'')
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   151
            | (NONE,    m'') => (SOME s', m''))
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   152
        | (NONE,    m') => demult (t, m')))
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   153
    | demult ((mC as Const (@{const_name HOL.divide}, _)) $ s $ t, m) =
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   154
      (* FIXME: Shouldn't we simplify nested quotients, e.g. '(s/t)/u' could
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   155
         become 's/(t*u)', and '(s*t)/u' could become 's*(t/u)' ?   Note that
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   156
         if we choose to do so here, the simpset used by arith must be able to
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   157
         perform the same simplifications. *)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   158
      (* FIXME: Currently we treat the numerator as atomic unless the
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   159
         denominator can be reduced to a numeric constant.  It might be better
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   160
         to demult the numerator in any case, and invent a new term of the form
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   161
         '1 / t' if the numerator can be reduced, but the denominator cannot. *)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   162
      (* FIXME: Currently we even treat the whole fraction as atomic unless the
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   163
         denominator can be reduced to a numeric constant.  It might be better
25015
1a84a9ae9d58 typo in comment fixed
webertj
parents: 24920
diff changeset
   164
         to use the partially reduced denominator (i.e. 's / (2*t)' could be
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   165
         demult'ed to 's / t' with multiplicity .5).   This would require a
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   166
         very simple change only below, but it breaks existing proofs. *)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   167
      (* quotient 's / t', where the denominator t can be NONE *)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   168
      (* Note: will raise Rat.DIVZERO iff m' is Rat.zero *)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   169
      (case demult (t, Rat.one) of
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   170
        (SOME _, _) => (SOME (mC $ s $ t), m)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   171
      | (NONE,  m') => apsnd (Rat.mult (Rat.inv m')) (demult (s, m)))
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   172
    (* terms that evaluate to numeric constants *)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   173
    | demult (Const (@{const_name HOL.uminus}, _) $ t, m) = demult (t, Rat.neg m)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   174
    | demult (Const (@{const_name HOL.zero}, _), m) = (NONE, Rat.zero)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   175
    | demult (Const (@{const_name HOL.one}, _), m) = (NONE, m)
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   176
    (*Warning: in rare cases number_of encloses a non-numeral,
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   177
      in which case dest_numeral raises TERM; hence all the handles below.
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   178
      Same for Suc-terms that turn out not to be numerals -
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   179
      although the simplifier should eliminate those anyway ...*)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   180
    | demult (t as Const ("Int.number_class.number_of", _) $ n, m) =
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   181
      ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   182
        handle TERM _ => (SOME t, m))
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   183
    | demult (t as Const (@{const_name Suc}, _) $ _, m) =
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   184
      ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat t)))
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   185
        handle TERM _ => (SOME t, m))
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   186
    (* injection constants are ignored *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   187
    | demult (t as Const f $ x, m) =
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   188
      if member (op =) inj_consts f then demult (x, m) else (SOME t, m)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   189
    (* everything else is considered atomic *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   190
    | demult (atom, m) = (SOME atom, m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   191
in demult end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   192
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   193
fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   194
            ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   195
let
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   196
  (* Turns a term 'all' and associated multiplicity 'm' into a list 'p' of
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   197
     summands and associated multiplicities, plus a constant 'i' (with implicit
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   198
     multiplicity 1) *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   199
  fun poly (Const (@{const_name HOL.plus}, _) $ s $ t,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   200
        m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   201
    | poly (all as Const (@{const_name HOL.minus}, T) $ s $ t, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   202
        if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   203
    | poly (all as Const (@{const_name HOL.uminus}, T) $ t, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   204
        if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   205
    | poly (Const (@{const_name HOL.zero}, _), _, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   206
        pi
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   207
    | poly (Const (@{const_name HOL.one}, _), m, (p, i)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   208
        (p, Rat.add i m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   209
    | poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   210
        poly (t, m, (p, Rat.add i m))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   211
    | poly (all as Const (@{const_name HOL.times}, _) $ _ $ _, m, pi as (p, i)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   212
        (case demult inj_consts (all, m) of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   213
           (NONE,   m') => (p, Rat.add i m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   214
         | (SOME u, m') => add_atom u m' pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   215
    | poly (all as Const (@{const_name HOL.divide}, _) $ _ $ _, m, pi as (p, i)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   216
        (case demult inj_consts (all, m) of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   217
           (NONE,   m') => (p, Rat.add i m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   218
         | (SOME u, m') => add_atom u m' pi)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   219
    | poly (all as Const ("Int.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   220
        (let val k = HOLogic.dest_numeral t
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   221
            val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   222
        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   223
        handle TERM _ => add_atom all m pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   224
    | poly (all as Const f $ x, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   225
        if f mem inj_consts then poly (x, m, pi) else add_atom all m pi
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   226
    | poly (all, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   227
        add_atom all m pi
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   228
  val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   229
  val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   230
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   231
  case rel of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   232
    @{const_name HOL.less}    => SOME (p, i, "<", q, j)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   233
  | @{const_name HOL.less_eq} => SOME (p, i, "<=", q, j)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   234
  | "op ="              => SOME (p, i, "=", q, j)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   235
  | _                   => NONE
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   236
end handle Rat.DIVZERO => NONE;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   237
24271
499608101177 avoid low-level tsig;
wenzelm
parents: 24196
diff changeset
   238
fun of_lin_arith_sort thy U =
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   239
  Sign.of_sort thy (U, @{sort Ring_and_Field.ordered_idom});
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   240
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   241
fun allows_lin_arith thy (discrete : string list) (U as Type (D, [])) : bool * bool =
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   242
      if of_lin_arith_sort thy U then (true, member (op =) discrete D)
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   243
      else if member (op =) discrete D then (true, true) else (false, false)
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   244
  | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   245
26942
87e4208700d1 renamed type decompT to decomp;
wenzelm
parents: 26110
diff changeset
   246
fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decomp option =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   247
  case T of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   248
    Type ("fun", [U, _]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   249
      (case allows_lin_arith thy discrete U of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   250
        (true, d) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   251
          (case decomp0 inj_consts xxx of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   252
            NONE                   => NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   253
          | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   254
      | (false, _) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   255
          NONE)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   256
  | _ => NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   257
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   258
fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   259
  | negate NONE                        = NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   260
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   261
fun decomp_negation data
26942
87e4208700d1 renamed type decompT to decomp;
wenzelm
parents: 26110
diff changeset
   262
  ((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decomp option =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   263
      decomp_typecheck data (T, (rel, lhs, rhs))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   264
  | decomp_negation data ((Const ("Trueprop", _)) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   265
  (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   266
      negate (decomp_typecheck data (T, (rel, lhs, rhs)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   267
  | decomp_negation data _ =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   268
      NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   269
26942
87e4208700d1 renamed type decompT to decomp;
wenzelm
parents: 26110
diff changeset
   270
fun decomp ctxt : term -> decomp option =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   271
  let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   272
    val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   273
    val {discrete, inj_consts, ...} = get_arith_data ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   274
  in decomp_negation (thy, discrete, inj_consts) end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   275
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   276
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _))                      = nT T
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   277
  | domain_is_nat (_ $ (Const ("Not", _) $ (Const (_, T) $ _ $ _))) = nT T
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   278
  | domain_is_nat _                                                 = false;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   279
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   280
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   281
(*---------------------------------------------------------------------------*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   282
(* the following code performs splitting of certain constants (e.g. min,     *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   283
(* max) in a linear arithmetic problem; similar to what split_tac later does *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   284
(* to the proof state                                                        *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   285
(*---------------------------------------------------------------------------*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   286
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   287
(* checks if splitting with 'thm' is implemented                             *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   288
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   289
fun is_split_thm (thm : thm) : bool =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   290
  case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   291
    (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   292
    case head_of lhs of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   293
      Const (a, _) => member (op =) [@{const_name Orderings.max},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   294
                                    @{const_name Orderings.min},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   295
                                    @{const_name HOL.abs},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   296
                                    @{const_name HOL.minus},
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   297
                                    "Int.nat",
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   298
                                    "Divides.div_class.mod",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   299
                                    "Divides.div_class.div"] a
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   300
    | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   301
                                 Display.string_of_thm thm);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   302
                       false))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   303
  | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   304
                   Display.string_of_thm thm);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   305
          false);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   306
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   307
(* substitute new for occurrences of old in a term, incrementing bound       *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   308
(* variables as needed when substituting inside an abstraction               *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   309
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   310
fun subst_term ([] : (term * term) list) (t : term) = t
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   311
  | subst_term pairs                     t          =
29528
00fe02648e5d bug fix
nipkow
parents: 29288
diff changeset
   312
      (case AList.lookup Pattern.aeconv pairs t of
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   313
        SOME new =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   314
          new
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   315
      | NONE     =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   316
          (case t of Abs (a, T, body) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   317
            let val pairs' = map (pairself (incr_boundvars 1)) pairs
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   318
            in  Abs (a, T, subst_term pairs' body)  end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   319
          | t1 $ t2                   =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   320
            subst_term pairs t1 $ subst_term pairs t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   321
          | _ => t));
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   322
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   323
(* approximates the effect of one application of split_tac (followed by NNF  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   324
(* normalization) on the subgoal represented by '(Ts, terms)'; returns a     *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   325
(* list of new subgoals (each again represented by a typ list for bound      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   326
(* variables and a term list for premises), or NONE if split_tac would fail  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   327
(* on the subgoal                                                            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   328
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   329
(* FIXME: currently only the effect of certain split theorems is reproduced  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   330
(*        (which is why we need 'is_split_thm').  A more canonical           *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   331
(*        implementation should analyze the right-hand side of the split     *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   332
(*        theorem that can be applied, and modify the subgoal accordingly.   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   333
(*        Or even better, the splitter should be extended to provide         *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   334
(*        splitting on terms as well as splitting on theorems (where the     *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   335
(*        former can have a faster implementation as it does not need to be  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   336
(*        proof-producing).                                                  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   337
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   338
fun split_once_items ctxt (Ts : typ list, terms : term list) :
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   339
                     (typ list * term list) list option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   340
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   341
  val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   342
  (* takes a list  [t1, ..., tn]  to the term                                *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   343
  (*   tn' --> ... --> t1' --> False  ,                                      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   344
  (* where ti' = HOLogic.dest_Trueprop ti                                    *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   345
  fun REPEAT_DETERM_etac_rev_mp terms' =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   346
    fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop terms') HOLogic.false_const
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   347
  val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   348
  val cmap       = Splitter.cmap_of_split_thms split_thms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   349
  val splits     = Splitter.split_posns cmap thy Ts (REPEAT_DETERM_etac_rev_mp terms)
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   350
  val split_limit = Config.get ctxt split_limit
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   351
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   352
  if length splits > split_limit then
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   353
   (tracing ("linarith_split_limit exceeded (current value is " ^
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   354
      string_of_int split_limit ^ ")"); NONE)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   355
  else (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   356
  case splits of [] =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   357
    (* split_tac would fail: no possible split *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   358
    NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   359
  | ((_, _, _, split_type, split_term) :: _) => (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   360
    (* ignore all but the first possible split *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   361
    case strip_comb split_term of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   362
    (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   363
      (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   364
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   365
        val rev_terms     = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   366
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   367
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   368
        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   369
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   370
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   371
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   372
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   373
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   374
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   375
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   376
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   377
    (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   378
    | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   379
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   380
        val rev_terms     = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   381
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   382
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   383
        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   384
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   385
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   386
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   387
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   388
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   389
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   390
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   391
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   392
    (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   393
    | (Const (@{const_name HOL.abs}, _), [t1]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   394
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   395
        val rev_terms   = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   396
        val terms1      = map (subst_term [(split_term, t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   397
        val terms2      = map (subst_term [(split_term, Const (@{const_name HOL.uminus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   398
                            split_type --> split_type) $ t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   399
        val zero        = Const (@{const_name HOL.zero}, split_type)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   400
        val zero_leq_t1 = Const (@{const_name HOL.less_eq},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   401
                            split_type --> split_type --> HOLogic.boolT) $ zero $ t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   402
        val t1_lt_zero  = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   403
                            split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   404
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   405
        val subgoal1    = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   406
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   407
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   408
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   409
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   410
    (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   411
    | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   412
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   413
        (* "d" in the above theorem becomes a new bound variable after NNF   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   414
        (* transformation, therefore some adjustment of indices is necessary *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   415
        val rev_terms       = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   416
        val zero            = Const (@{const_name HOL.zero}, split_type)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   417
        val d               = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   418
        val terms1          = map (subst_term [(split_term, zero)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   419
        val terms2          = map (subst_term [(incr_boundvars 1 split_term, d)])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   420
                                (map (incr_boundvars 1) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   421
        val t1'             = incr_boundvars 1 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   422
        val t2'             = incr_boundvars 1 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   423
        val t1_lt_t2        = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   424
                                split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   425
        val t1_eq_t2_plus_d = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   426
                                (Const (@{const_name HOL.plus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   427
                                  split_type --> split_type --> split_type) $ t2' $ d)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   428
        val not_false       = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   429
        val subgoal1        = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   430
        val subgoal2        = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   431
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   432
        SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   433
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   434
    (* ?P (nat ?i) = ((ALL n. ?i = int n --> ?P n) & (?i < 0 --> ?P 0)) *)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   435
    | (Const ("Int.nat", _), [t1]) =>
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   436
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   437
        val rev_terms   = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   438
        val zero_int    = Const (@{const_name HOL.zero}, HOLogic.intT)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   439
        val zero_nat    = Const (@{const_name HOL.zero}, HOLogic.natT)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   440
        val n           = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   441
        val terms1      = map (subst_term [(incr_boundvars 1 split_term, n)])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   442
                            (map (incr_boundvars 1) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   443
        val terms2      = map (subst_term [(split_term, zero_nat)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   444
        val t1'         = incr_boundvars 1 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   445
        val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
24196
f1dbfd7e3223 localized of_nat
haftmann
parents: 24112
diff changeset
   446
                            (Const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) $ n)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   447
        val t1_lt_zero  = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   448
                            HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   449
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   450
        val subgoal1    = (HOLogic.mk_Trueprop t1_eq_int_n) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   451
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   452
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   453
        SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   454
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   455
    (* "?P ((?n::nat) mod (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   456
         ((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   457
           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   458
    | (Const ("Divides.div_class.mod", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   459
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   460
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   461
        val zero                    = Const (@{const_name HOL.zero}, split_type)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   462
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   463
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   464
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   465
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, j)])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   466
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   467
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   468
        val t2'                     = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   469
        val t2_eq_zero              = Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   470
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   471
        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   472
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   473
        val j_lt_t2                 = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   474
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   475
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   476
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   477
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   478
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   479
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   480
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   481
        val subgoal2                = (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   482
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   483
                                          @ terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   484
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   485
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   486
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   487
    (* "?P ((?n::nat) div (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   488
         ((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   489
           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   490
    | (Const ("Divides.div_class.div", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   491
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   492
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   493
        val zero                    = Const (@{const_name HOL.zero}, split_type)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   494
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   495
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   496
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   497
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, i)])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   498
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   499
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   500
        val t2'                     = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   501
        val t2_eq_zero              = Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   502
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   503
        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   504
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   505
        val j_lt_t2                 = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   506
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   507
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   508
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   509
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   510
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   511
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   512
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   513
        val subgoal2                = (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   514
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   515
                                          @ terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   516
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   517
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   518
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   519
    (* "?P ((?n::int) mod (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   520
         ((iszero (number_of ?k) --> ?P ?n) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   521
          (neg (number_of (uminus ?k)) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   522
            (ALL i j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   523
          (neg (number_of ?k) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   524
            (ALL i j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   525
    | (Const ("Divides.div_class.mod",
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   526
        Type ("fun", [Type ("Int.int", []), _])), [t1, t2 as (number_of $ k)]) =>
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   527
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   528
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   529
        val zero                    = Const (@{const_name HOL.zero}, split_type)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   530
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   531
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   532
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   533
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, j)])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   534
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   535
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   536
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   537
        val iszero_t2               = Const ("Int.iszero", split_type --> HOLogic.boolT) $ t2
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   538
        val neg_minus_k             = Const ("Int.neg", split_type --> HOLogic.boolT) $
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   539
                                        (number_of $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   540
                                          (Const (@{const_name HOL.uminus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   541
                                            HOLogic.intT --> HOLogic.intT) $ k'))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   542
        val zero_leq_j              = Const (@{const_name HOL.less_eq},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   543
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   544
        val j_lt_t2                 = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   545
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   546
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   547
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   548
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   549
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   550
        val neg_t2                  = Const ("Int.neg", split_type --> HOLogic.boolT) $ t2'
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   551
        val t2_lt_j                 = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   552
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   553
        val j_leq_zero              = Const (@{const_name HOL.less_eq},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   554
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   555
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   556
        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   557
        val subgoal2                = (map HOLogic.mk_Trueprop [neg_minus_k, zero_leq_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   558
                                        @ hd terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   559
                                        :: (if tl terms2_3 = [] then [not_false] else [])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   560
                                        @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   561
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   562
        val subgoal3                = (map HOLogic.mk_Trueprop [neg_t2, t2_lt_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   563
                                        @ hd terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   564
                                        :: (if tl terms2_3 = [] then [not_false] else [])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   565
                                        @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   566
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   567
        val Ts'                     = split_type :: split_type :: Ts
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   568
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   569
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   570
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   571
    (* "?P ((?n::int) div (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   572
         ((iszero (number_of ?k) --> ?P 0) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   573
          (neg (number_of (uminus ?k)) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   574
            (ALL i. (EX j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j) --> ?P i)) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   575
          (neg (number_of ?k) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   576
            (ALL i. (EX j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j) --> ?P i))) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   577
    | (Const ("Divides.div_class.div",
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   578
        Type ("fun", [Type ("Int.int", []), _])), [t1, t2 as (number_of $ k)]) =>
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   579
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   580
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   581
        val zero                    = Const (@{const_name HOL.zero}, split_type)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   582
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   583
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   584
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   585
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, i)])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   586
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   587
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   588
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   589
        val iszero_t2               = Const ("Int.iszero", split_type --> HOLogic.boolT) $ t2
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   590
        val neg_minus_k             = Const ("Int.neg", split_type --> HOLogic.boolT) $
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   591
                                        (number_of $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   592
                                          (Const (@{const_name HOL.uminus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   593
                                            HOLogic.intT --> HOLogic.intT) $ k'))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   594
        val zero_leq_j              = Const (@{const_name HOL.less_eq},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   595
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   596
        val j_lt_t2                 = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   597
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   598
        val t1_eq_t2_times_i_plus_j = Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   599
                                        split_type --> split_type --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   600
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   601
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   602
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   603
        val neg_t2                  = Const ("Int.neg", split_type --> HOLogic.boolT) $ t2'
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   604
        val t2_lt_j                 = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   605
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   606
        val j_leq_zero              = Const (@{const_name HOL.less_eq},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   607
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   608
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   609
        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   610
        val subgoal2                = (HOLogic.mk_Trueprop neg_minus_k)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   611
                                        :: terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   612
                                        @ not_false
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   613
                                        :: (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   614
                                             [zero_leq_j, j_lt_t2, t1_eq_t2_times_i_plus_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   615
        val subgoal3                = (HOLogic.mk_Trueprop neg_t2)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   616
                                        :: terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   617
                                        @ not_false
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   618
                                        :: (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   619
                                             [t2_lt_j, j_leq_zero, t1_eq_t2_times_i_plus_j])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   620
        val Ts'                     = split_type :: split_type :: Ts
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   621
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   622
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   623
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   624
    (* this will only happen if a split theorem can be applied for which no  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   625
    (* code exists above -- in which case either the split theorem should be *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   626
    (* implemented above, or 'is_split_thm' should be modified to filter it  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   627
    (* out                                                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   628
    | (t, ts) => (
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24328
diff changeset
   629
      warning ("Lin. Arith.: split rule for " ^ Syntax.string_of_term ctxt t ^
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   630
               " (with " ^ string_of_int (length ts) ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   631
               " argument(s)) not implemented; proof reconstruction is likely to fail");
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   632
      NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   633
    ))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   634
  )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   635
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   636
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   637
(* remove terms that do not satisfy 'p'; change the order of the remaining   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   638
(* terms in the same way as filter_prems_tac does                            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   639
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   640
fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   641
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   642
  fun filter_prems (t, (left, right)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   643
    if  p t  then  (left, right @ [t])  else  (left @ right, [])
30190
479806475f3c use long names for old-style fold combinators;
wenzelm
parents: 29850
diff changeset
   644
  val (left, right) = List.foldl filter_prems ([], []) terms
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   645
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   646
  right @ left
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   647
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   648
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   649
(* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a     *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   650
(* subgoal that has 'terms' as premises                                      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   651
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   652
fun negated_term_occurs_positively (terms : term list) : bool =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   653
  List.exists
29528
00fe02648e5d bug fix
nipkow
parents: 29288
diff changeset
   654
    (fn (Trueprop $ (Const ("Not", _) $ t)) => member Pattern.aeconv terms (Trueprop $ t)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   655
      | _                                   => false)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   656
    terms;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   657
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   658
fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   659
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   660
  (* repeatedly split (including newly emerging subgoals) until no further   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   661
  (* splitting is possible                                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   662
  fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   663
    | split_loop (subgoal::subgoals)                = (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   664
        case split_once_items ctxt subgoal of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   665
          SOME new_subgoals => split_loop (new_subgoals @ subgoals)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   666
        | NONE              => subgoal :: split_loop subgoals
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   667
      )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   668
  fun is_relevant t  = isSome (decomp ctxt t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   669
  (* filter_prems_tac is_relevant: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   670
  val relevant_terms = filter_prems_tac_items is_relevant terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   671
  (* split_tac, NNF normalization: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   672
  val split_goals    = split_loop [(Ts, relevant_terms)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   673
  (* necessary because split_once_tac may normalize terms: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   674
  val beta_eta_norm  = map (apsnd (map (Envir.eta_contract o Envir.beta_norm))) split_goals
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   675
  (* TRY (etac notE) THEN eq_assume_tac: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   676
  val result         = List.filter (not o negated_term_occurs_positively o snd) beta_eta_norm
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   677
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   678
  result
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   679
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   680
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   681
(* takes the i-th subgoal  [| A1; ...; An |] ==> B  to                       *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   682
(* An --> ... --> A1 --> B,  performs splitting with the given 'split_thms'  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   683
(* (resulting in a different subgoal P), takes  P  to  ~P ==> False,         *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   684
(* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   685
(* disjunctions and existential quantifiers from the premises, possibly (in  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   686
(* the case of disjunctions) resulting in several new subgoals, each of the  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   687
(* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   688
(* !split_limit splits are possible.                              *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   689
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   690
local
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   691
  val nnf_simpset =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   692
    empty_ss setmkeqTrue mk_eq_True
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   693
    setmksimps (mksimps mksimps_pairs)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   694
    addsimps [imp_conv_disj, iff_conv_conj_imp, de_Morgan_disj, de_Morgan_conj,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   695
      not_all, not_ex, not_not]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   696
  fun prem_nnf_tac i st =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   697
    full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   698
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   699
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   700
fun split_once_tac ctxt split_thms =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   701
  let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   702
    val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   703
    val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   704
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   705
        val Ts = rev (map snd (Logic.strip_params subgoal))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   706
        val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   707
        val cmap = Splitter.cmap_of_split_thms split_thms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   708
        val splits = Splitter.split_posns cmap thy Ts concl
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   709
        val split_limit = Config.get ctxt split_limit
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   710
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   711
        if length splits > split_limit then no_tac
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   712
        else split_tac split_thms i
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   713
      end)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   714
  in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   715
    EVERY' [
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   716
      REPEAT_DETERM o etac rev_mp,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   717
      cond_split_tac,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   718
      rtac ccontr,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   719
      prem_nnf_tac,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   720
      TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   721
    ]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   722
  end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   723
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   724
end;  (* local *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   725
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   726
(* remove irrelevant premises, then split the i-th subgoal (and all new      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   727
(* subgoals) by using 'split_once_tac' repeatedly.  Beta-eta-normalize new   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   728
(* subgoals and finally attempt to solve them by finding an immediate        *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   729
(* contradiction (i.e. a term and its negation) in their premises.           *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   730
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   731
fun pre_tac ctxt i =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   732
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   733
  val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   734
  fun is_relevant t = isSome (decomp ctxt t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   735
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   736
  DETERM (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   737
    TRY (filter_prems_tac is_relevant i)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   738
      THEN (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   739
        (TRY o REPEAT_ALL_NEW (split_once_tac ctxt split_thms))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   740
          THEN_ALL_NEW
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   741
            (CONVERSION Drule.beta_eta_conversion
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   742
              THEN'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   743
            (TRY o (etac notE THEN' eq_assume_tac)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   744
      ) i
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   745
  )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   746
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   747
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   748
end;  (* LA_Data *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   749
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   750
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   751
val pre_tac = LA_Data.pre_tac;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   752
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   753
structure Fast_Arith = Fast_Lin_Arith(structure LA_Logic = LA_Logic and LA_Data = LA_Data);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   754
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   755
val map_data = Fast_Arith.map_data;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   756
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   757
fun map_inj_thms f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   758
  {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = f inj_thms,
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   759
    lessD = lessD, neqE = neqE, simpset = simpset, number_of = number_of};
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   760
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   761
fun map_lessD f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   762
  {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   763
    lessD = f lessD, neqE = neqE, simpset = simpset, number_of = number_of};
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   764
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   765
fun map_simpset f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   766
  {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   767
    lessD = lessD, neqE = neqE, simpset = f simpset, number_of = number_of};
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   768
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   769
fun map_number_of f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset, number_of} =
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   770
  {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   771
    lessD = lessD, neqE = neqE, simpset = simpset, number_of = f number_of};
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   772
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   773
fun add_inj_thms thms = Fast_Arith.map_data (map_inj_thms (append thms));
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   774
fun add_lessD thm = Fast_Arith.map_data (map_lessD (fn thms => thms @ [thm]));
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   775
fun add_simps thms = Fast_Arith.map_data (map_simpset (fn simpset => simpset addsimps thms));
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   776
fun add_simprocs procs = Fast_Arith.map_data (map_simpset (fn simpset => simpset addsimprocs procs));
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   777
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   778
fun set_number_of f = Fast_Arith.map_data (map_number_of (K (serial (), f)))
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   779
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   780
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   781
fun simple_tac ctxt = Fast_Arith.lin_arith_tac ctxt false;
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   782
val lin_arith_tac = Fast_Arith.lin_arith_tac;
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   783
val trace = Fast_Arith.trace;
27017
1e0e8c1adf8c added warning_count for issued reconstruction failure messages;
wenzelm
parents: 26942
diff changeset
   784
val warning_count = Fast_Arith.warning_count;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   785
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   786
(* reduce contradictory <= to False.
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   787
   Most of the work is done by the cancel tactics. *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   788
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   789
val init_arith_data =
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   790
  Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, number_of, ...} =>
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   791
   {add_mono_thms = @{thms add_mono_thms_ordered_semiring} @ @{thms add_mono_thms_ordered_field} @ add_mono_thms,
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   792
    mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} ::
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   793
      @{lemma "a = b ==> c*a = c*b" by (rule arg_cong)} :: mult_mono_thms,
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   794
    inj_thms = inj_thms,
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   795
    lessD = lessD @ [@{thm "Suc_leI"}],
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   796
    neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_ordered_idom}],
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   797
    simpset = HOL_basic_ss
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   798
      addsimps @{thms ring_distribs}
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   799
      addsimps [@{thm if_True}, @{thm if_False}]
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   800
      addsimps
28053
a2106c0d8c45 no parameter prefix for class interpretation
haftmann
parents: 27017
diff changeset
   801
       [@{thm "monoid_add_class.add_0_left"},
a2106c0d8c45 no parameter prefix for class interpretation
haftmann
parents: 27017
diff changeset
   802
        @{thm "monoid_add_class.add_0_right"},
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   803
        @{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   804
        @{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   805
        @{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   806
        @{thm "not_one_less_zero"}]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   807
      addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   808
       (*abel_cancel helps it work in abstract algebraic domains*)
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   809
      addsimprocs Nat_Arith.nat_cancel_sums_add
31510
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   810
      addcongs [if_weak_cong],
e0f2bb4b0021 fast_lin_arith uses proper multiplication instead of unfolding to additions
boehmes
parents: 31101
diff changeset
   811
    number_of = number_of}) #>
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   812
  add_discrete_type @{type_name nat};
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   813
29849
a2baf1b221be new attribute "arith" for facts supplied to arith.
nipkow
parents: 29548
diff changeset
   814
fun add_arith_facts ss =
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30528
diff changeset
   815
  add_prems (Arith_Data.get_arith_facts (MetaSimplifier.the_context ss)) ss;
29849
a2baf1b221be new attribute "arith" for facts supplied to arith.
nipkow
parents: 29548
diff changeset
   816
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   817
val simproc = add_arith_facts #> Fast_Arith.lin_arith_simproc;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   818
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   819
26110
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   820
(* generic refutation procedure *)
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   821
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   822
(* parameters:
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   823
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   824
   test: term -> bool
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   825
   tests if a term is at all relevant to the refutation proof;
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   826
   if not, then it can be discarded. Can improve performance,
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   827
   esp. if disjunctions can be discarded (no case distinction needed!).
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   828
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   829
   prep_tac: int -> tactic
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   830
   A preparation tactic to be applied to the goal once all relevant premises
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   831
   have been moved to the conclusion.
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   832
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   833
   ref_tac: int -> tactic
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   834
   the actual refutation tactic. Should be able to deal with goals
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   835
   [| A1; ...; An |] ==> False
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   836
   where the Ai are atomic, i.e. no top-level &, | or EX
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   837
*)
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   838
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   839
local
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   840
  val nnf_simpset =
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   841
    empty_ss setmkeqTrue mk_eq_True
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   842
    setmksimps (mksimps mksimps_pairs)
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   843
    addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   844
      @{thm de_Morgan_conj}, @{thm not_all}, @{thm not_ex}, @{thm not_not}];
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   845
  fun prem_nnf_tac i st =
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   846
    full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st;
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   847
in
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   848
fun refute_tac test prep_tac ref_tac =
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   849
  let val refute_prems_tac =
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   850
        REPEAT_DETERM
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   851
              (eresolve_tac [@{thm conjE}, @{thm exE}] 1 ORELSE
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   852
               filter_prems_tac test 1 ORELSE
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   853
               etac @{thm disjE} 1) THEN
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   854
        (DETERM (etac @{thm notE} 1 THEN eq_assume_tac 1) ORELSE
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   855
         ref_tac 1);
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   856
  in EVERY'[TRY o filter_prems_tac test,
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   857
            REPEAT_DETERM o etac @{thm rev_mp}, prep_tac, rtac @{thm ccontr}, prem_nnf_tac,
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   858
            SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   859
  end;
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   860
end;
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   861
06eacfd8dd9f moved refute_tac to linarith.ML
haftmann
parents: 26101
diff changeset
   862
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   863
(* arith proof method *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   864
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   865
local
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   866
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   867
fun raw_tac ctxt ex =
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   868
  (* FIXME: K true should be replaced by a sensible test (perhaps "isSome o
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   869
     decomp sg"? -- but note that the test is applied to terms already before
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   870
     they are split/normalized) to speed things up in case there are lots of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   871
     irrelevant terms involved; elimination of min/max can be optimized:
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   872
     (max m n + k <= r) = (m+k <= r & n+k <= r)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   873
     (l <= min m n + k) = (l <= m+k & l <= n+k)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   874
  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   875
  refute_tac (K true)
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   876
    (* Splitting is also done inside simple_tac, but not completely --   *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   877
    (* split_tac may use split theorems that have not been implemented in    *)
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   878
    (* simple_tac (cf. pre_decomp and split_once_items above), and       *)
31082
54a442b2d727 interface changes in linarith.ML
haftmann
parents: 30722
diff changeset
   879
    (* split_limit may trigger.                                   *)
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   880
    (* Therefore splitting outside of simple_tac may allow us to prove   *)
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   881
    (* some goals that simple_tac alone would fail on.                   *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   882
    (REPEAT_DETERM o split_tac (#splits (get_arith_data ctxt)))
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   883
    (lin_arith_tac ctxt ex);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   884
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   885
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   886
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   887
fun gen_tac ex ctxt = FIRST' [simple_tac ctxt,
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   888
  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_tac ctxt ex];
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   889
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   890
val tac = gen_tac true;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   891
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   892
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   893
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   894
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   895
(* context setup *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   896
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   897
val setup =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   898
  init_arith_data #>
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   899
  Simplifier.map_ss (fn ss => ss addsimprocs [Simplifier.simproc (@{theory}) "fast_nat_arith"
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   900
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] (K simproc)]
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   901
    (* Because of fast_nat_arith_simproc, the arithmetic solver is really only
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   902
    useful to detect inconsistencies among the premises for subgoals which are
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   903
    *not* themselves (in)equalities, because the latter activate
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   904
    fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   905
    solver all the time rather than add the additional check. *)
29850
14d9891c917b fix to [arith]
nipkow
parents: 29849
diff changeset
   906
    addSolver (mk_solver' "lin_arith"
31100
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   907
      (add_arith_facts #> Fast_Arith.cut_lin_arith_tac)))
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   908
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   909
val global_setup =
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   910
  setup_split_limit #> setup_neq_limit #>
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   911
  Attrib.setup @{binding arith_split} (Scan.succeed (Thm.declaration_attribute add_split))
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   912
    "declaration of split rules for arithmetic procedure" #>
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   913
  Method.setup @{binding linarith}
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   914
    (Args.bang_facts >> (fn prems => fn ctxt =>
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   915
      METHOD (fn facts =>
6a2e67fe4488 tuned interface of Lin_Arith
haftmann
parents: 31082
diff changeset
   916
        HEADGOAL (Method.insert_tac (prems @ Arith_Data.get_arith_facts ctxt @ facts)
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   917
          THEN' tac ctxt)))) "linear arithmetic" #>
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31100
diff changeset
   918
  Arith_Data.add_tactic "linear arithmetic" gen_tac;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   919
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   920
end;