]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite2/grafiteTypes.ml
1. matitaEngine splitted into disambiguation (now in grafite_parser) and
[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   proof_status: proof_status;
57   options: options;
58   objects: UriManager.uri list;
59   coercions: UriManager.uri list;
60   notation_ids: CicNotation.notation_id list;
61 }
62
63 let get_current_proof status =
64   match status.proof_status with
65   | Incomplete_proof { proof = p } -> p
66   | _ -> raise (Statement_error "no ongoing proof")
67
68 let get_proof_metasenv status =
69   match status.proof_status with
70   | No_proof -> []
71   | Proof (_, metasenv, _, _)
72   | Incomplete_proof { proof = (_, metasenv, _, _) }
73   | Intermediate metasenv ->
74       metasenv
75
76 let get_stack status =
77   match status.proof_status with
78   | Incomplete_proof p -> p.stack
79   | Proof _ -> Continuationals.Stack.empty
80   | _ -> assert false
81
82 let set_stack stack status =
83   match status.proof_status with
84   | Incomplete_proof p ->
85       { status with proof_status = Incomplete_proof { p with stack = stack } }
86   | Proof _ ->
87       assert (Continuationals.Stack.is_empty stack);
88       status
89   | _ -> assert false
90
91 let set_metasenv metasenv status =
92   let proof_status =
93     match status.proof_status with
94     | No_proof -> Intermediate metasenv
95     | Incomplete_proof ({ proof = (uri, _, proof, ty) } as incomplete_proof) ->
96         Incomplete_proof
97           { incomplete_proof with proof = (uri, metasenv, proof, ty) }
98     | Intermediate _ -> Intermediate metasenv 
99     | Proof _ -> assert false
100   in
101   { status with proof_status = proof_status }
102
103 let get_proof_context status goal =
104   match status.proof_status with
105   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
106       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
107       context
108   | _ -> []
109
110 let get_proof_conclusion status goal =
111   match status.proof_status with
112   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
113       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
114       conclusion
115   | _ -> raise (Statement_error "no ongoing proof")
116  
117 let add_moo_content cmds status =
118   let content, metadata = status.moo_content_rev in
119   let content' =
120     List.fold_right
121       (fun cmd acc ->
122 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
123         match cmd with
124         | GrafiteAst.Interpretation _
125         | GrafiteAst.Default _ ->
126             if List.mem cmd content then acc
127             else cmd :: acc
128         | GrafiteAst.Alias _ -> (* move Alias as the last inserted one *)
129             cmd :: (List.filter ((<>) cmd) acc)
130         | _ -> cmd :: acc)
131       cmds content
132   in
133 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
134     GrafiteAstPp.pp_command content')); *)
135   { status with moo_content_rev = content', metadata }
136
137 let get_option status name =
138   try
139     StringMap.find name status.options
140   with Not_found -> raise (Option_error (name, "not found"))
141
142 let set_option status name value =
143   let mangle_dir s =
144     let s = Str.global_replace (Str.regexp "//+") "/" s in
145     let s = Str.global_replace (Str.regexp "/$") "" s in
146     s
147   in
148   let types = [ "baseuri", (`String, mangle_dir); ] in
149   let ty_and_mangler =
150     try
151       List.assoc name types
152     with Not_found ->
153      command_error (Printf.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 (Printf.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
170 let get_string_option status name =
171   match get_option status name with
172   | String s -> s
173   | _ -> raise (Option_error (name, "not a string value"))
174
175 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
176
177 let add_moo_metadata new_metadata status =
178   let content, metadata = status.moo_content_rev in
179   let metadata' =
180     List.fold_left
181       (fun acc m ->
182         match m with
183         | GrafiteAst.Dependency buri ->
184             let is_self = (* self dependency *)
185               try
186                 get_string_option status "baseuri" = buri
187               with Option_error _ -> false  (* baseuri not yet set *)
188             in
189             if is_self
190               || List.exists (GrafiteAst.eq_metadata m) metadata (* duplicate *)
191             then acc
192             else m :: acc
193         | _ -> m :: acc)
194       metadata new_metadata
195   in
196   { status with moo_content_rev = content, metadata' }
197
198 let dump_status status = 
199   HLog.message "status.aliases:\n";
200   HLog.message "status.proof_status:"; 
201   HLog.message
202     (match status.proof_status with
203     | No_proof -> "no proof\n"
204     | Incomplete_proof _ -> "incomplete proof\n"
205     | Proof _ -> "proof\n"
206     | Intermediate _ -> "Intermediate\n");
207   HLog.message "status.options\n";
208   StringMap.iter (fun k v -> 
209     let v = 
210       match v with
211       | String s -> s
212       | Int i -> string_of_int i
213     in
214     HLog.message (k ^ "::=" ^ v)) status.options;
215   HLog.message "status.coercions\n";
216   HLog.message "status.objects:\n";
217   List.iter 
218     (fun u -> HLog.message (UriManager.string_of_uri u)) status.objects