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.NotEq (a,b)) -> (A.Eq (a,b) :: neg), pos
93 | A.Atom a -> neg, a::pos
96 let atom_of_formula f =
97 let neg, pos = atom_of_formula [] [] f in
101 let rec mk_arrow component tail = function
104 | Prop -> mk_ident prop
105 | Univ -> mk_ident universe
110 ((mk_ident "_"),Some (mk_ident component)),
111 mk_arrow component tail (n-1))
114 let build_ctx_for_arities univesally arities t =
115 let binder = if univesally then `Forall else `Exists in
116 let rec aux = function
118 | (name,(nargs,sort))::tl ->
121 (mk_ident name,Some (mk_arrow universe sort nargs)),
127 let convert_atom universally a =
129 | A.Proposition p -> mk_ident p
130 | A.Predicate (name,params) ->
131 PT.Appl ((mk_ident name) :: (List.map convert_term params))
132 | A.True -> mk_ident "True"
133 | A.False -> mk_ident "False"
135 | A.NotEq (l,r) -> (* removes the negation *)
136 PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
138 let rec aux2 = function
143 PT.Binder (`Forall, (mk_ident "_", Some (aux he)), aux2 tl)
145 PT.Appl [mk_ident "And";aux he;aux2 tl]
147 let arities = collect_arities_from_atom a in
148 let fv = collect_fv_from_atom a in
149 build_ctx_for_arities universally
151 (function (x,(0,Univ)) -> List.mem x fv | _-> false)
156 let collect_arities atom ctx =
157 let atoms = atom@(List.flatten (List.map atom_of_formula ctx)) in
158 collect_arities_from_atom atoms
161 let collect_arities_from_formulae f =
162 let rec collect_arities_from_formulae = function
163 | A.Disjunction (a,b) ->
164 collect_arities_from_formulae a @ collect_arities_from_formulae b
166 | A.Atom a -> collect_arities_from_atom [a]
168 HExtlib.list_uniq (List.sort compare (collect_arities_from_formulae f))
171 let is_formulae_1eq_negated f =
172 let atom = atom_of_formula f in
174 | [A.NotEq (l,r)] -> true
178 let collect_fv_1stord_from_formulae f =
179 let arities = collect_arities_from_formulae f in
180 let fv = collect_fv_from_formulae f in
182 (List.filter (function (x,(0,Univ)) -> List.mem x fv | _-> false) arities)
185 let rec convert_formula fv no_arities context f =
186 let atom = atom_of_formula f in
187 let t = convert_atom (fv = []) atom in
188 let rec build_ctx n = function
193 (mk_ident ("H" ^ string_of_int n),
194 Some (convert_formula [] true [] hp)),
197 let arities = if no_arities then [] else collect_arities atom context in
198 build_ctx_for_arities true arities (build_ctx 0 context)
201 let check_if_atom_is_negative = function
204 | A.Proposition _ -> false
205 | A.Predicate _ -> false
210 let rec check_if_formula_is_negative = function
211 | A.Disjunction (a,b) ->
212 check_if_formula_is_negative a && check_if_formula_is_negative b
213 | A.NegAtom a -> not (check_if_atom_is_negative a)
214 | A.Atom a -> check_if_atom_is_negative a
217 let ng_generate_tactics fv ueq_case context arities =
218 [ GA.Executable(floc,GA.NTactic(floc,
219 [GA.NIntro (floc,"Univ") ; GA.NDot(floc)])) ]
223 GA.Executable(floc,GA.NTactic(floc,
224 [GA.NIntro (floc,name);GA.NDot(floc)])))
229 GA.Executable(floc,GA.NTactic(floc,
230 [GA.NIntro (floc,"H"^string_of_int i);GA.NDot(floc)])))
237 [GA.Executable(floc,GA.NTactic(floc,
239 PT.Appl [mk_ident "ex_intro";PT.Implicit;PT.Implicit;
240 PT.Implicit;PT.Implicit]);GA.NBranch floc]));
241 GA.Executable(floc,GA.NTactic(floc,
242 [GA.NPos (floc,[2])]))])
245 [GA.Executable(floc,GA.NTactic(floc, [
246 if (*ueq_case*) true then
250 mk_ident ("H" ^ string_of_int i))
255 "depth",string_of_int 5;
256 "width",string_of_int 5;
257 "size",string_of_int 20;
258 "timeout",string_of_int 10;
261 GA.NSemicolon(floc)]));
263 GA.Executable(floc,GA.NTactic(floc, Some (GA.Try(floc,
264 GA.Assumption floc)), GA.Dot(floc)))
271 [GA.Executable(floc,GA.NTactic(floc, [GA.NShift floc;
272 GA.NSkip floc; GA.NMerge floc]))])
275 [GA.Executable(floc,GA.NCommand(floc, GA.NQed(floc)))]
278 let generate_tactics fv ueq_case =
279 [GA.Executable(floc,GA.Tactic(floc, Some
280 (GA.Intros (floc,(None,[]))),GA.Dot(floc)))] @
285 [GA.Executable(floc,GA.Tactic(floc, Some
286 (GA.Exists floc),GA.Branch floc));
287 GA.Executable(floc,GA.Tactic(floc, None,
288 (GA.Pos (floc,[2]))))])
291 [GA.Executable(floc,GA.Tactic(floc, Some (
292 if true (*ueq_case*) then
293 GA.AutoBatch (floc,([],["paramodulation","";
294 "timeout",string_of_int !paramod_timeout]))
296 GA.AutoBatch (floc,([],[
297 "depth",string_of_int 5;
298 "width",string_of_int 5;
299 "size",string_of_int 20;
300 "timeout",string_of_int 10;
303 GA.Semicolon(floc)));
304 GA.Executable(floc,GA.Tactic(floc, Some (GA.Try(floc,
305 GA.Assumption floc)), GA.Dot(floc)))
311 [GA.Executable(floc,GA.Tactic(floc, None, GA.Shift floc));
312 GA.Executable(floc,GA.NonPunctuationTactical(floc, GA.Skip floc,
316 [GA.Executable(floc,GA.Command(floc, GA.Print(floc,"proofterm")));
317 GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))]
320 let convert_ast ng statements context = function
322 let s = String.sub s 1 (String.length s - 1) in
324 if s.[String.length s - 1] = '\n' then
325 String.sub s 0 (String.length s - 1)
329 statements @ [GA.Comment (floc,GA.Note (floc,s))],
331 | A.Inclusion (s,_) ->
335 floc,"Inclusion of: " ^ s))], context
336 | A.AnnotatedFormula (name,kind,f,_,_) ->
340 statements, f::context
341 | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
342 statements, f::context
343 | A.Negated_conjecture ->
344 let ueq_case = is_formulae_1eq_negated f in
345 let fv = collect_fv_1stord_from_formulae f in
350 (mk_ident universe,Some (PT.Sort (`Type (CicUniv.fresh ())))),
351 convert_formula fv false context f)
353 let o = PT.Theorem (`Theorem,name,f,None) in
355 [ GA.Executable(floc,GA.Command(floc,
356 (*if ng then GA.NObj (floc,o) else*) GA.Obj(floc,o))); ] @
358 ng_generate_tactics fv ueq_case context
359 (let atom = atom_of_formula old_f in collect_arities atom context)
360 else generate_tactics fv ueq_case),
368 | A.Unknown -> assert false
372 let resolve ~tptppath s =
374 if Filename.check_suffix s ".p" then
375 (assert (String.length s > 5);
376 let prefix = String.sub s 0 3 in
377 tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
381 if HExtlib.is_regular resolved_name then
385 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
391 let tptp2grafite ?(timeout=600) ?(def_depth=10) ?raw_preamble ~tptppath ~filename ~ng () =
392 paramod_timeout := timeout;
394 let rec aux = function
396 | ((A.Inclusion (file,_)) as hd) :: tl ->
397 let file = resolve ~tptppath file in
398 let lexbuf = Lexing.from_channel (open_in file) in
399 let statements = Parser.main Lexer.yylex lexbuf in
400 hd :: aux (statements @ tl)
401 | hd::tl -> hd :: aux tl
403 let statements = aux [A.Inclusion (filename,[])] in
404 let grafite_ast_statements,_ =
407 let newst, ctx = convert_ast ng st ctx f in
412 (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
413 * which will show up using the following command line:
414 * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
415 let width = max_int in
416 let term_pp prec content_term =
417 let pres_term = TermContentPres.pp_ast content_term in
418 let lookup_uri = fun _ -> None in
419 let markup = CicNotationPres.render ~lookup_uri ~prec pres_term in
420 let s = BoxPp.render_to_string List.hd width markup ~map_unicode_to_tex:false in
422 ~rex:(Pcre.regexp ~flags:[`UTF8] "∀[Ha-z][a-z0-9_]*") ~subst:(fun x -> "\n" ^ x)
425 CicNotationPp.set_pp_term (term_pp 90);
426 let lazy_term_pp = fun x -> assert false in
427 let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
428 Pcre.replace ~pat:"theorem" ~templ:"ntheorem"
429 (GrafiteAstPp.pp_statement
430 ~map_unicode_to_tex:false ~term_pp:(term_pp 19) ~lazy_term_pp ~obj_pp t)
432 let buri = Pcre.replace ~pat:"\\.p$" ("cic:/matita/TPTP/" ^ filename) in
433 let extra_statements_start = [
434 (*GA.Executable(floc,GA.Command(floc,
435 GA.Set(floc,"baseuri",buri)))*)]
438 match raw_preamble with
440 pp (GA.Executable(floc,
441 GA.Command(floc,GA.Include(floc,true,"logic/equality.ma"))))
444 let extra_statements_end = [] in
446 (*[("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
447 ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
448 ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
449 ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
450 ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
451 ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")] *)
453 let s1 = List.map pp extra_statements_start in
457 LexiconAstPp.pp_command (LA.Alias(floc, LA.Ident_alias(n,s))) ^ ".")
460 let s3 = List.map pp grafite_ast_statements in
461 let s4 = List.map pp extra_statements_end in
462 String.concat "\n" (s1@[preamble]@s2@s3@s4)