1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 let default_history_size = 20
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 ]
34 type proof_status = ProofEngineTypes.proof * ProofEngineTypes.goal option
36 type 'a observer = (proof_status * 'a) option -> (proof_status * 'a) -> unit
37 type observer_id = int
39 exception Observer_failures of (observer_id * exn) list
40 exception Tactic_failure of exn
41 exception Data_failure of exn
44 ?(history_size = default_history_size)
45 ?uri ~typ ~body ~metasenv init_data compute_data ()
47 let next_observer_id =
48 let next_id = ref 0 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 *)
62 let initial_goal = next_goal ([], initial_proof) in
65 val mutable _proof = initial_proof
66 val mutable _goal = initial_goal
67 val mutable _data: 'a = init_data (initial_proof, initial_goal)
69 (* event -> (id, observer) list *)
70 val observers = Hashtbl.create 7
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
77 history#push self#internal_status
80 method private status = (_proof, _goal) (* logic status *)
81 method private set_status (proof, (goal: int option)) =
88 | None -> raise No_goal_left
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;
96 method set_goal goal =
99 let old_internal_status = self#internal_status in
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;
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
115 method set_metasenv metasenv =
116 let (uri, _, body, typ) = _proof in
117 _proof <- (uri, metasenv, body, typ)
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)
125 method conjecture goal =
126 let (_, metasenv, _, _) = _proof in
127 CicUtil.lookup_meta goal metasenv
129 method apply_tactic tactic =
130 let old_internal_status = self#internal_status in
131 let (new_proof, new_goals) =
133 ProofEngineTypes.apply_tactic tactic (_proof, self#goal)
134 with exn -> raise (Tactic_failure exn)
137 _goal <- next_goal (new_goals, new_proof);
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;
146 method proof_completed = _goal = None
148 method attach_observer ?(interested_in = default_events) observer
150 let id = next_observer_id () in
154 try Hashtbl.find observers event with Not_found -> []
156 Hashtbl.replace observers event ((id, observer)::prev_observers))
160 method detach_observer id =
164 try Hashtbl.find observers event with Not_found -> []
167 List.filter (fun (id', _) -> id' <> id) prev_observers
169 Hashtbl.replace observers event new_observers)
172 method private private_notify old_internal_status =
173 let cur_internal_status = (self#status, _data) in
175 let notify (id, observer) =
177 observer old_internal_status cur_internal_status
178 with exn -> exns := (id, exn) :: !exns
181 (try Hashtbl.find observers `Proof_changed with Not_found -> []);
182 if self#proof_completed then
184 (try Hashtbl.find observers `Proof_completed with Not_found -> []);
187 | exns -> raise (Observer_failures exns)
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
194 _data <- compute_data old_internal_status status
195 with exn -> raise (Data_failure exn)
197 method undo ?(steps = 1) () =
198 let ((proof, goal), data) = history#undo steps in
202 self#private_notify None
204 method redo ?(steps = 1) () = self#undo ~steps:~-steps ()
206 method notify = self#private_notify None
210 let trivial_status ?uri ~typ ~body ~metasenv () =
211 new status ?uri ~typ ~body ~metasenv (fun _ -> ()) (fun _ _ -> ()) ()