]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
new tacticals
[helm.git] / helm / matita / matitaMisc.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 open MatitaTypes 
28
29 (** Functions "imported" from Http_getter_misc *)
30
31 let strip_trailing_slash = Http_getter_misc.strip_trailing_slash
32 let normalize_dir = Http_getter_misc.normalize_dir
33 let strip_suffix = Http_getter_misc.strip_suffix
34
35 let baseuri_of_baseuri_decl st =
36   match st with
37   | GrafiteAst.Executable (_, GrafiteAst.Command (_, GrafiteAst.Set (_, "baseuri", buri))) ->
38       Some buri
39   | _ -> None
40
41 let baseuri_of_file file = 
42   let uri = ref None in
43   let ic = open_in file in
44   let istream = Ulexing.from_utf8_channel ic in
45   (try
46     while true do
47       try 
48         let stm = GrafiteParser.parse_statement istream in
49         match baseuri_of_baseuri_decl stm with
50         | Some buri -> 
51             let u = strip_trailing_slash buri in
52             if String.length u < 5 || String.sub u 0 5 <> "cic:/" then
53               MatitaLog.error (file ^ " sets an incorrect baseuri: " ^ buri);
54             (try 
55               ignore(Http_getter.resolve u)
56             with
57             | Http_getter_types.Unresolvable_URI _ -> 
58                 MatitaLog.error (file ^ " sets an unresolvable baseuri: "^buri)
59             | Http_getter_types.Key_not_found _ -> ());
60             uri := Some u;
61             raise End_of_file
62         | None -> ()
63       with
64         CicNotationParser.Parse_error _ as exn ->
65           prerr_endline ("Unable to parse: " ^ file);
66           prerr_endline (MatitaExcPp.to_string exn);
67           ()
68     done
69   with End_of_file -> close_in ic);
70   match !uri with
71   | Some uri -> uri
72   | None -> failwith ("No baseuri defined in " ^ file)
73
74 let is_empty buri =
75  List.for_all
76   (function
77       Http_getter_types.Ls_section _ -> true
78     | Http_getter_types.Ls_object _ -> false)
79   (Http_getter.ls (Http_getter_misc.strip_trailing_slash buri ^ "/"))
80
81 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
82
83 let is_dir_empty d =
84   try 
85     let od = Unix.opendir d in
86     try 
87       ignore (Unix.readdir od);
88       ignore (Unix.readdir od);
89       ignore (Unix.readdir od);
90       Unix.closedir od;
91       false
92     with End_of_file -> 
93       Unix.closedir od;
94       true
95   with Unix.Unix_error _ -> true
96
97 let safe_rmdir d = try Unix.rmdir d with Unix.Unix_error _ -> ()
98
99 let rec rmdir_descend d = 
100   if is_dir_empty d then
101     begin
102       safe_rmdir d;
103       rmdir_descend (Filename.dirname d)
104     end
105
106 let absolute_path file =
107   if file.[0] = '/' then file else Unix.getcwd () ^ "/" ^ file
108   
109 let is_proof_script fname = true  (** TODO Zack *)
110 let is_proof_object fname = true  (** TODO Zack *)
111
112 let append_phrase_sep s =
113   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
114     s ^ BuildTimeConf.phrase_sep
115   else
116     s
117
118 let empty_mathml = lazy (
119   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
120     ~qualifiedName:(Gdome.domString "math") ~doctype:None)
121
122 let empty_boxml = lazy (
123   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns) 
124     ~qualifiedName:(Gdome.domString "box") ~doctype:None)
125
126 let closed_goal_mathml = lazy (
127   DomMisc.domImpl#createDocumentFromURI ~uri:BuildTimeConf.closed_xml ())
128
129 exception History_failure
130
131 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
132
133 class type ['a] history =
134   object
135     method add : 'a -> unit
136     method next : 'a
137     method previous : 'a
138     method load: 'a memento -> unit
139     method save: 'a memento
140     method is_begin: bool
141     method is_end: bool
142   end
143
144 class basic_history (head, tail, cur) =
145   object
146     val mutable hd = head  (* insertion point *)
147     val mutable tl = tail (* oldest inserted item *)
148     val mutable cur = cur  (* current item for the history *)
149     
150     method is_begin = cur <= tl
151     method is_end = cur >= hd
152   end
153   
154   
155 class shell_history size =
156   let size = size + 1 in
157   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
158   let incr x = (x + 1) mod size in
159   object (self)
160     val data = Array.create size ""
161
162     inherit basic_history (0, -1 , -1)
163     
164     method add s =
165       data.(hd) <- s;
166       if tl = -1 then tl <- hd;
167       hd <- incr hd;
168       if hd = tl then tl <- incr tl;
169       cur <- hd
170     method previous =
171       if cur = tl then raise History_failure;
172       cur <- decr cur;
173       data.(cur)
174     method next =
175       if cur = hd then raise History_failure;
176       cur <- incr cur;
177       if cur = hd then "" else data.(cur)
178     method load (data', hd', tl', cur') =
179       assert (Array.length data = Array.length data');
180       hd <- hd'; tl <- tl'; cur <- cur';
181       Array.blit data' 0 data 0 (Array.length data')
182     method save = (Array.copy data, hd, tl, cur)
183   end
184
185 class ['a] browser_history ?memento size init =
186   object (self)
187     initializer match memento with Some m -> self#load m | _ -> ()
188     val data = Array.create size init
189
190     inherit basic_history (0, 0, 0)
191     
192     method previous =
193       if cur = tl then raise History_failure;
194       cur <- cur - 1;
195       if cur = ~-1 then cur <- size - 1;
196       data.(cur)
197     method next =
198       if cur = hd then raise History_failure;
199       cur <- cur + 1;
200       if cur = size then cur <- 0;
201       data.(cur)
202     method add (e:'a) =
203       if e <> data.(cur) then
204         begin
205           cur <- cur + 1;
206           if cur = size then cur <- 0;
207           if cur = tl then tl <- tl + 1;
208           if tl = size then tl <- 0;
209           hd <- cur;
210           data.(cur) <- e
211         end
212     method load (data', hd', tl', cur') =
213       assert (Array.length data = Array.length data');
214       hd <- hd'; tl <- tl'; cur <- cur';
215       Array.blit data' 0 data 0 (Array.length data')
216     method save = (Array.copy data, hd, tl, cur)
217   end
218
219 let singleton f =
220   let instance = lazy (f ()) in
221   fun () -> Lazy.force instance
222
223 let get_current_proof status =
224   match status.proof_status with
225   | Incomplete_proof { proof = p } -> p
226   | _ -> statement_error "no ongoing proof"
227
228 let get_proof_metasenv status =
229   match status.proof_status with
230   | No_proof -> []
231   | Proof (_, metasenv, _, _)
232   | Incomplete_proof { proof = (_, metasenv, _, _) }
233   | Intermediate metasenv ->
234       metasenv
235
236 let get_proof_context status goal =
237   match status.proof_status with
238   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
239       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
240       context
241   | _ -> []
242  
243 let get_proof_conclusion status goal =
244   match status.proof_status with
245   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
246       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
247       conclusion
248   | _ -> statement_error "no ongoing proof"
249
250 let get_stack status =
251   match status.proof_status with
252   | Incomplete_proof p -> p.stack
253   | Proof _ -> Continuationals.Stack.empty
254   | _ -> assert false
255
256 let set_stack stack status =
257   match status.proof_status with
258   | Incomplete_proof p ->
259       { status with proof_status = Incomplete_proof { p with stack = stack } }
260   | Proof _ ->
261       assert (Continuationals.Stack.is_empty stack);
262       status
263   | _ -> assert false
264  
265 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
266
267 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
268
269 let end_ma_RE = Pcre.regexp "\\.ma$"
270
271 let obj_file_of_baseuri baseuri =
272  let path =
273   Helm_registry.get "matita.basedir" ^ "/xml" ^
274    Pcre.replace ~pat:"^cic:" ~templ:"" baseuri
275  in
276   path ^ ".moo"
277
278 let obj_file_of_script f =
279  if f = "coq.ma" then BuildTimeConf.coq_notation_script else
280   let baseuri = baseuri_of_file f in
281    obj_file_of_baseuri baseuri
282
283 let list_tl_at ?(equality=(==)) e l =
284   let rec aux =
285     function
286     | [] -> raise Not_found
287     | hd :: tl as l when equality hd e -> l
288     | hd :: tl -> aux tl
289   in
290   aux l
291