(* Copyright (C) 2004-2005, 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 open MatitaTypes let alias_diff ~from status = let module Map = DisambiguateTypes.Environment in Map.fold (fun domain_item (description1,_ as codomain_item) acc -> try let description2,_ = Map.find domain_item from.aliases in if description1 <> description2 then (domain_item,codomain_item)::acc else acc with Not_found -> (domain_item,codomain_item)::acc) status.aliases [] let alias_diff = let profiler = HExtlib.profile "alias_diff (conteggiato anche in include)" in fun ~from status -> profiler.HExtlib.profile (alias_diff ~from) status let set_proof_aliases status new_aliases = let commands_of_aliases = List.map (fun alias -> GrafiteAst.Alias (DisambiguateTypes.dummy_floc, alias)) in let deps_of_aliases = HExtlib.filter_map (function | GrafiteAst.Ident_alias (_, suri) -> let buri = UriManager.buri_of_uri (UriManager.uri_of_string suri) in Some (GrafiteAst.Dependency buri) | _ -> None) in let aliases = List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc) status.aliases new_aliases in let multi_aliases = List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc) status.multi_aliases new_aliases in let new_status = { status with multi_aliases = multi_aliases ; aliases = aliases} in if new_aliases = [] then new_status else let aliases = DisambiguatePp.aliases_of_domain_and_codomain_items_list new_aliases in let status = add_moo_content (commands_of_aliases aliases) new_status in let status = add_moo_metadata (deps_of_aliases aliases) status in status (** given a uri and a type list (the contructors types) builds a list of pairs * (name,uri) that is used to generate automatic aliases **) let extract_alias types uri = fst(List.fold_left ( fun (acc,i) (name, _, _, cl) -> (name, UriManager.string_of_uri (UriManager.uri_of_uriref uri i None)) :: (fst(List.fold_left ( fun (acc,j) (name,_) -> (((name,UriManager.string_of_uri (UriManager.uri_of_uriref uri i (Some j))) :: acc) , j+1) ) (acc,1) cl)),i+1 ) ([],0) types) let build_aliases = List.map (fun (name,suri) -> DisambiguateTypes.Id name, (suri, fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string suri))) let add_aliases_for_inductive_def status types suri = let uri = UriManager.uri_of_string suri in let aliases = build_aliases (extract_alias types uri) in set_proof_aliases status aliases let add_alias_for_constant status suri = let uri = UriManager.uri_of_string suri in let name = UriManager.name_of_uri uri in let new_env = build_aliases [(name,suri)] in set_proof_aliases status new_env let add_aliases_for_object status suri = function Cic.InductiveDefinition (types,_,_,_) -> add_aliases_for_inductive_def status types suri | Cic.Constant _ -> add_alias_for_constant status suri | Cic.Variable _ | Cic.CurrentProof _ -> assert false let paths_and_uris_of_obj uri status = let basedir = get_string_option status "basedir" ^ "/xml" in let innertypesuri = UriManager.innertypesuri_of_uri uri in let bodyuri = UriManager.bodyuri_of_uri uri in let univgraphuri = UriManager.univgraphuri_of_uri uri in let innertypesfilename = Str.replace_first (Str.regexp "^cic:") "" (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in let innertypespath = basedir ^ "/" ^ innertypesfilename in let xmlfilename = Str.replace_first (Str.regexp "^cic:/") "" (UriManager.string_of_uri uri) ^ ".xml.gz" in let xmlpath = basedir ^ "/" ^ xmlfilename in let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") "" (UriManager.string_of_uri uri) ^ ".body.xml.gz" in let xmlbodypath = basedir ^ "/" ^ xmlbodyfilename in let xmlunivgraphfilename = Str.replace_first (Str.regexp "^cic:/") "" (UriManager.string_of_uri univgraphuri) ^ ".xml.gz" in let xmlunivgraphpath = basedir ^ "/" ^ xmlunivgraphfilename in xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri, xmlunivgraphpath, univgraphuri let save_object_to_disk status uri obj ugraph univlist = let ensure_path_exists path = let dir = Filename.dirname path in HExtlib.mkdir dir in (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *) let annobj = Cic2acic.plain_acic_object_of_cic_object obj in (* prepare XML *) let xml, bodyxml = Cic2Xml.print_object uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj in let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri, xmlunivgraphpath, univgraphuri = paths_and_uris_of_obj uri status in let path_scheme_of path = "file://" ^ path in List.iter HExtlib.mkdir (List.map Filename.dirname [xmlpath]); (* now write to disk *) ensure_path_exists xmlpath; Xml.pp ~gzip:true xml (Some xmlpath); CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph univlist; (* we return a list of uri,path we registered/created *) (uri,xmlpath) :: (univgraphuri,xmlunivgraphpath) :: (* 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); [bodyuri, xmlbodypath] | _-> assert false) let typecheck_obj = let profiler = HExtlib.profile "add_obj.typecheck_obj" in fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj let index_obj = let profiler = HExtlib.profile "add_obj.index_obj" in fun ~dbd ~uri -> profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri let add_obj uri obj status = let dbd = MatitaDb.instance () in let suri = UriManager.string_of_uri uri in if CicEnvironment.in_library uri then command_error (sprintf "%s already defined" suri) else begin typecheck_obj uri obj; (* 1 *) let _, ugraph, univlist = CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in try index_obj ~dbd ~uri; (* 2 must be in the env *) try let new_stuff=save_object_to_disk status uri obj ugraph univlist in(*3*) try MatitaLog.message (sprintf "%s defined" suri); let status = add_aliases_for_object status suri obj in { status with objects = new_stuff @ status.objects; proof_status = No_proof } with exc -> List.iter MatitaMisc.safe_remove (List.map snd new_stuff); (* -3 *) raise exc with exc -> ignore(MatitaDb.remove_uri uri); (* -2 *) raise exc with exc -> CicEnvironment.remove_obj uri; (* -1 *) raise exc end let add_obj = let profiler = HExtlib.profile "add_obj" in fun uri obj status -> profiler.HExtlib.profile (add_obj uri obj) status module OrderedUri = struct type t = UriManager.uri * string let compare (u1, _) (u2, _) = UriManager.compare u1 u2 end module OrderedId = struct type t = CicNotation.notation_id let compare = Pervasives.compare end module UriSet = Set.Make (OrderedUri) module IdSet = Set.Make (OrderedId) (** @return l2 \ l1 *) let urixstring_list_diff l2 l1 = let module S = UriSet in let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in let diff = S.diff s2 s1 in S.fold (fun uri uris -> uri :: uris) diff [] let uri_list_diff l2 l1 = let module S = UriManager.UriSet in let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in let diff = S.diff s2 s1 in S.fold (fun uri uris -> uri :: uris) diff [] (** @return l2 \ l1 *) let id_list_diff l2 l1 = let module S = IdSet in let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in let diff = S.diff s2 s1 in S.fold (fun uri uris -> uri :: uris) diff [] let remove_coercion uri = CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri) let time_travel ~present ~past = let objs_to_remove = urixstring_list_diff present.objects past.objects in let coercions_to_remove = uri_list_diff present.coercions past.coercions in let notation_to_remove = id_list_diff present.notation_ids past.notation_ids in let debug_list = ref [] in List.iter remove_coercion coercions_to_remove; List.iter (fun (uri,p) -> MatitaMisc.safe_remove p; (try CicEnvironment.remove_obj uri with CicEnvironment.Object_not_found _ -> MatitaLog.debug (sprintf "time_travel removes from cache %s that is not in" (UriManager.string_of_uri uri))); let l = MatitaDb.remove_uri uri in debug_list := UriManager.string_of_uri uri :: !debug_list @ l) objs_to_remove; List.iter CicNotation.remove_notation notation_to_remove (* (* this is debug code * idea: debug_list is the list of objects to be removed as computed from the * db, while list_of_objs_to_remove is the same list but computer from the * differences between statuses *) let l1 = List.sort Pervasives.compare !debug_list in let l2 = List.sort Pervasives.compare (List.map (fun (x,_) -> UriManager.string_of_uri x) list_of_objs_to_remove) in let rec uniq = function | [] -> [] | h::[] -> [h] | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl) | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl in let l1 = uniq l1 in let l2 = uniq l2 in try List.iter2 (fun x y -> if x <> y then raise Exit) l1 l2 with | Invalid_argument _ | Exit -> MatitaLog.debug "It seems you garbaged something..."; MatitaLog.debug "l1:"; List.iter MatitaLog.debug l1; MatitaLog.debug "l2:"; List.iter MatitaLog.debug l2 *) let last_baseuri = ref "" let remove ?(verbose=false) uri = let derived_uris_of_uri uri = UriManager.innertypesuri_of_uri uri :: UriManager.univgraphuri_of_uri uri :: (match UriManager.bodyuri_of_uri uri with | None -> [] | Some u -> [u]) in let to_remove = uri :: (if UriManager.uri_is_ind uri then MatitaDb.xpointers_of_ind uri else []) @ derived_uris_of_uri uri in List.iter (fun uri -> (try (* WARNING: non reentrant debugging code *) if verbose then let baseuri = UriManager.buri_of_uri uri in if !last_baseuri <> baseuri then begin MatitaLog.debug ("Removing: " ^ baseuri ^ "/*"); last_baseuri := baseuri end; let file = Http_getter.resolve' uri in MatitaMisc.safe_remove file; MatitaMisc.rmdir_descend (Filename.dirname file) with Http_getter_types.Key_not_found _ -> ()); remove_coercion uri; ignore (MatitaDb.remove_uri uri); CicEnvironment.remove_obj uri) to_remove