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