X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fmatita%2FmatitaInterpreter.ml;h=511a5931476f30fe354246257fb7bf4665263fb8;hb=ac813b7e251e4bac1a8a16befa628203775771ca;hp=f99e11854738b207ed4d963766622201939f7050;hpb=aaa11ef4a3958fb980f15f8104d22b9d5fea43a9;p=helm.git diff --git a/helm/matita/matitaInterpreter.ml b/helm/matita/matitaInterpreter.ml index f99e11854..511a59314 100644 --- a/helm/matita/matitaInterpreter.ml +++ b/helm/matita/matitaInterpreter.ml @@ -26,9 +26,9 @@ (** Interpreter for textual phrases coming from matita's console (textual entry * window at the bottom of the main window). * -* Interpreter is either in `Command state or in `Proof state (see state_tag type -* below). In `Command state commands for starting proofs are accepted, but -* tactic and tactical applications are not. In `Proof state both +* Interpreter is either in Command state or in Proof state (see state type +* below). In Command state commands for starting proofs are accepted, but +* tactic and tactical applications are not. In Proof state both * tactic/tacticals and commands are accepted. *) @@ -36,8 +36,8 @@ open Printf open MatitaTypes - (** None means: "same state as before" *) -type state_tag = [ `Command | `Proof ] option +type state = Command | Proof +type outcome = New_state of state | Quiet | Echo of string exception Command_error of string @@ -53,6 +53,10 @@ let qualify name = baseuri ^ name else String.concat "/" [baseuri; name] +let split_obj = function + | Cic.Constant (name, body, ty, _) + | Cic.Variable (name, body, ty, _) -> (name, body, ty) + | _ -> assert false let canonical_context metano metasenv = try @@ -61,9 +65,9 @@ let canonical_context metano metasenv = 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 get_context_and_metasenv (currentProof:MatitaTypes.currentProof) = + if currentProof#onGoing () then + let proof = currentProof#proof in let metasenv = proof#metasenv in let goal = proof#goal in (canonical_context goal metasenv, metasenv) @@ -72,9 +76,9 @@ let get_context_and_metasenv (proof_handler:MatitaTypes.proof_handler) = (** 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 disambiguate ~(disambiguator:MatitaTypes.disambiguator) ~currentProof ast = + if currentProof#onGoing () then begin + let proof = currentProof#proof in let metasenv = proof#metasenv in let goal = proof#goal in let context = canonical_context goal metasenv in @@ -96,7 +100,7 @@ class virtual interpreterState = (** eval a toplevel phrase in the current state and return the new state *) method parsePhrase s = - match CicTextualParser2.parse_tactical (Stream.of_string s) with + match CicTextualParser2.parse_tactical s with | (TacticAst.LocatedTactical (loc', tac)) as tactical -> loc := Some loc'; (match tac with (* update interpreter history *) @@ -111,11 +115,11 @@ class virtual interpreterState = | _ -> assert false method virtual evalTactical: - (CicAst.term, string) TacticAst.tactical -> state_tag + (CicAst.term, string) TacticAst.tactical -> outcome method evalPhrase s = debug_print (sprintf "evaluating '%s'" s); - self#evalTactical (self#parsePhrase s) + self#evalTactical (self#parsePhrase (Stream.of_string s)) method evalAst ast = self#evalTactical ast @@ -126,16 +130,12 @@ class virtual interpreterState = end -let check_widget: MatitaTypes.sequent_viewer lazy_t = lazy - (let gui = MatitaGui.instance () in - MatitaMathView.sequent_viewer ~show:true ~packing:gui#check#scrolledCheck#add - ()) - (** Implements phrases that should be accepted in all states *) class sharedState ~(disambiguator: MatitaTypes.disambiguator) - ~(proof_handler: MatitaTypes.proof_handler) + ~(currentProof: MatitaTypes.currentProof) ~(console: MatitaTypes.console) + ?(mathViewer: MatitaTypes.mathViewer option) ~(dbd: Mysql.dbd) () = @@ -143,23 +143,23 @@ class sharedState inherit interpreterState ~console method evalTactical = function | TacticAst.Command TacticAst.Quit -> - proof_handler.MatitaTypes.quit (); - Some `Command (* dummy answer, useless *) + currentProof#quit (); + assert false (* dummy answer, useless *) | TacticAst.Command TacticAst.Proof -> (* do nothing, just for compatibility with coq syntax *) - Some `Command + New_state Command | TacticAst.Command (TacticAst.Baseuri (Some uri)) -> baseuri := uri; console#echo_message (sprintf "base uri set to \"%s\"" uri); - None + Quiet | TacticAst.Command (TacticAst.Baseuri None) -> console#echo_message (sprintf "base uri is \"%s\"" !baseuri); - None + Quiet | TacticAst.Command (TacticAst.Check term) -> let (_, _, term,ugraph) = - disambiguate ~disambiguator ~proof_handler term + disambiguate ~disambiguator ~currentProof term in - let (context, metasenv) = get_context_and_metasenv proof_handler in + let (context, metasenv) = get_context_and_metasenv currentProof in let dummyno = CicMkImplicit.new_meta metasenv [] in let ty,ugraph1 = CicTypeChecker.type_of_aux' metasenv context term ugraph @@ -167,12 +167,10 @@ class sharedState (* TASSI: here ugraph1 is unused.... FIXME *) let expr = Cic.Cast (term, ty) in let sequent = (dummyno, context, expr) in - let widget = Lazy.force check_widget in - let gui = MatitaGui.instance () in - gui#check#checkWin#show (); - gui#main#showCheckMenuItem#set_active true; - widget#load_sequent (sequent::metasenv) dummyno; - None + (match mathViewer with + | None -> () + | Some v -> v#checkTerm sequent metasenv); + Quiet | TacticAst.Command (TacticAst.Search_pat (search_kind, pat)) -> let uris = match search_kind with @@ -181,7 +179,7 @@ class sharedState | _ -> assert false in (* TODO ZACK: show URIs to the user *) - None + Quiet | tactical -> raise (Command_error (TacticAstPp.pp_tactical tactical)) end @@ -262,15 +260,30 @@ let inddef_of_ast params indTypes (disambiguator:MatitaTypes.disambiguator) = *) (UriManager.uri_of_string uri, (cicIndTypes, [], paramsno)) - (** Implements phrases that should be accepted only in `Command state *) + (* 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 + + (** Implements phrases that should be accepted only in Command state *) class commandState ~(disambiguator: MatitaTypes.disambiguator) - ~(proof_handler: MatitaTypes.proof_handler) + ~(currentProof: MatitaTypes.currentProof) ~(console: MatitaTypes.console) + ?mathViewer ~(dbd: Mysql.dbd) () = - let shared = new sharedState ~disambiguator ~proof_handler ~console ~dbd () in + let shared = + new sharedState ~disambiguator ~currentProof ~console ?mathViewer ~dbd () + in object (self) inherit interpreterState ~console @@ -282,8 +295,8 @@ class commandState in let uri = UriManager.uri_of_string (qualify name ^ ".con") in let proof = MatitaProof.proof ~typ:expr ~uri ~metasenv () in - proof_handler.MatitaTypes.new_proof proof; - Some `Proof + currentProof#start proof; + New_state Proof | TacticAst.Command (TacticAst.Theorem (_, Some name, type_ast, Some body_ast)) -> let (_, metasenv, type_cic, ugraph) = @@ -299,18 +312,10 @@ class commandState let (subst, metasenv, ugraph) = CicUnification.fo_unif metasenv [] body_type type_cic ugraph in - let body_cic = CicMetaSubst.apply_subst subst body_cic in - let type_cic = CicMetaSubst.apply_subst subst type_cic in - let obj = - Cic.Constant - ((UriManager.name_of_uri uri), (Some body_cic),type_cic,[]) - 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:(Some body_cic) ~ty:type_cic; - None + 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 (); + Quiet | TacticAst.Command (TacticAst.Inductive (params, indTypes)) -> let (uri, (indTypes, params, leftno)) = inddef_of_ast params indTypes disambiguator @@ -324,13 +329,26 @@ class commandState CicEnvironment.put_inductive_definition uri (obj, ugraph); MetadataDb.index_inductive_def ~dbd ~owner:(Helm_registry.get "matita.owner") ~uri ~types:indTypes; - None + 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)) | TacticAst.Command TacticAst.Quit -> - proof_handler.MatitaTypes.quit (); - Some `Command (* dummy answer, useless *) + currentProof#quit (); + New_state Command (* dummy answer, useless *) | TacticAst.Command TacticAst.Proof -> (* do nothing, just for compatibility with coq syntax *) - Some `Command + New_state Command | tactical -> shared#evalTactical tactical end @@ -348,17 +366,18 @@ let namer_of names = end else FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context name ~typ - (** Implements phrases that should be accepted only in `Proof state, basically + (** Implements phrases that should be accepted only in Proof state, basically * tacticals *) class proofState ~(disambiguator: MatitaTypes.disambiguator) - ~(proof_handler: MatitaTypes.proof_handler) + ~(currentProof: MatitaTypes.currentProof) ~(console: MatitaTypes.console) + ?mathViewer ~(dbd: Mysql.dbd) () = let disambiguate ast = - let (_, _, term, _) = disambiguate ~disambiguator ~proof_handler ast in + let (_, _, term, _) = disambiguate ~disambiguator ~currentProof ast in term in (** tactic AST -> ProofEngineTypes.tactic *) @@ -404,28 +423,28 @@ class proofState | _ -> MatitaTypes.not_implemented "some tactic" in - let shared = new sharedState ~disambiguator ~proof_handler ~console ~dbd () in + let shared = new sharedState ~disambiguator ~currentProof ~console ~dbd () in object (self) inherit interpreterState ~console method evalTactical = function | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical | TacticAst.Command TacticAst.Abort -> - proof_handler.MatitaTypes.abort_proof (); - Some `Command + currentProof#abort (); + New_state Command | TacticAst.Command (TacticAst.Undo steps) -> - (proof_handler.MatitaTypes.get_proof ())#undo ?steps (); - Some `Proof + currentProof#proof#undo ?steps (); + New_state Proof | TacticAst.Command (TacticAst.Redo steps) -> - (proof_handler.MatitaTypes.get_proof ())#redo ?steps (); - Some `Proof + currentProof#proof#redo ?steps (); + New_state Proof | 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 + if not (currentProof#onGoing ()) then assert false; + let proof = currentProof#proof in let (uri, metasenv, bo, ty) = proof#proof in let uri = MatitaTypes.unopt_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"; let proved_ty,ugraph = CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph @@ -433,46 +452,35 @@ class proofState let b,ugraph = CicReduction.are_convertible [] proved_ty ty ugraph in - if not b then - failwith "Wrong proof"; - (* TODO Zack [] probably wrong *) - let obj = - Cic.Constant ((UriManager.name_of_uri uri), (Some bo),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:(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 - * - register uri to the getter - * - save universe file *) - Some `Command + if not b then failwith "Wrong proof"; + add_constant_to_world ~dbd ~uri ~body:bo ~ty ~ugraph (); + currentProof#abort (); + (match mathViewer with None -> () | Some v -> v#unload ()); + New_state Command | TacticAst.Seq tacticals -> (* TODO Zack check for proof completed at each step? *) List.iter (fun t -> ignore (self#evalTactical t)) tacticals; - Some `Proof + New_state Proof | TacticAst.Tactic tactic_phrase -> let tactic = lookup_tactic tactic_phrase in - (proof_handler.MatitaTypes.get_proof ())#apply_tactic tactic; - Some `Proof + currentProof#proof#apply_tactic tactic; + New_state Proof | tactical -> shared#evalTactical tactical end class interpreter ~(disambiguator: MatitaTypes.disambiguator) - ~(proof_handler: MatitaTypes.proof_handler) + ~(currentProof: MatitaTypes.currentProof) ~(console: MatitaTypes.console) + ?mathViewer ~(dbd: Mysql.dbd) () = let commandState = - new commandState ~disambiguator ~proof_handler ~console ~dbd () + new commandState ~disambiguator ~currentProof ~console ?mathViewer ~dbd () in let proofState = - new proofState ~disambiguator ~proof_handler ~console ~dbd () + new proofState ~disambiguator ~currentProof ~console ?mathViewer ~dbd () in object (self) val mutable state = commandState @@ -482,22 +490,20 @@ class interpreter method endOffset = state#endOffset method private updateState = function - | Some `Command -> state <- commandState - | Some `Proof -> state <- proofState - | None -> () - - method evalPhrase s = - let success = - console#wrap_exn (fun () -> self#updateState (state#evalPhrase s)) - in - if success then console#clear (); - success - - method evalAst ast = - let success = - console#wrap_exn (fun () -> self#updateState (state#evalAst ast)) - in - if success then console#clear (); - success + | New_state Command -> (state <- commandState) + | New_state Proof -> (state <- proofState) + | _ -> () + + method private eval f = + 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 () + | Some (Echo msg) -> console#echo_message msg; (true, false) + | Some Quiet -> ok () + | None -> (false, false) + + method evalPhrase s = self#eval (fun () -> state#evalPhrase s) + method evalAst ast = self#eval (fun () -> state#evalAst ast) end