author | blanchet |
Fri, 13 Dec 2013 22:54:39 +0800 | |
changeset 54735 | 05c9f3d84ba3 |
parent 54714 | ae01c51eadff |
child 54754 | 6b0ca7f79e93 |
permissions | -rw-r--r-- |
46320 | 1 |
(* Title: HOL/Tools/ATP/atp_proof_redirect.ML |
45877 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
3 |
||
4 |
Transformation of a proof by contradiction into a direct proof. |
|
5 |
*) |
|
6 |
||
45882 | 7 |
signature ATP_ATOM = |
8 |
sig |
|
9 |
type key |
|
10 |
val ord : key * key -> order |
|
11 |
val string_of : key -> string |
|
12 |
end; |
|
13 |
||
46320 | 14 |
signature ATP_PROOF_REDIRECT = |
45877 | 15 |
sig |
45882 | 16 |
type atom |
17 |
||
18 |
structure Atom_Graph : GRAPH |
|
45877 | 19 |
|
45882 | 20 |
type ref_sequent = atom list * atom |
51145 | 21 |
type refute_graph = unit Atom_Graph.T |
45882 | 22 |
|
23 |
type clause = atom list |
|
24 |
type direct_sequent = atom list * clause |
|
25 |
type direct_graph = unit Atom_Graph.T |
|
45877 | 26 |
|
27 |
type rich_sequent = clause list * clause |
|
28 |
||
45882 | 29 |
datatype direct_inference = |
45877 | 30 |
Have of rich_sequent | |
45882 | 31 |
Cases of (clause * direct_inference list) list |
32 |
||
33 |
type direct_proof = direct_inference list |
|
45877 | 34 |
|
51208
44f0bfe67b01
remove needless steps from refutation graph -- these confuse the proof redirection algorithm (and are needless)
blanchet
parents:
51207
diff
changeset
|
35 |
val make_refute_graph : atom -> (atom list * atom) list -> refute_graph |
51145 | 36 |
val axioms_of_refute_graph : refute_graph -> atom list -> atom list |
37 |
val tainted_atoms_of_refute_graph : refute_graph -> atom list -> atom list |
|
38 |
val sequents_of_refute_graph : refute_graph -> ref_sequent list |
|
39 |
val string_of_refute_graph : refute_graph -> string |
|
45882 | 40 |
val redirect_sequent : atom list -> atom -> ref_sequent -> direct_sequent |
45877 | 41 |
val direct_graph : direct_sequent list -> direct_graph |
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
42 |
val redirect_graph : atom list -> atom list -> atom -> refute_graph -> direct_proof |
45882 | 43 |
val succedent_of_cases : (clause * direct_inference list) list -> clause |
44 |
val string_of_direct_proof : direct_proof -> string |
|
45877 | 45 |
end; |
46 |
||
46320 | 47 |
functor ATP_Proof_Redirect(Atom : ATP_ATOM): ATP_PROOF_REDIRECT = |
45877 | 48 |
struct |
49 |
||
45882 | 50 |
type atom = Atom.key |
51 |
||
52 |
structure Atom_Graph = Graph(Atom) |
|
45877 | 53 |
|
45882 | 54 |
type ref_sequent = atom list * atom |
51145 | 55 |
type refute_graph = unit Atom_Graph.T |
45882 | 56 |
|
57 |
type clause = atom list |
|
58 |
type direct_sequent = atom list * clause |
|
59 |
type direct_graph = unit Atom_Graph.T |
|
45877 | 60 |
|
61 |
type rich_sequent = clause list * clause |
|
62 |
||
45882 | 63 |
datatype direct_inference = |
45877 | 64 |
Have of rich_sequent | |
45882 | 65 |
Cases of (clause * direct_inference list) list |
66 |
||
67 |
type direct_proof = direct_inference list |
|
45877 | 68 |
|
51213 | 69 |
val atom_eq = is_equal o Atom.ord |
70 |
val clause_ord = dict_ord Atom.ord |
|
71 |
val clause_eq = is_equal o clause_ord |
|
72 |
val direct_sequent_ord = prod_ord clause_ord clause_ord |
|
73 |
val direct_sequent_eq = is_equal o direct_sequent_ord |
|
45877 | 74 |
|
51208
44f0bfe67b01
remove needless steps from refutation graph -- these confuse the proof redirection algorithm (and are needless)
blanchet
parents:
51207
diff
changeset
|
75 |
fun make_refute_graph bot infers = |
45877 | 76 |
let |
77 |
fun add_edge to from = |
|
45882 | 78 |
Atom_Graph.default_node (from, ()) |
79 |
#> Atom_Graph.default_node (to, ()) |
|
80 |
#> Atom_Graph.add_edge_acyclic (from, to) |
|
45877 | 81 |
fun add_infer (froms, to) = fold (add_edge to) froms |
54735 | 82 |
|
51208
44f0bfe67b01
remove needless steps from refutation graph -- these confuse the proof redirection algorithm (and are needless)
blanchet
parents:
51207
diff
changeset
|
83 |
val graph = fold add_infer infers Atom_Graph.empty |
44f0bfe67b01
remove needless steps from refutation graph -- these confuse the proof redirection algorithm (and are needless)
blanchet
parents:
51207
diff
changeset
|
84 |
val reachable = Atom_Graph.all_preds graph [bot] |
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
85 |
in |
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
86 |
graph |> Atom_Graph.restrict (member (is_equal o Atom.ord) reachable) |
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
87 |
end |
45882 | 88 |
|
51145 | 89 |
fun axioms_of_refute_graph refute_graph conjs = |
90 |
subtract atom_eq conjs (Atom_Graph.minimals refute_graph) |
|
45877 | 91 |
|
54735 | 92 |
fun tainted_atoms_of_refute_graph refute_graph = Atom_Graph.all_succs refute_graph |
51145 | 93 |
|
94 |
fun sequents_of_refute_graph refute_graph = |
|
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
95 |
Atom_Graph.keys refute_graph |
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
96 |
|> filter_out (Atom_Graph.is_minimal refute_graph) |
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
97 |
|> map (`(Atom_Graph.immediate_preds refute_graph)) |
45877 | 98 |
|
47915 | 99 |
val string_of_context = map Atom.string_of #> space_implode ", " |
100 |
||
101 |
fun string_of_sequent (gamma, c) = |
|
102 |
string_of_context gamma ^ " \<turnstile> " ^ Atom.string_of c |
|
103 |
||
54735 | 104 |
fun string_of_refute_graph refute_graph = |
105 |
refute_graph |> sequents_of_refute_graph |> map string_of_sequent |> cat_lines |
|
47915 | 106 |
|
45882 | 107 |
fun redirect_sequent tainted bot (gamma, c) = |
108 |
if member atom_eq tainted c then |
|
109 |
gamma |> List.partition (not o member atom_eq tainted) |
|
110 |
|>> not (atom_eq (c, bot)) ? cons c |
|
45877 | 111 |
else |
45882 | 112 |
(gamma, [c]) |
45877 | 113 |
|
114 |
fun direct_graph seqs = |
|
115 |
let |
|
116 |
fun add_edge from to = |
|
45882 | 117 |
Atom_Graph.default_node (from, ()) |
118 |
#> Atom_Graph.default_node (to, ()) |
|
119 |
#> Atom_Graph.add_edge_acyclic (from, to) |
|
120 |
fun add_seq (gamma, c) = fold (fn l => fold (add_edge l) c) gamma |
|
54735 | 121 |
in |
122 |
fold add_seq seqs Atom_Graph.empty |
|
123 |
end |
|
45877 | 124 |
|
45882 | 125 |
fun disj cs = fold (union atom_eq) cs [] |> sort Atom.ord |
45877 | 126 |
|
45882 | 127 |
fun succedent_of_inference (Have (_, c)) = c |
128 |
| succedent_of_inference (Cases cases) = succedent_of_cases cases |
|
129 |
and succedent_of_case (c, []) = c |
|
130 |
| succedent_of_case (_, infs) = succedent_of_inference (List.last infs) |
|
131 |
and succedent_of_cases cases = disj (map succedent_of_case cases) |
|
45877 | 132 |
|
133 |
fun dest_Have (Have z) = z |
|
134 |
| dest_Have _ = raise Fail "non-Have" |
|
135 |
||
136 |
fun enrich_Have nontrivs trivs (cs, c) = |
|
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
137 |
Have (cs |> map (fn c => if member clause_eq nontrivs c then disj (c :: trivs) else c), |
45877 | 138 |
disj (c :: trivs)) |
139 |
||
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
140 |
fun s_cases cases = [Cases (filter_out (null o snd) cases)] |
45877 | 141 |
|
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
142 |
fun descendants direct_graph = these o try (Atom_Graph.all_succs direct_graph) o single |
45877 | 143 |
|
144 |
fun zones_of 0 _ = [] |
|
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
145 |
| zones_of n (bs :: bss) = (fold (subtract atom_eq) bss) bs :: zones_of (n - 1) (bss @ [bs]) |
45882 | 146 |
|
51145 | 147 |
fun redirect_graph axioms tainted bot refute_graph = |
45882 | 148 |
let |
54735 | 149 |
val seqs = map (redirect_sequent tainted bot) (sequents_of_refute_graph refute_graph) |
45882 | 150 |
val direct_graph = direct_graph seqs |
54735 | 151 |
|
45882 | 152 |
fun redirect c proved seqs = |
153 |
if null seqs then |
|
154 |
[] |
|
155 |
else if length c < 2 then |
|
156 |
let |
|
157 |
val proved = c @ proved |
|
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
158 |
val provable = filter (fn (gamma, _) => subset atom_eq (gamma, proved)) seqs |
45882 | 159 |
val horn_provable = filter (fn (_, [_]) => true | _ => false) provable |
51207 | 160 |
val seq as (gamma, c) = |
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
161 |
(case horn_provable @ provable of |
51207 | 162 |
[] => raise Fail "ill-formed refutation graph" |
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
163 |
| next :: _ => next) |
45882 | 164 |
in |
165 |
Have (map single gamma, c) :: |
|
166 |
redirect c proved (filter (curry (not o direct_sequent_eq) seq) seqs) |
|
167 |
end |
|
168 |
else |
|
169 |
let |
|
170 |
fun subsequents seqs zone = |
|
171 |
filter (fn (gamma, _) => subset atom_eq (gamma, zone @ proved)) seqs |
|
172 |
val zones = zones_of (length c) (map (descendants direct_graph) c) |
|
173 |
val subseqss = map (subsequents seqs) zones |
|
174 |
val seqs = fold (subtract direct_sequent_eq) subseqss seqs |
|
54714
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
175 |
val cases = map2 (fn l => fn subseqs => ([l], redirect [l] proved subseqs)) c subseqss |
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
176 |
in |
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
177 |
s_cases cases @ redirect (succedent_of_cases cases) proved seqs |
ae01c51eadff
removed inlined rewriting'' for one-branch case splits in Isar proofs, since these can yield huge unreadable formulas
blanchet
parents:
51243
diff
changeset
|
178 |
end |
54735 | 179 |
in |
180 |
redirect [] axioms seqs |
|
181 |
end |
|
45877 | 182 |
|
183 |
fun indent 0 = "" |
|
184 |
| indent n = " " ^ indent (n - 1) |
|
185 |
||
186 |
fun string_of_clause [] = "\<bottom>" |
|
45882 | 187 |
| string_of_clause ls = space_implode " \<or> " (map Atom.string_of ls) |
45877 | 188 |
|
189 |
fun string_of_rich_sequent ch ([], c) = ch ^ " " ^ string_of_clause c |
|
190 |
| string_of_rich_sequent ch (cs, c) = |
|
191 |
commas (map string_of_clause cs) ^ " " ^ ch ^ " " ^ string_of_clause c |
|
192 |
||
193 |
fun string_of_case depth (c, proof) = |
|
194 |
indent (depth + 1) ^ "[" ^ string_of_clause c ^ "]" |
|
195 |
|> not (null proof) ? suffix ("\n" ^ string_of_subproof (depth + 1) proof) |
|
196 |
||
197 |
and string_of_inference depth (Have seq) = |
|
198 |
indent depth ^ string_of_rich_sequent "\<triangleright>" seq |
|
199 |
| string_of_inference depth (Cases cases) = |
|
200 |
indent depth ^ "[\n" ^ |
|
54735 | 201 |
space_implode ("\n" ^ indent depth ^ "|\n") (map (string_of_case depth) cases) ^ "\n" ^ |
45877 | 202 |
indent depth ^ "]" |
203 |
||
45882 | 204 |
and string_of_subproof depth = cat_lines o map (string_of_inference depth) |
45877 | 205 |
|
45882 | 206 |
val string_of_direct_proof = string_of_subproof 0 |
45877 | 207 |
|
208 |
end; |