src/HOL/Tools/Qelim/cooper.ML
author wenzelm
Sun, 13 Dec 2015 21:56:15 +0100
changeset 61841 4d3527b94f2a
parent 61694 6571c78c9667
child 62348 9a5f43dac883
permissions -rw-r--r--
more general types Proof.method / context_tactic; proper context for Method.insert_tac; tuned;
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
    Author:     Amine Chaieb, TU Muenchen
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
     3
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
     4
Presburger arithmetic by Cooper's algorithm.
23466
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
36799
628fe06cbeff one structure is better than three
haftmann
parents: 36798
diff changeset
     7
signature COOPER =
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
     8
sig
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
     9
  type entry
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    10
  val get: Proof.context -> entry
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    11
  val del: term list -> attribute
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    12
  val add: term list -> attribute 
37117
59cee8807c29 eliminated various catch-all exception patterns, guessing at the concrete exeptions that are intended here;
wenzelm
parents: 36945
diff changeset
    13
  exception COOPER of string
36804
f4ad04780669 shorten names
haftmann
parents: 36802
diff changeset
    14
  val conv: Proof.context -> conv
f4ad04780669 shorten names
haftmann
parents: 36802
diff changeset
    15
  val tac: bool -> thm list -> thm list -> Proof.context -> int -> tactic
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    16
end;
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    17
36799
628fe06cbeff one structure is better than three
haftmann
parents: 36798
diff changeset
    18
structure Cooper: COOPER =
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    19
struct
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    20
36799
628fe06cbeff one structure is better than three
haftmann
parents: 36798
diff changeset
    21
