]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/gTopLevel/gTopLevel.ml
*** empty log message ***
[helm.git] / helm / gTopLevel / gTopLevel.ml
index 800e69aa07b485cd326638ad3b3f5ba33caee01c..789a65ba5c87330d11d00c027ea602f733fb9cac 100644 (file)
@@ -53,6 +53,7 @@ let htmlheader_and_content = ref htmlheader;;
 
 let current_cic_infos = ref None;;
 let current_goal_infos = ref None;;
+let current_scratch_infos = ref None;;
 
 
 (* MISC FUNCTIONS *)
@@ -82,12 +83,6 @@ let c2 = parseStyle "annotatedpres.xsl";;
 
 let getterURL = Configuration.getter_url;;
 let processorURL = Configuration.processor_url;;
-(*
-let processorURL = "http://phd.cs.unibo.it:8080/helm/servelt/uwobo/";;
-let getterURL = "http://phd.cs.unibo.it:8081/";;
-let processorURL = "http://localhost:8080/helm/servelt/uwobo/";;
-let getterURL = "http://localhost:8081/";;
-*)
 
 let mml_styles = [d_c ; c1 ; g ; c2 ; l];;
 let mml_args =
@@ -164,71 +159,106 @@ let mml_of_cic_object uri annobj ids_to_inner_sorts ids_to_inner_types =
 
 (* CALLBACKS *)
 
+exception RefreshSequentException of exn;;
+exception RefreshProofException of exn;;
+
 let refresh_proof (output : GMathView.math_view) =
- let uri,currentproof =
-  match !ProofEngine.proof with
-     None -> assert false
-   | Some (uri,metasenv,bo,ty) ->
-      uri,(Cic.CurrentProof (UriManager.name_of_uri uri, metasenv, bo, ty))
- in
- let
-  (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types)
- =
-  Cic2acic.acic_object_of_cic_object currentproof
- in
-  let mml = mml_of_cic_object uri acic ids_to_inner_sorts ids_to_inner_types in
-   output#load_tree mml ;
-   current_cic_infos := Some (ids_to_terms,ids_to_father_ids)
+ try
+  let uri,currentproof =
+   match !ProofEngine.proof with
+      None -> assert false
+    | Some (uri,metasenv,bo,ty) ->
+       uri,(Cic.CurrentProof (UriManager.name_of_uri uri, metasenv, bo, ty))
+  in
+   let
+    (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types)
+   =
+    Cic2acic.acic_object_of_cic_object currentproof
+   in
+    let mml =
+     mml_of_cic_object uri acic ids_to_inner_sorts ids_to_inner_types
+    in
+     output#load_tree mml ;
+     current_cic_infos := Some (ids_to_terms,ids_to_father_ids)
+ with
+  e ->
+ match !ProofEngine.proof with
+    None -> assert false
+  | Some (uri,metasenv,bo,ty) ->
+prerr_endline ("Offending proof: " ^ CicPp.ppobj (Cic.CurrentProof ("questa",metasenv,bo,ty))) ; flush stderr ;
+   raise (RefreshProofException e)
 ;;
 
 let refresh_sequent (proofw : GMathView.math_view) =
- match !ProofEngine.goal with
-    None -> proofw#unload
-  | Some (_,currentsequent) ->
-     let metasenv =
-      match !ProofEngine.proof with
-         None -> assert false
-       | Some (_,metasenv,_,_) -> metasenv
-     in
-      let sequent_gdome,ids_to_terms,ids_to_father_ids =
-       SequentPp.XmlPp.print_sequent metasenv currentsequent
+ try
+  match !ProofEngine.goal with
+     None -> proofw#unload
+   | Some metano ->
+      let metasenv =
+       match !ProofEngine.proof with
+          None -> assert false
+        | Some (_,metasenv,_,_) -> metasenv
       in
-       let sequent_doc =
-        Xml2Gdome.document_of_xml domImpl sequent_gdome
+      let currentsequent = List.find (function (m,_,_) -> m=metano) metasenv in
+       let sequent_gdome,ids_to_terms,ids_to_father_ids =
+        SequentPp.XmlPp.print_sequent metasenv currentsequent
        in
-        let sequent_mml =
-         applyStylesheets sequent_doc sequent_styles sequent_args
+        let sequent_doc =
+         Xml2Gdome.document_of_xml domImpl sequent_gdome
         in
