]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaInterpreter.ml
added save_object_to_disk and basedir
[helm.git] / helm / matita / matitaInterpreter.ml
index 511a5931476f30fe354246257fb7bf4665263fb8..f52901ea684f4a075e1766b1c6a7d32e0f9413b6 100644 (file)
@@ -46,16 +46,18 @@ let uri name =
   UriManager.uri_of_string (sprintf "%s/%s" BuildTimeConf.base_uri name)
 *)
 
-let baseuri = ref "cic:/matita"
+let baseuri = lazy (ref ("cic:/matita/" ^ Helm_registry.get "matita.owner"))
+let basedir = ref ((Unix.getpwuid (Unix.getuid ())).Unix.pw_dir) ;;
+
 let qualify name =
-  let baseuri = !baseuri in
+  let baseuri = !(Lazy.force baseuri) in
   if baseuri.[String.length baseuri - 1] = '/' then
     baseuri ^ name
   else
     String.concat "/" [baseuri; name]
 let split_obj = function
-  | Cic.Constant (name, body, ty, _)
-  | Cic.Variable (name, body, ty, _) -> (name, body, ty)
+  | Cic.Constant (name, body, ty, _, attrs)
+  | Cic.Variable (name, body, ty, _, attrs) -> (name, body, ty, attrs)
   | _ -> assert false
 
 let canonical_context metano metasenv =
@@ -149,17 +151,28 @@ class sharedState
             (* do nothing, just for compatibility with coq syntax *)
           New_state Command
       | TacticAst.Command (TacticAst.Baseuri (Some uri)) ->
-          baseuri := uri;
+          Lazy.force baseuri := uri;
           console#echo_message (sprintf "base uri set to \"%s\"" uri);
           Quiet
       | TacticAst.Command (TacticAst.Baseuri None) ->
-          console#echo_message (sprintf "base uri is \"%s\"" !baseuri);
+          console#echo_message (sprintf "base uri is \"%s\""
+            !(Lazy.force baseuri));
+          Quiet
+      | TacticAst.Command (TacticAst.Basedir (Some path)) ->
+          basedir := path;
+          console#echo_message (sprintf "base dir set to \"%s\"" path);
+          Quiet
+      | TacticAst.Command (TacticAst.Basedir None) ->
+          console#echo_message (sprintf "base dir is \"%s\"" !basedir);
           Quiet
       | TacticAst.Command (TacticAst.Check term) ->
           let (_, _, term,ugraph) = 
            disambiguate ~disambiguator ~currentProof term 
          in
           let (context, metasenv) = get_context_and_metasenv currentProof in
+(* this is the Eval Compute
+          let term = CicReduction.whd context term in
+*)         
           let dummyno = CicMkImplicit.new_meta metasenv [] in
           let ty,ugraph1 = 
            CicTypeChecker.type_of_aux' metasenv context term ugraph 
@@ -180,6 +193,13 @@ class sharedState
           in
           (* TODO ZACK: show URIs to the user *)
           Quiet
