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