4 |
4 |
5 Ferrante and Rackoff's algorithm for quantifier elimination in dense |
5 Ferrante and Rackoff's algorithm for quantifier elimination in dense |
6 linear orders. Proof-synthesis and tactic. |
6 linear orders. Proof-synthesis and tactic. |
7 *) |
7 *) |
8 |
8 |
9 signature FERRANTE_RACKOFF = |
9 signature FERRANTE_RACKOFF = |
10 sig |
10 sig |
|
11 val dlo_conv: Proof.context -> conv |
11 val dlo_tac: Proof.context -> int -> tactic |
12 val dlo_tac: Proof.context -> int -> tactic |
12 end; |
13 end; |
13 |
14 |
14 structure FerranteRackoff: FERRANTE_RACKOFF = |
15 structure FerranteRackoff: FERRANTE_RACKOFF = |
15 struct |
16 struct |
16 |
17 |
17 open Ferrante_Rackoff_Data; |
18 open Ferrante_Rackoff_Data; |
18 open Conv; |
19 open Conv; |
19 |
20 |
20 type entry = {minf: thm list, pinf: thm list, nmi: thm list, npi: thm list, |
21 type entry = {minf: thm list, pinf: thm list, nmi: thm list, npi: thm list, |
21 ld: thm list, qe: thm, atoms : cterm list} * |
22 ld: thm list, qe: thm, atoms : cterm list} * |
22 {isolate_conv: cterm list -> cterm -> thm, |
23 {isolate_conv: cterm list -> cterm -> thm, |
23 whatis : cterm -> cterm -> ord, |
24 whatis : cterm -> cterm -> ord, |
24 simpset : simpset}; |
25 simpset : simpset}; |
25 |
26 |
26 fun binop_cong b th1 th2 = Thm.combination (Drule.arg_cong_rule b th1) th2; |
27 fun get_p1 th = |
27 val is_refl = op aconv o Logic.dest_equals o Thm.prop_of; |
28 funpow 2 (Thm.dest_arg o snd o Thm.dest_abs NONE) |
28 fun C f x y = f y x |
29 (funpow 2 Thm.dest_arg (cprop_of th)) |> Thm.dest_arg |
29 |
|
30 fun get_p1 th = |
|
31 let |
|
32 fun appair f (x,y) = (f x, f y) |
|
33 in funpow 2 (Thm.dest_arg o snd o Thm.dest_abs NONE) |
|
34 (funpow 2 Thm.dest_arg (cprop_of th)) |> Thm.dest_arg |
|
35 end; |
|
36 |
30 |
37 fun ferrack_conv |
31 fun ferrack_conv |
38 (entr as ({minf = minf, pinf = pinf, nmi = nmi, npi = npi, |
32 (entr as ({minf = minf, pinf = pinf, nmi = nmi, npi = npi, |
39 ld = ld, qe = qe, atoms = atoms}, |
33 ld = ld, qe = qe, atoms = atoms}, |
40 {isolate_conv = icv, whatis = wi, simpset = simpset}):entry) = |
34 {isolate_conv = icv, whatis = wi, simpset = simpset}):entry) = |
41 let |
35 let |
42 fun uset (vars as (x::vs)) p = case term_of p of |
36 fun uset (vars as (x::vs)) p = case term_of p of |
43 Const("op &", _)$ _ $ _ => |
37 Const("op &", _)$ _ $ _ => |
44 let |
38 let |
45 val ((b,l),r) = Thm.dest_comb p |>> Thm.dest_comb |
39 val ((b,l),r) = Thm.dest_comb p |>> Thm.dest_comb |
46 val (lS,lth) = uset vars l val (rS, rth) = uset vars r |
40 val (lS,lth) = uset vars l val (rS, rth) = uset vars r |
47 in (lS@rS, binop_cong b lth rth) end |
41 in (lS@rS, Drule.binop_cong_rule b lth rth) end |
48 | Const("op |", _)$ _ $ _ => |
42 | Const("op |", _)$ _ $ _ => |
49 let |
43 let |
50 val ((b,l),r) = Thm.dest_comb p |>> Thm.dest_comb |
44 val ((b,l),r) = Thm.dest_comb p |>> Thm.dest_comb |
51 val (lS,lth) = uset vars l val (rS, rth) = uset vars r |
45 val (lS,lth) = uset vars l val (rS, rth) = uset vars r |
52 in (lS@rS, binop_cong b lth rth) end |
46 in (lS@rS, Drule.binop_cong_rule b lth rth) end |
53 | _ => |
47 | _ => |
54 let |
48 let |
55 val th = icv vars p |
49 val th = icv vars p |
56 val p' = Thm.rhs_of th |
50 val p' = Thm.rhs_of th |
57 val c = wi x p' |
51 val c = wi x p' |
58 val S = (if c mem [Lt, Le, Eq] then single o Thm.dest_arg |
52 val S = (if member (op =) [Lt, Le, Eq] c then single o Thm.dest_arg |
59 else if c mem [Gt, Ge] then single o Thm.dest_arg1 |
53 else if member (op =) [Gt, Ge] c then single o Thm.dest_arg1 |
60 else if c = NEq then single o Thm.dest_arg o Thm.dest_arg |
54 else if c = NEq then single o Thm.dest_arg o Thm.dest_arg |
61 else K []) p' |
55 else K []) p' |
62 in (S,th) end |
56 in (S,th) end |
63 |
57 |
64 val ((p1_v,p2_v),(mp1_v,mp2_v)) = |
58 val ((p1_v,p2_v),(mp1_v,mp2_v)) = |
|
59 funpow 2 (Thm.dest_arg o snd o Thm.dest_abs NONE) |
|
60 (funpow 4 Thm.dest_arg (cprop_of (hd minf))) |
|
61 |> Thm.dest_binop |> pairself Thm.dest_binop |> apfst (pairself Thm.dest_fun) |
|
62 |
|
63 fun myfwd (th1, th2, th3, th4, th5) p1 p2 |
|
64 [(th_1,th_2,th_3,th_4,th_5), (th_1',th_2',th_3',th_4',th_5')] = |
65 let |
65 let |
66 fun appair f (x,y) = (f x, f y) |
|
67 in funpow 2 (Thm.dest_arg o snd o Thm.dest_abs NONE) |
|
68 (funpow 4 Thm.dest_arg (cprop_of (hd minf))) |
|
69 |> Thm.dest_binop |> appair Thm.dest_binop |> apfst (appair Thm.dest_fun) |
|
70 end |
|
71 |
|
72 fun myfwd (th1, th2, th3, th4, th5) p1 p2 |
|
73 [(th_1,th_2,th_3,th_4,th_5), (th_1',th_2',th_3',th_4',th_5')] = |
|
74 let |
|
75 val (mp1, mp2) = (get_p1 th_1, get_p1 th_1') |
66 val (mp1, mp2) = (get_p1 th_1, get_p1 th_1') |
76 val (pp1, pp2) = (get_p1 th_2, get_p1 th_2') |
67 val (pp1, pp2) = (get_p1 th_2, get_p1 th_2') |
77 fun fw mi th th' th'' = |
68 fun fw mi th th' th'' = |
78 let |
69 let |
79 val th0 = if mi then |
70 val th0 = if mi then |
80 instantiate ([],[(p1_v, p1),(p2_v, p2),(mp1_v, mp1), (mp2_v, mp2)]) th |
71 instantiate ([],[(p1_v, p1),(p2_v, p2),(mp1_v, mp1), (mp2_v, mp2)]) th |
81 else instantiate ([],[(p1_v, p1),(p2_v, p2),(mp1_v, pp1), (mp2_v, pp2)]) th |
72 else instantiate ([],[(p1_v, p1),(p2_v, p2),(mp1_v, pp1), (mp2_v, pp2)]) th |
82 in implies_elim (implies_elim th0 th') th'' end |
73 in implies_elim (implies_elim th0 th') th'' end |
83 in (fw true th1 th_1 th_1', fw false th2 th_2 th_2', |
74 in (fw true th1 th_1 th_1', fw false th2 th_2 th_2', |
84 fw true th3 th_3 th_3', fw false th4 th_4 th_4', fw true th5 th_5 th_5') |
75 fw true th3 th_3 th_3', fw false th4 th_4 th_4', fw true th5 th_5 th_5') |
85 end |
76 end |
86 val U_v = (Thm.dest_arg o Thm.dest_arg o Thm.dest_arg1) (cprop_of qe) |
77 val U_v = (Thm.dest_arg o Thm.dest_arg o Thm.dest_arg1) (cprop_of qe) |
87 fun main vs p = |
78 fun main vs p = |
88 let |
79 let |
89 val ((xn,ce),(x,fm)) = (case term_of p of |
80 val ((xn,ce),(x,fm)) = (case term_of p of |
90 Const("Ex",_)$Abs(xn,xT,_) => |
81 Const("Ex",_)$Abs(xn,xT,_) => |
91 Thm.dest_comb p ||> Thm.dest_abs (SOME xn) |>> pair xn |
82 Thm.dest_comb p ||> Thm.dest_abs (SOME xn) |>> pair xn |
92 | _ => error "main QE only trats existential quantifiers!") |
83 | _ => raise CTERM ("main QE only treats existential quantifiers!", [p])) |
93 val cT = ctyp_of_term x |
84 val cT = ctyp_of_term x |
94 val (u,nth) = uset (x::vs) fm |>> distinct (op aconvc) |
85 val (u,nth) = uset (x::vs) fm |>> distinct (op aconvc) |
95 val nthx = Thm.abstract_rule xn x nth |
86 val nthx = Thm.abstract_rule xn x nth |
96 val q = Thm.rhs_of nth |
87 val q = Thm.rhs_of nth |
97 val qx = Thm.rhs_of nthx |
88 val qx = Thm.rhs_of nthx |
98 val enth = Drule.arg_cong_rule ce nthx |
89 val enth = Drule.arg_cong_rule ce nthx |
99 val [th0,th1] = map (instantiate' [SOME cT] []) @{thms "finite.intros"} |
90 val [th0,th1] = map (instantiate' [SOME cT] []) @{thms "finite.intros"} |
100 fun ins x th = |
91 fun ins x th = |
101 implies_elim (instantiate' [] [(SOME o Thm.dest_arg o Thm.dest_arg) |
92 implies_elim (instantiate' [] [(SOME o Thm.dest_arg o Thm.dest_arg) |
102 (Thm.cprop_of th), SOME x] th1) th |
93 (Thm.cprop_of th), SOME x] th1) th |
103 val fU = fold ins u th0 |
94 val fU = fold ins u th0 |
104 val cU = funpow 2 Thm.dest_arg (Thm.cprop_of fU) |
95 val cU = funpow 2 Thm.dest_arg (Thm.cprop_of fU) |
105 local |
96 local |
106 val insI1 = instantiate' [SOME cT] [] @{thm "insertI1"} |
97 val insI1 = instantiate' [SOME cT] [] @{thm "insertI1"} |
107 val insI2 = instantiate' [SOME cT] [] @{thm "insertI2"} |
98 val insI2 = instantiate' [SOME cT] [] @{thm "insertI2"} |
108 in |
99 in |
109 fun provein x S = |
100 fun provein x S = |
110 case term_of S of |
101 case term_of S of |
111 Const("{}",_) => error "provein : not a member!" |
102 Const("{}",_) => raise CTERM ("provein : not a member!", [S]) |
112 | Const("insert",_)$y$_ => |
103 | Const("insert",_)$y$_ => |
113 let val (cy,S') = Thm.dest_binop S |
104 let val (cy,S') = Thm.dest_binop S |
114 in if term_of x aconv y then instantiate' [] [SOME x, SOME S'] insI1 |
105 in if term_of x aconv y then instantiate' [] [SOME x, SOME S'] insI1 |
115 else implies_elim (instantiate' [] [SOME x, SOME S', SOME cy] insI2) |
106 else implies_elim (instantiate' [] [SOME x, SOME S', SOME cy] insI2) |
116 (provein x S') |
107 (provein x S') |
117 end |
108 end |
118 end |
109 end |
119 val tabU = fold (fn t => fn tab => Termtab.update (term_of t, provein t cU) tab) |
110 val tabU = fold (fn t => fn tab => Termtab.update (term_of t, provein t cU) tab) |
120 u Termtab.empty |
111 u Termtab.empty |
121 val U = valOf o Termtab.lookup tabU o term_of |
112 val U = the o Termtab.lookup tabU o term_of |
122 val [minf_conj, minf_disj, minf_eq, minf_neq, minf_lt, |
113 val [minf_conj, minf_disj, minf_eq, minf_neq, minf_lt, |
123 minf_le, minf_gt, minf_ge, minf_P] = minf |
114 minf_le, minf_gt, minf_ge, minf_P] = minf |
124 val [pinf_conj, pinf_disj, pinf_eq, pinf_neq, pinf_lt, |
115 val [pinf_conj, pinf_disj, pinf_eq, pinf_neq, pinf_lt, |
125 pinf_le, pinf_gt, pinf_ge, pinf_P] = pinf |
116 pinf_le, pinf_gt, pinf_ge, pinf_P] = pinf |
126 val [nmi_conj, nmi_disj, nmi_eq, nmi_neq, nmi_lt, |
117 val [nmi_conj, nmi_disj, nmi_eq, nmi_neq, nmi_lt, |
127 nmi_le, nmi_gt, nmi_ge, nmi_P] = map (instantiate ([],[(U_v,cU)])) nmi |
118 nmi_le, nmi_gt, nmi_ge, nmi_P] = map (instantiate ([],[(U_v,cU)])) nmi |
128 val [npi_conj, npi_disj, npi_eq, npi_neq, npi_lt, |
119 val [npi_conj, npi_disj, npi_eq, npi_neq, npi_lt, |
129 npi_le, npi_gt, npi_ge, npi_P] = map (instantiate ([],[(U_v,cU)])) npi |
120 npi_le, npi_gt, npi_ge, npi_P] = map (instantiate ([],[(U_v,cU)])) npi |
130 val [ld_conj, ld_disj, ld_eq, ld_neq, ld_lt, |
121 val [ld_conj, ld_disj, ld_eq, ld_neq, ld_lt, |
131 ld_le, ld_gt, ld_ge, ld_P] = map (instantiate ([],[(U_v,cU)])) ld |
122 ld_le, ld_gt, ld_ge, ld_P] = map (instantiate ([],[(U_v,cU)])) ld |
132 |
123 |
133 fun decomp_mpinf fm = |
124 fun decomp_mpinf fm = |
134 case term_of fm of |
125 case term_of fm of |
135 Const("op &",_)$_$_ => |
126 Const("op &",_)$_$_ => |
136 let val (p,q) = Thm.dest_binop fm |
127 let val (p,q) = Thm.dest_binop fm |
137 in ([p,q], myfwd (minf_conj,pinf_conj, nmi_conj, npi_conj,ld_conj) |
128 in ([p,q], myfwd (minf_conj,pinf_conj, nmi_conj, npi_conj,ld_conj) |
138 (Thm.cabs x p) (Thm.cabs x q)) |
129 (Thm.cabs x p) (Thm.cabs x q)) |
139 end |
130 end |
140 | Const("op |",_)$_$_ => |
131 | Const("op |",_)$_$_ => |
141 let val (p,q) = Thm.dest_binop fm |
132 let val (p,q) = Thm.dest_binop fm |
142 in ([p,q],myfwd (minf_disj, pinf_disj, nmi_disj, npi_disj,ld_disj) |
133 in ([p,q],myfwd (minf_disj, pinf_disj, nmi_disj, npi_disj,ld_disj) |
143 (Thm.cabs x p) (Thm.cabs x q)) |
134 (Thm.cabs x p) (Thm.cabs x q)) |
144 end |
135 end |
145 | _ => |
136 | _ => |
146 (let val c = wi x fm |
137 (let val c = wi x fm |
147 val t = (if c=Nox then I |
138 val t = (if c=Nox then I |
148 else if c mem [Lt, Le, Eq] then Thm.dest_arg |
139 else if member (op =) [Lt, Le, Eq] c then Thm.dest_arg |
149 else if c mem [Gt,Ge] then Thm.dest_arg1 |
140 else if member (op =) [Gt, Ge] c then Thm.dest_arg1 |
150 else if c = NEq then (Thm.dest_arg o Thm.dest_arg) |
141 else if c = NEq then (Thm.dest_arg o Thm.dest_arg) |
151 else error "decomp_mpinf: Impossible case!!") fm |
142 else sys_error "decomp_mpinf: Impossible case!!") fm |
152 val [mi_th, pi_th, nmi_th, npi_th, ld_th] = |
143 val [mi_th, pi_th, nmi_th, npi_th, ld_th] = |
153 if c = Nox then map (instantiate' [] [SOME fm]) |
144 if c = Nox then map (instantiate' [] [SOME fm]) |
154 [minf_P, pinf_P, nmi_P, npi_P, ld_P] |
145 [minf_P, pinf_P, nmi_P, npi_P, ld_P] |
155 else |
146 else |
156 let val [mi_th,pi_th,nmi_th,npi_th,ld_th] = |
147 let val [mi_th,pi_th,nmi_th,npi_th,ld_th] = |
157 map (instantiate' [] [SOME t]) |
148 map (instantiate' [] [SOME t]) |
158 (case c of Lt => [minf_lt, pinf_lt, nmi_lt, npi_lt, ld_lt] |
149 (case c of Lt => [minf_lt, pinf_lt, nmi_lt, npi_lt, ld_lt] |
159 | Le => [minf_le, pinf_le, nmi_le, npi_le, ld_le] |
150 | Le => [minf_le, pinf_le, nmi_le, npi_le, ld_le] |
160 | Gt => [minf_gt, pinf_gt, nmi_gt, npi_gt, ld_gt] |
151 | Gt => [minf_gt, pinf_gt, nmi_gt, npi_gt, ld_gt] |
161 | Ge => [minf_ge, pinf_ge, nmi_ge, npi_ge, ld_ge] |
152 | Ge => [minf_ge, pinf_ge, nmi_ge, npi_ge, ld_ge] |
195 | Const ("op -->", _) $ _ $ _ => find_args bounds tm |
187 | Const ("op -->", _) $ _ $ _ => find_args bounds tm |
196 | Const ("==>", _) $ _ $ _ => find_args bounds tm |
188 | Const ("==>", _) $ _ $ _ => find_args bounds tm |
197 | Const ("==", _) $ _ $ _ => find_args bounds tm |
189 | Const ("==", _) $ _ $ _ => find_args bounds tm |
198 | Const ("Trueprop", _) $ _ => h bounds (Thm.dest_arg tm) |
190 | Const ("Trueprop", _) $ _ => h bounds (Thm.dest_arg tm) |
199 | _ => Thm.dest_fun2 tm) |
191 | _ => Thm.dest_fun2 tm) |
200 and find_args bounds tm = |
192 and find_args bounds tm = |
201 (h bounds (Thm.dest_arg tm) handle CTERM _ => Thm.dest_arg1 tm) |
193 (h bounds (Thm.dest_arg tm) handle CTERM _ => Thm.dest_arg1 tm) |
202 and find_body bounds b = |
194 and find_body bounds b = |
203 let val (_, b') = Thm.dest_abs (SOME (Name.bound bounds)) b |
195 let val (_, b') = Thm.dest_abs (SOME (Name.bound bounds)) b |
204 in h (bounds + 1) b' end; |
196 in h (bounds + 1) b' end; |
205 in h end; |
197 in h end; |
206 |
198 |
207 fun raw_ferrack_qe_conv ctxt (thy, {isolate_conv, whatis, simpset}) tm = |
199 fun raw_ferrack_qe_conv ctxt (thy, {isolate_conv, whatis, simpset}) tm = |
208 let |
200 let |
209 val ss = simpset |
201 val ss = simpset |
210 val pcv = Simplifier.rewrite |
202 val ss' = |
211 (merge_ss (HOL_basic_ss addsimps (simp_thms @ ex_simps @ all_simps) |
203 merge_ss (HOL_basic_ss addsimps (simp_thms @ ex_simps @ all_simps) |
212 @ [not_all,@{thm "all_not_ex"}, ex_disj_distrib], ss)) |
204 @ [not_all,@{thm "all_not_ex"}, ex_disj_distrib], ss) |
213 val postcv = Simplifier.rewrite ss |
205 |> Simplifier.inherit_context ss |
214 val nnf = K (nnf_conv then_conv postcv) |
206 val pcv = Simplifier.rewrite ss' |
215 val qe_conv = Qelim.gen_qelim_conv pcv postcv pcv cons (Thm.add_cterm_frees tm []) |
207 val postcv = Simplifier.rewrite ss |
|
208 val nnf = K (nnf_conv then_conv postcv) |
|
209 val qe_conv = Qelim.gen_qelim_conv pcv postcv pcv cons (Thm.add_cterm_frees tm []) |
216 (isolate_conv ctxt) nnf |
210 (isolate_conv ctxt) nnf |
217 (fn vs => ferrack_conv (thy,{isolate_conv = isolate_conv ctxt, |
211 (fn vs => ferrack_conv (thy,{isolate_conv = isolate_conv ctxt, |
218 whatis = whatis, simpset = simpset}) vs |
212 whatis = whatis, simpset = simpset}) vs |
219 then_conv postcv) |
213 then_conv postcv) |
220 in (Simplifier.rewrite ss then_conv qe_conv) tm end; |
214 in (Simplifier.rewrite ss then_conv qe_conv) tm end; |
221 |
215 |
222 fun ferrackqe_conv ctxt tm = |
216 fun dlo_instance ctxt tm = |
223 case Ferrante_Rackoff_Data.match ctxt (grab_atom_bop 0 tm) of |
217 Ferrante_Rackoff_Data.match ctxt (grab_atom_bop 0 tm); |
224 NONE => error "ferrackqe_conv : no corresponding instance in context!" |
218 |
225 | SOME res => raw_ferrack_qe_conv ctxt res tm |
219 fun dlo_conv ctxt tm = |
226 |
220 (case dlo_instance ctxt tm of |
227 |
221 NONE => raise CTERM ("ferrackqe_conv: no corresponding instance in context!", [tm]) |
228 fun core_ferrack_tac ctxt res i st = |
222 | SOME instance => raw_ferrack_qe_conv ctxt instance tm); |
229 let val p = nth (cprems_of st) (i - 1) |
223 |
230 val th = symmetric (arg_conv (raw_ferrack_qe_conv ctxt res) p) |
224 fun dlo_tac ctxt = CSUBGOAL (fn (p, i) => |
231 val p' = Thm.lhs_of th |
225 (case dlo_instance ctxt p of |
232 val th' = implies_intr p' (equal_elim th (assume p')) |
226 NONE => no_tac |
233 val _ = print_thm th |
227 | SOME instance => |
234 in (rtac th' i) st |
228 ObjectLogic.full_atomize_tac i THEN |
235 end |
229 simp_tac (#simpset (snd instance)) i THEN (* FIXME already part of raw_ferrack_qe_conv? *) |
236 |
230 CONVERSION (ObjectLogic.judgment_conv (raw_ferrack_qe_conv ctxt instance)) i THEN |
237 fun dlo_tac ctxt i st = |
231 simp_tac (Simplifier.local_simpset_of ctxt) i)); (* FIXME really? *) |
238 let |
|
239 val instance = (case Ferrante_Rackoff_Data.match ctxt |
|
240 (grab_atom_bop 0 (nth (cprems_of st) (i - 1))) of |
|
241 NONE => error "ferrackqe_conv : no corresponding instance in context!" |
|
242 | SOME r => r) |
|
243 val ss = #simpset (snd instance) |
|
244 in |
|
245 (ObjectLogic.full_atomize_tac i THEN |
|
246 simp_tac ss i THEN |
|
247 core_ferrack_tac ctxt instance i THEN |
|
248 (TRY (simp_tac (Simplifier.local_simpset_of ctxt) i))) st |
|
249 end; |
|
250 |
232 |
251 end; |
233 end; |