1 (* Copyright (C) 2004-2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
31 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
32 with Unix.Unix_error _ -> false
34 let is_regular fname =
36 (Unix.stat fname).Unix.st_kind = Unix.S_REG
37 with Unix.Unix_error _ -> false
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;
47 let is_proof_script fname = true (** TODO Zack *)
48 let is_proof_object fname = true (** TODO Zack *)
50 let append_phrase_sep s =
51 if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
52 s ^ BuildTimeConf.phrase_sep
56 let strip_trailing_blanks =
57 let rex = Pcre.regexp "\\s*$" in
58 fun s -> Pcre.replace ~rex s
61 Misc.domImpl#createDocument ~namespaceURI:(Some Misc.mathml_ns)
62 ~qualifiedName:(Gdome.domString "math") ~doctype:None
65 Misc.domImpl#createDocument ~namespaceURI:(Some Misc.boxml_ns)
66 ~qualifiedName:(Gdome.domString "box") ~doctype:None
68 exception History_failure
70 type 'a memento = 'a array * int * int * int (* data, hd, tl, cur *)
72 class type ['a] history =
74 method add : 'a -> unit
77 method load: 'a memento -> unit
78 method save: 'a memento
83 class basic_history (head, tail, cur) =
85 val mutable hd = head (* insertion point *)
86 val mutable tl = tail (* oldest inserted item *)
87 val mutable cur = cur (* current item for the history *)
89 method is_begin = cur <= tl
90 method is_end = cur >= hd
94 class shell_history size =
95 let size = size + 1 in
96 let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
97 let incr x = (x + 1) mod size in
99 val data = Array.create size ""
101 inherit basic_history (0, -1 , -1)
105 if tl = -1 then tl <- hd;
107 if hd = tl then tl <- incr tl;
110 if cur = tl then raise History_failure;
114 if cur = hd then raise History_failure;
116 if cur = hd then "" else data.(cur)
117 method load (data', hd', tl', cur') =
118 assert (Array.length data = Array.length data');
119 hd <- hd'; tl <- tl'; cur <- cur';
120 Array.blit data' 0 data 0 (Array.length data')
121 method save = (Array.copy data, hd, tl, cur)
124 class ['a] browser_history ?memento size init =
126 initializer match memento with Some m -> self#load m | _ -> ()
127 val data = Array.create size init
129 inherit basic_history (0, 0, 0)
132 if cur = tl then raise History_failure;
134 if cur = ~-1 then cur <- size - 1;
137 if cur = hd then raise History_failure;
139 if cur = size then cur <- 0;
143 if cur = size then cur <- 0;
144 if cur = tl then tl <- tl + 1;
145 if tl = size then tl <- 0;
148 method load (data', hd', tl', cur') =
149 assert (Array.length data = Array.length data');
150 hd <- hd'; tl <- tl'; cur <- cur';
151 Array.blit data' 0 data 0 (Array.length data')
152 method save = (Array.copy data, hd, tl, cur)
156 let instance = lazy (f ()) in
157 fun () -> Lazy.force instance
160 let errmsg = sprintf "Unable to create directory \"%s\"" d in
162 (match Unix.system ("mkdir -p " ^ d) with
163 | Unix.WEXITED 0 -> ()
164 | _ -> failwith errmsg)
165 with Unix.Unix_error _ -> failwith errmsg
167 let get_proof_status status =
168 match status.proof_status with
169 | Incomplete_proof s -> s
170 | _ -> statement_error "no ongoing proof"
172 let get_proof_metasenv status =
173 match status.proof_status with
175 | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
176 | Proof (_, metasenv, _, _) -> metasenv
177 | Intermediate m -> m
179 let get_proof_context status =
180 match status.proof_status with
181 | Incomplete_proof ((_, metasenv, _, _), goal) ->
182 let (_, context, _) = CicUtil.lookup_meta goal metasenv in
186 let get_proof_aliases status = status.aliases
188 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
190 let unopt = function None -> failwith "unopt: None" | Some v -> v