]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
"include" command implemented.
[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 let is_dir fname =
30   try
31     (Unix.stat fname).Unix.st_kind = Unix.S_DIR
32   with Unix.Unix_error _ -> false
33
34 let is_regular fname =
35   try
36     (Unix.stat fname).Unix.st_kind = Unix.S_REG
37   with Unix.Unix_error _ -> false
38
39 let input_file fname =
40   let size = (Unix.stat fname).Unix.st_size in
41   let buf = Buffer.create size in
42   let ic = open_in fname in
43   Buffer.add_channel buf ic size;
44   close_in ic;
45   Buffer.contents buf
46
47 let is_proof_script fname = true  (** TODO Zack *)
48 let is_proof_object fname = true  (** TODO Zack *)
49
50 let append_phrase_sep s =
51   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
52     s ^ BuildTimeConf.phrase_sep
53   else
54     s
55
56 let strip_trailing_blanks =
57   let rex = Pcre.regexp "\\s*$" in
58   fun s -> Pcre.replace ~rex s
59
60 let strip_trailing_slash =
61   let rex = Pcre.regexp "/$" in
62   fun s -> Pcre.replace ~rex s
63
64 let empty_mathml () =
65   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
66     ~qualifiedName:(Gdome.domString "math") ~doctype:None
67
68 let empty_boxml () =
69   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns) 
70     ~qualifiedName:(Gdome.domString "box") ~doctype:None
71
72 exception History_failure
73
74 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
75
76 class type ['a] history =
77   object
78     method add : 'a -> unit
79     method next : 'a
80     method previous : 'a
81     method load: 'a memento -> unit
82     method save: 'a memento
83     method is_begin: bool
84     method is_end: bool
85   end
86
87 class basic_history (head, tail, cur) =
88   object
89     val mutable hd = head  (* insertion point *)
90     val mutable tl = tail (* oldest inserted item *)
91     val mutable cur = cur  (* current item for the history *)
92     
93     method is_begin = cur <= tl
94     method is_end = cur >= hd
95   end
96   
97   
98 class shell_history size =
99   let size = size + 1 in
100   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
101   let incr x = (x + 1) mod size in
102   object (self)
103     val data = Array.create size ""
104
105     inherit basic_history (0, -1 , -1)
106     
107     method add s =
108       data.(hd) <- s;
109       if tl = -1 then tl <- hd;
110       hd <- incr hd;
111       if hd = tl then tl <- incr tl;
112       cur <- hd
113     method previous =
114       if cur = tl then raise History_failure;
115       cur <- decr cur;
116       data.(cur)
117     method next =
118       if cur = hd then raise History_failure;
119       cur <- incr cur;
120       if cur = hd then "" else data.(cur)
121     method load (data', hd', tl', cur') =
122       assert (Array.length data = Array.length data');
123       hd <- hd'; tl <- tl'; cur <- cur';
124       Array.blit data' 0 data 0 (Array.length data')
125     method save = (Array.copy data, hd, tl, cur)
126   end
127
128 class ['a] browser_history ?memento size init =
129   object (self)
130     initializer match memento with Some m -> self#load m | _ -> ()
131     val data = Array.create size init
132
133     inherit basic_history (0, 0, 0)
134     
135     method previous =
136       if cur = tl then raise History_failure;
137       cur <- cur - 1;
138       if cur = ~-1 then cur <- size - 1;
139       data.(cur)
140     method next =
141       if cur = hd then raise History_failure;
142       cur <- cur + 1;
143       if cur = size then cur <- 0;
144       data.(cur)
145     method add (e:'a) =
146       if e <> data.(cur) then
147         begin
148           cur <- cur + 1;
149           if cur = size then cur <- 0;
150           if cur = tl then tl <- tl + 1;
151           if tl = size then tl <- 0;
152           hd <- cur;
153           data.(cur) <- e
154         end
155     method load (data', hd', tl', cur') =
156       assert (Array.length data = Array.length data');
157       hd <- hd'; tl <- tl'; cur <- cur';
158       Array.blit data' 0 data 0 (Array.length data')
159     method save = (Array.copy data, hd, tl, cur)
160   end
161
162 let singleton f =
163   let instance = lazy (f ()) in
164   fun () -> Lazy.force instance
165
166 let mkdir d =
167   let errmsg = sprintf "Unable to create directory \"%s\"" d in
168   try
169     (match Unix.system ("mkdir -p " ^ d) with
170     | Unix.WEXITED 0 -> ()
171     | _ -> failwith errmsg)
172   with Unix.Unix_error _ -> failwith errmsg
173
174 let get_proof_status status =
175   match status.proof_status with
176   | Incomplete_proof s -> s
177   | _ -> statement_error "no ongoing proof"
178
179 let get_proof_metasenv status =
180   match status.proof_status with
181   | No_proof -> []
182   | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
183   | Proof (_, metasenv, _, _) -> metasenv
184   | Intermediate m -> m
185
186 let get_proof_context status =
187   match status.proof_status with
188   | Incomplete_proof ((_, metasenv, _, _), goal) ->
189       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
190       context
191   | _ -> []
192  
193 let get_proof_aliases status = status.aliases
194
195 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
196
197 let unopt = function None -> failwith "unopt: None" | Some v -> v
198
199 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
200
201 let end_ma_RE = Pcre.regexp "\\.ma$"
202 let obj_file_of_script f = Pcre.replace ~rex:end_ma_RE ~templ:".moo" f
203
204 let rec list_uniq = function 
205   | [] -> []
206   | h::[] -> [h]
207   | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl) 
208   | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
209
210 let debug_wrap name f =
211   prerr_endline (sprintf "debug_wrap: ==>> %s" name);
212   let res = f () in
213   prerr_endline (sprintf "debug_wrap: <<== %s" name);
214   res
215