(* Copyright (C) 2004, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://helm.cs.unibo.it/ *) open Printf type state_tag = [ `Command | `Proof ] exception Command_not_found of string class virtual interpreterState ~(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 virtual evalTactical: (CicAst.term, string) TacticAst.tactical -> state_tag method evalPhrase s = self#evalTactical (self#parsePhrase s) end class commandState ~(disambiguator: MatitaTypes.disambiguator) ~(proof_handler: MatitaTypes.proof_handler) ~(console: MatitaConsole.console) () = object (self) inherit interpreterState ~console method evalTactical = function (* | TacticAst.Command _ -> failwith "1" | TacticAst.Tactic _ -> failwith "2" | TacticAst.LocatedTactical _ -> failwith "3" | TacticAst.Fail -> failwith "4" | TacticAst.Do (_, _) -> failwith "5" | TacticAst.IdTac -> failwith "6" | TacticAst.Repeat _ -> failwith "7" | TacticAst.Seq _ -> failwith "8" | TacticAst.Then (_, _) -> failwith "9" | TacticAst.Tries _ -> failwith "10" | TacticAst.Try _ -> failwith "11" *) | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical | TacticAst.Command (TacticAst.Theorem (_, Some name, ast, None)) -> let (_, metasenv, expr) = disambiguator#disambiguateTermAst ast in let _ = CicTypeChecker.type_of_aux' metasenv [] expr in let proof = MatitaProof.proof ~typ:expr ~metasenv () in proof_handler.MatitaTypes.new_proof proof; `Proof | TacticAst.Command TacticAst.Quit -> proof_handler.MatitaTypes.quit (); `Command (* dummy answer, useless *) | TacticAst.Command TacticAst.Proof -> (* do nothing, just for compatibility with coq syntax *) `Command | tactical -> raise (Command_not_found (TacticAstPp.pp_tactical tactical)) end class proofState ~(disambiguator: MatitaTypes.disambiguator) ~(proof_handler: MatitaTypes.proof_handler) ~(console: MatitaConsole.console) () = let commandState = new commandState ~disambiguator ~proof_handler ~console () in object inherit interpreterState ~console method evalTactical = function | TacticAst.Command TacticAst.Abort -> proof_handler.MatitaTypes.set_proof None; `Command | tactical -> (* fallback on command state *) commandState#evalTactical tactical end class interpreter ~(disambiguator: MatitaTypes.disambiguator) ~(proof_handler: MatitaTypes.proof_handler) ~(console: MatitaConsole.console) () = let commandState = new commandState ~disambiguator ~proof_handler ~console () in let proofState = new proofState ~disambiguator ~proof_handler ~console () in object val mutable state = commandState method evalPhrase s = try (match state#evalPhrase s with | `Command -> state <- commandState | `Proof -> state <- proofState) with exn -> console#echo_error (sprintf "Uncaught exception: %s" (Printexc.to_string exn)) end