1 module GA = GrafiteAst;;
2 module LA = LexiconAst;;
3 module PT = CicNotationPt;;
6 type sort = Prop | Univ;;
8 let floc = HExtlib.dummy_floc;;
11 let paramod_timeout = ref 600;;
14 let universe = "Univ" ;;
22 PT.Ident ((try List.assoc s kw with Not_found -> s),None)
25 let rec collect_arities_from_term = function
26 | A.Constant name -> [name,(0,Univ)]
27 | A.Variable name -> [name,(0,Univ)]
28 | A.Function (name,l) ->
29 (name,(List.length l,Univ))::
30 List.flatten (List.map collect_arities_from_term l)
33 let rec collect_fv_from_term = function
34 | A.Constant name -> []
35 | A.Variable name -> [name]
37 List.flatten (List.map collect_fv_from_term l)
40 let collect_arities_from_atom a =
42 | A.Proposition name -> [name,(0,Prop)]
43 | A.Predicate (name,args) ->
44 (name,(List.length args,Prop)) ::
45 (List.flatten (List.map collect_arities_from_term args))
49 collect_arities_from_term t1 @ collect_arities_from_term t2
51 collect_arities_from_term t1 @ collect_arities_from_term t2
53 HExtlib.list_uniq (List.sort compare (List.flatten (List.map aux a)))
56 let collect_fv_from_atom a =
58 | A.Proposition name -> [name]
59 | A.Predicate (name,args) ->
60 name :: List.flatten (List.map collect_fv_from_term args)
63 | A.Eq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
64 | A.NotEq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
66 let rec aux2 = function
68 | hd::tl -> aux hd @ aux2 tl
70 HExtlib.list_uniq (List.sort compare (aux2 a))
73 let rec collect_fv_from_formulae = function
74 | A.Disjunction (a,b) ->
75 collect_fv_from_formulae a @ collect_fv_from_formulae b
77 | A.Atom a -> collect_fv_from_atom [a]
80 let rec convert_term = function
81 | A.Variable x -> mk_ident x
82 | A.Constant x -> mk_ident x
83 | A.Function (name, args) ->
84 PT.Appl (mk_ident name :: List.map convert_term args)
87 let rec atom_of_formula neg pos = function
88 | A.Disjunction (a,b) ->
89 let neg, pos = atom_of_formula neg pos a in
90 atom_of_formula neg pos b
91 | A.NegAtom a -> a::neg, pos
92 | A.Atom a -> neg, a::pos
95 let atom_of_formula f =
96 let neg, pos = atom_of_formula [] [] f in
100 let rec mk_arrow component tail = function
103 | Prop -> mk_ident prop
104 | Univ -> mk_ident universe
109 ((mk_ident "_"),Some (mk_ident component)),
110 mk_arrow component tail (n-1))
113 let build_ctx_for_arities univesally arities t =
114 let binder = if univesally then `Forall else `Exists in
115 let rec aux = function
117 | (name,(nargs,sort))::tl ->
120 (mk_ident name,Some (mk_arrow universe sort nargs)),
126 let convert_atom universally a =
128 | A.Proposition p -> mk_ident p
129 | A.Predicate (name,params) ->
130 PT.Appl ((mk_ident name) :: (List.map convert_term params))
131 | A.True -> mk_ident "True"
132 | A.False -> mk_ident "False"
134 | A.NotEq (l,r) -> (* removes the negation *)
135 PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
137 let rec aux2 = function
142 PT.Binder (`Forall, (mk_ident "_", Some (aux he)), aux2 tl)
144 PT.Appl [mk_ident "And";aux he;aux2 tl]
146 let arities = collect_arities_from_atom a in
147 let fv = collect_fv_from_atom a in
148 build_ctx_for_arities universally
150 (function (x,(0,Univ)) -> List.mem x fv | _-> false)
155 let collect_arities atom ctx =
156 let atoms = atom@(List.flatten (List.map atom_of_formula ctx)) in
157 collect_arities_from_atom atoms
160 let collect_arities_from_formulae f =
161 let rec collect_arities_from_formulae = function
162 | A.Disjunction (a,b) ->
163 collect_arities_from_formulae a @ collect_arities_from_formulae b
165 | A.Atom a -> collect_arities_from_atom [a]
167 HExtlib.list_uniq (List.sort compare (collect_arities_from_formulae f))
170 let is_formulae_1eq_negated f =
171 let atom = atom_of_formula f in
173 | [A.NotEq (l,r)] -> true
177 let collect_fv_1stord_from_formulae f =
178 let arities = collect_arities_from_formulae f in
179 let fv = collect_fv_from_formulae f in
181 (List.filter (function (x,(0,Univ)) -> List.mem x fv | _-> false) arities)
184 let rec convert_formula fv no_arities context f =
185 let atom = atom_of_formula f in
186 let t = convert_atom (fv = []) atom in
187 let rec build_ctx n = function
192 (mk_ident ("H" ^ string_of_int n),
193 Some (convert_formula [] true [] hp)),
196 let arities = if no_arities then [] else collect_arities atom context in
197 build_ctx_for_arities true arities (build_ctx 0 context)
200 let check_if_atom_is_negative = function
203 | A.Proposition _ -> false
204 | A.Predicate _ -> false
209 let rec check_if_formula_is_negative = function
210 | A.Disjunction (a,b) ->
211 check_if_formula_is_negative a && check_if_formula_is_negative b
212 | A.NegAtom a -> not (check_if_atom_is_negative a)
213 | A.Atom a -> check_if_atom_is_negative a
216 let convert_ast statements context = function
218 let s = String.sub s 1 (String.length s - 1) in
220 if s.[String.length s - 1] = '\n' then
221 String.sub s 0 (String.length s - 1)
225 statements @ [GA.Comment (floc,GA.Note (floc,s))],
227 | A.Inclusion (s,_) ->
231 floc,"Inclusion of: " ^ s))], context
232 | A.AnnotatedFormula (name,kind,f,_,_) ->
236 statements, f::context
237 | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
238 statements, f::context
239 | A.Negated_conjecture ->
240 let ueq_case = is_formulae_1eq_negated f in
241 let fv = collect_fv_1stord_from_formulae f in
244 prerr_endline ("FREE VARIABLES: " ^ String.concat "," fv);
249 (mk_ident universe,Some (PT.Sort `Set)),
250 convert_formula fv false context f)
252 let o = PT.Theorem (`Theorem,name,f,None) in
254 GA.Executable(floc,GA.Command(floc,GA.Obj(floc,o)));
255 GA.Executable(floc,GA.Tactic(floc, Some
256 (GA.Intros (floc,(None,[]))),GA.Dot(floc)))] @
261 [GA.Executable(floc,GA.Tactic(floc, Some
262 (GA.Exists floc),GA.Branch floc));
263 GA.Executable(floc,GA.Tactic(floc, None,
264 (GA.Pos (floc,[2]))))])
267 [GA.Executable(floc,GA.Tactic(floc, Some (
269 GA.AutoBatch (floc,([],["paramodulation","";
270 "timeout",string_of_int !paramod_timeout]))
272 GA.AutoBatch (floc,([],[
273 "depth",string_of_int 5;
274 "width",string_of_int 5;
275 "size",string_of_int 20;
276 "timeout",string_of_int 10;
279 GA.Semicolon(floc)));
280 GA.Executable(floc,GA.Tactic(floc, Some (GA.Try(floc,
281 GA.Assumption floc)), GA.Dot(floc)))
287 [GA.Executable(floc,GA.Tactic(floc, None, GA.Shift floc));
288 GA.Executable(floc,GA.NonPunctuationTactical(floc, GA.Skip floc,
292 [GA.Executable(floc,GA.Command(floc, GA.Print(floc,"proofterm")));
293 GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))],
301 | A.Unknown -> assert false
305 let resolve ~tptppath s =
307 if Filename.check_suffix s ".p" then
308 (assert (String.length s > 5);
309 let prefix = String.sub s 0 3 in
310 tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
314 if HExtlib.is_regular resolved_name then
318 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
324 let tptp2grafite ?(timeout=600) ?(def_depth=10) ?raw_preamble ~tptppath ~filename () =
325 paramod_timeout := timeout;
327 let rec aux = function
329 | ((A.Inclusion (file,_)) as hd) :: tl ->
330 let file = resolve ~tptppath file in
331 let lexbuf = Lexing.from_channel (open_in file) in
332 let statements = Parser.main Lexer.yylex lexbuf in
333 hd :: aux (statements @ tl)
334 | hd::tl -> hd :: aux tl
336 let statements = aux [A.Inclusion (filename,[])] in
337 let grafite_ast_statements,_ =
340 let newst, ctx = convert_ast st ctx f in
345 (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
346 * which will show up using the following command line:
347 * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
348 let width = max_int in
349 let term_pp content_term =
350 let pres_term = TermContentPres.pp_ast content_term in
351 let dummy_tbl = Hashtbl.create 1 in
352 let markup = CicNotationPres.render dummy_tbl pres_term in
353 let s = BoxPp.render_to_string List.hd width markup ~map_unicode_to_tex:false in
355 ~pat:"\\\\forall [Ha-z][a-z0-9_]*" ~subst:(fun x -> "\n" ^ x) s
357 CicNotationPp.set_pp_term term_pp;
358 let lazy_term_pp = fun x -> assert false in
359 let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
360 GrafiteAstPp.pp_statement
361 ~map_unicode_to_tex:false ~term_pp ~lazy_term_pp ~obj_pp t
363 let buri = Pcre.replace ~pat:"\\.p$" ("cic:/matita/TPTP/" ^ filename) in
364 let extra_statements_start = [
365 GA.Executable(floc,GA.Command(floc,
366 GA.Set(floc,"baseuri",buri)))]
369 match raw_preamble with
371 pp (GA.Executable(floc,
372 GA.Command(floc,GA.Include(floc,"logic/equality.ma"))))
375 let extra_statements_end = [] in
377 (*[("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
378 ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
379 ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
380 ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
381 ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
382 ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")] *)
384 let s1 = List.map pp extra_statements_start in
388 LexiconAstPp.pp_command (LA.Alias(floc, LA.Ident_alias(n,s))) ^ ".")
391 let s3 = List.map pp grafite_ast_statements in
392 let s4 = List.map pp extra_statements_end in
393 String.concat "\n" (s1@[preamble]@s2@s3@s4)