type entry = simpset * term list;
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    22
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    23
val allowed_consts = 
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    24
  [@{term "op + :: int => _"}, @{term "op + :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    25
   @{term "op - :: int => _"}, @{term "op - :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    26
   @{term "op * :: int => _"}, @{term "op * :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    27
   @{term "op div :: int => _"}, @{term "op div :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    28
   @{term "op mod :: int => _"}, @{term "op mod :: nat => _"},
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    29
   @{term HOL.conj}, @{term HOL.disj}, @{term HOL.implies}, 
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    30
   @{term "op = :: int => _"}, @{term "op = :: nat => _"}, @{term "op = :: bool => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    31
   @{term "op < :: int => _"}, @{term "op < :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    32
   @{term "op <= :: int => _"}, @{term "op <= :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    33
   @{term "op dvd :: int => _"}, @{term "op dvd :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    34
   @{term "abs :: int => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    35
   @{term "max :: int => _"}, @{term "max :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    36
   @{term "min :: int => _"}, @{term "min :: nat => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    37
   @{term "uminus :: int => _"}, (*@ {term "uminus :: nat => _"},*)
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37117
diff changeset
    38
   @{term "Not"}, @{term Suc},
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    39
   @{term "Ex :: (int => _) => _"}, @{term "Ex :: (nat => _) => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    40
   @{term "All :: (int => _) => _"}, @{term "All :: (nat => _) => _"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    41
   @{term "nat"}, @{term "int"},
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46497
diff changeset
    42
   @{term "Num.One"}, @{term "Num.Bit0"}, @{term "Num.Bit1"},
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46497
diff changeset
    43
   @{term "Num.numeral :: num => int"}, @{term "Num.numeral :: num => nat"},
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    44
   @{term "0::int"}, @{term "1::int"}, @{term "0::nat"}, @{term "1::nat"},
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    45
   @{term "True"}, @{term "False"}];
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    46
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    47
structure Data = Generic_Data
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    48
(
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    49
  type T = simpset * term list;
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    50
  val empty = (HOL_ss, allowed_consts);
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 39159
diff changeset
    51
  val extend = I;
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    52
  fun merge ((ss1, ts1), (ss2, ts2)) =
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    53
    (merge_ss (ss1, ss2), Library.merge (op aconv) (ts1, ts2));
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    54
);
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    55
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    56
val get = Data.get o Context.Proof;
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    57
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    58
fun add ts = Thm.declaration_attribute (fn th => fn context => 
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    59
  context |> Data.map (fn (ss, ts') =>
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    60
     (simpset_map (Context.proof_of context) (fn ctxt => ctxt addsimps [th]) ss,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    61
      merge (op aconv) (ts', ts))))
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    62
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    63
fun del ts = Thm.declaration_attribute (fn th => fn context => 
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    64
  context |> Data.map (fn (ss, ts') =>
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    65
     (simpset_map (Context.proof_of context) (fn ctxt => ctxt delsimps [th]) ss,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    66
      subtract (op aconv) ts' ts)))
36798
3981db162131 less complex organization of cooper source code
haftmann
parents: 36797
diff changeset
    67
27018
b3e63f39fc0f proper context for simp_thms_conv;
wenzelm
parents: 26928
diff changeset
    68
fun simp_thms_conv ctxt =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    69
  Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps @{thms simp_thms});
23484
731658208196 made type conv pervasive;
wenzelm
parents: 23466
diff changeset
    70
val FWD = Drule.implies_elim_list;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    71
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    72
val true_tm = @{cterm "True"};
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    73
val false_tm = @{cterm "False"};
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    74
val zdvd1_eq = @{thm "zdvd1_eq"};
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    75
val presburger_ss = simpset_of (@{context} addsimps [zdvd1_eq]);
45196
78478d938cb8 inlined @{thms} (ML compile-time) allows to get rid of legacy zadd_ac as well (cf. 49e305100097);
wenzelm
parents: 44121
diff changeset
    76
val lin_ss =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
    77
  simpset_of (put_simpset presburger_ss @{context}
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 56245
diff changeset
    78
    addsimps (@{thm dvd_eq_mod_eq_0} :: zdvd1_eq :: @{thms ac_simps [where 'a=int]}));
23689
0410269099dc replaced code generator framework for reflected cooper
haftmann
parents: 23582
diff changeset
    79
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    80
val iT = HOLogic.intT
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    81
val bT = HOLogic.boolT;
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
    82
val dest_number = HOLogic.dest_number #> snd;
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
    83
val perhaps_number = try dest_number;
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
    84
val is_number = can dest_number;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    85
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
    86
val [miconj, midisj, mieq, mineq, milt, mile, migt, mige, midvd, mindvd, miP] =
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
    87
    map (Thm.instantiate' [SOME @{ctyp "int"}] []) @{thms "minf"};
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    88
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
    89
val [infDconj, infDdisj, infDdvd,infDndvd,infDP] =
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
    90
    map (Thm.instantiate' [SOME @{ctyp "int"}] []) @{thms "inf_period"};
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    91
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
    92
val [piconj, pidisj, pieq,pineq,pilt,pile,pigt,pige,pidvd,pindvd,piP] =
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
    93
    map (Thm.instantiate' [SOME @{ctyp "int"}] []) @{thms "pinf"};
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    94
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
    95
val [miP, piP] = map (Thm.instantiate' [SOME @{ctyp "bool"}] []) [miP, piP];
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    96
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
    97
val infDP = Thm.instantiate' (map SOME [@{ctyp "int"}, @{ctyp "bool"}]) [] infDP;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
    98
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
    99
val [[asetconj, asetdisj, aseteq, asetneq, asetlt, asetle,
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   100
      asetgt, asetge, asetdvd, asetndvd,asetP],
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   101
     [bsetconj, bsetdisj, bseteq, bsetneq, bsetlt, bsetle,
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   102
      bsetgt, bsetge, bsetdvd, bsetndvd,bsetP]]  = [@{thms "aset"}, @{thms "bset"}];
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   103
36797
cb074cec7a30 dropped unused bindings; avoid open (documents dependency on generated code more explicitly)
haftmann
parents: 36717
diff changeset
   104
val [cpmi, cppi] = [@{thm "cpmi"}, @{thm "cppi"}];
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   105
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   106
val unity_coeff_ex = Thm.instantiate' [SOME @{ctyp "int"}] [] @{thm "unity_coeff_ex"};
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   107
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   108
val [zdvd_mono,simp_from_to,all_not_ex] =
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   109
     [@{thm "zdvd_mono"}, @{thm "simp_from_to"}, @{thm "all_not_ex"}];
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   110
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   111
val [dvd_uminus, dvd_uminus'] = @{thms "uminus_dvd_conv"};
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   112
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   113
val eval_ss =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   114
  simpset_of (put_simpset presburger_ss @{context}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   115
    addsimps [simp_from_to] delsimps [insert_iff, bex_triv]);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   116
fun eval_conv ctxt = Simplifier.rewrite (put_simpset eval_ss ctxt);
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   117
23689
0410269099dc replaced code generator framework for reflected cooper
haftmann
parents: 23582
diff changeset
   118
(* recognising cterm without moving to terms *)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   119
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   120
datatype fm = And of cterm*cterm| Or of cterm*cterm| Eq of cterm | NEq of cterm
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   121
            | Lt of cterm | Le of cterm | Gt of cterm | Ge of cterm
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   122
            | Dvd of cterm*cterm | NDvd of cterm*cterm | Nox
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   123
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   124
fun whatis x ct =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   125
( case Thm.term_of ct of
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   126
  Const(@{const_name HOL.conj},_)$_$_ => And (Thm.dest_binop ct)
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   127
| Const (@{const_name HOL.disj},_)$_$_ => Or (Thm.dest_binop ct)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   128
| Const (@{const_name HOL.eq},_)$y$_ => if Thm.term_of x aconv y then Eq (Thm.dest_arg ct) else Nox
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38808
diff changeset
   129
| Const (@{const_name Not},_) $ (Const (@{const_name HOL.eq},_)$y$_) =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   130
  if Thm.term_of x aconv y then NEq (funpow 2 Thm.dest_arg ct) else Nox
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35050
diff changeset
   131
| Const (@{const_name Orderings.less}, _) $ y$ z =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   132
   if Thm.term_of x aconv y then Lt (Thm.dest_arg ct)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   133
   else if Thm.term_of x aconv z then Gt (Thm.dest_arg1 ct) else Nox
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35050
diff changeset
   134
| Const (@{const_name Orderings.less_eq}, _) $ y $ z =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   135
   if Thm.term_of x aconv y then Le (Thm.dest_arg ct)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   136
   else if Thm.term_of x aconv z then Ge (Thm.dest_arg1 ct) else Nox
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   137
| Const (@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$y$_) =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   138
   if Thm.term_of x aconv y then Dvd (Thm.dest_binop ct ||> Thm.dest_arg) else Nox
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   139
| Const (@{const_name Not},_) $ (Const (@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$y$_)) =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   140
   if Thm.term_of x aconv y then
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   141
   NDvd (Thm.dest_binop (Thm.dest_arg ct) ||> Thm.dest_arg) else Nox
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   142
| _ => Nox)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   143
  handle CTERM _ => Nox;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   144
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   145
fun get_pmi_term t =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   146
  let val (x,eq) =
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   147
     (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
   148
        (Thm.dest_arg t)
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   149
in (Thm.lambda x o Thm.dest_arg o Thm.dest_arg) eq end;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   150
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   151
val get_pmi = get_pmi_term o Thm.cprop_of;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   152
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   153
val p_v' = (("P'", 0), @{typ "int \<Rightarrow> bool"});
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   154
val q_v' = (("Q'", 0), @{typ "int \<Rightarrow> bool"});
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   155
val p_v = (("P", 0), @{typ "int \<Rightarrow> bool"});
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   156
val q_v = (("Q", 0), @{typ "int \<Rightarrow> bool"});
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   157
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   158
fun myfwd (th1, th2, th3) p q
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   159
      [(th_1,th_2,th_3), (th_1',th_2',th_3')] =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   160
  let
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   161
   val (mp', mq') = (get_pmi th_1, get_pmi th_1')
43333
2bdec7f430d3 renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents: 42793
diff changeset
   162
   val mi_th = FWD (Drule.instantiate_normalize ([],[(p_v,p),(q_v,q), (p_v',mp'),(q_v',mq')]) th1)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   163
                   [th_1, th_1']
43333
2bdec7f430d3 renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents: 42793
diff changeset
   164
   val infD_th = FWD (Drule.instantiate_normalize ([],[(p_v,mp'), (q_v, mq')]) th3) [th_3,th_3']
2bdec7f430d3 renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents: 42793
diff changeset
   165
   val set_th = FWD (Drule.instantiate_normalize ([],[(p_v,p), (q_v,q)]) th2) [th_2, th_2']
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   166
  in (mi_th, set_th, infD_th)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   167
  end;
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   168
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   169
val inst' = fn cts => Thm.instantiate' [] (map SOME cts);
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   170
val infDTrue = Thm.instantiate' [] [SOME true_tm] infDP;
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   171
val infDFalse = Thm.instantiate' [] [SOME false_tm] infDP;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   172
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   173
val cadd =  @{cterm "op + :: int => _"}
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   174
val cmulC =  @{cterm "op * :: int => _"}
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   175
val cminus =  @{cterm "op - :: int => _"}
23689
0410269099dc replaced code generator framework for reflected cooper
haftmann
parents: 23582
diff changeset
   176
val cone =  @{cterm "1 :: int"}
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   177
val [addC, mulC, subC] = map Thm.term_of [cadd, cmulC, cminus]
23689
0410269099dc replaced code generator framework for reflected cooper
haftmann
parents: 23582
diff changeset
   178
val [zero, one] = [@{term "0 :: int"}, @{term "1 :: int"}];
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   179
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   180
fun numeral1 f n = HOLogic.mk_number iT (f (dest_number n));
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   181
fun numeral2 f m n = HOLogic.mk_number iT (f (dest_number m) (dest_number n));
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   182
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   183
val [minus1,plus1] =
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   184
    map (fn c => fn t => Thm.apply (Thm.apply c t) cone) [cminus,cadd];
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   185
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   186
fun decomp_pinf x dvd inS [aseteq, asetneq, asetlt, asetle,
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   187
                           asetgt, asetge,asetdvd,asetndvd,asetP,
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   188
                           infDdvd, infDndvd, asetconj,
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   189
                           asetdisj, infDconj, infDdisj] cp =
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   190
 case (whatis x cp) of
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   191
  And (p,q) => ([p,q], myfwd (piconj, asetconj, infDconj) (Thm.lambda x p) (Thm.lambda x q))
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   192
| Or (p,q) => ([p,q], myfwd (pidisj, asetdisj, infDdisj) (Thm.lambda x p) (Thm.lambda x q))
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   193
| 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
   194
| 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
   195
| 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
   196
| 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
   197
| Gt t => ([], K (inst' [t] pigt, (inst' [t] asetgt), infDTrue))
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   198
| Ge t => ([], K (inst' [t] pige, (inst' [t] asetge), infDTrue))
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   199
| Dvd (d,s) =>
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   200
   ([],let val dd = dvd d
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   201
       in K (inst' [d,s] pidvd, FWD (inst' [d,s] asetdvd) [dd],FWD (inst' [d,s] infDdvd) [dd]) end)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   202
| NDvd(d,s) => ([],let val dd = dvd d
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   203
        in K (inst' [d,s] pindvd, FWD (inst' [d,s] asetndvd) [dd], FWD (inst' [d,s] infDndvd) [dd]) end)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   204
| _ => ([], K (inst' [cp] piP, inst' [cp] asetP, inst' [cp] infDP));
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 decomp_minf x dvd inS [bseteq,bsetneq,bsetlt, bsetle, bsetgt,
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   207
                           bsetge,bsetdvd,bsetndvd,bsetP,
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   208
                           infDdvd, infDndvd, bsetconj,
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   209
                           bsetdisj, infDconj, infDdisj] cp =
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   210
 case (whatis x cp) of
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   211
  And (p,q) => ([p,q], myfwd (miconj, bsetconj, infDconj) (Thm.lambda x p) (Thm.lambda x q))
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   212
| Or (p,q) => ([p,q], myfwd (midisj, bsetdisj, infDdisj) (Thm.lambda x p) (Thm.lambda x q))
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   213
| 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
   214
| 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
   215
| Lt t => ([], K (inst' [t] milt, (inst' [t] bsetlt), infDTrue))
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   216
| Le t => ([], K (inst' [t] mile, (inst' [t] bsetle), infDTrue))
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   217
| 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
   218
| 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
   219
| Dvd (d,s) => ([],let val dd = dvd d
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   220
        in K (inst' [d,s] midvd, FWD (inst' [d,s] bsetdvd) [dd] , FWD (inst' [d,s] infDdvd) [dd]) end)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   221
| NDvd (d,s) => ([],let val dd = dvd d
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   222
        in K (inst' [d,s] mindvd, FWD (inst' [d,s] bsetndvd) [dd], FWD (inst' [d,s] infDndvd) [dd]) end)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   223
| _ => ([], K (inst' [cp] miP, inst' [cp] bsetP, inst' [cp] infDP))
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   224
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   225
    (* Canonical linear form for terms, formulae etc.. *)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   226
fun provelin ctxt t = Goal.prove ctxt [] [] t
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   227
  (fn _ => EVERY [simp_tac (put_simpset lin_ss ctxt) 1, TRY (Lin_Arith.tac ctxt 1)]);
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   228
fun linear_cmul 0 tm = zero
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   229
  | linear_cmul n tm = case tm of
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   230
      Const (@{const_name Groups.plus}, _) $ a $ b => addC $ linear_cmul n a $ linear_cmul n b
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   231
    | Const (@{const_name Groups.times}, _) $ c $ x => mulC $ numeral1 (fn m => n * m) c $ x
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   232
    | Const (@{const_name Groups.minus}, _) $ a $ b => subC $ linear_cmul n a $ linear_cmul n b
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   233
    | (m as Const (@{const_name Groups.uminus}, _)) $ a => m $ linear_cmul n a
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   234
    | _ => numeral1 (fn m => n * m) tm;
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   235
fun earlier [] x y = false
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   236
  | earlier (h::t) x y =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   237
    if h aconv y then false else if h aconv x then true else earlier t x y;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   238
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   239
fun linear_add vars tm1 tm2 = case (tm1, tm2) of
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   240
    (Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c1 $ x1) $ r1,
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   241
    Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c2 $ x2) $ r2) =>
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   242
   if x1 = x2 then
33002
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 32603
diff changeset
   243
     let val c = numeral2 Integer.add c1 c2
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   244
      in if c = zero then linear_add vars r1 r2
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   245
         else addC$(mulC$c$x1)$(linear_add vars r1 r2)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   246
     end
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   247
     else if earlier vars x1 x2 then addC $ (mulC $ c1 $ x1) $ linear_add vars r1 tm2
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   248
   else addC $ (mulC $ c2 $ x2) $ linear_add vars tm1 r2
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   249
 | (Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c1 $ x1) $ r1, _) =>
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   250
      addC $ (mulC $ c1 $ x1) $ linear_add vars r1 tm2
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   251
 | (_, Const (@{const_name Groups.plus}, _) $ (Const (@{const_name Groups.times}, _) $ c2 $ x2) $ r2) =>
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   252
      addC $ (mulC $ c2 $ x2) $ linear_add vars tm1 r2
33002
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 32603
diff changeset
   253
 | (_, _) => numeral2 Integer.add tm1 tm2;
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   254
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   255
fun linear_neg tm = linear_cmul ~1 tm;
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   256
fun linear_sub vars tm1 tm2 = linear_add vars tm1 (linear_neg tm2);
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   257
36806
fc27b0465a4c stylized COOPER exception
haftmann
parents: 36805
diff changeset
   258
exception COOPER of string;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   259
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   260
fun lint vars tm =  if is_number tm then tm  else case tm of
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   261
  Const (@{const_name Groups.uminus}, _) $ t => linear_neg (lint vars t)
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   262
| Const (@{const_name Groups.plus}, _) $ s $ t => linear_add vars (lint vars s) (lint vars t)
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   263
| Const (@{const_name Groups.minus}, _) $ s $ t => linear_sub vars (lint vars s) (lint vars t)
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   264
| Const (@{const_name Groups.times}, _) $ s $ t =>
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   265
  let val s' = lint vars s
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   266
      val t' = lint vars t
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   267
  in case perhaps_number s' of SOME n => linear_cmul n t'
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   268
   | NONE => (case perhaps_number t' of SOME n => linear_cmul n s'
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   269
   | NONE => raise COOPER "lint: not linear")
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   270
  end
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   271
 | _ => addC $ (mulC $ one $ tm) $ zero;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   272
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   273
fun lin (vs as _::_) (Const (@{const_name Not}, _) $ (Const (@{const_name Orderings.less}, T) $ s $ t)) =
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35050
diff changeset
   274
    lin vs (Const (@{const_name Orderings.less_eq}, T) $ t $ s)
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   275
  | lin (vs as _::_) (Const (@{const_name Not},_) $ (Const(@{const_name Orderings.less_eq}, T) $ s $ t)) =
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35050
diff changeset
   276
    lin vs (Const (@{const_name Orderings.less}, T) $ t $ s)
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   277
  | lin vs (Const (@{const_name Not},T)$t) = Const (@{const_name Not},T)$ (lin vs t)
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   278
  | lin (vs as _::_) (Const(@{const_name Rings.dvd},_)$d$t) =
35050
9f841f20dca6 renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
haftmann
parents: 34974
diff changeset
   279
    HOLogic.mk_binrel @{const_name Rings.dvd} (numeral1 abs d, lint vs t)
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38808
diff changeset
   280
  | lin (vs as x::_) ((b as Const(@{const_name HOL.eq},_))$s$t) =
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   281
     (case lint vs (subC$t$s) of
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   282
      (t as _$(m$c$y)$r) =>
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   283
        if x <> y then b$zero$t
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   284
        else if dest_number c < 0 then b$(m$(numeral1 ~ c)$y)$r
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   285
        else b$(m$c$y)$(linear_neg r)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   286
      | t => b$zero$t)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   287
  | lin (vs as x::_) (b$s$t) =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   288
     (case lint vs (subC$t$s) of
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   289
      (t as _$(m$c$y)$r) =>
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   290
        if x <> y then b$zero$t
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   291
        else if dest_number c < 0 then b$(m$(numeral1 ~ c)$y)$r
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   292
        else b$(linear_neg r)$(m$c$y)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   293
      | t => b$zero$t)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   294
  | lin vs fm = fm;
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   295
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   296
fun lint_conv ctxt vs ct =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   297
let val t = Thm.term_of ct
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   298
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
   299
             RS eq_reflection
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   300
end;
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   301
32398
40a0760a00ea stricter condition for (binary) integer relation
boehmes
parents: 32264
diff changeset
   302
fun is_intrel_type T = T = @{typ "int => int => bool"};
40a0760a00ea stricter condition for (binary) integer relation
boehmes
parents: 32264
diff changeset
   303
40a0760a00ea stricter condition for (binary) integer relation
boehmes
parents: 32264
diff changeset
   304
fun is_intrel (b$_$_) = is_intrel_type (fastype_of b)
40a0760a00ea stricter condition for (binary) integer relation
boehmes
parents: 32264
diff changeset
   305
  | is_intrel (@{term "Not"}$(b$_$_)) = is_intrel_type (fastype_of b)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   306
  | is_intrel _ = false;
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   307
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   308
fun linearize_conv ctxt vs ct = case Thm.term_of ct of
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   309
  Const(@{const_name Rings.dvd},_)$_$_ =>
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   310
  let
36797
cb074cec7a30 dropped unused bindings; avoid open (documents dependency on generated code more explicitly)
haftmann
parents: 36717
diff changeset
   311
    val th = Conv.binop_conv (lint_conv ctxt vs) ct
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   312
    val (d',t') = Thm.dest_binop (Thm.rhs_of th)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   313
    val (dt',tt') = (Thm.term_of d', Thm.term_of t')
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   314
  in if is_number dt' andalso is_number tt'
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   315
     then Conv.fconv_rule (Conv.arg_conv (Simplifier.rewrite (put_simpset presburger_ss ctxt))) th
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   316
     else
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   317
     let
50321
df5553c4973f add check to Cooper's algorithm that left-hand of dvd is a numeral
hoelzl
parents: 47476
diff changeset
   318
       val dth =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   319
         case perhaps_number (Thm.term_of d') of
50321
df5553c4973f add check to Cooper's algorithm that left-hand of dvd is a numeral
hoelzl
parents: 47476
diff changeset
   320
           SOME d => if d < 0 then
df5553c4973f add check to Cooper's algorithm that left-hand of dvd is a numeral
hoelzl
parents: 47476
diff changeset
   321
             (Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (lint_conv ctxt vs)))
df5553c4973f add check to Cooper's algorithm that left-hand of dvd is a numeral
hoelzl
parents: 47476
diff changeset
   322
                              (Thm.transitive th (inst' [d',t'] dvd_uminus))
df5553c4973f add check to Cooper's algorithm that left-hand of dvd is a numeral
hoelzl
parents: 47476
diff changeset
   323
              handle TERM _ => th)
df5553c4973f add check to Cooper's algorithm that left-hand of dvd is a numeral
hoelzl
parents: 47476
diff changeset
   324
            else th
df5553c4973f add check to Cooper's algorithm that left-hand of dvd is a numeral
hoelzl
parents: 47476
diff changeset
   325
         | NONE => raise COOPER "linearize_conv: not linear"
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   326
      val d'' = Thm.rhs_of dth |> Thm.dest_arg1
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   327
     in
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   328
      case tt' of
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   329
        Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$_)$_ =>
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   330
        let val x = dest_number c
36797
cb074cec7a30 dropped unused bindings; avoid open (documents dependency on generated code more explicitly)
haftmann
parents: 36717
diff changeset
   331
        in if x < 0 then Conv.fconv_rule (Conv.arg_conv (Conv.arg_conv (lint_conv ctxt vs)))
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   332
                                       (Thm.transitive dth (inst' [d'',t'] dvd_uminus'))
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   333
        else dth end
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   334
      | _ => dth
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   335
     end
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   336
  end
36797
cb074cec7a30 dropped unused bindings; avoid open (documents dependency on generated code more explicitly)
haftmann
parents: 36717
diff changeset
   337
| Const (@{const_name Not},_)$(Const(@{const_name Rings.dvd},_)$_$_) => Conv.arg_conv (linearize_conv ctxt vs) ct
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   338
| t => if is_intrel t
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   339
      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
   340
       RS eq_reflection
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   341
      else Thm.reflexive ct;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   342
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   343
val dvdc = @{cterm "op dvd :: int => _"};
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   344
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   345
fun unify ctxt q =
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   346
 let
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   347
  val (e,(cx,p)) = q |> Thm.dest_comb ||> Thm.dest_abs NONE
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   348
  val x = Thm.term_of cx
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
   349
  val ins = insert (op = : int * int -> bool)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   350
  fun h (acc,dacc) t =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   351
   case Thm.term_of t of
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   352
    Const(s,_)$(Const(@{const_name Groups.times},_)$c$y)$ _ =>
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23713
diff changeset
   353
    if x aconv y andalso member (op =)
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38808
diff changeset
   354
      [@{const_name HOL.eq}, @{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   355
    then (ins (dest_number c) acc,dacc) else (acc,dacc)
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   356
  | Const(s,_)$_$(Const(@{const_name Groups.times},_)$c$y) =>
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23713
diff changeset
   357
    if x aconv y andalso member (op =)
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35050
diff changeset
   358
       [@{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   359
    then (ins (dest_number c) acc, dacc) else (acc,dacc)
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   360
  | Const(@{const_name Rings.dvd},_)$_$(Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$y)$_) =>
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   361
    if x aconv y then (acc,ins (dest_number c) dacc) else (acc,dacc)
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   362
  | Const(@{const_name HOL.conj},_)$_$_ => h (h (acc,dacc) (Thm.dest_arg1 t)) (Thm.dest_arg t)
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   363
  | Const(@{const_name HOL.disj},_)$_$_ => h (h (acc,dacc) (Thm.dest_arg1 t)) (Thm.dest_arg t)
25768
1c1ca4b20ec6 some more antiquotations
haftmann
parents: 24630
diff changeset
   364
  | Const (@{const_name Not},_)$_ => h (acc,dacc) (Thm.dest_arg t)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   365
  | _ => (acc, dacc)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   366
  val (cs,ds) = h ([],[]) p
33042
ddf1f03a9ad9 curried union as canonical list operation
haftmann
parents: 33039
diff changeset
   367
  val l = Integer.lcms (union (op =) cs ds)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   368
  fun cv k ct =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   369
    let val (tm as b$s$t) = Thm.term_of ct
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   370
    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
   371
         |> HOLogic.mk_Trueprop |> provelin ctxt) RS eq_reflection end
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   372
  fun nzprop x =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   373
   let
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   374
    val th =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   375
     Simplifier.rewrite (put_simpset lin_ss ctxt)
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   376
      (Thm.apply @{cterm Trueprop} (Thm.apply @{cterm "Not"}
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   377
           (Thm.apply (Thm.apply @{cterm "op = :: int => _"} (Numeral.mk_cnumber @{ctyp "int"} x))
23689
0410269099dc replaced code generator framework for reflected cooper
haftmann
parents: 23582
diff changeset
   378
           @{cterm "0::int"})))
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   379
   in Thm.equal_elim (Thm.symmetric th) TrueI end;
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   380
  val notz =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   381
    let val tab = fold Inttab.update
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   382
          (ds ~~ (map (fn x => nzprop (l div x)) ds)) Inttab.empty
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   383
    in
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   384
      fn ct => the (Inttab.lookup tab (ct |> Thm.term_of |> dest_number))
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51717
diff changeset
   385
        handle Option.Option =>
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   386
          (writeln ("noz: Theorems-Table contains no entry for " ^
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51717
diff changeset
   387
              Syntax.string_of_term ctxt (Thm.term_of ct)); raise Option.Option)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   388
    end
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   389
  fun unit_conv t =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   390
   case Thm.term_of t of
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   391
   Const(@{const_name HOL.conj},_)$_$_ => Conv.binop_conv unit_conv t
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   392
  | Const(@{const_name HOL.disj},_)$_$_ => Conv.binop_conv unit_conv t
36797
cb074cec7a30 dropped unused bindings; avoid open (documents dependency on generated code more explicitly)
haftmann
parents: 36717
diff changeset
   393
  | Const (@{const_name Not},_)$_ => Conv.arg_conv unit_conv t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   394
  | Const(s,_)$(Const(@{const_name Groups.times},_)$c$y)$ _ =>
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23713
diff changeset
   395
    if x=y andalso member (op =)
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38808
diff changeset
   396
      [@{const_name HOL.eq}, @{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   397
    then cv (l div dest_number c) t else Thm.reflexive t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   398
  | Const(s,_)$_$(Const(@{const_name Groups.times},_)$c$y) =>
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23713
diff changeset
   399
    if x=y andalso member (op =)
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35050
diff changeset
   400
      [@{const_name Orderings.less}, @{const_name Orderings.less_eq}] s
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   401
    then cv (l div dest_number c) t else Thm.reflexive t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   402
  | Const(@{const_name Rings.dvd},_)$d$(r as (Const(@{const_name Groups.plus},_)$(Const(@{const_name Groups.times},_)$c$y)$_)) =>
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   403
    if x=y then
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   404
      let
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   405
       val k = l div dest_number c
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   406
       val kt = HOLogic.mk_number iT k
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   407
       val th1 = inst' [Thm.dest_arg1 t, Thm.dest_arg t]
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   408
             ((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
   409
       val (d',t') = (mulC$kt$d, mulC$kt$r)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   410
       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
   411
                   RS eq_reflection
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   412
       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
   413
                 RS eq_reflection
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   414
      in Thm.transitive th1 (Thm.combination (Drule.arg_cong_rule dvdc thc) tht) end
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   415
    else Thm.reflexive t
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   416
  | _ => Thm.reflexive t
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   417
  val uth = unit_conv p
23689
0410269099dc replaced code generator framework for reflected cooper
haftmann
parents: 23582
diff changeset
   418
  val clt =  Numeral.mk_cnumber @{ctyp "int"} l
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   419
  val ltx = Thm.apply (Thm.apply cmulC clt) cx
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   420
  val th = Drule.arg_cong_rule e (Thm.abstract_rule (fst (dest_Free x )) cx uth)
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   421
  val th' = inst' [Thm.lambda ltx (Thm.rhs_of uth), clt] unity_coeff_ex
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   422
  val thf = Thm.transitive th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   423
      (Thm.transitive (Thm.symmetric (Thm.beta_conversion true (Thm.cprop_of th' |> Thm.dest_arg1))) th')
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   424
  val (lth,rth) = Thm.dest_comb (Thm.cprop_of thf) |>> Thm.dest_arg |>> Thm.beta_conversion true
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   425
                  ||> Thm.beta_conversion true |>> Thm.symmetric
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   426
 in Thm.transitive (Thm.transitive lth thf) rth end;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   427
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   428
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   429
val emptyIS = @{cterm "{}::int set"};
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   430
val insert_tm = @{cterm "insert :: int => _"};
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   431
fun mkISet cts = fold_rev (Thm.apply insert_tm #> Thm.apply) cts emptyIS;
39159
0dec18004e75 more antiquotations;
wenzelm
parents: 38864
diff changeset
   432
val eqelem_imp_imp = @{thm eqelem_imp_iff} RS iffD1;
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   433
val [A_v,B_v] =
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   434
  map (fn th => Thm.cprop_of th |> funpow 2 Thm.dest_arg
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   435
    |> Thm.dest_abs NONE |> snd |> Thm.dest_arg1 |> Thm.dest_arg
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   436
    |> Thm.dest_abs NONE |> snd |> Thm.dest_fun |> Thm.dest_arg
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   437
    |> Thm.term_of |> dest_Var) [asetP, bsetP];
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   438
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   439
val D_v = (("D", 0), @{typ int});
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   440
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   441
fun cooperex_conv ctxt vs q =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   442
let
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   443
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   444
 val uth = unify ctxt q
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   445
 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
   446
 val ins = insert (op aconvc)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   447
 fun h t (bacc,aacc,dacc) =
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   448
  case (whatis x t) of
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   449
    And (p,q) => h q (h p (bacc,aacc,dacc))
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   450
  | Or (p,q) => h q  (h p (bacc,aacc,dacc))
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   451
  | Eq t => (ins (minus1 t) bacc,
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   452
             ins (plus1 t) aacc,dacc)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   453
  | NEq t => (ins t bacc,
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   454
              ins t aacc, dacc)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   455
  | Lt t => (bacc, ins t aacc, dacc)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   456
  | Le t => (bacc, ins (plus1 t) aacc,dacc)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   457
  | Gt t => (ins t bacc, aacc,dacc)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   458
  | Ge t => (ins (minus1 t) bacc, aacc,dacc)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   459
  | Dvd (d,_) => (bacc,aacc,insert (op =) (Thm.term_of d |> dest_number) dacc)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   460
  | NDvd (d,_) => (bacc,aacc,insert (op =) (Thm.term_of d|> dest_number) dacc)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   461
  | _ => (bacc, aacc, dacc)
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   462
 val (b0,a0,ds) = h p ([],[],[])
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
   463
 val d = Integer.lcms ds
23582
94d0dd87cc24 avoid polymorphic equality;
wenzelm
parents: 23523
diff changeset
   464
 val cd = Numeral.mk_cnumber @{ctyp "int"} d
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   465
 fun divprop x =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   466
   let
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   467
    val th =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   468
     Simplifier.rewrite (put_simpset lin_ss ctxt)
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   469
      (Thm.apply @{cterm Trueprop}
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   470
           (Thm.apply (Thm.apply dvdc (Numeral.mk_cnumber @{ctyp "int"} x)) cd))
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   471
   in Thm.equal_elim (Thm.symmetric th) TrueI end;
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   472
 val dvd =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   473
   let val tab = fold Inttab.update (ds ~~ (map divprop ds)) Inttab.empty in
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   474
     fn ct => the (Inttab.lookup tab (Thm.term_of ct |> dest_number))
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51717
diff changeset
   475
       handle Option.Option =>
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   476
        (writeln ("dvd: Theorems-Table contains no entry for" ^
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51717
diff changeset
   477
            Syntax.string_of_term ctxt (Thm.term_of ct)); raise Option.Option)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   478
   end
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   479
 val dp =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   480
   let val th = Simplifier.rewrite (put_simpset lin_ss ctxt)
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   481
      (Thm.apply @{cterm Trueprop}
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   482
           (Thm.apply (Thm.apply @{cterm "op < :: int => _"} @{cterm "0::int"}) cd))
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   483
   in Thm.equal_elim (Thm.symmetric th) TrueI end;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   484
    (* A and B set *)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   485
   local
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   486
     val insI1 = Thm.instantiate' [SOME @{ctyp "int"}] [] @{thm "insertI1"}
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   487
     val insI2 = Thm.instantiate' [SOME @{ctyp "int"}] [] @{thm "insertI2"}
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   488
   in
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   489
    fun provein x S =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   490
     case Thm.term_of S of
32264
0be31453f698 Set.UNIV and Set.empty are mere abbreviations for top and bot
haftmann
parents: 31101
diff changeset
   491
        Const(@{const_name Orderings.bot}, _) => error "Unexpected error in Cooper, please email Amine Chaieb"
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   492
      | Const(@{const_name insert}, _) $ y $ _ =>
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   493
         let val (cy,S') = Thm.dest_binop S
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   494
         in if Thm.term_of x aconv y then Thm.instantiate' [] [SOME x, SOME S'] insI1
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   495
         else Thm.implies_elim (Thm.instantiate' [] [SOME x, SOME S', SOME cy] insI2)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   496
                           (provein x S')
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   497
         end
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   498
   end
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   499
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   500
 val al = map (lint vs o Thm.term_of) a0
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   501
 val bl = map (lint vs o Thm.term_of) b0
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   502
 val (sl,s0,f,abths,cpth) =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   503
   if length (distinct (op aconv) bl) <= length (distinct (op aconv) al)
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   504
   then
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   505
    (bl,b0,decomp_minf,
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   506
     fn B => (map (fn th => Thm.implies_elim (Thm.instantiate ([],[(B_v,B), (D_v,cd)]) th) dp)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   507
                     [bseteq,bsetneq,bsetlt, bsetle, bsetgt,bsetge])@
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   508
                   (map (Thm.instantiate ([],[(B_v,B), (D_v,cd)]))
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   509
                        [bsetdvd,bsetndvd,bsetP,infDdvd, infDndvd,bsetconj,
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   510
                         bsetdisj,infDconj, infDdisj]),
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   511
                       cpmi)
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   512
     else (al,a0,decomp_pinf,fn A =>
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   513
          (map (fn th => Thm.implies_elim (Thm.instantiate ([],[(A_v,A), (D_v,cd)]) th) dp)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   514
                   [aseteq,asetneq,asetlt, asetle, asetgt,asetge])@
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60352
diff changeset
   515
                   (map (Thm.instantiate ([],[(A_v,A), (D_v,cd)]))
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   516
                   [asetdvd,asetndvd, asetP, infDdvd, infDndvd,asetconj,
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   517
                         asetdisj,infDconj, infDdisj]),cppi)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   518
 val cpth =
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   519
  let
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   520
   val sths = map (fn (tl,t0) =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   521
                      if tl = Thm.term_of t0
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   522
                      then Thm.instantiate' [SOME @{ctyp "int"}] [SOME t0] refl
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   523
                      else provelin ctxt ((HOLogic.eq_const iT)$tl$(Thm.term_of t0)
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   524
                                 |> HOLogic.mk_Trueprop))
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   525
                   (sl ~~ s0)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   526
   val csl = distinct (op aconvc) (map (Thm.cprop_of #> Thm.dest_arg #> Thm.dest_arg1) sths)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   527
   val S = mkISet csl
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   528
   val inStab = fold (fn ct => fn tab => Termtab.update (Thm.term_of ct, provein ct S) tab)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   529
                    csl Termtab.empty
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   530
   val eqelem_th = Thm.instantiate' [SOME @{ctyp "int"}] [NONE,NONE, SOME S] eqelem_imp_imp
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   531
   val inS =
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   532
     let
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   533
      val tab = fold Termtab.update
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   534
        (map (fn eq =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   535
                let val (s,t) = Thm.cprop_of eq |> Thm.dest_arg |> Thm.dest_binop
59848
18c21d5c9138 clarified equality of formal entities;
wenzelm
parents: 59657
diff changeset
   536
                    val th =
18c21d5c9138 clarified equality of formal entities;
wenzelm
parents: 59657
diff changeset
   537
                      if s aconvc t
18c21d5c9138 clarified equality of formal entities;
wenzelm
parents: 59657
diff changeset
   538
                      then the (Termtab.lookup inStab (Thm.term_of s))
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   539
                      else FWD (Thm.instantiate' [] [SOME s, SOME t] eqelem_th)
59848
18c21d5c9138 clarified equality of formal entities;
wenzelm
parents: 59657
diff changeset
   540
                        [eq, the (Termtab.lookup inStab (Thm.term_of s))]
18c21d5c9138 clarified equality of formal entities;
wenzelm
parents: 59657
diff changeset
   541
                 in (Thm.term_of t, th) end) sths) Termtab.empty
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   542
        in
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   543
          fn ct => the (Termtab.lookup tab (Thm.term_of ct))
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51717
diff changeset
   544
            handle Option.Option =>
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   545
              (writeln ("inS: No theorem for " ^ Syntax.string_of_term ctxt (Thm.term_of ct));
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51717
diff changeset
   546
                raise Option.Option)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   547
        end
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   548
       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
   549
   in [dp, inf, nb, pd] MRS cpth
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   550
   end
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   551
 val cpth' = Thm.transitive uth (cpth RS eq_reflection)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   552
in Thm.transitive cpth' ((simp_thms_conv ctxt then_conv eval_conv ctxt) (Thm.rhs_of cpth'))
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   553
end;
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   554
32429
54758ca53fd6 modernized messages -- eliminated old Display.print_cterm;
wenzelm
parents: 32398
diff changeset
   555
fun literals_conv bops uops env cv =
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   556
 let fun h t =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   557
  case Thm.term_of t of
36797
cb074cec7a30 dropped unused bindings; avoid open (documents dependency on generated code more explicitly)
haftmann
parents: 36717
diff changeset
   558
   b$_$_ => if member (op aconv) bops b then Conv.binop_conv h t else cv env t
cb074cec7a30 dropped unused bindings; avoid open (documents dependency on generated code more explicitly)
haftmann
parents: 36717
diff changeset
   559
 | u$_ => if member (op aconv) uops u then Conv.arg_conv h t else cv env t
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   560
 | _ => cv env t
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   561
 in h end;
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   562
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   563
fun integer_nnf_conv ctxt env =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   564
  nnf_conv ctxt then_conv literals_conv [HOLogic.conj, HOLogic.disj] [] env (linearize_conv ctxt);
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   565
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   566
val conv_ss =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   567
  simpset_of (put_simpset HOL_basic_ss @{context}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   568
    addsimps (@{thms simp_thms} @ take 4 @{thms ex_simps} @
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   569
      [not_all, all_not_ex, @{thm ex_disj_distrib}]));
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   570
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   571
fun conv ctxt p =
61075
f6b0d827240e tuned -- avoid slightly odd @{cpat};
wenzelm
parents: 60801
diff changeset
   572
  Qelim.gen_qelim_conv ctxt
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   573
    (Simplifier.rewrite (put_simpset conv_ss ctxt))
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   574
    (Simplifier.rewrite (put_simpset presburger_ss ctxt))
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   575
    (Simplifier.rewrite (put_simpset conv_ss ctxt))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   576
    (cons o Thm.term_of) (Misc_Legacy.term_frees (Thm.term_of p))
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   577
    (linearize_conv ctxt) (integer_nnf_conv ctxt)
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   578
    (cooperex_conv ctxt) p
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   579
  handle CTERM _ => raise COOPER "bad cterm"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   580
       | THM _ => raise COOPER "bad thm"
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   581
       | TYPE _ => raise COOPER "bad type"
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   582
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   583
fun add_bools t =
36807
abcfc8372694 tuned; dropped strange myassoc2
haftmann
parents: 36806
diff changeset
   584
  let
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   585
    val ops = [@{term "op = :: int => _"}, @{term "op < :: int => _"}, @{term "op <= :: int => _"},
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   586
      @{term HOL.conj}, @{term HOL.disj}, @{term HOL.implies}, @{term "op = :: bool => _"},
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   587
      @{term "Not"}, @{term "All :: (int => _) => _"},
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   588
      @{term "Ex :: (int => _) => _"}, @{term "True"}, @{term "False"}];
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   589
    val is_op = member (op =) ops;
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   590
    val skip = not (fastype_of t = HOLogic.boolT)
36807
abcfc8372694 tuned; dropped strange myassoc2
haftmann
parents: 36806
diff changeset
   591
  in case t of
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   592
      (l as f $ a) $ b => if skip orelse is_op f then add_bools b o add_bools l
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   593
              else insert (op aconv) t
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   594
    | f $ a => if skip orelse is_op f then add_bools a o add_bools f
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   595
              else insert (op aconv) t
42284
326f57825e1a explicit structure Syntax_Trans;
wenzelm
parents: 41472
diff changeset
   596
    | Abs p => add_bools (snd (Syntax_Trans.variant_abs p))  (* FIXME !? *)
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   597
    | _ => if skip orelse is_op t then I else insert (op aconv) t
36807
abcfc8372694 tuned; dropped strange myassoc2
haftmann
parents: 36806
diff changeset
   598
  end;
abcfc8372694 tuned; dropped strange myassoc2
haftmann
parents: 36806
diff changeset
   599
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   600
fun descend vs (abs as (_, xT, _)) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   601
  let
42284
326f57825e1a explicit structure Syntax_Trans;
wenzelm
parents: 41472
diff changeset
   602
    val (xn', p') = Syntax_Trans.variant_abs abs;  (* FIXME !? *)
36833
9628f969d843 represent de-Bruin indices simply by position in list
haftmann
parents: 36832
diff changeset
   603
  in ((xn', xT) :: vs, p') end;
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   604
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   605
local structure Proc = Cooper_Procedure in
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   606
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   607
fun num_of_term vs (Free vT) = Proc.Bound (Proc.nat_of_integer (find_index (fn vT' => vT' = vT) vs))
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   608
  | num_of_term vs (Term.Bound i) = Proc.Bound (Proc.nat_of_integer i)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   609
  | num_of_term vs @{term "0::int"} = Proc.C (Proc.Int_of_integer 0)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   610
  | num_of_term vs @{term "1::int"} = Proc.C (Proc.Int_of_integer 1)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46497
diff changeset
   611
  | num_of_term vs (t as Const (@{const_name numeral}, _) $ _) =
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   612
      Proc.C (Proc.Int_of_integer (dest_number t))
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   613
  | num_of_term vs (Const (@{const_name Groups.uminus}, _) $ t') =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   614
      Proc.Neg (num_of_term vs t')
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   615
  | num_of_term vs (Const (@{const_name Groups.plus}, _) $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   616
      Proc.Add (num_of_term vs t1, num_of_term vs t2)
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   617
  | num_of_term vs (Const (@{const_name Groups.minus}, _) $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   618
      Proc.Sub (num_of_term vs t1, num_of_term vs t2)
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   619
  | num_of_term vs (Const (@{const_name Groups.times}, _) $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   620
     (case perhaps_number t1
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   621
       of SOME n => Proc.Mul (Proc.Int_of_integer n, num_of_term vs t2)
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   622
        | NONE => (case perhaps_number t2
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   623
           of SOME n => Proc.Mul (Proc.Int_of_integer n, num_of_term vs t1)
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   624
            | NONE => raise COOPER "reification: unsupported kind of multiplication"))
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   625
  | num_of_term _ _ = raise COOPER "reification: bad term";
23689
0410269099dc replaced code generator framework for reflected cooper
haftmann
parents: 23582
diff changeset
   626
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   627
fun fm_of_term ps vs (Const (@{const_name True}, _)) = Proc.T
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   628
  | fm_of_term ps vs (Const (@{const_name False}, _)) = Proc.F
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   629
  | fm_of_term ps vs (Const (@{const_name HOL.conj}, _) $ t1 $ t2) =
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   630
      Proc.And (fm_of_term ps vs t1, fm_of_term ps vs t2)
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   631
  | fm_of_term ps vs (Const (@{const_name HOL.disj}, _) $ t1 $ t2) =
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   632
      Proc.Or (fm_of_term ps vs t1, fm_of_term ps vs t2)
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38558
diff changeset
   633
  | fm_of_term ps vs (Const (@{const_name HOL.implies}, _) $ t1 $ t2) =
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   634
      Proc.Imp (fm_of_term ps vs t1, fm_of_term ps vs t2)
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   635
  | fm_of_term ps vs (@{term "op = :: bool => _ "} $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   636
      Proc.Iff (fm_of_term ps vs t1, fm_of_term ps vs t2)
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   637
  | fm_of_term ps vs (Const (@{const_name Not}, _) $ t') =
61128
8e5072cba671 formally regenerated
haftmann
parents: 61075
diff changeset
   638
      Proc.NOT (fm_of_term ps vs t')
38558
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
   639
  | fm_of_term ps vs (Const (@{const_name Ex}, _) $ Abs abs) =
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   640
      Proc.E (uncurry (fm_of_term ps) (descend vs abs))
38558
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
   641
  | fm_of_term ps vs (Const (@{const_name All}, _) $ Abs abs) =
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   642
      Proc.A (uncurry (fm_of_term ps) (descend vs abs))
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   643
  | fm_of_term ps vs (@{term "op = :: int => _"} $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   644
      Proc.Eq (Proc.Sub (num_of_term vs t1, num_of_term vs t2))
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   645
  | fm_of_term ps vs (Const (@{const_name Orderings.less_eq}, _) $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   646
      Proc.Le (Proc.Sub (num_of_term vs t1, num_of_term vs t2))
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   647
  | fm_of_term ps vs (Const (@{const_name Orderings.less}, _) $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   648
      Proc.Lt (Proc.Sub (num_of_term vs t1, num_of_term vs t2))
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   649
  | fm_of_term ps vs (Const (@{const_name Rings.dvd}, _) $ t1 $ t2) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   650
     (case perhaps_number t1
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   651
       of SOME n => Proc.Dvd (Proc.Int_of_integer n, num_of_term vs t2)
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   652
        | NONE => raise COOPER "reification: unsupported dvd")
36833
9628f969d843 represent de-Bruin indices simply by position in list
haftmann
parents: 36832
diff changeset
   653
  | fm_of_term ps vs t = let val n = find_index (fn t' => t aconv t') ps
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   654
      in if n > 0 then Proc.Closed (Proc.nat_of_integer n) else raise COOPER "reification: unknown term" end;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   655
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   656
fun term_of_num vs (Proc.C i) = HOLogic.mk_number HOLogic.intT (Proc.integer_of_int i)
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   657
  | term_of_num vs (Proc.Bound n) = Free (nth vs (Proc.integer_of_nat n))
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   658
  | term_of_num vs (Proc.Neg t') =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   659
      @{term "uminus :: int => _"} $ term_of_num vs t'
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   660
  | term_of_num vs (Proc.Add (t1, t2)) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   661
      @{term "op + :: int => _"} $ term_of_num vs t1 $ term_of_num vs t2
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   662
  | term_of_num vs (Proc.Sub (t1, t2)) =
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   663
      @{term "op - :: int => _"} $ term_of_num vs t1 $ term_of_num vs t2
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   664
  | term_of_num vs (Proc.Mul (i, t2)) =
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   665
      @{term "op * :: int => _"} $ HOLogic.mk_number HOLogic.intT (Proc.integer_of_int i) $ term_of_num vs t2
61128
8e5072cba671 formally regenerated
haftmann
parents: 61075
diff changeset
   666
  | term_of_num vs (Proc.CN (n, i, t')) =
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   667
      term_of_num vs (Proc.Add (Proc.Mul (i, Proc.Bound n), t'));
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   668
45740
132a3e1c0fe5 more antiquotations;
wenzelm
parents: 45620
diff changeset
   669
fun term_of_fm ps vs Proc.T = @{term True}
132a3e1c0fe5 more antiquotations;
wenzelm
parents: 45620
diff changeset
   670
  | term_of_fm ps vs Proc.F = @{term False}
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   671
  | term_of_fm ps vs (Proc.And (t1, t2)) = HOLogic.conj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   672
  | term_of_fm ps vs (Proc.Or (t1, t2)) = HOLogic.disj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   673
  | term_of_fm ps vs (Proc.Imp (t1, t2)) = HOLogic.imp $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   674
  | term_of_fm ps vs (Proc.Iff (t1, t2)) = @{term "op = :: bool => _"} $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
61128
8e5072cba671 formally regenerated
haftmann
parents: 61075
diff changeset
   675
  | term_of_fm ps vs (Proc.NOT t') = HOLogic.Not $ term_of_fm ps vs t'
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   676
  | term_of_fm ps vs (Proc.Eq t') = @{term "op = :: int => _ "} $ term_of_num vs t'$ @{term "0::int"}
61128
8e5072cba671 formally regenerated
haftmann
parents: 61075
diff changeset
   677
  | term_of_fm ps vs (Proc.NEq t') = term_of_fm ps vs (Proc.NOT (Proc.Eq t'))
36832
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   678
  | term_of_fm ps vs (Proc.Lt t') = @{term "op < :: int => _ "} $ term_of_num vs t' $ @{term "0::int"}
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   679
  | term_of_fm ps vs (Proc.Le t') = @{term "op <= :: int => _ "} $ term_of_num vs t' $ @{term "0::int"}
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   680
  | term_of_fm ps vs (Proc.Gt t') = @{term "op < :: int => _ "} $ @{term "0::int"} $ term_of_num vs t'
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   681
  | term_of_fm ps vs (Proc.Ge t') = @{term "op <= :: int => _ "} $ @{term "0::int"} $ term_of_num vs t'
e6078ef937df tuned reification functions
haftmann
parents: 36831
diff changeset
   682
  | term_of_fm ps vs (Proc.Dvd (i, t')) = @{term "op dvd :: int => _ "} $
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   683
      HOLogic.mk_number HOLogic.intT (Proc.integer_of_int i) $ term_of_num vs t'
61128
8e5072cba671 formally regenerated
haftmann
parents: 61075
diff changeset
   684
  | term_of_fm ps vs (Proc.NDvd (i, t')) = term_of_fm ps vs (Proc.NOT (Proc.Dvd (i, t')))
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   685
  | term_of_fm ps vs (Proc.Closed n) = nth ps (Proc.integer_of_nat n)
61128
8e5072cba671 formally regenerated
haftmann
parents: 61075
diff changeset
   686
  | term_of_fm ps vs (Proc.NClosed n) = term_of_fm ps vs (Proc.NOT (Proc.Closed n));
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   687
36833
9628f969d843 represent de-Bruin indices simply by position in list
haftmann
parents: 36832
diff changeset
   688
fun procedure t =
23713
db10cf19f1f8 now works with SML/NJ
haftmann
parents: 23689
diff changeset
   689
  let
36833
9628f969d843 represent de-Bruin indices simply by position in list
haftmann
parents: 36832
diff changeset
   690
    val vs = Term.add_frees t [];
9628f969d843 represent de-Bruin indices simply by position in list
haftmann
parents: 36832
diff changeset
   691
    val ps = add_bools t [];
9628f969d843 represent de-Bruin indices simply by position in list
haftmann
parents: 36832
diff changeset
   692
  in (term_of_fm ps vs o Proc.pa o fm_of_term ps vs) t end;
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   693
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   694
end;
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   695
38808
89ae86205739 more antiquotations;
wenzelm
parents: 38795
diff changeset
   696
val (_, oracle) = Context.>>> (Context.map_theory_result
89ae86205739 more antiquotations;
wenzelm
parents: 38795
diff changeset
   697
  (Thm.add_oracle (@{binding cooper},
89ae86205739 more antiquotations;
wenzelm
parents: 38795
diff changeset
   698
    (fn (ctxt, t) =>
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59586
diff changeset
   699
      (Thm.cterm_of ctxt o Logic.mk_equals o apply2 HOLogic.mk_Trueprop)
38808
89ae86205739 more antiquotations;
wenzelm
parents: 38795
diff changeset
   700
        (t, procedure t)))));
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   701
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   702
val comp_ss =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   703
  simpset_of (put_simpset HOL_ss @{context} addsimps @{thms semiring_norm});
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   704
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   705
fun strip_objimp ct =
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   706
  (case Thm.term_of ct of
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38558
diff changeset
   707
    Const (@{const_name HOL.implies}, _) $ _ $ _ =>
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   708
      let val (A, B) = Thm.dest_binop ct
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   709
      in A :: strip_objimp B end
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   710
  | _ => [ct]);
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   711
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   712
fun strip_objall ct = 
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   713
 case Thm.term_of ct of 
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 50321
diff changeset
   714
  Const (@{const_name All}, _) $ Abs (xn,_,_) => 
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   715
   let val (a,(v,t')) = (apsnd (Thm.dest_abs (SOME xn)) o Thm.dest_comb) ct
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   716
   in apfst (cons (a,v)) (strip_objall t')
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   717
   end
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   718
| _ => ([],ct);
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   719
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   720
local
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   721
  val all_maxscope_ss =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   722
    simpset_of (put_simpset HOL_basic_ss @{context}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   723
      addsimps map (fn th => th RS sym) @{thms "all_simps"})
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   724
in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   725
fun thin_prems_tac ctxt P =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   726
  simp_tac (put_simpset all_maxscope_ss ctxt) THEN'
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   727
  CSUBGOAL (fn (p', i) =>
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   728
    let
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   729
     val (qvs, p) = strip_objall (Thm.dest_arg p')
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   730
     val (ps, c) = split_last (strip_objimp p)
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   731
     val qs = filter P ps
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   732
     val q = if P c then c else @{cterm "False"}
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   733
     val ng = fold_rev (fn (a,v) => fn t => Thm.apply a (Thm.lambda v t)) qvs 
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   734
         (fold_rev (fn p => fn q => Thm.apply (Thm.apply @{cterm HOL.implies} p) q) qs q)
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 45740
diff changeset
   735
     val g = Thm.apply (Thm.apply @{cterm "op ==>"} (Thm.apply @{cterm "Trueprop"} ng)) p'
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   736
     val ntac = (case qs of [] => q aconvc @{cterm "False"}
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   737
                         | _ => false)
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   738
    in 
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   739
      if ntac then no_tac
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   740
      else
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   741
        (case try (fn () =>
54883
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 54742
diff changeset
   742
            Goal.prove_internal ctxt [] g (K (blast_tac (put_claset HOL_cs ctxt) 1))) () of
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   743
          NONE => no_tac
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   744
        | SOME r => resolve_tac ctxt [r] i)
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   745
    end)
23466
886655a150f6 moved quantifier elimination tools to Tools/Qelim/;
wenzelm
parents:
diff changeset
   746
end;
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   747
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   748
local
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   749
 fun isnum t = case t of 
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   750
   Const(@{const_name Groups.zero},_) => true
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   751
 | Const(@{const_name Groups.one},_) => true
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37117
diff changeset
   752
 | @{term Suc}$s => isnum s
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   753
 | @{term "nat"}$s => isnum s
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   754
 | @{term "int"}$s => isnum s
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   755
 | Const(@{const_name Groups.uminus},_)$s => isnum s
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   756
 | Const(@{const_name Groups.plus},_)$l$r => isnum l andalso isnum r
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   757
 | Const(@{const_name Groups.times},_)$l$r => isnum l andalso isnum r
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   758
 | Const(@{const_name Groups.minus},_)$l$r => isnum l andalso isnum r
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   759
 | Const(@{const_name Power.power},_)$l$r => isnum l andalso isnum r
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   760
 | Const(@{const_name Divides.mod},_)$l$r => isnum l andalso isnum r
60352
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59848
diff changeset
   761
 | Const(@{const_name Rings.divide},_)$l$r => isnum l andalso isnum r
36831
3037d6810fca tuned code; toward a tightended interface with generated code
haftmann
parents: 36807
diff changeset
   762
 | _ => is_number t orelse can HOLogic.dest_nat t
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   763
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   764
 fun ty cts t = 
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   765
  if not (member (op =) [HOLogic.intT, HOLogic.natT, HOLogic.boolT] (Thm.typ_of_cterm t))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   766
  then false 
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   767
  else case Thm.term_of t of 
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   768
    c$l$r => if member (op =) [@{term"op *::int => _"}, @{term"op *::nat => _"}] c
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   769
             then not (isnum l orelse isnum r)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   770
             else not (member (op aconv) cts c)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   771
  | c$_ => not (member (op aconv) cts c)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   772
  | c => not (member (op aconv) cts c)
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   773
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   774
 val term_constants =
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   775
  let fun h acc t = case t of
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   776
    Const _ => insert (op aconv) t acc
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   777
  | a$b => h (h acc a) b
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   778
  | Abs (_,_,t) => h acc t
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   779
  | _ => acc
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   780
 in h [] end;
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   781
in 
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   782
fun is_relevant ctxt ct = 
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   783
 subset (op aconv) (term_constants (Thm.term_of ct), snd (get ctxt))
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   784
 andalso
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   785
  forall (fn Free (_, T) => member (op =) [@{typ int}, @{typ nat}] T)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   786
    (Misc_Legacy.term_frees (Thm.term_of ct))
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   787
 andalso
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   788
  forall (fn Var (_, T) => member (op =) [@{typ int}, @{typ nat}] T)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   789
    (Misc_Legacy.term_vars (Thm.term_of ct));
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   790
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   791
fun int_nat_terms ctxt ct =
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   792
 let 
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   793
  val cts = snd (get ctxt)
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   794
  fun h acc t = if ty cts t then insert (op aconvc) t acc else
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   795
   case Thm.term_of t of
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   796
    _$_ => h (h acc (Thm.dest_arg t)) (Thm.dest_fun t)
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   797
  | Abs(_,_,_) => Thm.dest_abs NONE t ||> h acc |> uncurry (remove (op aconvc))
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   798
  | _ => acc
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   799
 in h [] ct end
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   800
end;
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   801
61075
f6b0d827240e tuned -- avoid slightly odd @{cpat};
wenzelm
parents: 60801
diff changeset
   802
fun generalize_tac ctxt f = CSUBGOAL (fn (p, _) => PRIMITIVE (fn st =>
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   803
 let 
61075
f6b0d827240e tuned -- avoid slightly odd @{cpat};
wenzelm
parents: 60801
diff changeset
   804
   fun all x t =
f6b0d827240e tuned -- avoid slightly odd @{cpat};
wenzelm
parents: 60801
diff changeset
   805
    Thm.apply (Thm.cterm_of ctxt (Logic.all_const (Thm.typ_of_cterm x))) (Thm.lambda x t)
f6b0d827240e tuned -- avoid slightly odd @{cpat};
wenzelm
parents: 60801
diff changeset
   806
   val ts = sort (fn (a, b) => Term_Ord.fast_term_ord (Thm.term_of a, Thm.term_of b)) (f p)
f6b0d827240e tuned -- avoid slightly odd @{cpat};
wenzelm
parents: 60801
diff changeset
   807
   val p' = fold_rev all ts p
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36833
diff changeset
   808
 in Thm.implies_intr p' (Thm.implies_elim st (fold Thm.forall_elim ts (Thm.assume p'))) end));
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   809
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   810
local
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   811
val ss1 =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   812
  simpset_of (put_simpset comp_ss @{context}
61694
6571c78c9667 Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
paulson <lp15@cam.ac.uk>
parents: 61476
diff changeset
   813
    addsimps @{thms simp_thms} @ 
6571c78c9667 Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
paulson <lp15@cam.ac.uk>
parents: 61476
diff changeset
   814
            [@{thm "nat_numeral"} RS sym, @{thm "zdvd_int"}, @{thm "of_nat_add"}, @{thm "of_nat_mult"}] 
6571c78c9667 Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
paulson <lp15@cam.ac.uk>
parents: 61476
diff changeset
   815
        @ map (fn r => r RS sym) [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}]
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   816
    |> Splitter.add_split @{thm "zdiff_int_split"})
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   817
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   818
val ss2 =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   819
  simpset_of (put_simpset HOL_basic_ss @{context}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   820
    addsimps [@{thm "nat_0_le"}, @{thm "int_numeral"},
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   821
              @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "zero_le_numeral"}, 
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   822
              @{thm "le_numeral_extra"(3)}, @{thm "int_0"}, @{thm "int_1"}, @{thm "Suc_eq_plus1"}]
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   823
    |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}])
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   824
val div_mod_ss =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   825
  simpset_of (put_simpset HOL_basic_ss @{context}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   826
    addsimps @{thms simp_thms}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   827
    @ map (Thm.symmetric o mk_meta_eq) 
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   828
      [@{thm "dvd_eq_mod_eq_0"},
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   829
       @{thm "mod_add_left_eq"}, @{thm "mod_add_right_eq"}, 
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   830
       @{thm "mod_add_eq"}, @{thm "div_add1_eq"}, @{thm "zdiv_zadd1_eq"}]
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   831
    @ [@{thm "mod_self"}, @{thm "mod_by_0"}, @{thm "div_by_0"},
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   832
       @{thm "div_0"}, @{thm "mod_0"}, @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, 
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   833
       @{thm "mod_1"}, @{thm "Suc_eq_plus1"}]
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 56245
diff changeset
   834
    @ @{thms ac_simps}
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   835
   addsimprocs [@{simproc cancel_div_mod_nat}, @{simproc cancel_div_mod_int}])
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45196
diff changeset
   836
val splits_ss =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   837
  simpset_of (put_simpset comp_ss @{context}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   838
    addsimps [@{thm "mod_div_equality'"}]
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   839
    |> fold Splitter.add_split
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   840
      [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"}, 
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   841
       @{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}])
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   842
in
58820
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   843
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   844
fun nat_to_int_tac ctxt = 
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   845
  simp_tac (put_simpset ss1 ctxt) THEN_ALL_NEW
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   846
  simp_tac (put_simpset ss2 ctxt) THEN_ALL_NEW
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   847
  simp_tac (put_simpset comp_ss ctxt);
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   848
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   849
fun div_mod_tac ctxt = simp_tac (put_simpset div_mod_ss ctxt);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   850
fun splits_tac ctxt = simp_tac (put_simpset splits_ss ctxt);
58820
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   851
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   852
end;
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   853
36804
f4ad04780669 shorten names
haftmann
parents: 36802
diff changeset
   854
fun core_tac ctxt = CSUBGOAL (fn (p, i) =>
36805
929b23461a14 simplified oracle
haftmann
parents: 36804
diff changeset
   855
   let
58820
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   856
     val cpth = 
52059
2f970c7f722b proper option quick_and_dirty;
wenzelm
parents: 51930
diff changeset
   857
       if Config.get ctxt quick_and_dirty
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   858
       then oracle (ctxt, Envir.beta_norm (Envir.eta_long [] (Thm.term_of (Thm.dest_arg p))))
36804
f4ad04780669 shorten names
haftmann
parents: 36802
diff changeset
   859
       else Conv.arg_conv (conv ctxt) p
58820
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   860
     val p' = Thm.rhs_of cpth
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   861
     val th = Thm.implies_intr p' (Thm.equal_elim (Thm.symmetric cpth) (Thm.assume p'))
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   862
   in resolve_tac ctxt [th] i end
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   863
   handle COOPER _ => no_tac);
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   864
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   865
fun finish_tac ctxt q = SUBGOAL (fn (_, i) =>
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   866
  (if q then I else TRY) (resolve_tac ctxt [TrueI] i));
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   867
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   868
fun tac elim add_ths del_ths = Subgoal.FOCUS_PARAMS (fn {context = ctxt, ...} =>
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   869
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   870
    val simpset_ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   871
      put_simpset (fst (get ctxt)) ctxt delsimps del_ths addsimps add_ths
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   872
  in
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61694
diff changeset
   873
    Method.insert_tac ctxt (rev (Named_Theorems.get ctxt @{named_theorems arith}))
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54489
diff changeset
   874
    THEN_ALL_NEW Object_Logic.full_atomize_tac ctxt
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   875
    THEN_ALL_NEW CONVERSION Thm.eta_long_conversion
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   876
    THEN_ALL_NEW simp_tac simpset_ctxt
61075
f6b0d827240e tuned -- avoid slightly odd @{cpat};
wenzelm
parents: 60801
diff changeset
   877
    THEN_ALL_NEW (TRY o generalize_tac ctxt (int_nat_terms ctxt))
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54489
diff changeset
   878
    THEN_ALL_NEW Object_Logic.full_atomize_tac ctxt
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   879
    THEN_ALL_NEW (thin_prems_tac ctxt (is_relevant ctxt))
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54489
diff changeset
   880
    THEN_ALL_NEW Object_Logic.full_atomize_tac ctxt
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   881
    THEN_ALL_NEW div_mod_tac ctxt
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   882
    THEN_ALL_NEW splits_tac ctxt
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51143
diff changeset
   883
    THEN_ALL_NEW simp_tac simpset_ctxt
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   884
    THEN_ALL_NEW CONVERSION Thm.eta_long_conversion
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   885
    THEN_ALL_NEW nat_to_int_tac ctxt
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54489
diff changeset
   886
    THEN_ALL_NEW core_tac ctxt
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   887
    THEN_ALL_NEW finish_tac ctxt elim
47476
92d1c566ebbf refined Cooper.tac / "presburger" method: Subgoal.FOCUS_PARAMS allows to solve more problems with outer quantifiers, e.g "!!x. [| 0 <= (x::int); x div 2 < f x |] ==> x < f x * 2";
wenzelm
parents: 47432
diff changeset
   888
  end 1);
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   889
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   890
58820
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   891
(* attribute syntax *)
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   892
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   893
local
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   894
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   895
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   896
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   897
val constsN = "consts";
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   898
val any_keyword = keyword constsN
61476
1884c40f1539 tuned signature;
wenzelm
parents: 61128
diff changeset
   899
val thms = Scan.repeats (Scan.unless any_keyword Attrib.multi_thm);
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   900
val terms = thms >> map (Thm.term_of o Drule.dest_term);
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   901
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   902
fun optional scan = Scan.optional scan [];
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   903
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   904
in
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   905
58820
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   906
val _ =
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   907
  Theory.setup
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   908
    (Attrib.setup @{binding presburger}
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   909
      ((Scan.lift (Args.$$$ "del") |-- optional (keyword constsN |-- terms)) >> del ||
3ad2759acc52 modernized setup;
wenzelm
parents: 57955
diff changeset
   910
        optional (keyword constsN |-- terms) >> add) "data for Cooper's algorithm"
59657
2441a80fb6c1 eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents: 59621
diff changeset
   911
    #> Arith_Data.add_tactic "Presburger arithmetic" (tac true [] []));
36802
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   912
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   913
end;
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   914
5f9fe7b3295d only one module fpr presburger method
haftmann
parents: 36799
diff changeset
   915
end;