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