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