]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite_engine/grafiteTypes.ml
test branch
[helm.git] / helm / ocaml / grafite_engine / 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 (* $Id$ *)
27
28 exception Option_error of string * string
29 exception Statement_error of string
30 exception Command_error of string
31
32 let command_error msg = raise (Command_error msg)
33
34 type incomplete_proof = {
35   proof: ProofEngineTypes.proof;
36   stack: Continuationals.Stack.t;
37 }
38
39 type proof_status =
40   | No_proof
41   | Incomplete_proof of incomplete_proof
42   | Proof of ProofEngineTypes.proof
43   | Intermediate of Cic.metasenv
44       (* Status in which the proof could be while it is being processed by the
45       * engine. No status entering/exiting the engine could be in it. *)
46
47 module StringMap = Map.Make (String)
48 type option_value =
49   | String of string
50   | Int of int
51 type options = option_value StringMap.t
52 let no_options = StringMap.empty
53
54 type status = {
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 }
61
62 let get_current_proof status =
63   match status.proof_status with
64   | Incomplete_proof { proof = p } -> p
65   | _ -> raise (Statement_error "no ongoing proof")
66
67 let get_proof_metasenv status =
68   match status.proof_status with
69   | No_proof -> []
70   | Proof (_, metasenv, _, _)
71   | Incomplete_proof { proof = (_, metasenv, _, _) }
72   | Intermediate metasenv ->
73       metasenv
74
75 let get_stack status =
76   match status.proof_status with
77   | Incomplete_proof p -> p.stack
78   | Proof _ -> Continuationals.Stack.empty
79   | _ -> assert false
80
81 let set_stack stack status =
82   match status.proof_status with
83   | Incomplete_proof p ->
84       { status with proof_status = Incomplete_proof { p with stack = stack } }
85   | Proof _ ->
86       assert (Continuationals.Stack.is_empty stack);
87       status
88   | _ -> assert false
89
90 let set_metasenv metasenv status =
91   let proof_status =
92     match status.proof_status with
93     | No_proof -> Intermediate metasenv
94     | Incomplete_proof ({ proof = (uri, _, proof, ty) } as incomplete_proof) ->
95         Incomplete_proof
96           { incomplete_proof with proof = (uri, metasenv, proof, ty) }
97     | Intermediate _ -> Intermediate metasenv 
98     | Proof (_, metasenv', _, _) ->
99        assert (metasenv = metasenv');
100        status.proof_status
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.Default _ ->
126             if List.mem cmd content then acc
127             else cmd :: acc
128         | _ -> cmd :: acc)
129       cmds content
130   in
131 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
132     GrafiteAstPp.pp_command content')); *)
133   { status with moo_content_rev = content' }
134
135 let get_option status name =
136   try
137     StringMap.find name status.options
138   with Not_found -> raise (Option_error (name, "not found"))
139
140 let set_option status name value =
141   let mangle_dir s =
142     let s = Str.global_replace (Str.regexp "//+") "/" s in
143     let s = Str.global_replace (Str.regexp "/$") "" s in
144     s
145   in
146   let types = [ "baseuri", (`String, mangle_dir); ] in
147   let ty_and_mangler =
148     try
149       List.assoc name types
150     with Not_found ->
151      command_error (Printf.sprintf "Unknown option \"%s\"" name)
152   in
153   let value =
154     match ty_and_mangler with
155     | `String, f -> String (f value)
156     | `Int, f ->
157         (try
158           Int (int_of_string (f value))
159         with Failure _ ->
160           command_error (Printf.sprintf "Not an integer value \"%s\"" value))
161   in
162   if StringMap.mem name status.options && name = "baseuri" then
163     command_error "Redefinition of 'baseuri' is forbidden."
164   else
165     { status with options = StringMap.add name value status.options }
166
167
168 let get_string_option status name =
169   match get_option status name with
170   | String s -> s
171   | _ -> raise (Option_error (name, "not a string value"))
172
173 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
174
175 let dump_status status = 
176   HLog.message "status.aliases:\n";
177   HLog.message "status.proof_status:"; 
178   HLog.message
179     (match status.proof_status with
180     | No_proof -> "no proof\n"
181     | Incomplete_proof _ -> "incomplete proof\n"
182     | Proof _ -> "proof\n"
183     | Intermediate _ -> "Intermediate\n");
184   HLog.message "status.options\n";
185   StringMap.iter (fun k v -> 
186     let v = 
187       match v with
188       | String s -> s
189       | Int i -> string_of_int i
190     in
191     HLog.message (k ^ "::=" ^ v)) status.options;
192   HLog.message "status.coercions\n";
193   HLog.message "status.objects:\n";
194   List.iter 
195     (fun u -> HLog.message (UriManager.string_of_uri u)) status.objects