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, GA.Pos (floc,2),None))])
202 [GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
203 GA.Auto (floc,None,None,Some "paramodulation",None)),
204 Some (GA.Dot(floc))))]@
209 [GA.Executable(floc,GA.Tactical(floc, GA.Shift floc, None));
210 GA.Executable(floc,GA.Tactical(floc, GA.Skip floc,Some
214 [GA.Executable(floc,GA.Tactical(floc, GA.Try(floc,
215 GA.Tactic (floc, GA.Assumption floc)), Some (GA.Dot(floc))));
216 GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))],
224 | A.Unknown -> assert false
228 let tptppath = ref "./";;
229 let librarymode = ref false;;
232 Arg.String (fun x -> tptppath := x),
233 "Where to find the Axioms/ and Problems/ directory");
236 "... not supported yet")
242 if Filename.check_suffix s ".p" then
243 (assert (String.length s > 5);
244 let prefix = String.sub s 0 3 in
245 !tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
249 if HExtlib.is_regular resolved_name then
253 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
260 let usage = "Usage: tptp2grafite [options] file" in
261 let inputfile = ref "" in
262 Arg.parse spec (fun s -> inputfile := s) usage;
263 if !inputfile = "" then
268 let rec aux = function
270 | ((A.Inclusion (file,_)) as hd) :: tl ->
271 let file = resolve file in
272 let lexbuf = Lexing.from_channel (open_in file) in
273 let statements = Parser.main Lexer.yylex lexbuf in
274 hd :: aux (statements @ tl)
275 | hd::tl -> hd :: aux tl
277 let statements = aux [A.Inclusion (!inputfile ^ ".p",[])] in
278 let grafite_ast_statements,_ =
281 let newst, ctx = convert_ast st ctx f in
286 (* for a correct pp we should disambiguate the term... *)
287 let term_pp = CicNotationPp.pp_term in
288 let lazy_term_pp = fun x -> assert false in
289 let obj_pp = CicNotationPp.pp_obj in
291 (GrafiteAstPp.pp_statement ~term_pp ~lazy_term_pp ~obj_pp t)
293 let extra_statements_start = [
294 GA.Executable(floc,GA.Command(floc,
295 GA.Set(floc,"baseuri","cic:/matita/TPTP/" ^ !inputfile)));
296 GA.Executable(floc,GA.Command(floc, GA.Include(floc,"legacy/coq.ma")))]
298 List.iter pp extra_statements_start;
300 (LexiconAstPp.pp_command
302 LA.Ident_alias("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)"))) ^ ".");
303 List.iter pp grafite_ast_statements;