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