]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/grafite_engine/grafiteTypes.ml
Initial implementation of statuses using objects in place of nested records.
[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 type ng_status =
48   | ProofMode of NTacStatus.tac_status
49   | CommandMode of NEstatus.extra_status
50
51 type status = {
52   moo_content_rev: GrafiteMarshal.moo;
53   proof_status: proof_status;
54   objects: UriManager.uri list;
55   coercions: CoercDb.coerc_db;
56   automation_cache:AutomationCache.cache;  
57   baseuri: string;
58   ng_status: ng_status;
59 }
60
61 let get_lexicon x = 
62   match x.ng_status with
63   | ProofMode t -> t.NTacStatus.istatus.NTacStatus.estatus.NEstatus.lstatus
64   | CommandMode e -> e.NEstatus.lstatus
65 ;;
66  
67 let set_lexicon new_lexicon_status new_grafite_status = 
68   { new_grafite_status with ng_status =
69    match new_grafite_status.ng_status with
70    | CommandMode estatus -> 
71       CommandMode 
72         { estatus with NEstatus.lstatus = new_lexicon_status }
73    | ProofMode t -> 
74        ProofMode 
75         { t with NTacStatus.istatus = 
76           { t.NTacStatus.istatus with NTacStatus.estatus = 
77             { t.NTacStatus.istatus.NTacStatus.estatus with NEstatus.lstatus =
78                new_lexicon_status }}}}
79 ;; 
80
81 let get_estatus x = 
82   match x.ng_status with
83   | ProofMode t -> t.NTacStatus.istatus.NTacStatus.estatus
84   | CommandMode e -> e
85 ;;
86
87 let set_estatus e x =
88  { x with ng_status =
89    match x.ng_status with
90       ProofMode t ->
91        ProofMode
92         {t with NTacStatus.istatus =
93          {t.NTacStatus.istatus with NTacStatus.estatus = e}}
94     | CommandMode _ -> CommandMode e}
95 ;;
96
97 let get_dstatus x = (get_estatus x).NEstatus.rstatus;;
98
99 let set_dstatus h x =
100  let estatus = get_estatus x in
101   set_estatus { estatus with NEstatus.rstatus = h } x
102 ;;
103
104 let get_current_proof status =
105   match status.proof_status with
106   | Incomplete_proof { proof = p } -> p
107   | Proof p -> p
108   | _ -> raise (Statement_error "no ongoing proof")
109
110 let get_proof_metasenv status =
111   match status.proof_status with
112   | No_proof -> []
113   | Proof (_, metasenv, _, _, _, _)
114   | Incomplete_proof { proof = (_, metasenv, _, _, _, _) }
115   | Intermediate metasenv ->
116       metasenv
117
118 let get_stack status =
119   match status.proof_status with
120   | Incomplete_proof p -> p.stack
121   | Proof _ -> Continuationals.Stack.empty
122   | _ -> assert false
123
124 let set_stack stack status =
125   match status.proof_status with
126   | Incomplete_proof p ->
127       { status with proof_status = Incomplete_proof { p with stack = stack } }
128   | Proof _ ->
129       assert (Continuationals.Stack.is_empty stack);
130       status
131   | _ -> assert false
132
133 let set_metasenv metasenv status =
134   let proof_status =
135     match status.proof_status with
136     | No_proof -> Intermediate metasenv
137     | Incomplete_proof ({ proof = (uri, _, subst, proof, ty, attrs) } as incomplete_proof) ->
138         Incomplete_proof
139           { incomplete_proof with proof = (uri, metasenv, subst, proof, ty, attrs) }
140     | Intermediate _ -> Intermediate metasenv 
141     | Proof (_, metasenv', _, _, _, _) ->
142        assert (metasenv = metasenv');
143        status.proof_status
144   in
145   { status with proof_status = proof_status }
146
147 let get_proof_context status goal =
148   match status.proof_status with
149   | Incomplete_proof { proof = (_, metasenv, _, _, _, _) } ->
150       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
151       context
152   | _ -> []
153
154 let get_proof_conclusion status goal =
155   match status.proof_status with
156   | Incomplete_proof { proof = (_, metasenv, _, _, _, _) } ->
157       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
158       conclusion
159   | _ -> raise (Statement_error "no ongoing proof")
160  
161 let add_moo_content cmds status =
162   let content = status.moo_content_rev in
163   let content' =
164     List.fold_right
165       (fun cmd acc ->
166 (*         prerr_endline ("adding to moo command: " ^ GrafiteAstPp.pp_command cmd); *)
167         match cmd with
168         | GrafiteAst.Default _ 
169         | GrafiteAst.Index _
170         | GrafiteAst.Coercion _ ->
171             if List.mem cmd content then acc
172             else cmd :: acc
173         | _ -> cmd :: acc)
174       cmds content
175   in
176 (*   prerr_endline ("new moo content: " ^ String.concat " " (List.map
177     GrafiteAstPp.pp_command content')); *)
178   { status with moo_content_rev = content' }
179
180 let get_baseuri status = status.baseuri;;
181
182 (*
183 let get_option status name =
184   try
185     StringMap.find name status.options
186   with Not_found -> raise (Option_error (name, "not found"))
187  
188 let set_option status name value =
189   let types = [ (* no set options defined! *) ] in
190   let ty_and_mangler =
191     try List.assoc name types
192     with Not_found ->
193      command_error (Printf.sprintf "Unknown option \"%s\"" name)
194   in
195   let value =
196     match ty_and_mangler with
197     | `String, f -> String (f value)
198     | `Int, f ->
199         (try
200           Int (int_of_string (f value))
201         with Failure _ ->
202           command_error (Printf.sprintf "Not an integer value \"%s\"" value))
203   in
204     { status with options = StringMap.add name value status.options }
205
206
207 let get_string_option status name =
208   match get_option status name with
209   | String s -> s
210   | _ -> raise (Option_error (name, "not a string value"))
211 *)
212
213 let dump_status status = 
214   HLog.message "status.aliases:\n";
215   HLog.message "status.proof_status:"; 
216   HLog.message
217     (match status.proof_status with
218     | No_proof -> "no proof\n"
219     | Incomplete_proof _ -> "incomplete proof\n"
220     | Proof _ -> "proof\n"
221     | Intermediate _ -> "Intermediate\n");
222   HLog.message "status.options\n";
223 (* REMOVEME
224   StringMap.iter (fun k v -> 
225     let v = 
226       match v with
227       | String s -> s
228       | Int i -> string_of_int i
229     in
230     HLog.message (k ^ "::=" ^ v)) status.options;
231 *)
232   HLog.message "status.coercions\n";
233   HLog.message "status.objects:\n";
234   List.iter 
235     (fun u -> HLog.message (UriManager.string_of_uri u)) status.objects