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