]> matita.cs.unibo.it Git - helm.git/blobdiff - components/binaries/tptp2grafite/main.ml
implemented tinycals:
[helm.git] / components / binaries / tptp2grafite / main.ml
index 18fa713a9a05b1f48b71ae81528652018ce28453..b7efd6820bcf464d8a0fa88750b6621336ec7ecd 100644 (file)
@@ -4,6 +4,8 @@ module PT = CicNotationPt;;
 module A = Ast;;
 let floc = HExtlib.dummy_floc;;
 
+let universe = "Univ" ;;
+
 let kw = [
  "and","myand"
 ];;
@@ -12,12 +14,11 @@ let mk_ident s =
   PT.Ident ((try List.assoc s kw with Not_found -> s),None)
 ;;
 
-
 let rec collect_arities_from_term = function
   | A.Constant name -> [name,0]
   | A.Variable name -> []
   | A.Function (name,l) -> 
-      (name,List.length l) :: List.flatten (List.map collect_arities_from_term l)
+      (name,List.length l)::List.flatten (List.map collect_arities_from_term l)
 ;;
 
 let rec collect_fv_from_term = function
@@ -79,19 +80,20 @@ let rec mk_arrow component = function
           mk_arrow component (n-1))
 ;;
 
-let build_ctx_for_arities arities t = 
+let build_ctx_for_arities univesally arities t = 
+  let binder = if univesally then `Forall else `Exists in
   let rec aux = function
     | [] -> t
     | (name,nargs)::tl ->
         PT.Binder 
-          (`Forall,
-            (mk_ident name,Some (mk_arrow "A" nargs)),
+          (binder,
+            (mk_ident name,Some (mk_arrow universe nargs)),
             aux tl)
   in
   aux arities
 ;;
 
-let convert_atom a = 
+let convert_atom universally a = 
   let aux = function
   | A.Proposition _ -> assert false
   | A.Predicate (name,params) -> 
@@ -99,11 +101,12 @@ let convert_atom a =
       assert false
   | A.True -> mk_ident "True"
   | A.False -> mk_ident "False"
-  | A.Eq (l,r) 
+  | A.Eq (l,r)
   | A.NotEq (l,r) -> (* removes the negation *)
-      PT.Appl [mk_ident "eq";mk_ident "A";convert_term l;convert_term r]
+      PT.Appl [mk_ident "eq";mk_ident universe;convert_term l;convert_term r]
   in
-  build_ctx_for_arities (List.map (fun x -> (x,0)) (collect_fv_from_atom a)) (aux a)
+  build_ctx_for_arities universally 
+    (List.map (fun x -> (x,0)) (collect_fv_from_atom a)) (aux a)
 ;;
 
 let collect_arities atom ctx = 
@@ -112,20 +115,40 @@ let collect_arities atom ctx =
     (List.flatten (List.map collect_arities_from_atom atoms)))
 ;;
 
-let rec convert_formula no_arities context f =
+let assert_formulae_is_1eq_negated f =
   let atom = atom_of_formula f in
-  let t = convert_atom atom in
+  match atom with
+  | A.Eq (l,r) -> failwith "Negated formula is not negated"
+  | A.NotEq (l,r) -> ()
+  | _ -> failwith "Not a unit equality formula"
+;;  
+
+let rec convert_formula fv no_arities context f =
+  let atom = atom_of_formula f in
+  let t = convert_atom (fv = []) atom in
   let rec build_ctx n = function
     | [] -> t
     | hp::tl -> 
         PT.Binder 
           (`Forall,
             (mk_ident ("H" ^ string_of_int n), 
-                       Some (convert_formula true [] hp)), 
+              Some (convert_formula [] true [] hp)), 
             build_ctx (n+1) tl)
   in
   let arities = if no_arities then [] else collect_arities atom context in
-  build_ctx_for_arities arities (build_ctx 0 context) 
+  build_ctx_for_arities true arities (build_ctx 0 context) 
+;;
+
+let check_if_atom_is_negative = function
+  | A.True | A.False | A.Proposition _ | A.Predicate _ -> assert false
+  | A.Eq _ -> false
+  | A.NotEq _ -> true
+;;
+
+let check_if_formula_is_negative = function
+  | A.Disjunction _ -> assert false
+  | A.NegAtom a -> not (check_if_atom_is_negative a)
+  | A.Atom a -> check_if_atom_is_negative a
 ;;
 
 let convert_ast statements context = function
