]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaInterpreter.ml
snapshot, notably:
[helm.git] / helm / matita / matitaInterpreter.ml
index d5ca65d22bebddf4dddaf3aa131bc807e1703b93..35ec6119d2bf4d781fcf0c7c6e386da915e5dd6a 100644 (file)
 
 open Printf
 
+open MatitaTypes
+
   (** None means: "same state as before" *)
 type state_tag = [ `Command | `Proof ] option
 
 exception Command_error of string
 
+(*
 let uri name =
   UriManager.uri_of_string (sprintf "%s/%s" BuildTimeConf.base_uri name)
+*)
+
+let baseuri = ref "cic:/matita"
+let qualify name =
+  let baseuri = !baseuri in
+  if baseuri.[String.length baseuri - 1] = '/' then
+    baseuri ^ name
+  else
+    String.concat "/" [baseuri; name]
 
 let canonical_context metano metasenv =
   try
@@ -74,16 +86,49 @@ let disambiguate ~(disambiguator:MatitaTypes.disambiguator) ~proof_handler ast =
   end else
     disambiguator#disambiguateTermAst ast
 
-class virtual interpreterState ~(console: MatitaConsole.console) =
+class virtual interpreterState = 
+    (* static values, shared by all states inheriting this class *)
+  let loc = ref None in
+  let history = ref [] in
+  fun ~(console: MatitaConsole.console) ->
   object (self)
+
       (** eval a toplevel phrase in the current state and return the new state
       *)
-    method parsePhrase s = CicTextualParser2.parse_tactical (Stream.of_string s)
+    method parsePhrase s =
+      match CicTextualParser2.parse_tactical (Stream.of_string s) with
+      | (TacticAst.LocatedTactical (loc', tac)) as tactical ->
+          loc := Some loc';
+          (match tac with (* update interpreter history *)
+          | TacticAst.Command (TacticAst.Qed None) ->
+              history := `Qed :: !history
+          | TacticAst.Command (TacticAst.Theorem (_, Some name, _, None)) ->
+              history := `Theorem name :: !history
+          | TacticAst.Command (TacticAst.Qed _)
+          | TacticAst.Command (TacticAst.Theorem _) -> assert false
+          | _ -> history := `Tactic :: !history);
+          tactical
+      | _ -> assert false
 
     method virtual evalTactical:
       (CicAst.term, string) TacticAst.tactical -> state_tag
 
-    method evalPhrase s = self#evalTactical (self#parsePhrase s)
+(*
+    method undo ?(steps = 1) () =
+      for i = 1 to steps do
+        match List.hd !history with
+        | `Qed 
+        FINQUI
+*)
+
+    method evalPhrase s =
+      debug_print (sprintf "evaluating '%s'" s);
+      self#evalTactical (self#parsePhrase s)
+
+    method endOffset =
+      match !loc with
+      | Some (start_pos, end_pos) -> end_pos.Lexing.pos_cnum
+      | None -> failwith "MatitaInterpreter: no offset recorded"
 
   end
 
@@ -108,6 +153,13 @@ class sharedState
       | TacticAst.Command TacticAst.Proof ->
             (* do nothing, just for compatibility with coq syntax *)
           Some `Command
+      | TacticAst.Command (TacticAst.Baseuri (Some uri)) ->
+          baseuri := uri;
+          console#echo_message (sprintf "base uri set to \"%s\"" uri);
+          None
+      | TacticAst.Command (TacticAst.Baseuri None) ->
+          console#echo_message (sprintf "base uri is \"%s\"" !baseuri);
+          None
       | TacticAst.Command (TacticAst.Check term) ->
           let (_, _, term) = disambiguate ~disambiguator ~proof_handler term in
           let (context, metasenv) = get_context_and_metasenv proof_handler in
@@ -138,14 +190,10 @@ class commandState
 
     method evalTactical = function
       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
-      | TacticAst.Command (TacticAst.Theorem (_, name_opt, ast, None)) ->
+      | TacticAst.Command (TacticAst.Theorem (_, Some name, ast, None)) ->
           let (_, metasenv, expr) = disambiguator#disambiguateTermAst ast in
-          let uri =
-            match name_opt with
-            | None -> None
-            | Some name -> Some (uri name)
-          in
-          let proof = MatitaProof.proof ~typ:expr ?uri ~metasenv () in
+          let uri = UriManager.uri_of_string (qualify name) in
+          let proof = MatitaProof.proof ~typ:expr ~uri ~metasenv () in
           proof_handler.MatitaTypes.new_proof proof;
           Some `Proof
       | TacticAst.Command TacticAst.Quit ->
@@ -240,14 +288,11 @@ class proofState
       | TacticAst.Command (TacticAst.Redo steps) ->
           (proof_handler.MatitaTypes.get_proof ())#redo ?steps ();
           Some `Proof
-      | TacticAst.Command (TacticAst.Qed name_opt) ->
+      | TacticAst.Command (TacticAst.Qed None) ->
           (* TODO Zack this function probably should not simply fail with
           * Failure, but rather raise some more meaningful exception *)
           if not (proof_handler.MatitaTypes.has_proof ()) then assert false;
           let proof = proof_handler.MatitaTypes.get_proof () in
-          (match name_opt with
-          | None -> ()
-          | Some name -> proof#set_uri (uri name));
           let (uri, metasenv, bo, ty) = proof#proof in
           let uri = MatitaTypes.unopt_uri uri in
           if metasenv <> [] then failwith "Proof not completed";
@@ -258,6 +303,7 @@ class proofState
           CicEnvironment.add_type_checked_term uri
             (Cic.Constant ((UriManager.name_of_uri uri),(Some bo),ty,[]));
           proof_handler.MatitaTypes.set_proof None;
+          (MatitaMathView.proof_viewer_instance ())#unload;
           (* TODO Zack a lot more to be done here:
             * - save object to disk in xml format
             * - collect metadata
@@ -289,34 +335,22 @@ class interpreter
 
     method reset = state <- commandState
 
+    method endOffset = state#endOffset
+
     method private updateState = function
       | Some `Command -> state <- commandState
       | Some `Proof -> state <- proofState
       | None -> ()
 
-    method evalPhrase s =
+    method evalPhrase ?(transparent = false) s =
       try
         self#updateState (state#evalPhrase s)
       with exn ->
-        console#echo_error (sprintf "Uncaught exception: %s"
-          (Printexc.to_string exn))
-
-(*
-    method evalAll s =
-      let get_end_pos = function
-        | TacticAst.LocatedTactical ((_, end_pos), _) -> end_pos.Lexing.pos_cnum
-        | _ -> assert false
-      in
-      let str_len = String.length s in
-      let rec aux offset =
-        let tactical =
-          self#parsePhrase (String.sub s offset (str_len - offset))
-        in
-        self#updateState (state#evalTactical tactical);
-        let next_offset = get_end_pos tactical + offset in
-        if next_offset = str_len - 1 then
-      in
-*)
+        if transparent then
+          raise exn
+        else
+          console#echo_error (sprintf "Uncaught exception: %s"
+            (Printexc.to_string exn))
 
   end