src/HOL/Tools/Qelim/cooper.ML
author wenzelm
Tue May 25 20:28:16 2010 +0200 (2010-05-25)
changeset 37117 59cee8807c29
parent 36945 9bec62c10714
child 37388 793618618f78
permissions -rw-r--r--
eliminated various catch-all exception patterns, guessing at the concrete exeptions that are intended here;
haftmann@24584
     1
(*  Title:      HOL/Tools/Qelim/cooper.ML
wenzelm@23466
     2
    Author:     Amine Chaieb, TU Muenchen
haftmann@36802
     3
haftmann@36802
     4
Presburger arithmetic by Cooper's algorithm.
wenzelm@23466
     5
*)
wenzelm@23466
     6
haftmann@36799
     7
signature COOPER =
haftmann@36798
     8
sig
haftmann@36798
     9
  type entry
haftmann@36798
    10
  val get: Proof.context -> entry
haftmann@36798
    11
  val del: term list -> attribute
haftmann@36798
    12
  val add: term list -> attribute 
wenzelm@37117
    13
  exception COOPER of string
haftmann@36804
    14
  val conv: Proof.context -> conv
haftmann@36804
    15
  val tac: bool -> thm list -> thm list -> Proof.context -> int -> tactic
haftmann@36804
    16
  val method: (Proof.context -> Method.method) context_parser
haftmann@36798
    17
  val setup: theory -> theory
wenzelm@23466
    18
end;
wenzelm@23466
    19
haftmann@36799
    20
structure Cooper: COOPER =
haftmann@36798
    21
struct
haftmann@36798
    22
haftmann@36799
    23
type entry = simpset * term list;
haftmann@36798
    24
haftmann@36798
    25
val allowed_consts = 
haftmann@36798
    26
  [@{term "op + :: int => _"}, @{term "op + :: nat => _"},
haftmann@36798
    27
   @{term "op - :: int => _"}, @{term "op - :: nat => _"},
haftmann@36798
    28
   @{term "op * :: int => _"}, @{term "op * :: nat => _"},
haftmann@36798
    29
   @{term "op div :: int => _"}, @{term "op div :: nat => _"},
haftmann@36798
    30
   @{term "op mod :: int => _"}, @{term "op mod :: nat => _"},
haftmann@36798
    31
   @{term "op &"}, @{term "op |"}, @{term "op -->"}, 
haftmann@36798
    32
   @{term "op = :: int => _"}, @{term "op = :: nat => _"}, @{term "op = :: bool => _"},
haftmann@36798
    33
   @{term "op < :: int => _"}, @{term "op < :: nat => _"},
haftmann@36798
    34
   @{term "op <= :: int => _"}, @{term "op <= :: nat => _"},
haftmann@36798
    35
   @{term "op dvd :: int => _"}, @{term "op dvd :: nat => _"},
haftmann@36798
    36
   @{term "abs :: int => _"},
haftmann@36798
    37
   @{term "max :: int => _"}, @{term "max :: nat => _"},
haftmann@36798
    38
   @{term "min :: int => _"}, @{term "min :: nat => _"},
haftmann@36798
    39
   @{term "uminus :: int => _"}, (*@ {term "uminus :: nat => _"},*)
haftmann@36798
    40
   @{term "Not"}, @{term "Suc"},
haftmann@36798
    41
   @{term "Ex :: (int => _) => _"}, @{term "Ex :: (nat => _) => _"},
haftmann@36798
    42
   @{term "All :: (int => _) => _"}, @{term "All :: (nat => _) => _"},
haftmann@36798
    43
   @{term "nat"}, @{term "int"},
haftmann@36798
    44
   @{term "Int.Bit0"}, @{term "Int.Bit1"},
haftmann@36798
    45
   @{term "Int.Pls"}, @{term "Int.Min"},
haftmann@36798
    46
   @{term "Int.number_of :: int => int"}, @{term "Int.number_of :: int => nat"},
haftmann@36798
    47
   @{term "0::int"}, @{term "1::int"}, @{term "0::nat"}, @{term "1::nat"},
haftmann@36798
    48
   @{term "True"}, @{term "False"}];
haftmann@36798
    49
haftmann@36798
    50
structure Data = Generic_Data
haftmann@36798
    51
(
haftmann@36798
    52
  type T = simpset * term list;
haftmann@36798
    53
  val empty = (HOL_ss, allowed_consts);
haftmann@36798
    54
  val extend  = I;
haftmann@36798
    55
  fun merge ((ss1, ts1), (ss2, ts2)) =
haftmann@36798
    56
    (merge_ss (ss1, ss2), Library.merge (op aconv) (ts1, ts2));
haftmann@36798
    57
);
haftmann@36798
    58
haftmann@36798
    59
val get = Data.get o Context.Proof;
haftmann@36798
    60
haftmann@36798
    61
