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/
28 (** user hit the cancel button *)
31 (** statement invoked in the wrong context (e.g. tactic with no ongoing proof)
33 exception Statement_error of string
34 let statement_error msg = raise (Statement_error msg)
36 exception Command_error of string
37 let command_error msg = raise (Command_error msg)
39 (** parameters are: option name, error message *)
40 exception Option_error of string * string
42 exception Unbound_identifier of string
44 type incomplete_proof = {
45 proof: ProofEngineTypes.proof;
46 stack: Continuationals.Stack.t;
51 | Incomplete_proof of incomplete_proof
52 | Proof of ProofEngineTypes.proof
53 | Intermediate of Cic.metasenv
54 (* Status in which the proof could be while it is being processed by the
55 * engine. No status entering/exiting the engine could be in it. *)
57 module StringMap = Map.Make (String)
62 type options = option_value StringMap.t
63 let no_options = StringMap.empty
65 type ast_command = (CicNotationPt.term, CicNotationPt.obj) GrafiteAst.command
66 type moo = ast_command list * GrafiteAst.metadata list
69 aliases: DisambiguateTypes.environment;
70 multi_aliases: DisambiguateTypes.multiple_environment;
72 proof_status: proof_status;
74 objects: (UriManager.uri * string) list;
75 coercions: UriManager.uri list;
76 notation_ids: CicNotation.notation_id list;
79 let set_metasenv metasenv status =
81 match status.proof_status with
82 | No_proof -> Intermediate metasenv
83 | Incomplete_proof ({ proof = (uri, _, proof, ty) } as incomplete_proof) ->
85 { incomplete_proof with proof = (uri, metasenv, proof, ty) }
86 | Intermediate _ -> Intermediate metasenv
87 | Proof _ -> assert false
89 { status with proof_status = proof_status }
91 let dump_status status =
92 MatitaLog.message "status.aliases:\n";
93 MatitaLog.message "status.proof_status:";
95 (match status.proof_status with
96 | No_proof -> "no proof\n"
97 | Incomplete_proof _ -> "incomplete proof\n"
98 | Proof _ -> "proof\n"
99 | Intermediate _ -> "Intermediate\n");
100 MatitaLog.message "status.options\n";
101 StringMap.iter (fun k v ->
105 | Int i -> string_of_int i
107 MatitaLog.message (k ^ "::=" ^ v)) status.options;
108 MatitaLog.message "status.coercions\n";
109 MatitaLog.message "status.objects:\n";
112 MatitaLog.message (UriManager.string_of_uri u)) status.objects
114 let get_option status name =
116 StringMap.find name status.options
117 with Not_found -> raise (Option_error (name, "not found"))
119 let get_string_option status name =
120 match get_option status name with
122 | _ -> raise (Option_error (name, "not a string value"))
124 let set_option status name value =
126 let s = Str.global_replace (Str.regexp "//+") "/" s in
127 let s = Str.global_replace (Str.regexp "/$") "" s in
131 [ "baseuri", (`String, mangle_dir);
132 "basedir", (`String, mangle_dir);
137 List.assoc name types
138 with Not_found -> command_error (sprintf "Unknown option \"%s\"" name)
141 match ty_and_mangler with
142 | `String, f -> String (f value)
145 Int (int_of_string (f value))
147 command_error (sprintf "Not an integer value \"%s\"" value))
149 if StringMap.mem name status.options && name = "baseuri" then
150 command_error "Redefinition of 'baseuri' is forbidden."
152 { status with options = StringMap.add name value status.options }
154 let add_moo_content cmds status =
155 let content, metadata = status.moo_content_rev in
159 (* prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
161 | GrafiteAst.Interpretation _
162 | GrafiteAst.Default _ ->
163 if List.mem cmd content then acc
165 | GrafiteAst.Alias _ -> (* move Alias as the last inserted one *)
166 cmd :: (List.filter ((<>) cmd) acc)
170 (* prerr_endline ("new moo content: " ^ String.concat " " (List.map
171 GrafiteAstPp.pp_command content')); *)
172 { status with moo_content_rev = content', metadata }
174 let add_moo_metadata new_metadata status =
175 let content, metadata = status.moo_content_rev in
180 | GrafiteAst.Dependency buri ->
181 let is_self = (* self dependency *)
183 get_string_option status "baseuri" = buri
184 with Option_error _ -> false (* baseuri not yet set *)
187 || List.exists (GrafiteAst.eq_metadata m) metadata (* duplicate *)
191 metadata new_metadata
193 { status with moo_content_rev = content, metadata' }
195 (* subset of MatitaConsole.console methods needed by MatitaInterpreter *)
198 method clear : unit -> unit
199 method echo_error : string -> unit
200 method echo_message : string -> unit
201 method wrap_exn : 'a. (unit -> 'a) -> 'a option
202 method choose_uri : string list -> string
203 method show : ?msg:string -> unit -> unit
212 type mathViewer_entry =
213 [ `About of abouts (* current proof *)
214 | `Check of string (* term *)
215 | `Cic of Cic.term * Cic.metasenv
216 | `Dir of string (* "directory" in cic uris namespace *)
217 | `Uri of UriManager.uri (* cic object uri *)
218 | `Whelp of string * UriManager.uri list (* query and results *)
221 let string_of_entry = function
222 | `About `Blank -> "about:blank"
223 | `About `Current_proof -> "about:proof"
224 | `About `Us -> "about:us"
225 | `Check _ -> "check:"
226 | `Cic (_, _) -> "term:"
228 | `Uri uri -> UriManager.string_of_uri uri
229 | `Whelp (query, _) -> query
231 let entry_of_string = function
232 | "about:blank" -> `About `Blank
233 | "about:proof" -> `About `Current_proof
234 | "about:us" -> `About `Us
235 | _ -> (* only about entries supported ATM *)
236 raise (Invalid_argument "entry_of_string")
238 class type mathViewer =
240 (** @param reuse if set reused last opened cic browser otherwise
241 * opens a new one. default is false
243 method show_entry: ?reuse:bool -> mathViewer_entry -> unit
244 method show_uri_list:
245 ?reuse:bool -> entry:mathViewer_entry -> UriManager.uri list -> unit
248 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
250 let get_current_proof status =
251 match status.proof_status with
252 | Incomplete_proof { proof = p } -> p
253 | _ -> statement_error "no ongoing proof"
255 let get_proof_metasenv status =
256 match status.proof_status with
258 | Proof (_, metasenv, _, _)
259 | Incomplete_proof { proof = (_, metasenv, _, _) }
260 | Intermediate metasenv ->
263 let get_proof_context status goal =
264 match status.proof_status with
265 | Incomplete_proof { proof = (_, metasenv, _, _) } ->
266 let (_, context, _) = CicUtil.lookup_meta goal metasenv in
270 let get_proof_conclusion status goal =
271 match status.proof_status with
272 | Incomplete_proof { proof = (_, metasenv, _, _) } ->
273 let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
275 | _ -> statement_error "no ongoing proof"
277 let get_stack status =
278 match status.proof_status with
279 | Incomplete_proof p -> p.stack
280 | Proof _ -> Continuationals.Stack.empty
283 let set_stack stack status =
284 match status.proof_status with
285 | Incomplete_proof p ->
286 { status with proof_status = Incomplete_proof { p with stack = stack } }
288 assert (Continuationals.Stack.is_empty stack);