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