@@ -149,23 +172,48 @@ let convert_ast statements context = function
       | A.Axiom 
       | A.Hypothesis ->
           statements, f::context
+      | A.Negated_conjecture when not (check_if_formula_is_negative f) ->
+          statements, f::context
       | A.Negated_conjecture ->
-          if collect_fv_from_formulae f <> [] then
-            prerr_endline "CONTIENE FV";
+          assert_formulae_is_1eq_negated f;
+          let fv = collect_fv_from_formulae f in 
+          if fv <> [] then 
+            prerr_endline ("FREE VARIABLES: " ^ String.concat "," fv);
           let f = 
             PT.Binder 
              (`Forall,
-               (mk_ident "A",Some (PT.Sort `Set)), 
-               convert_formula false context f)
+               (mk_ident universe,Some (PT.Sort `Set)), 
+               convert_formula fv false context f)
           in
           let o = PT.Theorem (`Theorem,name,f,None) in
           statements @ [
             GA.Executable(floc,GA.Command(floc,GA.Obj(floc,o)));
             GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
-              GA.Intros (floc,None,[])),Some (GA.Dot(floc))));
-            GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
+            GA.Intros (floc,None,[])),Some (GA.Dot(floc))))] @
+          (if fv <> [] then     
+            (List.flatten
+              (List.map 
+                (fun _ -> 
+                  [GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
+                    GA.Exists floc),Some (GA.Branch floc)));
+                   GA.Executable(floc,GA.Tactical(floc,
+                    GA.Pos (floc,[2]),None))])
+                fv)) 
+           else [])@
+            [GA.Executable(floc,GA.Tactical(floc, GA.Tactic(floc,
               GA.Auto (floc,None,None,Some "paramodulation",None)),
-                Some (GA.Dot(floc))));
+                Some (GA.Dot(floc))))]@
+          (if fv <> [] then     
+            (List.flatten
+              (List.map 
+                (fun _ -> 
+                  [GA.Executable(floc,GA.Tactical(floc, GA.Shift floc, None));
+                   GA.Executable(floc,GA.Tactical(floc, GA.Skip floc,Some
+                   (GA.Merge floc)))])
+                fv)) 
+           else [])@
+            [GA.Executable(floc,GA.Tactical(floc, GA.Try(floc,
+              GA.Tactic (floc, GA.Assumption floc)), Some (GA.Dot(floc))));
             GA.Executable(floc,GA.Command(floc, GA.Qed(floc)))],
           context
       | A.Definition 
@@ -236,8 +284,19 @@ let _ =
       ([],[]) statements 
   in
   let pp t = 
-    (* for a correct pp we should disambiguate the term... *)
-    let term_pp = CicNotationPp.pp_term in
+    (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
+     * which will show up using the following command line:
+     * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
+    let width = max_int in
+    let term_pp content_term =
+      let pres_term = TermContentPres.pp_ast content_term in
+      let dummy_tbl = Hashtbl.create 1 in
+      let markup = CicNotationPres.render dummy_tbl pres_term in
+      let s = BoxPp.render_to_string width markup in
+      Pcre.substitute 
+        ~pat:"\\\\forall [Ha-z][a-z0-9_]*" ~subst:(fun x -> "\n" ^ x) s
+    in
+    CicNotationPp.set_pp_term term_pp;
     let lazy_term_pp = fun x -> assert false in
     let obj_pp = CicNotationPp.pp_obj in
     print_endline 
@@ -249,9 +308,17 @@ let _ =
     GA.Executable(floc,GA.Command(floc, GA.Include(floc,"legacy/coq.ma")))]
   in
   List.iter pp extra_statements_start;
-  print_endline
+  List.iter 
+   (fun (n,s) -> 
+   print_endline
     (LexiconAstPp.pp_command 
       (LA.Alias(floc,
-        LA.Ident_alias("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)"))) ^ ".");
+        LA.Ident_alias(n,s))) ^ "."))
+   [("eq","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1)");
+   ("trans_eq","cic:/Coq/Init/Logic/trans_eq.con");
+   ("eq_ind_r","cic:/Coq/Init/Logic/eq_ind_r.con");
+   ("eq_ind","cic:/Coq/Init/Logic/eq_ind.con");
+   ("sym_eq","cic:/Coq/Init/Logic/sym_eq.con");
+   ("refl_equal","cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)")];
   List.iter pp grafite_ast_statements;
   exit 0