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