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