+ 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 -> outcome
+
+ method evalPhrase s =
+ debug_print (sprintf "evaluating '%s'" s);
+ self#evalTactical (self#parsePhrase (Stream.of_string s))
+
+ method evalAst ast = self#evalTactical ast
+
+ 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)
+ ~(console: #MatitaTypes.console)
+ ?(mathViewer: MatitaTypes.mathViewer option)
+ ()
+=
+ object (self)
+ inherit interpreterState ~console
+ method evalTactical = function
+ | TacticAst.Command TacticAst.Quit ->
+ currentProof#quit ();
+ assert false (* dummy answer, useless *)
+ | TacticAst.Command TacticAst.Proof ->
+ (* do nothing, just for compatibility with coq syntax *)
+ 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)) ->
+ 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\"" !basedir);
+ Quiet
+ | TacticAst.Command (TacticAst.Check term) ->
+ 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,ugraph1 =
+ CicTypeChecker.type_of_aux' metasenv context term ugraph
+ in
+ (* TASSI: here ugraph1 is unused.... FIXME *)
+ let expr = Cic.Cast (term, ty) in
+ (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
+
+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" in
+ let innertypespath = !basedir ^ "/" ^ innertypesfilename in
+ let xmlfilename = Str.replace_first (Str.regexp "^cic:") ""
+ (UriManager.string_of_uri uri) ^ ".xml" in
+ let xmlpath = !basedir ^ "/" ^ xmlfilename in
+ let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:") ""
+ (UriManager.string_of_uri uri) ^ ".body.xml" in
+ let xmlbodypath = !basedir ^ "/" ^ xmlbodyfilename in
+ let path_scheme_of path = "file:/" ^ path in
+
+ (* now write to disk *)
+ ensure_path_exists innertypespath;
+ Xml.pp ~quiet:true xmlinnertypes (Some innertypespath) ;
+ ensure_path_exists xmlpath;
+ Xml.pp ~quiet: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 ~quiet:true bodyxml (Some xmlbodypath) ;
+ Http_getter.register' bodyuri (path_scheme_of xmlbodypath)
+ | _-> assert false)
+;;
+
+
+
+ (* 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 ~(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
+ ~owner:(Helm_registry.get "matita.owner") ~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 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
+ ~owner:(Helm_registry.get "matita.owner") ~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 ())) ];