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