]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaTypes.ml
better dependencies among modules and symlinking of several matitatools to a single...
[helm.git] / helm / matita / matitaTypes.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   (** user hit the cancel button *)
29 exception Cancel
30
31   (** statement invoked in the wrong context (e.g. tactic with no ongoing proof)
32    *)
33 exception Statement_error of string
34 let statement_error msg = raise (Statement_error msg)
35
36 exception Command_error of string
37 let command_error msg = raise (Command_error msg)
38
39   (** parameters are: option name, error message *)
40 exception Option_error of string * string
41
42 exception Unbound_identifier of string
43
44 type incomplete_proof = {
45   proof: ProofEngineTypes.proof;
46   stack: Continuationals.Stack.t;
47 }
48
49 type proof_status =
50   | No_proof
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. *)
56
57 module StringMap = Map.Make (String)
58
59 type option_value =
60   | String of string
61   | Int of int
62 type options = option_value StringMap.t
63 let no_options = StringMap.empty
64
65 type ast_command = (CicNotationPt.term, GrafiteAst.obj) GrafiteAst.command
66 type moo = ast_command list * GrafiteAst.metadata list
67
68 type status = {
69   aliases: DisambiguateTypes.environment;
70   multi_aliases: DisambiguateTypes.multiple_environment;
71   moo_content_rev: moo;
72   proof_status: proof_status;
73   options: options;
74   objects: (UriManager.uri * string) list;
75   notation_ids: CicNotation.notation_id list;
76 }
77
78 let set_metasenv metasenv status =
79   let proof_status =
80     match status.proof_status with
81     | No_proof -> Intermediate metasenv
82     | Incomplete_proof ({ proof = (uri, _, proof, ty) } as incomplete_proof) ->
83         Incomplete_proof
84           { incomplete_proof with proof = (uri, metasenv, proof, ty) }
85     | Intermediate _ -> Intermediate metasenv 
86     | Proof _ -> assert false
87   in
88   { status with proof_status = proof_status }
89
90 let dump_status status = 
91   MatitaLog.message "status.aliases:\n";
92   MatitaLog.message "status.proof_status:"; 
93   MatitaLog.message
94     (match status.proof_status with
95     | No_proof -> "no proof\n"
96     | Incomplete_proof _ -> "incomplete proof\n"
97     | Proof _ -> "proof\n"
98     | Intermediate _ -> "Intermediate\n");
99   MatitaLog.message "status.options\n";
100   StringMap.iter (fun k v -> 
101     let v = 
102       match v with
103       | String s -> s
104       | Int i -> string_of_int i
105     in
106     MatitaLog.message (k ^ "::=" ^ v)) status.options;
107   MatitaLog.message "status.coercions\n";
108   MatitaLog.message "status.objects:\n";
109   List.iter 
110     (fun (u,_) -> 
111       MatitaLog.message (UriManager.string_of_uri u)) status.objects 
112   
113 let get_option status name =
114   try
115     StringMap.find name status.options
116   with Not_found -> raise (Option_error (name, "not found"))
117
118 let get_string_option status name =
119   match get_option status name with
120   | String s -> s
121   | _ -> raise (Option_error (name, "not a string value"))
122
123 let set_option status name value =
124   let mangle_dir s =
125     let s = Str.global_replace (Str.regexp "//+") "/" s in
126     let s = Str.global_replace (Str.regexp "/$") "" s in
127     s
128   in
129   let types =
130     [ "baseuri", (`String, mangle_dir);
131       "basedir", (`String, mangle_dir);
132     ]
133   in
134   let ty_and_mangler =
135     try
136       List.assoc name types
137     with Not_found -> command_error (sprintf "Unknown option \"%s\"" name)
138   in
139   let value =
140     match ty_and_mangler with
141     | `String, f -> String (f value)
142     | `Int, f ->
143         (try
144           Int (int_of_string (f value))
145         with Failure _ ->
146           command_error (sprintf "Not an integer value \"%s\"" value))
147   in
148   if StringMap.mem name status.options && name = "baseuri" then
149     command_error "Redefinition of 'baseuri' is forbidden."
150   else
151     { status with options = StringMap.add name value status.options }
152
153 let add_moo_content cmds status =
154   let content, metadata = status.moo_content_rev in
155   let content' =
156     List.fold_right
157       (fun cmd acc ->
158 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
159         match cmd with
160         | GrafiteAst.Interpretation _
161         | GrafiteAst.Default _ ->
162             if List.mem cmd content then acc
163             else cmd :: acc
164         | GrafiteAst.Alias _ -> (* move Alias as the last inserted one *)
165             cmd :: (List.filter ((<>) cmd) acc)
166         | _ -> cmd :: acc)
167       cmds content
168   in
169 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
170     GrafiteAstPp.pp_command content')); *)
171   { status with moo_content_rev = content', metadata }
172
173 let add_moo_metadata new_metadata status =
174   let content, metadata = status.moo_content_rev in
175   let metadata' =
176     List.fold_left
177       (fun acc m ->
178         match m with
179         | GrafiteAst.Dependency buri ->
180             let is_self = (* self dependency *)
181               try
182                 get_string_option status "baseuri" = buri
183               with Option_error _ -> false  (* baseuri not yet set *)
184             in
185             if is_self
186               || List.exists (GrafiteAst.eq_metadata m) metadata (* duplicate *)
187             then acc
188             else m :: acc
189         | _ -> m :: acc)
190       metadata new_metadata
191   in
192   { status with moo_content_rev = content, metadata' }
193
194   (* subset of MatitaConsole.console methods needed by MatitaInterpreter *)
195 class type console =
196   object
197     method clear : unit -> unit
198     method echo_error : string -> unit
199     method echo_message : string -> unit
200     method wrap_exn : 'a. (unit -> 'a) -> 'a option
201     method choose_uri : string list -> string
202     method show : ?msg:string -> unit -> unit
203   end
204
205 type abouts =
206   [ `Blank
207   | `Current_proof
208   | `Us
209   ]
210   
211 type mathViewer_entry =
212   [ `About of abouts  (* current proof *)
213   | `Check of string (* term *)
214   | `Cic of Cic.term * Cic.metasenv
215   | `Dir of string (* "directory" in cic uris namespace *)
216   | `Uri of UriManager.uri (* cic object uri *)
217   | `Whelp of string * UriManager.uri list (* query and results *)
218   ]
219
220 let string_of_entry = function
221   | `About `Blank -> "about:blank"
222   | `About `Current_proof -> "about:proof"
223   | `About `Us -> "about:us"
224   | `Check _ -> "check:"
225   | `Cic (_, _) -> "term:"
226   | `Dir uri -> uri
227   | `Uri uri -> UriManager.string_of_uri uri
228   | `Whelp (query, _) -> query
229
230 let entry_of_string = function
231   | "about:blank" -> `About `Blank
232   | "about:proof" -> `About `Current_proof
233   | "about:us"    -> `About `Us
234   | _ ->  (* only about entries supported ATM *)
235       raise (Invalid_argument "entry_of_string")
236
237 class type mathViewer =
238   object
239     (** @param reuse if set reused last opened cic browser otherwise 
240      *  opens a new one. default is false
241      *)
242     method show_entry: ?reuse:bool -> mathViewer_entry -> unit
243     method show_uri_list:
244       ?reuse:bool -> entry:mathViewer_entry -> UriManager.uri list -> unit
245   end
246   
247 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
248
249 let get_current_proof status =
250   match status.proof_status with
251   | Incomplete_proof { proof = p } -> p
252   | _ -> statement_error "no ongoing proof"
253
254 let get_proof_metasenv status =
255   match status.proof_status with
256   | No_proof -> []
257   | Proof (_, metasenv, _, _)
258   | Incomplete_proof { proof = (_, metasenv, _, _) }
259   | Intermediate metasenv ->
260       metasenv
261
262 let get_proof_context status goal =
263   match status.proof_status with
264   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
265       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
266       context
267   | _ -> []
268  
269 let get_proof_conclusion status goal =
270   match status.proof_status with
271   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
272       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
273       conclusion
274   | _ -> statement_error "no ongoing proof"
275
276 let get_stack status =
277   match status.proof_status with
278   | Incomplete_proof p -> p.stack
279   | Proof _ -> Continuationals.Stack.empty
280   | _ -> assert false
281
282 let set_stack stack status =
283   match status.proof_status with
284   | Incomplete_proof p ->
285       { status with proof_status = Incomplete_proof { p with stack = stack } }
286   | Proof _ ->
287       assert (Continuationals.Stack.is_empty stack);
288       status
289   | _ -> assert false
290