]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite2/grafiteTypes.ml
Huge reorganization of matita and ocaml.
[helm.git] / helm / ocaml / grafite2 / grafiteTypes.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 exception Option_error of string * string
27 exception Statement_error of string
28 exception Command_error of string
29
30 let command_error msg = raise (Command_error msg)
31
32 type incomplete_proof = {
33   proof: ProofEngineTypes.proof;
34   stack: Continuationals.Stack.t;
35 }
36
37 type proof_status =
38   | No_proof
39   | Incomplete_proof of incomplete_proof
40   | Proof of ProofEngineTypes.proof
41   | Intermediate of Cic.metasenv
42       (* Status in which the proof could be while it is being processed by the
43       * engine. No status entering/exiting the engine could be in it. *)
44
45 module StringMap = Map.Make (String)
46 type option_value =
47   | String of string
48   | Int of int
49 type options = option_value StringMap.t
50 let no_options = StringMap.empty
51
52 type status = {
53   aliases: DisambiguateTypes.environment;
54   multi_aliases: DisambiguateTypes.multiple_environment;
55   moo_content_rev: GrafiteMarshal.moo;
56   metadata: LibraryNoDb.metadata list;
57   proof_status: proof_status;
58   options: options;
59   objects: UriManager.uri list;
60   coercions: UriManager.uri list;
61   notation_ids: CicNotation.notation_id list;
62 }
63
64 let get_current_proof status =
65   match status.proof_status with
66   | Incomplete_proof { proof = p } -> p
67   | _ -> raise (Statement_error "no ongoing proof")
68
69 let get_proof_metasenv status =
70   match status.proof_status with
71   | No_proof -> []
72   | Proof (_, metasenv, _, _)
73   | Incomplete_proof { proof = (_, metasenv, _, _) }
74   | Intermediate metasenv ->
75       metasenv
76
77 let get_stack status =
78   match status.proof_status with
79   | Incomplete_proof p -> p.stack
80   | Proof _ -> Continuationals.Stack.empty
81   | _ -> assert false
82
83 let set_stack stack status =
84   match status.proof_status with
85   | Incomplete_proof p ->
86       { status with proof_status = Incomplete_proof { p with stack = stack } }
87   | Proof _ ->
88       assert (Continuationals.Stack.is_empty stack);
89       status
90   | _ -> assert false
91
92 let set_metasenv metasenv status =
93   let proof_status =
94     match status.proof_status with
95     | No_proof -> Intermediate metasenv
96     | Incomplete_proof ({ proof = (uri, _, proof, ty) } as incomplete_proof) ->
97         Incomplete_proof
98           { incomplete_proof with proof = (uri, metasenv, proof, ty) }
99     | Intermediate _ -> Intermediate metasenv 
100     | Proof _ -> assert false
101   in
102   { status with proof_status = proof_status }
103
104 let get_proof_context status goal =
105   match status.proof_status with
106   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
107       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
108       context
109   | _ -> []
110
111 let get_proof_conclusion status goal =
112   match status.proof_status with
113   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
114       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
115       conclusion
116   | _ -> raise (Statement_error "no ongoing proof")
117  
118 let add_moo_content cmds status =
119   let content = status.moo_content_rev in
120   let content' =
121     List.fold_right
122       (fun cmd acc ->
123 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
124         match cmd with
125         | GrafiteAst.Interpretation _
126         | GrafiteAst.Default _ ->
127             if List.mem cmd content then acc
128             else cmd :: acc
129         | GrafiteAst.Alias _ -> (* move Alias as the last inserted one *)
130             cmd :: (List.filter ((<>) cmd) acc)
131         | _ -> cmd :: acc)
132       cmds content
133   in
134 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
135     GrafiteAstPp.pp_command content')); *)
136   { status with moo_content_rev = content' }
137
138 let get_option status name =
139   try
140     StringMap.find name status.options
141   with Not_found -> raise (Option_error (name, "not found"))
142
143 let set_option status name value =
144   let mangle_dir s =
145     let s = Str.global_replace (Str.regexp "//+") "/" s in
146     let s = Str.global_replace (Str.regexp "/$") "" s in
147     s
148   in
149   let types = [ "baseuri", (`String, mangle_dir); ] in
150   let ty_and_mangler =
151     try
152       List.assoc name types
153     with Not_found ->
154      command_error (Printf.sprintf "Unknown option \"%s\"" name)
155   in
156   let value =
157     match ty_and_mangler with
158     | `String, f -> String (f value)
159     | `Int, f ->
160         (try
161           Int (int_of_string (f value))
162         with Failure _ ->
163           command_error (Printf.sprintf "Not an integer value \"%s\"" value))
164   in
165   if StringMap.mem name status.options && name = "baseuri" then
166     command_error "Redefinition of 'baseuri' is forbidden."
167   else
168     { status with options = StringMap.add name value status.options }
169
170
171 let get_string_option status name =
172   match get_option status name with
173   | String s -> s
174   | _ -> raise (Option_error (name, "not a string value"))
175
176 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
177
178 let add_metadata new_metadata status =
179   if Helm_registry.get_bool "db.nodb" then
180     let metadata = status.metadata in
181     let metadata' =
182       List.fold_left
183         (fun acc m ->
184           match m with
185           | LibraryNoDb.Dependency buri ->
186               let is_self = (* self dependency *)
187                 try
188                   get_string_option status "baseuri" = buri
189                 with Option_error _ -> false  (* baseuri not yet set *)
190               in
191               if is_self (* duplicate *)
192                 || List.exists (LibraryNoDb.eq_metadata m) metadata
193               then acc
194               else m :: acc
195           | _ -> m :: acc)
196         metadata new_metadata
197     in
198     { status with metadata = metadata' }
199   else
200     status
201
202 let dump_status status = 
203   HLog.message "status.aliases:\n";
204   HLog.message "status.proof_status:"; 
205   HLog.message
206     (match status.proof_status with
207     | No_proof -> "no proof\n"
208     | Incomplete_proof _ -> "incomplete proof\n"
209     | Proof _ -> "proof\n"
210     | Intermediate _ -> "Intermediate\n");
211   HLog.message "status.options\n";
212   StringMap.iter (fun k v -> 
213     let v = 
214       match v with
215       | String s -> s
216       | Int i -> string_of_int i
217     in
218     HLog.message (k ^ "::=" ^ v)) status.options;
219   HLog.message "status.coercions\n";
220   HLog.message "status.objects:\n";
221   List.iter 
222     (fun u -> HLog.message (UriManager.string_of_uri u)) status.objects