+      | TacticAst.Command (TacticAst.Print `Env) ->
+          let uris = CicEnvironment.list_uri () in
+          List.iter (fun u ->
+            console#echo_message (UriManager.string_of_uri u);
+            prerr_endline "x"
+          ) uris;
+          Quiet
       | tactical ->
           raise (Command_error (TacticAstPp.pp_tactical tactical))
   end
@@ -254,23 +274,115 @@ let inddef_of_ast params indTypes (disambiguator:MatitaTypes.disambiguator) =
       [] indTypes
   in
   let cicIndTypes = List.rev cicIndTypes in
-(*
-  prerr_endline uri;
-  pp_indtypes cicIndTypes;
-*)
   (UriManager.uri_of_string uri, (cicIndTypes, [], paramsno))
 
+ (* 
+ *
+ *
+ * FIXME this should be in another module, shared with gTopLevel 
+ *
+ *
+ * *)
+let
+ save_object_to_disk uri annobj ids_to_inner_sorts ids_to_inner_types pathname
+=
+ let name =
+  let struri = UriManager.string_of_uri uri in
+  let idx = (String.rindex struri '/') + 1 in
+   String.sub struri idx (String.length struri - idx)
+ in
+  let path = pathname ^ "/" ^ name in
+  let xml, bodyxml =
+   Cic2Xml.print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter:false
+    annobj 
+  in
+  let xmlinnertypes =
+   Cic2Xml.print_inner_types uri ~ids_to_inner_sorts ~ids_to_inner_types
+    ~ask_dtd_to_the_getter:false
+  in
+   (* innertypes *)
+   let innertypesuri = UriManager.innertypesuri_of_uri uri in
+    Xml.pp ~quiet:true xmlinnertypes (Some (path ^ ".types.xml")) ;
+    Http_getter.register' innertypesuri
+     (Helm_registry.get "local_library.url" ^
+       Str.replace_first (Str.regexp "^cic:") ""
+        (UriManager.string_of_uri innertypesuri) ^ ".xml"
+     ) ;
+    (* constant type / variable / mutual inductive types definition *)
+    Xml.pp ~quiet:true xml (Some (path ^ ".xml")) ;
+    Http_getter.register' uri
+     (Helm_registry.get "local_library.url" ^
+       Str.replace_first (Str.regexp "^cic:") ""
+        (UriManager.string_of_uri uri) ^ ".xml"
+     ) ;
+    match bodyxml with
+       None -> ()
+     | Some bodyxml' ->
+        (* constant body *)
+        let bodyuri =
+         match UriManager.bodyuri_of_uri uri with
+            None -> assert false
+          | Some bodyuri -> bodyuri
+        in
+         Xml.pp ~quiet:true bodyxml' (Some (path ^ ".body.xml")) ;
+         Http_getter.register' bodyuri
+          (Helm_registry.get "local_library.url" ^
+            Str.replace_first (Str.regexp "^cic:") ""
+             (UriManager.string_of_uri bodyuri) ^ ".xml"
+          )
+;;
+
   (* TODO Zack a lot more to be done here:
     * - save object to disk in xml format
     * - register uri to the getter 
     * - save universe file *)
-let add_constant_to_world ~dbd ~uri ?body ~ty ~ugraph () =
-  let name = UriManager.name_of_uri uri in
-  let obj = Cic.Constant (name, body, ty, []) in
-  let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in
-  CicEnvironment.add_type_checked_term uri (obj, ugraph);
-  MetadataDb.index_constant ~dbd
-    ~owner:(Helm_registry.get "matita.owner") ~uri ~body ~ty
+let add_constant_to_world ~(console:MatitaTypes.console)
+  ~dbd ~uri ?body ~ty ?(params = []) ?(attrs = []) ~ugraph ()
+=
+  let suri = UriManager.string_of_uri uri in
+  if CicEnvironment.in_library uri then
+    error (sprintf "%s constant already defined" suri)
+  else begin
+    let name = UriManager.name_of_uri uri in
+    let obj = Cic.Constant (name, body, ty, params, attrs) in
+    let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in
+    CicEnvironment.add_type_checked_term uri (obj, ugraph);
+    MetadataDb.index_constant ~dbd
+      ~owner:(Helm_registry.get "matita.owner") ~uri ~body ~ty;
+    console#echo_message (sprintf "%s constant defined" suri)
+  end
+
+let add_inductive_def_to_world ~(console:MatitaTypes.console)
+  ~dbd ~uri ~indTypes ?(params = []) ?(leftno = 0) ?(attrs = []) ~ugraph ()
+=
+  let suri = UriManager.string_of_uri uri in
+  if CicEnvironment.in_library uri then
+    error (sprintf "%s inductive type already defined" suri)
+  else begin
+    let name = UriManager.name_of_uri uri in
+    let obj = Cic.InductiveDefinition (indTypes, params, leftno, attrs) in
+    let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in
+    CicEnvironment.put_inductive_definition uri (obj, ugraph);
+    MetadataDb.index_inductive_def ~dbd
+      ~owner:(Helm_registry.get "matita.owner") ~uri ~types:indTypes;
+    console#echo_message (sprintf "%s inductive type defined" suri);
+    let elim sort =
+      try
+        let obj = CicElim.elim_of ~sort uri 0 in
+        let (name, body, ty, attrs) = split_obj obj in
+        let suri = qualify name ^ ".con" in
+        let uri = UriManager.uri_of_string suri in
+          (* TODO Zack: make CicElim returns a universe *)
+        let ugraph = CicUniv.empty_ugraph in
+        add_constant_to_world ~console ~dbd ~uri ?body ~ty ~attrs ~ugraph ();
+(*
+        console#echo_message
+          (sprintf "%s eliminator (automatically) defined" suri)
+*)
+      with CicElim.Can_t_eliminate -> ()
+    in
+    List.iter elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
+  end
 
   (** Implements phrases that should be accepted only in Command state *)
 class commandState
@@ -314,41 +426,101 @@ class commandState
           in
           let body = CicMetaSubst.apply_subst subst body_cic in
           let ty = CicMetaSubst.apply_subst subst type_cic in
-          add_constant_to_world ~dbd ~uri ~body ~ty ~ugraph ();
+          add_constant_to_world ~console ~dbd ~uri ~body ~ty ~ugraph ();
           Quiet
       | TacticAst.Command (TacticAst.Inductive (params, indTypes)) ->
+          
           let (uri, (indTypes, params, leftno)) =
             inddef_of_ast params indTypes disambiguator
           in
-          let obj = Cic.InductiveDefinition (indTypes, params, leftno) in
+          let obj = Cic.InductiveDefinition (indTypes, params, leftno, []) in
           let ugraph =
             CicTypeChecker.typecheck_mutual_inductive_defs uri
               (indTypes, params, leftno) CicUniv.empty_ugraph
-         in
-          let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in 
-          CicEnvironment.put_inductive_definition uri (obj, ugraph);
-          MetadataDb.index_inductive_def ~dbd
-            ~owner:(Helm_registry.get "matita.owner") ~uri ~types:indTypes;
-          let msgs = ref [] in
-          let elim sort =
-            try
-              let obj = CicElim.elim_of ~sort uri 0 in
-              let (name, body, ty) = split_obj obj in
-              let uri = UriManager.uri_of_string (qualify name ^ ".con") in
-                (* TODO Zack: make CicElim returns a universe *)
-              let ugraph = CicUniv.empty_ugraph in
-              add_constant_to_world ~dbd ~uri ?body ~ty ~ugraph ();
-              msgs := (sprintf "%s defined" name) :: !msgs;
-            with CicElim.Can_t_eliminate -> ()
           in
-          List.iter elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
-          Echo (String.concat "\n" (List.rev !msgs))
+          add_inductive_def_to_world ~console
+            ~dbd ~uri ~indTypes ~params ~leftno ~ugraph ();
+          Quiet
       | TacticAst.Command TacticAst.Quit ->
           currentProof#quit ();
           New_state Command (* dummy answer, useless *)
       | TacticAst.Command TacticAst.Proof ->
             (* do nothing, just for compatibility with coq syntax *)
           New_state Command
+      | TacticAst.Command (TacticAst.Coercion c_ast) ->
+          prerr_endline ("beccata la coercion " ^ (CicAstPp.pp_term c_ast));
+
+          let env, metasenv, coercion, ugraph = 
+            disambiguator#disambiguateTermAst c_ast 
+          in
+          let coer_uri,coer_ty =
+            match coercion with 
+            | Cic.Const (uri,_)
+            | Cic.Var (uri,_) ->
+                let o,_ = 
+                  CicEnvironment.get_obj CicUniv.empty_ugraph uri 
+                in
+                (match o with
+                | Cic.Constant (_,_,ty,_,_)
+                | Cic.Variable (_,_,ty,_,_) ->
+                    uri,ty
+                | _ -> assert false)
+            | Cic.MutConstruct (uri,t,c,_) ->
+                let o,_ = 
+                  CicEnvironment.get_obj CicUniv.empty_ugraph uri 
+                in
+                (match o with
+                | Cic.InductiveDefinition (l,_,_,_) ->
+                    let (_,_,_,cl) = List.nth l t in
+                    let (_,cty) = List.nth cl c in
+                      uri,cty
+                | _ -> assert false)
+            | _ -> assert false 
+          in
+          (* we have to get the source and the tgt type uri 
+           * in Coq syntax we have already their names, but 
+           * since we don't support Funclass and similar I think
+           * all the coercion should be of the form
+           * (A:?)(B:?)T1->T2
+           * So we should be able to extract them from the coercion type
+           *)
+          let extract_last_two_p ty =
+            let rec aux = function
+              | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))   
+              | Cic.Prod( _, src, tgt) -> src, tgt
+              | _ -> assert false
+            in  
+            aux ty
+          in
+          let uri_of_term = function
+            | Cic.Const(u,_) -> u
+            | Cic.MutInd (u, i , _) ->
+                (* we have to build by hand the #xpointer *)
+                let base = UriManager.string_of_uri u in
+                let xp = "#xpointer(1/" ^ (string_of_int (i+1)) ^ ")" in
+                  UriManager.uri_of_string (base ^ xp)
+            | _ -> assert false 
+          in
+          let ty_src,ty_tgt = extract_last_two_p coer_ty in
+          let src_uri = uri_of_term ty_src in
+          let tgt_uri = uri_of_term ty_tgt in
+          let coercions_to_add = 
+            CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri
+          in
+          (* FIXME: we should chek it this object can be a coercion 
+           * maybe add the check to extract_last_two_p
+           *)
+          List.iter (fun (uri,obj,ugraph) -> 
+            (*
+            prerr_endline (Printf.sprintf 
+             "Aggiungo la coercion %s\n%s\n\n"
+             (UriManager.string_of_uri uri) (CicPp.ppobj obj));
+            *)
+            let (name, body, ty, attrs) = split_obj obj in
+            add_constant_to_world ~console 
+              ~dbd ~uri ?body ~ty ~attrs ~ugraph ();
+          ) coercions_to_add;
+          Quiet
       | tactical -> shared#evalTactical tactical
   end
 
@@ -423,7 +595,9 @@ class proofState
     | _ ->
         MatitaTypes.not_implemented "some tactic"
   in
-  let shared = new sharedState ~disambiguator ~currentProof ~console ~dbd () in
+  let shared =
+    new sharedState ~disambiguator ~currentProof ~console ?mathViewer ~dbd ()
+  in
   object (self)
     inherit interpreterState ~console
 
@@ -443,6 +617,7 @@ class proofState
           let proof = currentProof#proof in
           let (uri, metasenv, bo, ty) = proof#proof in
           let uri = MatitaTypes.unopt_uri uri in
+          let suri = UriManager.string_of_uri uri in
             (* TODO Zack this function probably should not simply fail with
             * Failure, but rather raise some more meaningful exception *)
           if metasenv <> [] then failwith "Proof not completed";
@@ -453,9 +628,10 @@ class proofState
            CicReduction.are_convertible [] proved_ty ty ugraph 
          in
           if not b then failwith "Wrong proof";
-          add_constant_to_world ~dbd ~uri ~body:bo ~ty ~ugraph ();
+          add_constant_to_world ~console ~dbd ~uri ~body:bo ~ty ~ugraph ();
           currentProof#abort ();
           (match mathViewer with None -> () | Some v -> v#unload ());
+          console#echo_message (sprintf "%s defined" suri);
           New_state Command
       | TacticAst.Seq tacticals ->
           (* TODO Zack check for proof completed at each step? *)
@@ -495,7 +671,7 @@ class interpreter
       | _ -> ()
 
     method private eval f =
-      let ok () = console#clear (); (true, true) in
+      let ok () = (* console#clear (); *) (true, true) in
       match console#wrap_exn f with
       | Some (New_state Command) -> (state <- commandState); ok ()
       | Some (New_state Proof) -> (state <- proofState); ok ()