]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaInterpreter.ml
snapshot (notably: implemented "check")
[helm.git] / helm / matita / matitaInterpreter.ml
index 2f475f748f2246640072175e7648c615fd83af71..45332107fd0efdc06c5b813c0bb7b8a6bae3cb0b 100644 (file)
 
 open Printf
 
-type state_tag = [ `Command | `Proof ]
+  (** None means: "same state as before" *)
+type state_tag = [ `Command | `Proof ] option
 
 exception Command_error of string
 
+let canonical_context metano metasenv =
+  try
+    let (_, context, _) = List.find (fun (m, _, _) -> m = metano) metasenv in
+    context
+  with Not_found ->
+    failwith (sprintf "Can't find canonical context for %d" metano)
+
+let get_context_and_metasenv (proof_handler:MatitaTypes.proof_handler) =
+  if proof_handler.MatitaTypes.has_proof () then
+    let proof = proof_handler.MatitaTypes.get_proof () in
+    let metasenv = proof#metasenv in
+    let goal = proof#goal in
+    (canonical_context goal metasenv, metasenv)
+  else
+    ([], [])
+
+  (** term AST -> Cic.term. Uses disambiguator and change imperatively the
+  * metasenv as needed *)
+let disambiguate ~(disambiguator:MatitaTypes.disambiguator) ~proof_handler ast =
+  if proof_handler.MatitaTypes.has_proof () then begin
+    let proof = proof_handler.MatitaTypes.get_proof () in
+    let metasenv = proof#metasenv in
+    let goal = proof#goal in
+    let context = canonical_context goal metasenv in
+    let (_, metasenv, term) as retval =
+      disambiguator#disambiguateTermAst ~context ~metasenv ast
+    in
+    proof#set_metasenv metasenv;
+    retval
+  end else
+    disambiguator#disambiguateTermAst ast
+
 class virtual interpreterState ~(console: MatitaConsole.console) =
   object (self)
       (** eval a toplevel phrase in the current state and return the new state
@@ -50,6 +83,12 @@ class virtual interpreterState ~(console: MatitaConsole.console) =
     method evalPhrase s = self#evalTactical (self#parsePhrase s)
   end
 
+let check_widget: MatitaTypes.sequents_viewer lazy_t = lazy (
+  let gui = MatitaGui.instance () in
+  let notebook = GPack.notebook ~show:true ~packing:gui#check#checkWin#add () in
+  let sequent_viewer = MatitaMathView.sequent_viewer ~show:true () in
+  MatitaMathView.sequents_viewer ~notebook ~sequent_viewer ())
+
   (** Implements phrases that should be accepted in all states *)
 class sharedState
   ~(disambiguator: MatitaTypes.disambiguator)
@@ -62,10 +101,24 @@ class sharedState
     method evalTactical = function
       | TacticAst.Command TacticAst.Quit ->
           proof_handler.MatitaTypes.quit ();
-          `Command (* dummy answer, useless *)
+          Some `Command (* dummy answer, useless *)
       | TacticAst.Command TacticAst.Proof ->
             (* do nothing, just for compatibility with coq syntax *)
-          `Command
+          Some `Command
+      | TacticAst.Command (TacticAst.Check term) ->
+          let dummyno = 1023 in
+          let (_, _, term) = disambiguate ~disambiguator ~proof_handler term in
+          let (context, metasenv) = get_context_and_metasenv proof_handler in
+          let sequent = (dummyno, context, term) in
+          let metadata = Cic2acic.asequent_of_sequent metasenv sequent in
+          let widget = Lazy.force check_widget in
+          let gui = MatitaGui.instance () in
+          gui#check#checkWin#show ();
+          gui#main#showCheckMenuItem#set_active true;
+          widget#reset;
+          widget#load_sequents [dummyno, metadata];
+          widget#goto_sequent dummyno;
+          None
       | tactical ->
           raise (Command_error (TacticAstPp.pp_tactical tactical))
   end
@@ -87,23 +140,16 @@ class commandState
           let (_, metasenv, expr) = disambiguator#disambiguateTermAst ast in
           let proof = MatitaProof.proof ~typ:expr ~metasenv () in
           proof_handler.MatitaTypes.new_proof proof;
-          `Proof
+          Some `Proof
       | TacticAst.Command TacticAst.Quit ->
           proof_handler.MatitaTypes.quit ();
-          `Command (* dummy answer, useless *)
+          Some `Command (* dummy answer, useless *)
       | TacticAst.Command TacticAst.Proof ->
             (* do nothing, just for compatibility with coq syntax *)
-          `Command
+          Some `Command
       | tactical -> shared#evalTactical tactical
   end
 
-let canonical_context metano metasenv =
-  try
-    let (_, context, _) = List.find (fun (m, _, _) -> m = metano) metasenv in
-    context
-  with Not_found ->
-    failwith (sprintf "Can't find canonical context for %d" metano)
-
   (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
   * names as long as they are available, then it fallbacks to name generation
   * using FreshNamesGenerator module *)
@@ -126,17 +172,8 @@ class proofState
   ~(console: MatitaConsole.console)
   ()
 =
-    (** term AST -> Cic.term. Uses disambiguator and change imperatively the
-    * metasenv as needed *)
   let disambiguate ast =
-    let proof = proof_handler.MatitaTypes.get_proof () in
-    let metasenv = proof#metasenv in
-    let goal =  proof#goal in
-    let context = canonical_context goal metasenv in
-    let (_, metasenv, term) =
-      disambiguator#disambiguateTermAst ~context ~metasenv ast
-    in
-    proof#set_metasenv metasenv;
+    let (_, _, term) = disambiguate ~disambiguator ~proof_handler ast in
     term
   in
     (** tactic AST -> ProofEngineTypes.tactic *)
@@ -190,21 +227,21 @@ class proofState
       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
       | TacticAst.Command TacticAst.Abort ->
           proof_handler.MatitaTypes.abort_proof ();
-          `Command
+          Some `Command
       | TacticAst.Command (TacticAst.Undo steps) ->
           (proof_handler.MatitaTypes.get_proof ())#undo ?steps ();
-          `Proof
+          Some `Proof
       | TacticAst.Command (TacticAst.Redo steps) ->
           (proof_handler.MatitaTypes.get_proof ())#redo ?steps ();
-          `Proof
+          Some `Proof
       | TacticAst.Seq tacticals ->
           (* TODO Zack check for proof completed at each step? *)
           List.iter (fun t -> ignore (self#evalTactical t)) tacticals;
-          `Proof
+          Some `Proof
       | TacticAst.Tactic tactic_phrase ->
           let tactic = lookup_tactic tactic_phrase in
           (proof_handler.MatitaTypes.get_proof ())#apply_tactic tactic;
-          `Proof
+          Some `Proof
       | tactical -> shared#evalTactical tactical
   end
 
@@ -221,11 +258,14 @@ class interpreter
   object
     val mutable state = commandState
 
+    method reset = state <- commandState
+
     method evalPhrase s =
       try
         (match state#evalPhrase s with
-        | `Command -> state <- commandState
-        | `Proof -> state <- proofState)
+        | Some `Command -> state <- commandState
+        | Some `Proof -> state <- proofState
+        | None -> ())
       with exn ->
         console#echo_error (sprintf "Uncaught exception: %s"
           (Printexc.to_string exn))