1 module GA = GrafiteAst;;
2 module LA = LexiconAst;;
3 module PT = CicNotationPt;;
5 let floc = HExtlib.dummy_floc;;
12 PT.Ident ((try List.assoc s kw with Not_found -> s),None)
16 let rec collect_arities_from_term = function
17 | A.Constant name -> [name,0]
18 | A.Variable name -> []
19 | A.Function (name,l) ->
20 (name,List.length l) :: List.flatten (List.map collect_arities_from_term l)
23 let rec collect_fv_from_term = function
24 | A.Constant name -> []
25 | A.Variable name -> [name]
27 List.flatten (List.map collect_fv_from_term l)
30 let collect_arities_from_atom a =
32 | A.Proposition name -> assert false
33 | A.Predicate _ -> assert false
36 | A.Eq (t1,t2) -> collect_arities_from_term t1 @ collect_arities_from_term t2
37 | A.NotEq (t1,t2) -> collect_arities_from_term t1 @ collect_arities_from_term t2
42 let collect_fv_from_atom a =
44 | A.Proposition name -> assert false
45 | A.Predicate _ -> assert false
48 | A.Eq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
49 | A.NotEq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
51 HExtlib.list_uniq (List.sort compare (aux a))
54 let rec convert_term = function
55 | A.Variable x -> mk_ident x
56 | A.Constant x -> mk_ident x
57 | A.Function (name, args) ->
58 PT.Appl (mk_ident name :: List.map convert_term args)
61 let atom_of_formula = function
62 | A.Disjunction _ -> assert false
63 | A.NegAtom a -> a (* removes the negation *)
67 let rec mk_arrow component = function
68 | 0 -> mk_ident component
72 ((mk_ident "_"),Some (mk_ident component)),
73 mk_arrow component (n-1))
76 let build_ctx_for_arities arities t =
77 let rec aux = function
82 (mk_ident name,Some (mk_arrow "A" nargs)),
90 | A.Proposition _ -> assert false
91 | A.Predicate (name,params) ->
92 prerr_endline ("Predicate is unsupported: " ^ name);
94 | A.True -> mk_ident "True"
95 | A.False -> mk_ident "False"
97 | A.NotEq (l,r) -> (* removes the negation *)
98 PT.Appl [mk_ident "eq";mk_ident "A";convert_term l;convert_term r]
100 build_ctx_for_arities (List.map (fun x -> (x,0)) (collect_fv_from_atom a)) (aux a)
103 let collect_arities atom ctx =
104 let atoms = atom::(List.map atom_of_formula ctx) in
105 HExtlib.list_uniq (List.sort (fun (a,_) (b,_) -> compare a b)
106 (List.flatten (List.map collect_arities_from_atom atoms)))
109 let rec convert_formula no_arities context f =
110 let atom = atom_of_formula f in
111 let t = convert_atom atom in
112 let rec build_ctx n = function
117 (mk_ident ("H" ^ string_of_int n),
118 Some (convert_formula true [] hp)),
121 let arities = if no_arities then [] else collect_arities atom context in
122 build_ctx_for_arities arities (build_ctx 0 context)
125 let convert_ast statements context = function
127 let s = String.sub s 1 (String.length s - 1) in
129 if s.[String.length s - 1] = '\n' then
130 String.sub s 0 (String.length s - 1)
134 statements @ [GA.Comment (floc,GA.Note (floc,s))],
136 | A.Inclusion (s,_) ->
140 floc,"Inclusion of: " ^ s))], context
141 | A.AnnotatedFormula (name,kind,f,_,_) ->
145 statements, f::context
146 | A.Negated_conjecture ->
150 (mk_ident "A",Some (PT.Sort `Set)),
151 convert_formula false context f)
153 let o = PT.Theorem (`Theorem,name,f,None) in
155 GA.Executable(floc,GA.Command(floc,GA.Obj(floc,o)));
156 GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
157 GA.Intros (floc,None,[])),Some (GA.Dot(floc))));
158 GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
159 GA.Auto (floc,None,None,Some "paramodulation",None)),
160 Some (GA.Dot(floc))));
161 GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))],
169 | A.Unknown -> assert false
173 let tptppath = ref "./";;
174 let librarymode = ref false;;
177 Arg.String (fun x -> tptppath := x),
178 "Where to find the Axioms/ and Problems/ directory");
181 "... not supported yet")
187 if Filename.check_suffix s ".p" then
188 (assert (String.length s > 5);
189 let prefix = String.sub s 0 3 in
190 !tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
194 if HExtlib.is_regular resolved_name then
198 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
205 let usage = "Usage: tptp2grafite [options] file" in
206 let inputfile = ref "" in
207 Arg.parse spec (fun s -> inputfile := s) usage;
208 if !inputfile = "" then
213 let rec aux = function
215 | ((A.Inclusion (file,_)) as hd) :: tl ->
216 let file = resolve file in
217 let lexbuf = Lexing.from_channel (open_in file) in
218 let statements = Parser.main Lexer.yylex lexbuf in
219 hd :: aux (statements @ tl)
220 | hd::tl -> hd :: aux tl
222 let statements = aux [A.Inclusion (!inputfile ^ ".p",[])] in
223 let grafite_ast_statements,_ =
226 let newst, ctx = convert_ast st ctx f in
231 (* for a correct pp we should disambiguate the term... *)
232 let term_pp = CicNotationPp.pp_term in
233 let lazy_term_pp = fun x -> assert false in
234 let obj_pp = CicNotationPp.pp_obj in
236 (GrafiteAstPp.pp_statement ~term_pp ~lazy_term_pp ~obj_pp t)
238 let extra_statements_start = [
239 GA.Executable(floc,GA.Command(floc,
240 GA.Set(floc,"baseuri","cic:/matita/TPTP/" ^ !inputfile)));
241 GA.Executable(floc,GA.Command(floc, GA.Include(floc,"legacy/coq.ma")))]
243 List.iter pp extra_statements_start;
245 (LexiconAstPp.pp_command
247 LA.Ident_alias("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)"))) ^ ".");
248 List.iter pp grafite_ast_statements;