]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tptp_grafite/tptp2grafite.ml
b4675066f6c0aac3fa85416463452cb96909c72f
[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.NotEq (a,b)) -> (A.Eq (a,b) :: neg), pos
93     | A.Atom a -> neg, a::pos
94 ;;
95
96 let atom_of_formula f =
97   let neg, pos = atom_of_formula [] [] f in
98   neg @ pos
99 ;;
100   
101 let rec mk_arrow component tail = function
102   | 0 -> begin
103       match tail with
104       | Prop -> mk_ident prop
105       | Univ -> mk_ident universe
106       end
107   | n -> 
108       PT.Binder 
109         (`Forall,
110           ((mk_ident "_"),Some (mk_ident component)),
111           mk_arrow component tail (n-1))
112 ;;
113
114 let build_ctx_for_arities univesally arities t = 
115   let binder = if univesally then `Forall else `Exists in
116   let rec aux = function
117     | [] -> t
118     | (name,(nargs,sort))::tl ->
119         PT.Binder 
120           (binder,
121             (mk_ident name,Some (mk_arrow universe sort nargs)),
122             aux tl)
123   in
124   aux arities
125 ;;
126
127 let convert_atom universally a = 
128   let aux = function
129   | A.Proposition p -> mk_ident p
130   | A.Predicate (name,params) -> 
131       PT.Appl ((mk_ident name) :: (List.map convert_term params))
132   | A.True -> mk_ident "True"
133   | A.False -> mk_ident "False"
134   | A.Eq (l,r)
135   | A.NotEq (l,r) -> (* removes the negation *)
136       PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
137   in
138   let rec aux2 = function
139     | [] -> assert false
140     | [x] -> aux x
141     | he::tl -> 
142         if universally then 
143           PT.Binder (`Forall, (mk_ident "_", Some (aux he)), aux2 tl)
144         else
145           PT.Appl [mk_ident "And";aux he;aux2 tl]
146   in
147   let arities = collect_arities_from_atom a in
148   let fv = collect_fv_from_atom a in
149   build_ctx_for_arities universally 
150     (List.filter 
151       (function (x,(0,Univ)) -> List.mem x fv | _-> false) 
152       arities) 
153     (aux2 a)
154 ;;
155
156 let collect_arities atom ctx = 
157   let atoms = atom@(List.flatten (List.map atom_of_formula ctx)) in
158   collect_arities_from_atom atoms
159 ;;
160
161 let collect_arities_from_formulae f =
162   let rec collect_arities_from_formulae = function
163   | A.Disjunction (a,b) -> 
164       collect_arities_from_formulae a @ collect_arities_from_formulae b
165   | A.NegAtom a 
166   | A.Atom a -> collect_arities_from_atom [a]
167   in
168   HExtlib.list_uniq (List.sort compare (collect_arities_from_formulae f))
169 ;;
170
171 let is_formulae_1eq_negated f =
172   let atom = atom_of_formula f in
173   match atom with
174   | [A.NotEq (l,r)] -> true
175   | _ -> false
176 ;;  
177
178 let collect_fv_1stord_from_formulae f =
179   let arities = collect_arities_from_formulae f in
180   let fv = collect_fv_from_formulae f in
181   List.map fst 
182     (List.filter (function (x,(0,Univ)) -> List.mem x fv | _-> false) arities)
183 ;;
184
185 let rec convert_formula fv no_arities context f =
186   let atom = atom_of_formula f in
187   let t = convert_atom (fv = []) atom in
188   let rec build_ctx n = function
189     | [] -> t
190     | hp::tl -> 
191         PT.Binder 
192           (`Forall,
193             (mk_ident ("H" ^ string_of_int n), 
194               Some (convert_formula [] true [] hp)), 
195             build_ctx (n+1) tl)
196   in
197   let arities = if no_arities then [] else collect_arities atom context in
198   build_ctx_for_arities true arities (build_ctx 0 context) 
199 ;;
200
201 let check_if_atom_is_negative = function
202   | A.True -> false
203   | A.False -> true
204   | A.Proposition _ -> false
205   | A.Predicate _ -> false
206   | A.Eq _ -> false
207   | A.NotEq _ -> true
208 ;;
209
210 let rec check_if_formula_is_negative = function
211   | A.Disjunction (a,b) ->
212       check_if_formula_is_negative a && check_if_formula_is_negative b
213   | A.NegAtom a -> not (check_if_atom_is_negative a)
214   | A.Atom a -> check_if_atom_is_negative a
215 ;;
216
217 let ng_generate_tactics fv ueq_case context arities =
218   [ GA.Executable(floc,GA.NTactic(floc, 
219       (GA.NIntro (floc,"Univ")),GA.Dot(floc))) ]
220   @      
221   (HExtlib.list_mapi
222    (fun (name,_) _-> 
223      GA.Executable(floc,GA.NTactic(floc, 
224       (GA.NIntro (floc,name)),GA.Dot(floc))))
225    arities)
226   @
227   (HExtlib.list_mapi
228    (fun _ i-> 
229      GA.Executable(floc,GA.NTactic(floc, 
230       (GA.NIntro (floc,"H"^string_of_int i)),GA.Dot(floc))))
231    context)
232   @
233 (if fv <> [] then     
234   (List.flatten
235     (List.map 
236       (fun _ -> 
237         [GA.Executable(floc,GA.NTactic(floc, 
238           (GA.NApply (floc,mk_ident "ex_intro")),GA.Branch floc));
239          GA.Executable(floc,GA.NTactic(floc, GA.NId floc ,
240           (GA.Pos (floc,[2]))))])
241       fv)) 
242  else [])@
243   [GA.Executable(floc,GA.NTactic(floc, (
244     if (*ueq_case*) true then
245         GA.NAuto (floc,(
246           HExtlib.list_mapi 
247             (fun _ i -> 
248                mk_ident ("H" ^ string_of_int i)) 
249             context    
250                 ,[]))
251     else
252         GA.NAuto (floc,([],[
253                 "depth",string_of_int 5;
254                 "width",string_of_int 5;
255                 "size",string_of_int 20;
256                 "timeout",string_of_int 10;
257         ]))
258   ),
259     GA.Semicolon(floc)));
260 (*
261   GA.Executable(floc,GA.NTactic(floc, Some (GA.Try(floc,
262     GA.Assumption floc)), GA.Dot(floc)))
263 *)
264   ]@
265 (if fv <> [] then     
266   (List.flatten
267     (List.map 
268       (fun _ -> 
269         [GA.Executable(floc,GA.NTactic(floc, GA.NId floc, GA.Shift floc));
270          GA.Executable(floc,GA.NNonPunctuationTactical(floc, GA.Skip floc,
271          (GA.Merge floc)))])
272       fv)) 
273  else [])@
274   [GA.Executable(floc,GA.Command(floc, GA.NQed(floc)))]
275 ;;
276
277 let generate_tactics fv ueq_case =
278   [GA.Executable(floc,GA.Tactic(floc, Some
279    (GA.Intros (floc,(None,[]))),GA.Dot(floc)))] @
280 (if fv <> [] then     
281   (List.flatten
282     (List.map 
283       (fun _ -> 
284         [GA.Executable(floc,GA.Tactic(floc, Some
285           (GA.Exists floc),GA.Branch floc));
286          GA.Executable(floc,GA.Tactic(floc, None,
287           (GA.Pos (floc,[2]))))])
288       fv)) 
289  else [])@
290   [GA.Executable(floc,GA.Tactic(floc, Some (
291     if true (*ueq_case*) then
292         GA.AutoBatch (floc,([],["paramodulation","";
293         "timeout",string_of_int !paramod_timeout]))
294     else
295         GA.AutoBatch (floc,([],[
296                 "depth",string_of_int 5;
297                 "width",string_of_int 5;
298                 "size",string_of_int 20;
299                 "timeout",string_of_int 10;
300         ]))
301   ),
302     GA.Semicolon(floc)));
303   GA.Executable(floc,GA.Tactic(floc, Some (GA.Try(floc,
304     GA.Assumption floc)), GA.Dot(floc)))
305   ]@
306 (if fv <> [] then     
307   (List.flatten
308     (List.map 
309       (fun _ -> 
310         [GA.Executable(floc,GA.Tactic(floc, None, GA.Shift floc));
311          GA.Executable(floc,GA.NonPunctuationTactical(floc, GA.Skip floc,
312          (GA.Merge floc)))])
313       fv)) 
314  else [])@
315   [GA.Executable(floc,GA.Command(floc, GA.Print(floc,"proofterm")));
316    GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))]
317 ;;
318
319 let convert_ast ng statements context = function
320   | A.Comment s -> 
321       let s = String.sub s 1 (String.length s - 1) in
322       let s = 
323         if s.[String.length s - 1] = '\n' then
324           String.sub s 0 (String.length s - 1)
325         else 
326           s
327       in
328       statements @ [GA.Comment (floc,GA.Note (floc,s))],
329       context
330   | A.Inclusion (s,_) ->
331       statements @ [
332         GA.Comment (
333           floc, GA.Note (
334             floc,"Inclusion of: " ^ s))], context
335   | A.AnnotatedFormula (name,kind,f,_,_) -> 
336       match kind with
337       | A.Axiom 
338       | A.Hypothesis ->
339           statements, f::context
340       | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
341           statements, f::context
342       | A.Negated_conjecture ->
343           let ueq_case = is_formulae_1eq_negated f in
344           let fv = collect_fv_1stord_from_formulae f in 
345           let old_f = f in
346           let f = 
347             PT.Binder 
348              (`Forall,
349                (mk_ident universe,Some (PT.Sort (`Type (CicUniv.fresh ())))), 
350                convert_formula fv false context f)
351           in
352           let o = PT.Theorem (`Theorem,name,f,None) in
353           (statements @ 
354           [ GA.Executable(floc,GA.Command(floc,
355              (*if ng then GA.NObj (floc,o) else*) GA.Obj(floc,o))); ] @
356           if ng then 
357             ng_generate_tactics fv ueq_case context
358               (let atom = atom_of_formula old_f in collect_arities atom context)
359           else generate_tactics fv ueq_case),
360           context
361       | A.Definition 
362       | A.Lemma 
363       | A.Theorem 
364       | A.Conjecture
365       | A.Lemma_conjecture 
366       | A.Plain 
367       | A.Unknown -> assert false
368 ;;
369
370 (* HELPERS *)
371 let resolve ~tptppath s = 
372   let resolved_name = 
373     if Filename.check_suffix s ".p" then
374       (assert (String.length s > 5);
375       let prefix = String.sub s 0 3 in
376       tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
377     else
378       tptppath ^ "/" ^ s
379   in
380   if HExtlib.is_regular resolved_name then
381     resolved_name
382   else
383     begin
384       prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
385       exit 1
386     end
387 ;;
388
389 (* MAIN *)
390 let tptp2grafite ?(timeout=600) ?(def_depth=10) ?raw_preamble ~tptppath ~filename ~ng () =
391   paramod_timeout := timeout;
392   depth := def_depth;
393   let rec aux = function
394     | [] -> []
395     | ((A.Inclusion (file,_)) as hd) :: tl ->
396         let file = resolve ~tptppath file in
397         let lexbuf = Lexing.from_channel (open_in file) in
398         let statements = Parser.main Lexer.yylex lexbuf in
399         hd :: aux (statements @ tl)
400     | hd::tl -> hd :: aux tl
401   in
402   let statements = aux [A.Inclusion (filename,[])] in
403   let grafite_ast_statements,_ = 
404     List.fold_left 
405       (fun (st, ctx) f -> 
406         let newst, ctx = convert_ast ng st ctx f in
407         newst, ctx)
408       ([],[]) statements 
409   in
410   let pp t = 
411     (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
412      * which will show up using the following command line:
413      * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
414     let width = max_int in
415     let term_pp content_term =
416       let pres_term = TermContentPres.pp_ast content_term in
417       let lookup_uri = fun _ -> None in
418       let markup = CicNotationPres.render ~lookup_uri pres_term in
419       let s = BoxPp.render_to_string List.hd width markup ~map_unicode_to_tex:false in
420       Pcre.substitute 
421        ~rex:(Pcre.regexp ~flags:[`UTF8] "∀[Ha-z][a-z0-9_]*") ~subst:(fun x -> "\n" ^ x) 
422        s
423     in
424     CicNotationPp.set_pp_term term_pp;
425     let lazy_term_pp = fun x -> assert false in
426     let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
427     Pcre.replace ~pat:"theorem" ~templ:"ntheorem" 
428      (GrafiteAstPp.pp_statement
429        ~map_unicode_to_tex:false ~term_pp ~lazy_term_pp ~obj_pp t)
430   in
431   let buri = Pcre.replace ~pat:"\\.p$" ("cic:/matita/TPTP/" ^ filename) in
432   let extra_statements_start = [
433     (*GA.Executable(floc,GA.Command(floc,
434     GA.Set(floc,"baseuri",buri)))*)]
435   in
436   let preamble = 
437     match raw_preamble with
438     | None -> 
439        pp (GA.Executable(floc,
440            GA.Command(floc,GA.Include(floc,true,"logic/equality.ma"))))
441     | Some s -> s buri
442   in
443   let extra_statements_end = [] in
444   let aliases = []
445    (*[("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
446    ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
447    ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
448    ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
449    ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
450    ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")] *)
451   in
452   let s1 = List.map pp extra_statements_start in
453   let s2 = 
454     List.map 
455      (fun (n,s) -> 
456        LexiconAstPp.pp_command (LA.Alias(floc, LA.Ident_alias(n,s))) ^ ".")
457      aliases
458   in
459   let s3 = List.map pp grafite_ast_statements in
460   let s4 = List.map pp extra_statements_end in
461   String.concat "\n" (s1@[preamble]@s2@s3@s4)
462 ;;