]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaTypes.ml
Yet another implementation of the single aliases / multi aliases idea.
[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 proof_status =
45   | No_proof
46   | Incomplete_proof of ProofEngineTypes.status
47   | Proof of ProofEngineTypes.proof
48   | Intermediate of Cic.metasenv
49       (* Status in which the proof could be while it is being processed by the
50       * engine. No status entering/exiting the engine could be in it. *)
51
52 module StringMap = Map.Make (String)
53
54 type option_value =
55   | String of string
56   | Int of int
57 type options = option_value StringMap.t
58 let no_options = StringMap.empty
59
60 type ast_command = (CicNotationPt.term, GrafiteAst.obj) GrafiteAst.command
61
62 type status = {
63   aliases: DisambiguateTypes.environment;
64   multi_aliases: DisambiguateTypes.multiple_environment;
65   moo_content_rev: ast_command list;
66   proof_status: proof_status;
67   options: options;
68   objects: (UriManager.uri * string) list;
69   notation_ids: CicNotation.notation_id list;
70 }
71
72 let set_metasenv metasenv status =
73   let proof_status =
74     match status.proof_status with
75     | No_proof -> Intermediate metasenv
76     | Incomplete_proof ((uri, _, proof, ty), goal) ->
77         Incomplete_proof ((uri, metasenv, proof, ty), goal)
78     | Intermediate _ -> Intermediate metasenv 
79     | Proof _ -> assert false
80   in
81   { status with proof_status = proof_status }
82
83 let add_moo_content cmds status =
84   let content = status.moo_content_rev in
85   let content' =
86     List.fold_right
87       (fun cmd acc ->
88 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
89         match cmd with
90         | GrafiteAst.Interpretation _
91         | GrafiteAst.Default _ ->
92             if List.mem cmd content then acc
93             else cmd :: acc
94         | GrafiteAst.Alias _ -> (* move Alias as the last inserted one *)
95             cmd :: (List.filter ((<>) cmd) acc)
96         | _ -> cmd :: acc)
97       cmds content
98   in
99 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
100     GrafiteAstPp.pp_command content')); *)
101   { status with moo_content_rev = content' }
102
103 let dump_status status = 
104   MatitaLog.message "status.aliases:\n";
105   MatitaLog.message
106   (DisambiguatePp.pp_environment status.aliases ^ "\n");
107   MatitaLog.message "status.proof_status:"; 
108   MatitaLog.message
109     (match status.proof_status with
110     | No_proof -> "no proof\n"
111     | Incomplete_proof _ -> "incomplete proof\n"
112     | Proof _ -> "proof\n"
113     | Intermediate _ -> "Intermediate\n");
114   MatitaLog.message "status.options\n";
115   StringMap.iter (fun k v -> 
116     let v = 
117       match v with
118       | String s -> s
119       | Int i -> string_of_int i
120     in
121     MatitaLog.message (k ^ "::=" ^ v)) status.options;
122   MatitaLog.message "status.coercions\n";
123   MatitaLog.message "status.objects:\n";
124   List.iter 
125     (fun (u,_) -> 
126       MatitaLog.message (UriManager.string_of_uri u)) status.objects 
127   
128
129 let get_option status name =
130   try
131     StringMap.find name status.options
132   with Not_found -> raise (Option_error (name, "not found"))
133
134 let get_string_option status name =
135   match get_option status name with
136   | String s -> s
137   | _ -> raise (Option_error (name, "not a string value"))
138
139 let set_option status name value =
140   let mangle_dir s =
141     let s = Str.global_replace (Str.regexp "//+") "/" s in
142     let s = Str.global_replace (Str.regexp "/$") "" s in
143     s
144   in
145   let types =
146     [ "baseuri", (`String, mangle_dir);
147       "basedir", (`String, mangle_dir);
148     ]
149   in
150   let ty_and_mangler =
151     try
152       List.assoc name types
153     with Not_found -> command_error (sprintf "Unknown option \"%s\"" name)
154   in
155   let value =
156     match ty_and_mangler with
157     | `String, f -> String (f value)
158     | `Int, f ->
159         (try
160           Int (int_of_string (f value))
161         with Failure _ ->
162           command_error (sprintf "Not an integer value \"%s\"" value))
163   in
164   if StringMap.mem name status.options && name = "baseuri" then
165     command_error "Redefinition of 'baseuri' is forbidden."
166   else
167     { status with options = StringMap.add name value status.options }
168
169   (* subset of MatitaConsole.console methods needed by MatitaInterpreter *)
170 class type console =
171   object
172     method clear : unit -> unit
173     method echo_error : string -> unit
174     method echo_message : string -> unit
175     method wrap_exn : 'a. (unit -> 'a) -> 'a option
176     method choose_uri : string list -> string
177     method show : ?msg:string -> unit -> unit
178   end
179
180 type abouts =
181   [ `Blank
182   | `Current_proof
183   | `Us
184   ]
185   
186 type mathViewer_entry =
187   [ `About of abouts  (* current proof *)
188   | `Check of string (* term *)
189   | `Cic of Cic.term * Cic.metasenv
190   | `Dir of string (* "directory" in cic uris namespace *)
191   | `Uri of UriManager.uri (* cic object uri *)
192   | `Whelp of string * UriManager.uri list (* query and results *)
193   ]
194
195 let string_of_entry = function
196   | `About `Blank -> "about:blank"
197   | `About `Current_proof -> "about:proof"
198   | `About `Us -> "about:us"
199   | `Check _ -> "check:"
200   | `Cic (_, _) -> "term:"
201   | `Dir uri -> uri
202   | `Uri uri -> UriManager.string_of_uri uri
203   | `Whelp (query, _) -> query
204
205 let entry_of_string = function
206   | "about:blank" -> `About `Blank
207   | "about:proof" -> `About `Current_proof
208   | "about:us"    -> `About `Us
209   | _ ->  (* only about entries supported ATM *)
210       raise (Invalid_argument "entry_of_string")
211
212 class type mathViewer =
213   object
214     (** @param reuse if set reused last opened cic browser otherwise 
215      *  opens a new one. default is false
216      *)
217     method show_entry: ?reuse:bool -> mathViewer_entry -> unit
218     method show_uri_list:
219       ?reuse:bool -> entry:mathViewer_entry -> UriManager.uri list -> unit
220   end
221