]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tptp_grafite/tptp2grafite.ml
fixed generation of horn clauses, negated atoms are hypotheses
[helm.git] / helm / software / components / tptp_grafite / tptp2grafite.ml
1 module GA = GrafiteAst;;
2 module LA = LexiconAst;;
3 module PT = CicNotationPt;;
4 module A = Ast;;
5
6 type sort = Prop | Univ;;
7
8 let floc = HExtlib.dummy_floc;;
9
10
11 let paramod_timeout = ref 600;;
12 let depth = ref 10;;
13
14 let universe = "Univ" ;;
15 let prop = "Prop";;
16
17 let kw = [
18  "and","myand"
19 ];;
20
21 let mk_ident s =
22   PT.Ident ((try List.assoc s kw with Not_found -> s),None)
23 ;;
24
25 let rec collect_arities_from_term = function
26   | A.Constant name -> [name,(0,Univ)]
27   | A.Variable name -> [name,(0,Univ)]
28   | A.Function (name,l) -> 
29       (name,(List.length l,Univ))::
30         List.flatten (List.map collect_arities_from_term l)
31 ;;
32
33 let rec collect_fv_from_term = function
34   | A.Constant name -> []
35   | A.Variable name -> [name]
36   | A.Function (_,l) -> 
37       List.flatten (List.map collect_fv_from_term l)
38 ;;
39
40 let collect_arities_from_atom a = 
41   let aux = function
42     | A.Proposition name -> [name,(0,Prop)]
43     | A.Predicate (name,args) -> 
44         (name,(List.length args,Prop)) :: 
45           (List.flatten (List.map collect_arities_from_term args))
46     | A.True -> []
47     | A.False -> []
48     | A.Eq (t1,t2) -> 
49         collect_arities_from_term t1 @ collect_arities_from_term t2
50     | A.NotEq (t1,t2) -> 
51         collect_arities_from_term t1 @ collect_arities_from_term t2
52   in
53   HExtlib.list_uniq (List.sort compare (List.flatten (List.map aux a)))
54 ;;
55   
56 let collect_fv_from_atom a = 
57   let aux = function
58     | A.Proposition name -> [name] 
59     | A.Predicate (name,args) -> 
60         name :: List.flatten (List.map collect_fv_from_term args)
61     | A.True -> []
62     | A.False -> []
63     | A.Eq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
64     | A.NotEq (t1,t2) -> collect_fv_from_term t1 @ collect_fv_from_term t2
65   in
66   let rec aux2 = function
67     | [] -> []
68     | hd::tl -> aux hd @ aux2 tl
69   in
70   HExtlib.list_uniq (List.sort compare (aux2 a))
71 ;;  
72
73 let rec collect_fv_from_formulae = function
74   | A.Disjunction (a,b) -> 
75       collect_fv_from_formulae a @ collect_fv_from_formulae b
76   | A.NegAtom a 
77   | A.Atom a -> collect_fv_from_atom [a]
78 ;;
79
80 let rec convert_term = function
81   | A.Variable x -> mk_ident x
82   | A.Constant x -> mk_ident x
83   | A.Function (name, args) -> 
84       PT.Appl (mk_ident name :: List.map convert_term args)
85 ;;
86
87 let rec atom_of_formula neg pos = function
88     | A.Disjunction (a,b) ->
89         let neg, pos = atom_of_formula neg pos a in
90         atom_of_formula neg pos b 
91     | A.NegAtom a -> a::neg, pos 
92     | A.Atom a -> neg, a::pos
93 ;;
94
95 let atom_of_formula f =
96   let neg, pos = atom_of_formula [] [] f in
97   neg @ pos
98 ;;
99   
100 let rec mk_arrow component tail = function
101   | 0 -> begin
102       match tail with
103       | Prop -> mk_ident prop
104       | Univ -> mk_ident universe
105       end
106   | n -> 
107       PT.Binder 
108         (`Forall,
109           ((mk_ident "_"),Some (mk_ident component)),
110           mk_arrow component tail (n-1))
111 ;;
112
113 let build_ctx_for_arities univesally arities t = 
114   let binder = if univesally then `Forall else `Exists in
115   let rec aux = function
116     | [] -> t
117     | (name,(nargs,sort))::tl ->
118         PT.Binder 
119           (binder,
120             (mk_ident name,Some (mk_arrow universe sort nargs)),
121             aux tl)
122   in
123   aux arities
124 ;;
125
126 let convert_atom universally a = 
127   let aux = function
128   | A.Proposition p -> mk_ident p
129   | A.Predicate (name,params) -> 
130       PT.Appl ((mk_ident name) :: (List.map convert_term params))
131   | A.True -> mk_ident "True"
132   | A.False -> mk_ident "False"
133   | A.Eq (l,r)
134   | A.NotEq (l,r) -> (* removes the negation *)
135       PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
136   in
137   let rec aux2 = function
138     | [] -> assert false
139     | [x] -> aux x
140     | he::tl -> 
141         if universally then 
142           PT.Binder (`Forall, (mk_ident "_", Some (aux he)), aux2 tl)
143         else
144           PT.Appl [mk_ident "And";aux he;aux2 tl]
145   in
146   let arities = collect_arities_from_atom a in
147   let fv = collect_fv_from_atom a in
148   build_ctx_for_arities universally 
149     (List.filter 
150       (function (x,(0,Univ)) -> List.mem x fv | _-> false) 
151       arities) 
152     (aux2 a)
153 ;;
154
155 let collect_arities atom ctx = 
156   let atoms = atom@(List.flatten (List.map atom_of_formula ctx)) in
157   collect_arities_from_atom atoms
158 ;;
159
160 let collect_arities_from_formulae f =
161   let rec collect_arities_from_formulae = function
162   | A.Disjunction (a,b) -> 
163       collect_arities_from_formulae a @ collect_arities_from_formulae b
164   | A.NegAtom a 
165   | A.Atom a -> collect_arities_from_atom [a]
166   in
167   HExtlib.list_uniq (List.sort compare (collect_arities_from_formulae f))
168 ;;
169
170 let is_formulae_1eq_negated f =
171   let atom = atom_of_formula f in
172   match atom with
173   | [A.NotEq (l,r)] -> true
174   | _ -> false
175 ;;  
176
177 let collect_fv_1stord_from_formulae f =
178   let arities = collect_arities_from_formulae f in
179   let fv = collect_fv_from_formulae f in
180   List.map fst 
181     (List.filter (function (x,(0,Univ)) -> List.mem x fv | _-> false) arities)
182 ;;
183
184 let rec convert_formula fv no_arities context f =
185   let atom = atom_of_formula f in
186   let t = convert_atom (fv = []) atom in
187   let rec build_ctx n = function
188     | [] -> t
189     | hp::tl -> 
190         PT.Binder 
191           (`Forall,
192             (mk_ident ("H" ^ string_of_int n), 
193               Some (convert_formula [] true [] hp)), 
194             build_ctx (n+1) tl)
195   in
196   let arities = if no_arities then [] else collect_arities atom context in
197   build_ctx_for_arities true arities (build_ctx 0 context) 
198 ;;
199
200 let check_if_atom_is_negative = function
201   | A.True -> false
202   | A.False -> true
203   | A.Proposition _ -> false
204   | A.Predicate _ -> false
205   | A.Eq _ -> false
206   | A.NotEq _ -> true
207 ;;
208
209 let rec check_if_formula_is_negative = function
210   | A.Disjunction (a,b) ->
211       check_if_formula_is_negative a && check_if_formula_is_negative b
212   | A.NegAtom a -> not (check_if_atom_is_negative a)
213   | A.Atom a -> check_if_atom_is_negative a
214 ;;
215
216 let convert_ast statements context = function
217   | A.Comment s -> 
218       let s = String.sub s 1 (String.length s - 1) in
219       let s = 
220         if s.[String.length s - 1] = '\n' then
221           String.sub s 0 (String.length s - 1)
222         else 
223           s
224       in
225       statements @ [GA.Comment (floc,GA.Note (floc,s))],
226       context
227   | A.Inclusion (s,_) ->
228       statements @ [
229         GA.Comment (
230           floc, GA.Note (
231             floc,"Inclusion of: " ^ s))], context
232   | A.AnnotatedFormula (name,kind,f,_,_) -> 
233       match kind with
234       | A.Axiom 
235       | A.Hypothesis ->
236           statements, f::context
237       | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
238           statements, f::context
239       | A.Negated_conjecture ->
240           let ueq_case = is_formulae_1eq_negated f in
241           let fv = collect_fv_1stord_from_formulae f in 
242 (*
243           if fv <> [] then 
244             prerr_endline ("FREE VARIABLES: " ^ String.concat "," fv);
245 *)
246           let f = 
247             PT.Binder 
248              (`Forall,
249                (mk_ident universe,Some (PT.Sort `Set)), 
250                convert_formula fv false context f)
251           in
252           let o = PT.Theorem (`Theorem,name,f,None) in
253           statements @ [
254             GA.Executable(floc,GA.Command(floc,GA.Obj(floc,o)));
255             GA.Executable(floc,GA.Tactic(floc, Some
256              (GA.Intros (floc,(None,[]))),GA.Dot(floc)))] @
257           (if fv <> [] then     
258             (List.flatten
259               (List.map 
260                 (fun _ -> 
261                   [GA.Executable(floc,GA.Tactic(floc, Some
262                     (GA.Exists floc),GA.Branch floc));
263                    GA.Executable(floc,GA.Tactic(floc, None,
264                     (GA.Pos (floc,[2]))))])
265                 fv)) 
266            else [])@
267             [GA.Executable(floc,GA.Tactic(floc, Some (
268               if ueq_case then
269                   GA.AutoBatch (floc,([],["paramodulation","";
270                   "timeout",string_of_int !paramod_timeout]))
271               else
272                   GA.AutoBatch (floc,([],[
273                           "depth",string_of_int 5;
274                           "width",string_of_int 5;
275                           "size",string_of_int 20;
276                           "timeout",string_of_int 10;
277                   ]))
278             ),
279               GA.Semicolon(floc)));
280             GA.Executable(floc,GA.Tactic(floc, Some (GA.Try(floc,
281               GA.Assumption floc)), GA.Dot(floc)))
282             ]@
283           (if fv <> [] then     
284             (List.flatten
285               (List.map 
286                 (fun _ -> 
287                   [GA.Executable(floc,GA.Tactic(floc, None, GA.Shift floc));
288                    GA.Executable(floc,GA.NonPunctuationTactical(floc, GA.Skip floc,
289                    (GA.Merge floc)))])
290                 fv)) 
291            else [])@
292             [GA.Executable(floc,GA.Command(floc, GA.Print(floc,"proofterm")));
293              GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))],
294           context
295       | A.Definition 
296       | A.Lemma 
297       | A.Theorem 
298       | A.Conjecture
299       | A.Lemma_conjecture 
300       | A.Plain 
301       | A.Unknown -> assert false
302 ;;
303
304 (* HELPERS *)
305 let resolve ~tptppath s = 
306   let resolved_name = 
307     if Filename.check_suffix s ".p" then
308       (assert (String.length s > 5);
309       let prefix = String.sub s 0 3 in
310       tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
311     else
312       tptppath ^ "/" ^ s
313   in
314   if HExtlib.is_regular resolved_name then
315     resolved_name
316   else
317     begin
318       prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
319       exit 1
320     end
321 ;;
322
323 (* MAIN *)
324 let tptp2grafite ?(timeout=600) ?(def_depth=10) ?raw_preamble ~tptppath ~filename () =
325   paramod_timeout := timeout;
326   depth := def_depth;
327   let rec aux = function
328     | [] -> []
329     | ((A.Inclusion (file,_)) as hd) :: tl ->
330         let file = resolve ~tptppath file in
331         let lexbuf = Lexing.from_channel (open_in file) in
332         let statements = Parser.main Lexer.yylex lexbuf in
333         hd :: aux (statements @ tl)
334     | hd::tl -> hd :: aux tl
335   in
336   let statements = aux [A.Inclusion (filename,[])] in
337   let grafite_ast_statements,_ = 
338     List.fold_left 
339       (fun (st, ctx) f -> 
340         let newst, ctx = convert_ast st ctx f in
341         newst, ctx)
342       ([],[]) statements 
343   in
344   let pp t = 
345     (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
346      * which will show up using the following command line:
347      * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
348     let width = max_int in
349     let term_pp content_term =
350       let pres_term = TermContentPres.pp_ast content_term in
351       let dummy_tbl = Hashtbl.create 1 in
352       let markup = CicNotationPres.render dummy_tbl pres_term in
353       let s = BoxPp.render_to_string List.hd width markup ~map_unicode_to_tex:false in
354       Pcre.substitute 
355         ~pat:"\\\\forall [Ha-z][a-z0-9_]*" ~subst:(fun x -> "\n" ^ x) s
356     in
357     CicNotationPp.set_pp_term term_pp;
358     let lazy_term_pp = fun x -> assert false in
359     let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
360     GrafiteAstPp.pp_statement
361      ~map_unicode_to_tex:false ~term_pp ~lazy_term_pp ~obj_pp t
362   in
363   let buri = Pcre.replace ~pat:"\\.p$" ("cic:/matita/TPTP/" ^ filename) in
364   let extra_statements_start = [
365     GA.Executable(floc,GA.Command(floc,
366     GA.Set(floc,"baseuri",buri)))]
367   in
368   let preamble = 
369     match raw_preamble with
370     | None -> 
371        pp (GA.Executable(floc,
372            GA.Command(floc,GA.Include(floc,"logic/equality.ma"))))
373     | Some s -> s buri
374   in
375   let extra_statements_end = [] in
376   let aliases = []
377    (*[("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
378    ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
379    ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
380    ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
381    ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
382    ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")] *)
383   in
384   let s1 = List.map pp extra_statements_start in
385   let s2 = 
386     List.map 
387      (fun (n,s) -> 
388        LexiconAstPp.pp_command (LA.Alias(floc, LA.Ident_alias(n,s))) ^ ".")
389      aliases
390   in
391   let s3 = List.map pp grafite_ast_statements in
392   let s4 = List.map pp extra_statements_end in
393   String.concat "\n" (s1@[preamble]@s2@s3@s4)
394 ;;