author | blanchet |
Wed, 18 Apr 2012 22:40:25 +0200 | |
changeset 47562 | a72239723ae8 |
parent 47557 | 32f35b3d9e42 |
child 47565 | 762eb0dacaa6 |
permissions | -rw-r--r-- |
46324 | 1 |
(* Title: HOL/TPTP/atp_problem_import.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2012 |
|
4 |
||
5 |
Import TPTP problems as Isabelle terms or goals. |
|
6 |
*) |
|
7 |
||
8 |
signature ATP_PROBLEM_IMPORT = |
|
9 |
sig |
|
10 |
val isabelle_tptp_file : string -> unit |
|
11 |
val nitpick_tptp_file : string -> unit |
|
12 |
val refute_tptp_file : string -> unit |
|
13 |
val sledgehammer_tptp_file : string -> unit |
|
46325 | 14 |
val translate_tptp_file : string -> string -> string -> unit |
46324 | 15 |
end; |
16 |
||
17 |
structure ATP_Problem_Import : ATP_PROBLEM_IMPORT = |
|
18 |
struct |
|
19 |
||
20 |
open ATP_Util |
|
21 |
open ATP_Problem |
|
22 |
open ATP_Proof |
|
23 |
||
24 |
||
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
25 |
(** Crude TPTP CNF and FOF parsing **) |
46324 | 26 |
|
27 |
exception SYNTAX of string |
|
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
28 |
exception THF of unit |
46324 | 29 |
|
30 |
val tptp_explode = raw_explode o strip_spaces_except_between_idents |
|
31 |
||
32 |
fun parse_file_path (c :: ss) = |
|
33 |
if c = "'" orelse c = "\"" then |
|
34 |
ss |> chop_while (curry (op <>) c) |>> implode ||> tl |
|
35 |
else |
|
36 |
raise SYNTAX "invalid file path" |
|
37 |
| parse_file_path [] = raise SYNTAX "invalid file path" |
|
38 |
||
39 |
fun parse_include x = |
|
40 |
let |
|
41 |
val (file_name, rest) = |
|
42 |
(Scan.this_string "include" |-- $$ "(" |-- parse_file_path --| $$ ")" |
|
43 |
--| $$ ".") x |
|
44 |
val path = file_name |> Path.explode |
|
45 |
val path = |
|
46 |
path |> not (Path.is_absolute path) ? Path.append (Path.explode "$TPTP") |
|
47 |
in ((), (path |> File.read |> tptp_explode) @ rest) end |
|
48 |
||
49 |
val parse_cnf_or_fof = |
|
50 |
(Scan.this_string "cnf" || Scan.this_string "fof") |-- $$ "(" |-- |
|
51 |
Scan.many (not_equal ",") |-- $$ "," |-- |
|
52 |
(Scan.this_string "axiom" || Scan.this_string "definition" |
|
53 |
|| Scan.this_string "theorem" || Scan.this_string "lemma" |
|
54 |
|| Scan.this_string "hypothesis" || Scan.this_string "conjecture" |
|
55 |
|| Scan.this_string "negated_conjecture") --| $$ "," -- parse_formula |
|
56 |
--| $$ ")" --| $$ "." |
|
47562 | 57 |
>> (fn ("conjecture", phi) => (true, AConn (ANot, [phi])) |
58 |
| (_, phi) => (false, phi)) |
|
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
59 |
|| Scan.this_string "thf" >> (fn _ => raise THF ()) |
46324 | 60 |
|
61 |
val parse_problem = |
|
62 |
Scan.repeat parse_include |
|
63 |
|-- Scan.repeat (parse_cnf_or_fof --| Scan.repeat parse_include) |
|
64 |
||
65 |
val parse_tptp_problem = |
|
66 |
Scan.finite Symbol.stopper |
|
67 |
(Scan.error (!! (fn _ => raise SYNTAX "malformed TPTP input") |
|
68 |
parse_problem)) |
|
69 |
o tptp_explode |
|
70 |
||
71 |
val iota_T = @{typ iota} |
|
46325 | 72 |
val quant_T = @{typ "(iota => bool) => bool"} |
46324 | 73 |
|
74 |
fun is_variable s = Char.isUpper (String.sub (s, 0)) |
|
75 |
||
76 |
fun hol_term_from_fo_term res_T (ATerm (x, us)) = |
|
77 |
let val ts = map (hol_term_from_fo_term iota_T) us in |
|
78 |
list_comb ((case x of |
|
79 |
"$true" => @{const_name True} |
|
80 |
| "$false" => @{const_name False} |
|
81 |
| "=" => @{const_name HOL.eq} |
|
82 |
| "equal" => @{const_name HOL.eq} |
|
83 |
| _ => x, map fastype_of ts ---> res_T) |
|
84 |
|> (if is_variable x then Free else Const), ts) |
|
85 |
end |
|
86 |
||
87 |
fun hol_prop_from_formula phi = |
|
88 |
case phi of |
|
89 |
AQuant (_, [], phi') => hol_prop_from_formula phi' |
|
90 |
| AQuant (q, (x, _) :: xs, phi') => |
|
91 |
Const (case q of AForall => @{const_name All} | AExists => @{const_name Ex}, |
|
92 |
quant_T) |
|
93 |
$ lambda (Free (x, iota_T)) (hol_prop_from_formula (AQuant (q, xs, phi'))) |
|
94 |
| AConn (ANot, [u']) => HOLogic.mk_not (hol_prop_from_formula u') |
|
95 |
| AConn (c, [u1, u2]) => |
|
96 |
pairself hol_prop_from_formula (u1, u2) |
|
97 |
|> (case c of |
|
98 |
AAnd => HOLogic.mk_conj |
|
99 |
| AOr => HOLogic.mk_disj |
|
100 |
| AImplies => HOLogic.mk_imp |
|
101 |
| AIff => HOLogic.mk_eq |
|
102 |
| ANot => raise Fail "binary \"ANot\"") |
|
103 |
| AConn _ => raise Fail "malformed AConn" |
|
46325 | 104 |
| AAtom u => hol_term_from_fo_term @{typ bool} u |
46324 | 105 |
|
106 |
fun mk_all x t = Const (@{const_name All}, quant_T) $ lambda x t |
|
107 |
||
108 |
fun close_hol_prop t = fold (mk_all o Free) (Term.add_frees t []) t |
|
109 |
||
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
110 |
fun mk_meta_not P = Logic.mk_implies (P, @{prop False}) |
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
111 |
|
47562 | 112 |
fun get_tptp_formula (_, role, P, _) = |
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
113 |
P |> role = TPTP_Syntax.Role_Conjecture ? mk_meta_not |
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
114 |
|
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
115 |
fun read_tptp_file thy file_name = |
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
116 |
let val path = Path.explode file_name in |
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
117 |
(case parse_tptp_problem (File.read path) of |
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
118 |
(_, s :: ss) => |
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
119 |
raise SYNTAX ("cannot parse " ^ quote (implode (s :: ss))) |
47562 | 120 |
| (problem, []) => |
121 |
(exists fst problem, |
|
122 |
map (HOLogic.mk_Trueprop o close_hol_prop o hol_prop_from_formula o snd) |
|
123 |
problem)) |
|
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
124 |
handle THF () => |
47562 | 125 |
let |
126 |
val problem = |
|
127 |
TPTP_Interpret.interpret_file true (Path.explode "$TPTP") path [] [] thy |
|
128 |
|> fst |> #3 |
|
129 |
in |
|
130 |
(exists (fn (_, role, _, _) => role = TPTP_Syntax.Role_Conjecture) problem, |
|
131 |
map get_tptp_formula problem) |
|
132 |
end |
|
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
133 |
end |
46325 | 134 |
|
46324 | 135 |
(** Isabelle (combination of provers) **) |
136 |
||
137 |
fun isabelle_tptp_file file_name = () |
|
138 |
||
139 |
||
140 |
(** Nitpick (alias Nitrox) **) |
|
141 |
||
142 |
fun nitpick_tptp_file file_name = |
|
46325 | 143 |
let |
47562 | 144 |
val (falsify, ts) = read_tptp_file @{theory} file_name |
46325 | 145 |
val state = Proof.init @{context} |
146 |
val params = |
|
47557
32f35b3d9e42
started integrating Nik's parser into TPTP command-line tools
blanchet
parents:
46325
diff
changeset
|
147 |
[("card", "1\<emdash>100"), |
46325 | 148 |
("box", "false"), |
149 |
("sat_solver", "smart"), |
|
150 |
("max_threads", "1"), |
|
151 |
("batch_size", "10"), |
|
47562 | 152 |
("falsify", if falsify then "true" else "false"), |
46325 | 153 |
(* ("debug", "true"), *) |
154 |
("verbose", "true"), |
|
155 |
(* ("overlord", "true"), *) |
|
156 |
("show_consts", "true"), |
|
157 |
("format", "1000"), |
|
158 |
("max_potential", "0"), |
|
159 |
("timeout", "none"), |
|
160 |
("expect", Nitpick.genuineN)] |
|
161 |
|> Nitpick_Isar.default_params @{theory} |
|
162 |
val i = 1 |
|
163 |
val n = 1 |
|
164 |
val step = 0 |
|
165 |
val subst = [] |
|
166 |
in |
|
47562 | 167 |
Nitpick.pick_nits_in_term state params Nitpick.TPTP i n step subst ts |
168 |
(if falsify then @{prop False} else @{prop True}); () |
|
46325 | 169 |
end |
46324 | 170 |
|
171 |
||
172 |
(** Refute **) |
|
173 |
||
47562 | 174 |
fun print_szs_from_outcome falsify s = |
175 |
"% SZS status " ^ |
|
176 |
(if s = "genuine" then |
|
177 |
if falsify then "CounterSatisfiable" else "Satisfiable" |
|
178 |
else |
|
179 |
"Unknown") |
|
180 |
|> writeln |
|
181 |
||
46325 | 182 |
fun refute_tptp_file file_name = |
183 |
let |
|
47562 | 184 |
val (falsify, ts) = read_tptp_file @{theory} file_name |
46325 | 185 |
val params = |
186 |
[("maxtime", "10000"), |
|
187 |
("assms", "true"), |
|
188 |
("expect", Nitpick.genuineN)] |
|
189 |
in |
|
190 |
Refute.refute_term @{context} params ts @{prop False} |
|
47562 | 191 |
|> print_szs_from_outcome falsify |
46325 | 192 |
end |
46324 | 193 |
|
194 |
||
195 |
(** Sledgehammer **) |
|
196 |
||
197 |
fun sledgehammer_tptp_file file_name = () |
|
198 |
||
199 |
||
200 |
(** Translator between TPTP(-like) file formats **) |
|
201 |
||
46325 | 202 |
fun translate_tptp_file format in_file_name out_file_name = () |
46324 | 203 |
|
204 |
end; |