-         proofw#load_tree ~dom:sequent_mml ;
-         current_goal_infos := Some (ids_to_terms,ids_to_father_ids)
+         let sequent_mml =
+          applyStylesheets sequent_doc sequent_styles sequent_args
+         in
+          proofw#load_tree ~dom:sequent_mml ;
+          current_goal_infos := Some (ids_to_terms,ids_to_father_ids)
+ with
+  e ->
+let metano =
+  match !ProofEngine.goal with
+     None -> assert false
+   | Some m -> m
+in
+let metasenv =
+ match !ProofEngine.proof with
+    None -> assert false
+  | Some (_,metasenv,_,_) -> metasenv
+in
+let currentsequent = List.find (function (m,_,_) -> m=metano) metasenv in
+prerr_endline ("Offending sequent: " ^ SequentPp.TextualPp.print_sequent currentsequent) ; flush stderr ;
+   raise (RefreshSequentException e)
+;;
+
 (*
 ignore(domImpl#saveDocumentToFile ~doc:sequent_doc
  ~name:"/public/sacerdot/guruguru1" ~indent:true ()) ;
-ignore(domImpl#saveDocumentToFile ~doc:sequent_mml
- ~name:"/public/sacerdot/guruguru2" ~indent:true ())
 *)
-;;
 
-let mml_of_cic_term term =
+let mml_of_cic_term metano term =
+ let metasenv =
+  match !ProofEngine.proof with
+     None -> []
+   | Some (_,metasenv,_,_) -> metasenv
+ in
  let context =
   match !ProofEngine.goal with
      None -> []
-   | Some (_,(context,_)) -> context
+   | Some metano ->
+      let (_,canonical_context,_) =
+       List.find (function (m,_,_) -> m=metano) metasenv
+      in
+       canonical_context
  in
-  let metasenv =
-   match !ProofEngine.proof with
-      None -> []
-    | Some (_,metasenv,_,_) -> metasenv
-  in
    let sequent_gdome,ids_to_terms,ids_to_father_ids =
-    SequentPp.XmlPp.print_sequent metasenv (context,term)
+    SequentPp.XmlPp.print_sequent metasenv (metano,context,term)
    in
     let sequent_doc =
      Xml2Gdome.document_of_xml domImpl sequent_gdome
     in
-     applyStylesheets sequent_doc sequent_styles sequent_args
-     (*CSC: magari prima o poi serve*)
-     (*current_scratch_infos := Some (ids_to_terms,ids_to_father_ids)*)
+     let res =
+      applyStylesheets sequent_doc sequent_styles sequent_args ;
+     in
+      current_scratch_infos := Some (term,ids_to_terms,ids_to_father_ids) ;
+      res
 ;;
 
 let output_html outputhtml msg =
@@ -253,11 +283,26 @@ let call_tactic tactic rendering_window () =
     refresh_sequent proofw ;
     refresh_proof output
    with
-    e ->
-     output_html outputhtml
-      ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
-     ProofEngine.proof := savedproof ;
-     ProofEngine.goal := savedgoal ;
+      RefreshSequentException e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+         "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
+       ProofEngine.proof := savedproof ;
+       ProofEngine.goal := savedgoal ;
+       refresh_sequent proofw
+    | RefreshProofException e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+         "proof: " ^ Printexc.to_string e ^ "</h1>") ;
+       ProofEngine.proof := savedproof ;
+       ProofEngine.goal := savedgoal ;
+       refresh_sequent proofw ;
+       refresh_proof output
+    | e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
+       ProofEngine.proof := savedproof ;
+       ProofEngine.goal := savedgoal ;
   end
 ;;
 
@@ -277,12 +322,23 @@ let call_tactic_with_input tactic rendering_window () =
        None -> assert false
      | Some (curi,_,_,_) -> curi
    in
+   let metasenv =
+    match !ProofEngine.proof with
+       None -> assert false
+     | Some (_,metasenv,_,_) -> metasenv
+   in
    let context =
     List.map
-     (function (_,n,_) -> n)
+     (function
+         Some (n,_) -> Some n
+       | None -> None)
      (match !ProofEngine.goal with
          None -> assert false
-       | Some (_,(ctx,_)) -> ctx
+       | Some metano ->
+          let (_,canonical_context,_) =
+           List.find (function (m,_,_) -> m=metano) metasenv
+          in
+           canonical_context
      )
    in
     try
