src/HOL/Tools/Qelim/cooper.ML
author haftmann
Mon May 10 13:58:18 2010 +0200 (2010-05-10)
changeset 36798 3981db162131
parent 36797 cb074cec7a30
child 36799 628fe06cbeff
permissions -rw-r--r--
less complex organization of cooper source code
haftmann@24584
     1
(*  Title:      HOL/Tools/Qelim/cooper.ML
wenzelm@23466
     2
    Author:     Amine Chaieb, TU Muenchen
wenzelm@23466
     3
*)
wenzelm@23466
     4
haftmann@36798
     5
signature COOPER_DATA =
haftmann@36798
     6
sig
haftmann@36798
     7
  type entry
haftmann@36798
     8
  val get: Proof.context -> entry
haftmann@36798
     9
  val del: term list -> attribute
haftmann@36798
    10
  val add: term list -> attribute 
haftmann@36798
    11
  val setup: theory -> theory
haftmann@36798
    12
end;
haftmann@36798
    13
wenzelm@23466
    14
signature COOPER =
haftmann@36701
    15
sig
haftmann@36798
    16
  val cooper_conv: Proof.context -> conv
wenzelm@23466
    17
  exception COOPER of string * exn
wenzelm@23466
    18
end;
wenzelm@23466
    19
haftmann@36798
    20
signature COOPER_REIFY =
haftmann@36798
    21
sig
haftmann@36798
    22
  val cooper_oracle: cterm -> cterm
haftmann@36798
    23
end;
haftmann@36798
    24
haftmann@36798
    25
structure CooperData : COOPER_DATA =
haftmann@36798
    26
struct
haftmann@36798
    27
haftmann@36798
    28
type entry = simpset * (term list);
haftmann@36798
    29
haftmann@36798
    30
val allowed_consts = 
haftmann@36798
    31
  [@{term "op + :: int => _"}, @{term "op + :: nat => _"},
haftmann@36798
    32
   @{term "op - :: int => _"}, @{term "op - :: nat => _"},
haftmann@36798
    33
   @{term "op * :: int => _"}, @{term "op * :: nat => _"},
haftmann@36798
    34
   @{term "op div :: int => _"}, @{term "op div :: nat => _"},
haftmann@36798
    35
   @{term "op mod :: int => _"}, @{term "op mod :: nat => _"},
haftmann@36798
    36
   @{term "Int.Bit0"}, @{term "Int.Bit1"},
haftmann@36798
    37
   @{term "op &"}, @{term "op |"}, @{term "op -->"}, 
haftmann@36798
    38
   @{term "op = :: int => _"}, @{term "op = :: nat => _"}, @{term "op = :: bool => _"},
haftmann@36798
    39
   @{term "op < :: int => _"}, @{term "op < :: nat => _"},
haftmann@36798
    40
   @{term "op <= :: int => _"}, @{term "op <= :: nat => _"},
haftmann@36798
    41
   @{term "op dvd :: int => _"}, @{term "op dvd :: nat => _"},
haftmann@36798
    42
   @{term "abs :: int => _"},
haftmann@36798
    43
   @{term "max :: int => _"}, @{term "max :: nat => _"},
haftmann@36798
    44
   @{term "min :: int => _"}, @{term "min :: nat => _"},
haftmann@36798
    45
   @{term "uminus :: int => _"}, (*@ {term "uminus :: nat => _"},*)
haftmann@36798
    46
   @{term "Not"}, @{term "Suc"},
haftmann@36798
    47
   @{term "Ex :: (int => _) => _"}, @{term "Ex :: (nat => _) => _"},
haftmann@36798
    48
   @{term "All :: (int => _) => _"}, @{term "All :: (nat => _) => _"},
haftmann@36798
    49
   @{term "nat"}, @{term "int"},
haftmann@36798
    50
   @{term "Int.Bit0"}, @{term "Int.Bit1"},
haftmann@36798
    51
   @{term "Int.Pls"}, @{term "Int.Min"},
haftmann@36798
    52
   @{term "Int.number_of :: int => int"}, @{term "Int.number_of :: int => nat"},
haftmann@36798
    53
   @{term "0::int"}, @{term "1::int"}, @{term "0::nat"}, @{term "1::nat"},
haftmann@36798
    54
   @{term "True"}, @{term "False"}];
haftmann@36798
    55
haftmann@36798
    56
structure Data = Generic_Data
haftmann@36798
    57
(
haftmann@36798
    58
  type T = simpset * term list;
haftmann@36798
    59
  val empty = (HOL_ss, allowed_consts);
haftmann@36798
    60
  val extend  = I;
haftmann@36798
    61
  fun merge ((ss1, ts1), (ss2, ts2)) =
haftmann@36798
    62
    (merge_ss (ss1, ss2), Library.merge (op aconv) (ts1, ts2));
haftmann@36798
    63
);
haftmann@36798
    64
haftmann@36798
    65
val get = Data.get o Context.Proof;
haftmann@36798
    66
haftmann@36798
    67
