]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaInterpreter.ml
bumped changelog line to match upload date
[helm.git] / helm / matita / matitaInterpreter.ml
index 3e505c28e1b1b18bdcfc35171100b79567cc0413..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,50 @@ 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
 
 let check_widget: MatitaTypes.sequent_viewer lazy_t = lazy
@@ -107,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
@@ -137,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 ->
@@ -187,28 +236,26 @@ class proofState
     | TacticAst.LocatedTactic (_, tactic) -> lookup_tactic tactic
     | TacticAst.Intros (_, names) ->  (* TODO Zack implement intros length *)
         PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
-    | TacticAst.Reflexivity -> EqualityTactics.reflexivity_tac
-    | TacticAst.Assumption -> VariousTactics.assumption_tac
-    | TacticAst.Contradiction -> NegationTactics.contradiction_tac
-    | TacticAst.Exists -> IntroductionTactics.exists_tac
-    | TacticAst.Fourier -> FourierR.fourier_tac
-    | TacticAst.Left -> IntroductionTactics.left_tac
-    | TacticAst.Right -> IntroductionTactics.right_tac
-    | TacticAst.Ring -> Ring.ring_tac
-    | TacticAst.Split -> IntroductionTactics.split_tac
-    | TacticAst.Symmetry -> EqualityTactics.symmetry_tac
-    | TacticAst.Transitivity term ->
-        EqualityTactics.transitivity_tac (disambiguate term)
-    | TacticAst.Apply term -> PrimitiveTactics.apply_tac (disambiguate term)
-    | TacticAst.Absurd term -> NegationTactics.absurd_tac (disambiguate term)
-    | TacticAst.Exact term -> PrimitiveTactics.exact_tac (disambiguate term)
-    | TacticAst.Cut term -> PrimitiveTactics.cut_tac (disambiguate term)
+    | TacticAst.Reflexivity -> Tactics.reflexivity
+    | TacticAst.Assumption -> Tactics.assumption
+    | TacticAst.Contradiction -> Tactics.contradiction
+    | TacticAst.Exists -> Tactics.exists
+    | TacticAst.Fourier -> Tactics.fourier
+    | TacticAst.Left -> Tactics.left
+    | TacticAst.Right -> Tactics.right
+    | TacticAst.Ring -> Tactics.ring
+    | TacticAst.Split -> Tactics.split
+    | TacticAst.Symmetry -> Tactics.symmetry
+    | TacticAst.Transitivity term -> Tactics.transitivity (disambiguate term)
+    | TacticAst.Apply term -> Tactics.apply (disambiguate term)
+    | TacticAst.Absurd term -> Tactics.absurd (disambiguate term)
+    | TacticAst.Exact term -> Tactics.exact (disambiguate term)
+    | TacticAst.Cut term -> Tactics.cut (disambiguate term)
     | TacticAst.Elim (term, _) -> (* TODO Zack implement "using" argument *)
-        PrimitiveTactics.elim_intros_simpl_tac (disambiguate term)
-    | TacticAst.ElimType term ->
-        EliminationTactics.elim_type_tac (disambiguate term)
+        Tactics.elim_intros_simpl (disambiguate term)
+    | TacticAst.ElimType term -> Tactics.elim_type (disambiguate term)
     | TacticAst.Replace (what, with_what) ->
-        EqualityTactics.replace_tac ~what:(disambiguate what)
+        Tactics.replace ~what:(disambiguate what)
           ~with_what:(disambiguate with_what)
   (*
     (* TODO Zack a lot more of tactics to be implemented here ... *)
@@ -241,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";
@@ -259,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
@@ -285,19 +330,27 @@ class interpreter
     new commandState ~disambiguator ~proof_handler ~console ()
   in
   let proofState = new proofState ~disambiguator ~proof_handler ~console () in
-  object
+  object (self)
     val mutable state = commandState
 
     method reset = state <- commandState
 
-    method evalPhrase s =
+    method endOffset = state#endOffset
+
+    method private updateState = function
+      | Some `Command -> state <- commandState
+      | Some `Proof -> state <- proofState
+      | None -> ()
+
+    method evalPhrase ?(transparent = false) s =
       try
-        (match state#evalPhrase s with
-        | Some `Command -> state <- commandState
-        | Some `Proof -> state <- proofState
-        | None -> ())
+        self#updateState (state#evalPhrase s)
       with exn ->
-        console#echo_error (sprintf "Uncaught exception: %s"
-          (Printexc.to_string exn))
+        if transparent then
+          raise exn
+        else
+          console#echo_error (sprintf "Uncaught exception: %s"
+            (Printexc.to_string exn))
+
   end