src/HOL/Tools/lin_arith.ML
author wenzelm
Tue, 31 Jul 2007 19:40:23 +0200
changeset 24092 71c27b320610
child 24112 6c4e7d17f9b0
permissions -rw-r--r--
HOL setup for linear arithmetic -- moved here from arith_data.ML;
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
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    17
  val fast_arith_split_limit: int ConfigOption.T
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
    18
  val fast_arith_neq_limit: int ConfigOption.T
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} =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   113
    {splits = insert Thm.eq_thm_prop thm splits,
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,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   118
   discrete = insert (op =) d discrete, tactics = tactics});
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} =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   121
  {splits = splits, inj_consts = insert (op =) c inj_consts,
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,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   126
   tactics = insert eq_arith_tactic tac tactics});
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
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   129
val (fast_arith_split_limit, setup1) = ConfigOption.int "fast_arith_split_limit" 9;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   130
val (fast_arith_neq_limit, setup2) = ConfigOption.int "fast_arith_neq_limit" 9;
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*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   143
type decompT = ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   144
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   145
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   146
  | nT _                      = false;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   147
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   148
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
   149
             (term * Rat.rat) list * Rat.rat =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   150
  case AList.lookup (op =) p t of NONE   => ((t, m) :: p, i)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   151
                                | SOME n => (AList.update (op =) (t, Rat.add n m) p, i);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   152
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   153
exception Zero;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   154
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   155
fun rat_of_term (numt, dent) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   156
  let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   157
    val num = HOLogic.dest_numeral numt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   158
    val den = HOLogic.dest_numeral dent
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   159
  in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   160
    if den = 0 then raise Zero else Rat.rat_of_quotient (num, den)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   161
  end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   162
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   163
(*Warning: in rare cases number_of encloses a non-numeral,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   164
  in which case dest_numeral raises TERM; hence all the handles below.
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   165
  Same for Suc-terms that turn out not to be numerals -
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   166
  although the simplifier should eliminate those anyway ...*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   167
fun number_of_Sucs (Const ("Suc", _) $ n) : int =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   168
      number_of_Sucs n + 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   169
  | number_of_Sucs t =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   170
      if HOLogic.is_zero t then 0 else raise TERM ("number_of_Sucs", []);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   171
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   172
(*decompose nested multiplications, bracketing them to the right and combining
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   173
  all their coefficients*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   174
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
   175
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   176
  fun demult ((mC as Const (@{const_name HOL.times}, _)) $ s $ t, m) = (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   177
    (case s of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   178
      Const ("Numeral.number_class.number_of", _) $ n =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   179
        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   180
    | Const (@{const_name HOL.uminus}, _) $ (Const ("Numeral.number_class.number_of", _) $ n) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   181
        demult (t, Rat.mult m (Rat.rat_of_int (~(HOLogic.dest_numeral n))))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   182
    | Const (@{const_name Suc}, _) $ _ =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   183
        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat s)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   184
    | Const (@{const_name HOL.times}, _) $ s1 $ s2 =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   185
        demult (mC $ s1 $ (mC $ s2 $ t), m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   186
    | Const (@{const_name HOL.divide}, _) $ numt $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   187
          (Const ("Numeral.number_class.number_of", _) $ dent) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   188
        let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   189
          val den = HOLogic.dest_numeral dent
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   190
        in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   191
          if den = 0 then
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   192
            raise Zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   193
          else
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   194
            demult (mC $ numt $ t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   195
        end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   196
    | _ =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   197
        atomult (mC, s, t, m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   198
    ) handle TERM _ => atomult (mC, s, t, m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   199
  )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   200
    | demult (atom as Const(@{const_name HOL.divide}, _) $ t $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   201
        (Const ("Numeral.number_class.number_of", _) $ dent), m) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   202
      (let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   203
        val den = HOLogic.dest_numeral dent
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   204
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   205
        if den = 0 then
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   206
          raise Zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   207
        else
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   208
          demult (t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   209
      end handle TERM _ => (SOME atom, m))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   210
    | 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
   211
    | demult (Const (@{const_name HOL.one}, _), m) = (NONE, m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   212
    | demult (t as Const ("Numeral.number_class.number_of", _) $ n, m) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   213
        ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   214
          handle TERM _ => (SOME t, m))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   215
    | demult (Const (@{const_name HOL.uminus}, _) $ t, m) = demult (t, Rat.neg m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   216
    | demult (t as Const f $ x, m) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   217
        (if member (op =) inj_consts f then SOME x else SOME t, m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   218
    | demult (atom, m) = (SOME atom, m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   219
and
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   220
  atomult (mC, atom, t, m) = (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   221
    case demult (t, m) of (NONE, m')    => (SOME atom, m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   222
                        | (SOME t', m') => (SOME (mC $ atom $ t'), m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   223
  )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   224
in demult end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   225
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   226
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
   227
            ((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
   228
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   229
  (* Turn term into list of summand * multiplicity plus a constant *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   230
  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
   231
        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
   232
    | 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
   233
        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
   234
    | 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
   235
        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
   236
    | poly (Const (@{const_name HOL.zero}, _), _, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   237
        pi
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   238
    | 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
   239
        (p, Rat.add i m)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   240
    | 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
   241
        poly (t, m, (p, Rat.add i m))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   242
    | 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
   243
        (case demult inj_consts (all, m) of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   244
           (NONE,   m') => (p, Rat.add i m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   245
         | (SOME u, m') => add_atom u m' pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   246
    | 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
   247
        (case demult inj_consts (all, m) of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   248
           (NONE,   m') => (p, Rat.add i m')
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   249
         | (SOME u, m') => add_atom u m' pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   250
    | poly (all as Const ("Numeral.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   251
        (let val k = HOLogic.dest_numeral t
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   252
            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
   253
        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
   254
        handle TERM _ => add_atom all m pi)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   255
    | poly (all as Const f $ x, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   256
        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
   257
    | poly (all, m, pi) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   258
        add_atom all m pi
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   259
  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
   260
  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
   261
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   262
  case rel of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   263
    @{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
   264
  | @{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
   265
  | "op ="              => SOME (p, i, "=", q, j)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   266
  | _                   => NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   267
end handle Zero => NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   268
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   269
fun of_lin_arith_sort sg (U : typ) : bool =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   270
  Type.of_sort (Sign.tsig_of sg) (U, ["Ring_and_Field.ordered_idom"])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   271
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   272
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
   273
  if of_lin_arith_sort sg U then
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   274
    (true, D mem discrete)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   275
  else (* special cases *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   276
    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
   277
  | allows_lin_arith sg discrete U =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   278
  (of_lin_arith_sort sg U, false);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   279
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   280
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
   281
  case T of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   282
    Type ("fun", [U, _]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   283
      (case allows_lin_arith thy discrete U of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   284
        (true, d) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   285
          (case decomp0 inj_consts xxx of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   286
            NONE                   => NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   287
          | 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
   288
      | (false, _) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   289
          NONE)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   290
  | _ => NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   291
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   292
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
   293
  | negate NONE                        = NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   294
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   295
fun decomp_negation data
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   296
  ((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
   297
      decomp_typecheck data (T, (rel, lhs, rhs))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   298
  | decomp_negation data ((Const ("Trueprop", _)) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   299
  (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   300
      negate (decomp_typecheck data (T, (rel, lhs, rhs)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   301
  | decomp_negation data _ =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   302
      NONE;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   303
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   304
fun decomp ctxt : term -> decompT option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   305
  let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   306
    val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   307
    val {discrete, inj_consts, ...} = get_arith_data ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   308
  in decomp_negation (thy, discrete, inj_consts) end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   309
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   310
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _))                      = nT T
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   311
  | 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
   312
  | domain_is_nat _                                                 = false;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   313
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   314
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
   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
(* 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
   318
(* 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
   319
(* to the proof state                                                        *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   320
(*---------------------------------------------------------------------------*)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   321
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   322
(* checks if splitting with 'thm' is implemented                             *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   323
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   324
fun is_split_thm (thm : thm) : bool =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   325
  case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   326
    (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   327
    case head_of lhs of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   328
      Const (a, _) => member (op =) [@{const_name Orderings.max},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   329
                                    @{const_name Orderings.min},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   330
                                    @{const_name HOL.abs},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   331
                                    @{const_name HOL.minus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   332
                                    "IntDef.nat",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   333
                                    "Divides.div_class.mod",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   334
                                    "Divides.div_class.div"] a
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   335
    | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   336
                                 Display.string_of_thm thm);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   337
                       false))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   338
  | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   339
                   Display.string_of_thm thm);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   340
          false);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   341
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   342
(* 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
   343
(* variables as needed when substituting inside an abstraction               *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   344
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   345
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
   346
  | subst_term pairs                     t          =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   347
      (case AList.lookup (op aconv) pairs t of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   348
        SOME new =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   349
          new
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   350
      | NONE     =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   351
          (case t of Abs (a, T, body) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   352
            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
   353
            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
   354
          | t1 $ t2                   =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   355
            subst_term pairs t1 $ subst_term pairs t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   356
          | _ => t));
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   357
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   358
(* 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
   359
(* 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
   360
(* 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
   361
(* 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
   362
(* on the subgoal                                                            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   363
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   364
(* 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
   365
(*        (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
   366
(*        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
   367
(*        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
   368
(*        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
   369
(*        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
   370
(*        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
   371
(*        proof-producing).                                                  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   372
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   373
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
   374
                     (typ list * term list) list option =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   375
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   376
  val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   377
  (* takes a list  [t1, ..., tn]  to the term                                *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   378
  (*   tn' --> ... --> t1' --> False  ,                                      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   379
  (* where ti' = HOLogic.dest_Trueprop ti                                    *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   380
  fun REPEAT_DETERM_etac_rev_mp terms' =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   381
    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
   382
  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
   383
  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
   384
  val splits     = Splitter.split_posns cmap thy Ts (REPEAT_DETERM_etac_rev_mp terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   385
  val split_limit = ConfigOption.get ctxt fast_arith_split_limit
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   386
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   387
  if length splits > split_limit then
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   388
   (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
   389
      string_of_int split_limit ^ ")"); NONE)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   390
  else (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   391
  case splits of [] =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   392
    (* split_tac would fail: no possible split *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   393
    NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   394
  | ((_, _, _, split_type, split_term) :: _) => (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   395
    (* ignore all but the first possible split *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   396
    case strip_comb split_term of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   397
    (* ?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
   398
      (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   399
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   400
        val rev_terms     = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   401
        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
   402
        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
   403
        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
   404
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   405
        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
   406
        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
   407
        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
   408
        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
   409
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   410
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   411
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   412
    (* ?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
   413
    | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   414
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   415
        val rev_terms     = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   416
        val 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
   417
        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
   418
        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
   419
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   420
        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
   421
        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
   422
        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
   423
        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
   424
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   425
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   426
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   427
    (* ?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
   428
    | (Const (@{const_name HOL.abs}, _), [t1]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   429
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   430
        val rev_terms   = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   431
        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
   432
        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
   433
                            split_type --> split_type) $ t1)]) rev_terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   434
        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
   435
        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
   436
                            split_type --> split_type --> HOLogic.boolT) $ zero $ t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   437
        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
   438
                            split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   439
        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
   440
        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
   441
        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
   442
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   443
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   444
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   445
    (* ?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
   446
    | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   447
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   448
        (* "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
   449
        (* transformation, therefore some adjustment of indices is necessary *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   450
        val rev_terms       = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   451
        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
   452
        val d               = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   453
        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
   454
        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
   455
                                (map (incr_boundvars 1) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   456
        val t1'             = incr_boundvars 1 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   457
        val t2'             = incr_boundvars 1 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   458
        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
   459
                                split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   460
        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
   461
                                (Const (@{const_name HOL.plus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   462
                                  split_type --> split_type --> split_type) $ t2' $ d)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   463
        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
   464
        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
   465
        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
   466
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   467
        SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   468
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   469
    (* ?P (nat ?i) = ((ALL n. ?i = int n --> ?P n) & (?i < 0 --> ?P 0)) *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   470
    | (Const ("IntDef.nat", _), [t1]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   471
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   472
        val rev_terms   = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   473
        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
   474
        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
   475
        val n           = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   476
        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
   477
                            (map (incr_boundvars 1) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   478
        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
   479
        val t1'         = incr_boundvars 1 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   480
        val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   481
                            (Const ("Nat.of_nat", HOLogic.natT --> HOLogic.intT) $ n)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   482
        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
   483
                            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
   484
        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
   485
        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
   486
        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
   487
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   488
        SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   489
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   490
    (* "?P ((?n::nat) mod (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   491
         ((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
   492
           (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
   493
    | (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
   494
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   495
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   496
        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
   497
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   498
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   499
        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
   500
        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
   501
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   502
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   503
        val t2'                     = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   504
        val t2_eq_zero              = Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   505
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   506
        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
   507
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   508
        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
   509
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   510
        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
   511
                                       (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
   512
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   513
                                           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
   514
        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
   515
        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
   516
        val subgoal2                = (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   517
                                        [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
   518
                                          @ terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   519
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   520
        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
   521
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   522
    (* "?P ((?n::nat) div (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   523
         ((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
   524
           (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
   525
    | (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
   526
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   527
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   528
        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
   529
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   530
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   531
        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
   532
        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
   533
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   534
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   535
        val t2'                     = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   536
        val t2_eq_zero              = Const ("op =",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   537
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   538
        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
   539
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   540
        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
   541
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   542
        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
   543
                                       (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
   544
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   545
                                           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
   546
        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
   547
        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
   548
        val subgoal2                = (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   549
                                        [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
   550
                                          @ terms2 @ [not_false]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   551
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   552
        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
   553
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   554
    (* "?P ((?n::int) mod (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   555
         ((iszero (number_of ?k) --> ?P ?n) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   556
          (neg (number_of (uminus ?k)) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   557
            (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
   558
          (neg (number_of ?k) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   559
            (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
   560
    | (Const ("Divides.div_class.mod",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   561
        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   562
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   563
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   564
        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
   565
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   566
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   567
        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
   568
        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
   569
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   570
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   571
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   572
        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   573
        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   574
                                        (number_of $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   575
                                          (Const (@{const_name HOL.uminus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   576
                                            HOLogic.intT --> HOLogic.intT) $ k'))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   577
        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
   578
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   579
        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
   580
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   581
        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
   582
                                       (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
   583
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   584
                                           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
   585
        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   586
        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
   587
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   588
        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
   589
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   590
        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
   591
        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
   592
        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
   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_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
   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 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
   598
                                        @ hd terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   599
                                        :: (if tl terms2_3 = [] then [not_false] else [])
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   600
                                        @ (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
   601
                                        @ (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
   602
        val Ts'                     = split_type :: split_type :: Ts
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   603
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   604
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   605
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   606
    (* "?P ((?n::int) div (number_of ?k)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   607
         ((iszero (number_of ?k) --> ?P 0) &
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   608
          (neg (number_of (uminus ?k)) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   609
            (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
   610
          (neg (number_of ?k) -->
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   611
            (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
   612
    | (Const ("Divides.div_class.div",
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   613
        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   614
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   615
        val rev_terms               = rev terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   616
        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
   617
        val i                       = Bound 1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   618
        val j                       = Bound 0
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   619
        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
   620
        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
   621
                                        (map (incr_boundvars 2) rev_terms)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   622
        val t1'                     = incr_boundvars 2 t1
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   623
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   624
        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   625
        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   626
                                        (number_of $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   627
                                          (Const (@{const_name HOL.uminus},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   628
                                            HOLogic.intT --> HOLogic.intT) $ k'))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   629
        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
   630
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   631
        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
   632
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   633
        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
   634
                                        split_type --> split_type --> HOLogic.boolT) $ t1' $
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   635
                                       (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
   636
                                         (Const (@{const_name HOL.times},
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   637
                                           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
   638
        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   639
        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
   640
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   641
        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
   642
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   643
        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
   644
        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
   645
        val subgoal2                = (HOLogic.mk_Trueprop neg_minus_k)
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
                                             [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
   650
        val subgoal3                = (HOLogic.mk_Trueprop neg_t2)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   651
                                        :: terms2_3
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   652
                                        @ not_false
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   653
                                        :: (map HOLogic.mk_Trueprop
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   654
                                             [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
   655
        val Ts'                     = split_type :: split_type :: Ts
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   656
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   657
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   658
      end
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   659
    (* 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
   660
    (* 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
   661
    (* 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
   662
    (* out                                                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   663
    | (t, ts) => (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   664
      warning ("Lin. Arith.: split rule for " ^ ProofContext.string_of_term ctxt t ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   665
               " (with " ^ string_of_int (length ts) ^
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   666
               " 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
   667
      NONE
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   668
    ))
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
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   671
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   672
(* 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
   673
(* 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
   674
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   675
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
   676
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   677
  fun filter_prems (t, (left, right)) =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   678
    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
   679
  val (left, right) = foldl filter_prems ([], []) terms
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   680
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   681
  right @ left
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   682
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   683
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   684
(* 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
   685
(* subgoal that has 'terms' as premises                                      *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   686
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   687
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
   688
  List.exists
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   689
    (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
   690
      | _                                   => false)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   691
    terms;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   692
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   693
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
   694
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   695
  (* 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
   696
  (* splitting is possible                                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   697
  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
   698
    | split_loop (subgoal::subgoals)                = (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   699
        case split_once_items ctxt subgoal of
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   700
          SOME new_subgoals => split_loop (new_subgoals @ subgoals)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   701
        | NONE              => subgoal :: split_loop subgoals
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   702
      )
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   703
  fun is_relevant t  = isSome (decomp ctxt t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   704
  (* filter_prems_tac is_relevant: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   705
  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
   706
  (* split_tac, NNF normalization: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   707
  val split_goals    = split_loop [(Ts, relevant_terms)]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   708
  (* necessary because split_once_tac may normalize terms: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   709
  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
   710
  (* TRY (etac notE) THEN eq_assume_tac: *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   711
  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
   712
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   713
  result
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   714
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   715
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   716
(* 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
   717
(* 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
   718
(* (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
   719
(* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   720
(* 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
   721
(* 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
   722
(* 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
   723
(* !fast_arith_split_limit splits are possible.                              *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   724
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   725
local
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   726
  val nnf_simpset =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   727
    empty_ss setmkeqTrue mk_eq_True
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   728
    setmksimps (mksimps mksimps_pairs)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   729
    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
   730
      not_all, not_ex, not_not]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   731
  fun prem_nnf_tac i st =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   732
    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
   733
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   734
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   735
fun split_once_tac ctxt split_thms =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   736
  let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   737
    val thy = ProofContext.theory_of ctxt
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   738
    val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   739
      let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   740
        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
   741
        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
   742
        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
   743
        val splits = Splitter.split_posns cmap thy Ts concl
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   744
        val split_limit = ConfigOption.get ctxt fast_arith_split_limit
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   745
      in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   746
        if length splits > split_limit then no_tac
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   747
        else split_tac split_thms i
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   748
      end)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   749
  in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   750
    EVERY' [
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   751
      REPEAT_DETERM o etac rev_mp,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   752
      cond_split_tac,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   753
      rtac ccontr,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   754
      prem_nnf_tac,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   755
      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
   756
    ]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   757
  end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   758
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   759
end;  (* local *)
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
(* 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
   762
(* 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
   763
(* 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
   764
(* 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
   765
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   766
fun pre_tac ctxt i =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   767
let
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   768
  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
   769
  fun is_relevant t = isSome (decomp ctxt t)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   770
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   771
  DETERM (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   772
    TRY (filter_prems_tac is_relevant i)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   773
      THEN (
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   774
        (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
   775
          THEN_ALL_NEW
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   776
            (CONVERSION Drule.beta_eta_conversion
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   777
              THEN'
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   778
            (TRY o (etac notE THEN' eq_assume_tac)))
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   779
      ) i
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
end;
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
end;  (* LA_Data_Ref *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   784
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 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
   787
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   788
structure Fast_Arith =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   789
  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
   790
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   791
val map_data = Fast_Arith.map_data;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   792
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   793
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
   794
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
   795
val trace_arith            = Fast_Arith.trace;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   796
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   797
(* reduce contradictory <= to False.
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   798
   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
   799
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   800
val init_arith_data =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   801
 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
   802
   {add_mono_thms = add_mono_thms @
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   803
    @{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
   804
    mult_mono_thms = mult_mono_thms,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   805
    inj_thms = inj_thms,
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   806
    lessD = lessD @ [thm "Suc_leI"],
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   807
    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
   808
    simpset = HOL_basic_ss
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   809
      addsimps
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   810
       [@{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
   811
        @{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
   812
        @{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
   813
        @{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
   814
        @{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
   815
        @{thm "not_one_less_zero"}]
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   816
      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
   817
       (*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
   818
      addsimprocs nat_cancel_sums_add}) #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   819
  arith_discrete "nat";
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   820
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   821
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
   822
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   823
val fast_nat_arith_simproc =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   824
  Simplifier.simproc (the_context ()) "fast_nat_arith"
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   825
    ["(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
   826
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   827
(* 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
   828
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
   829
*not* themselves (in)equalities, because the latter activate
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   830
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
   831
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
   832
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   833
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   834
(* arith proof method *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   835
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   836
local
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   837
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   838
fun raw_arith_tac ctxt ex =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   839
  (* 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
   840
     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
   841
     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
   842
     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
   843
     (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
   844
     (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
   845
  *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   846
  refute_tac (K true)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   847
    (* 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
   848
    (* 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
   849
    (* 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
   850
    (* fast_arith_split_limit may trigger.                                   *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   851
    (* 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
   852
    (* 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
   853
    (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
   854
    (fast_ex_arith_tac ctxt ex);
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   855
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   856
fun more_arith_tacs ctxt =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   857
  let val tactics = #tactics (get_arith_data ctxt)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   858
  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
   859
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   860
in
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   861
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   862
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
   863
  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
   864
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   865
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
   866
  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
   867
  more_arith_tacs ctxt];
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   868
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   869
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
   870
  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
   871
  more_arith_tacs 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
fun arith_method src =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   874
  Method.syntax Args.bang_facts src
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   875
  #> (fn (prems, ctxt) => Method.METHOD (fn facts =>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   876
      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
   877
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   878
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   879
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   880
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   881
(* context setup *)
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   882
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   883
val setup =
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   884
  init_arith_data #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   885
  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
   886
    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
   887
  Context.mapping
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   888
   (setup_options #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   889
    Method.add_methods
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   890
      [("arith", arith_method, "decide linear arithmethic")] #>
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   891
    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
   892
      "declaration of split rules for arithmetic procedure")]) I;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   893
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   894
end;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   895
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   896
structure BasicLinArith: BASIC_LIN_ARITH = LinArith;
71c27b320610 HOL setup for linear arithmetic -- moved here from arith_data.ML;
wenzelm
parents:
diff changeset
   897
open BasicLinArith;