@@ -299,11 +355,26 @@ let call_tactic_with_input tactic rendering_window () =
     with
        CicTextualParser0.Eof ->
         inputt#delete_text 0 inputlen
+     | RefreshSequentException e ->
+        output_html outputhtml
+         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+          "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
+        ProofEngine.proof := savedproof ;
+        ProofEngine.goal := savedgoal ;
+        refresh_sequent proofw
+     | RefreshProofException e ->
+        output_html outputhtml
+         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+          "proof: " ^ Printexc.to_string e ^ "</h1>") ;
+        ProofEngine.proof := savedproof ;
+        ProofEngine.goal := savedgoal ;
+        refresh_sequent proofw ;
+        refresh_proof output
      | e ->
         output_html outputhtml
-         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>");
+         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
         ProofEngine.proof := savedproof ;
-        ProofEngine.goal := savedgoal
+        ProofEngine.goal := savedgoal ;
 ;;
 
 let call_tactic_with_goal_input tactic rendering_window () =
@@ -333,9 +404,26 @@ let call_tactic_with_goal_input tactic rendering_window () =
                refresh_proof rendering_window#output
            | None -> assert false (* "ERROR: No current term!!!" *)
          with
-          e ->
-           output_html outputhtml
-            ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
+            RefreshSequentException e ->
+             output_html outputhtml
+              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+               "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
+             ProofEngine.proof := savedproof ;
+             ProofEngine.goal := savedgoal ;
+             refresh_sequent proofw
+          | RefreshProofException e ->
+             output_html outputhtml
+              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+               "proof: " ^ Printexc.to_string e ^ "</h1>") ;
+             ProofEngine.proof := savedproof ;
+             ProofEngine.goal := savedgoal ;
+             refresh_sequent proofw ;
+             refresh_proof output
+          | e ->
+             output_html outputhtml
+              ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
+             ProofEngine.proof := savedproof ;
+             ProofEngine.goal := savedgoal ;
         end
    | None ->
       output_html outputhtml
@@ -374,12 +462,23 @@ let call_tactic_with_input_and_goal_input tactic rendering_window () =
                     None -> assert false
                   | Some (curi,_,_,_) -> curi
                 in
+                let metasenv =
+                 match !ProofEngine.proof with
+                    None -> assert false
+                  | Some (_,metasenv,_,_) -> metasenv
+                in
                 let context =
                  List.map
-                  (function (_,n,_) -> n)
+                  (function
+                      Some (n,_) -> Some n
+                    | None -> None)
                   (match !ProofEngine.goal with
                       None -> assert false
-                    | Some (_,(ctx,_)) -> ctx
+                    | Some metano ->
+                       let (_,canonical_context,_) =
+                        List.find (function (m,_,_) -> m=metano) metasenv
+                       in
+                        canonical_context
                   )
                 in
                  begin
@@ -401,6 +500,60 @@ let call_tactic_with_input_and_goal_input tactic rendering_window () =
                       inputt#delete_text 0 inputlen
                  end
            | None -> assert false (* "ERROR: No current term!!!" *)
