]> matita.cs.unibo.it Git - helm.git/blob - components/grafite_engine/grafiteTypes.ml
0d59e4b469351dc7d39e2b2da2b0c5996780df82
[helm.git] / components / 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   | 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 (_, metasenv', _, _) ->
100        assert (metasenv = metasenv');
101        status.proof_status
102   in
103   { status with proof_status = proof_status }
104
105 let get_proof_context status goal =
106   match status.proof_status with
107   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
108       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
109       context
110   | _ -> []
111
112 let get_proof_conclusion status goal =
113   match status.proof_status with
114   | Incomplete_proof { proof = (_, metasenv, _, _) } ->
115       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
116       conclusion
117   | _ -> raise (Statement_error "no ongoing proof")
118  
119 let add_moo_content cmds status =
120   let content = status.moo_content_rev in
121   let content' =
122     List.fold_right
123       (fun cmd acc ->
124 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
125         match cmd with
126         | GrafiteAst.Default _ ->
127             if List.mem cmd content then acc
128             else cmd :: acc
129         | _ -> cmd :: acc)
130       cmds content
131   in
132 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
133     GrafiteAstPp.pp_command content')); *)
134   { status with moo_content_rev = content' }
135
136 let get_option status name =
137   try
138     StringMap.find name status.options
139   with Not_found -> raise (Option_error (name, "not found"))
140
141 let set_option status name value =
142   let mangle_dir s =
143     let s = Str.global_replace (Str.regexp "//+") "/" s in
144     let s = Str.global_replace (Str.regexp "/$") "" s in
145     s
146   in
147   let types = [ "baseuri", (`String, mangle_dir); ] in
148   let ty_and_mangler =
149     try
150       List.assoc name types
151     with Not_found ->
152      command_error (Printf.sprintf "Unknown option \"%s\"" name)
153   in
154   let value =
155     match ty_and_mangler with
156     | `String, f -> String (f value)
157     | `Int, f ->
158         (try
159           Int (int_of_string (f value))
160         with Failure _ ->
161           command_error (Printf.sprintf "Not an integer value \"%s\"" value))
162   in
163   if StringMap.mem name status.options && name = "baseuri" then
164     command_error "Redefinition of 'baseuri' is forbidden."
165   else
166     { status with options = StringMap.add name value status.options }
167
168
169 let get_string_option status name =
170   match get_option status name with
171   | String s -> s
172   | _ -> raise (Option_error (name, "not a string value"))
173
174 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
175
176 let dump_status status = 
177   HLog.message "status.aliases:\n";
178   HLog.message "status.proof_status:"; 
179   HLog.message
180     (match status.proof_status with
181     | No_proof -> "no proof\n"
182     | Incomplete_proof _ -> "incomplete proof\n"
183     | Proof _ -> "proof\n"
184     | Intermediate _ -> "Intermediate\n");
185   HLog.message "status.options\n";
186   StringMap.iter (fun k v -> 
187     let v = 
188       match v with
189       | String s -> s
190       | Int i -> string_of_int i
191     in
192     HLog.message (k ^ "::=" ^ v)) status.options;
193   HLog.message "status.coercions\n";
194   HLog.message "status.objects:\n";
195   List.iter 
196     (fun u -> HLog.message (UriManager.string_of_uri u)) status.objects