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 = function
88 | A.Disjunction (a,b) ->
89 atom_of_formula a @ atom_of_formula b
90 | A.NegAtom a -> [a] (* removes the negation *)
94 let rec mk_arrow component tail = function
97 | Prop -> mk_ident prop
98 | Univ -> mk_ident universe
103 ((mk_ident "_"),Some (mk_ident component)),
104 mk_arrow component tail (n-1))
107 let build_ctx_for_arities univesally arities t =
108 let binder = if univesally then `Forall else `Exists in
109 let rec aux = function
111 | (name,(nargs,sort))::tl ->
114 (mk_ident name,Some (mk_arrow universe sort nargs)),
120 let convert_atom universally a =
122 | A.Proposition p -> mk_ident p
123 | A.Predicate (name,params) ->
124 PT.Appl ((mk_ident name) :: (List.map convert_term params))
125 | A.True -> mk_ident "True"
126 | A.False -> mk_ident "False"
128 | A.NotEq (l,r) -> (* removes the negation *)
129 PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
131 let rec aux2 = function
136 PT.Binder (`Forall, (mk_ident "_", Some (aux he)), aux2 tl)
138 PT.Appl [mk_ident "And";aux he;aux2 tl]
140 let arities = collect_arities_from_atom a in
141 let fv = collect_fv_from_atom a in
142 build_ctx_for_arities universally
144 (function (x,(0,Univ)) -> List.mem x fv | _-> false)
149 let collect_arities atom ctx =
150 let atoms = atom@(List.flatten (List.map atom_of_formula ctx)) in
151 collect_arities_from_atom atoms
154 let collect_arities_from_formulae f =
155 let rec collect_arities_from_formulae = function
156 | A.Disjunction (a,b) ->
157 collect_arities_from_formulae a @ collect_arities_from_formulae b
159 | A.Atom a -> collect_arities_from_atom [a]
161 HExtlib.list_uniq (List.sort compare (collect_arities_from_formulae f))
164 let is_formulae_1eq_negated f =
165 let atom = atom_of_formula f in
167 | [A.NotEq (l,r)] -> true
171 let collect_fv_1stord_from_formulae f =
172 let arities = collect_arities_from_formulae f in
173 let fv = collect_fv_from_formulae f in
175 (List.filter (function (x,(0,Univ)) -> List.mem x fv | _-> false) arities)
178 let rec convert_formula fv no_arities context f =
179 let atom = atom_of_formula f in
180 let t = convert_atom (fv = []) atom in
181 let rec build_ctx n = function
186 (mk_ident ("H" ^ string_of_int n),
187 Some (convert_formula [] true [] hp)),
190 let arities = if no_arities then [] else collect_arities atom context in
191 build_ctx_for_arities true arities (build_ctx 0 context)
194 let check_if_atom_is_negative = function
197 | A.Proposition _ -> false
198 | A.Predicate _ -> false
203 let rec check_if_formula_is_negative = function
204 | A.Disjunction (a,b) ->
205 check_if_formula_is_negative a && check_if_formula_is_negative b
206 | A.NegAtom a -> not (check_if_atom_is_negative a)
207 | A.Atom a -> check_if_atom_is_negative a
210 let convert_ast statements context = function
212 let s = String.sub s 1 (String.length s - 1) in
214 if s.[String.length s - 1] = '\n' then
215 String.sub s 0 (String.length s - 1)
219 statements @ [GA.Comment (floc,GA.Note (floc,s))],
221 | A.Inclusion (s,_) ->
225 floc,"Inclusion of: " ^ s))], context
226 | A.AnnotatedFormula (name,kind,f,_,_) ->
230 statements, f::context
231 | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
232 statements, f::context
233 | A.Negated_conjecture ->
234 let ueq_case = is_formulae_1eq_negated f in
235 let fv = collect_fv_1stord_from_formulae f in
238 prerr_endline ("FREE VARIABLES: " ^ String.concat "," fv);
243 (mk_ident universe,Some (PT.Sort `Set)),
244 convert_formula fv false context f)
246 let o = PT.Theorem (`Theorem,name,f,None) in
248 GA.Executable(floc,GA.Command(floc,GA.Obj(floc,o)));
249 GA.Executable(floc,GA.Tactic(floc, Some
250 (GA.Intros (floc,(None,[]))),GA.Dot(floc)))] @
255 [GA.Executable(floc,GA.Tactic(floc, Some
256 (GA.Exists floc),GA.Branch floc));
257 GA.Executable(floc,GA.Tactic(floc, None,
258 (GA.Pos (floc,[2]))))])
261 [GA.Executable(floc,GA.Tactic(floc, Some (
263 GA.AutoBatch (floc,([],["paramodulation","";
264 "timeout",string_of_int !paramod_timeout]))
266 GA.AutoBatch (floc,([],["depth",string_of_int !depth]))
268 GA.Semicolon(floc)));
269 GA.Executable(floc,GA.Tactic(floc, Some (GA.Try(floc,
270 GA.Assumption floc)), GA.Dot(floc)))
276 [GA.Executable(floc,GA.Tactic(floc, None, GA.Shift floc));
277 GA.Executable(floc,GA.NonPunctuationTactical(floc, GA.Skip floc,
281 [GA.Executable(floc,GA.Command(floc, GA.Print(floc,"proofterm")));
282 GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))],
290 | A.Unknown -> assert false
294 let resolve ~tptppath s =
296 if Filename.check_suffix s ".p" then
297 (assert (String.length s > 5);
298 let prefix = String.sub s 0 3 in
299 tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
303 if HExtlib.is_regular resolved_name then
307 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
313 let tptp2grafite ?(timeout=600) ?(def_depth=10) ?raw_preamble ~tptppath ~filename () =
314 paramod_timeout := timeout;
316 let rec aux = function
318 | ((A.Inclusion (file,_)) as hd) :: tl ->
319 let file = resolve ~tptppath file in
320 let lexbuf = Lexing.from_channel (open_in file) in
321 let statements = Parser.main Lexer.yylex lexbuf in
322 hd :: aux (statements @ tl)
323 | hd::tl -> hd :: aux tl
325 let statements = aux [A.Inclusion (filename,[])] in
326 let grafite_ast_statements,_ =
329 let newst, ctx = convert_ast st ctx f in
334 (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
335 * which will show up using the following command line:
336 * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
337 let width = max_int in
338 let term_pp content_term =
339 let pres_term = TermContentPres.pp_ast content_term in
340 let dummy_tbl = Hashtbl.create 1 in
341 let markup = CicNotationPres.render dummy_tbl pres_term in
342 let s = BoxPp.render_to_string List.hd width markup ~map_unicode_to_tex:false in
344 ~pat:"\\\\forall [Ha-z][a-z0-9_]*" ~subst:(fun x -> "\n" ^ x) s
346 CicNotationPp.set_pp_term term_pp;
347 let lazy_term_pp = fun x -> assert false in
348 let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
349 GrafiteAstPp.pp_statement
350 ~map_unicode_to_tex:false ~term_pp ~lazy_term_pp ~obj_pp t
352 let buri = Pcre.replace ~pat:"\\.p$" ("cic:/matita/TPTP/" ^ filename) in
353 let extra_statements_start = [
354 GA.Executable(floc,GA.Command(floc,
355 GA.Set(floc,"baseuri",buri)))]
358 match raw_preamble with
360 pp (GA.Executable(floc,
361 GA.Command(floc,GA.Include(floc,"logic/equality.ma"))))
364 let extra_statements_end = [] in
366 (*[("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
367 ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
368 ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
369 ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
370 ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
371 ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")] *)
373 let s1 = List.map pp extra_statements_start in
377 LexiconAstPp.pp_command (LA.Alias(floc, LA.Ident_alias(n,s))) ^ ".")
380 let s3 = List.map pp grafite_ast_statements in
381 let s4 = List.map pp extra_statements_end in
382 String.concat "\n" (s1@[preamble]@s2@s3@s4)