+         with
+            RefreshSequentException e ->
+             output_html outputhtml
+              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+               "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
+             ProofEngine.proof := savedproof ;
+             ProofEngine.goal := savedgoal ;
+             refresh_sequent proofw
+          | RefreshProofException e ->
+             output_html outputhtml
+              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+               "proof: " ^ Printexc.to_string e ^ "</h1>") ;
+             ProofEngine.proof := savedproof ;
+             ProofEngine.goal := savedgoal ;
+             refresh_sequent proofw ;
+             refresh_proof output
+          | e ->
+             output_html outputhtml
+              ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
+             ProofEngine.proof := savedproof ;
+             ProofEngine.goal := savedgoal ;
+        end
+   | None ->
+      output_html outputhtml
+       ("<h1 color=\"red\">No term selected</h1>")
+;;
+
+let call_tactic_with_goal_input_in_scratch tactic scratch_window () =
+ let module L = LogicalOperations in
+ let module G = Gdome in
+  let mmlwidget = (scratch_window#mmlwidget : GMathView.math_view) in
+  let outputhtml = (scratch_window#outputhtml : GHtml.xmhtml) in
+  let savedproof = !ProofEngine.proof in
+  let savedgoal  = !ProofEngine.goal in
+   match mmlwidget#get_selection with
+     Some node ->
+      let xpath =
+       ((node : Gdome.element)#getAttributeNS
+         ~namespaceURI:helmns
+         ~localName:(G.domString "xref"))#to_string
+      in
+       if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
+       else
+        begin
+         try
+          match !current_scratch_infos with
+             (* term is the whole goal in the scratch_area *)
+             Some (term,ids_to_terms, ids_to_father_ids) ->
+              let id = xpath in
+               let expr = tactic term (Hashtbl.find ids_to_terms id) in
+                let mml = mml_of_cic_term 111 expr in
+                 scratch_window#show () ;
+                 scratch_window#mmlwidget#load_tree ~dom:mml
+           | None -> assert false (* "ERROR: No current term!!!" *)
          with
           e ->
            output_html outputhtml
@@ -418,8 +571,8 @@ let exact rendering_window =
 let apply rendering_window =
  call_tactic_with_input ProofEngine.apply rendering_window
 ;;
-let elimintros rendering_window =
- call_tactic_with_input ProofEngine.elim_intros rendering_window
+let elimintrossimpl rendering_window =
+ call_tactic_with_input ProofEngine.elim_intros_simpl rendering_window
 ;;
 let whd rendering_window =
  call_tactic_with_goal_input ProofEngine.whd rendering_window
@@ -439,8 +592,24 @@ let cut rendering_window =
 let change rendering_window =
  call_tactic_with_input_and_goal_input ProofEngine.change rendering_window
 ;;
+let letin rendering_window =
+ call_tactic_with_input ProofEngine.letin rendering_window
+;;
 
 
+let whd_in_scratch scratch_window =
+ call_tactic_with_goal_input_in_scratch ProofEngine.whd_in_scratch
+  scratch_window
+;;
+let reduce_in_scratch scratch_window =
+ call_tactic_with_goal_input_in_scratch ProofEngine.reduce_in_scratch
+  scratch_window
+;;
+let simpl_in_scratch scratch_window =
+ call_tactic_with_goal_input_in_scratch ProofEngine.simpl_in_scratch
+  scratch_window
+;;
+
 
 
 (**********************)
@@ -450,11 +619,14 @@ let change rendering_window =
 exception OpenConjecturesStillThere;;
 exception WrongProof;;
 
-let save rendering_window () =
+let qed rendering_window () =
  match !ProofEngine.proof with
     None -> assert false
   | Some (uri,[],bo,ty) ->
-     if CicReduction.are_convertible (CicTypeChecker.type_of_aux' [] [] bo) ty then
+     if
+      CicReduction.are_convertible []
+       (CicTypeChecker.type_of_aux' [] [] bo) ty
+     then
       begin
        (*CSC: Wrong: [] is just plainly wrong *)
        let proof = Cic.Definition (UriManager.name_of_uri uri,bo,ty,[]) in
@@ -475,6 +647,71 @@ let save rendering_window () =
   | _ -> raise OpenConjecturesStillThere
 ;;
 
+(*????
+let dtdname = "http://www.cs.unibo.it/helm/dtd/cic.dtd";;
+*)
+let dtdname = "/projects/helm/V7/dtd/cic.dtd";;
+
+let save rendering_window () =
+ let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
+  match !ProofEngine.proof with
+     None -> assert false
+   | Some (uri, metasenv, bo, ty) ->
+      let currentproof =
+       Cic.CurrentProof (UriManager.name_of_uri uri,metasenv,bo,ty)
+      in
+       let (acurrentproof,_,_,ids_to_inner_sorts,_) =
+        Cic2acic.acic_object_of_cic_object currentproof
+       in
+        let xml = Cic2Xml.print_object uri ids_to_inner_sorts acurrentproof in
+         let xml' =
+          [< Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
+             Xml.xml_cdata
+              ("<!DOCTYPE CurrentProof SYSTEM \"" ^ dtdname ^ "\">\n\n") ;
+             xml
+          >]
+         in
+          Xml.pp ~quiet:true xml' (Some "/public/sacerdot/currentproof") ;
+          output_html outputhtml
+           ("<h1 color=\"Green\">Current proof saved to " ^
+             "/public/sacerdot/currentproof</h1>")
+;;
+
+let load rendering_window () =
+ let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
+ let output = (rendering_window#output : GMathView.math_view) in
+ let proofw = (rendering_window#proofw : GMathView.math_view) in
+  try
+   let uri = UriManager.uri_of_string "cic:/dummy.con" in
+    match CicParser.obj_of_xml "/public/sacerdot/currentproof" uri with
+       Cic.CurrentProof (_,metasenv,bo,ty) ->
+        ProofEngine.proof :=
+         Some (uri, metasenv, bo, ty) ;
+        ProofEngine.goal :=
+         (match metasenv with
+             [] -> None
+           | (metano,_,_)::_ -> Some metano
+         ) ;
+        refresh_proof output ;
+        refresh_sequent proofw ;
+         output_html outputhtml
+          ("<h1 color=\"Green\">Current proof loaded from " ^
+            "/public/sacerdot/currentproof</h1>")
+     | _ -> assert false
+  with
+     RefreshSequentException e ->
+      output_html outputhtml
+       ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+        "sequent: " ^ Printexc.to_string e ^ "</h1>")
+   | RefreshProofException e ->
+      output_html outputhtml
+       ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+        "proof: " ^ Printexc.to_string e ^ "</h1>")
+   | e ->
+      output_html outputhtml
+       ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
+;;
+
 let proveit rendering_window () =
  let module L = LogicalOperations in
  let module G = Gdome in
@@ -496,18 +733,118 @@ let proveit rendering_window () =
          match !current_cic_infos with
             Some (ids_to_terms, ids_to_father_ids) ->
              let id = xpath in
-              if L.to_sequent id ids_to_terms ids_to_father_ids then
-               refresh_proof rendering_window#output ;
+              L.to_sequent id ids_to_terms ids_to_father_ids ;
+              refresh_proof rendering_window#output ;
               refresh_sequent rendering_window#proofw
           | None -> assert false (* "ERROR: No current term!!!" *)
         with
-         e ->
-          output_html outputhtml
-           ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
+           RefreshSequentException e ->
+            output_html outputhtml
+             ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+              "sequent: " ^ Printexc.to_string e ^ "</h1>")
+         | RefreshProofException e ->
+            output_html outputhtml
+             ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+              "proof: " ^ Printexc.to_string e ^ "</h1>")
+         | e ->
+            output_html outputhtml
+             ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
        end
   | None -> assert false (* "ERROR: No selection!!!" *)
 ;;
 
+let focus rendering_window () =
+ let module L = LogicalOperations in
+ let module G = Gdome in
+ let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
+  match rendering_window#output#get_selection with
+    Some node ->
+     let xpath =
+      ((node : Gdome.element)#getAttributeNS
+      (*CSC: OCAML DIVERGE
+      ((element : G.element)#getAttributeNS
+      *)
+        ~namespaceURI:helmns
+        ~localName:(G.domString "xref"))#to_string
+     in
+      if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
+      else
+       begin
+        try
+         match !current_cic_infos with
+            Some (ids_to_terms, ids_to_father_ids) ->
+             let id = xpath in
+              L.focus id ids_to_terms ids_to_father_ids ;
+              refresh_sequent rendering_window#proofw
+          | None -> assert false (* "ERROR: No current term!!!" *)
+        with
+           RefreshSequentException e ->
+            output_html outputhtml
+             ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+              "sequent: " ^ Printexc.to_string e ^ "</h1>")
+         | RefreshProofException e ->
+            output_html outputhtml
+             ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+              "proof: " ^ Printexc.to_string e ^ "</h1>")
+         | e ->
+            output_html outputhtml
+             ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
+       end
+  | None -> assert false (* "ERROR: No selection!!!" *)
+;;
+
+exception NoPrevGoal;;
+exception NoNextGoal;;
+
+let prevgoal metasenv metano =
+ let rec aux =
+  function
+     [] -> assert false
+   | [(m,_,_)] -> raise NoPrevGoal
+   | (n,_,_)::(m,_,_)::_ when m=metano -> n
+   | _::tl -> aux tl
+ in
+  aux metasenv
+;;
+
+let nextgoal metasenv metano =
+ let rec aux =
+  function
+     [] -> assert false
+   | [(m,_,_)] when m = metano -> raise NoNextGoal
+   | (m,_,_)::(n,_,_)::_ when m=metano -> n
+   | _::tl -> aux tl
+ in
+  aux metasenv
+;;
+
+let prev_or_next_goal select_goal rendering_window () =
+ let module L = LogicalOperations in
+ let module G = Gdome in
+ let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
+  let metasenv =
+   match !ProofEngine.proof with
+      None -> assert false
+    | Some (_,metasenv,_,_) -> metasenv
+  in
+  let metano =
+   match !ProofEngine.goal with
+      None -> assert false
+    | Some m -> m
+  in
+   try
+    ProofEngine.goal := Some (select_goal metasenv metano) ;
+    refresh_sequent rendering_window#proofw
+   with
+      RefreshSequentException e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+         "sequent: " ^ Printexc.to_string e ^ "</h1>")
+    | e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
+;;
+
 exception NotADefinition;;
 
 let open_ rendering_window () =
