]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaTypes.ml
new tacticals
[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
93   (DisambiguatePp.pp_environment status.aliases ^ "\n");
94   MatitaLog.message "status.proof_status:"; 
95   MatitaLog.message
96     (match status.proof_status with
97     | No_proof -> "no proof\n"
98     | Incomplete_proof _ -> "incomplete proof\n"
99     | Proof _ -> "proof\n"
100     | Intermediate _ -> "Intermediate\n");
101   MatitaLog.message "status.options\n";
102   StringMap.iter (fun k v -> 
103     let v = 
104       match v with
105       | String s -> s
106       | Int i -> string_of_int i
107     in
108     MatitaLog.message (k ^ "::=" ^ v)) status.options;
109   MatitaLog.message "status.coercions\n";
110   MatitaLog.message "status.objects:\n";
111   List.iter 
112     (fun (u,_) -> 
113       MatitaLog.message (UriManager.string_of_uri u)) status.objects 
114   
115 let get_option status name =
116   try
117     StringMap.find name status.options
118   with Not_found -> raise (Option_error (name, "not found"))
119
120 let get_string_option status name =
121   match get_option status name with
122   | String s -> s
123   | _ -> raise (Option_error (name, "not a string value"))
124
125 let set_option status name value =
126   let mangle_dir s =
127     let s = Str.global_replace (Str.regexp "//+") "/" s in
128     let s = Str.global_replace (Str.regexp "/$") "" s in
129     s
130   in
131   let types =
132     [ "baseuri", (`String, mangle_dir);
133       "basedir", (`String, mangle_dir);
134     ]
135   in
136   let ty_and_mangler =
137     try
138       List.assoc name types
139     with Not_found -> command_error (sprintf "Unknown option \"%s\"" name)
140   in
141   let value =
142     match ty_and_mangler with
143     | `String, f -> String (f value)
144     | `Int, f ->
145         (try
146           Int (int_of_string (f value))
147         with Failure _ ->
148           command_error (sprintf "Not an integer value \"%s\"" value))
149   in
150   if StringMap.mem name status.options && name = "baseuri" then
151     command_error "Redefinition of 'baseuri' is forbidden."
152   else
153     { status with options = StringMap.add name value status.options }
154
155 let add_moo_content cmds status =
156   let content, metadata = status.moo_content_rev in
157   let content' =
158     List.fold_right
159       (fun cmd acc ->
160 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
161         match cmd with
162         | GrafiteAst.Interpretation _
163         | GrafiteAst.Default _ ->
164             if List.mem cmd content then acc
165             else cmd :: acc
166         | GrafiteAst.Alias _ -> (* move Alias as the last inserted one *)
167             cmd :: (List.filter ((<>) cmd) acc)
168         | _ -> cmd :: acc)
169       cmds content
170   in
171 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
172     GrafiteAstPp.pp_command content')); *)
173   { status with moo_content_rev = content', metadata }
174
175 let add_moo_metadata new_metadata status =
176   let content, metadata = status.moo_content_rev in
177   let metadata' =
178     List.fold_left
179       (fun acc m ->
180         match m with
181         | GrafiteAst.Dependency buri ->
182             let is_self = (* self dependency *)
183               try
184                 get_string_option status "baseuri" = buri
185               with Option_error _ -> false  (* baseuri not yet set *)
186             in
187             if is_self
188               || List.exists (GrafiteAst.eq_metadata m) metadata (* duplicate *)
189             then acc
190             else m :: acc
191         | _ -> m :: acc)
192       metadata new_metadata
193   in
194   { status with moo_content_rev = content, metadata' }
195
196   (* subset of MatitaConsole.console methods needed by MatitaInterpreter *)
197 class type console =
198   object
199     method clear : unit -> unit
200     method echo_error : string -> unit
201     method echo_message : string -> unit
202     method wrap_exn : 'a. (unit -> 'a) -> 'a option
203     method choose_uri : string list -> string
204     method show : ?msg:string -> unit -> unit
205   end
206
207 type abouts =
208   [ `Blank
209   | `Current_proof
210   | `Us
211   ]
212   
213 type mathViewer_entry =
214   [ `About of abouts  (* current proof *)
215   | `Check of string (* term *)
216   | `Cic of Cic.term * Cic.metasenv
217   | `Dir of string (* "directory" in cic uris namespace *)
218   | `Uri of UriManager.uri (* cic object uri *)
219   | `Whelp of string * UriManager.uri list (* query and results *)
220   ]
221
222 let string_of_entry = function
223   | `About `Blank -> "about:blank"
224   | `About `Current_proof -> "about:proof"
225   | `About `Us -> "about:us"
226   | `Check _ -> "check:"
227   | `Cic (_, _) -> "term:"
228   | `Dir uri -> uri
229   | `Uri uri -> UriManager.string_of_uri uri
230   | `Whelp (query, _) -> query
231
232 let entry_of_string = function
233   | "about:blank" -> `About `Blank
234   | "about:proof" -> `About `Current_proof
235   | "about:us"    -> `About `Us
236   | _ ->  (* only about entries supported ATM *)
237       raise (Invalid_argument "entry_of_string")
238
239 class type mathViewer =
240   object
241     (** @param reuse if set reused last opened cic browser otherwise 
242      *  opens a new one. default is false
243      *)
244     method show_entry: ?reuse:bool -> mathViewer_entry -> unit
245     method show_uri_list:
246       ?reuse:bool -> entry:mathViewer_entry -> UriManager.uri list -> unit
247   end
248