]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tptp_grafite/tptp2grafite.ml
1) Include files for NG were neither recursively processes nor accumulated.
[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.NDot(floc)])) ]
220   @      
221   (HExtlib.list_mapi
222    (fun (name,_) _-> 
223      GA.Executable(floc,GA.NTactic(floc, 
224       [GA.NIntro (floc,(try List.assoc name kw with Not_found -> name));
225        GA.NDot(floc)])))
226    arities)
227   @
228   (HExtlib.list_mapi
229    (fun _ i-> 
230      GA.Executable(floc,GA.NTactic(floc, 
231       [GA.NIntro (floc,"H"^string_of_int i);GA.NDot(floc)])))
232    context)
233   @
234 (if fv <> [] then     
235   (List.flatten
236     (List.map 
237       (fun _ -> 
238         [GA.Executable(floc,GA.NTactic(floc, 
239           [GA.NApply (floc,
240             PT.Appl [mk_ident "ex_intro";PT.Implicit;PT.Implicit;
241               PT.Implicit;PT.Implicit]);GA.NBranch floc]));
242          GA.Executable(floc,GA.NTactic(floc, 
243           [GA.NPos (floc,[2])]))])
244       fv)) 
245  else [])@
246   [GA.Executable(floc,GA.NTactic(floc, [
247     if (*ueq_case*) true then
248         GA.NAuto (floc,(
249           HExtlib.list_mapi 
250             (fun _ i -> 
251                mk_ident ("H" ^ string_of_int i)) 
252             context    
253                 ,[]))
254     else
255         GA.NAuto (floc,([],[
256                 "depth",string_of_int 5;
257                 "width",string_of_int 5;
258                 "size",string_of_int 20;
259                 "timeout",string_of_int 10;
260         ]))
261  ;
262   GA.NSemicolon(floc)]));
263 (*
264   GA.Executable(floc,GA.NTactic(floc, Some (GA.Try(floc,
265     GA.Assumption floc)), GA.Dot(floc)))
266 *)
267   ]@
268 (if fv <> [] then     
269   (List.flatten
270     (List.map 
271       (fun _ -> 
272               [GA.Executable(floc,GA.NTactic(floc, [GA.NShift floc;
273                GA.NSkip floc; GA.NMerge floc]))])
274       fv)) 
275  else [])@
276     [GA.Executable(floc,GA.NTactic(floc,[GA.NTry(floc, GA.NAssumption(floc));
277                                          GA.NSemicolon(floc)]))]@
278   [GA.Executable(floc,GA.NCommand(floc, GA.NQed(floc)))]
279 ;;
280
281 let generate_tactics fv ueq_case =
282   [GA.Executable(floc,GA.Tactic(floc, Some
283    (GA.Intros (floc,(None,[]))),GA.Dot(floc)))] @
284 (if fv <> [] then     
285   (List.flatten
286     (List.map 
287       (fun _ -> 
288         [GA.Executable(floc,GA.Tactic(floc, Some
289           (GA.Exists floc),GA.Branch floc));
290          GA.Executable(floc,GA.Tactic(floc, None,
291           (GA.Pos (floc,[2]))))])
292       fv)) 
293  else [])@
294   [GA.Executable(floc,GA.Tactic(floc, Some (
295     if true (*ueq_case*) then
296         GA.AutoBatch (floc,([],["paramodulation","";
297         "timeout",string_of_int !paramod_timeout]))
298     else
299         GA.AutoBatch (floc,([],[
300                 "depth",string_of_int 5;
301                 "width",string_of_int 5;
302                 "size",string_of_int 20;
303                 "timeout",string_of_int 10;
304         ]))
305   ),
306     GA.Semicolon(floc)));
307   GA.Executable(floc,GA.Tactic(floc, Some (GA.Try(floc,
308     GA.Assumption floc)), GA.Dot(floc)))
309   ]@
310 (if fv <> [] then     
311   (List.flatten
312     (List.map 
313       (fun _ -> 
314         [GA.Executable(floc,GA.Tactic(floc, None, GA.Shift floc));
315          GA.Executable(floc,GA.NonPunctuationTactical(floc, GA.Skip floc,
316          (GA.Merge floc)))])
317       fv)) 
318  else [])@
319   [GA.Executable(floc,GA.Command(floc, GA.Print(floc,"proofterm")));
320    GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))]
321 ;;
322
323 let convert_ast ng statements context = function
324   | A.Comment s -> 
325       let s = String.sub s 1 (String.length s - 1) in
326       let s = 
327         if s.[String.length s - 1] = '\n' then
328           String.sub s 0 (String.length s - 1)
329         else 
330           s
331       in
332       statements @ [GA.Comment (floc,GA.Note (floc,s))],
333       context
334   | A.Inclusion (s,_) ->
335       statements @ [
336         GA.Comment (
337           floc, GA.Note (
338             floc,"Inclusion of: " ^ s))], context
339   | A.AnnotatedFormula (name,kind,f,_,_) -> 
340       match kind with
341       | A.Axiom 
342       | A.Hypothesis ->
343           statements, f::context
344       | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
345           statements, f::context
346       | A.Negated_conjecture ->
347           let ueq_case = is_formulae_1eq_negated f in
348           let fv = collect_fv_1stord_from_formulae f in 
349           let old_f = f in
350           let f = 
351             PT.Binder 
352              (`Forall,
353                (mk_ident universe,Some (PT.Sort (`Type (CicUniv.fresh ())))), 
354                convert_formula fv false context f)
355           in
356           let o = PT.Theorem (`Theorem,name,f,None) in
357           (statements @ 
358           [ GA.Executable(floc,GA.Command(floc,
359              (*if ng then GA.NObj (floc,o) else*) GA.Obj(floc,o))); ] @
360           if ng then 
361             ng_generate_tactics fv ueq_case context
362               (let atom = atom_of_formula old_f in collect_arities atom context)
363           else generate_tactics fv ueq_case),
364           context
365       | A.Definition 
366       | A.Lemma 
367       | A.Theorem 
368       | A.Conjecture
369       | A.Lemma_conjecture 
370       | A.Plain 
371       | A.Unknown -> assert false
372 ;;
373
374 (* HELPERS *)
375 let resolve ~tptppath s = 
376   let resolved_name = 
377     if Filename.check_suffix s ".p" then
378       (assert (String.length s > 5);
379       let prefix = String.sub s 0 3 in
380       tptppath ^ "/Problems/" ^ prefix ^ "/" ^ s)
381     else
382       tptppath ^ "/" ^ s
383   in
384   if HExtlib.is_regular resolved_name then
385     resolved_name
386   else
387     begin
388       prerr_endline ("Unable to find " ^ s ^ " (" ^ resolved_name ^ ")");
389       exit 1
390     end
391 ;;
392
393 (* MAIN *)
394 let tptp2grafite ?(timeout=600) ?(def_depth=10) ?raw_preamble ~tptppath ~filename ~ng () =
395   paramod_timeout := timeout;
396   depth := def_depth;
397   let rec aux = function
398     | [] -> []
399     | ((A.Inclusion (file,_)) as hd) :: tl ->
400         let file = resolve ~tptppath file in
401         let lexbuf = Lexing.from_channel (open_in file) in
402         let statements = Parser.main Lexer.yylex lexbuf in
403         hd :: aux (statements @ tl)
404     | hd::tl -> hd :: aux tl
405   in
406   let statements = aux [A.Inclusion (filename,[])] in
407   let grafite_ast_statements,_ = 
408     List.fold_left 
409       (fun (st, ctx) f -> 
410         let newst, ctx = convert_ast ng st ctx f in
411         newst, ctx)
412       ([],[]) statements 
413   in
414   let pp t = 
415     (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
416      * which will show up using the following command line:
417      * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
418     let width = max_int in
419     let term_pp prec content_term =
420       let pres_term = TermContentPres.pp_ast content_term in
421       let lookup_uri = fun _ -> None in
422       let markup = CicNotationPres.render ~lookup_uri ~prec pres_term in
423       let s = BoxPp.render_to_string List.hd width markup ~map_unicode_to_tex:false in
424       Pcre.substitute 
425        ~rex:(Pcre.regexp ~flags:[`UTF8] "∀[Ha-z][a-z0-9_]*") ~subst:(fun x -> "\n" ^ x) 
426        s
427     in
428     CicNotationPp.set_pp_term (term_pp 90);
429     let lazy_term_pp = fun x -> assert false in
430     let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
431     Pcre.replace ~pat:"theorem" ~templ:"ntheorem" 
432      (GrafiteAstPp.pp_statement
433        ~map_unicode_to_tex:false ~term_pp:(term_pp 19) ~lazy_term_pp ~obj_pp t)
434   in
435   let buri = Pcre.replace ~pat:"\\.p$" ("cic:/matita/TPTP/" ^ filename) in
436   let extra_statements_start = [
437     (*GA.Executable(floc,GA.Command(floc,
438     GA.Set(floc,"baseuri",buri)))*)]
439   in
440   let preamble = 
441     match raw_preamble with
442     | None -> 
443       pp
444        (GA.Executable(floc,
445          GA.Command(floc,GA.Include(floc,true,`OldAndNew,"logic/equality.ma"))))
446     | Some s -> s buri
447   in
448   let extra_statements_end = [] in
449   let aliases = []
450    (*[("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
451    ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
452    ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
453    ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
454    ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
455    ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")] *)
456   in
457   let s1 = List.map pp extra_statements_start in
458   let s2 = 
459     List.map 
460      (fun (n,s) -> 
461        LexiconAstPp.pp_command (LA.Alias(floc, LA.Ident_alias(n,s))) ^ ".")
462      aliases
463   in
464   let s3 = List.map pp grafite_ast_statements in
465   let s4 = List.map pp extra_statements_end in
466   String.concat "\n" (s1@[preamble]@s2@s3@s4)
467 ;;