src/HOL/Tools/lin_arith.ML
author haftmann
Tue, 15 Jan 2008 16:19:23 +0100
changeset 25919 8b1c0d434824
parent 25015 1a84a9ae9d58
child 26061 59de52bec3ec
permissions -rw-r--r--
joined theories IntDef, Numeral, IntArith to theory Int
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
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Tjark Weber and Tobias Nipkow
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     4
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     5
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
     6
*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     7
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     8
signature BASIC_LIN_ARITH =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
     9
sig
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    10
  type arith_tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    11
  val mk_arith_tactic: string -> (Proof.context -> int -> tactic) -> arith_tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    12
  val eq_arith_tactic: arith_tactic * arith_tactic -> bool
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    13
  val arith_split_add: attribute
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    14
  val arith_discrete: string -> Context.generic -> Context.generic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    15
  val arith_inj_const: string * typ -> Context.generic -> Context.generic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    16
  val arith_tactic_add: arith_tactic -> Context.generic -> Context.generic
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
    17
  val fast_arith_split_limit: int Config.T
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
    18
  val fast_arith_neq_limit: int Config.T
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    19
  val lin_arith_pre_tac: Proof.context -> int -> tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    20
  val fast_arith_tac: Proof.context -> int -> tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    21
  val fast_ex_arith_tac: Proof.context -> bool -> int -> tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    22
  val trace_arith: bool ref
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    23
  val lin_arith_simproc: simpset -> term -> thm option
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    24
  val fast_nat_arith_simproc: simproc
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    25
  val simple_arith_tac: Proof.context -> int -> tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    26
  val arith_tac: Proof.context -> int -> tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    27
  val silent_arith_tac: Proof.context -> int -> tactic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    28
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    29
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    30
signature LIN_ARITH =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    31
sig
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    32
  include BASIC_LIN_ARITH
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    33
  val map_data:
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    34
    ({add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    35
      lessD: thm list, neqE: thm list, simpset: Simplifier.simpset} ->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    36
     {add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    37
      lessD: thm list, neqE: thm list, simpset: Simplifier.simpset}) ->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    38
    Context.generic -> Context.generic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    39
  val setup: Context.generic -> Context.generic
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    40
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    41
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    42
structure LinArith: LIN_ARITH =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    43
struct
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    44
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    45
(* Parameters data for general linear arithmetic functor *)
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
structure LA_Logic: LIN_ARITH_LOGIC =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    48
struct
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    49
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    50
val ccontr = ccontr;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    51
val conjI = conjI;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    52
val notI = notI;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    53
val sym = sym;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    54
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
    55
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
    56
