1 module GA = GrafiteAst;;
2 module PT = CicNotationPt;;
5 let rec collect_arities_from_term = function
6 | A.Constant name -> [name,0]
7 | A.Variable name -> []
8 | A.Function (name,l) ->
9 (name,List.length l) :: List.flatten (List.map collect_arities_from_term l)
12 let rec collect_fv_from_term = function
13 | A.Constant name -> []
14 | A.Variable name -> [name]
16 List.flatten (List.map collect_fv_from_term l)
19 let collect_arities_from_atom a =
21 | A.Proposition name -> assert false
22 | A.Predicate _ -> assert false
25 | A.Eq (t1,t2) -> collect_arities_from_term t1 @ collect_arities_from_term t2
26 | A.NotEq (t1,t2) -> collect_arities_from_term t1 @ collect_arities_from_term t2
28 HExtlib.list_uniq (List.sort (fun (a,_) (b,_) -> compare a b) (aux a))
31 let collect_fv_from_atom a =
33 | A.Proposition name -> assert false
34 | A.Predicate _ -> assert false
37 | A.Eq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
38 | A.NotEq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
40 HExtlib.list_uniq (List.sort compare (aux a))
43 let rec convert_term = function
44 | A.Variable x -> PT.Ident (x,None)
45 | A.Constant x -> PT.Ident (x,None)
46 | A.Function (name, args) ->
47 PT.Appl (PT.Ident (name,None) :: List.map convert_term args)
50 let atom_of_formula = function
51 | A.Disjunction _ -> assert false
52 | A.NegAtom a -> a (* removes the negation *)
56 let rec mk_arrow component = function
57 | 0 -> PT.Ident (component,None)
61 ((PT.Variable (PT.FreshVar "_")),Some (PT.Ident (component,None))),
62 mk_arrow component (n-1))
65 let build_ctx_for_arities arities t =
66 let rec aux = function
71 (PT.Ident (name,None),Some (mk_arrow "A" nargs)),
79 | A.Proposition _ -> assert false
80 | A.Predicate (name,params) -> assert false
81 | A.True -> PT.Ident ("True",None)
82 | A.False -> PT.Ident ("False",None)
84 | A.NotEq (l,r) -> (* removes the negation *)
86 PT.Ident ("eq",None); PT.Ident ("A",None); convert_term l; convert_term r]
88 build_ctx_for_arities (List.map (fun x -> (x,0)) (collect_fv_from_atom a)) (aux a)
91 let rec convert_formula no_arities context f =
92 let atom = atom_of_formula f in
93 let t = convert_atom atom in
94 let rec build_ctx n = function
99 (PT.Ident ("H" ^ string_of_int n,None),
100 Some (convert_formula true [] hp)),
103 let arities = if no_arities then [] else collect_arities_from_atom atom in
104 build_ctx_for_arities arities (build_ctx 0 context)
107 let convert_ast statements context = function
109 let s = String.sub s 1 (String.length s - 1) in
110 if s.[String.length s - 1] = '\n' then
111 s.[String.length s - 1] <- '\000';
112 statements @ [GA.Comment (HExtlib.dummy_floc,GA.Note (HExtlib.dummy_floc,s))],
114 | A.Inclusion (s,_) ->
117 HExtlib.dummy_floc, GA.Note (
118 HExtlib.dummy_floc,"Inclusion of: " ^ s))], context
119 | A.AnnotatedFormula (name,kind,f,_,_) ->
123 statements, f::context
124 | A.Negated_conjecture ->
128 (PT.Ident ("A",None),Some (PT.Sort `Set)),
129 convert_formula false context f)
131 let o = PT.Theorem (`Theorem,name,f,None) in
134 HExtlib.dummy_floc,GA.Command(
135 HExtlib.dummy_floc,GA.Obj(HExtlib.dummy_floc,o)))],
143 | A.Unknown -> assert false
147 let tptppath = ref "./";;
148 let librarymode = ref false;;
151 Arg.String (fun x -> tptppath := x),
152 "Where to find the Axioms/ and Problems/ directory");
155 "... not supported yet")
161 if Filename.check_suffix s ".p" then
162 (assert (String.length s > 5);
163 let prefix = String.sub s 0 3 in
164 !tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
168 if HExtlib.is_regular resolved_name then
172 prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
179 let usage = "Usage: tptp2grafite [options] file" in
180 let inputfile = ref "" in
181 Arg.parse spec (fun s -> inputfile := s) usage;
182 if !inputfile = "" then
187 let rec aux = function
189 | ((A.Inclusion (file,_)) as hd) :: tl ->
190 let file = resolve file in
191 let lexbuf = Lexing.from_channel (open_in file) in
192 let statements = Parser.main Lexer.yylex lexbuf in
193 hd :: aux (statements @ tl)
194 | hd::tl -> hd :: aux tl
196 let statements = aux [A.Inclusion (!inputfile ^ ".p",[])] in
197 let grafite_ast_statements,_ =
200 let newst, ctx = convert_ast st ctx f in
205 (* for a correct pp we should disambiguate the term... *)
206 let term_pp = CicNotationPp.pp_term in
207 let lazy_term_pp = fun x -> assert false in
208 let obj_pp = CicNotationPp.pp_obj in
210 (GrafiteAstPp.pp_statement ~term_pp ~lazy_term_pp ~obj_pp t)
212 List.iter pp grafite_ast_statements;