fun add ts = Thm.declaration_attribute (fn th => fn context => 
haftmann@36798
    62
  context |> Data.map (fn (ss,ts') => 
haftmann@36798
    63
     (ss addsimps [th], merge (op aconv) (ts',ts) ))) 
haftmann@36798
    64
haftmann@36798
    65
fun del ts = Thm.declaration_attribute (fn th => fn context => 
haftmann@36798
    66
  context |> Data.map (fn (ss,ts') => 
haftmann@36798
    67
     (ss delsimps [th], subtract (op aconv) ts' ts ))) 
haftmann@36798
    68
wenzelm@27018
    69
fun simp_thms_conv ctxt =
wenzelm@35410
    70
  Simplifier.rewrite (Simplifier.context ctxt HOL_basic_ss addsimps @{thms simp_thms});
wenzelm@23484
    71
val FWD = Drule.implies_elim_list;
wenzelm@23466
    72
wenzelm@23466
    73
val true_tm = @{cterm "True"};
wenzelm@23466
    74
val false_tm = @{cterm "False"};
wenzelm@23466
    75
val zdvd1_eq = @{thm "zdvd1_eq"};
wenzelm@23466
    76
val presburger_ss = @{simpset} addsimps [zdvd1_eq];
wenzelm@30595
    77
val lin_ss = presburger_ss addsimps (@{thm dvd_eq_mod_eq_0} :: zdvd1_eq :: @{thms zadd_ac});
haftmann@23689
    78
wenzelm@23466
    79
val iT = HOLogic.intT
wenzelm@23466
    80
val bT = HOLogic.boolT;
haftmann@36831
    81
val dest_number = HOLogic.dest_number #> snd;
haftmann@36831
    82
val perhaps_number = try dest_number;
haftmann@36831
    83
val is_number = can dest_number;
wenzelm@23466
    84
wenzelm@32429
    85
val [miconj, midisj, mieq, mineq, milt, mile, migt, mige, midvd, mindvd, miP] =
wenzelm@23466
    86
    map(instantiate' [SOME @{ctyp "int"}] []) @{thms "minf"};
wenzelm@23466
    87
wenzelm@32429
    88
val [infDconj, infDdisj, infDdvd,infDndvd,infDP] =
wenzelm@23466
    89
    map(instantiate' [SOME @{ctyp "int"}] []) @{thms "inf_period"};
wenzelm@23466
    90
wenzelm@32429
    91
val [piconj, pidisj, pieq,pineq,pilt,pile,pigt,pige,pidvd,pindvd,piP] =
wenzelm@23466
    92
    map (instantiate' [SOME @{ctyp "int"}] []) @{thms "pinf"};
wenzelm@23466
    93
wenzelm@23466
    94
val [miP, piP] = map (instantiate' [SOME @{ctyp "bool"}] []) [miP, piP];
wenzelm@23466
    95
wenzelm@23466
    96
val infDP = instantiate' (map SOME [@{ctyp "int"}, @{ctyp "bool"}]) [] infDP;
wenzelm@23466
    97
wenzelm@32429
    98
val [[asetconj, asetdisj, aseteq, asetneq, asetlt, asetle,
wenzelm@23466
    99
      asetgt, asetge, asetdvd, asetndvd,asetP],
wenzelm@32429
   100
     [bsetconj, bsetdisj, bseteq, bsetneq, bsetlt, bsetle,
wenzelm@23466
   101
      bsetgt, bsetge, bsetdvd, bsetndvd,bsetP]]  = [@{thms "aset"}, @{thms "bset"}];
wenzelm@23466
   102
haftmann@36797
   103
val [cpmi, cppi] = [@{thm "cpmi"}, @{thm "cppi"}];
wenzelm@23466
   104
wenzelm@23466
   105
val unity_coeff_ex = instantiate' [SOME @{ctyp "int"}] [] @{thm "unity_coeff_ex"};
wenzelm@23466
   106
wenzelm@32429
   107
val [zdvd_mono,simp_from_to,all_not_ex] =
wenzelm@23466
   108
     [@{thm "zdvd_mono"}, @{thm "simp_from_to"}, @{thm "all_not_ex"}];
wenzelm@23466
   109
wenzelm@23466
   110
val [dvd_uminus, dvd_uminus'] = @{thms "uminus_dvd_conv"};
wenzelm@23466
   111
wenzelm@23466
   112
val eval_ss = presburger_ss addsimps [simp_from_to] delsimps [insert_iff,bex_triv];
wenzelm@23466
   113
val eval_conv = Simplifier.rewrite eval_ss;
wenzelm@23466
   114
haftmann@23689
   115
(* recognising cterm without moving to terms *)
wenzelm@23466
   116
wenzelm@32429
   117
datatype fm = And of cterm*cterm| Or of cterm*cterm| Eq of cterm | NEq of cterm
wenzelm@23466
   118
            | Lt of cterm | Le of cterm | Gt of cterm | Ge of cterm
wenzelm@23466
   119
            | Dvd of cterm*cterm | NDvd of cterm*cterm | Nox
wenzelm@23466
   120
wenzelm@32429
   121
fun whatis x ct =
wenzelm@32429
   122
( case (term_of ct) of
wenzelm@23466
   123
  Const("op &",_)$_$_ => And (Thm.dest_binop ct)
wenzelm@23466
   124
| Const ("op |",_)$_$_ => Or (Thm.dest_binop ct)
haftmann@36797
   125
| Const ("op =",_)$y$_ => if term_of x aconv y then Eq (Thm.dest_arg ct) else Nox
wenzelm@32429
   126
| Const (@{const_name Not},_) $ (Const ("op =",_)$y$_) =>
wenzelm@23466
   127
  if term_of x aconv y then NEq (funpow 2 Thm.dest_arg ct) else Nox
haftmann@35092
   128
| Const (@{const_name Orderings.less}, _) $ y$ z =>
wenzelm@32429
   129
   if term_of x aconv y then Lt (Thm.dest_arg ct)
wenzelm@23466
   130
   else if term_of x aconv z then Gt (Thm.dest_arg1 ct) else Nox
haftmann@35092
   131
| Const (@{const_name Orderings.less_eq}, _) $ y $ z =>
wenzelm@32429
   132
   if term_of x aconv y then Le (Thm.dest_arg ct)
wenzelm@23466
   133
   else if term_of x aconv z then Ge (Thm.dest_arg1 ct) else Nox
haftmann@35267
   134
| Const (@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$y$_) =>
wenzelm@32429
   135
   if term_of x aconv y then Dvd (Thm.dest_binop ct ||> Thm.dest_arg) else Nox
haftmann@35267
   136
| Const (@{const_name Not},_) $ (Const (@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$y$_)) =>
wenzelm@32429
   137
   if term_of x aconv y then
wenzelm@32429
   138
   NDvd (Thm.dest_binop (Thm.dest_arg ct) ||> Thm.dest_arg) else Nox
wenzelm@23466
   139
| _ => Nox)
wenzelm@32429
   140
  handle CTERM _ => Nox;
wenzelm@23466
   141
wenzelm@32429
   142
fun get_pmi_term t =
wenzelm@32429
   143
  let val (x,eq) =
wenzelm@23466
   144
     (Thm.dest_abs NONE o Thm.dest_arg o snd o Thm.dest_abs NONE o Thm.dest_arg)
wenzelm@23466
   145
        (Thm.dest_arg t)
wenzelm@23466
   146
in (Thm.cabs x o Thm.dest_arg o Thm.dest_arg) eq end;
wenzelm@23466
   147
wenzelm@23466
   148
val get_pmi = get_pmi_term o cprop_of;
wenzelm@23466
   149
wenzelm@32429
   150
val p_v' = @{cpat "?P' :: int => bool"};
wenzelm@23466
   151
val q_v' = @{cpat "?Q' :: int => bool"};
wenzelm@23466
   152
val p_v = @{cpat "?P:: int => bool"};
wenzelm@23466
   153
val q_v = @{cpat "?Q:: int => bool"};
wenzelm@23466
   154
wenzelm@32429
   155
fun myfwd (th1, th2, th3) p q
wenzelm@32429
   156
      [(th_1,th_2,th_3), (th_1',th_2',th_3')] =
wenzelm@32429
   157
  let
wenzelm@23466
   158
   val (mp', mq') = (get_pmi th_1, get_pmi th_1')
wenzelm@32429
   159
   val mi_th = FWD (instantiate ([],[(p_v,p),(q_v,q), (p_v',mp'),(q_v',mq')]) th1)
wenzelm@23466
   160
                   [th_1, th_1']
wenzelm@23466
   161
   val infD_th = FWD (instantiate ([],[(p_v,mp'), (q_v, mq')]) th3) [th_3,th_3']
wenzelm@23466
   162
   val set_th = FWD (instantiate ([],[(p_v,p), (q_v,q)]) th2) [th_2, th_2']
wenzelm@23466
   163
  in (mi_th, set_th, infD_th)
wenzelm@23466
   164
  end;
wenzelm@23466
   165
wenzelm@23466
   166
val inst' = fn cts => instantiate' [] (map SOME cts);
wenzelm@23466
   167
val infDTrue = instantiate' [] [SOME true_tm] infDP;
wenzelm@23466
   168
val infDFalse = instantiate' [] [SOME false_tm] infDP;
wenzelm@23466
   169
wenzelm@23466
   170
val cadd =  @{cterm "op + :: int => _"}
wenzelm@23466
   171
val cmulC =  @{cterm "op * :: int => _"}
wenzelm@23466
   172
val cminus =  @{cterm "op - :: int => _"}
haftmann@23689
   173
val cone =  @{cterm "1 :: int"}
haftmann@36797
   174
val [addC, mulC, subC] = map term_of [cadd, cmulC, cminus]
haftmann@23689
   175
val [zero, one] = [@{term "0 :: int"}, @{term "1 :: int"}];
wenzelm@23466
   176
haftmann@36831
   177
fun numeral1 f n = HOLogic.mk_number iT (f (dest_number n));
haftmann@36831
   178
fun numeral2 f m n = HOLogic.mk_number iT (f (dest_number m) (dest_number n));
wenzelm@23466
   179
wenzelm@32429
   180
val [minus1,plus1] =
wenzelm@23466
   181
    map (fn c => fn t => Thm.capply (Thm.capply c t) cone) [cminus,cadd];
wenzelm@23466
   182
wenzelm@32429
   183
fun decomp_pinf x dvd inS [aseteq, asetneq, asetlt, asetle,
wenzelm@23466
   184
                           asetgt, asetge,asetdvd,asetndvd,asetP,
wenzelm@23466
   185
                           infDdvd, infDndvd, asetconj,
wenzelm@23466
   186
                           asetdisj, infDconj, infDdisj] cp =
wenzelm@23466
   187
 case (whatis x cp) of
wenzelm@23466
   188
  And (p,q) => ([p,q], myfwd (piconj, asetconj, infDconj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   189
| Or (p,q) => ([p,q], myfwd (pidisj, asetdisj, infDdisj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   190
| Eq t => ([], K (inst' [t] pieq, FWD (inst' [t] aseteq) [inS (plus1 t)], infDFalse))
wenzelm@23466
   191
| NEq t => ([], K (inst' [t] pineq, FWD (inst' [t] asetneq) [inS t], infDTrue))
wenzelm@23466
   192
| Lt t => ([], K (inst' [t] pilt, FWD (inst' [t] asetlt) [inS t], infDFalse))
wenzelm@23466
   193
| Le t => ([], K (inst' [t] pile, FWD (inst' [t] asetle) [inS (plus1 t)], infDFalse))
wenzelm@23466
   194
| Gt t => ([], K (inst' [t] pigt, (inst' [t] asetgt), infDTrue))
wenzelm@23466
   195
| Ge t => ([], K (inst' [t] pige, (inst' [t] asetge), infDTrue))
wenzelm@32429
   196
| Dvd (d,s) =>
wenzelm@23466
   197
   ([],let val dd = dvd d
wenzelm@32429
   198
       in K (inst' [d,s] pidvd, FWD (inst' [d,s] asetdvd) [dd],FWD (inst' [d,s] infDdvd) [dd]) end)
wenzelm@23466
   199
| NDvd(d,s) => ([],let val dd = dvd d
wenzelm@32429
   200
        in K (inst' [d,s] pindvd, FWD (inst' [d,s] asetndvd) [dd], FWD (inst' [d,s] infDndvd) [dd]) end)
wenzelm@23466
   201
| _ => ([], K (inst' [cp] piP, inst' [cp] asetP, inst' [cp] infDP));
wenzelm@23466
   202
wenzelm@23466
   203
fun decomp_minf x dvd inS [bseteq,bsetneq,bsetlt, bsetle, bsetgt,
wenzelm@23466
   204
                           bsetge,bsetdvd,bsetndvd,bsetP,
wenzelm@23466
   205
                           infDdvd, infDndvd, bsetconj,
wenzelm@23466
   206
                           bsetdisj, infDconj, infDdisj] cp =
wenzelm@23466
   207
 case (whatis x cp) of
wenzelm@23466
   208
  And (p,q) => ([p,q], myfwd (miconj, bsetconj, infDconj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   209
| Or (p,q) => ([p,q], myfwd (midisj, bsetdisj, infDdisj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   210
| Eq t => ([], K (inst' [t] mieq, FWD (inst' [t] bseteq) [inS (minus1 t)], infDFalse))
wenzelm@23466
   211
| NEq t => ([], K (inst' [t] mineq, FWD (inst' [t] bsetneq) [inS t], infDTrue))
wenzelm@23466
   212
| Lt t => ([], K (inst' [t] milt, (inst' [t] bsetlt), infDTrue))
wenzelm@23466
   213
| Le t => ([], K (inst' [t] mile, (inst' [t] bsetle), infDTrue))
wenzelm@23466
   214
| Gt t => ([], K (inst' [t] migt, FWD (inst' [t] bsetgt) [inS t], infDFalse))
wenzelm@23466
   215
| Ge t => ([], K (inst' [t] mige,FWD (inst' [t] bsetge) [inS (minus1 t)], infDFalse))
wenzelm@23466
   216
| Dvd (d,s) => ([],let val dd = dvd d
wenzelm@32429
   217
        in K (inst' [d,s] midvd, FWD (inst' [d,s] bsetdvd) [dd] , FWD (inst' [d,s] infDdvd) [dd]) end)
wenzelm@23466
   218
| NDvd (d,s) => ([],let val dd = dvd d
wenzelm@32429
   219
        in K (inst' [d,s] mindvd, FWD (inst' [d,s] bsetndvd) [dd], FWD (inst' [d,s] infDndvd) [dd]) end)
wenzelm@23466
   220
| _ => ([], K (inst' [cp] miP, inst' [cp] bsetP, inst' [cp] infDP))
wenzelm@23466
   221
wenzelm@23466
   222
    (* Canonical linear form for terms, formulae etc.. *)
wenzelm@32429
   223
fun provelin ctxt t = Goal.prove ctxt [] [] t
haftmann@31101
   224
  (fn _ => EVERY [simp_tac lin_ss 1, TRY (Lin_Arith.tac ctxt 1)]);
wenzelm@32429
   225
fun linear_cmul 0 tm = zero
wenzelm@32429
   226
  | linear_cmul n tm = case tm of
haftmann@35267
   227
      Const (@{const_name Groups.plus}, _) $ a $ b => addC $ linear_cmul n a $ linear_cmul n b
haftmann@35267
   228
    | Const (@{const_name Groups.times}, _) $ c $ x => mulC $ numeral1 (fn m => n * m) c $ x
haftmann@35267
   229
    | Const (@{const_name Groups.minus}, _) $ a $ b => subC $ linear_cmul n a $ linear_cmul n b
haftmann@35267
   230
    | (m as Const (@{const_name Groups.uminus}, _)) $ a => m $ linear_cmul n a
haftmann@25768
   231
    | _ => numeral1 (fn m => n * m) tm;
wenzelm@32429
   232
fun earlier [] x y = false
wenzelm@32429
   233
  | earlier (h::t) x y =
wenzelm@32429
   234
    if h aconv y then false else if h aconv x then true else earlier t x y;
wenzelm@23466
   235
wenzelm@32429
   236
fun linear_add vars tm1 tm2 = case (tm1, tm2) of
haftmann@35267
   237
    (Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c1 $ x1) $ r1,
haftmann@35267
   238
    Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c2 $ x2) $ r2) =>
wenzelm@32429
   239
   if x1 = x2 then
wenzelm@33002
   240
     let val c = numeral2 Integer.add c1 c2
haftmann@25768
   241
      in if c = zero then linear_add vars r1 r2
haftmann@25768
   242
         else addC$(mulC$c$x1)$(linear_add vars r1 r2)
wenzelm@32429
   243
     end
haftmann@25768
   244
     else if earlier vars x1 x2 then addC $ (mulC $ c1 $ x1) $ linear_add vars r1 tm2
haftmann@25768
   245
   else addC $ (mulC $ c2 $ x2) $ linear_add vars tm1 r2
haftmann@35267
   246
 | (Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c1 $ x1) $ r1, _) =>
haftmann@25768
   247
      addC $ (mulC $ c1 $ x1) $ linear_add vars r1 tm2
haftmann@35267
   248
 | (_, Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c2 $ x2) $ r2) =>
haftmann@25768
   249
      addC $ (mulC $ c2 $ x2) $ linear_add vars tm1 r2
wenzelm@33002
   250
 | (_, _) => numeral2 Integer.add tm1 tm2;
wenzelm@32429
   251
wenzelm@32429
   252
fun linear_neg tm = linear_cmul ~1 tm;
wenzelm@32429
   253
fun linear_sub vars tm1 tm2 = linear_add vars tm1 (linear_neg tm2);
wenzelm@23466
   254
haftmann@36806
   255
exception COOPER of string;
wenzelm@23466
   256
haftmann@36831
   257
fun lint vars tm =  if is_number tm then tm  else case tm of
haftmann@35267
   258
  Const (@{const_name Groups.uminus}, _) $ t => linear_neg (lint vars t)
haftmann@35267
   259
| Const (@{const_name Groups.plus}, _) $ s $ t => linear_add vars (lint vars s) (lint vars t)
haftmann@35267
   260
| Const (@{const_name Groups.minus}, _) $ s $ t => linear_sub vars (lint vars s) (lint vars t)
haftmann@35267
   261
| Const (@{const_name Groups.times}, _) $ s $ t =>
wenzelm@32429
   262
  let val s' = lint vars s
wenzelm@32429
   263
      val t' = lint vars t
haftmann@36831
   264
  in case perhaps_number s' of SOME n => linear_cmul n t'
haftmann@36831
   265
   | NONE => (case perhaps_number t' of SOME n => linear_cmul n s'
haftmann@36831
   266
   | NONE => raise COOPER "lint: not linear")
wenzelm@32429
   267
  end
haftmann@25768
   268
 | _ => addC $ (mulC $ one $ tm) $ zero;
wenzelm@23466
   269
haftmann@35092
   270
fun lin (vs as x::_) (Const (@{const_name Not}, _) $ (Const (@{const_name Orderings.less}, T) $ s $ t)) =
haftmann@35092
   271
    lin vs (Const (@{const_name Orderings.less_eq}, T) $ t $ s)
haftmann@35092
   272
  | lin (vs as x::_) (Const (@{const_name Not},_) $ (Const(@{const_name Orderings.less_eq}, T) $ s $ t)) =
haftmann@35092
   273
    lin vs (Const (@{const_name Orderings.less}, T) $ t $ s)
haftmann@25768
   274
  | lin vs (Const (@{const_name Not},T)$t) = Const (@{const_name Not},T)$ (lin vs t)
haftmann@35050
   275
  | lin (vs as x::_) (Const(@{const_name Rings.dvd},_)$d$t) =
haftmann@35050
   276
    HOLogic.mk_binrel @{const_name Rings.dvd} (numeral1 abs d, lint vs t)
wenzelm@32429
   277
  | lin (vs as x::_) ((b as Const("op =",_))$s$t) =
wenzelm@32429
   278
     (case lint vs (subC$t$s) of
wenzelm@32429
   279
      (t as a$(m$c$y)$r) =>
wenzelm@23466
   280
        if x <> y then b$zero$t
haftmann@36831
   281
        else if dest_number c < 0 then b$(m$(numeral1 ~ c)$y)$r
wenzelm@23466
   282
        else b$(m$c$y)$(linear_neg r)
wenzelm@23466
   283
      | t => b$zero$t)
wenzelm@32429
   284
  | lin (vs as x::_) (b$s$t) =
wenzelm@32429
   285
     (case lint vs (subC$t$s) of
wenzelm@32429
   286
      (t as a$(m$c$y)$r) =>
wenzelm@23466
   287
        if x <> y then b$zero$t
haftmann@36831
   288
        else if dest_number c < 0 then b$(m$(numeral1 ~ c)$y)$r
wenzelm@23466
   289
        else b$(linear_neg r)$(m$c$y)
wenzelm@23466
   290
      | t => b$zero$t)
wenzelm@23466
   291
  | lin vs fm = fm;
wenzelm@23466
   292
wenzelm@32429
   293
fun lint_conv ctxt vs ct =
wenzelm@23466
   294
let val t = term_of ct
wenzelm@23466
   295
in (provelin ctxt ((HOLogic.eq_const iT)$t$(lint vs t) |> HOLogic.mk_Trueprop))
wenzelm@23466
   296
             RS eq_reflection
wenzelm@23466
   297
end;
wenzelm@23466
   298
boehmes@32398
   299
fun is_intrel_type T = T = @{typ "int => int => bool"};
boehmes@32398
   300
boehmes@32398
   301
fun is_intrel (b$_$_) = is_intrel_type (fastype_of b)
boehmes@32398
   302
  | is_intrel (@{term "Not"}$(b$_$_)) = is_intrel_type (fastype_of b)
wenzelm@23466
   303
  | is_intrel _ = false;
wenzelm@32429
   304
haftmann@25768
   305
fun linearize_conv ctxt vs ct = case term_of ct of
haftmann@35050
   306
  Const(@{const_name Rings.dvd},_)$d$t =>
wenzelm@32429
   307
  let
haftmann@36797
   308
    val th = Conv.binop_conv (lint_conv ctxt vs) ct
wenzelm@23466
   309
    val (d',t') = Thm.dest_binop (Thm.rhs_of th)
wenzelm@23466
   310
    val (dt',tt') = (term_of d', term_of t')
haftmann@36831
   311
  in if is_number dt' andalso is_number tt'
haftmann@36797
   312
     then Conv.fconv_rule (Conv.arg_conv (Simplifier.rewrite presburger_ss)) th
wenzelm@32429
   313
     else
wenzelm@32429
   314
     let
wenzelm@32429
   315
      val dth =
haftmann@36831
   316
      ((if dest_number (term_of d') < 0 then
haftmann@36797
   317
          Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (lint_conv ctxt vs)))
wenzelm@23466
   318
                           (Thm.transitive th (inst' [d',t'] dvd_uminus))
wenzelm@23466
   319
        else th) handle TERM _ => th)
wenzelm@23466
   320
      val d'' = Thm.rhs_of dth |> Thm.dest_arg1
wenzelm@23466
   321
     in
wenzelm@32429
   322
      case tt' of
haftmann@35267
   323
        Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$_)$_ =>
haftmann@36831
   324
        let val x = dest_number c
haftmann@36797
   325
        in if x < 0 then Conv.fconv_rule (Conv.arg_conv (Conv.arg_conv (lint_conv ctxt vs)))
wenzelm@23466
   326
                                       (Thm.transitive dth (inst' [d'',t'] dvd_uminus'))
wenzelm@23466
   327
        else dth end
wenzelm@23466
   328
      | _ => dth
wenzelm@23466
   329
     end
wenzelm@23466
   330
  end
haftmann@36797
   331
| Const (@{const_name Not},_)$(Const(@{const_name Rings.dvd},_)$_$_) => Conv.arg_conv (linearize_conv ctxt vs) ct
wenzelm@32429
   332
| t => if is_intrel t
wenzelm@23466
   333
      then (provelin ctxt ((HOLogic.eq_const bT)$t$(lin vs t) |> HOLogic.mk_Trueprop))
wenzelm@23466
   334
       RS eq_reflection
wenzelm@36945
   335
      else Thm.reflexive ct;
wenzelm@23466
   336
wenzelm@23466
   337
val dvdc = @{cterm "op dvd :: int => _"};
wenzelm@23466
   338
wenzelm@32429
   339
fun unify ctxt q =
wenzelm@23466
   340
 let
wenzelm@23466
   341
  val (e,(cx,p)) = q |> Thm.dest_comb ||> Thm.dest_abs NONE
wenzelm@32429
   342
  val x = term_of cx
wenzelm@24630
   343
  val ins = insert (op = : int * int -> bool)
wenzelm@32429
   344
  fun h (acc,dacc) t =
wenzelm@23466
   345
   case (term_of t) of
haftmann@35267
   346
    Const(s,_)$(Const(@{const_name Groups.times},_)$c$y)$ _ =>
haftmann@23881
   347
    if x aconv y andalso member (op =)
haftmann@35092
   348
      ["op =", @{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
haftmann@36831
   349
    then (ins (dest_number c) acc,dacc) else (acc,dacc)
haftmann@35267
   350
  | Const(s,_)$_$(Const(@{const_name Groups.times},_)$c$y) =>
haftmann@23881
   351
    if x aconv y andalso member (op =)
haftmann@35092
   352
       [@{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
haftmann@36831
   353
    then (ins (dest_number c) acc, dacc) else (acc,dacc)
haftmann@35267
   354
  | Const(@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$y)$_) =>
haftmann@36831
   355
    if x aconv y then (acc,ins (dest_number c) dacc) else (acc,dacc)
wenzelm@23466
   356
  | Const("op &",_)$_$_ => h (h (acc,dacc) (Thm.dest_arg1 t)) (Thm.dest_arg t)
wenzelm@23466
   357
  | Const("op |",_)$_$_ => h (h (acc,dacc) (Thm.dest_arg1 t)) (Thm.dest_arg t)
haftmann@25768
   358
  | Const (@{const_name Not},_)$_ => h (acc,dacc) (Thm.dest_arg t)
wenzelm@23466
   359
  | _ => (acc, dacc)
wenzelm@23466
   360
  val (cs,ds) = h ([],[]) p
haftmann@33042
   361
  val l = Integer.lcms (union (op =) cs ds)
wenzelm@32429
   362
  fun cv k ct =
wenzelm@32429
   363
    let val (tm as b$s$t) = term_of ct
wenzelm@23466
   364
    in ((HOLogic.eq_const bT)$tm$(b$(linear_cmul k s)$(linear_cmul k t))
wenzelm@23466
   365
         |> HOLogic.mk_Trueprop |> provelin ctxt) RS eq_reflection end
wenzelm@32429
   366
  fun nzprop x =
wenzelm@32429
   367
   let
wenzelm@32429
   368
    val th =
wenzelm@32429
   369
     Simplifier.rewrite lin_ss
wenzelm@32429
   370
      (Thm.capply @{cterm Trueprop} (Thm.capply @{cterm "Not"}
wenzelm@32429
   371
           (Thm.capply (Thm.capply @{cterm "op = :: int => _"} (Numeral.mk_cnumber @{ctyp "int"} x))
haftmann@23689
   372
           @{cterm "0::int"})))
wenzelm@36945
   373
   in Thm.equal_elim (Thm.symmetric th) TrueI end;
wenzelm@32429
   374
  val notz =
wenzelm@32429
   375
    let val tab = fold Inttab.update
wenzelm@32429
   376
          (ds ~~ (map (fn x => nzprop (l div x)) ds)) Inttab.empty
wenzelm@32429
   377
    in
haftmann@36831
   378
      fn ct => the (Inttab.lookup tab (ct |> term_of |> dest_number))
wenzelm@32429
   379
        handle Option =>
wenzelm@32429
   380
          (writeln ("noz: Theorems-Table contains no entry for " ^
wenzelm@32429
   381
              Syntax.string_of_term ctxt (Thm.term_of ct)); raise Option)
wenzelm@32429
   382
    end
wenzelm@32429
   383
  fun unit_conv t =
wenzelm@23466
   384
   case (term_of t) of
haftmann@36797
   385
   Const("op &",_)$_$_ => Conv.binop_conv unit_conv t
haftmann@36797
   386
  | Const("op |",_)$_$_ => Conv.binop_conv unit_conv t
haftmann@36797
   387
  | Const (@{const_name Not},_)$_ => Conv.arg_conv unit_conv t
haftmann@35267
   388
  | Const(s,_)$(Const(@{const_name Groups.times},_)$c$y)$ _ =>
haftmann@23881
   389
    if x=y andalso member (op =)
haftmann@35092
   390
      ["op =", @{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
haftmann@36831
   391
    then cv (l div dest_number c) t else Thm.reflexive t
haftmann@35267
   392
  | Const(s,_)$_$(Const(@{const_name Groups.times},_)$c$y) =>
haftmann@23881
   393
    if x=y andalso member (op =)
haftmann@35092
   394
      [@{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
haftmann@36831
   395
    then cv (l div dest_number c) t else Thm.reflexive t
haftmann@35267
   396
  | Const(@{const_name Rings.dvd},_)$d$(r as (Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$y)$_)) =>
wenzelm@32429
   397
    if x=y then
wenzelm@32429
   398
      let
haftmann@36831
   399
       val k = l div dest_number c
wenzelm@23466
   400
       val kt = HOLogic.mk_number iT k
wenzelm@32429
   401
       val th1 = inst' [Thm.dest_arg1 t, Thm.dest_arg t]
wenzelm@23466
   402
             ((Thm.dest_arg t |> funpow 2 Thm.dest_arg1 |> notz) RS zdvd_mono)
wenzelm@23466
   403
       val (d',t') = (mulC$kt$d, mulC$kt$r)
wenzelm@23466
   404
       val thc = (provelin ctxt ((HOLogic.eq_const iT)$d'$(lint [] d') |> HOLogic.mk_Trueprop))
wenzelm@23466
   405
                   RS eq_reflection
wenzelm@23466
   406
       val tht = (provelin ctxt ((HOLogic.eq_const iT)$t'$(linear_cmul k r) |> HOLogic.mk_Trueprop))
wenzelm@23466
   407
                 RS eq_reflection
wenzelm@32429
   408
      in Thm.transitive th1 (Thm.combination (Drule.arg_cong_rule dvdc thc) tht) end
wenzelm@23466
   409
    else Thm.reflexive t
wenzelm@23466
   410
  | _ => Thm.reflexive t
wenzelm@23466
   411
  val uth = unit_conv p
haftmann@23689
   412
  val clt =  Numeral.mk_cnumber @{ctyp "int"} l
wenzelm@23466
   413
  val ltx = Thm.capply (Thm.capply cmulC clt) cx
wenzelm@23466
   414
  val th = Drule.arg_cong_rule e (Thm.abstract_rule (fst (dest_Free x )) cx uth)
wenzelm@23466
   415
  val th' = inst' [Thm.cabs ltx (Thm.rhs_of uth), clt] unity_coeff_ex
wenzelm@36945
   416
  val thf = Thm.transitive th
wenzelm@36945
   417
      (Thm.transitive (Thm.symmetric (Thm.beta_conversion true (cprop_of th' |> Thm.dest_arg1))) th')
wenzelm@23466
   418
  val (lth,rth) = Thm.dest_comb (cprop_of thf) |>> Thm.dest_arg |>> Thm.beta_conversion true
wenzelm@36945
   419
                  ||> Thm.beta_conversion true |>> Thm.symmetric
wenzelm@36945
   420
 in Thm.transitive (Thm.transitive lth thf) rth end;
wenzelm@23466
   421
wenzelm@23466
   422
wenzelm@23466
   423
val emptyIS = @{cterm "{}::int set"};
wenzelm@23466
   424
val insert_tm = @{cterm "insert :: int => _"};
wenzelm@23466
   425
fun mkISet cts = fold_rev (Thm.capply insert_tm #> Thm.capply) cts emptyIS;
wenzelm@23466
   426
val eqelem_imp_imp = (thm"eqelem_imp_iff") RS iffD1;
wenzelm@32429
   427
val [A_tm,B_tm] = map (fn th => cprop_of th |> funpow 2 Thm.dest_arg |> Thm.dest_abs NONE |> snd |> Thm.dest_arg1 |> Thm.dest_arg
wenzelm@23466
   428
                                      |> Thm.dest_abs NONE |> snd |> Thm.dest_fun |> Thm.dest_arg)
wenzelm@23466
   429
                      [asetP,bsetP];
wenzelm@23466
   430
wenzelm@23466
   431
val D_tm = @{cpat "?D::int"};
wenzelm@23466
   432
wenzelm@32429
   433
fun cooperex_conv ctxt vs q =
wenzelm@32429
   434
let
wenzelm@23466
   435
wenzelm@23466
   436
 val uth = unify ctxt q
wenzelm@23466
   437
 val (x,p) = Thm.dest_abs NONE (Thm.dest_arg (Thm.rhs_of uth))
wenzelm@23466
   438
 val ins = insert (op aconvc)
wenzelm@32429
   439
 fun h t (bacc,aacc,dacc) =
wenzelm@23466
   440
  case (whatis x t) of
wenzelm@23466
   441
    And (p,q) => h q (h p (bacc,aacc,dacc))
wenzelm@23466
   442
  | Or (p,q) => h q  (h p (bacc,aacc,dacc))
wenzelm@32429
   443
  | Eq t => (ins (minus1 t) bacc,
wenzelm@23466
   444
             ins (plus1 t) aacc,dacc)
wenzelm@32429
   445
  | NEq t => (ins t bacc,
wenzelm@23466
   446
              ins t aacc, dacc)
wenzelm@23466
   447
  | Lt t => (bacc, ins t aacc, dacc)
wenzelm@23466
   448
  | Le t => (bacc, ins (plus1 t) aacc,dacc)
wenzelm@23466
   449
  | Gt t => (ins t bacc, aacc,dacc)
wenzelm@23466
   450
  | Ge t => (ins (minus1 t) bacc, aacc,dacc)
haftmann@36831
   451
  | Dvd (d,_) => (bacc,aacc,insert (op =) (term_of d |> dest_number) dacc)
haftmann@36831
   452
  | NDvd (d,_) => (bacc,aacc,insert (op =) (term_of d|> dest_number) dacc)
wenzelm@23466
   453
  | _ => (bacc, aacc, dacc)
wenzelm@23466
   454
 val (b0,a0,ds) = h p ([],[],[])
wenzelm@24630
   455
 val d = Integer.lcms ds
wenzelm@23582
   456
 val cd = Numeral.mk_cnumber @{ctyp "int"} d
wenzelm@32429
   457
 fun divprop x =
wenzelm@32429
   458
   let
wenzelm@32429
   459
    val th =
wenzelm@32429
   460
     Simplifier.rewrite lin_ss
wenzelm@32429
   461
      (Thm.capply @{cterm Trueprop}
wenzelm@23582
   462
           (Thm.capply (Thm.capply dvdc (Numeral.mk_cnumber @{ctyp "int"} x)) cd))
wenzelm@36945
   463
   in Thm.equal_elim (Thm.symmetric th) TrueI end;
wenzelm@32429
   464
 val dvd =
wenzelm@32429
   465
   let val tab = fold Inttab.update (ds ~~ (map divprop ds)) Inttab.empty in
haftmann@36831
   466
     fn ct => the (Inttab.lookup tab (term_of ct |> dest_number))
wenzelm@32429
   467
       handle Option =>
wenzelm@32429
   468
        (writeln ("dvd: Theorems-Table contains no entry for" ^
wenzelm@32429
   469
            Syntax.string_of_term ctxt (Thm.term_of ct)); raise Option)
wenzelm@32429
   470
   end
wenzelm@32429
   471
 val dp =
wenzelm@32429
   472
   let val th = Simplifier.rewrite lin_ss
wenzelm@32429
   473
      (Thm.capply @{cterm Trueprop}
wenzelm@23466
   474
           (Thm.capply (Thm.capply @{cterm "op < :: int => _"} @{cterm "0::int"}) cd))
wenzelm@36945
   475
   in Thm.equal_elim (Thm.symmetric th) TrueI end;
wenzelm@23466
   476
    (* A and B set *)
wenzelm@32429
   477
   local
wenzelm@23466
   478
     val insI1 = instantiate' [SOME @{ctyp "int"}] [] @{thm "insertI1"}
wenzelm@23466
   479
     val insI2 = instantiate' [SOME @{ctyp "int"}] [] @{thm "insertI2"}
wenzelm@23466
   480
   in
wenzelm@32429
   481
    fun provein x S =
wenzelm@23466
   482
     case term_of S of
haftmann@32264
   483
        Const(@{const_name Orderings.bot}, _) => error "Unexpected error in Cooper, please email Amine Chaieb"
wenzelm@32429
   484
      | Const(@{const_name insert}, _) $ y $ _ =>
wenzelm@23466
   485
         let val (cy,S') = Thm.dest_binop S
wenzelm@23466
   486
         in if term_of x aconv y then instantiate' [] [SOME x, SOME S'] insI1
wenzelm@36945
   487
         else Thm.implies_elim (instantiate' [] [SOME x, SOME S', SOME cy] insI2)
wenzelm@23466
   488
                           (provein x S')
wenzelm@23466
   489
         end
wenzelm@23466
   490
   end
wenzelm@32429
   491
wenzelm@23466
   492
 val al = map (lint vs o term_of) a0
wenzelm@23466
   493
 val bl = map (lint vs o term_of) b0
wenzelm@32429
   494
 val (sl,s0,f,abths,cpth) =
wenzelm@32429
   495
   if length (distinct (op aconv) bl) <= length (distinct (op aconv) al)
wenzelm@32429
   496
   then
wenzelm@23466
   497
    (bl,b0,decomp_minf,
wenzelm@36945
   498
     fn B => (map (fn th => Thm.implies_elim (Thm.instantiate ([],[(B_tm,B), (D_tm,cd)]) th) dp)
wenzelm@23466
   499
                     [bseteq,bsetneq,bsetlt, bsetle, bsetgt,bsetge])@
wenzelm@32429
   500
                   (map (Thm.instantiate ([],[(B_tm,B), (D_tm,cd)]))
wenzelm@23466
   501
                        [bsetdvd,bsetndvd,bsetP,infDdvd, infDndvd,bsetconj,
wenzelm@23466
   502
                         bsetdisj,infDconj, infDdisj]),
wenzelm@32429
   503
                       cpmi)
wenzelm@32429
   504
     else (al,a0,decomp_pinf,fn A =>
wenzelm@36945
   505
          (map (fn th => Thm.implies_elim (Thm.instantiate ([],[(A_tm,A), (D_tm,cd)]) th) dp)
wenzelm@23466
   506
                   [aseteq,asetneq,asetlt, asetle, asetgt,asetge])@
wenzelm@32429
   507
                   (map (Thm.instantiate ([],[(A_tm,A), (D_tm,cd)]))
wenzelm@23466
   508
                   [asetdvd,asetndvd, asetP, infDdvd, infDndvd,asetconj,
wenzelm@23466
   509
                         asetdisj,infDconj, infDdisj]),cppi)
wenzelm@32429
   510
 val cpth =
wenzelm@23466
   511
  let
wenzelm@32429
   512
   val sths = map (fn (tl,t0) =>
wenzelm@32429
   513
                      if tl = term_of t0
wenzelm@23466
   514
                      then instantiate' [SOME @{ctyp "int"}] [SOME t0] refl
wenzelm@32429
   515
                      else provelin ctxt ((HOLogic.eq_const iT)$tl$(term_of t0)
wenzelm@32429
   516
                                 |> HOLogic.mk_Trueprop))
wenzelm@23466
   517
                   (sl ~~ s0)
wenzelm@23466
   518
   val csl = distinct (op aconvc) (map (cprop_of #> Thm.dest_arg #> Thm.dest_arg1) sths)
wenzelm@23466
   519
   val S = mkISet csl
wenzelm@32429
   520
   val inStab = fold (fn ct => fn tab => Termtab.update (term_of ct, provein ct S) tab)
wenzelm@23466
   521
                    csl Termtab.empty
wenzelm@23466
   522
   val eqelem_th = instantiate' [SOME @{ctyp "int"}] [NONE,NONE, SOME S] eqelem_imp_imp
wenzelm@32429
   523
   val inS =
wenzelm@32429
   524
     let
wenzelm@23466
   525
      val tab = fold Termtab.update
wenzelm@32429
   526
        (map (fn eq =>
wenzelm@32429
   527
                let val (s,t) = cprop_of eq |> Thm.dest_arg |> Thm.dest_binop
wenzelm@32429
   528
                    val th = if term_of s = term_of t
wenzelm@33035
   529
                             then the (Termtab.lookup inStab (term_of s))
wenzelm@32429
   530
                             else FWD (instantiate' [] [SOME s, SOME t] eqelem_th)
wenzelm@33035
   531
                                [eq, the (Termtab.lookup inStab (term_of s))]
wenzelm@23466
   532
                 in (term_of t, th) end)
wenzelm@23466
   533
                  sths) Termtab.empty
wenzelm@32429
   534
        in
wenzelm@33035
   535
          fn ct => the (Termtab.lookup tab (term_of ct))
wenzelm@32429
   536
            handle Option =>
wenzelm@32429
   537
              (writeln ("inS: No theorem for " ^ Syntax.string_of_term ctxt (Thm.term_of ct));
wenzelm@32429
   538
                raise Option)
wenzelm@23466
   539
        end
wenzelm@23466
   540
       val (inf, nb, pd) = divide_and_conquer (f x dvd inS (abths S)) p
wenzelm@23466
   541
   in [dp, inf, nb, pd] MRS cpth
wenzelm@23466
   542
   end
wenzelm@23466
   543
 val cpth' = Thm.transitive uth (cpth RS eq_reflection)
wenzelm@27018
   544
in Thm.transitive cpth' ((simp_thms_conv ctxt then_conv eval_conv) (Thm.rhs_of cpth'))
wenzelm@23466
   545
end;
wenzelm@23466
   546
wenzelm@32429
   547
fun literals_conv bops uops env cv =
wenzelm@23466
   548
 let fun h t =
wenzelm@32429
   549
  case (term_of t) of
haftmann@36797
   550
   b$_$_ => if member (op aconv) bops b then Conv.binop_conv h t else cv env t
haftmann@36797
   551
 | u$_ => if member (op aconv) uops u then Conv.arg_conv h t else cv env t
wenzelm@23466
   552
 | _ => cv env t
wenzelm@23466
   553
 in h end;
wenzelm@23466
   554
wenzelm@23466
   555
fun integer_nnf_conv ctxt env =
wenzelm@23466
   556
 nnf_conv then_conv literals_conv [HOLogic.conj, HOLogic.disj] [] env (linearize_conv ctxt);
wenzelm@23466
   557
haftmann@36831
   558
val conv_ss = HOL_basic_ss addsimps
haftmann@36831
   559
  (@{thms simp_thms} @ take 4 @{thms ex_simps} @ [not_all, all_not_ex, @{thm ex_disj_distrib}]);
haftmann@36831
   560
haftmann@36831
   561
fun conv ctxt p =
haftmann@36831
   562
  Qelim.gen_qelim_conv (Simplifier.rewrite conv_ss) (Simplifier.rewrite presburger_ss) (Simplifier.rewrite conv_ss)
haftmann@36831
   563
    (cons o term_of) (OldTerm.term_frees (term_of p)) (linearize_conv ctxt) (integer_nnf_conv ctxt)
haftmann@36831
   564
    (cooperex_conv ctxt) p
haftmann@36831
   565
  handle CTERM s => raise COOPER "bad cterm"
haftmann@36831
   566
       | THM s => raise COOPER "bad thm"
haftmann@36831
   567
       | TYPE s => raise COOPER "bad type"
wenzelm@23466
   568
haftmann@36831
   569
fun add_bools t =
haftmann@36807
   570
  let
haftmann@36831
   571
    val ops = [@{term "op = :: int => _"}, @{term "op < :: int => _"}, @{term "op <= :: int => _"},
haftmann@36831
   572
      @{term "op &"}, @{term "op |"}, @{term "op -->"}, @{term "op = :: bool => _"},
haftmann@36831
   573
      @{term "Not"}, @{term "All :: (int => _) => _"},
haftmann@36831
   574
      @{term "Ex :: (int => _) => _"}, @{term "True"}, @{term "False"}];
haftmann@36831
   575
    val is_op = member (op =) ops;
haftmann@36831
   576
    val skip = not (fastype_of t = HOLogic.boolT)
haftmann@36807
   577
  in case t of
haftmann@36831
   578
      (l as f $ a) $ b => if skip orelse is_op f then add_bools b o add_bools l
haftmann@36831
   579
              else insert (op aconv) t
haftmann@36831
   580
    | f $ a => if skip orelse is_op f then add_bools a o add_bools f
haftmann@36831
   581
              else insert (op aconv) t
haftmann@36831
   582
    | Abs p => add_bools (snd (variant_abs p))
haftmann@36831
   583
    | _ => if skip orelse is_op t then I else insert (op aconv) t
haftmann@36807
   584
  end;
haftmann@36807
   585
haftmann@36832
   586
fun descend vs (abs as (_, xT, _)) =
haftmann@36832
   587
  let
haftmann@36832
   588
    val (xn', p') = variant_abs abs;
haftmann@36833
   589
  in ((xn', xT) :: vs, p') end;
haftmann@36832
   590
haftmann@36831
   591
local structure Proc = Cooper_Procedure in
haftmann@36831
   592
haftmann@36833
   593
fun num_of_term vs (Free vT) = Proc.Bound (find_index (fn vT' => vT' = vT) vs)
haftmann@36832
   594
  | num_of_term vs (Term.Bound i) = Proc.Bound i
haftmann@36832
   595
  | num_of_term vs @{term "0::int"} = Proc.C 0
haftmann@36832
   596
  | num_of_term vs @{term "1::int"} = Proc.C 1
haftmann@36832
   597
  | num_of_term vs (t as Const (@{const_name number_of}, _) $ _) =
haftmann@36832
   598
      Proc.C (dest_number t)
haftmann@36832
   599
  | num_of_term vs (Const (@{const_name Groups.uminus}, _) $ t') =
haftmann@36832
   600
      Proc.Neg (num_of_term vs t')
haftmann@36832
   601
  | num_of_term vs (Const (@{const_name Groups.plus}, _) $ t1 $ t2) =
haftmann@36832
   602
      Proc.Add (num_of_term vs t1, num_of_term vs t2)
haftmann@36832
   603
  | num_of_term vs (Const (@{const_name Groups.minus}, _) $ t1 $ t2) =
haftmann@36832
   604
      Proc.Sub (num_of_term vs t1, num_of_term vs t2)
haftmann@36832
   605
  | num_of_term vs (Const (@{const_name Groups.times}, _) $ t1 $ t2) =
haftmann@36832
   606
     (case perhaps_number t1
haftmann@36832
   607
       of SOME n => Proc.Mul (n, num_of_term vs t2)
haftmann@36832
   608
        | NONE => (case perhaps_number t2
haftmann@36832
   609
           of SOME n => Proc.Mul (n, num_of_term vs t1)
haftmann@36832
   610
            | NONE => raise COOPER "reification: unsupported kind of multiplication"))
haftmann@36832
   611
  | num_of_term _ _ = raise COOPER "reification: bad term";
haftmann@23689
   612
haftmann@36832
   613
fun fm_of_term ps vs (Const (@{const_name True}, _)) = Proc.T
haftmann@36832
   614
  | fm_of_term ps vs (Const (@{const_name False}, _)) = Proc.F
haftmann@36832
   615
  | fm_of_term ps vs (Const ("op &", _) $ t1 $ t2) =
haftmann@36832
   616
      Proc.And (fm_of_term ps vs t1, fm_of_term ps vs t2)
haftmann@36832
   617
  | fm_of_term ps vs (Const ("op |", _) $ t1 $ t2) =
haftmann@36832
   618
      Proc.Or (fm_of_term ps vs t1, fm_of_term ps vs t2)
haftmann@36832
   619
  | fm_of_term ps vs (Const ("op -->", _) $ t1 $ t2) =
haftmann@36832
   620
      Proc.Imp (fm_of_term ps vs t1, fm_of_term ps vs t2)
haftmann@36832
   621
  | fm_of_term ps vs (@{term "op = :: bool => _ "} $ t1 $ t2) =
haftmann@36832
   622
      Proc.Iff (fm_of_term ps vs t1, fm_of_term ps vs t2)
haftmann@36832
   623
  | fm_of_term ps vs (Const (@{const_name Not}, _) $ t') =
haftmann@36832
   624
      Proc.Not (fm_of_term ps vs t')
haftmann@36832
   625
  | fm_of_term ps vs (Const ("Ex", _) $ Abs abs) =
haftmann@36832
   626
      Proc.E (uncurry (fm_of_term ps) (descend vs abs))
haftmann@36832
   627
  | fm_of_term ps vs (Const ("All", _) $ Abs abs) =
haftmann@36832
   628
      Proc.A (uncurry (fm_of_term ps) (descend vs abs))
haftmann@36832
   629
  | fm_of_term ps vs (@{term "op = :: int => _"} $ t1 $ t2) =
haftmann@36832
   630
      Proc.Eq (Proc.Sub (num_of_term vs t1, num_of_term vs t2))
haftmann@36832
   631
  | fm_of_term ps vs (Const (@{const_name Orderings.less_eq}, _) $ t1 $ t2) =
haftmann@36832
   632
      Proc.Le (Proc.Sub (num_of_term vs t1, num_of_term vs t2))
haftmann@36832
   633
  | fm_of_term ps vs (Const (@{const_name Orderings.less}, _) $ t1 $ t2) =
haftmann@36832
   634
      Proc.Lt (Proc.Sub (num_of_term vs t1, num_of_term vs t2))
haftmann@36832
   635
  | fm_of_term ps vs (Const (@{const_name Rings.dvd}, _) $ t1 $ t2) =
haftmann@36832
   636
     (case perhaps_number t1
haftmann@36832
   637
       of SOME n => Proc.Dvd (n, num_of_term vs t2)
haftmann@36832
   638
        | NONE => raise COOPER "reification: unsupported dvd")
haftmann@36833
   639
  | fm_of_term ps vs t = let val n = find_index (fn t' => t aconv t') ps
haftmann@36833
   640
      in if n > 0 then Proc.Closed n else raise COOPER "reification: unknown term" end;
wenzelm@23466
   641
haftmann@36832
   642
fun term_of_num vs (Proc.C i) = HOLogic.mk_number HOLogic.intT i
haftmann@36833
   643
  | term_of_num vs (Proc.Bound n) = Free (nth vs n)
haftmann@36832
   644
  | term_of_num vs (Proc.Neg t') =
haftmann@36832
   645
      @{term "uminus :: int => _"} $ term_of_num vs t'
haftmann@36832
   646
  | term_of_num vs (Proc.Add (t1, t2)) =
haftmann@36832
   647
      @{term "op + :: int => _"} $ term_of_num vs t1 $ term_of_num vs t2
haftmann@36832
   648
  | term_of_num vs (Proc.Sub (t1, t2)) =
haftmann@36832
   649
      @{term "op - :: int => _"} $ term_of_num vs t1 $ term_of_num vs t2
haftmann@36832
   650
  | term_of_num vs (Proc.Mul (i, t2)) =
haftmann@36832
   651
      @{term "op * :: int => _"} $ HOLogic.mk_number HOLogic.intT i $ term_of_num vs t2
haftmann@36832
   652
  | term_of_num vs (Proc.Cn (n, i, t')) =
haftmann@36832
   653
      term_of_num vs (Proc.Add (Proc.Mul (i, Proc.Bound n), t'));
wenzelm@23466
   654
haftmann@36832
   655
fun term_of_fm ps vs Proc.T = HOLogic.true_const
haftmann@36832
   656
  | term_of_fm ps vs Proc.F = HOLogic.false_const
haftmann@36832
   657
  | term_of_fm ps vs (Proc.And (t1, t2)) = HOLogic.conj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
haftmann@36832
   658
  | term_of_fm ps vs (Proc.Or (t1, t2)) = HOLogic.disj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
haftmann@36832
   659
  | term_of_fm ps vs (Proc.Imp (t1, t2)) = HOLogic.imp $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
haftmann@36832
   660
  | term_of_fm ps vs (Proc.Iff (t1, t2)) = @{term "op = :: bool => _"} $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
haftmann@36832
   661
  | term_of_fm ps vs (Proc.Not t') = HOLogic.Not $ term_of_fm ps vs t'
haftmann@36832
   662
  | term_of_fm ps vs (Proc.Eq t') = @{term "op = :: int => _ "} $ term_of_num vs t'$ @{term "0::int"}
haftmann@36832
   663
  | term_of_fm ps vs (Proc.NEq t') = term_of_fm ps vs (Proc.Not (Proc.Eq t'))
haftmann@36832
   664
  | term_of_fm ps vs (Proc.Lt t') = @{term "op < :: int => _ "} $ term_of_num vs t' $ @{term "0::int"}
haftmann@36832
   665
  | term_of_fm ps vs (Proc.Le t') = @{term "op <= :: int => _ "} $ term_of_num vs t' $ @{term "0::int"}
haftmann@36832
   666
  | term_of_fm ps vs (Proc.Gt t') = @{term "op < :: int => _ "} $ @{term "0::int"} $ term_of_num vs t'
haftmann@36832
   667
  | term_of_fm ps vs (Proc.Ge t') = @{term "op <= :: int => _ "} $ @{term "0::int"} $ term_of_num vs t'
haftmann@36832
   668
  | term_of_fm ps vs (Proc.Dvd (i, t')) = @{term "op dvd :: int => _ "} $
haftmann@36832
   669
      HOLogic.mk_number HOLogic.intT i $ term_of_num vs t'
haftmann@36832
   670
  | term_of_fm ps vs (Proc.NDvd (i, t')) = term_of_fm ps vs (Proc.Not (Proc.Dvd (i, t')))
haftmann@36833
   671
  | term_of_fm ps vs (Proc.Closed n) = nth ps n
haftmann@36832
   672
  | term_of_fm ps vs (Proc.NClosed n) = term_of_fm ps vs (Proc.Not (Proc.Closed n));
wenzelm@23466
   673
haftmann@36833
   674
fun procedure t =
haftmann@23713
   675
  let
haftmann@36833
   676
    val vs = Term.add_frees t [];
haftmann@36833
   677
    val ps = add_bools t [];
haftmann@36833
   678
  in (term_of_fm ps vs o Proc.pa o fm_of_term ps vs) t end;
wenzelm@23466
   679
haftmann@36831
   680
end;
haftmann@36831
   681
haftmann@36833
   682
val (_, oracle) = Context.>>> (Context.map_theory_result (Thm.add_oracle (Binding.name "cooper",
haftmann@36833
   683
  (fn (ctxt, t) => (Thm.cterm_of (ProofContext.theory_of ctxt) o Logic.mk_equals o pairself HOLogic.mk_Trueprop)
haftmann@36833
   684
    (t, procedure t)))));
haftmann@36802
   685
haftmann@36802
   686
val comp_ss = HOL_ss addsimps @{thms semiring_norm};
haftmann@36802
   687
haftmann@36802
   688
fun strip_objimp ct =
haftmann@36802
   689
  (case Thm.term_of ct of
haftmann@36802
   690
    Const ("op -->", _) $ _ $ _ =>
haftmann@36802
   691
      let val (A, B) = Thm.dest_binop ct
haftmann@36802
   692
      in A :: strip_objimp B end
haftmann@36802
   693
  | _ => [ct]);
haftmann@36802
   694
haftmann@36802
   695
fun strip_objall ct = 
haftmann@36802
   696
 case term_of ct of 
haftmann@36802
   697
  Const ("All", _) $ Abs (xn,xT,p) => 
haftmann@36802
   698
   let val (a,(v,t')) = (apsnd (Thm.dest_abs (SOME xn)) o Thm.dest_comb) ct
haftmann@36802
   699
   in apfst (cons (a,v)) (strip_objall t')
haftmann@36802
   700
   end
haftmann@36802
   701
| _ => ([],ct);
haftmann@36802
   702
haftmann@36802
   703
local
haftmann@36802
   704
  val all_maxscope_ss = 
haftmann@36802
   705
     HOL_basic_ss addsimps map (fn th => th RS sym) @{thms "all_simps"}
haftmann@36802
   706
in
haftmann@36802
   707
fun thin_prems_tac P = simp_tac all_maxscope_ss THEN'
haftmann@36802
   708
  CSUBGOAL (fn (p', i) =>
haftmann@36802
   709
    let
haftmann@36802
   710
     val (qvs, p) = strip_objall (Thm.dest_arg p')
haftmann@36802
   711
     val (ps, c) = split_last (strip_objimp p)
haftmann@36802
   712
     val qs = filter P ps
haftmann@36802
   713
     val q = if P c then c else @{cterm "False"}
haftmann@36802
   714
     val ng = fold_rev (fn (a,v) => fn t => Thm.capply a (Thm.cabs v t)) qvs 
haftmann@36802
   715
         (fold_rev (fn p => fn q => Thm.capply (Thm.capply @{cterm "op -->"} p) q) qs q)
haftmann@36802
   716
     val g = Thm.capply (Thm.capply @{cterm "op ==>"} (Thm.capply @{cterm "Trueprop"} ng)) p'
haftmann@36802
   717
     val ntac = (case qs of [] => q aconvc @{cterm "False"}
haftmann@36802
   718
                         | _ => false)
haftmann@36802
   719
    in 
haftmann@36802
   720
    if ntac then no_tac
haftmann@36802
   721
      else rtac (Goal.prove_internal [] g (K (blast_tac HOL_cs 1))) i
haftmann@36802
   722
    end)
wenzelm@23466
   723
end;
haftmann@36802
   724
haftmann@36802
   725
local
haftmann@36802
   726
 fun isnum t = case t of 
haftmann@36802
   727
   Const(@{const_name Groups.zero},_) => true
haftmann@36802
   728
 | Const(@{const_name Groups.one},_) => true
haftmann@36802
   729
 | @{term "Suc"}$s => isnum s
haftmann@36802
   730
 | @{term "nat"}$s => isnum s
haftmann@36802
   731
 | @{term "int"}$s => isnum s
haftmann@36802
   732
 | Const(@{const_name Groups.uminus},_)$s => isnum s
haftmann@36802
   733
 | Const(@{const_name Groups.plus},_)$l$r => isnum l andalso isnum r
haftmann@36802
   734
 | Const(@{const_name Groups.times},_)$l$r => isnum l andalso isnum r
haftmann@36802
   735
 | Const(@{const_name Groups.minus},_)$l$r => isnum l andalso isnum r
haftmann@36802
   736
 | Const(@{const_name Power.power},_)$l$r => isnum l andalso isnum r
haftmann@36802
   737
 | Const(@{const_name Divides.mod},_)$l$r => isnum l andalso isnum r
haftmann@36802
   738
 | Const(@{const_name Divides.div},_)$l$r => isnum l andalso isnum r
haftmann@36831
   739
 | _ => is_number t orelse can HOLogic.dest_nat t
haftmann@36802
   740
haftmann@36802
   741
 fun ty cts t = 
haftmann@36802
   742
 if not (member (op =) [HOLogic.intT, HOLogic.natT, HOLogic.boolT] (typ_of (ctyp_of_term t))) then false 
haftmann@36802
   743
    else case term_of t of 
haftmann@36802
   744
      c$l$r => if member (op =) [@{term"op *::int => _"}, @{term"op *::nat => _"}] c
haftmann@36802
   745
               then not (isnum l orelse isnum r)
haftmann@36802
   746
               else not (member (op aconv) cts c)
haftmann@36802
   747
    | c$_ => not (member (op aconv) cts c)
haftmann@36802
   748
    | c => not (member (op aconv) cts c)
haftmann@36802
   749
haftmann@36802
   750
 val term_constants =
haftmann@36802
   751
  let fun h acc t = case t of
haftmann@36802
   752
    Const _ => insert (op aconv) t acc
haftmann@36802
   753
  | a$b => h (h acc a) b
haftmann@36802
   754
  | Abs (_,_,t) => h acc t
haftmann@36802
   755
  | _ => acc
haftmann@36802
   756
 in h [] end;
haftmann@36802
   757
in 
haftmann@36802
   758
fun is_relevant ctxt ct = 
haftmann@36802
   759
 subset (op aconv) (term_constants (term_of ct) , snd (get ctxt))
haftmann@36802
   760
 andalso forall (fn Free (_,T) => member (op =) [@{typ int}, @{typ nat}] T) (OldTerm.term_frees (term_of ct))
haftmann@36802
   761
 andalso forall (fn Var (_,T) => member (op =) [@{typ int}, @{typ nat}] T) (OldTerm.term_vars (term_of ct));
haftmann@36802
   762
haftmann@36802
   763
fun int_nat_terms ctxt ct =
haftmann@36802
   764
 let 
haftmann@36802
   765
  val cts = snd (get ctxt)
haftmann@36802
   766
  fun h acc t = if ty cts t then insert (op aconvc) t acc else
haftmann@36802
   767
   case (term_of t) of
haftmann@36802
   768
    _$_ => h (h acc (Thm.dest_arg t)) (Thm.dest_fun t)
haftmann@36802
   769
  | Abs(_,_,_) => Thm.dest_abs NONE t ||> h acc |> uncurry (remove (op aconvc))
haftmann@36802
   770
  | _ => acc
haftmann@36802
   771
 in h [] ct end
haftmann@36802
   772
end;
haftmann@36802
   773
haftmann@36802
   774
fun generalize_tac f = CSUBGOAL (fn (p, i) => PRIMITIVE (fn st =>
haftmann@36802
   775
 let 
haftmann@36802
   776
   fun all T = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "all"}
haftmann@36802
   777
   fun gen x t = Thm.capply (all (ctyp_of_term x)) (Thm.cabs x t)
haftmann@36802
   778
   val ts = sort (fn (a,b) => Term_Ord.fast_term_ord (term_of a, term_of b)) (f p)
haftmann@36802
   779
   val p' = fold_rev gen ts p
wenzelm@36945
   780
 in Thm.implies_intr p' (Thm.implies_elim st (fold Thm.forall_elim ts (Thm.assume p'))) end));
haftmann@36802
   781
haftmann@36802
   782
local
haftmann@36802
   783
val ss1 = comp_ss
haftmann@36802
   784
  addsimps @{thms simp_thms} @ [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}] 
haftmann@36802
   785
      @ map (fn r => r RS sym) 
haftmann@36802
   786
        [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
haftmann@36802
   787
         @{thm "zmult_int"}]
haftmann@36802
   788
    addsplits [@{thm "zdiff_int_split"}]
haftmann@36802
   789
haftmann@36802
   790
val ss2 = HOL_basic_ss
haftmann@36802
   791
  addsimps [@{thm "nat_0_le"}, @{thm "int_nat_number_of"},
haftmann@36802
   792
            @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"}, 
haftmann@36802
   793
            @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}, @{thm "Suc_eq_plus1"}]
haftmann@36802
   794
  addcongs [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
haftmann@36802
   795
val div_mod_ss = HOL_basic_ss addsimps @{thms simp_thms}
wenzelm@36945
   796
  @ map (Thm.symmetric o mk_meta_eq) 
haftmann@36802
   797
    [@{thm "dvd_eq_mod_eq_0"},
haftmann@36802
   798
     @{thm "mod_add_left_eq"}, @{thm "mod_add_right_eq"}, 
haftmann@36802
   799
     @{thm "mod_add_eq"}, @{thm "div_add1_eq"}, @{thm "zdiv_zadd1_eq"}]
haftmann@36802
   800
  @ [@{thm "mod_self"}, @{thm "zmod_self"}, @{thm "mod_by_0"}, 
haftmann@36802
   801
     @{thm "div_by_0"}, @{thm "DIVISION_BY_ZERO"} RS conjunct1, 
haftmann@36802
   802
     @{thm "DIVISION_BY_ZERO"} RS conjunct2, @{thm "zdiv_zero"}, @{thm "zmod_zero"}, 
haftmann@36802
   803
     @{thm "div_0"}, @{thm "mod_0"}, @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, 
haftmann@36802
   804
     @{thm "mod_1"}, @{thm "Suc_eq_plus1"}]
haftmann@36802
   805
  @ @{thms add_ac}
haftmann@36802
   806
 addsimprocs [cancel_div_mod_nat_proc, cancel_div_mod_int_proc]
haftmann@36802
   807
 val splits_ss = comp_ss addsimps [@{thm "mod_div_equality'"}] addsplits 
haftmann@36802
   808
     [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"}, 
haftmann@36802
   809
      @{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}]
haftmann@36802
   810
in
haftmann@36802
   811
fun nat_to_int_tac ctxt = 
haftmann@36802
   812
  simp_tac (Simplifier.context ctxt ss1) THEN_ALL_NEW
haftmann@36802
   813
  simp_tac (Simplifier.context ctxt ss2) THEN_ALL_NEW
haftmann@36802
   814
  simp_tac (Simplifier.context ctxt comp_ss);
haftmann@36802
   815
haftmann@36802
   816
fun div_mod_tac ctxt i = simp_tac (Simplifier.context ctxt div_mod_ss) i;
haftmann@36802
   817
fun splits_tac ctxt i = simp_tac (Simplifier.context ctxt splits_ss) i;
haftmann@36802
   818
end;
haftmann@36802
   819
haftmann@36804
   820
fun core_tac ctxt = CSUBGOAL (fn (p, i) =>
haftmann@36805
   821
   let
haftmann@36802
   822
    val cpth = 
haftmann@36802
   823
       if !quick_and_dirty 
haftmann@36805
   824
       then oracle (ctxt, Envir.beta_norm (Pattern.eta_long [] (term_of (Thm.dest_arg p))))
haftmann@36804
   825
       else Conv.arg_conv (conv ctxt) p
haftmann@36802
   826
    val p' = Thm.rhs_of cpth
wenzelm@36945
   827
    val th = Thm.implies_intr p' (Thm.equal_elim (Thm.symmetric cpth) (Thm.assume p'))
haftmann@36802
   828
   in rtac th i end
haftmann@36802
   829
   handle COOPER _ => no_tac);
haftmann@36802
   830
haftmann@36802
   831
fun finish_tac q = SUBGOAL (fn (_, i) =>
haftmann@36802
   832
  (if q then I else TRY) (rtac TrueI i));
haftmann@36802
   833
haftmann@36804
   834
fun tac elim add_ths del_ths ctxt =
haftmann@36802
   835
let val ss = Simplifier.context ctxt (fst (get ctxt)) delsimps del_ths addsimps add_ths
haftmann@36802
   836
    val aprems = Arith_Data.get_arith_facts ctxt
haftmann@36802
   837
in
haftmann@36802
   838
  Method.insert_tac aprems
haftmann@36802
   839
  THEN_ALL_NEW Object_Logic.full_atomize_tac
haftmann@36802
   840
  THEN_ALL_NEW CONVERSION Thm.eta_long_conversion
haftmann@36802
   841
  THEN_ALL_NEW simp_tac ss
haftmann@36802
   842
  THEN_ALL_NEW (TRY o generalize_tac (int_nat_terms ctxt))
haftmann@36802
   843
  THEN_ALL_NEW Object_Logic.full_atomize_tac
haftmann@36802
   844
  THEN_ALL_NEW (thin_prems_tac (is_relevant ctxt))
haftmann@36802
   845
  THEN_ALL_NEW Object_Logic.full_atomize_tac
haftmann@36802
   846
  THEN_ALL_NEW div_mod_tac ctxt
haftmann@36802
   847
  THEN_ALL_NEW splits_tac ctxt
haftmann@36802
   848
  THEN_ALL_NEW simp_tac ss
haftmann@36802
   849
  THEN_ALL_NEW CONVERSION Thm.eta_long_conversion
haftmann@36802
   850
  THEN_ALL_NEW nat_to_int_tac ctxt
haftmann@36804
   851
  THEN_ALL_NEW (core_tac ctxt)
haftmann@36802
   852
  THEN_ALL_NEW finish_tac elim
haftmann@36802
   853
end;
haftmann@36802
   854
haftmann@36804
   855
val method =
haftmann@36802
   856
  let
haftmann@36802
   857
    fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
haftmann@36802
   858
    fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
haftmann@36802
   859
    val addN = "add"
haftmann@36802
   860
    val delN = "del"
haftmann@36802
   861
    val elimN = "elim"
haftmann@36802
   862
    val any_keyword = keyword addN || keyword delN || simple_keyword elimN
haftmann@36802
   863
    val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
haftmann@36802
   864
  in
haftmann@36802
   865
    Scan.optional (simple_keyword elimN >> K false) true --
haftmann@36802
   866
    Scan.optional (keyword addN |-- thms) [] --
haftmann@36802
   867
    Scan.optional (keyword delN |-- thms) [] >>
haftmann@36802
   868
    (fn ((elim, add_ths), del_ths) => fn ctxt =>
haftmann@36804
   869
      SIMPLE_METHOD' (tac elim add_ths del_ths ctxt))
haftmann@36802
   870
  end;
haftmann@36802
   871
haftmann@36802
   872
haftmann@36802
   873
(* theory setup *)
haftmann@36802
   874
haftmann@36802
   875
local
haftmann@36802
   876
haftmann@36802
   877
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
haftmann@36802
   878
haftmann@36802
   879
val constsN = "consts";
haftmann@36802
   880
val any_keyword = keyword constsN
haftmann@36802
   881
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
haftmann@36802
   882
val terms = thms >> map (term_of o Drule.dest_term);
haftmann@36802
   883
haftmann@36802
   884
fun optional scan = Scan.optional scan [];
haftmann@36802
   885
haftmann@36802
   886
in
haftmann@36802
   887
haftmann@36802
   888
val setup =
haftmann@36802
   889
  Attrib.setup @{binding presburger}
haftmann@36802
   890
    ((Scan.lift (Args.$$$ "del") |-- optional (keyword constsN |-- terms)) >> del ||
haftmann@36802
   891
      optional (keyword constsN |-- terms) >> add) "data for Cooper's algorithm"
haftmann@36804
   892
  #> Arith_Data.add_tactic "Presburger arithmetic" (K (tac true [] []));
haftmann@36802
   893
haftmann@36802
   894
end;
haftmann@36802
   895
haftmann@36802
   896
end;