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