]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaSync.ml
Generation of auxiliary lemmas for inductive types and records moved
[helm.git] / helm / matita / matitaSync.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 open MatitaTypes
29
30 let alias_diff ~from status = 
31   let module Map = DisambiguateTypes.Environment in
32   Map.fold
33     (fun domain_item (description1,_ as codomain_item) acc ->
34       try
35        let description2,_ = Map.find domain_item from.aliases in
36         if description1 <> description2 then
37          (domain_item,codomain_item)::acc
38         else
39           acc
40       with
41        Not_found ->
42          (domain_item,codomain_item)::acc)
43     status.aliases []
44
45 let alias_diff =
46  let profiler = HExtlib.profile "alias_diff (conteggiato anche in include)" in
47  fun ~from status -> profiler.HExtlib.profile (alias_diff ~from) status
48
49 let set_proof_aliases status new_aliases =
50  let commands_of_aliases =
51    List.map
52     (fun alias -> GrafiteAst.Alias (DisambiguateTypes.dummy_floc, alias))
53  in
54  let deps_of_aliases =
55    HExtlib.filter_map
56     (function
57     | GrafiteAst.Ident_alias (_, suri) ->
58         let buri = UriManager.buri_of_uri (UriManager.uri_of_string suri) in
59         Some (GrafiteAst.Dependency buri)
60     | _ -> None)
61  in
62  let aliases =
63   List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
64    status.aliases new_aliases in
65  let multi_aliases =
66   List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc)
67    status.multi_aliases new_aliases in
68  let new_status =
69    { status with multi_aliases = multi_aliases ; aliases = aliases}
70  in
71  if new_aliases = [] then
72    new_status
73  else
74    let aliases = 
75      DisambiguatePp.aliases_of_domain_and_codomain_items_list new_aliases
76    in
77    let status = add_moo_content (commands_of_aliases aliases) new_status in
78    let status = add_moo_metadata (deps_of_aliases aliases) status in
79    status
80
81 (** given a uri and a type list (the contructors types) builds a list of pairs
82  *  (name,uri) that is used to generate automatic aliases **)
83 let extract_alias types uri = 
84   fst(List.fold_left (
85     fun (acc,i) (name, _, _, cl) -> 
86       (name, UriManager.uri_of_uriref uri i None) ::
87       (fst(List.fold_left (
88         fun (acc,j) (name,_) ->
89           (((name,UriManager.uri_of_uriref uri i
90           (Some j)) :: acc) , j+1)
91         ) (acc,1) cl)),i+1
92   ) ([],0) types)
93
94 let build_aliases =
95  List.map
96   (fun (name,uri) ->
97     DisambiguateTypes.Id name,
98      (UriManager.string_of_uri uri, fun _ _ _ -> CicUtil.term_of_uri uri))
99
100 let add_aliases_for_inductive_def status types uri = 
101   let aliases = build_aliases (extract_alias types uri) in
102    set_proof_aliases status aliases
103
104 let add_alias_for_constant status uri =
105  let name = UriManager.name_of_uri uri in
106  let new_env = build_aliases [(name,uri)] in
107  set_proof_aliases status new_env
108
109 let add_aliases_for_object status uri =
110  function
111     Cic.InductiveDefinition (types,_,_,_) ->
112      add_aliases_for_inductive_def status types uri
113   | Cic.Constant _ -> add_alias_for_constant status uri
114   | Cic.Variable _
115   | Cic.CurrentProof _ -> assert false
116   
117 let add_obj uri obj status =
118  let basedir = Helm_registry.get "matita.basedir" in
119  let lemmas = LibrarySync.add_obj uri obj basedir in
120   List.fold_left add_alias_for_constant
121    (add_aliases_for_object status uri obj) lemmas
122
123 let add_obj =
124  let profiler = HExtlib.profile "add_obj" in
125   fun uri obj status -> profiler.HExtlib.profile (add_obj uri obj) status
126    
127 module OrderedUri =
128 struct
129   type t = UriManager.uri * string
130   let compare (u1, _) (u2, _) = UriManager.compare u1 u2
131 end
132
133 module OrderedId = 
134 struct
135   type t = CicNotation.notation_id
136   let compare = Pervasives.compare
137 end
138
139 module UriSet = Set.Make (OrderedUri)
140 module IdSet  = Set.Make (OrderedId)
141
142   (** @return l2 \ l1 *)
143 let urixstring_list_diff l2 l1 =
144   let module S = UriSet in
145   let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
146   let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
147   let diff = S.diff s2 s1 in
148   S.fold (fun uri uris -> uri :: uris) diff []
149
150 let uri_list_diff l2 l1 =
151   let module S = UriManager.UriSet in
152   let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
153   let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
154   let diff = S.diff s2 s1 in
155   S.fold (fun uri uris -> uri :: uris) diff []
156
157   (** @return l2 \ l1 *)
158 let id_list_diff l2 l1 =
159   let module S = IdSet in
160   let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
161   let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
162   let diff = S.diff s2 s1 in
163   S.fold (fun uri uris -> uri :: uris) diff []
164
165 let time_travel ~present ~past =
166   let objs_to_remove = urixstring_list_diff present.objects past.objects in
167   let coercions_to_remove = uri_list_diff present.coercions past.coercions in
168   let notation_to_remove =
169     id_list_diff present.notation_ids past.notation_ids
170   in
171   let debug_list = ref [] in
172   List.iter
173    (fun uri -> CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri))
174    coercions_to_remove;
175   List.iter
176     (fun (uri,p) -> 
177       HExtlib.safe_remove p;
178       (try 
179         CicEnvironment.remove_obj uri
180       with CicEnvironment.Object_not_found _ -> 
181         HLog.debug
182           (sprintf "time_travel removes from cache %s that is not in" 
183             (UriManager.string_of_uri uri)));
184       let l = LibraryDb.remove_uri uri in
185       debug_list := UriManager.string_of_uri uri :: !debug_list @ l) 
186     objs_to_remove;
187   List.iter CicNotation.remove_notation notation_to_remove
188   (*
189   (* this is debug code
190   * idea: debug_list is the list of objects to be removed as computed from the
191   * db, while list_of_objs_to_remove is the same list but computer from the
192   * differences between statuses *)
193   let l1 = List.sort Pervasives.compare !debug_list in
194   let l2 = List.sort Pervasives.compare 
195     (List.map (fun (x,_) -> UriManager.string_of_uri x) list_of_objs_to_remove)
196   in
197   let rec uniq = function 
198     | [] -> []
199     | h::[] -> [h]
200     | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl)
201     | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
202   in
203   let l1 =  uniq l1 in
204   let l2 =  uniq l2 in
205   try
206     List.iter2 (fun x y -> if x <> y then raise Exit) l1 l2
207   with
208   | Invalid_argument _ | Exit -> 
209       HLog.debug "It seems you garbaged something...";
210       HLog.debug "l1:";
211       List.iter HLog.debug l1;
212       HLog.debug "l2:";
213       List.iter HLog.debug l2
214       *)