1 (* Copyright (C) 2004-2005, 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/
30 (** raised when one of the script margins (top or bottom) is reached *)
33 let safe_substring s i j =
34 try String.sub s i j with Invalid_argument _ -> assert false
36 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*"
37 let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$"
38 let multiline_RE = Pcre.regexp "^\n[^\n]+$"
39 let newline_RE = Pcre.regexp "\n"
42 if Pcre.pmatch ~rex:multiline_RE str then
43 "\n(** " ^ (Pcre.replace ~rex:newline_RE str) ^ " **)"
45 "\n(**\n" ^ str ^ "\n**)"
48 let s = Pcre.replace ~rex:heading_nl_RE s in
50 let nl_pos = String.index s '\n' in
54 let prepend_text header base =
55 if Pcre.pmatch ~rex:heading_nl_RE base then
56 sprintf "\n%s%s" header base
58 sprintf "%s\n%s" header base
60 (** creates a statement AST for the Goal tactic, e.g. "goal 7" *)
62 let module A = TacticAst in
63 let loc = CicAst.dummy_floc in
64 A.Executable (loc, A.Tactical (loc, A.Tactic (loc, A.Goal (loc, n))))
66 let eval_with_engine status user_goal parsed_text st =
67 let module TA = TacticAst in
68 let module TAPp = TacticAstPp in
69 let parsed_text_length = String.length parsed_text in
71 match st with TA.Executable (loc,ex) -> loc, ex | _ -> assert false
73 let goal_changed = ref false in
75 match status.proof_status with
76 | Incomplete_proof (_, goal) when goal <> user_goal ->
78 MatitaEngine.eval_ast status (goal_ast user_goal)
81 let new_status = MatitaEngine.eval_ast status st in
84 | TA.Command (_, TA.Alias _) ->
85 DisambiguateTypes.Environment.empty
86 | _ -> MatitaSync.alias_diff ~from:status new_status
88 (* we remove the defined object since we consider them "automathic aliases" *)
90 let module DTE = DisambiguateTypes.Environment in
91 let module UM = UriManager in
93 fun k ((v,_) as value) acc ->
94 let v = UM.strip_xpointer (UM.uri_of_string v) in
95 if List.exists (fun (s,_) -> s = v) new_status.objects then
99 ) new_aliases DTE.empty
102 if DisambiguateTypes.Environment.is_empty new_aliases then
105 prepend_text (CicTextualParser2.EnvironmentP3.to_string new_aliases)
109 if !goal_changed then
111 (TAPp.pp_tactic (TA.Goal (loc, user_goal))(* ^ "\n"*))
116 [ new_status, new_text ], parsed_text_length
118 let disambiguate term status =
119 let module MD = MatitaDisambiguator in
120 let dbd = MatitaDb.instance () in
121 let metasenv = MatitaMisc.get_proof_metasenv status in
122 let context = MatitaMisc.get_proof_context status in
123 let aliases = MatitaMisc.get_proof_aliases status in
124 let interps = MD.disambiguate_term dbd context metasenv aliases term in
129 let eval_macro status (mathviewer:MatitaTypes.mathViewer) urichooser parsed_text
132 let module TA = TacticAst in
133 let module TAPp = TacticAstPp in
134 let module MQ = MetadataQuery in
135 let module MDB = MatitaDb in
136 let module CTC = CicTypeChecker in
137 let module CU = CicUniv in
138 (* no idea why ocaml wants this *)
139 let advance ?statement () = script#advance ?statement () in
140 let parsed_text_length = String.length parsed_text in
141 let dbd = MatitaDb.instance () in
144 | TA.WMatch (loc, term) ->
145 let term = disambiguate term status in
146 let l = MQ.match_term ~dbd term in
147 let entry = `Whelp (TAPp.pp_macro_cic (TA.WMatch (loc, term)), l) in
148 mathviewer#show_uri_list ~reuse:true ~entry l;
149 [], parsed_text_length
150 | TA.WInstance (loc, term) ->
151 let term = disambiguate term status in
152 let l = MQ.instance ~dbd term in
153 let entry = `Whelp (TAPp.pp_macro_cic (TA.WInstance (loc, term)), l) in
154 mathviewer#show_uri_list ~reuse:true ~entry l;
155 [], parsed_text_length
156 | TA.WLocate (loc, s) ->
157 let l = MQ.locate ~dbd s in
158 let entry = `Whelp (TAPp.pp_macro_cic (TA.WLocate (loc, s)), l) in
159 mathviewer#show_uri_list ~reuse:true ~entry l;
160 [], parsed_text_length
161 | TA.WElim (loc, term) ->
162 let term = disambiguate term status in
165 | Cic.MutInd (uri,n,_) -> UriManager.string_of_uriref (uri,[n])
166 | _ -> failwith "Not a MutInd"
168 let l = MQ.elim ~dbd uri in
169 let entry = `Whelp (TAPp.pp_macro_cic (TA.WElim (loc, term)), l) in
170 mathviewer#show_uri_list ~reuse:true ~entry l;
171 [], parsed_text_length
172 | TA.WHint (loc, term) ->
173 let term = disambiguate term status in
174 let s = ((None,[0,[],term], Cic.Meta (0,[]) ,term),0) in
175 let l = List.map fst (MQ.experimental_hint ~dbd s) in
176 let entry = `Whelp (TAPp.pp_macro_cic (TA.WHint (loc, term)), l) in
177 mathviewer#show_uri_list ~reuse:true ~entry l;
178 [], parsed_text_length
181 let s = MatitaMisc.get_proof_status status in
182 let l = List.map fst (MQ.experimental_hint ~dbd s) in
183 let selected = urichooser l in
185 | [] -> [], parsed_text_length
191 TA.Apply (loc, CicAst.Uri (uri,None)))))))
193 let new_status = MatitaEngine.eval_ast status ast in
195 comment parsed_text ^
196 "\n" ^ TAPp.pp_statement ast
198 [ new_status , extra_text ], parsed_text_length
201 "The result of the urichooser should be only 1 uri, not:\n";
203 fun u -> MatitaLog.error (u ^ "\n")
206 | TA.Check (_,term) ->
207 let metasenv = MatitaMisc.get_proof_metasenv status in
208 let context = MatitaMisc.get_proof_context status in
209 let aliases = MatitaMisc.get_proof_aliases status in
211 MatitaDisambiguator.disambiguate_term
212 dbd context metasenv aliases term
214 let _, metasenv , term, ugraph =
219 let ty,_ = CTC.type_of_aux' metasenv context term ugraph in
220 let t_and_ty = Cic.Cast (term,ty) in
221 mathviewer#show_entry (`Cic (t_and_ty,metasenv));
222 [], parsed_text_length
225 let status = script#status.proof_status in
228 | _ -> script#retract ();go_back()
230 [], parsed_text_length, Some go_back
231 | TA.Redo (_, Some i) -> [], parsed_text_length,
232 Some (fun () -> for j = 1 to i do advance () done)
233 | TA.Redo (_, None) -> [], parsed_text_length,
234 Some (fun () -> advance ())
235 | TA.Undo (_, Some i) -> [], parsed_text_length,
236 Some (fun () -> for j = 1 to i do script#retract () done)
237 | TA.Undo (_, None) -> [], parsed_text_length,
238 Some (fun () -> script#retract ()) *)
240 | TA.Quit _ -> failwith "not implemented"
241 | TA.Print (_,kind) -> failwith "not implemented"
242 | TA.Search_pat (_, search_kind, str) -> failwith "not implemented"
243 | TA.Search_term (_, search_kind, term) -> failwith "not implemented"
246 let eval_executable status (mathviewer:MatitaTypes.mathViewer) urichooser
247 user_goal parsed_text script ex =
248 let module TA = TacticAst in
249 let module TAPp = TacticAstPp in
250 let module MD = MatitaDisambiguator in
251 let parsed_text_length = String.length parsed_text in
253 | TA.Command (loc, _) | TA.Tactical (loc, _) ->
254 eval_with_engine status user_goal parsed_text (TA.Executable (loc, ex))
255 | TA.Macro (_,mac) ->
256 eval_macro status mathviewer urichooser parsed_text script mac
258 let rec eval_statement status (mathviewer:MatitaTypes.mathViewer) urichooser
260 if Pcre.pmatch ~rex:only_dust_RE s then raise Margin;
261 let st = CicTextualParser2.parse_statement (Stream.of_string s) in
262 let text_of_loc loc =
263 let parsed_text_length = snd (CicAst.loc_of_floc loc) in
264 let parsed_text = safe_substring s 0 parsed_text_length in
265 parsed_text, parsed_text_length
268 | TacticAst.Comment (loc,_)->
269 let parsed_text, parsed_text_length = text_of_loc loc in
270 let remain_len = String.length s - parsed_text_length in
271 let s = String.sub s parsed_text_length remain_len in
273 eval_statement status mathviewer urichooser user_goal script s
276 | (status, text) :: tl ->
277 ((status, parsed_text ^ text)::tl), (parsed_text_length + len)
279 | TacticAst.Executable (loc, ex) ->
280 let parsed_text, parsed_text_length = text_of_loc loc in
282 status mathviewer urichooser user_goal parsed_text script ex
285 class script ~(buffer: GText.buffer) ~(init: MatitaTypes.status)
286 ~(mathviewer: MatitaTypes.mathViewer)
289 initializer self#reset ()
291 val mutable statements = []; (** executed statements *)
292 val mutable history = [ init ];
293 (** list of states before having executed statements. Head element of this
294 * list is the current state, last element is the state at the beginning of
296 * Invariant: this list length is 1 + length of statements *)
298 (** goal as seen by the user (i.e. metano corresponding to current tab) *)
299 val mutable userGoal = ~-1
301 (** text mark and tag representing locked part of a script *)
303 buffer#create_mark ~name:"locked" ~left_gravity:true buffer#start_iter
304 val locked_tag = buffer#create_tag [`BACKGROUND "lightblue"; `EDITABLE false]
306 (* history can't be empty, the invariant above grant that it contains at
307 * least the init status *)
308 method status = match history with hd :: _ -> hd | _ -> assert false
310 method private _advance ?statement () =
311 let s = match statement with Some s -> s | None -> self#getFuture in
312 MatitaLog.debug ("evaluating: " ^ first_line s ^ " ...");
313 let (entries, parsed_len) =
314 eval_statement self#status mathviewer urichooser userGoal self s in
315 let (new_statuses, new_statements) = List.split entries in
317 prerr_endline "evalStatement returned";
318 List.iter (fun s -> prerr_endline ("'" ^ s ^ "'")) new_statements;
320 history <- List.rev new_statuses @ history;
321 statements <- List.rev new_statements @ statements;
322 let start = buffer#get_iter_at_mark (`MARK locked_mark) in
323 if statement = None then begin
324 let stop = start#copy#forward_chars parsed_len in
325 buffer#delete ~start ~stop
327 let new_text = String.concat "" new_statements in
328 buffer#insert ~iter:start new_text;
329 self#moveMark (String.length new_text)
331 method private _retract () =
332 match statements, history with
333 | last_statement :: _, cur_status :: prev_status :: _ ->
334 MatitaSync.time_travel ~present:cur_status ~past:prev_status;
335 statements <- List.tl statements;
336 history <- List.tl history;
337 self#moveMark (- (String.length last_statement));
340 method advance ?statement () =
342 self#_advance ?statement ()
345 method retract () = try self#_retract () with Margin -> ()
347 method private getFuture =
348 buffer#get_text ~start:(buffer#get_iter_at_mark (`MARK locked_mark))
349 ~stop:buffer#end_iter ()
351 (** @param rel_offset relative offset from current position of locked_mark *)
352 method private moveMark rel_offset =
353 let mark = `MARK locked_mark in
354 let old_insert = buffer#get_iter_at_mark `INSERT in
355 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
356 let current_mark_pos = buffer#get_iter_at_mark mark in
358 match rel_offset with
359 | 0 -> current_mark_pos
360 | n when n > 0 -> current_mark_pos#forward_chars n
361 | n (* when n < 0 *) -> current_mark_pos#backward_chars (abs n)
363 buffer#move_mark mark ~where:new_mark_pos;
364 buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:new_mark_pos;
365 buffer#move_mark `INSERT old_insert;
368 val mutable observers = []
370 method addObserver (o: MatitaTypes.status -> unit) =
371 observers <- o :: observers
373 method private notify =
374 let status = self#status in
375 List.iter (fun o -> o status) observers
377 method loadFrom fname =
378 buffer#set_text (MatitaMisc.input_file fname);
381 method saveTo fname =
382 let oc = open_out fname in
383 output_string oc (buffer#get_text ~start:buffer#start_iter
384 ~stop:buffer#end_iter ());
387 method private goto_top =
388 MatitaSync.time_travel ~present:self#status ~past:init;
393 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
394 buffer#move_mark (`MARK locked_mark) ~where:buffer#start_iter
398 buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter
400 method goto (pos: [`Top | `Bottom | `Cursor]) () =
402 | `Top -> self#goto_top
404 (try while true do self#_advance () done with Margin -> ())
406 let locked_iter () = buffer#get_iter_at_mark (`NAME "locked") in
407 let cursor_iter () = buffer#get_iter_at_mark `INSERT in
408 let rec forward_until_cursor () = (* go forward until locked > cursor *)
410 if (locked_iter ())#compare (cursor_iter ()) < 0 then
411 forward_until_cursor ()
413 let rec back_until_cursor () = (* go backward until locked < cursor *)
415 if (locked_iter ())#compare (cursor_iter ()) > 0 then
418 let cmp = (locked_iter ())#compare (cursor_iter ()) in
420 if cmp < 0 then (* locked < cursor *)
421 forward_until_cursor ()
422 else if cmp > 0 then (* locked > cursor *)
424 else (* cursor = locked *)
428 method onGoingProof () =
429 match self#status.proof_status with
430 | No_proof | Proof _ -> false
431 | Incomplete_proof _ -> true
432 | Intermediate _ -> assert false
434 method proofStatus = MatitaMisc.get_proof_status self#status
435 method proofMetasenv = MatitaMisc.get_proof_metasenv self#status
436 method proofContext = MatitaMisc.get_proof_context self#status
437 method setGoal n = userGoal <- n
441 MatitaLog.debug "script status:";
442 MatitaLog.debug ("history size: " ^ string_of_int (List.length history));
443 MatitaLog.debug (sprintf "%d statements:" (List.length statements));
444 List.iter MatitaLog.debug statements;
448 let _script = ref None
450 let script ~buffer ~init ~mathviewer ~urichooser () =
451 let s = new script ~buffer ~init ~mathviewer ~urichooser () in
455 let instance () = match !_script with None -> assert false | Some s -> s