(** 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.
*)
open Printf
- (** None means: "same state as before" *)
-type state_tag = [ `Command | `Proof ] option
+open MatitaTypes
+
+type state = Command | Proof
+type outcome = New_state of state | Quiet | Echo of string
exception Command_error of string
+(*
let uri name =
UriManager.uri_of_string (sprintf "%s/%s" BuildTimeConf.base_uri name)
+*)
-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)
+let baseuri = lazy (ref ("cic:/matita/" ^ Helm_registry.get "matita.owner"))
+let basedir = lazy (ref (Helm_registry.get "matita.basedir"))
+
+let qualify name =
+ let baseuri = !(Lazy.force baseuri) in
+ if baseuri.[String.length baseuri - 1] = '/' then
+ baseuri ^ name
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
+ String.concat "/" [baseuri; name]
+let split_obj = function
+ | Cic.Constant (name, body, ty, _, attrs)
+ | Cic.Variable (name, body, ty, _, attrs) -> (name, body, ty, attrs)
+ | _ -> assert false
-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: #MatitaTypes.console) ->
object (self)
+
+ val dbd = MatitaMisc.dbd_instance ()
+ val currentProof = MatitaProof.instance ()
+ val disambiguator = MatitaDisambiguator.instance ()
+
(** 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 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
+ (CicAst.term, string) TacticAst.tactical -> outcome
- method evalPhrase s = self#evalTactical (self#parsePhrase s)
+ method evalPhrase s =
+ debug_print (sprintf "evaluating '%s'" s);
+ self#evalTactical (self#parsePhrase (Stream.of_string s))
- end
+ method evalAst ast = self#evalTactical ast
-let check_widget: MatitaTypes.sequent_viewer lazy_t = lazy
- (let gui = MatitaGui.instance () in
- MatitaMathView.sequent_viewer ~show:true ~packing:gui#check#scrolledCheck#add
- ())
+ method endOffset =
+ match !loc with
+ | Some (start_pos, end_pos) -> end_pos.Lexing.pos_cnum
+ | None -> failwith "MatitaInterpreter: no offset recorded"
+
+ end
(** Implements phrases that should be accepted in all states *)
class sharedState
- ~(disambiguator: MatitaTypes.disambiguator)
- ~(proof_handler: MatitaTypes.proof_handler)
- ~(console: MatitaConsole.console)
+ ~(console: #MatitaTypes.console)
+ ?(mathViewer: MatitaTypes.mathViewer option)
()
=
object (self)
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)) ->
+ Lazy.force baseuri := uri;
+ console#echo_message (sprintf "base uri set to \"%s\"" uri);
+ Quiet
+ | TacticAst.Command (TacticAst.Baseuri None) ->
+ console#echo_message (sprintf "base uri is \"%s\""
+ !(Lazy.force baseuri));
+ Quiet
+ | TacticAst.Command (TacticAst.Basedir (Some path)) ->
+ Lazy.force basedir := path;
+ console#echo_message (sprintf "base dir set to \"%s\"" path);
+ Quiet
+ | TacticAst.Command (TacticAst.Basedir None) ->
+ console#echo_message (sprintf "base dir is \"%s\""
+ !(Lazy.force basedir));
+ Quiet
| TacticAst.Command (TacticAst.Check term) ->
- let (_, _, term) = disambiguate ~disambiguator ~proof_handler term in
- let (context, metasenv) = get_context_and_metasenv proof_handler in
+ let (_, _, term,ugraph) =
+ MatitaCicMisc.disambiguate ~disambiguator ~currentProof term
+ in
+ let (context, metasenv) =
+ MatitaCicMisc.get_context_and_metasenv currentProof
+ in
+(* this is the Eval Compute
+ let term = CicReduction.whd context term in
+*)
let dummyno = CicMkImplicit.new_meta metasenv [] in
- let ty = CicTypeChecker.type_of_aux' metasenv context term in
+ let ty,ugraph1 =
+ CicTypeChecker.type_of_aux' metasenv context term ugraph
+ in
+ (* 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
+ | Some v -> v#checkTerm (`Cic expr)
+ | _ -> ());
+ Quiet
+ | TacticAst.Command (TacticAst.Search_pat (search_kind, pat)) ->
+ let uris =
+ match search_kind with
+ | `Locate -> MetadataQuery.locate ~dbd pat
+ | `Elim -> MetadataQuery.elim ~dbd pat
+ | _ -> assert false
+ in
+ (* TODO ZACK: show URIs to the user *)
+ Quiet
+ | TacticAst.Command (TacticAst.Print `Env) ->
+ let uris = CicEnvironment.list_uri () in
+ console#echo_message "Environment:";
+ List.iter (fun u ->
+ console#echo_message (" " ^ (UriManager.string_of_uri u))
+ ) uris;
+ Quiet
+ | TacticAst.Command (TacticAst.Print `Coer) ->
+ let uris = CoercGraph.get_coercions_list () in
+ console#echo_message "Coercions:";
+ List.iter (fun (s,t,u) ->
+ console#echo_message (" " ^ (UriManager.string_of_uri u))
+ ) uris;
+ Quiet
| tactical ->
raise (Command_error (TacticAstPp.pp_tactical tactical))
end
- (** Implements phrases that should be accepted only in `Command state *)
-class commandState
- ~(disambiguator: MatitaTypes.disambiguator)
- ~(proof_handler: MatitaTypes.proof_handler)
- ~(console: MatitaConsole.console)
- ()
+open Printf
+
+let pp_indtypes indTypes =
+ List.iter
+ (fun (name, _, typ, constructors) ->
+ printf "%s: %s\n" name (CicPp.ppterm typ);
+ List.iter
+ (fun (name, term) -> printf "\t%s: %s\n" name (CicPp.ppterm term))
+ constructors)
+ indTypes;
+ flush stdout
+
+let inddef_of_ast params indTypes (disambiguator:MatitaTypes.disambiguator) =
+ let add_pi binders t =
+ List.fold_right
+ (fun (name, ast) acc ->
+ CicAst.Binder (`Forall, (Cic.Name name, Some ast), acc))
+ binders t
+ in
+ let ind_binders =
+ List.map (fun (name, _, typ, _) -> (name, add_pi params typ)) indTypes
+ in
+ let binders = ind_binders @ params in
+ let asts = ref [] in
+ let add_ast ast = asts := ast :: !asts in
+ let paramsno = List.length params in
+ let indbindersno = List.length ind_binders in
+ List.iter
+ (fun (name, _, typ, constructors) ->
+ add_ast (add_pi params typ);
+ List.iter (fun (_, ast) -> add_ast (add_pi binders ast)) constructors)
+ indTypes;
+ let (_, metasenv, terms, ugraph) =
+ disambiguator#disambiguateTermAsts ~metasenv:[] !asts
+ in
+ let terms = ref (List.rev terms) in
+ let get_term () =
+ match !terms with [] -> assert false | hd :: tl -> terms := tl; hd
+ in
+ let uri =
+ match indTypes with
+ | (name, _, _, _) :: _ -> qualify name ^ ".ind"
+ | _ -> assert false
+ in
+ let mutinds =
+ let counter = ref 0 in
+ List.map
+ (fun _ ->
+ incr counter;
+ CicUtil.term_of_uri (sprintf "%s#xpointer(1/%d)" uri !counter))
+ indTypes
+ in
+ let subst_mutinds = List.fold_right CicSubstitution.subst mutinds in
+ let cicIndTypes =
+ List.fold_left
+ (fun acc (name, inductive, typ, constructors) ->
+ let cicTyp = get_term () in
+ let cicConstructors =
+ List.fold_left
+ (fun acc (name, _) ->
+ let typ =
+ subst_mutinds (CicUtil.strip_prods indbindersno (get_term ()))
+ in
+ (name, typ) :: acc)
+ [] constructors
+ in
+ (name, inductive, cicTyp, List.rev cicConstructors) :: acc)
+ [] indTypes
+ in
+ let cicIndTypes = List.rev cicIndTypes in
+ (UriManager.uri_of_string uri, (cicIndTypes, [], paramsno))
+
+let save_object_to_disk uri obj =
+ let ensure_path_exists path =
+ let dir = Filename.dirname path in
+ try
+ let stats = Unix.stat dir in
+ if stats.Unix.st_kind <> Unix.S_DIR then
+ raise (Failure (dir ^ " already exists and is not a directory"))
+ else
+ ()
+ with
+ Unix.Unix_error (_,_,_) ->
+ let pstatus = Unix.system ("mkdir -p " ^ dir) in
+ match pstatus with
+ | Unix.WEXITED n when n = 0 -> ()
+ | _ -> raise (Failure ("Unable to create " ^ dir))
+ in
+ (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
+ let annobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
+ Cic2acic.acic_object_of_cic_object ~eta_fix:false obj
+ in
+ (* prepare XML *)
+ let xml, bodyxml =
+ Cic2Xml.print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter:false
+ annobj
+ in
+ let xmlinnertypes =
+ Cic2Xml.print_inner_types uri ~ids_to_inner_sorts ~ids_to_inner_types
+ ~ask_dtd_to_the_getter:false
+ in
+ (* prepare URIs and paths *)
+ let innertypesuri = UriManager.innertypesuri_of_uri uri in
+ let bodyuri = UriManager.bodyuri_of_uri uri in
+ let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
+ (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
+ let innertypespath = !(Lazy.force basedir) ^ "/" ^ innertypesfilename in
+ let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
+ (UriManager.string_of_uri uri) ^ ".xml.gz" in
+ let xmlpath = !(Lazy.force basedir) ^ "/" ^ xmlfilename in
+ let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
+ (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
+ let xmlbodypath = !(Lazy.force basedir) ^ "/" ^ xmlbodyfilename in
+ let path_scheme_of path = "file://" ^ path in
+ MatitaMisc.mkdirs (List.map Filename.dirname [innertypespath; xmlpath]);
+ (* now write to disk *)
+ ensure_path_exists innertypespath;
+ Xml.pp ~gzip:true xmlinnertypes (Some innertypespath) ;
+ ensure_path_exists xmlpath;
+ Xml.pp ~gzip:true xml (Some xmlpath) ;
+
+ (* now register to the getter *)
+ Http_getter.register' innertypesuri (path_scheme_of innertypespath);
+ Http_getter.register' uri (path_scheme_of xmlpath);
+ (* now the optional body, both write and register *)
+ (match bodyxml,bodyuri with
+ None,None -> ()
+ | Some bodyxml,Some bodyuri->
+ ensure_path_exists xmlbodypath;
+ Xml.pp ~gzip:true bodyxml (Some xmlbodypath) ;
+ Http_getter.register' bodyuri (path_scheme_of xmlbodypath)
+ | _-> assert false)
+
+ (* TODO ZACK a lot more to be done here:
+ * - save universe file *)
+let add_constant_to_world ~(console: #MatitaTypes.console)
+ ~dbd ~uri ?body ~ty ?(params = []) ?(attrs = []) ~ugraph ()
+=
+ let suri = UriManager.string_of_uri uri in
+ if CicEnvironment.in_library uri then
+ error (sprintf "%s constant already defined" suri)
+ else begin
+ let name = UriManager.name_of_uri uri in
+ let obj = Cic.Constant (name, body, ty, params, attrs) in
+ let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in
+ CicEnvironment.add_type_checked_term uri (obj, ugraph);
+ MetadataDb.index_constant ~dbd ~uri ~body ~ty;
+ save_object_to_disk uri obj;
+ console#echo_message (sprintf "%s constant defined" suri)
+ end
+
+let add_inductive_def_to_world ~(console: #MatitaTypes.console)
+ ~dbd ~uri ~indTypes ?(params = []) ?(leftno = 0) ?(attrs = []) ~ugraph ()
=
- let shared = new sharedState ~disambiguator ~proof_handler ~console () in
+ let suri = UriManager.string_of_uri uri in
+ if CicEnvironment.in_library uri then
+ error (sprintf "%s inductive type already defined" suri)
+ else begin
+ let name = UriManager.name_of_uri uri in
+ let obj = Cic.InductiveDefinition (indTypes, params, leftno, attrs) in
+ let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in
+ CicEnvironment.put_inductive_definition uri (obj, ugraph);
+ MetadataDb.index_inductive_def ~dbd ~uri ~types:indTypes;
+ save_object_to_disk uri obj;
+ console#echo_message (sprintf "%s inductive type defined" suri);
+ let elim sort =
+ try
+ let obj = CicElim.elim_of ~sort uri 0 in
+ let (name, body, ty, attrs) = split_obj obj in
+ let suri = qualify name ^ ".con" in
+ let uri = UriManager.uri_of_string suri in
+ (* TODO Zack: make CicElim returns a universe *)
+ let ugraph = CicUniv.empty_ugraph in
+ add_constant_to_world ~console ~dbd ~uri ?body ~ty ~attrs ~ugraph ();
+(*
+ console#echo_message
+ (sprintf "%s eliminator (automatically) defined" suri)
+*)
+ with CicElim.Can_t_eliminate -> ()
+ in
+ List.iter elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
+ end
+
+ (** Implements phrases that should be accepted only in Command state *)
+class commandState ~(console: #MatitaTypes.console) ?mathViewer () =
+ let shared = new sharedState ~console ?mathViewer () in
object (self)
inherit interpreterState ~console
method evalTactical = function
| TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
- | TacticAst.Command (TacticAst.Theorem (_, name_opt, ast, None)) ->
- let (_, metasenv, expr) = disambiguator#disambiguateTermAst ast in
- let uri =
- match name_opt with
- | None -> None
- | Some name -> Some (uri name)
+ | TacticAst.Command (TacticAst.Theorem (_, Some name, ast, None)) ->
+ let (_, metasenv, expr,ugraph) =
+ disambiguator#disambiguateTermAst ast
+ in
+ let uri = UriManager.uri_of_string (qualify name ^ ".con") in
+ let proof = MatitaProof.proof ~typ:expr ~uri ~metasenv () in
+ currentProof#start proof;
+ New_state Proof
+ | TacticAst.Command
+ (TacticAst.Theorem (_, Some name, type_ast, Some body_ast)) ->
+ let (_, metasenv, type_cic, ugraph) =
+ disambiguator#disambiguateTermAst type_ast
+ in
+ let (_, metasenv, body_cic, ugraph) =
+ disambiguator#disambiguateTermAst ~metasenv body_ast
+ in
+ let (body_type, ugraph) =
+ CicTypeChecker.type_of_aux' metasenv [] body_cic ugraph
+ in
+ let uri = UriManager.uri_of_string (qualify name ^ ".con") in
+ let (subst, metasenv, ugraph) =
+ CicUnification.fo_unif metasenv [] body_type type_cic ugraph
in
- let proof = MatitaProof.proof ~typ:expr ?uri ~metasenv () in
- proof_handler.MatitaTypes.new_proof proof;
- Some `Proof
+ let body = CicMetaSubst.apply_subst subst body_cic in
+ let ty = CicMetaSubst.apply_subst subst type_cic in
+ add_constant_to_world ~console ~dbd ~uri ~body ~ty ~ugraph ();
+ Quiet
+ | TacticAst.Command (TacticAst.Inductive (params, indTypes)) ->
+
+ let (uri, (indTypes, params, leftno)) =
+ inddef_of_ast params indTypes disambiguator
+ in
+ let obj = Cic.InductiveDefinition (indTypes, params, leftno, []) in
+ let ugraph =
+ CicTypeChecker.typecheck_mutual_inductive_defs uri
+ (indTypes, params, leftno) CicUniv.empty_ugraph
+ in
+ add_inductive_def_to_world ~console
+ ~dbd ~uri ~indTypes ~params ~leftno ~ugraph ();
+ Quiet
| 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
+ | TacticAst.Command (TacticAst.Coercion c_ast) ->
+ let env, metasenv, coercion, ugraph =
+ disambiguator#disambiguateTermAst c_ast
+ in
+ let coer_uri,coer_ty =
+ match coercion with
+ | Cic.Const (uri,_)
+ | Cic.Var (uri,_) ->
+ let o,_ =
+ CicEnvironment.get_obj CicUniv.empty_ugraph uri
+ in
+ (match o with
+ | Cic.Constant (_,_,ty,_,_)
+ | Cic.Variable (_,_,ty,_,_) ->
+ uri,ty
+ | _ -> assert false)
+ | Cic.MutConstruct (uri,t,c,_) ->
+ let o,_ =
+ CicEnvironment.get_obj CicUniv.empty_ugraph uri
+ in
+ (match o with
+ | Cic.InductiveDefinition (l,_,_,_) ->
+ let (_,_,_,cl) = List.nth l t in
+ let (_,cty) = List.nth cl c in
+ uri,cty
+ | _ -> assert false)
+ | _ -> assert false
+ in
+ (* we have to get the source and the tgt type uri
+ * in Coq syntax we have already their names, but
+ * since we don't support Funclass and similar I think
+ * all the coercion should be of the form
+ * (A:?)(B:?)T1->T2
+ * So we should be able to extract them from the coercion type
+ *)
+ let extract_last_two_p ty =
+ let rec aux = function
+ | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))
+ | Cic.Prod( _, src, tgt) -> src, tgt
+ | _ -> assert false
+ in
+ aux ty
+ in
+ let rec uri_of_term = function
+ | Cic.Const(u,_) -> u
+ | Cic.MutInd (u, i , _) ->
+ (* we have to build by hand the #xpointer *)
+ let base = UriManager.string_of_uri u in
+ let xp = "#xpointer(1/" ^ (string_of_int (i+1)) ^ ")" in
+ UriManager.uri_of_string (base ^ xp)
+ | Cic.Appl (he::_) -> uri_of_term he
+ | t ->
+ prerr_endline ("Fallisco a estrarre la uri di " ^
+ (CicPp.ppterm t));
+ assert false
+ in
+ let ty_src,ty_tgt = extract_last_two_p coer_ty in
+ let src_uri = uri_of_term ty_src in
+ let tgt_uri = uri_of_term ty_tgt in
+ let coercions_to_add =
+ CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri
+ in
+ (* FIXME: we should chek it this object can be a coercion
+ * maybe add the check to extract_last_two_p
+ *)
+ console#echo_message (sprintf "Coercion %s"
+ (UriManager.string_of_uri coer_uri));
+ List.iter (fun (uri,obj,ugraph) ->
+ (*
+ console#echo_message
+ (sprintf "Coercion (automatic) %s"
+ (UriManager.string_of_uri uri));
+ *)
+ let (name, body, ty, attrs) = split_obj obj in
+ add_constant_to_world ~console
+ ~dbd ~uri ?body ~ty ~attrs ~ugraph ();
+ ) coercions_to_add;
+ Quiet
| tactical -> shared#evalTactical tactical
end
incr count;
name
end else
- FreshNamesGenerator.mk_fresh_name metasenv context name ~typ
+ 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)
- ~(console: MatitaConsole.console)
- ()
-=
- let disambiguate ast =
- let (_, _, term) = disambiguate ~disambiguator ~proof_handler ast in
- term
- in
- (** tactic AST -> ProofEngineTypes.tactic *)
- let rec lookup_tactic = function
- | 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.Elim (term, _) -> (* TODO Zack implement "using" argument *)
- PrimitiveTactics.elim_intros_simpl_tac (disambiguate term)
- | TacticAst.ElimType term ->
- EliminationTactics.elim_type_tac (disambiguate term)
- | TacticAst.Replace (what, with_what) ->
- EqualityTactics.replace_tac ~what:(disambiguate what)
- ~with_what:(disambiguate with_what)
- (*
- (* TODO Zack a lot more of tactics to be implemented here ... *)
- | TacticAst.Change of 'term * 'term * 'ident option
- | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
- | TacticAst.Decompose of 'ident * 'ident list
- | TacticAst.Discriminate of 'ident
- | TacticAst.Fold of reduction_kind * 'term
- | TacticAst.Injection of 'ident
- | TacticAst.LetIn of 'term * 'ident
- | TacticAst.Reduce of reduction_kind * 'term pattern * 'ident option
- | TacticAst.Replace_pattern of 'term pattern * 'term
- | TacticAst.Rewrite of direction * 'term * 'ident option
- *)
- | _ ->
- MatitaTypes.not_implemented "some tactic"
- in
- let shared = new sharedState ~disambiguator ~proof_handler ~console () in
+class proofState ~(console: #MatitaTypes.console) ?mathViewer () =
+ let shared = new sharedState ~console ?mathViewer () in
object (self)
inherit interpreterState ~console
+ method private disambiguate ast =
+ let (_, _, term, _) =
+ MatitaCicMisc.disambiguate ~disambiguator ~currentProof ast
+ in
+ term
+
+ (** tactic AST -> ProofEngineTypes.tactic *)
+ method private lookup_tactic = function
+ | TacticAst.LocatedTactic (_, tactic) -> self#lookup_tactic tactic
+ | TacticAst.Intros (_, names) -> (* TODO Zack implement intros length *)
+ PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names)
+ ()
+ | 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 (self#disambiguate term)
+ | TacticAst.Apply term -> Tactics.apply (self#disambiguate term)
+ | TacticAst.Absurd term -> Tactics.absurd (self#disambiguate term)
+ | TacticAst.Exact term -> Tactics.exact (self#disambiguate term)
+ | TacticAst.Cut term -> Tactics.cut (self#disambiguate term)
+ | TacticAst.Elim (term, _) -> (* TODO Zack implement "using" argument *)
+ Tactics.elim_intros_simpl (self#disambiguate term)
+ | TacticAst.ElimType term -> Tactics.elim_type (self#disambiguate term)
+ | TacticAst.Replace (what, with_what) ->
+ Tactics.replace ~what:(self#disambiguate what)
+ ~with_what:(self#disambiguate with_what)
+ | TacticAst.Auto -> Tactics.auto_new ~dbd
+ | TacticAst.Change (what, with_what, _) ->
+ let what = self#disambiguate what in
+ let with_what = self#disambiguate with_what in
+ Tactics.change ~what ~with_what
+ (*
+ (* TODO Zack a lot more of tactics to be implemented here ... *)
+ | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
+ | TacticAst.Change of 'term * 'term * 'ident option
+ | TacticAst.Decompose of 'ident * 'ident list
+ | TacticAst.Discriminate of 'ident
+ | TacticAst.Fold of reduction_kind * 'term
+ | TacticAst.Injection of 'ident
+ | TacticAst.LetIn of 'term * 'ident
+ | TacticAst.Reduce of reduction_kind * 'term pattern * 'ident option
+ | TacticAst.Replace_pattern of 'term pattern * 'term
+ | TacticAst.Rewrite of direction * 'term * 'ident option
+ *)
+ | _ -> MatitaTypes.not_implemented "some tactic"
+
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
- | TacticAst.Command (TacticAst.Qed name_opt) ->
- (* 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));
+ currentProof#proof#redo ?steps ();
+ New_state Proof
+ | TacticAst.Command (TacticAst.Qed None) ->
+ 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
+ let suri = UriManager.string_of_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 = CicTypeChecker.type_of_aux' [] [] bo in
- if not (CicReduction.are_convertible [] proved_ty ty) then
- failwith "Wrong proof";
- (* TODO Zack [] probably wrong *)
- CicEnvironment.add_type_checked_term uri
- (Cic.Constant ((UriManager.name_of_uri uri),(Some bo),ty,[]));
- proof_handler.MatitaTypes.set_proof None;
- (* TODO Zack a lot more to be done here:
- * - save object to disk in xml format
- * - collect metadata
- * - register uri to the getter *)
- Some `Command
+ let proved_ty,ugraph =
+ CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph
+ in
+ let b,ugraph =
+ CicReduction.are_convertible [] proved_ty ty ugraph
+ in
+ if not b then failwith "Wrong proof";
+ add_constant_to_world ~console ~dbd ~uri ~body:bo ~ty ~ugraph ();
+ currentProof#abort ();
+ console#echo_message (sprintf "%s defined" suri);
+ 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
+ let tactic = self#lookup_tactic tactic_phrase in
+ currentProof#proof#apply_tactic tactic;
+ New_state Proof
| tactical -> shared#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
+class interpreter ~(console: #MatitaTypes.console) ?mathViewer () =
+ let commandState = new commandState ~console ?mathViewer () in
+ let proofState = new proofState ~console ?mathViewer () in
object (self)
val mutable state = commandState
method reset = state <- commandState
- method private updateState = function
- | Some `Command -> state <- commandState
- | Some `Proof -> state <- proofState
- | None -> ()
+ method endOffset = state#endOffset
- method evalPhrase s =
- try
- self#updateState (state#evalPhrase s)
- with exn ->
- console#echo_error (sprintf "Uncaught exception: %s"
- (Printexc.to_string exn))
+ method private updateState = function
+ | New_state Command -> (state <- commandState)
+ | New_state Proof -> (state <- proofState)
+ | _ -> ()
-(*
- method evalAll s =
- let get_end_pos = function
- | TacticAst.LocatedTactical ((_, end_pos), _) -> end_pos.Lexing.pos_cnum
- | _ -> assert false
- in
- let str_len = String.length s in
- let rec aux offset =
- let tactical =
- self#parsePhrase (String.sub s offset (str_len - offset))
- in
- self#updateState (state#evalTactical tactical);
- let next_offset = get_end_pos tactical + offset in
- if next_offset = str_len - 1 then
- in
-*)
+ 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
+let interpreter ~(console: #MatitaTypes.console) ?mathViewer () =
+ new interpreter ~console ?mathViewer ()
+