@@ -532,14 +869,22 @@ let open_ rendering_window () =
       ProofEngine.proof :=
        Some (uri, metasenv, bo, ty) ;
       ProofEngine.goal := None ;
-      inputt#delete_text 0 inputlen ;
-      ignore(oldinputt#insert_text input oldinputt#length) ;
       refresh_sequent proofw ;
       refresh_proof output ;
+      inputt#delete_text 0 inputlen ;
+      ignore(oldinputt#insert_text input oldinputt#length)
    with
-    e ->
-     output_html outputhtml
-      ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
+      RefreshSequentException e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+         "sequent: " ^ Printexc.to_string e ^ "</h1>")
+    | RefreshProofException e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+         "proof: " ^ Printexc.to_string e ^ "</h1>")
+    | e ->
+       output_html outputhtml
+        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
 ;;
 
 let state rendering_window () =
@@ -558,23 +903,26 @@ let state rendering_window () =
       match CicTextualParser.main CicTextualLexer.token lexbuf with
          None -> ()
        | Some expr ->
-          try
-           let _  = CicTypeChecker.type_of_aux' [] [] expr in
-            ProofEngine.proof :=
-             Some (UriManager.uri_of_string "cic:/dummy.con",
-                    [1,expr], Cic.Meta 1, expr) ;
-            ProofEngine.goal := Some (1,([],expr)) ;
-            refresh_sequent proofw ;
-            refresh_proof output ;
-          with
-           e ->
-            print_endline ("? " ^ CicPp.ppterm expr) ;
-            raise e
+          let _  = CicTypeChecker.type_of_aux' [] [] expr in
+           ProofEngine.proof :=
+            Some (UriManager.uri_of_string "cic:/dummy.con",
+                   [1,[],expr], Cic.Meta (1,[]), expr) ;
+           ProofEngine.goal := Some 1 ;
+           refresh_sequent proofw ;
+           refresh_proof output ;
      done
     with
        CicTextualParser0.Eof ->
         inputt#delete_text 0 inputlen ;
         ignore(oldinputt#insert_text input oldinputt#length)
+     | RefreshSequentException e ->
+        output_html outputhtml
+         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+          "sequent: " ^ Printexc.to_string e ^ "</h1>")
+     | RefreshProofException e ->
+        output_html outputhtml
+         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
+          "proof: " ^ Printexc.to_string e ^ "</h1>")
      | e ->
         output_html outputhtml
          ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
