]> matita.cs.unibo.it Git - helm.git/blob - components/binaries/tptp2grafite/main.ml
0ecee127eb2d623c6d2d78a940c608abb3e334cd
[helm.git] / components / binaries / tptp2grafite / main.ml
1 module GA = GrafiteAst;;
2 module PT = CicNotationPt;;
3 module A = Ast;;
4
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)
10 ;;
11
12 let rec collect_fv_from_term = function
13   | A.Constant name -> []
14   | A.Variable name -> [name]
15   | A.Function (_,l) -> 
16       List.flatten (List.map collect_fv_from_term l)
17 ;;
18
19 let collect_arities_from_atom a = 
20   let aux = function
21     | A.Proposition name -> assert false
22     | A.Predicate _ -> assert false
23     | A.True -> []
24     | A.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
27   in
28   HExtlib.list_uniq (List.sort (fun (a,_) (b,_) -> compare a b) (aux a))
29 ;;
30   
31 let collect_fv_from_atom a = 
32   let aux = function
33     | A.Proposition name -> assert false
34     | A.Predicate _ -> assert false
35     | A.True -> []
36     | A.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
39   in
40   HExtlib.list_uniq (List.sort compare (aux a))
41 ;;  
42
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)
48 ;;
49
50 let atom_of_formula = function
51     | A.Disjunction _ -> assert false
52     | A.NegAtom a -> a (* removes the negation *)
53     | A.Atom a -> a
54 ;;
55   
56 let rec mk_arrow component = function
57   | 0 -> PT.Ident (component,None)
58   | n -> 
59       PT.Binder 
60         (`Forall,
61           ((PT.Variable (PT.FreshVar "_")),Some (PT.Ident (component,None))),
62           mk_arrow component (n-1))
63 ;;
64
65 let build_ctx_for_arities arities t = 
66   let rec aux = function
67     | [] -> t
68     | (name,nargs)::tl ->
69         PT.Binder 
70           (`Forall,
71             (PT.Ident (name,None),Some (mk_arrow "A" nargs)),
72             aux tl)
73   in
74   aux arities
75 ;;
76
77 let convert_atom a = 
78   let aux = function
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)
83   | A.Eq (l,r) 
84   | A.NotEq (l,r) -> (* removes the negation *)
85       PT.Appl [
86         PT.Ident ("eq",None); PT.Ident ("A",None); convert_term l; convert_term r]
87   in
88   build_ctx_for_arities (List.map (fun x -> (x,0)) (collect_fv_from_atom a)) (aux a)
89 ;;
90
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
95     | [] -> t
96     | hp::tl -> 
97         PT.Binder 
98           (`Forall,
99             (PT.Ident ("H" ^ string_of_int n,None), 
100                        Some (convert_formula true [] hp)), 
101             build_ctx (n+1) tl)
102   in
103   let arities = if no_arities then [] else collect_arities_from_atom atom in
104   build_ctx_for_arities arities (build_ctx 0 context) 
105 ;;
106
107 let convert_ast statements context = function
108   | A.Comment s -> 
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))],
113       context
114   | A.Inclusion (s,_) ->
115       statements @ [
116         GA.Comment (
117           HExtlib.dummy_floc, GA.Note (
118             HExtlib.dummy_floc,"Inclusion of: " ^ s))], context
119   | A.AnnotatedFormula (name,kind,f,_,_) -> 
120       match kind with
121       | A.Axiom 
122       | A.Hypothesis ->
123           statements, f::context
124       | A.Negated_conjecture ->
125           let f = 
126             PT.Binder 
127              (`Forall,
128                (PT.Ident ("A",None),Some (PT.Sort `Set)), 
129                convert_formula false context f)
130           in
131           let o = PT.Theorem (`Theorem,name,f,None) in
132           statements @ [
133             GA.Executable(
134               HExtlib.dummy_floc,GA.Command(
135                 HExtlib.dummy_floc,GA.Obj(HExtlib.dummy_floc,o)))],
136           context
137       | A.Definition 
138       | A.Lemma 
139       | A.Theorem 
140       | A.Conjecture
141       | A.Lemma_conjecture 
142       | A.Plain 
143       | A.Unknown -> assert false
144 ;;
145
146 (* OPTIONS *)
147 let tptppath = ref "./";;
148 let librarymode = ref false;;
149 let spec = [
150   ("-tptppath", 
151       Arg.String (fun x -> tptppath := x), 
152       "Where to find the Axioms/ and Problems/ directory");
153   ("-librarymode",
154       Arg.Set librarymode,
155       "... not supported yet")
156 ]
157
158 (* HELPERS *)
159 let resolve s = 
160   let resolved_name = 
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)
165     else
166       !tptppath ^ "/" ^ s
167   in
168   if HExtlib.is_regular resolved_name then
169     resolved_name
170   else
171     begin
172       prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
173       exit 1
174     end
175 ;;
176
177 (* MAIN *)
178 let _ =
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 
183     begin
184       prerr_endline usage;
185       exit 1
186     end;
187   let rec aux = function
188     | [] -> []
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
195   in
196   let statements = aux [A.Inclusion (!inputfile ^ ".p",[])] in
197   let grafite_ast_statements,_ = 
198     List.fold_left 
199       (fun (st, ctx) f -> 
200         let newst, ctx = convert_ast st ctx f in
201         newst, ctx)
202       ([],[]) statements 
203   in
204   let pp t = 
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
209     print_endline 
210       (GrafiteAstPp.pp_statement ~term_pp ~lazy_term_pp ~obj_pp t)
211   in
212   List.iter pp grafite_ast_statements;
213   exit 0