1 module GA = GrafiteAst;;
2 module LA = LexiconAst;;
3 module PT = CicNotationPt;;
5 let floc = HExtlib.dummy_floc;;
7 let universe = "Univ" ;;
14 PT.Ident ((try List.assoc s kw with Not_found -> s),None)
17 let rec collect_arities_from_term = function
18 | A.Constant name -> [name,0]
19 | A.Variable name -> []
20 | A.Function (name,l) ->
21 (name,List.length l)::List.flatten (List.map collect_arities_from_term l)
24 let rec collect_fv_from_term = function
25 | A.Constant name -> []
26 | A.Variable name -> [name]
28 List.flatten (List.map collect_fv_from_term l)
31 let collect_arities_from_atom a =
33 | A.Proposition name -> assert false
34 | A.Predicate _ -> assert false
37 | A.Eq (t1,t2) -> collect_arities_from_term t1 @ collect_arities_from_term t2
38 | A.NotEq (t1,t2) -> collect_arities_from_term t1 @ collect_arities_from_term t2
43 let collect_fv_from_atom a =
45 | A.Proposition name -> assert false
46 | A.Predicate _ -> assert false
49 | A.Eq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
50 | A.NotEq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
52 HExtlib.list_uniq (List.sort compare (aux a))
55 let collect_fv_from_formulae = function
56 | A.Disjunction _ -> assert false
58 | A.Atom a -> collect_fv_from_atom a
61 let rec convert_term = function
62 | A.Variable x -> mk_ident x
63 | A.Constant x -> mk_ident x
64 | A.Function (name, args) ->
65 PT.Appl (mk_ident name :: List.map convert_term args)
68 let atom_of_formula = function
69 | A.Disjunction _ -> assert false
70 | A.NegAtom a -> a (* removes the negation *)
74 let rec mk_arrow component = function
75 | 0 -> mk_ident component
79 ((mk_ident "_"),Some (mk_ident component)),
80 mk_arrow component (n-1))
83 let build_ctx_for_arities univesally arities t =
84 let binder = if univesally then `Forall else `Exists in
85 let rec aux = function
90 (mk_ident name,Some (mk_arrow universe nargs)),
96 let convert_atom universally a =
98 | A.Proposition _ -> assert false
99 | A.Predicate (name,params) ->
100 prerr_endline ("Predicate is unsupported: " ^ name);
102 | A.True -> mk_ident "True"
103 | A.False -> mk_ident "False"
105 | A.NotEq (l,r) -> (* removes the negation *)
106 PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
108 build_ctx_for_arities universally
109 (List.map (fun x -> (x,0)) (collect_fv_from_atom a)) (aux a)
112 let collect_arities atom ctx =
113 let atoms = atom::(List.map atom_of_formula ctx) in
114 HExtlib.list_uniq (List.sort (fun (a,_) (b,_) -> compare a b)
115 (List.flatten (List.map collect_arities_from_atom atoms)))
118 let assert_formulae_is_1eq_negated f =
119 let atom = atom_of_formula f in
121 | A.Eq (l,r) -> failwith "Negated formula is not negated"
122 | A.NotEq (l,r) -> ()
123 | _ -> failwith "Not a unit equality formula"
126 let rec convert_formula fv no_arities context f =
127 let atom = atom_of_formula f in
128 let t = convert_atom (fv = []) atom in
129 let rec build_ctx n = function
134 (mk_ident ("H" ^ string_of_int n),
135 Some (convert_formula [] true [] hp)),
138 let arities = if no_arities then [] else collect_arities atom context in
139 build_ctx_for_arities true arities (build_ctx 0 context)
142 let check_if_atom_is_negative = function
143 | A.True | A.False | A.Proposition _ | A.Predicate _ -> assert false
148 let check_if_formula_is_negative = function
149 | A.Disjunction _ -> assert false
150 | A.NegAtom a -> not (check_if_atom_is_negative a)
151 | A.Atom a -> check_if_atom_is_negative a
154 let convert_ast statements context = function
156 let s = String.sub s 1 (String.length s - 1) in
158 if s.[String.length s - 1] = '\n' then
159 String.sub s 0 (String.length s - 1)
163 statements @ [GA.Comment (floc,GA.Note (floc,s))],
165 | A.Inclusion (s,_) ->
169 floc,"Inclusion of: " ^ s))], context
170 | A.AnnotatedFormula (name,kind,f,_,_) ->
174 statements, f::context
175 | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
176 statements, f::context
177 | A.Negated_conjecture ->
178 assert_formulae_is_1eq_negated f;
179 let fv = collect_fv_from_formulae f in
181 prerr_endline ("FREE VARIABLES: " ^ String.concat "," fv);
185 (mk_ident universe,Some (PT.Sort `Set)),
186 convert_formula fv false context f)
188 let o = PT.Theorem (`Theorem,name,f,None) in
190 GA.Executable(floc,GA.Command(floc,GA.Obj(floc,o)));
191 GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
192 GA.Intros (floc,None,[])),Some (GA.Dot(floc))))] @
197 [GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
198 GA.Exists floc),Some (GA.Branch floc)));
199 GA.Executable(floc,GA.Tactical(floc,
200 GA.Pos (floc,[2]),None))])
203 [GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
204 GA.Auto (floc,None,None,Some "paramodulation",None)),
205 Some (GA.Dot(floc))))]@
210 [GA.Executable(floc,GA.Tactical(floc, GA.Shift floc, None));
211 GA.Executable(floc,GA.Tactical(floc, GA.Skip floc,Some
215 [GA.Executable(floc,GA.Tactical(floc, GA.Try(floc,
216 GA.Tactic (floc, GA.Assumption floc)), Some (GA.Dot(floc))));
217 GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))],
225 | A.Unknown -> assert false
229 let tptppath = ref "./";;
230 let librarymode = ref false;;
233 Arg.String (fun x -> tptppath := x),
234 "Where to find the Axioms/ and Problems/ directory");
237 "... not supported yet")
243 if Filename.check_suffix s ".p" then
244 (assert (String.length s > 5);
245 let prefix = String.sub s 0 3 in
246 !tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
250 if HExtlib.is_regular resolved_name then
254 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
261 let usage = "Usage: tptp2grafite [options] file" in
262 let inputfile = ref "" in
263 Arg.parse spec (fun s -> inputfile := s) usage;
264 if !inputfile = "" then
269 let rec aux = function
271 | ((A.Inclusion (file,_)) as hd) :: tl ->
272 let file = resolve file in
273 let lexbuf = Lexing.from_channel (open_in file) in
274 let statements = Parser.main Lexer.yylex lexbuf in
275 hd :: aux (statements @ tl)
276 | hd::tl -> hd :: aux tl
278 let statements = aux [A.Inclusion (!inputfile ^ ".p",[])] in
279 let grafite_ast_statements,_ =
282 let newst, ctx = convert_ast st ctx f in
287 (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
288 * which will show up using the following command line:
289 * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
290 let width = max_int in
291 let term_pp content_term =
292 let pres_term = TermContentPres.pp_ast content_term in
293 let dummy_tbl = Hashtbl.create 1 in
294 let markup = CicNotationPres.render dummy_tbl pres_term in
295 let s = BoxPp.render_to_string width markup in
297 ~pat:"\\\\forall [Ha-z][a-z0-9_]*" ~subst:(fun x -> "\n" ^ x) s
299 CicNotationPp.set_pp_term term_pp;
300 let lazy_term_pp = fun x -> assert false in
301 let obj_pp = CicNotationPp.pp_obj in
303 (GrafiteAstPp.pp_statement ~term_pp ~lazy_term_pp ~obj_pp t)
305 let extra_statements_start = [
306 GA.Executable(floc,GA.Command(floc,
307 GA.Set(floc,"baseuri","cic:/matita/TPTP/" ^ !inputfile)));
308 GA.Executable(floc,GA.Command(floc, GA.Include(floc,"library_notation.ma")))]
310 List.iter pp extra_statements_start;
314 (LexiconAstPp.pp_command
316 LA.Ident_alias(n,s))) ^ "."))
317 [(*("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
318 ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
319 ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
320 ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
321 ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
322 ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)") *)];
323 List.iter pp grafite_ast_statements;