1 (* Copyright (C) 2000-2002, 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://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
34 (******************************************************************************)
38 exception RefreshSequentException of exn;;
39 exception RefreshProofException of exn;;
41 module type Callbacks =
44 val sequent_viewer : unit -> TermViewer.sequent_viewer
45 val term_editor : unit -> TermEditor.term_editor
48 < sequent_viewer: TermViewer.sequent_viewer ;
51 set_term : Cic.term -> unit ;
52 metasenv: Cic.metasenv ;
53 set_metasenv : Cic.metasenv -> unit ;
54 context: Cic.context ;
55 set_context : Cic.context -> unit >
57 val output_html : Ui_logger.html_msg -> unit
58 (* GUI refresh functions *)
59 val refresh_proof : unit -> unit
60 val refresh_goals : unit -> unit
61 (* callbacks for user-tactics interaction *)
62 val decompose_uris_choice_callback :
63 (UriManager.uri * int * 'a) list ->
64 (UriManager.uri * int * 'b list) list
65 val mk_fresh_name_callback : ProofEngineTypes.mk_fresh_name_type
71 val intros : unit -> unit
72 val exact : ?term:string -> unit -> unit
73 val apply : ?term:string -> unit -> unit
74 val elimintrossimpl : ?term:string -> unit -> unit
75 val elimtype : ?term:string -> unit -> unit
76 val whd : unit -> unit
77 val reduce : unit -> unit
78 val simpl : unit -> unit
79 val fold_whd : ?term:string -> unit -> unit
80 val fold_reduce : ?term:string -> unit -> unit
81 val fold_simpl : ?term:string -> unit -> unit
82 val cut : ?term:string -> unit -> unit
83 val change : unit -> unit
84 val letin : ?term:string -> unit -> unit
85 val ring : unit -> unit
86 val clearbody : unit -> unit
87 val clear : unit -> unit
88 val fourier : unit -> unit
89 val rewritesimpl : ?term:string -> unit -> unit
90 val rewritebacksimpl : ?term:string -> unit -> unit
91 val replace : unit -> unit
92 val reflexivity : unit -> unit
93 val symmetry : unit -> unit
94 val transitivity : ?term:string -> unit -> unit
95 val exists : unit -> unit
96 val split : unit -> unit
97 val left : unit -> unit
98 val right : unit -> unit
99 val assumption : unit -> unit
100 val generalize : unit -> unit
101 val absurd : ?term:string -> unit -> unit
102 val contradiction : unit -> unit
103 val decompose : ?term:string -> unit -> unit
104 val injection : ?term:string -> unit -> unit
105 val discriminate : ?term:string -> unit -> unit
106 val whd_in_scratch : unit -> unit
107 val reduce_in_scratch : unit -> unit
108 val simpl_in_scratch : unit -> unit
111 module Make (C: Callbacks) : Tactics =
114 let print_uncaught_exception e =
115 C.output_html (`Error (`T (sprintf "Uncaught exception: %s"
116 (Printexc.to_string e))))
118 let handle_refresh_exception f savedproof savedgoal =
122 | RefreshSequentException e ->
123 C.output_html (`Error (`T
124 (sprintf "Exception raised during the refresh of the sequent: %s"
125 (Printexc.to_string e))));
126 ProofEngine.set_proof savedproof ;
127 ProofEngine.goal := savedgoal ;
129 | RefreshProofException e ->
130 C.output_html (`Error (`T
131 (sprintf "Exception raised during the refresh of the proof: %s"
132 (Printexc.to_string e))));
133 ProofEngine.set_proof savedproof ;
134 ProofEngine.goal := savedgoal ;
138 print_uncaught_exception e;
139 ProofEngine.set_proof savedproof ;
140 ProofEngine.goal := savedgoal
142 let call_tactic tactic () =
143 let savedproof = ProofEngine.get_proof () in
144 let savedgoal = !ProofEngine.goal in
145 handle_refresh_exception
152 let call_tactic_with_input tactic ?term () =
153 let savedproof = ProofEngine.get_proof () in
154 let savedgoal = !ProofEngine.goal in
155 let uri,metasenv,bo,ty =
156 match ProofEngine.get_proof () with
158 | Some (uri,metasenv,bo,ty) -> uri,metasenv,bo,ty
160 let canonical_context =
161 match !ProofEngine.goal with
164 let (_,canonical_context,_) =
165 List.find (function (m,_,_) -> m=metano) metasenv
169 handle_refresh_exception
174 | Some t -> (C.term_editor ())#set_term t);
175 (C.term_editor ())#get_metasenv_and_term canonical_context metasenv
177 ProofEngine.set_proof (Some (uri,metasenv',bo,ty)) ;
181 (C.term_editor ())#reset)
184 let call_tactic_with_goal_input tactic () =
185 let module L = LogicalOperations in
186 let module G = Gdome in
187 let savedproof = ProofEngine.get_proof () in
188 let savedgoal = !ProofEngine.goal in
189 match (C.sequent_viewer ())#get_selected_terms with
191 handle_refresh_exception
197 | [] -> C.output_html (`Error (`T "No term selected"))
198 | _ -> C.output_html (`Error (`T "Too many terms selected"))
200 let call_tactic_with_goal_inputs tactic () =
201 let module L = LogicalOperations in
202 let module G = Gdome in
203 let savedproof = ProofEngine.get_proof () in
204 let savedgoal = !ProofEngine.goal in
205 handle_refresh_exception
207 match (C.sequent_viewer ())#get_selected_terms with
208 | [] -> C.output_html (`Error (`T "No term selected"))
215 let call_tactic_with_input_and_goal_input tactic () =
216 let module L = LogicalOperations in
217 let module G = Gdome in
218 let savedproof = ProofEngine.get_proof () in
219 let savedgoal = !ProofEngine.goal in
220 match (C.sequent_viewer ())#get_selected_terms with
222 handle_refresh_exception
224 let uri,metasenv,bo,ty =
225 match ProofEngine.get_proof () with
227 | Some (uri,metasenv,bo,ty) -> uri,metasenv,bo,ty
229 let canonical_context =
230 match !ProofEngine.goal with
233 let (_,canonical_context,_) =
234 List.find (function (m,_,_) -> m=metano) metasenv
237 let (metasenv',expr) =
238 (C.term_editor ())#get_metasenv_and_term
239 canonical_context metasenv
241 ProofEngine.set_proof (Some (uri,metasenv',bo,ty)) ;
242 tactic ~goal_input:term ~input:expr ;
245 (C.term_editor ())#reset)
247 | [] -> C.output_html (`Error (`T "No term selected"))
248 | _ -> C.output_html (`Error (`T "Too many terms selected"))
250 let call_tactic_with_goal_input_in_scratch tactic () =
251 let module L = LogicalOperations in
252 let module G = Gdome in
253 let scratch_window = C.scratch_window () in
254 match scratch_window#sequent_viewer#get_selected_terms with
258 let expr = tactic term scratch_window#term in
259 scratch_window#sequent_viewer#load_sequent
260 scratch_window#metasenv (111,scratch_window#context,expr) ;
261 scratch_window#set_term expr ;
262 scratch_window#show () ;
264 e -> print_uncaught_exception e
266 | [] -> C.output_html (`Error (`T "No term selected"))
267 | _ -> C.output_html (`Error (`T "Too many terms selected"))
269 let call_tactic_with_goal_inputs_in_scratch tactic () =
270 let module L = LogicalOperations in
271 let module G = Gdome in
272 let scratch_window = C.scratch_window () in
273 match scratch_window#sequent_viewer#get_selected_terms with
274 | [] -> C.output_html (`Error (`T "No term selected"))
277 let expr = tactic terms scratch_window#term in
278 scratch_window#sequent_viewer#load_sequent
279 scratch_window#metasenv (111,scratch_window#context,expr) ;
280 scratch_window#set_term expr ;
281 scratch_window#show () ;
283 e -> print_uncaught_exception e
285 let call_tactic_with_hypothesis_input tactic () =
286 let module L = LogicalOperations in
287 let module G = Gdome in
288 let savedproof = ProofEngine.get_proof () in
289 let savedgoal = !ProofEngine.goal in
290 match (C.sequent_viewer ())#get_selected_hypotheses with
292 handle_refresh_exception
298 | [] -> C.output_html (`Error (`T "No hypothesis selected"))
299 | _ -> C.output_html (`Error (`T "Too many hypotheses selected"))
304 (ProofEngine.intros ~mk_fresh_name_callback:C.mk_fresh_name_callback)
305 let exact = call_tactic_with_input ProofEngine.exact
306 let apply = call_tactic_with_input ProofEngine.apply
307 let elimintrossimpl = call_tactic_with_input ProofEngine.elim_intros_simpl
308 let elimtype = call_tactic_with_input ProofEngine.elim_type
309 let whd = call_tactic_with_goal_inputs ProofEngine.whd
310 let reduce = call_tactic_with_goal_inputs ProofEngine.reduce
311 let simpl = call_tactic_with_goal_inputs ProofEngine.simpl
312 let fold_whd = call_tactic_with_input ProofEngine.fold_whd
313 let fold_reduce = call_tactic_with_input ProofEngine.fold_reduce
314 let fold_simpl = call_tactic_with_input ProofEngine.fold_simpl
316 call_tactic_with_input
317 (ProofEngine.cut ~mk_fresh_name_callback:C.mk_fresh_name_callback)
318 let change = call_tactic_with_input_and_goal_input ProofEngine.change
320 call_tactic_with_input
321 (ProofEngine.letin ~mk_fresh_name_callback:C.mk_fresh_name_callback)
322 let ring = call_tactic ProofEngine.ring
323 let clearbody = call_tactic_with_hypothesis_input ProofEngine.clearbody
324 let clear = call_tactic_with_hypothesis_input ProofEngine.clear
325 let fourier = call_tactic ProofEngine.fourier
326 let rewritesimpl = call_tactic_with_input ProofEngine.rewrite_simpl
327 let rewritebacksimpl = call_tactic_with_input ProofEngine.rewrite_back_simpl
328 let replace = call_tactic_with_input_and_goal_input ProofEngine.replace
329 let reflexivity = call_tactic ProofEngine.reflexivity
330 let symmetry = call_tactic ProofEngine.symmetry
331 let transitivity = call_tactic_with_input ProofEngine.transitivity
332 let exists = call_tactic ProofEngine.exists
333 let split = call_tactic ProofEngine.split
334 let left = call_tactic ProofEngine.left
335 let right = call_tactic ProofEngine.right
336 let assumption = call_tactic ProofEngine.assumption
337 let injection = call_tactic_with_input ProofEngine.injection
338 let discriminate = call_tactic_with_input ProofEngine.discriminate
340 call_tactic_with_goal_inputs
341 (ProofEngine.generalize ~mk_fresh_name_callback:C.mk_fresh_name_callback)
342 let absurd = call_tactic_with_input ProofEngine.absurd
343 let contradiction = call_tactic ProofEngine.contradiction
345 call_tactic_with_input
346 (ProofEngine.decompose
347 ~uris_choice_callback:C.decompose_uris_choice_callback)
349 call_tactic_with_goal_inputs_in_scratch ProofEngine.whd_in_scratch
350 let reduce_in_scratch =
351 call_tactic_with_goal_inputs_in_scratch ProofEngine.reduce_in_scratch
352 let simpl_in_scratch =
353 call_tactic_with_goal_inputs_in_scratch ProofEngine.simpl_in_scratch