4 type sort = Prop | Univ;;
6 let floc = HExtlib.dummy_floc;;
9 let paramod_timeout = ref 600;;
12 let universe = "Univ" ;;
20 PT.Ident ((try List.assoc s kw with Not_found -> s),None)
23 let rec collect_arities_from_term = function
24 | A.Constant name -> [name,(0,Univ)]
25 | A.Variable name -> [name,(0,Univ)]
26 | A.Function (name,l) ->
27 (name,(List.length l,Univ))::
28 List.flatten (List.map collect_arities_from_term l)
31 let rec collect_fv_from_term = function
32 | A.Constant name -> []
33 | A.Variable name -> [name]
35 List.flatten (List.map collect_fv_from_term l)
38 let collect_arities_from_atom a =
40 | A.Proposition name -> [name,(0,Prop)]
41 | A.Predicate (name,args) ->
42 (name,(List.length args,Prop)) ::
43 (List.flatten (List.map collect_arities_from_term args))
47 collect_arities_from_term t1 @ collect_arities_from_term t2
49 collect_arities_from_term t1 @ collect_arities_from_term t2
51 HExtlib.list_uniq (List.sort compare (List.flatten (List.map aux a)))
54 let collect_fv_from_atom a =
56 | A.Proposition name -> [name]
57 | A.Predicate (name,args) ->
58 name :: List.flatten (List.map collect_fv_from_term args)
61 | A.Eq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
62 | A.NotEq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
64 let rec aux2 = function
66 | hd::tl -> aux hd @ aux2 tl
68 HExtlib.list_uniq (List.sort compare (aux2 a))
71 let rec collect_fv_from_formulae = function
72 | A.Disjunction (a,b) ->
73 collect_fv_from_formulae a @ collect_fv_from_formulae b
75 | A.Atom a -> collect_fv_from_atom [a]
78 let rec convert_term = function
79 | A.Variable x -> mk_ident x
80 | A.Constant x -> mk_ident x
81 | A.Function (name, args) ->
82 PT.Appl (mk_ident name :: List.map convert_term args)
85 let rec atom_of_formula neg pos = function
86 | A.Disjunction (a,b) ->
87 let neg, pos = atom_of_formula neg pos a in
88 atom_of_formula neg pos b
89 | A.NegAtom a -> a::neg, pos
90 | A.Atom (A.NotEq (a,b)) -> (A.Eq (a,b) :: neg), pos
91 | A.Atom a -> neg, a::pos
94 let atom_of_formula f =
95 let neg, pos = atom_of_formula [] [] f in
99 let rec mk_arrow component tail = function
102 | Prop -> mk_ident prop
103 | Univ -> mk_ident universe
108 ((mk_ident "_"),Some (mk_ident component)),
109 mk_arrow component tail (n-1))
112 let build_ctx_for_arities univesally arities t =
113 let binder = if univesally then `Forall else `Exists in
114 let rec aux = function
116 | (name,(nargs,sort))::tl ->
119 (mk_ident name,Some (mk_arrow universe sort nargs)),
125 let convert_atom universally a =
127 | A.Proposition p -> mk_ident p
128 | A.Predicate (name,params) ->
129 PT.Appl ((mk_ident name) :: (List.map convert_term params))
130 | A.True -> mk_ident "True"
131 | A.False -> mk_ident "False"
133 | A.NotEq (l,r) -> (* removes the negation *)
134 PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
136 let rec aux2 = function
141 PT.Binder (`Forall, (mk_ident "_", Some (aux he)), aux2 tl)
143 PT.Appl [mk_ident "And";aux he;aux2 tl]
145 let arities = collect_arities_from_atom a in
146 let fv = collect_fv_from_atom a in
147 build_ctx_for_arities universally
149 (function (x,(0,Univ)) -> List.mem x fv | _-> false)
154 let collect_arities atom ctx =
155 let atoms = atom@(List.flatten (List.map atom_of_formula ctx)) in
156 collect_arities_from_atom atoms
159 let collect_arities_from_formulae f =
160 let rec collect_arities_from_formulae = function
161 | A.Disjunction (a,b) ->
162 collect_arities_from_formulae a @ collect_arities_from_formulae b
164 | A.Atom a -> collect_arities_from_atom [a]
166 HExtlib.list_uniq (List.sort compare (collect_arities_from_formulae f))
169 let is_formulae_1eq_negated f =
170 let atom = atom_of_formula f in
172 | [A.NotEq (l,r)] -> true
176 let collect_fv_1stord_from_formulae f =
177 let arities = collect_arities_from_formulae f in
178 let fv = collect_fv_from_formulae f in
180 (List.filter (function (x,(0,Univ)) -> List.mem x fv | _-> false) arities)
183 let rec convert_formula fv no_arities context f =
184 let atom = atom_of_formula f in
185 let t = convert_atom (fv = []) atom in
186 let rec build_ctx n = function
191 (mk_ident ("H" ^ string_of_int n),
192 Some (convert_formula [] true [] hp)),
195 let arities = if no_arities then [] else collect_arities atom context in
196 build_ctx_for_arities true arities (build_ctx 0 context)
199 let check_if_atom_is_negative = function
202 | A.Proposition _ -> false
203 | A.Predicate _ -> false
208 let rec check_if_formula_is_negative = function
209 | A.Disjunction (a,b) ->
210 check_if_formula_is_negative a && check_if_formula_is_negative b
211 | A.NegAtom a -> not (check_if_atom_is_negative a)
212 | A.Atom a -> check_if_atom_is_negative a
215 let ng_generate_tactics fv ueq_case context arities =
216 [ GA.Executable(floc,GA.NTactic(floc,
217 [GA.NIntro (floc,"Univ") ; GA.NDot(floc)])) ]
221 GA.Executable(floc,GA.NTactic(floc,
222 [GA.NIntro (floc,(try List.assoc name kw with Not_found -> name));
228 GA.Executable(floc,GA.NTactic(floc,
229 [GA.NIntro (floc,"H"^string_of_int i);GA.NDot(floc)])))
236 [GA.Executable(floc,GA.NTactic(floc,
238 PT.Appl [mk_ident "ex_intro";PT.Implicit;PT.Implicit;
239 PT.Implicit;PT.Implicit]);GA.NBranch floc]));
240 GA.Executable(floc,GA.NTactic(floc,
241 [GA.NPos (floc,[2])]))])
244 [GA.Executable(floc,GA.NTactic(floc, [
245 if (*ueq_case*) true then
249 mk_ident ("H" ^ string_of_int i))
254 "depth",string_of_int 5;
255 "width",string_of_int 5;
256 "size",string_of_int 20;
257 "timeout",string_of_int 10;
260 GA.NSemicolon(floc)]));
262 GA.Executable(floc,GA.NTactic(floc, Some (GA.Try(floc,
263 GA.Assumption floc)), GA.Dot(floc)))
270 [GA.Executable(floc,GA.NTactic(floc, [GA.NShift floc;
271 GA.NSkip floc; GA.NMerge floc]))])
274 [GA.Executable(floc,GA.NTactic(floc,[GA.NTry(floc, GA.NAssumption(floc));
275 GA.NSemicolon(floc)]))]@
276 [GA.Executable(floc,GA.NCommand(floc, GA.NQed(floc)))]
279 let generate_tactics fv ueq_case =
280 [GA.Executable(floc,GA.Tactic(floc, Some
281 (GA.Intros (floc,(None,[]))),GA.Dot(floc)))] @
286 [GA.Executable(floc,GA.Tactic(floc, Some
287 (GA.Exists floc),GA.Branch floc));
288 GA.Executable(floc,GA.Tactic(floc, None,
289 (GA.Pos (floc,[2]))))])
292 [GA.Executable(floc,GA.Tactic(floc, Some (
293 if true (*ueq_case*) then
294 GA.AutoBatch (floc,([],["paramodulation","";
295 "timeout",string_of_int !paramod_timeout]))
297 GA.AutoBatch (floc,([],[
298 "depth",string_of_int 5;
299 "width",string_of_int 5;
300 "size",string_of_int 20;
301 "timeout",string_of_int 10;
304 GA.Semicolon(floc)));
305 GA.Executable(floc,GA.Tactic(floc, Some (GA.Try(floc,
306 GA.Assumption floc)), GA.Dot(floc)))
312 [GA.Executable(floc,GA.Tactic(floc, None, GA.Shift floc));
313 GA.Executable(floc,GA.NonPunctuationTactical(floc, GA.Skip floc,
317 [GA.Executable(floc,GA.Command(floc, GA.Print(floc,"proofterm")));
318 GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))]
321 let convert_ast ng statements context = function
323 let s = String.sub s 1 (String.length s - 1) in
325 if s.[String.length s - 1] = '\n' then
326 String.sub s 0 (String.length s - 1)
330 statements @ [GA.Comment (floc,GA.Note (floc,s))],
332 | A.Inclusion (s,_) ->
336 floc,"Inclusion of: " ^ s))], context
337 | A.AnnotatedFormula (name,kind,f,_,_) ->
341 statements, f::context
342 | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
343 statements, f::context
344 | A.Negated_conjecture ->
345 let ueq_case = is_formulae_1eq_negated f in
346 let fv = collect_fv_1stord_from_formulae f in
351 (mk_ident universe,Some (PT.Sort (`Type (CicUniv.fresh ())))),
352 convert_formula fv false context f)
354 let o = PT.Theorem (`Theorem,name,f,None) in
356 [ GA.Executable(floc,GA.Command(floc,
357 (*if ng then GA.NObj (floc,o) else*) GA.Obj(floc,o))); ] @
359 ng_generate_tactics fv ueq_case context
360 (let atom = atom_of_formula old_f in collect_arities atom context)
361 else generate_tactics fv ueq_case),
369 | A.Unknown -> assert false
373 let resolve ~tptppath s =
375 if Filename.check_suffix s ".p" then
376 (assert (String.length s > 5);
377 let prefix = String.sub s 0 3 in
378 tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
382 if HExtlib.is_regular resolved_name then
386 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
392 let tptp2grafite ?(timeout=600) ?(def_depth=10) ?raw_preamble ~tptppath ~filename ~ng () =
393 paramod_timeout := timeout;
395 let rec aux = function
397 | ((A.Inclusion (file,_)) as hd) :: tl ->
398 let file = resolve ~tptppath file in
399 let lexbuf = Lexing.from_channel (open_in file) in
400 let statements = Parser.main Lexer.yylex lexbuf in
401 hd :: aux (statements @ tl)
402 | hd::tl -> hd :: aux tl
404 let statements = aux [A.Inclusion (filename,[])] in
405 let grafite_ast_statements,_ =
408 let newst, ctx = convert_ast ng st ctx f in
413 (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
414 * which will show up using the following command line:
415 * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
416 let width = max_int in
417 let term_pp prec content_term =
418 let pres_term = TermContentPres.pp_ast content_term in
419 let lookup_uri = fun _ -> None in
420 let markup = CicNotationPres.render ~lookup_uri ~prec pres_term in
421 let s = BoxPp.render_to_string List.hd width markup ~map_unicode_to_tex:false in
423 ~rex:(Pcre.regexp ~flags:[`UTF8] "∀[Ha-z][a-z0-9_]*") ~subst:(fun x -> "\n" ^ x)
426 CicNotationPp.set_pp_term (term_pp 90);
427 let lazy_term_pp = fun x -> assert false in
428 let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
429 Pcre.replace ~pat:"theorem" ~templ:"ntheorem"
430 (GrafiteAstPp.pp_statement
431 ~map_unicode_to_tex:false ~term_pp:(term_pp 19) ~lazy_term_pp ~obj_pp t)
433 let buri = Pcre.replace ~pat:"\\.p$" ("cic:/matita/TPTP/" ^ filename) in
434 let extra_statements_start = [
435 (*GA.Executable(floc,GA.Command(floc,
436 GA.Set(floc,"baseuri",buri)))*)]
439 match raw_preamble with
441 pp (GA.Executable(floc,
442 GA.Command(floc,GA.Include(floc,true,"logic/equality.ma"))))
445 let extra_statements_end = [] in
447 (*[("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
448 ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
449 ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
450 ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
451 ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
452 ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")] *)
454 let s1 = List.map pp extra_statements_start in
458 LexiconAstPp.pp_command (LA.Alias(floc, LA.Ident_alias(n,s))) ^ ".")
461 let s3 = List.map pp grafite_ast_statements in
462 let s4 = List.map pp extra_statements_end in
463 String.concat "\n" (s1@[preamble]@s2@s3@s4)
467 let parse _ = assert false;;