@@ -592,14 +940,22 @@ let check rendering_window scratch_window () =
       None -> UriManager.uri_of_string "cic:/dummy.con", []
     | Some (curi,metasenv,_,_) -> curi,metasenv
   in
-  let ciccontext,names_context =
+  let context,names_context =
    let context =
     match !ProofEngine.goal with
        None -> []
-     | Some (_,(ctx,_)) -> ctx
+     | Some metano ->
+        let (_,canonical_context,_) =
+         List.find (function (m,_,_) -> m=metano) metasenv
+        in
+         canonical_context
    in
-    ProofEngine.cic_context_of_context context,
-     List.map (function (_,n,_) -> n) context
+    context,
+    List.map
+     (function
+         Some (n,_) -> Some n
+       | None -> None
+     ) context
   in
    (* Do something interesting *)
    let lexbuf = Lexing.from_string input in
@@ -613,23 +969,61 @@ let check rendering_window scratch_window () =
          None -> ()
        | Some expr ->
           try
-           let ty  = CicTypeChecker.type_of_aux' metasenv ciccontext expr in
-            let mml = mml_of_cic_term ty in
+           let ty  = CicTypeChecker.type_of_aux' metasenv context expr in
+            let mml = mml_of_cic_term 111 (Cic.Cast (expr,ty)) in
              scratch_window#show () ;
