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