fun add ts = Thm.declaration_attribute (fn th => fn context => 
haftmann@36798
    68
  context |> Data.map (fn (ss,ts') => 
haftmann@36798
    69
     (ss addsimps [th], merge (op aconv) (ts',ts) ))) 
haftmann@36798
    70
haftmann@36798
    71
fun del ts = Thm.declaration_attribute (fn th => fn context => 
haftmann@36798
    72
  context |> Data.map (fn (ss,ts') => 
haftmann@36798
    73
     (ss delsimps [th], subtract (op aconv) ts' ts ))) 
haftmann@36798
    74
haftmann@36798
    75
haftmann@36798
    76
(* theory setup *)
haftmann@36798
    77
haftmann@36798
    78
local
haftmann@36798
    79
haftmann@36798
    80
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
haftmann@36798
    81
haftmann@36798
    82
val constsN = "consts";
haftmann@36798
    83
val any_keyword = keyword constsN
haftmann@36798
    84
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
haftmann@36798
    85
val terms = thms >> map (term_of o Drule.dest_term);
haftmann@36798
    86
haftmann@36798
    87
fun optional scan = Scan.optional scan [];
haftmann@36798
    88
haftmann@36798
    89
in
haftmann@36798
    90
haftmann@36798
    91
val setup =
haftmann@36798
    92
  Attrib.setup @{binding presburger}
haftmann@36798
    93
    ((Scan.lift (Args.$$$ "del") |-- optional (keyword constsN |-- terms)) >> del ||
haftmann@36798
    94
      optional (keyword constsN |-- terms) >> add) "Cooper data";
haftmann@36798
    95
haftmann@36798
    96
end;
haftmann@36798
    97
haftmann@36798
    98
end;
haftmann@36798
    99
wenzelm@23466
   100
structure Cooper: COOPER =
wenzelm@23466
   101
struct
haftmann@23689
   102
wenzelm@23466
   103
exception COOPER of string * exn;
wenzelm@27018
   104
fun simp_thms_conv ctxt =
wenzelm@35410
   105
  Simplifier.rewrite (Simplifier.context ctxt HOL_basic_ss addsimps @{thms simp_thms});
wenzelm@23484
   106
val FWD = Drule.implies_elim_list;
wenzelm@23466
   107
wenzelm@23466
   108
val true_tm = @{cterm "True"};
wenzelm@23466
   109
val false_tm = @{cterm "False"};
wenzelm@23466
   110
val zdvd1_eq = @{thm "zdvd1_eq"};
wenzelm@23466
   111
val presburger_ss = @{simpset} addsimps [zdvd1_eq];
wenzelm@30595
   112
val lin_ss = presburger_ss addsimps (@{thm dvd_eq_mod_eq_0} :: zdvd1_eq :: @{thms zadd_ac});
haftmann@23689
   113
wenzelm@23466
   114
val iT = HOLogic.intT
wenzelm@23466
   115
val bT = HOLogic.boolT;
wenzelm@23466
   116
val dest_numeral = HOLogic.dest_number #> snd;
wenzelm@23466
   117
wenzelm@32429
   118
val [miconj, midisj, mieq, mineq, milt, mile, migt, mige, midvd, mindvd, miP] =
wenzelm@23466
   119
    map(instantiate' [SOME @{ctyp "int"}] []) @{thms "minf"};
wenzelm@23466
   120
wenzelm@32429
   121
val [infDconj, infDdisj, infDdvd,infDndvd,infDP] =
wenzelm@23466
   122
    map(instantiate' [SOME @{ctyp "int"}] []) @{thms "inf_period"};
wenzelm@23466
   123
wenzelm@32429
   124
val [piconj, pidisj, pieq,pineq,pilt,pile,pigt,pige,pidvd,pindvd,piP] =
wenzelm@23466
   125
    map (instantiate' [SOME @{ctyp "int"}] []) @{thms "pinf"};
wenzelm@23466
   126
wenzelm@23466
   127
val [miP, piP] = map (instantiate' [SOME @{ctyp "bool"}] []) [miP, piP];
wenzelm@23466
   128
wenzelm@23466
   129
val infDP = instantiate' (map SOME [@{ctyp "int"}, @{ctyp "bool"}]) [] infDP;
wenzelm@23466
   130
wenzelm@32429
   131
val [[asetconj, asetdisj, aseteq, asetneq, asetlt, asetle,
wenzelm@23466
   132
      asetgt, asetge, asetdvd, asetndvd,asetP],
wenzelm@32429
   133
     [bsetconj, bsetdisj, bseteq, bsetneq, bsetlt, bsetle,
wenzelm@23466
   134
      bsetgt, bsetge, bsetdvd, bsetndvd,bsetP]]  = [@{thms "aset"}, @{thms "bset"}];
wenzelm@23466
   135
haftmann@36797
   136
val [cpmi, cppi] = [@{thm "cpmi"}, @{thm "cppi"}];
wenzelm@23466
   137
wenzelm@23466
   138
val unity_coeff_ex = instantiate' [SOME @{ctyp "int"}] [] @{thm "unity_coeff_ex"};
wenzelm@23466
   139
wenzelm@32429
   140
val [zdvd_mono,simp_from_to,all_not_ex] =
wenzelm@23466
   141
     [@{thm "zdvd_mono"}, @{thm "simp_from_to"}, @{thm "all_not_ex"}];
wenzelm@23466
   142
wenzelm@23466
   143
val [dvd_uminus, dvd_uminus'] = @{thms "uminus_dvd_conv"};
wenzelm@23466
   144
wenzelm@23466
   145
val eval_ss = presburger_ss addsimps [simp_from_to] delsimps [insert_iff,bex_triv];
wenzelm@23466
   146
val eval_conv = Simplifier.rewrite eval_ss;
wenzelm@23466
   147
haftmann@23689
   148
(* recognising cterm without moving to terms *)
wenzelm@23466
   149
wenzelm@32429
   150
datatype fm = And of cterm*cterm| Or of cterm*cterm| Eq of cterm | NEq of cterm
wenzelm@23466
   151
            | Lt of cterm | Le of cterm | Gt of cterm | Ge of cterm
wenzelm@23466
   152
            | Dvd of cterm*cterm | NDvd of cterm*cterm | Nox
wenzelm@23466
   153
wenzelm@32429
   154
fun whatis x ct =
wenzelm@32429
   155
( case (term_of ct) of
wenzelm@23466
   156
  Const("op &",_)$_$_ => And (Thm.dest_binop ct)
wenzelm@23466
   157
| Const ("op |",_)$_$_ => Or (Thm.dest_binop ct)
haftmann@36797
   158
| Const ("op =",_)$y$_ => if term_of x aconv y then Eq (Thm.dest_arg ct) else Nox
wenzelm@32429
   159
| Const (@{const_name Not},_) $ (Const ("op =",_)$y$_) =>
wenzelm@23466
   160
  if term_of x aconv y then NEq (funpow 2 Thm.dest_arg ct) else Nox
haftmann@35092
   161
| Const (@{const_name Orderings.less}, _) $ y$ z =>
wenzelm@32429
   162
   if term_of x aconv y then Lt (Thm.dest_arg ct)
wenzelm@23466
   163
   else if term_of x aconv z then Gt (Thm.dest_arg1 ct) else Nox
haftmann@35092
   164
| Const (@{const_name Orderings.less_eq}, _) $ y $ z =>
wenzelm@32429
   165
   if term_of x aconv y then Le (Thm.dest_arg ct)
wenzelm@23466
   166
   else if term_of x aconv z then Ge (Thm.dest_arg1 ct) else Nox
haftmann@35267
   167
| Const (@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$y$_) =>
wenzelm@32429
   168
   if term_of x aconv y then Dvd (Thm.dest_binop ct ||> Thm.dest_arg) else Nox
haftmann@35267
   169
| Const (@{const_name Not},_) $ (Const (@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$y$_)) =>
wenzelm@32429
   170
   if term_of x aconv y then
wenzelm@32429
   171
   NDvd (Thm.dest_binop (Thm.dest_arg ct) ||> Thm.dest_arg) else Nox
wenzelm@23466
   172
| _ => Nox)
wenzelm@32429
   173
  handle CTERM _ => Nox;
wenzelm@23466
   174
wenzelm@32429
   175
fun get_pmi_term t =
wenzelm@32429
   176
  let val (x,eq) =
wenzelm@23466
   177
     (Thm.dest_abs NONE o Thm.dest_arg o snd o Thm.dest_abs NONE o Thm.dest_arg)
wenzelm@23466
   178
        (Thm.dest_arg t)
wenzelm@23466
   179
in (Thm.cabs x o Thm.dest_arg o Thm.dest_arg) eq end;
wenzelm@23466
   180
wenzelm@23466
   181
val get_pmi = get_pmi_term o cprop_of;
wenzelm@23466
   182
wenzelm@32429
   183
val p_v' = @{cpat "?P' :: int => bool"};
wenzelm@23466
   184
val q_v' = @{cpat "?Q' :: int => bool"};
wenzelm@23466
   185
val p_v = @{cpat "?P:: int => bool"};
wenzelm@23466
   186
val q_v = @{cpat "?Q:: int => bool"};
wenzelm@23466
   187
wenzelm@32429
   188
fun myfwd (th1, th2, th3) p q
wenzelm@32429
   189
      [(th_1,th_2,th_3), (th_1',th_2',th_3')] =
wenzelm@32429
   190
  let
wenzelm@23466
   191
   val (mp', mq') = (get_pmi th_1, get_pmi th_1')
wenzelm@32429
   192
   val mi_th = FWD (instantiate ([],[(p_v,p),(q_v,q), (p_v',mp'),(q_v',mq')]) th1)
wenzelm@23466
   193
                   [th_1, th_1']
wenzelm@23466
   194
   val infD_th = FWD (instantiate ([],[(p_v,mp'), (q_v, mq')]) th3) [th_3,th_3']
wenzelm@23466
   195
   val set_th = FWD (instantiate ([],[(p_v,p), (q_v,q)]) th2) [th_2, th_2']
wenzelm@23466
   196
  in (mi_th, set_th, infD_th)
wenzelm@23466
   197
  end;
wenzelm@23466
   198
wenzelm@23466
   199
val inst' = fn cts => instantiate' [] (map SOME cts);
wenzelm@23466
   200
val infDTrue = instantiate' [] [SOME true_tm] infDP;
wenzelm@23466
   201
val infDFalse = instantiate' [] [SOME false_tm] infDP;
wenzelm@23466
   202
wenzelm@23466
   203
val cadd =  @{cterm "op + :: int => _"}
wenzelm@23466
   204
val cmulC =  @{cterm "op * :: int => _"}
wenzelm@23466
   205
val cminus =  @{cterm "op - :: int => _"}
haftmann@23689
   206
val cone =  @{cterm "1 :: int"}
haftmann@36797
   207
val [addC, mulC, subC] = map term_of [cadd, cmulC, cminus]
haftmann@23689
   208
val [zero, one] = [@{term "0 :: int"}, @{term "1 :: int"}];
wenzelm@23466
   209
wenzelm@32429
   210
val is_numeral = can dest_numeral;
wenzelm@23466
   211
wenzelm@32429
   212
fun numeral1 f n = HOLogic.mk_number iT (f (dest_numeral n));
wenzelm@23466
   213
fun numeral2 f m n = HOLogic.mk_number iT (f (dest_numeral m) (dest_numeral n));
wenzelm@23466
   214
wenzelm@32429
   215
val [minus1,plus1] =
wenzelm@23466
   216
    map (fn c => fn t => Thm.capply (Thm.capply c t) cone) [cminus,cadd];
wenzelm@23466
   217
wenzelm@32429
   218
fun decomp_pinf x dvd inS [aseteq, asetneq, asetlt, asetle,
wenzelm@23466
   219
                           asetgt, asetge,asetdvd,asetndvd,asetP,
wenzelm@23466
   220
                           infDdvd, infDndvd, asetconj,
wenzelm@23466
   221
                           asetdisj, infDconj, infDdisj] cp =
wenzelm@23466
   222
 case (whatis x cp) of
wenzelm@23466
   223
  And (p,q) => ([p,q], myfwd (piconj, asetconj, infDconj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   224
| Or (p,q) => ([p,q], myfwd (pidisj, asetdisj, infDdisj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   225
| Eq t => ([], K (inst' [t] pieq, FWD (inst' [t] aseteq) [inS (plus1 t)], infDFalse))
wenzelm@23466
   226
| NEq t => ([], K (inst' [t] pineq, FWD (inst' [t] asetneq) [inS t], infDTrue))
wenzelm@23466
   227
| Lt t => ([], K (inst' [t] pilt, FWD (inst' [t] asetlt) [inS t], infDFalse))
wenzelm@23466
   228
| Le t => ([], K (inst' [t] pile, FWD (inst' [t] asetle) [inS (plus1 t)], infDFalse))
wenzelm@23466
   229
| Gt t => ([], K (inst' [t] pigt, (inst' [t] asetgt), infDTrue))
wenzelm@23466
   230
| Ge t => ([], K (inst' [t] pige, (inst' [t] asetge), infDTrue))
wenzelm@32429
   231
| Dvd (d,s) =>
wenzelm@23466
   232
   ([],let val dd = dvd d
wenzelm@32429
   233
       in K (inst' [d,s] pidvd, FWD (inst' [d,s] asetdvd) [dd],FWD (inst' [d,s] infDdvd) [dd]) end)
wenzelm@23466
   234
| NDvd(d,s) => ([],let val dd = dvd d
wenzelm@32429
   235
        in K (inst' [d,s] pindvd, FWD (inst' [d,s] asetndvd) [dd], FWD (inst' [d,s] infDndvd) [dd]) end)
wenzelm@23466
   236
| _ => ([], K (inst' [cp] piP, inst' [cp] asetP, inst' [cp] infDP));
wenzelm@23466
   237
wenzelm@23466
   238
fun decomp_minf x dvd inS [bseteq,bsetneq,bsetlt, bsetle, bsetgt,
wenzelm@23466
   239
                           bsetge,bsetdvd,bsetndvd,bsetP,
wenzelm@23466
   240
                           infDdvd, infDndvd, bsetconj,
wenzelm@23466
   241
                           bsetdisj, infDconj, infDdisj] cp =
wenzelm@23466
   242
 case (whatis x cp) of
wenzelm@23466
   243
  And (p,q) => ([p,q], myfwd (miconj, bsetconj, infDconj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   244
| Or (p,q) => ([p,q], myfwd (midisj, bsetdisj, infDdisj) (Thm.cabs x p) (Thm.cabs x q))
wenzelm@23466
   245
| Eq t => ([], K (inst' [t] mieq, FWD (inst' [t] bseteq) [inS (minus1 t)], infDFalse))
wenzelm@23466
   246
| NEq t => ([], K (inst' [t] mineq, FWD (inst' [t] bsetneq) [inS t], infDTrue))
wenzelm@23466
   247
| Lt t => ([], K (inst' [t] milt, (inst' [t] bsetlt), infDTrue))
wenzelm@23466
   248
| Le t => ([], K (inst' [t] mile, (inst' [t] bsetle), infDTrue))
wenzelm@23466
   249
| Gt t => ([], K (inst' [t] migt, FWD (inst' [t] bsetgt) [inS t], infDFalse))
wenzelm@23466
   250
| Ge t => ([], K (inst' [t] mige,FWD (inst' [t] bsetge) [inS (minus1 t)], infDFalse))
wenzelm@23466
   251
| Dvd (d,s) => ([],let val dd = dvd d
wenzelm@32429
   252
        in K (inst' [d,s] midvd, FWD (inst' [d,s] bsetdvd) [dd] , FWD (inst' [d,s] infDdvd) [dd]) end)
wenzelm@23466
   253
| NDvd (d,s) => ([],let val dd = dvd d
wenzelm@32429
   254
        in K (inst' [d,s] mindvd, FWD (inst' [d,s] bsetndvd) [dd], FWD (inst' [d,s] infDndvd) [dd]) end)
wenzelm@23466
   255
| _ => ([], K (inst' [cp] miP, inst' [cp] bsetP, inst' [cp] infDP))
wenzelm@23466
   256
wenzelm@23466
   257
    (* Canonical linear form for terms, formulae etc.. *)
wenzelm@32429
   258
fun provelin ctxt t = Goal.prove ctxt [] [] t
haftmann@31101
   259
  (fn _ => EVERY [simp_tac lin_ss 1, TRY (Lin_Arith.tac ctxt 1)]);
wenzelm@32429
   260
fun linear_cmul 0 tm = zero
wenzelm@32429
   261
  | linear_cmul n tm = case tm of
haftmann@35267
   262
      Const (@{const_name Groups.plus}, _) $ a $ b => addC $ linear_cmul n a $ linear_cmul n b
haftmann@35267
   263
    | Const (@{const_name Groups.times}, _) $ c $ x => mulC $ numeral1 (fn m => n * m) c $ x
haftmann@35267
   264
    | Const (@{const_name Groups.minus}, _) $ a $ b => subC $ linear_cmul n a $ linear_cmul n b
haftmann@35267
   265
    | (m as Const (@{const_name Groups.uminus}, _)) $ a => m $ linear_cmul n a
haftmann@25768
   266
    | _ => numeral1 (fn m => n * m) tm;
wenzelm@32429
   267
fun earlier [] x y = false
wenzelm@32429
   268
  | earlier (h::t) x y =
wenzelm@32429
   269
    if h aconv y then false else if h aconv x then true else earlier t x y;
wenzelm@23466
   270
wenzelm@32429
   271
fun linear_add vars tm1 tm2 = case (tm1, tm2) of
haftmann@35267
   272
    (Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c1 $ x1) $ r1,
haftmann@35267
   273
    Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c2 $ x2) $ r2) =>
wenzelm@32429
   274
   if x1 = x2 then
wenzelm@33002
   275
     let val c = numeral2 Integer.add c1 c2
haftmann@25768
   276
      in if c = zero then linear_add vars r1 r2
haftmann@25768
   277
         else addC$(mulC$c$x1)$(linear_add vars r1 r2)
wenzelm@32429
   278
     end
haftmann@25768
   279
     else if earlier vars x1 x2 then addC $ (mulC $ c1 $ x1) $ linear_add vars r1 tm2
haftmann@25768
   280
   else addC $ (mulC $ c2 $ x2) $ linear_add vars tm1 r2
haftmann@35267
   281
 | (Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c1 $ x1) $ r1, _) =>
haftmann@25768
   282
      addC $ (mulC $ c1 $ x1) $ linear_add vars r1 tm2
haftmann@35267
   283
 | (_, Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c2 $ x2) $ r2) =>
haftmann@25768
   284
      addC $ (mulC $ c2 $ x2) $ linear_add vars tm1 r2
wenzelm@33002
   285
 | (_, _) => numeral2 Integer.add tm1 tm2;
wenzelm@32429
   286
wenzelm@32429
   287
fun linear_neg tm = linear_cmul ~1 tm;
wenzelm@32429
   288
fun linear_sub vars tm1 tm2 = linear_add vars tm1 (linear_neg tm2);
wenzelm@23466
   289
wenzelm@23466
   290
wenzelm@32429
   291
fun lint vars tm =  if is_numeral tm then tm  else case tm of
haftmann@35267
   292
  Const (@{const_name Groups.uminus}, _) $ t => linear_neg (lint vars t)
haftmann@35267
   293
| Const (@{const_name Groups.plus}, _) $ s $ t => linear_add vars (lint vars s) (lint vars t)
haftmann@35267
   294
| Const (@{const_name Groups.minus}, _) $ s $ t => linear_sub vars (lint vars s) (lint vars t)
haftmann@35267
   295
| Const (@{const_name Groups.times}, _) $ s $ t =>
wenzelm@32429
   296
  let val s' = lint vars s
wenzelm@32429
   297
      val t' = lint vars t
wenzelm@32429
   298
  in if is_numeral s' then (linear_cmul (dest_numeral s') t')
wenzelm@32429
   299
     else if is_numeral t' then (linear_cmul (dest_numeral t') s')
wenzelm@23466
   300
     else raise COOPER ("Cooper Failed", TERM ("lint: not linear",[tm]))
wenzelm@32429
   301
  end
haftmann@25768
   302
 | _ => addC $ (mulC $ one $ tm) $ zero;
wenzelm@23466
   303
haftmann@35092
   304
fun lin (vs as x::_) (Const (@{const_name Not}, _) $ (Const (@{const_name Orderings.less}, T) $ s $ t)) =
haftmann@35092
   305
    lin vs (Const (@{const_name Orderings.less_eq}, T) $ t $ s)
haftmann@35092
   306
  | lin (vs as x::_) (Const (@{const_name Not},_) $ (Const(@{const_name Orderings.less_eq}, T) $ s $ t)) =
haftmann@35092
   307
    lin vs (Const (@{const_name Orderings.less}, T) $ t $ s)
haftmann@25768
   308
  | lin vs (Const (@{const_name Not},T)$t) = Const (@{const_name Not},T)$ (lin vs t)
haftmann@35050
   309
  | lin (vs as x::_) (Const(@{const_name Rings.dvd},_)$d$t) =
haftmann@35050
   310
    HOLogic.mk_binrel @{const_name Rings.dvd} (numeral1 abs d, lint vs t)
wenzelm@32429
   311
  | lin (vs as x::_) ((b as Const("op =",_))$s$t) =
wenzelm@32429
   312
     (case lint vs (subC$t$s) of
wenzelm@32429
   313
      (t as a$(m$c$y)$r) =>
wenzelm@23466
   314
        if x <> y then b$zero$t
wenzelm@23466
   315
        else if dest_numeral c < 0 then b$(m$(numeral1 ~ c)$y)$r
wenzelm@23466
   316
        else b$(m$c$y)$(linear_neg r)
wenzelm@23466
   317
      | t => b$zero$t)
wenzelm@32429
   318
  | lin (vs as x::_) (b$s$t) =
wenzelm@32429
   319
     (case lint vs (subC$t$s) of
wenzelm@32429
   320
      (t as a$(m$c$y)$r) =>
wenzelm@23466
   321
        if x <> y then b$zero$t
wenzelm@23466
   322
        else if dest_numeral c < 0 then b$(m$(numeral1 ~ c)$y)$r
wenzelm@23466
   323
        else b$(linear_neg r)$(m$c$y)
wenzelm@23466
   324
      | t => b$zero$t)
wenzelm@23466
   325
  | lin vs fm = fm;
wenzelm@23466
   326
wenzelm@32429
   327
fun lint_conv ctxt vs ct =
wenzelm@23466
   328
let val t = term_of ct
wenzelm@23466
   329
in (provelin ctxt ((HOLogic.eq_const iT)$t$(lint vs t) |> HOLogic.mk_Trueprop))
wenzelm@23466
   330
             RS eq_reflection
wenzelm@23466
   331
end;
wenzelm@23466
   332
boehmes@32398
   333
fun is_intrel_type T = T = @{typ "int => int => bool"};
boehmes@32398
   334
boehmes@32398
   335
fun is_intrel (b$_$_) = is_intrel_type (fastype_of b)
boehmes@32398
   336
  | is_intrel (@{term "Not"}$(b$_$_)) = is_intrel_type (fastype_of b)
wenzelm@23466
   337
  | is_intrel _ = false;
wenzelm@32429
   338
haftmann@25768
   339
fun linearize_conv ctxt vs ct = case term_of ct of
haftmann@35050
   340
  Const(@{const_name Rings.dvd},_)$d$t =>
wenzelm@32429
   341
  let
haftmann@36797
   342
    val th = Conv.binop_conv (lint_conv ctxt vs) ct
wenzelm@23466
   343
    val (d',t') = Thm.dest_binop (Thm.rhs_of th)
wenzelm@23466
   344
    val (dt',tt') = (term_of d', term_of t')
wenzelm@32429
   345
  in if is_numeral dt' andalso is_numeral tt'
haftmann@36797
   346
     then Conv.fconv_rule (Conv.arg_conv (Simplifier.rewrite presburger_ss)) th
wenzelm@32429
   347
     else
wenzelm@32429
   348
     let
wenzelm@32429
   349
      val dth =
wenzelm@32429
   350
      ((if dest_numeral (term_of d') < 0 then
haftmann@36797
   351
          Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (lint_conv ctxt vs)))
wenzelm@23466
   352
                           (Thm.transitive th (inst' [d',t'] dvd_uminus))
wenzelm@23466
   353
        else th) handle TERM _ => th)
wenzelm@23466
   354
      val d'' = Thm.rhs_of dth |> Thm.dest_arg1
wenzelm@23466
   355
     in
wenzelm@32429
   356
      case tt' of
haftmann@35267
   357
        Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$_)$_ =>
wenzelm@23466
   358
        let val x = dest_numeral c
haftmann@36797
   359
        in if x < 0 then Conv.fconv_rule (Conv.arg_conv (Conv.arg_conv (lint_conv ctxt vs)))
wenzelm@23466
   360
                                       (Thm.transitive dth (inst' [d'',t'] dvd_uminus'))
wenzelm@23466
   361
        else dth end
wenzelm@23466
   362
      | _ => dth
wenzelm@23466
   363
     end
wenzelm@23466
   364
  end
haftmann@36797
   365
| Const (@{const_name Not},_)$(Const(@{const_name Rings.dvd},_)$_$_) => Conv.arg_conv (linearize_conv ctxt vs) ct
wenzelm@32429
   366
| t => if is_intrel t
wenzelm@23466
   367
      then (provelin ctxt ((HOLogic.eq_const bT)$t$(lin vs t) |> HOLogic.mk_Trueprop))
wenzelm@23466
   368
       RS eq_reflection
wenzelm@23466
   369
      else reflexive ct;
wenzelm@23466
   370
wenzelm@23466
   371
val dvdc = @{cterm "op dvd :: int => _"};
wenzelm@23466
   372
wenzelm@32429
   373
fun unify ctxt q =
wenzelm@23466
   374
 let
wenzelm@23466
   375
  val (e,(cx,p)) = q |> Thm.dest_comb ||> Thm.dest_abs NONE
wenzelm@32429
   376
  val x = term_of cx
wenzelm@24630
   377
  val ins = insert (op = : int * int -> bool)
wenzelm@32429
   378
  fun h (acc,dacc) t =
wenzelm@23466
   379
   case (term_of t) of
haftmann@35267
   380
    Const(s,_)$(Const(@{const_name Groups.times},_)$c$y)$ _ =>
haftmann@23881
   381
    if x aconv y andalso member (op =)
haftmann@35092
   382
      ["op =", @{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
wenzelm@23466
   383
    then (ins (dest_numeral c) acc,dacc) else (acc,dacc)
haftmann@35267
   384
  | Const(s,_)$_$(Const(@{const_name Groups.times},_)$c$y) =>
haftmann@23881
   385
    if x aconv y andalso member (op =)
haftmann@35092
   386
       [@{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
wenzelm@23466
   387
    then (ins (dest_numeral c) acc, dacc) else (acc,dacc)
haftmann@35267
   388
  | Const(@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$y)$_) =>
wenzelm@23466
   389
    if x aconv y then (acc,ins (dest_numeral c) dacc) else (acc,dacc)
wenzelm@23466
   390
  | Const("op &",_)$_$_ => h (h (acc,dacc) (Thm.dest_arg1 t)) (Thm.dest_arg t)
wenzelm@23466
   391
  | Const("op |",_)$_$_ => h (h (acc,dacc) (Thm.dest_arg1 t)) (Thm.dest_arg t)
haftmann@25768
   392
  | Const (@{const_name Not},_)$_ => h (acc,dacc) (Thm.dest_arg t)
wenzelm@23466
   393
  | _ => (acc, dacc)
wenzelm@23466
   394
  val (cs,ds) = h ([],[]) p
haftmann@33042
   395
  val l = Integer.lcms (union (op =) cs ds)
wenzelm@32429
   396
  fun cv k ct =
wenzelm@32429
   397
    let val (tm as b$s$t) = term_of ct
wenzelm@23466
   398
    in ((HOLogic.eq_const bT)$tm$(b$(linear_cmul k s)$(linear_cmul k t))
wenzelm@23466
   399
         |> HOLogic.mk_Trueprop |> provelin ctxt) RS eq_reflection end
wenzelm@32429
   400
  fun nzprop x =
wenzelm@32429
   401
   let
wenzelm@32429
   402
    val th =
wenzelm@32429
   403
     Simplifier.rewrite lin_ss
wenzelm@32429
   404
      (Thm.capply @{cterm Trueprop} (Thm.capply @{cterm "Not"}
wenzelm@32429
   405
           (Thm.capply (Thm.capply @{cterm "op = :: int => _"} (Numeral.mk_cnumber @{ctyp "int"} x))
haftmann@23689
   406
           @{cterm "0::int"})))
wenzelm@23466
   407
   in equal_elim (Thm.symmetric th) TrueI end;
wenzelm@32429
   408
  val notz =
wenzelm@32429
   409
    let val tab = fold Inttab.update
wenzelm@32429
   410
          (ds ~~ (map (fn x => nzprop (l div x)) ds)) Inttab.empty
wenzelm@32429
   411
    in
wenzelm@33035
   412
      fn ct => the (Inttab.lookup tab (ct |> term_of |> dest_numeral))
wenzelm@32429
   413
        handle Option =>
wenzelm@32429
   414
          (writeln ("noz: Theorems-Table contains no entry for " ^
wenzelm@32429
   415
              Syntax.string_of_term ctxt (Thm.term_of ct)); raise Option)
wenzelm@32429
   416
    end
wenzelm@32429
   417
  fun unit_conv t =
wenzelm@23466
   418
   case (term_of t) of
haftmann@36797
   419
   Const("op &",_)$_$_ => Conv.binop_conv unit_conv t
haftmann@36797
   420
  | Const("op |",_)$_$_ => Conv.binop_conv unit_conv t
haftmann@36797
   421
  | Const (@{const_name Not},_)$_ => Conv.arg_conv unit_conv t
haftmann@35267
   422
  | Const(s,_)$(Const(@{const_name Groups.times},_)$c$y)$ _ =>
haftmann@23881
   423
    if x=y andalso member (op =)
haftmann@35092
   424
      ["op =", @{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
wenzelm@24630
   425
    then cv (l div dest_numeral c) t else Thm.reflexive t
haftmann@35267
   426
  | Const(s,_)$_$(Const(@{const_name Groups.times},_)$c$y) =>
haftmann@23881
   427
    if x=y andalso member (op =)
haftmann@35092
   428
      [@{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
wenzelm@24630
   429
    then cv (l div dest_numeral c) t else Thm.reflexive t
haftmann@35267
   430
  | Const(@{const_name Rings.dvd},_)$d$(r as (Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$y)$_)) =>
wenzelm@32429
   431
    if x=y then
wenzelm@32429
   432
      let
wenzelm@24630
   433
       val k = l div dest_numeral c
wenzelm@23466
   434
       val kt = HOLogic.mk_number iT k
wenzelm@32429
   435
       val th1 = inst' [Thm.dest_arg1 t, Thm.dest_arg t]
wenzelm@23466
   436
             ((Thm.dest_arg t |> funpow 2 Thm.dest_arg1 |> notz) RS zdvd_mono)
wenzelm@23466
   437
       val (d',t') = (mulC$kt$d, mulC$kt$r)
wenzelm@23466
   438
       val thc = (provelin ctxt ((HOLogic.eq_const iT)$d'$(lint [] d') |> HOLogic.mk_Trueprop))
wenzelm@23466
   439
                   RS eq_reflection
wenzelm@23466
   440
       val tht = (provelin ctxt ((HOLogic.eq_const iT)$t'$(linear_cmul k r) |> HOLogic.mk_Trueprop))
wenzelm@23466
   441
                 RS eq_reflection
wenzelm@32429
   442
      in Thm.transitive th1 (Thm.combination (Drule.arg_cong_rule dvdc thc) tht) end
wenzelm@23466
   443
    else Thm.reflexive t
wenzelm@23466
   444
  | _ => Thm.reflexive t
wenzelm@23466
   445
  val uth = unit_conv p
haftmann@23689
   446
  val clt =  Numeral.mk_cnumber @{ctyp "int"} l
wenzelm@23466
   447
  val ltx = Thm.capply (Thm.capply cmulC clt) cx
wenzelm@23466
   448
  val th = Drule.arg_cong_rule e (Thm.abstract_rule (fst (dest_Free x )) cx uth)
wenzelm@23466
   449
  val th' = inst' [Thm.cabs ltx (Thm.rhs_of uth), clt] unity_coeff_ex
wenzelm@32429
   450
  val thf = transitive th
wenzelm@23466
   451
      (transitive (symmetric (beta_conversion true (cprop_of th' |> Thm.dest_arg1))) th')
wenzelm@23466
   452
  val (lth,rth) = Thm.dest_comb (cprop_of thf) |>> Thm.dest_arg |>> Thm.beta_conversion true
wenzelm@23466
   453
                  ||> beta_conversion true |>> Thm.symmetric
wenzelm@23466
   454
 in transitive (transitive lth thf) rth end;
wenzelm@23466
   455
wenzelm@23466
   456
wenzelm@23466
   457
val emptyIS = @{cterm "{}::int set"};
wenzelm@23466
   458
val insert_tm = @{cterm "insert :: int => _"};
wenzelm@23466
   459
fun mkISet cts = fold_rev (Thm.capply insert_tm #> Thm.capply) cts emptyIS;
wenzelm@23466
   460
val eqelem_imp_imp = (thm"eqelem_imp_iff") RS iffD1;
wenzelm@32429
   461
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
   462
                                      |> Thm.dest_abs NONE |> snd |> Thm.dest_fun |> Thm.dest_arg)
wenzelm@23466
   463
                      [asetP,bsetP];
wenzelm@23466
   464
wenzelm@23466
   465
val D_tm = @{cpat "?D::int"};
wenzelm@23466
   466
wenzelm@32429
   467
fun cooperex_conv ctxt vs q =
wenzelm@32429
   468
let
wenzelm@23466
   469
wenzelm@23466
   470
 val uth = unify ctxt q
wenzelm@23466
   471
 val (x,p) = Thm.dest_abs NONE (Thm.dest_arg (Thm.rhs_of uth))
wenzelm@23466
   472
 val ins = insert (op aconvc)
wenzelm@32429
   473
 fun h t (bacc,aacc,dacc) =
wenzelm@23466
   474
  case (whatis x t) of
wenzelm@23466
   475
    And (p,q) => h q (h p (bacc,aacc,dacc))
wenzelm@23466
   476
  | Or (p,q) => h q  (h p (bacc,aacc,dacc))
wenzelm@32429
   477
  | Eq t => (ins (minus1 t) bacc,
wenzelm@23466
   478
             ins (plus1 t) aacc,dacc)
wenzelm@32429
   479
  | NEq t => (ins t bacc,
wenzelm@23466
   480
              ins t aacc, dacc)
wenzelm@23466
   481
  | Lt t => (bacc, ins t aacc, dacc)
wenzelm@23466
   482
  | Le t => (bacc, ins (plus1 t) aacc,dacc)
wenzelm@23466
   483
  | Gt t => (ins t bacc, aacc,dacc)
wenzelm@23466
   484
  | Ge t => (ins (minus1 t) bacc, aacc,dacc)
haftmann@36797
   485
  | Dvd (d,_) => (bacc,aacc,insert (op =) (term_of d |> dest_numeral) dacc)
haftmann@36797
   486
  | NDvd (d,_) => (bacc,aacc,insert (op =) (term_of d|> dest_numeral) dacc)
wenzelm@23466
   487
  | _ => (bacc, aacc, dacc)
wenzelm@23466
   488
 val (b0,a0,ds) = h p ([],[],[])
wenzelm@24630
   489
 val d = Integer.lcms ds
wenzelm@23582
   490
 val cd = Numeral.mk_cnumber @{ctyp "int"} d
wenzelm@32429
   491
 fun divprop x =
wenzelm@32429
   492
   let
wenzelm@32429
   493
    val th =
wenzelm@32429
   494
     Simplifier.rewrite lin_ss
wenzelm@32429
   495
      (Thm.capply @{cterm Trueprop}
wenzelm@23582
   496
           (Thm.capply (Thm.capply dvdc (Numeral.mk_cnumber @{ctyp "int"} x)) cd))
wenzelm@23466
   497
   in equal_elim (Thm.symmetric th) TrueI end;
wenzelm@32429
   498
 val dvd =
wenzelm@32429
   499
   let val tab = fold Inttab.update (ds ~~ (map divprop ds)) Inttab.empty in
wenzelm@33035
   500
     fn ct => the (Inttab.lookup tab (term_of ct |> dest_numeral))
wenzelm@32429
   501
       handle Option =>
wenzelm@32429
   502
        (writeln ("dvd: Theorems-Table contains no entry for" ^
wenzelm@32429
   503
            Syntax.string_of_term ctxt (Thm.term_of ct)); raise Option)
wenzelm@32429
   504
   end
wenzelm@32429
   505
 val dp =
wenzelm@32429
   506
   let val th = Simplifier.rewrite lin_ss
wenzelm@32429
   507
      (Thm.capply @{cterm Trueprop}
wenzelm@23466
   508
           (Thm.capply (Thm.capply @{cterm "op < :: int => _"} @{cterm "0::int"}) cd))
wenzelm@23466
   509
   in equal_elim (Thm.symmetric th) TrueI end;
wenzelm@23466
   510
    (* A and B set *)
wenzelm@32429
   511
   local
wenzelm@23466
   512
     val insI1 = instantiate' [SOME @{ctyp "int"}] [] @{thm "insertI1"}
wenzelm@23466
   513
     val insI2 = instantiate' [SOME @{ctyp "int"}] [] @{thm "insertI2"}
wenzelm@23466
   514
   in
wenzelm@32429
   515
    fun provein x S =
wenzelm@23466
   516
     case term_of S of
haftmann@32264
   517
        Const(@{const_name Orderings.bot}, _) => error "Unexpected error in Cooper, please email Amine Chaieb"
wenzelm@32429
   518
      | Const(@{const_name insert}, _) $ y $ _ =>
wenzelm@23466
   519
         let val (cy,S') = Thm.dest_binop S
wenzelm@23466
   520
         in if term_of x aconv y then instantiate' [] [SOME x, SOME S'] insI1
wenzelm@32429
   521
         else implies_elim (instantiate' [] [SOME x, SOME S', SOME cy] insI2)
wenzelm@23466
   522
                           (provein x S')
wenzelm@23466
   523
         end
wenzelm@23466
   524
   end
wenzelm@32429
   525
wenzelm@23466
   526
 val al = map (lint vs o term_of) a0
wenzelm@23466
   527
 val bl = map (lint vs o term_of) b0
wenzelm@32429
   528
 val (sl,s0,f,abths,cpth) =
wenzelm@32429
   529
   if length (distinct (op aconv) bl) <= length (distinct (op aconv) al)
wenzelm@32429
   530
   then
wenzelm@23466
   531
    (bl,b0,decomp_minf,
wenzelm@32429
   532
     fn B => (map (fn th => implies_elim (Thm.instantiate ([],[(B_tm,B), (D_tm,cd)]) th) dp)
wenzelm@23466
   533
                     [bseteq,bsetneq,bsetlt, bsetle, bsetgt,bsetge])@
wenzelm@32429
   534
                   (map (Thm.instantiate ([],[(B_tm,B), (D_tm,cd)]))
wenzelm@23466
   535
                        [bsetdvd,bsetndvd,bsetP,infDdvd, infDndvd,bsetconj,
wenzelm@23466
   536
                         bsetdisj,infDconj, infDdisj]),
wenzelm@32429
   537
                       cpmi)
wenzelm@32429
   538
     else (al,a0,decomp_pinf,fn A =>
wenzelm@23466
   539
          (map (fn th => implies_elim (Thm.instantiate ([],[(A_tm,A), (D_tm,cd)]) th) dp)
wenzelm@23466
   540
                   [aseteq,asetneq,asetlt, asetle, asetgt,asetge])@
wenzelm@32429
   541
                   (map (Thm.instantiate ([],[(A_tm,A), (D_tm,cd)]))
wenzelm@23466
   542
                   [asetdvd,asetndvd, asetP, infDdvd, infDndvd,asetconj,
wenzelm@23466
   543
                         asetdisj,infDconj, infDdisj]),cppi)
wenzelm@32429
   544
 val cpth =
wenzelm@23466
   545
  let
wenzelm@32429
   546
   val sths = map (fn (tl,t0) =>
wenzelm@32429
   547
                      if tl = term_of t0
wenzelm@23466
   548
                      then instantiate' [SOME @{ctyp "int"}] [SOME t0] refl
wenzelm@32429
   549
                      else provelin ctxt ((HOLogic.eq_const iT)$tl$(term_of t0)
wenzelm@32429
   550
                                 |> HOLogic.mk_Trueprop))
wenzelm@23466
   551
                   (sl ~~ s0)
wenzelm@23466
   552
   val csl = distinct (op aconvc) (map (cprop_of #> Thm.dest_arg #> Thm.dest_arg1) sths)
wenzelm@23466
   553
   val S = mkISet csl
wenzelm@32429
   554
   val inStab = fold (fn ct => fn tab => Termtab.update (term_of ct, provein ct S) tab)
wenzelm@23466
   555
                    csl Termtab.empty
wenzelm@23466
   556
   val eqelem_th = instantiate' [SOME @{ctyp "int"}] [NONE,NONE, SOME S] eqelem_imp_imp
wenzelm@32429
   557
   val inS =
wenzelm@32429
   558
     let
wenzelm@23466
   559
      val tab = fold Termtab.update
wenzelm@32429
   560
        (map (fn eq =>
wenzelm@32429
   561
                let val (s,t) = cprop_of eq |> Thm.dest_arg |> Thm.dest_binop
wenzelm@32429
   562
                    val th = if term_of s = term_of t
wenzelm@33035
   563
                             then the (Termtab.lookup inStab (term_of s))
wenzelm@32429
   564
                             else FWD (instantiate' [] [SOME s, SOME t] eqelem_th)
wenzelm@33035
   565
                                [eq, the (Termtab.lookup inStab (term_of s))]
wenzelm@23466
   566
                 in (term_of t, th) end)
wenzelm@23466
   567
                  sths) Termtab.empty
wenzelm@32429
   568
        in
wenzelm@33035
   569
          fn ct => the (Termtab.lookup tab (term_of ct))
wenzelm@32429
   570
            handle Option =>
wenzelm@32429
   571
              (writeln ("inS: No theorem for " ^ Syntax.string_of_term ctxt (Thm.term_of ct));
wenzelm@32429
   572
                raise Option)
wenzelm@23466
   573
        end
wenzelm@23466
   574
       val (inf, nb, pd) = divide_and_conquer (f x dvd inS (abths S)) p
wenzelm@23466
   575
   in [dp, inf, nb, pd] MRS cpth
wenzelm@23466
   576
   end
wenzelm@23466
   577
 val cpth' = Thm.transitive uth (cpth RS eq_reflection)
wenzelm@27018
   578
in Thm.transitive cpth' ((simp_thms_conv ctxt then_conv eval_conv) (Thm.rhs_of cpth'))
wenzelm@23466
   579
end;
wenzelm@23466
   580
wenzelm@32429
   581
fun literals_conv bops uops env cv =
wenzelm@23466
   582
 let fun h t =
wenzelm@32429
   583
  case (term_of t) of
haftmann@36797
   584
   b$_$_ => if member (op aconv) bops b then Conv.binop_conv h t else cv env t
haftmann@36797
   585
 | u$_ => if member (op aconv) uops u then Conv.arg_conv h t else cv env t
wenzelm@23466
   586
 | _ => cv env t
wenzelm@23466
   587
 in h end;
wenzelm@23466
   588
wenzelm@23466
   589
fun integer_nnf_conv ctxt env =
wenzelm@23466
   590
 nnf_conv then_conv literals_conv [HOLogic.conj, HOLogic.disj] [] env (linearize_conv ctxt);
wenzelm@23466
   591
wenzelm@23466
   592
local
wenzelm@32429
   593
 val pcv = Simplifier.rewrite
wenzelm@35410
   594
     (HOL_basic_ss addsimps (@{thms simp_thms} @ List.take(@{thms ex_simps}, 4)
wenzelm@35410
   595
                      @ [not_all, all_not_ex, @{thm ex_disj_distrib}]))
wenzelm@23466
   596
 val postcv = Simplifier.rewrite presburger_ss
wenzelm@32429
   597
 fun conv ctxt p =
wenzelm@24298
   598
  let val _ = ()
wenzelm@23466
   599
  in
wenzelm@32429
   600
   Qelim.gen_qelim_conv pcv postcv pcv (cons o term_of)
wenzelm@32429
   601
      (OldTerm.term_frees (term_of p)) (linearize_conv ctxt) (integer_nnf_conv ctxt)
wenzelm@32429
   602
      (cooperex_conv ctxt) p
wenzelm@23466
   603
  end
wenzelm@23466
   604
  handle  CTERM s => raise COOPER ("Cooper Failed", CTERM s)
wenzelm@32429
   605
        | THM s => raise COOPER ("Cooper Failed", THM s)
wenzelm@32429
   606
        | TYPE s => raise COOPER ("Cooper Failed", TYPE s)
wenzelm@32429
   607
in val cooper_conv = conv
wenzelm@23466
   608
end;
wenzelm@23466
   609
end;
wenzelm@23466
   610
haftmann@36798
   611
structure Coopereif : COOPER_REIFY =
wenzelm@23466
   612
struct
wenzelm@23466
   613
haftmann@23713
   614
fun cooper s = raise Cooper.COOPER ("Cooper oracle failed", ERROR s);
haftmann@23713
   615
fun i_of_term vs t = case t
haftmann@23713
   616
 of Free (xn, xT) => (case AList.lookup (op aconv) vs t
haftmann@23713
   617
     of NONE   => cooper "Variable not found in the list!"
haftmann@36798
   618
      | SOME n => Cooper_Procedure.Bound n)
haftmann@36798
   619
  | @{term "0::int"} => Cooper_Procedure.C 0
haftmann@36798
   620
  | @{term "1::int"} => Cooper_Procedure.C 1
haftmann@36798
   621
  | Term.Bound i => Cooper_Procedure.Bound i
haftmann@36798
   622
  | Const(@{const_name Groups.uminus},_)$t' => Cooper_Procedure.Neg (i_of_term vs t')
haftmann@36798
   623
  | Const(@{const_name Groups.plus},_)$t1$t2 => Cooper_Procedure.Add (i_of_term vs t1,i_of_term vs t2)
haftmann@36798
   624
  | Const(@{const_name Groups.minus},_)$t1$t2 => Cooper_Procedure.Sub (i_of_term vs t1,i_of_term vs t2)
haftmann@35267
   625
  | Const(@{const_name Groups.times},_)$t1$t2 =>
haftmann@36798
   626
     (Cooper_Procedure.Mul (HOLogic.dest_number t1 |> snd, i_of_term vs t2)
wenzelm@32429
   627
    handle TERM _ =>
haftmann@36798
   628
       (Cooper_Procedure.Mul (HOLogic.dest_number t2 |> snd, i_of_term vs t1)
haftmann@23713
   629
        handle TERM _ => cooper "Reification: Unsupported kind of multiplication"))
haftmann@36798
   630
  | _ => (Cooper_Procedure.C (HOLogic.dest_number t |> snd)
haftmann@23713
   631
           handle TERM _ => cooper "Reification: unknown term");
haftmann@23689
   632
haftmann@23713
   633
fun qf_of_term ps vs t =  case t
haftmann@36798
   634
 of Const("True",_) => Cooper_Procedure.T
haftmann@36798
   635
  | Const("False",_) => Cooper_Procedure.F
haftmann@36798
   636
  | Const(@{const_name Orderings.less},_)$t1$t2 => Cooper_Procedure.Lt (Cooper_Procedure.Sub (i_of_term vs t1,i_of_term vs t2))
haftmann@36798
   637
  | Const(@{const_name Orderings.less_eq},_)$t1$t2 => Cooper_Procedure.Le (Cooper_Procedure.Sub(i_of_term vs t1,i_of_term vs t2))
haftmann@35050
   638
  | Const(@{const_name Rings.dvd},_)$t1$t2 =>
haftmann@36798
   639
      (Cooper_Procedure.Dvd(HOLogic.dest_number t1 |> snd, i_of_term vs t2) handle _ => cooper "Reification: unsupported dvd")  (* FIXME avoid handle _ *)
haftmann@36798
   640
  | @{term "op = :: int => _"}$t1$t2 => Cooper_Procedure.Eq (Cooper_Procedure.Sub (i_of_term vs t1,i_of_term vs t2))
haftmann@36798
   641
  | @{term "op = :: bool => _ "}$t1$t2 => Cooper_Procedure.Iff(qf_of_term ps vs t1,qf_of_term ps vs t2)
haftmann@36798
   642
  | Const("op &",_)$t1$t2 => Cooper_Procedure.And(qf_of_term ps vs t1,qf_of_term ps vs t2)
haftmann@36798
   643
  | Const("op |",_)$t1$t2 => Cooper_Procedure.Or(qf_of_term ps vs t1,qf_of_term ps vs t2)
haftmann@36798
   644
  | Const("op -->",_)$t1$t2 => Cooper_Procedure.Imp(qf_of_term ps vs t1,qf_of_term ps vs t2)
haftmann@36798
   645
  | Const (@{const_name Not},_)$t' => Cooper_Procedure.Not(qf_of_term ps vs t')
wenzelm@32429
   646
  | Const("Ex",_)$Abs(xn,xT,p) =>
haftmann@23713
   647
     let val (xn',p') = variant_abs (xn,xT,p)
haftmann@23713
   648
         val vs' = (Free (xn',xT), 0) :: (map (fn(v,n) => (v,1+ n)) vs)
haftmann@36798
   649
     in Cooper_Procedure.E (qf_of_term ps vs' p')
haftmann@23713
   650
     end
wenzelm@32429
   651
  | Const("All",_)$Abs(xn,xT,p) =>
haftmann@23713
   652
     let val (xn',p') = variant_abs (xn,xT,p)
haftmann@23713
   653
         val vs' = (Free (xn',xT), 0) :: (map (fn(v,n) => (v,1+ n)) vs)
haftmann@36798
   654
     in Cooper_Procedure.A (qf_of_term ps vs' p')
haftmann@23713
   655
     end
wenzelm@32429
   656
  | _ =>(case AList.lookup (op aconv) ps t of
haftmann@23713
   657
           NONE => cooper "Reification: unknown term!"
haftmann@36798
   658
         | SOME n => Cooper_Procedure.Closed n);
wenzelm@23466
   659
wenzelm@23466
   660
local
wenzelm@23466
   661
 val ops = [@{term "op &"}, @{term "op |"}, @{term "op -->"}, @{term "op = :: bool => _"},
wenzelm@32429
   662
             @{term "op = :: int => _"}, @{term "op < :: int => _"},
wenzelm@32429
   663
             @{term "op <= :: int => _"}, @{term "Not"}, @{term "All:: (int => _) => _"},
wenzelm@23466
   664
             @{term "Ex:: (int => _) => _"}, @{term "True"}, @{term "False"}]
wenzelm@23466
   665
fun ty t = Bool.not (fastype_of t = HOLogic.boolT)
wenzelm@23466
   666
in
wenzelm@23466
   667
fun term_bools acc t =
wenzelm@32429
   668
case t of
haftmann@36692
   669
    (l as f $ a) $ b => if ty t orelse member (op =) ops f then term_bools (term_bools acc l)b
wenzelm@23466
   670
            else insert (op aconv) t acc
haftmann@36692
   671
  | f $ a => if ty t orelse member (op =) ops f then term_bools (term_bools acc f) a
wenzelm@23466
   672
            else insert (op aconv) t acc
wenzelm@23466
   673
  | Abs p => term_bools acc (snd (variant_abs p))
haftmann@36692
   674
  | _ => if ty t orelse member (op =) ops t then acc else insert (op aconv) t acc
wenzelm@23466
   675
end;
wenzelm@32429
   676
wenzelm@23466
   677
fun myassoc2 l v =
wenzelm@23466
   678
    case l of
wenzelm@32429
   679
  [] => NONE
haftmann@23689
   680
      | (x,v')::xs => if v = v' then SOME x
wenzelm@32429
   681
          else myassoc2 xs v;
wenzelm@23466
   682
haftmann@23713
   683
fun term_of_i vs t = case t
haftmann@36798
   684
 of Cooper_Procedure.C i => HOLogic.mk_number HOLogic.intT i
haftmann@36798
   685
  | Cooper_Procedure.Bound n => the (myassoc2 vs n)
haftmann@36798
   686
  | Cooper_Procedure.Neg t' => @{term "uminus :: int => _"} $ term_of_i vs t'
haftmann@36798
   687
  | Cooper_Procedure.Add (t1, t2) => @{term "op + :: int => _"} $ term_of_i vs t1 $ term_of_i vs t2
haftmann@36798
   688
  | Cooper_Procedure.Sub (t1, t2) => @{term "op - :: int => _"} $ term_of_i vs t1 $ term_of_i vs t2
haftmann@36798
   689
  | Cooper_Procedure.Mul (i, t2) => @{term "op * :: int => _"} $
haftmann@23713
   690
      HOLogic.mk_number HOLogic.intT i $ term_of_i vs t2
haftmann@36798
   691
  | Cooper_Procedure.Cn (n, i, t') => term_of_i vs (Cooper_Procedure.Add (Cooper_Procedure.Mul (i, Cooper_Procedure.Bound n), t'));
wenzelm@23466
   692
wenzelm@32429
   693
fun term_of_qf ps vs t =
wenzelm@32429
   694
 case t of
haftmann@36798
   695
   Cooper_Procedure.T => HOLogic.true_const
haftmann@36798
   696
 | Cooper_Procedure.F => HOLogic.false_const
haftmann@36798
   697
 | Cooper_Procedure.Lt t' => @{term "op < :: int => _ "}$ term_of_i vs t'$ @{term "0::int"}
haftmann@36798
   698
 | Cooper_Procedure.Le t' => @{term "op <= :: int => _ "}$ term_of_i vs t' $ @{term "0::int"}
haftmann@36798
   699
 | Cooper_Procedure.Gt t' => @{term "op < :: int => _ "}$ @{term "0::int"}$ term_of_i vs t'
haftmann@36798
   700
 | Cooper_Procedure.Ge t' => @{term "op <= :: int => _ "}$ @{term "0::int"}$ term_of_i vs t'
haftmann@36798
   701
 | Cooper_Procedure.Eq t' => @{term "op = :: int => _ "}$ term_of_i vs t'$ @{term "0::int"}
haftmann@36798
   702
 | Cooper_Procedure.NEq t' => term_of_qf ps vs (Cooper_Procedure.Not (Cooper_Procedure.Eq t'))
haftmann@36798
   703
 | Cooper_Procedure.Dvd(i,t') => @{term "op dvd :: int => _ "} $
haftmann@23713
   704
    HOLogic.mk_number HOLogic.intT i $ term_of_i vs t'
haftmann@36798
   705
 | Cooper_Procedure.NDvd(i,t')=> term_of_qf ps vs (Cooper_Procedure.Not(Cooper_Procedure.Dvd(i,t')))
haftmann@36798
   706
 | Cooper_Procedure.Not t' => HOLogic.Not$(term_of_qf ps vs t')
haftmann@36798
   707
 | Cooper_Procedure.And(t1,t2) => HOLogic.conj$(term_of_qf ps vs t1)$(term_of_qf ps vs t2)
haftmann@36798
   708
 | Cooper_Procedure.Or(t1,t2) => HOLogic.disj$(term_of_qf ps vs t1)$(term_of_qf ps vs t2)
haftmann@36798
   709
 | Cooper_Procedure.Imp(t1,t2) => HOLogic.imp$(term_of_qf ps vs t1)$(term_of_qf ps vs t2)
haftmann@36798
   710
 | Cooper_Procedure.Iff(t1,t2) => @{term "op = :: bool => _"} $ term_of_qf ps vs t1 $ term_of_qf ps vs t2
haftmann@36798
   711
 | Cooper_Procedure.Closed n => the (myassoc2 ps n)
haftmann@36798
   712
 | Cooper_Procedure.NClosed n => term_of_qf ps vs (Cooper_Procedure.Not (Cooper_Procedure.Closed n))
haftmann@29787
   713
 | _ => cooper "If this is raised, Isabelle/HOL or code generator is inconsistent!";
wenzelm@23466
   714
wenzelm@28290
   715
fun cooper_oracle ct =
haftmann@23713
   716
  let
wenzelm@28290
   717
    val thy = Thm.theory_of_cterm ct;
wenzelm@28290
   718
    val t = Thm.term_of ct;
wenzelm@29265
   719
    val (vs, ps) = pairself (map_index swap) (OldTerm.term_frees t, term_bools [] t);
haftmann@23713
   720
  in
wenzelm@28290
   721
    Thm.cterm_of thy (Logic.mk_equals (HOLogic.mk_Trueprop t,
haftmann@36798
   722
      HOLogic.mk_Trueprop (term_of_qf ps vs (Cooper_Procedure.pa (qf_of_term ps vs t)))))
haftmann@23713
   723
  end;
wenzelm@23466
   724
wenzelm@23466
   725
end;