-             scratch_window#display ~dom:mml
+             scratch_window#mmlwidget#load_tree ~dom:mml
           with
            e ->
             print_endline ("? " ^ CicPp.ppterm expr) ;
             raise e
      done
     with
-       CicTextualParser0.Eof ->
-        inputt#delete_text 0 inputlen
+       CicTextualParser0.Eof -> ()
      | e ->
        output_html outputhtml
         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
 ;;
 
+let locate rendering_window () =
+ let inputt = (rendering_window#inputt : GEdit.text) in
+ let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
+  let inputlen = inputt#length in
+  let input = inputt#get_chars 0 inputlen in
+  output_html outputhtml (
+     try
+        match Str.split (Str.regexp "[ \t]+") input with
+           | [] -> ""
+           | head :: tail ->
+              inputt#delete_text 0 inputlen;
+              Mquery.locate head 
+     with
+        e -> "<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>"
+  )
+;;
+
+let backward rendering_window () =
+   let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
+   let inputt = (rendering_window#inputt : GEdit.text) in
+    let inputlen = inputt#length in
+    let input = inputt#get_chars 0 inputlen in
+    let level = int_of_string input in
+    let metasenv =
+     match !ProofEngine.proof with
+        None -> assert false
+      | Some (_,metasenv,_,_) -> metasenv
+    in
+    let result =
+       match !ProofEngine.goal with
+          | None -> ""
+          | Some metano ->
+             let (_,_,ty) =
+              List.find (function (m,_,_) -> m=metano) metasenv
+             in
+              Mquery.backward ty level
+       in 
+   output_html outputhtml result
+      
 let choose_selection
      (mmlwidget : GMathView.math_view) (element : Gdome.element option)
 =
@@ -777,16 +1171,38 @@ end;;
 
 (* Scratch window *)
 
-class scratch_window () =
+class scratch_window outputhtml =
  let window =
   GWindow.window ~title:"MathML viewer" ~border_width:2 () in
+ let vbox =
+  GPack.vbox ~packing:window#add () in
+ let hbox =
+  GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
+ let whdb =
+  GButton.button ~label:"Whd"
+   ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
+ let reduceb =
+  GButton.button ~label:"Reduce"
+   ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
+ let simplb =
+  GButton.button ~label:"Simpl"
+   ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
+ let scrolled_window =
+  GBin.scrolled_window ~border_width:10
+   ~packing:(vbox#pack ~expand:true ~padding:5) () in
  let mmlwidget =
-  GMathView.math_view ~packing:(window#add) ~width:400 ~height:280 () in
+  GMathView.math_view
+   ~packing:(scrolled_window#add) ~width:400 ~height:280 () in
 object(self)
- method display = mmlwidget#load_tree
- method show = window#show
+ method outputhtml = outputhtml
+ method mmlwidget = mmlwidget
+ method show () = window#misc#hide () ; window#show ()
  initializer
-  ignore(window#event#connect#delete (fun _ -> window#misc#hide () ; true ))
+  ignore(mmlwidget#connect#selection_changed (choose_selection mmlwidget)) ;
+  ignore(window#event#connect#delete (fun _ -> window#misc#hide () ; true )) ;
+  ignore(whdb#connect#clicked (whd_in_scratch self)) ;
+  ignore(reduceb#connect#clicked (reduce_in_scratch self)) ;
+  ignore(simplb#connect#clicked (simpl_in_scratch self))
 end;;
 
 (* Main window *)
@@ -811,9 +1227,15 @@ class rendering_window output proofw (label : GMisc.label) =
  let button_export_to_postscript =
   GButton.button ~label:"export_to_postscript"
   ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
+ let qedb =
+  GButton.button ~label:"Qed"
+   ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
  let saveb =
   GButton.button ~label:"Save"
    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
+ let loadb =
+  GButton.button ~label:"Load"
+   ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
  let closeb =
   GButton.button ~label:"Close"
    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
@@ -822,6 +1244,15 @@ class rendering_window output proofw (label : GMisc.label) =
  let proveitb =
   GButton.button ~label:"Prove It"
    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
+ let focusb =
+  GButton.button ~label:"Focus"
+   ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
+ let prevgoalb =
+  GButton.button ~label:"<"
+   ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
+ let nextgoalb =
+  GButton.button ~label:">"
+   ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
  let oldinputt = GEdit.text ~editable:false ~width:400 ~height:180
    ~packing:(vbox#pack ~padding:5) () in
  let hbox4 =
@@ -835,6 +1266,12 @@ class rendering_window output proofw (label : GMisc.label) =
  let checkb =
   GButton.button ~label:"Check"
    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
+ let locateb =
+  GButton.button ~label:"Locate"
+   ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
+ let backwardb =
+  GButton.button ~label:"Backward"
+   ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
  let inputt = GEdit.text ~editable:true ~width:400 ~height: 100
    ~packing:(vbox#pack ~padding:5) () in
  let vbox1 =
@@ -854,8 +1291,8 @@ class rendering_window output proofw (label : GMisc.label) =
  let applyb =
   GButton.button ~label:"Apply"
    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
- let elimintrosb =
-  GButton.button ~label:"ElimIntros"
+ let elimintrossimplb =
+  GButton.button ~label:"ElimIntrosSimpl"
    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
  let whdb =
   GButton.button ~label:"Whd"
@@ -875,20 +1312,23 @@ class rendering_window output proofw (label : GMisc.label) =
  let changeb =
   GButton.button ~label:"Change"
    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
+ let letinb =
+  GButton.button ~label:"Let ... In"
+   ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
  let outputhtml =
   GHtml.xmhtml
    ~source:"<html><body bgColor=\"white\"></body></html>"
    ~width:400 ~height: 200
    ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5)
    ~show:true () in
- let scratch_window = new scratch_window () in
+ let scratch_window = new scratch_window outputhtml in
 object(self)
  method outputhtml = outputhtml
  method oldinputt = oldinputt
  method inputt = inputt
  method output = (output : GMathView.math_view)
  method proofw = (proofw : GMathView.math_view)
- method show () = window#show ()
+ method show = window#show
  initializer
   button_export_to_postscript#misc#set_sensitive false ;
 
@@ -901,21 +1341,29 @@ object(self)
    button_export_to_postscript (choose_selection output) in
   ignore(settingsb#connect#clicked settings_window#show) ;
   ignore(button_export_to_postscript#connect#clicked (export_to_postscript output)) ;
+  ignore(qedb#connect#clicked (qed self)) ;
   ignore(saveb#connect#clicked (save self)) ;
+  ignore(loadb#connect#clicked (load self)) ;
   ignore(proveitb#connect#clicked (proveit self)) ;
+  ignore(focusb#connect#clicked (focus self)) ;
+  ignore(prevgoalb#connect#clicked (prev_or_next_goal prevgoal self)) ;
+  ignore(nextgoalb#connect#clicked (prev_or_next_goal nextgoal self)) ;
   ignore(window#event#connect#delete (fun _ -> GMain.Main.quit () ; true )) ;
   ignore(stateb#connect#clicked (state self)) ;
   ignore(openb#connect#clicked (open_ self)) ;
   ignore(checkb#connect#clicked (check self scratch_window)) ;
+  ignore(locateb#connect#clicked (locate self)) ;
+  ignore(backwardb#connect#clicked (backward self)) ;
   ignore(exactb#connect#clicked (exact self)) ;
   ignore(applyb#connect#clicked (apply self)) ;
-  ignore(elimintrosb#connect#clicked (elimintros self)) ;
+  ignore(elimintrossimplb#connect#clicked (elimintrossimpl self)) ;
   ignore(whdb#connect#clicked (whd self)) ;
   ignore(reduceb#connect#clicked (reduce self)) ;
   ignore(simplb#connect#clicked (simpl self)) ;
   ignore(foldb#connect#clicked (fold self)) ;
   ignore(cutb#connect#clicked (cut self)) ;
   ignore(changeb#connect#clicked (change self)) ;
+  ignore(letinb#connect#clicked (letin self)) ;
   ignore(introsb#connect#clicked (intros self)) ;
   Logger.log_callback :=
    (Logger.log_to_html ~print_and_flush:(output_html outputhtml))
@@ -937,6 +1385,8 @@ let initialize_everything () =
 
 let _ =
  CicCooking.init () ;
+ Mquery.init () ;
  ignore (GtkMain.Main.init ()) ;
- initialize_everything ()
+ initialize_everything () ;
+ Mquery.close ()
 ;;