val le0 = thm "le0";
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 mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    59
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    60
val mk_Trueprop = HOLogic.mk_Trueprop;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    61
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    62
fun atomize thm = case Thm.prop_of thm of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    63
    Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    64
    atomize(thm RS conjunct1) @ atomize(thm RS conjunct2)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    65
  | _ => [thm];
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    66
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    67
fun neg_prop ((TP as Const("Trueprop",_)) $ (Const("Not",_) $ t)) = TP $ t
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    68
  | neg_prop ((TP as Const("Trueprop",_)) $ t) = TP $ (HOLogic.Not $t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    69
  | neg_prop t = raise TERM ("neg_prop", [t]);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    70
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    71
fun is_False thm =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    72
  let val _ $ t = Thm.prop_of thm
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    73
  in t = Const("False",HOLogic.boolT) end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    74
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    75
fun is_nat(t) = fastype_of1 t = HOLogic.natT;
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
fun mk_nat_thm sg t =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    78
  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    79
  in instantiate ([],[(cn,ct)]) le0 end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    80
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    81
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    82
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    83
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    84
(* arith context data *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    85
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    86
datatype arith_tactic =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    87
  ArithTactic of {name: string, tactic: Proof.context -> int -> tactic, id: stamp};
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
fun mk_arith_tactic name tactic = ArithTactic {name = name, tactic = tactic, id = stamp ()};
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    90
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    91
fun eq_arith_tactic (ArithTactic {id = id1, ...}, ArithTactic {id = id2, ...}) = (id1 = id2);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    92
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    93
structure ArithContextData = GenericDataFun
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    94
(
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    95
  type T = {splits: thm list,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    96
            inj_consts: (string * typ) list,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    97
            discrete: string list,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    98
            tactics: arith_tactic list};
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    99
  val empty = {splits = [], inj_consts = [], discrete = [], tactics = []};
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   100
  val extend = I;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   101
  fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, tactics= tactics1},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   102
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, tactics= tactics2}) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   103
   {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
   104
    inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   105
    discrete = Library.merge (op =) (discrete1, discrete2),
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   106
    tactics = Library.merge eq_arith_tactic (tactics1, tactics2)};
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   107
);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   108
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   109
val get_arith_data = ArithContextData.get o Context.Proof;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   110
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   111
val arith_split_add = Thm.declaration_attribute (fn thm =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   112
  ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   113
    {splits = update Thm.eq_thm_prop thm splits,
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   114
     inj_consts = inj_consts, discrete = discrete, tactics = tactics}));
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
fun arith_discrete d = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   117
  {splits = splits, inj_consts = inj_consts,
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   118
   discrete = update (op =) d discrete, tactics = tactics});
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 arith_inj_const c = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   121
  {splits = splits, inj_consts = update (op =) c inj_consts,
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   122
   discrete = discrete, tactics= tactics});
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   123
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   124
fun arith_tactic_add tac = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   125
  {splits = splits, inj_consts = inj_consts, discrete = discrete,
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   126
   tactics = update eq_arith_tactic tac tactics});
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   127
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   128
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   129
val (fast_arith_split_limit, setup1) = Attrib.config_int "fast_arith_split_limit" 9;
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   130
val (fast_arith_neq_limit, setup2) = Attrib.config_int "fast_arith_neq_limit" 9;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   131
val setup_options = setup1 #> setup2;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   132
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   133
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   134
structure LA_Data_Ref =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   135
struct
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   136
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   137
val fast_arith_neq_limit = fast_arith_neq_limit;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   138
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   139
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   140
(* Decomposition of terms *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   141
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   142
(*internal representation of linear (in-)equations*)
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   143
type decompT =
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   144
  ((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
   145
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   146
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   147
  | nT _                      = false;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   148
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   149
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
   150
             (term * Rat.rat) list * Rat.rat =
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   151
  case AList.lookup (op =) p t of
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   152
      NONE   => ((t, m) :: p, i)
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   153
    | SOME n => (AList.update (op =) (t, Rat.add n m) p, i);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   154
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   155
(* 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
   156
   all their coefficients
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   157
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   158
   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
   159
               (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
   160
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   161
   m: multiplicity associated with the entire product
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   162
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   163
   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
   164
*)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   165
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
   166
let
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   167
  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
   168
      (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
   169
        (* 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
   170
        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
   171
      | _ =>
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   172
        (* 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
   173
        (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
   174
          (SOME s', m') =>
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   175
            (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
   176
              (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
   177
            | (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
   178
        | (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
   179
    | 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
   180
      (* 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
   181
         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
   182
         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
   183
         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
   184
      (* 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
   185
         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
   186
         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
   187
         '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
   188
      (* 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
   189
         denominator can be reduced to a numeric constant.  It might be better
25015
1a84a9ae9d58 typo in comment fixed
webertj
parents: 24920
diff changeset
   190
         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
   191
         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
   192
         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
   193
      (* 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
   194
      (* 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
   195
      (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
   196
        (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
   197
      | (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
   198
    (* 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
   199
    | 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
   200
    | 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
   201
    | 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
   202
    (*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
   203
      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
   204
      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
   205
      although the simplifier should eliminate those anyway ...*)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   206
    | 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
   207
      ((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
   208
        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
   209
    | 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
   210
      ((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
   211
        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
   212
    (* injection constants are ignored *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   213
    | 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
   214
      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
   215
    (* everything else is considered atomic *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   216
    | demult (atom, m) = (SOME atom, m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   217
in demult end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   218
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   219
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
   220
            ((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
   221
let
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   222
  (* 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
   223
     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
   224
     multiplicity 1) *)
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   225
  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
   226
        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
   227
    | 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
   228
        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
   229
    | 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
   230
        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
   231
    | poly (Const (@{const_name HOL.zero}, _), _, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   232
        pi
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   233
    | 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
   234
        (p, Rat.add i m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   235
    | 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
   236
        poly (t, m, (p, Rat.add i m))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   237
    | 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
   238
        (case demult inj_consts (all, m) of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   239
           (NONE,   m') => (p, Rat.add i m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   240
         | (SOME u, m') => add_atom u m' pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   241
    | 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
   242
        (case demult inj_consts (all, m) of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   243
           (NONE,   m') => (p, Rat.add i m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   244
         | (SOME u, m') => add_atom u m' pi)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   245
    | 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
   246
        (let val k = HOLogic.dest_numeral t
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   247
            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
   248
        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
   249
        handle TERM _ => add_atom all m pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   250
    | poly (all as Const f $ x, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   251
        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
   252
    | poly (all, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   253
        add_atom all m pi
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   254
  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
   255
  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
   256
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   257
  case rel of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   258
    @{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
   259
  | @{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
   260
  | "op ="              => SOME (p, i, "=", q, j)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   261
  | _                   => NONE
24328
83afe527504d fixed a bug in demult: -a in (-a * b) is no longer treated as atomic
webertj
parents: 24271
diff changeset
   262
end handle Rat.DIVZERO => NONE;
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   263
24271
499608101177 avoid low-level tsig;
wenzelm
parents: 24196
diff changeset
   264
fun of_lin_arith_sort thy U =
499608101177 avoid low-level tsig;
wenzelm
parents: 24196
diff changeset
   265
  Sign.of_sort thy (U, ["Ring_and_Field.ordered_idom"]);
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   266
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   267
fun allows_lin_arith sg (discrete : string list) (U as Type (D, [])) : bool * bool =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   268
  if of_lin_arith_sort sg U then
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   269
    (true, D mem discrete)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   270
  else (* special cases *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   271
    if D mem discrete then  (true, true)  else  (false, false)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   272
  | allows_lin_arith sg discrete U =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   273
  (of_lin_arith_sort sg U, false);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   274
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   275
fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decompT option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   276
  case T of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   277
    Type ("fun", [U, _]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   278
      (case allows_lin_arith thy discrete U of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   279
        (true, d) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   280
          (case decomp0 inj_consts xxx of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   281
            NONE                   => NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   282
          | 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
   283
      | (false, _) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   284
          NONE)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   285
  | _ => NONE;
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
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
   288
  | negate NONE                        = NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   289
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   290
fun decomp_negation data
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   291
  ((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decompT option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   292
      decomp_typecheck data (T, (rel, lhs, rhs))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   293
  | decomp_negation data ((Const ("Trueprop", _)) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   294
  (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   295
      negate (decomp_typecheck data (T, (rel, lhs, rhs)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   296
  | decomp_negation data _ =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   297
      NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   298
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   299
fun decomp ctxt : term -> decompT option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   300
  let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   301
    val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   302
    val {discrete, inj_consts, ...} = get_arith_data ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   303
  in decomp_negation (thy, discrete, inj_consts) end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   304
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   305
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _))                      = nT T
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   306
  | 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
   307
  | domain_is_nat _                                                 = false;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   308
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   309
fun number_of (n, T) = HOLogic.mk_number T n;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   310
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   311
(*---------------------------------------------------------------------------*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   312
(* 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
   313
(* 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
   314
(* to the proof state                                                        *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   315
(*---------------------------------------------------------------------------*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   316
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   317
(* checks if splitting with 'thm' is implemented                             *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   318
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   319
fun is_split_thm (thm : thm) : bool =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   320
  case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   321
    (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   322
    case head_of lhs of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   323
      Const (a, _) => member (op =) [@{const_name Orderings.max},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   324
                                    @{const_name Orderings.min},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   325
                                    @{const_name HOL.abs},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   326
                                    @{const_name HOL.minus},
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   327
                                    "Int.nat",
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   328
                                    "Divides.div_class.mod",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   329
                                    "Divides.div_class.div"] a
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   330
    | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   331
                                 Display.string_of_thm thm);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   332
                       false))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   333
  | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   334
                   Display.string_of_thm thm);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   335
          false);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   336
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   337
(* 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
   338
(* variables as needed when substituting inside an abstraction               *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   339
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   340
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
   341
  | subst_term pairs                     t          =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   342
      (case AList.lookup (op aconv) pairs t of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   343
        SOME new =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   344
          new
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   345
      | NONE     =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   346
          (case t of Abs (a, T, body) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   347
            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
   348
            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
   349
          | t1 $ t2                   =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   350
            subst_term pairs t1 $ subst_term pairs t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   351
          | _ => t));
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   352
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   353
(* 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
   354
(* 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
   355
(* 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
   356
(* 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
   357
(* on the subgoal                                                            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   358
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   359
(* 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
   360
(*        (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
   361
(*        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
   362
(*        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
   363
(*        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
   364
(*        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
   365
(*        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
   366
(*        proof-producing).                                                  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   367
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   368
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
   369
                     (typ list * term list) list option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   370
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   371
  val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   372
  (* takes a list  [t1, ..., tn]  to the term                                *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   373
  (*   tn' --> ... --> t1' --> False  ,                                      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   374
  (* where ti' = HOLogic.dest_Trueprop ti                                    *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   375
  fun REPEAT_DETERM_etac_rev_mp terms' =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   376
    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
   377
  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
   378
  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
   379
  val splits     = Splitter.split_posns cmap thy Ts (REPEAT_DETERM_etac_rev_mp terms)
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   380
  val split_limit = Config.get ctxt fast_arith_split_limit
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   381
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   382
  if length splits > split_limit then
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   383
   (tracing ("fast_arith_split_limit exceeded (current value is " ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   384
      string_of_int split_limit ^ ")"); NONE)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   385
  else (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   386
  case splits of [] =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   387
    (* split_tac would fail: no possible split *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   388
    NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   389
  | ((_, _, _, split_type, split_term) :: _) => (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   390
    (* ignore all but the first possible split *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   391
    case strip_comb split_term of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   392
    (* ?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
   393
      (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
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, t2)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   398
        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
   399
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   400
        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
   401
        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
   402
        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
   403
        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
   404
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   405
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   406
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   407
    (* ?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
   408
    | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   409
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   410
        val rev_terms     = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   411
        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
   412
        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
   413
        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
   414
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   415
        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
   416
        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
   417
        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
   418
        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
   419
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   420
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   421
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   422
    (* ?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
   423
    | (Const (@{const_name HOL.abs}, _), [t1]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   424
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   425
        val rev_terms   = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   426
        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
   427
        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
   428
                            split_type --> split_type) $ t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   429
        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
   430
        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
   431
                            split_type --> split_type --> HOLogic.boolT) $ zero $ t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   432
        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
   433
                            split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   434
        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
   435
        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
   436
        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
   437
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   438
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   439
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   440
    (* ?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
   441
    | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   442
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   443
        (* "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
   444
        (* transformation, therefore some adjustment of indices is necessary *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   445
        val rev_terms       = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   446
        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
   447
        val d               = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   448
        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
   449
        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
   450
                                (map (incr_boundvars 1) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   451
        val t1'             = incr_boundvars 1 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   452
        val t2'             = incr_boundvars 1 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   453
        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
   454
                                split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   455
        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
   456
                                (Const (@{const_name HOL.plus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   457
                                  split_type --> split_type --> split_type) $ t2' $ d)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   458
        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
   459
        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
   460
        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
   461
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   462
        SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   463
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   464
    (* ?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
   465
    | (Const ("Int.nat", _), [t1]) =>
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   466
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   467
        val rev_terms   = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   468
        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
   469
        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
   470
        val n           = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   471
        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
   472
                            (map (incr_boundvars 1) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   473
        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
   474
        val t1'         = incr_boundvars 1 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   475
        val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
24196
f1dbfd7e3223 localized of_nat
haftmann
parents: 24112
diff changeset
   476
                            (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
   477
        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
   478
                            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
   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 t1_eq_int_n) :: terms1 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   481
        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
   482
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   483
        SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   484
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   485
    (* "?P ((?n::nat) mod (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   486
         ((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
   487
           (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
   488
    | (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
   489
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   490
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   491
        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
   492
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   493
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   494
        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
   495
        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
   496
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   497
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   498
        val t2'                     = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   499
        val t2_eq_zero              = Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   500
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   501
        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
   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 j_lt_t2                 = Const (@{const_name HOL.less},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   504
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   505
        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
   506
                                       (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
   507
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   508
                                           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
   509
        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
   510
        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
   511
        val subgoal2                = (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   512
                                        [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
   513
                                          @ terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   514
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   515
        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
   516
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   517
    (* "?P ((?n::nat) div (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   518
         ((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
   519
           (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
   520
    | (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
   521
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   522
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   523
        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
   524
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   525
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   526
        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
   527
        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
   528
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   529
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   530
        val t2'                     = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   531
        val t2_eq_zero              = Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   532
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   533
        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
   534
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   535
        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
   536
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   537
        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
   538
                                       (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
   539
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   540
                                           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
   541
        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
   542
        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
   543
        val subgoal2                = (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   544
                                        [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
   545
                                          @ terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   546
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   547
        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
   548
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   549
    (* "?P ((?n::int) mod (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   550
         ((iszero (number_of ?k) --> ?P ?n) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   551
          (neg (number_of (uminus ?k)) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   552
            (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
   553
          (neg (number_of ?k) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   554
            (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
   555
    | (Const ("Divides.div_class.mod",
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   556
        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
   557
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   558
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   559
        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
   560
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   561
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   562
        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
   563
        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
   564
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   565
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   566
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   567
        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
   568
        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
   569
                                        (number_of $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   570
                                          (Const (@{const_name HOL.uminus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   571
                                            HOLogic.intT --> HOLogic.intT) $ k'))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   572
        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
   573
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   574
        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
   575
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   576
        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
   577
                                       (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
   578
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   579
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   580
        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
   581
        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
   582
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   583
        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
   584
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   585
        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
   586
        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
   587
        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
   588
                                        @ hd terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   589
                                        :: (if tl terms2_3 = [] then [not_false] else [])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   590
                                        @ (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
   591
                                        @ (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
   592
        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
   593
                                        @ hd terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   594
                                        :: (if tl terms2_3 = [] then [not_false] else [])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   595
                                        @ (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
   596
                                        @ (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
   597
        val Ts'                     = split_type :: split_type :: Ts
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   598
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   599
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   600
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   601
    (* "?P ((?n::int) div (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   602
         ((iszero (number_of ?k) --> ?P 0) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   603
          (neg (number_of (uminus ?k)) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   604
            (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
   605
          (neg (number_of ?k) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   606
            (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
   607
    | (Const ("Divides.div_class.div",
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   608
        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
   609
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   610
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   611
        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
   612
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   613
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   614
        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
   615
        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
   616
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   617
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   618
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   619
        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
   620
        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
   621
                                        (number_of $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   622
                                          (Const (@{const_name HOL.uminus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   623
                                            HOLogic.intT --> HOLogic.intT) $ k'))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   624
        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
   625
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   626
        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
   627
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   628
        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
   629
                                        split_type --> split_type --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   630
                                       (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
   631
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   632
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25015
diff changeset
   633
        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
   634
        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
   635
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   636
        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
   637
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   638
        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
   639
        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
   640
        val subgoal2                = (HOLogic.mk_Trueprop neg_minus_k)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   641
                                        :: terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   642
                                        @ not_false
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   643
                                        :: (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   644
                                             [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
   645
        val subgoal3                = (HOLogic.mk_Trueprop neg_t2)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   646
                                        :: terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   647
                                        @ not_false
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   648
                                        :: (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   649
                                             [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
   650
        val Ts'                     = split_type :: split_type :: Ts
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   651
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   652
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   653
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   654
    (* 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
   655
    (* 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
   656
    (* 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
   657
    (* out                                                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   658
    | (t, ts) => (
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24328
diff changeset
   659
      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
   660
               " (with " ^ string_of_int (length ts) ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   661
               " 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
   662
      NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   663
    ))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   664
  )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   665
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   666
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   667
(* 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
   668
(* 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
   669
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   670
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
   671
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   672
  fun filter_prems (t, (left, right)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   673
    if  p t  then  (left, right @ [t])  else  (left @ right, [])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   674
  val (left, right) = foldl filter_prems ([], []) terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   675
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   676
  right @ left
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   677
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   678
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   679
(* 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
   680
(* subgoal that has 'terms' as premises                                      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   681
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   682
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
   683
  List.exists
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   684
    (fn (Trueprop $ (Const ("Not", _) $ t)) => member (op aconv) terms (Trueprop $ t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   685
      | _                                   => false)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   686
    terms;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   687
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   688
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
   689
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   690
  (* 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
   691
  (* splitting is possible                                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   692
  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
   693
    | split_loop (subgoal::subgoals)                = (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   694
        case split_once_items ctxt subgoal of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   695
          SOME new_subgoals => split_loop (new_subgoals @ subgoals)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   696
        | NONE              => subgoal :: split_loop subgoals
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   697
      )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   698
  fun is_relevant t  = isSome (decomp ctxt t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   699
  (* filter_prems_tac is_relevant: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   700
  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
   701
  (* split_tac, NNF normalization: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   702
  val split_goals    = split_loop [(Ts, relevant_terms)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   703
  (* necessary because split_once_tac may normalize terms: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   704
  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
   705
  (* TRY (etac notE) THEN eq_assume_tac: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   706
  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
   707
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   708
  result
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   709
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   710
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   711
(* 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
   712
(* 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
   713
(* (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
   714
(* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   715
(* 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
   716
(* 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
   717
(* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   718
(* !fast_arith_split_limit splits are possible.                              *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   719
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   720
local
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   721
  val nnf_simpset =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   722
    empty_ss setmkeqTrue mk_eq_True
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   723
    setmksimps (mksimps mksimps_pairs)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   724
    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
   725
      not_all, not_ex, not_not]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   726
  fun prem_nnf_tac i st =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   727
    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
   728
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   729
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   730
fun split_once_tac ctxt split_thms =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   731
  let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   732
    val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   733
    val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   734
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   735
        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
   736
        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
   737
        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
   738
        val splits = Splitter.split_posns cmap thy Ts concl
24112
6c4e7d17f9b0 simplified internal Config interface;
wenzelm
parents: 24092
diff changeset
   739
        val split_limit = Config.get ctxt fast_arith_split_limit
24092
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   740
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   741
        if length splits > split_limit then no_tac
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   742
        else split_tac split_thms i
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   743
      end)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   744
  in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   745
    EVERY' [
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   746
      REPEAT_DETERM o etac rev_mp,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   747
      cond_split_tac,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   748
      rtac ccontr,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   749
      prem_nnf_tac,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   750
      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
   751
    ]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   752
  end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   753
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   754
end;  (* local *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   755
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   756
(* 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
   757
(* 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
   758
(* 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
   759
(* 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
   760
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   761
fun pre_tac ctxt i =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   762
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   763
  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
   764
  fun is_relevant t = isSome (decomp ctxt t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   765
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   766
  DETERM (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   767
    TRY (filter_prems_tac is_relevant i)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   768
      THEN (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   769
        (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
   770
          THEN_ALL_NEW
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   771
            (CONVERSION Drule.beta_eta_conversion
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   772
              THEN'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   773
            (TRY o (etac notE THEN' eq_assume_tac)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   774
      ) i
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   775
  )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   776
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   777
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   778
end;  (* LA_Data_Ref *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   779
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   780
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   781
val lin_arith_pre_tac = LA_Data_Ref.pre_tac;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   782
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   783
structure Fast_Arith =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   784
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
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
val map_data = Fast_Arith.map_data;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   787
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   788
fun fast_arith_tac ctxt    = Fast_Arith.lin_arith_tac ctxt false;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   789
val fast_ex_arith_tac      = Fast_Arith.lin_arith_tac;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   790
val trace_arith            = Fast_Arith.trace;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   791
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   792
(* reduce contradictory <= to False.
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   793
   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
   794
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   795
val init_arith_data =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   796
 Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   797
   {add_mono_thms = add_mono_thms @
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   798
    @{thms add_mono_thms_ordered_semiring} @ @{thms add_mono_thms_ordered_field},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   799
    mult_mono_thms = mult_mono_thms,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   800
    inj_thms = inj_thms,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   801
    lessD = lessD @ [thm "Suc_leI"],
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   802
    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
   803
    simpset = HOL_basic_ss
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   804
      addsimps
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   805
       [@{thm "monoid_add_class.zero_plus.add_0_left"},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   806
        @{thm "monoid_add_class.zero_plus.add_0_right"},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   807
        @{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
   808
        @{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
   809
        @{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
   810
        @{thm "not_one_less_zero"}]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   811
      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
   812
       (*abel_cancel helps it work in abstract algebraic domains*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   813
      addsimprocs nat_cancel_sums_add}) #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   814
  arith_discrete "nat";
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   815
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   816
val lin_arith_simproc = Fast_Arith.lin_arith_simproc;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   817
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   818
val fast_nat_arith_simproc =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   819
  Simplifier.simproc (the_context ()) "fast_nat_arith"
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   820
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] (K Fast_Arith.lin_arith_simproc);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   821
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   822
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   823
useful to detect inconsistencies among the premises for subgoals which are
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   824
*not* themselves (in)equalities, because the latter activate
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   825
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   826
solver all the time rather than add the additional check. *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   827
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   828
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   829
(* arith proof method *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   830
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   831
local
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   832
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   833
fun raw_arith_tac ctxt ex =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   834
  (* 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
   835
     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
   836
     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
   837
     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
   838
     (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
   839
     (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
   840
  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   841
  refute_tac (K true)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   842
    (* Splitting is also done inside fast_arith_tac, but not completely --   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   843
    (* split_tac may use split theorems that have not been implemented in    *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   844
    (* fast_arith_tac (cf. pre_decomp and split_once_items above), and       *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   845
    (* fast_arith_split_limit may trigger.                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   846
    (* Therefore splitting outside of fast_arith_tac may allow us to prove   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   847
    (* some goals that fast_arith_tac alone would fail on.                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   848
    (REPEAT_DETERM o split_tac (#splits (get_arith_data ctxt)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   849
    (fast_ex_arith_tac ctxt ex);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   850
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   851
fun more_arith_tacs ctxt =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   852
  let val tactics = #tactics (get_arith_data ctxt)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   853
  in FIRST' (map (fn ArithTactic {tactic, ...} => tactic ctxt) tactics) end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   854
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   855
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   856
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   857
fun simple_arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   858
  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt true];
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   859
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   860
fun arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   861
  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt true,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   862
  more_arith_tacs ctxt];
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   863
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   864
fun silent_arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   865
  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt false,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   866
  more_arith_tacs ctxt];
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   867
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   868
fun arith_method src =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   869
  Method.syntax Args.bang_facts src
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   870
  #> (fn (prems, ctxt) => Method.METHOD (fn facts =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   871
      HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac ctxt)));
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   872
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   873
end;
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
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   876
(* context setup *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   877
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   878
val setup =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   879
  init_arith_data #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   880
  Simplifier.map_ss (fn ss => ss addsimprocs [fast_nat_arith_simproc]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   881
    addSolver (mk_solver' "lin_arith" Fast_Arith.cut_lin_arith_tac)) #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   882
  Context.mapping
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   883
   (setup_options #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   884
    Method.add_methods
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   885
      [("arith", arith_method, "decide linear arithmethic")] #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   886
    Attrib.add_attributes [("arith_split", Attrib.no_args arith_split_add,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   887
      "declaration of split rules for arithmetic procedure")]) I;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   888
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   889
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   890
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   891
structure BasicLinArith: BASIC_LIN_ARITH = LinArith;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   892
open BasicLinArith;