]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/statefulProofEngine.ml
4e3badea4781e97f3595235971590e21eddae451
[helm.git] / components / tactics / statefulProofEngine.ml
1 (* Copyright (C) 2004, 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 let default_history_size = 20
29
30 exception No_goal_left
31 exception Uri_redefinition
32 type event = [ `Proof_changed | `Proof_completed ]
33 let all_events = [ `Proof_changed; `Proof_completed ]
34 let default_events: event list = [ `Proof_changed ]
35
36 type proof_status = ProofEngineTypes.proof * ProofEngineTypes.goal option
37
38 type 'a observer = (proof_status * 'a) option -> (proof_status * 'a) -> unit
39 type observer_id = int
40
41 exception Observer_failures of (observer_id * exn) list
42 exception Tactic_failure of exn
43 exception Data_failure of exn
44
45 class ['a] status
46   ?(history_size = default_history_size)
47   ?uri ~typ ~body ~metasenv ~attrs init_data compute_data ()
48   =
49   let next_observer_id =
50     let next_id = ref 0 in
51     fun () ->
52       incr next_id;
53       !next_id
54   in
55   let initial_proof = ((uri: UriManager.uri option), metasenv, body, typ, attrs) in
56   let next_goal (goals, proof) =
57     match goals, proof with
58     | goal :: _, _ -> Some goal
59     | [], (_, (goal, _, _) :: _, _, _, _) ->
60         (* the tactic left no open goal: let's choose the first open goal *)
61         Some goal
62     | _, _ -> None
63   in
64   let initial_goal = next_goal ([], initial_proof) in
65   object (self)
66
67     val mutable _proof = initial_proof
68     val mutable _goal = initial_goal
69     val mutable _data: 'a = init_data (initial_proof, initial_goal)
70
71       (* event -> (id, observer) list *)
72     val observers = Hashtbl.create 7
73
74       (* assumption: all items in history are uncompleted proofs, thus option on
75       * goal could be ignored and goal are stored as bare integers *)
76     val history = new History.history history_size
77
78     initializer
79       history#push self#internal_status
80
81     method proof = _proof
82     method private status = (_proof, _goal)  (* logic status *)
83     method private set_status (proof, (goal: int option)) =
84       _proof <- proof;
85       _goal <- goal
86
87     method goal =
88       match _goal with
89       | Some goal -> goal
90       | None -> raise No_goal_left
91
92       (* what will be kept in history *)
93     method private internal_status = (self#status, _data)
94     method private set_internal_status (status, data) =
95       self#set_status status;
96       _data <- data
97
98     method set_goal goal =
99       _goal <- Some goal
100 (*
101       let old_internal_status = self#internal_status in
102       _goal <- Some goal;
103       try
104         self#update_data old_internal_status;
105         history#push self#internal_status;
106         self#private_notify (Some old_internal_status)
107       with (Data_failure _) as exn ->
108         self#set_internal_status old_internal_status;
109         raise exn
110 *)
111
112     method uri      = let (uri, _, _, _, _)      = _proof in uri
113     method metasenv = let (_, metasenv, _, _, _) = _proof in metasenv
114     method body     = let (_, _, body, _, _)     = _proof in body
115     method typ      = let (_, _, _, typ, _)      = _proof in typ
116     method attrs    = let (_, _, _, _, attrs)    = _proof in attrs
117
118     method set_metasenv metasenv =
119       let (uri, _, body, typ, attes) = _proof in
120       _proof <- (uri, metasenv, body, typ, attrs)
121
122     method set_uri uri =
123       let (old_uri, metasenv, body, typ, attrs) = _proof in
124       if old_uri <> None then
125         raise Uri_redefinition;
126       _proof <- (Some uri, metasenv, body, typ, attrs)
127
128     method conjecture goal =
129       let (_, metasenv, _, _, _) = _proof in
130       CicUtil.lookup_meta goal metasenv
131
132     method apply_tactic tactic =
133       let old_internal_status = self#internal_status in
134       let (new_proof, new_goals) =
135         try
136           ProofEngineTypes.apply_tactic tactic (_proof, self#goal)
137         with exn -> raise (Tactic_failure exn)
138       in
139       _proof <- new_proof;
140       _goal <- next_goal (new_goals, new_proof);
141       try
142         self#update_data old_internal_status;
143         history#push self#internal_status;
144         self#private_notify (Some old_internal_status)
145       with (Data_failure _) as exn ->
146         self#set_internal_status old_internal_status;
147         raise exn
148
149     method proof_completed = _goal = None
150
151     method attach_observer ?(interested_in = default_events) observer
152       =
153       let id = next_observer_id () in
154       List.iter
155         (fun event ->
156           let prev_observers =
157             try Hashtbl.find observers event with Not_found -> []
158           in
159           Hashtbl.replace observers event ((id, observer)::prev_observers))
160         interested_in;
161       id
162
163     method detach_observer id =
164       List.iter
165         (fun event ->
166           let prev_observers =
167             try Hashtbl.find observers event with Not_found -> []
168           in
169           let new_observers =
170             List.filter (fun (id', _) -> id' <> id) prev_observers
171           in
172           Hashtbl.replace observers event new_observers)
173         all_events
174
175     method private private_notify old_internal_status =
176       let cur_internal_status = (self#status, _data) in
177       let exns = ref [] in
178       let notify (id, observer) =
179         try
180           observer old_internal_status cur_internal_status
181         with exn -> exns := (id, exn) :: !exns
182       in
183       List.iter notify
184         (try Hashtbl.find observers `Proof_changed with Not_found -> []);
185       if self#proof_completed then
186         List.iter notify
187           (try Hashtbl.find observers `Proof_completed with Not_found -> []);
188       match !exns with
189       | [] -> ()
190       | exns -> raise (Observer_failures exns)
191
192     method private update_data old_internal_status =
193       (* invariant: _goal and/or _proof has been changed
194        * invariant: proof is not yet completed *)
195       let status = self#status in
196       try
197         _data <- compute_data old_internal_status status
198       with exn -> raise (Data_failure exn)
199
200     method undo ?(steps = 1) () =
201       let ((proof, goal), data) = history#undo steps in
202       _proof <- proof;
203       _goal <- goal;
204       _data <- data;
205       self#private_notify None
206
207     method redo ?(steps = 1) () = self#undo ~steps:~-steps ()
208
209     method notify = self#private_notify None
210
211   end
212
213 let trivial_status ?uri ~typ ~body ~metasenv ~attrs () =
214   new status ?uri ~typ ~body ~metasenv ~attrs (fun _ -> ()) (fun _ _ -> ()) ()
215