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