author | blanchet |
Thu, 21 Feb 2013 12:22:26 +0100 | |
changeset 51213 | 7d08487aa603 |
parent 51208 | 44f0bfe67b01 |
child 51243 | ffd9d7f73e65 |
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 |
51031
63d71b247323
more robustness in Isar proof reconstruction (cf. bug report by Ondrej)
blanchet
parents:
51030
diff
changeset
|
42 |
val redirect_graph : |
51145 | 43 |
atom list -> atom list -> atom -> refute_graph -> direct_proof |
45882 | 44 |
val succedent_of_cases : (clause * direct_inference list) list -> clause |
45 |
val string_of_direct_proof : direct_proof -> string |
|
45877 | 46 |
end; |
47 |
||
46320 | 48 |
functor ATP_Proof_Redirect(Atom : ATP_ATOM): ATP_PROOF_REDIRECT = |
45877 | 49 |
struct |
50 |
||
45882 | 51 |
type atom = Atom.key |
52 |
||
53 |
structure Atom_Graph = Graph(Atom) |
|
45877 | 54 |
|
45882 | 55 |
type ref_sequent = atom list * atom |
51145 | 56 |
type refute_graph = unit Atom_Graph.T |
45882 | 57 |
|
58 |
type clause = atom list |
|
59 |
type direct_sequent = atom list * clause |
|
60 |
type direct_graph = unit Atom_Graph.T |
|
45877 | 61 |
|
62 |
type rich_sequent = clause list * clause |
|
63 |
||
45882 | 64 |
datatype direct_inference = |
45877 | 65 |
Have of rich_sequent | |
45882 | 66 |
Cases of (clause * direct_inference list) list |
67 |
||
68 |
type direct_proof = direct_inference list |
|
45877 | 69 |
|
51213 | 70 |
val atom_eq = is_equal o Atom.ord |
71 |
val clause_ord = dict_ord Atom.ord |
|
72 |
val clause_eq = is_equal o clause_ord |
|
73 |
val direct_sequent_ord = prod_ord clause_ord clause_ord |
|
74 |
val direct_sequent_eq = is_equal o direct_sequent_ord |
|
45877 | 75 |
|
51208
44f0bfe67b01
remove needless steps from refutation graph -- these confuse the proof redirection algorithm (and are needless)
blanchet
parents:
51207
diff
changeset
|
76 |
fun make_refute_graph bot infers = |
45877 | 77 |
let |
78 |
fun add_edge to from = |
|
45882 | 79 |
Atom_Graph.default_node (from, ()) |
80 |
#> Atom_Graph.default_node (to, ()) |
|
81 |
#> Atom_Graph.add_edge_acyclic (from, to) |
|
45877 | 82 |
fun add_infer (froms, to) = fold (add_edge to) froms |
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] |
44f0bfe67b01
remove needless steps from refutation graph -- these confuse the proof redirection algorithm (and are needless)
blanchet
parents:
51207
diff
changeset
|
85 |
in graph |> Atom_Graph.restrict (member (is_equal o Atom.ord) reachable) end |
45882 | 86 |
|
51145 | 87 |
fun axioms_of_refute_graph refute_graph conjs = |
88 |
subtract atom_eq conjs (Atom_Graph.minimals refute_graph) |
|
45877 | 89 |
|
51145 | 90 |
fun tainted_atoms_of_refute_graph refute_graph = |
91 |
Atom_Graph.all_succs refute_graph |
|
92 |
||
93 |
fun sequents_of_refute_graph refute_graph = |
|
94 |
map (`(Atom_Graph.immediate_preds refute_graph)) |
|
95 |
(filter_out (Atom_Graph.is_minimal refute_graph) |
|
96 |
(Atom_Graph.keys refute_graph)) |
|
45877 | 97 |
|
47915 | 98 |
val string_of_context = map Atom.string_of #> space_implode ", " |
99 |
||
100 |
fun string_of_sequent (gamma, c) = |
|
101 |
string_of_context gamma ^ " \<turnstile> " ^ Atom.string_of c |
|
102 |
||
51145 | 103 |
val string_of_refute_graph = |
104 |
sequents_of_refute_graph #> map string_of_sequent #> cat_lines |
|
47915 | 105 |
|
45882 | 106 |
fun redirect_sequent tainted bot (gamma, c) = |
107 |
if member atom_eq tainted c then |
|
108 |
gamma |> List.partition (not o member atom_eq tainted) |
|
109 |
|>> not (atom_eq (c, bot)) ? cons c |
|
45877 | 110 |
else |
45882 | 111 |
(gamma, [c]) |
45877 | 112 |
|
113 |
fun direct_graph seqs = |
|
114 |
let |
|
115 |
fun add_edge from to = |
|
45882 | 116 |
Atom_Graph.default_node (from, ()) |
117 |
#> Atom_Graph.default_node (to, ()) |
|
118 |
#> Atom_Graph.add_edge_acyclic (from, to) |
|
119 |
fun add_seq (gamma, c) = fold (fn l => fold (add_edge l) c) gamma |
|
120 |
in Atom_Graph.empty |> fold add_seq seqs end |
|
45877 | 121 |
|
45882 | 122 |
fun disj cs = fold (union atom_eq) cs [] |> sort Atom.ord |
45877 | 123 |
|
45882 | 124 |
fun succedent_of_inference (Have (_, c)) = c |
125 |
| succedent_of_inference (Cases cases) = succedent_of_cases cases |
|
126 |
and succedent_of_case (c, []) = c |
|
127 |
| succedent_of_case (_, infs) = succedent_of_inference (List.last infs) |
|
128 |
and succedent_of_cases cases = disj (map succedent_of_case cases) |
|
45877 | 129 |
|
130 |
fun dest_Have (Have z) = z |
|
131 |
| dest_Have _ = raise Fail "non-Have" |
|
132 |
||
133 |
fun enrich_Have nontrivs trivs (cs, c) = |
|
45882 | 134 |
(cs |> map (fn c => if member clause_eq nontrivs c then disj (c :: trivs) |
45877 | 135 |
else c), |
136 |
disj (c :: trivs)) |
|
137 |
|> Have |
|
138 |
||
139 |
fun s_cases cases = |
|
140 |
case cases |> List.partition (null o snd) of |
|
45882 | 141 |
(trivs, nontrivs as [(nontriv0, proof)]) => |
45877 | 142 |
if forall (can dest_Have) proof then |
143 |
let val seqs = proof |> map dest_Have in |
|
144 |
seqs |> map (enrich_Have (nontriv0 :: map snd seqs) (map fst trivs)) |
|
145 |
end |
|
146 |
else |
|
45882 | 147 |
[Cases nontrivs] |
148 |
| (_, nontrivs) => [Cases nontrivs] |
|
45877 | 149 |
|
45882 | 150 |
fun descendants direct_graph = |
151 |
these o try (Atom_Graph.all_succs direct_graph) o single |
|
45877 | 152 |
|
153 |
fun zones_of 0 _ = [] |
|
45882 | 154 |
| zones_of n (bs :: bss) = |
155 |
(fold (subtract atom_eq) bss) bs :: zones_of (n - 1) (bss @ [bs]) |
|
156 |
||
51145 | 157 |
fun redirect_graph axioms tainted bot refute_graph = |
45882 | 158 |
let |
159 |
val seqs = |
|
51145 | 160 |
map (redirect_sequent tainted bot) (sequents_of_refute_graph refute_graph) |
45882 | 161 |
val direct_graph = direct_graph seqs |
162 |
fun redirect c proved seqs = |
|
163 |
if null seqs then |
|
164 |
[] |
|
165 |
else if length c < 2 then |
|
166 |
let |
|
167 |
val proved = c @ proved |
|
168 |
val provable = |
|
169 |
filter (fn (gamma, _) => subset atom_eq (gamma, proved)) seqs |
|
170 |
val horn_provable = filter (fn (_, [_]) => true | _ => false) provable |
|
51207 | 171 |
val seq as (gamma, c) = |
172 |
case horn_provable @ provable of |
|
173 |
[] => raise Fail "ill-formed refutation graph" |
|
174 |
| next :: _ => next |
|
45882 | 175 |
in |
176 |
Have (map single gamma, c) :: |
|
177 |
redirect c proved (filter (curry (not o direct_sequent_eq) seq) seqs) |
|
178 |
end |
|
179 |
else |
|
180 |
let |
|
181 |
fun subsequents seqs zone = |
|
182 |
filter (fn (gamma, _) => subset atom_eq (gamma, zone @ proved)) seqs |
|
183 |
val zones = zones_of (length c) (map (descendants direct_graph) c) |
|
184 |
val subseqss = map (subsequents seqs) zones |
|
185 |
val seqs = fold (subtract direct_sequent_eq) subseqss seqs |
|
186 |
val cases = |
|
187 |
map2 (fn l => fn subseqs => ([l], redirect [l] proved subseqs)) |
|
188 |
c subseqss |
|
189 |
in s_cases cases @ redirect (succedent_of_cases cases) proved seqs end |
|
45877 | 190 |
in redirect [] axioms seqs end |
191 |
||
192 |
fun indent 0 = "" |
|
193 |
| indent n = " " ^ indent (n - 1) |
|
194 |
||
195 |
fun string_of_clause [] = "\<bottom>" |
|
45882 | 196 |
| string_of_clause ls = space_implode " \<or> " (map Atom.string_of ls) |
45877 | 197 |
|
198 |
fun string_of_rich_sequent ch ([], c) = ch ^ " " ^ string_of_clause c |
|
199 |
| string_of_rich_sequent ch (cs, c) = |
|
200 |
commas (map string_of_clause cs) ^ " " ^ ch ^ " " ^ string_of_clause c |
|
201 |
||
202 |
fun string_of_case depth (c, proof) = |
|
203 |
indent (depth + 1) ^ "[" ^ string_of_clause c ^ "]" |
|
204 |
|> not (null proof) ? suffix ("\n" ^ string_of_subproof (depth + 1) proof) |
|
205 |
||
206 |
and string_of_inference depth (Have seq) = |
|
207 |
indent depth ^ string_of_rich_sequent "\<triangleright>" seq |
|
208 |
| string_of_inference depth (Cases cases) = |
|
209 |
indent depth ^ "[\n" ^ |
|
210 |
space_implode ("\n" ^ indent depth ^ "|\n") |
|
211 |
(map (string_of_case depth) cases) ^ "\n" ^ |
|
212 |
indent depth ^ "]" |
|
213 |
||
45882 | 214 |
and string_of_subproof depth = cat_lines o map (string_of_inference depth) |
45877 | 215 |
|
45882 | 216 |
val string_of_direct_proof = string_of_subproof 0 |
45877 | 217 |
|
218 |
end; |