]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite_engine/grafiteTypes.ml
a13cc074e81f7a12379f6404405542485e741c8b
[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 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   moo_content_rev: GrafiteMarshal.moo;
54   proof_status: proof_status;
55   options: options;
56   objects: UriManager.uri list;
57   coercions: UriManager.uri list;
58 }
59
60 let get_current_proof status =
61   match status.proof_status with
62   | Incomplete_proof { proof = p } -> p
63   | _ -> raise (Statement_error "no ongoing proof")
64
65 let get_proof_metasenv status =
66   match status.proof_status with
67   | No_proof -> []
68   | Proof (_, metasenv, _, _)
69   | Incomplete_proof { proof = (_, metasenv, _, _) }
70   | Intermediate metasenv ->
71       metasenv
72
73 let get_stack status =
74   match status.proof_status with
75   | Incomplete_proof p -> p.stack
76   | Proof _ -> Continuationals.Stack.empty
77   | _ -> assert false
78
79 let set_stack stack status =
80   match status.proof_status with
81   | Incomplete_proof p ->
82       { status with proof_status = Incomplete_proof { p with stack = stack } }
83   | Proof _ ->
84       assert (Continuationals.Stack.is_empty stack);
85       status
86   | _ -> assert false
87
88 let set_metasenv metasenv status =
89   let proof_status =
90     match status.proof_status with
91     | No_proof -> Intermediate metasenv
92     | Incomplete_proof ({ proof = (uri, _, proof, ty) } as incomplete_proof) ->
93         Incomplete_proof
94           { incomplete_proof with proof = (uri, metasenv, proof, ty) }
95     | Intermediate _ -> Intermediate metasenv 
96     | Proof (_, metasenv', _, _) ->
97        assert (metasenv = metasenv');
98        status.proof_status
99   in
100   { status with proof_status = proof_status }
101
102 let get_proof_context status goal =
103   match status.proof_status with
104   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
105       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
106       context
107   | _ -> []
108
109 let get_proof_conclusion status goal =
110   match status.proof_status with
111   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
112       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
113       conclusion
114   | _ -> raise (Statement_error "no ongoing proof")
115  
116 let add_moo_content cmds status =
117   let content = status.moo_content_rev in
118   let content' =
119     List.fold_right
120       (fun cmd acc ->
121 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
122         match cmd with
123         | GrafiteAst.Default _ ->
124             if List.mem cmd content then acc
125             else cmd :: acc
126         | _ -> cmd :: acc)
127       cmds content
128   in
129 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
130     GrafiteAstPp.pp_command content')); *)
131   { status with moo_content_rev = content' }
132
133 let get_option status name =
134   try
135     StringMap.find name status.options
136   with Not_found -> raise (Option_error (name, "not found"))
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 = [ "baseuri", (`String, mangle_dir); ] in
145   let ty_and_mangler =
146     try
147       List.assoc name types
148     with Not_found ->
149      command_error (Printf.sprintf "Unknown option \"%s\"" name)
150   in
151   let value =
152     match ty_and_mangler with
153     | `String, f -> String (f value)
154     | `Int, f ->
155         (try
156           Int (int_of_string (f value))
157         with Failure _ ->
158           command_error (Printf.sprintf "Not an integer value \"%s\"" value))
159   in
160   if StringMap.mem name status.options && name = "baseuri" then
161     command_error "Redefinition of 'baseuri' is forbidden."
162   else
163     { status with options = StringMap.add name value status.options }
164
165
166 let get_string_option status name =
167   match get_option status name with
168   | String s -> s
169   | _ -> raise (Option_error (name, "not a string value"))
170
171 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
172
173 let dump_status status = 
174   HLog.message "status.aliases:\n";
175   HLog.message "status.proof_status:"; 
176   HLog.message
177     (match status.proof_status with
178     | No_proof -> "no proof\n"
179     | Incomplete_proof _ -> "incomplete proof\n"
180     | Proof _ -> "proof\n"
181     | Intermediate _ -> "Intermediate\n");
182   HLog.message "status.options\n";
183   StringMap.iter (fun k v -> 
184     let v = 
185       match v with
186       | String s -> s
187       | Int i -> string_of_int i
188     in
189     HLog.message (k ^ "::=" ^ v)) status.options;
190   HLog.message "status.coercions\n";
191   HLog.message "status.objects:\n";
192   List.iter 
193     (fun u -> HLog.message (UriManager.string_of_uri u)) status.objects