1 (* Copyright (C) 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/
31 exception UnableToInclude of string
32 exception IncludedFileNotCompiled of string
35 let debug_print = if debug then prerr_endline else ignore ;;
38 do_heavy_checks: bool ;
39 include_paths: string list ;
43 (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
44 * names as long as they are available, then it fallbacks to name generation
45 * using FreshNamesGenerator module *)
47 let len = List.length names in
49 fun metasenv context name ~typ ->
50 if !count < len then begin
51 let name = Cic.Name (List.nth names !count) in
55 FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context name ~typ
57 let tactic_of_ast ast =
58 let module PET = ProofEngineTypes in
60 | GrafiteAst.Absurd (_, term) -> Tactics.absurd term
61 | GrafiteAst.Apply (_, term) -> Tactics.apply term
62 | GrafiteAst.Assumption _ -> Tactics.assumption
63 | GrafiteAst.Auto (_,depth,width,paramodulation,full) ->
64 AutoTactic.auto_tac ?depth ?width ?paramodulation ?full
65 ~dbd:(LibraryDb.instance ()) ()
66 | GrafiteAst.Change (_, pattern, with_what) ->
67 Tactics.change ~pattern with_what
68 | GrafiteAst.Clear (_,id) -> Tactics.clear id
69 | GrafiteAst.ClearBody (_,id) -> Tactics.clearbody id
70 | GrafiteAst.Contradiction _ -> Tactics.contradiction
71 | GrafiteAst.Compare (_, term) -> Tactics.compare term
72 | GrafiteAst.Constructor (_, n) -> Tactics.constructor n
73 | GrafiteAst.Cut (_, ident, term) ->
74 let names = match ident with None -> [] | Some id -> [id] in
75 Tactics.cut ~mk_fresh_name_callback:(namer_of names) term
76 | GrafiteAst.DecideEquality _ -> Tactics.decide_equality
77 | GrafiteAst.Decompose (_, types, what, names) ->
78 let to_type = function
79 | GrafiteAst.Type (uri, typeno) -> uri, typeno
80 | GrafiteAst.Ident _ -> assert false
82 let user_types = List.rev_map to_type types in
83 let dbd = LibraryDb.instance () in
84 let mk_fresh_name_callback = namer_of names in
85 Tactics.decompose ~mk_fresh_name_callback ~dbd ~user_types what
86 | GrafiteAst.Discriminate (_,term) -> Tactics.discriminate term
87 | GrafiteAst.Elim (_, what, using, depth, names) ->
88 Tactics.elim_intros ?using ?depth ~mk_fresh_name_callback:(namer_of names)
90 | GrafiteAst.ElimType (_, what, using, depth, names) ->
91 Tactics.elim_type ?using ?depth ~mk_fresh_name_callback:(namer_of names)
93 | GrafiteAst.Exact (_, term) -> Tactics.exact term
94 | GrafiteAst.Exists _ -> Tactics.exists
95 | GrafiteAst.Fail _ -> Tactics.fail
96 | GrafiteAst.Fold (_, reduction_kind, term, pattern) ->
98 match reduction_kind with
100 PET.const_lazy_reduction
101 (CicReduction.normalize ~delta:false ~subst:[])
102 | `Reduce -> PET.const_lazy_reduction ProofEngineReduction.reduce
103 | `Simpl -> PET.const_lazy_reduction ProofEngineReduction.simpl
105 PET.const_lazy_reduction (ProofEngineReduction.unfold ?what:None)
106 | `Unfold (Some lazy_term) ->
107 (fun context metasenv ugraph ->
108 let what, metasenv, ugraph = lazy_term context metasenv ugraph in
109 ProofEngineReduction.unfold ~what, metasenv, ugraph)
111 PET.const_lazy_reduction (CicReduction.whd ~delta:false ~subst:[])
113 Tactics.fold ~reduction ~term ~pattern
114 | GrafiteAst.Fourier _ -> Tactics.fourier
115 | GrafiteAst.FwdSimpl (_, hyp, names) ->
116 Tactics.fwd_simpl ~mk_fresh_name_callback:(namer_of names)
117 ~dbd:(LibraryDb.instance ()) hyp
118 | GrafiteAst.Generalize (_,pattern,ident) ->
119 let names = match ident with None -> [] | Some id -> [id] in
120 Tactics.generalize ~mk_fresh_name_callback:(namer_of names) pattern
121 | GrafiteAst.Goal (_, n) -> Tactics.set_goal n
122 | GrafiteAst.IdTac _ -> Tactics.id
123 | GrafiteAst.Injection (_,term) -> Tactics.injection term
124 | GrafiteAst.Intros (_, None, names) ->
125 PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
126 | GrafiteAst.Intros (_, Some num, names) ->
127 PrimitiveTactics.intros_tac ~howmany:num
128 ~mk_fresh_name_callback:(namer_of names) ()
129 | GrafiteAst.LApply (_, how_many, to_what, what, ident) ->
130 let names = match ident with None -> [] | Some id -> [id] in
131 Tactics.lapply ~mk_fresh_name_callback:(namer_of names) ?how_many
133 | GrafiteAst.Left _ -> Tactics.left
134 | GrafiteAst.LetIn (loc,term,name) ->
135 Tactics.letin term ~mk_fresh_name_callback:(namer_of [name])
136 | GrafiteAst.Reduce (_, reduction_kind, pattern) ->
137 (match reduction_kind with
138 | `Normalize -> Tactics.normalize ~pattern
139 | `Reduce -> Tactics.reduce ~pattern
140 | `Simpl -> Tactics.simpl ~pattern
141 | `Unfold what -> Tactics.unfold ~pattern what
142 | `Whd -> Tactics.whd ~pattern)
143 | GrafiteAst.Reflexivity _ -> Tactics.reflexivity
144 | GrafiteAst.Replace (_, pattern, with_what) ->
145 Tactics.replace ~pattern ~with_what
146 | GrafiteAst.Rewrite (_, direction, t, pattern) ->
147 EqualityTactics.rewrite_tac ~direction ~pattern t
148 | GrafiteAst.Right _ -> Tactics.right
149 | GrafiteAst.Ring _ -> Tactics.ring
150 | GrafiteAst.Split _ -> Tactics.split
151 | GrafiteAst.Symmetry _ -> Tactics.symmetry
152 | GrafiteAst.Transitivity (_, term) -> Tactics.transitivity term
154 let singleton = function
158 (** @param term not meaningful when context is given *)
159 let disambiguate_term ?context status_ref goal term =
160 let status = !status_ref in
164 | None -> MatitaTypes.get_proof_context status goal
166 let (diff, metasenv, cic, _) =
168 (MatitaDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
169 ~aliases:status.aliases ~universe:(Some status.multi_aliases)
170 ~context ~metasenv:(MatitaTypes.get_proof_metasenv status) term)
172 let status = MatitaTypes.set_metasenv metasenv status in
173 let status = MatitaSync.set_proof_aliases status diff in
174 status_ref := status;
177 (** disambiguate_lazy_term (circa): term -> (unit -> status) * lazy_term
178 * rationale: lazy_term will be invoked in different context to obtain a term,
179 * each invocation will disambiguate the term and can add aliases. Once all
180 * disambiguations have been performed, the first returned function can be
181 * used to obtain the resulting aliases *)
182 let disambiguate_lazy_term status_ref term =
183 (fun context metasenv ugraph ->
184 let status = !status_ref in
185 let (diff, metasenv, cic, ugraph) =
187 (MatitaDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
188 ~initial_ugraph:ugraph ~aliases:status.aliases
189 ~universe:(Some status.multi_aliases) ~context ~metasenv term)
191 let status = MatitaTypes.set_metasenv metasenv status in
192 let status = MatitaSync.set_proof_aliases status diff in
193 status_ref := status;
194 cic, metasenv, ugraph)
196 let disambiguate_pattern status_ref (wanted, hyp_paths, goal_path) =
197 let interp path = Disambiguate.interpretate_path [] path in
198 let goal_path = interp goal_path in
199 let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
204 let wanted = disambiguate_lazy_term status_ref wanted in
207 (wanted, hyp_paths ,goal_path)
209 let disambiguate_reduction_kind aliases_ref = function
210 | `Unfold (Some t) ->
211 let t = disambiguate_lazy_term aliases_ref t in
217 | `Whd as kind -> kind
219 let disambiguate_tactic status goal tactic =
220 let status_ref = ref status in
223 | GrafiteAst.Absurd (loc, term) ->
224 let cic = disambiguate_term status_ref goal term in
225 GrafiteAst.Absurd (loc, cic)
226 | GrafiteAst.Apply (loc, term) ->
227 let cic = disambiguate_term status_ref goal term in
228 GrafiteAst.Apply (loc, cic)
229 | GrafiteAst.Assumption loc -> GrafiteAst.Assumption loc
230 | GrafiteAst.Auto (loc,depth,width,paramodulation,full) ->
231 GrafiteAst.Auto (loc,depth,width,paramodulation,full)
232 | GrafiteAst.Change (loc, pattern, with_what) ->
233 let with_what = disambiguate_lazy_term status_ref with_what in
234 let pattern = disambiguate_pattern status_ref pattern in
235 GrafiteAst.Change (loc, pattern, with_what)
236 | GrafiteAst.Clear (loc,id) -> GrafiteAst.Clear (loc,id)
237 | GrafiteAst.ClearBody (loc,id) -> GrafiteAst.ClearBody (loc,id)
238 | GrafiteAst.Compare (loc,term) ->
239 let term = disambiguate_term status_ref goal term in
240 GrafiteAst.Compare (loc,term)
241 | GrafiteAst.Constructor (loc,n) -> GrafiteAst.Constructor (loc,n)
242 | GrafiteAst.Contradiction loc -> GrafiteAst.Contradiction loc
243 | GrafiteAst.Cut (loc, ident, term) ->
244 let cic = disambiguate_term status_ref goal term in
245 GrafiteAst.Cut (loc, ident, cic)
246 | GrafiteAst.DecideEquality loc -> GrafiteAst.DecideEquality loc
247 | GrafiteAst.Decompose (loc, types, what, names) ->
248 let disambiguate types = function
249 | GrafiteAst.Type _ -> assert false
250 | GrafiteAst.Ident id ->
251 (match disambiguate_term status_ref goal
252 (CicNotationPt.Ident (id, None))
254 | Cic.MutInd (uri, tyno, _) ->
255 (GrafiteAst.Type (uri, tyno) :: types)
256 | _ -> raise (MatitaDisambiguator.DisambiguationError (0,[[None,lazy "Decompose works only on inductive types"]])))
258 let types = List.fold_left disambiguate [] types in
259 GrafiteAst.Decompose (loc, types, what, names)
260 | GrafiteAst.Discriminate (loc,term) ->
261 let term = disambiguate_term status_ref goal term in
262 GrafiteAst.Discriminate(loc,term)
263 | GrafiteAst.Exact (loc, term) ->
264 let cic = disambiguate_term status_ref goal term in
265 GrafiteAst.Exact (loc, cic)
266 | GrafiteAst.Elim (loc, what, Some using, depth, idents) ->
267 let what = disambiguate_term status_ref goal what in
268 let using = disambiguate_term status_ref goal using in
269 GrafiteAst.Elim (loc, what, Some using, depth, idents)
270 | GrafiteAst.Elim (loc, what, None, depth, idents) ->
271 let what = disambiguate_term status_ref goal what in
272 GrafiteAst.Elim (loc, what, None, depth, idents)
273 | GrafiteAst.ElimType (loc, what, Some using, depth, idents) ->
274 let what = disambiguate_term status_ref goal what in
275 let using = disambiguate_term status_ref goal using in
276 GrafiteAst.ElimType (loc, what, Some using, depth, idents)
277 | GrafiteAst.ElimType (loc, what, None, depth, idents) ->
278 let what = disambiguate_term status_ref goal what in
279 GrafiteAst.ElimType (loc, what, None, depth, idents)
280 | GrafiteAst.Exists loc -> GrafiteAst.Exists loc
281 | GrafiteAst.Fail loc -> GrafiteAst.Fail loc
282 | GrafiteAst.Fold (loc,red_kind, term, pattern) ->
283 let pattern = disambiguate_pattern status_ref pattern in
284 let term = disambiguate_lazy_term status_ref term in
285 let red_kind = disambiguate_reduction_kind status_ref red_kind in
286 GrafiteAst.Fold (loc, red_kind, term, pattern)
287 | GrafiteAst.FwdSimpl (loc, hyp, names) ->
288 GrafiteAst.FwdSimpl (loc, hyp, names)
289 | GrafiteAst.Fourier loc -> GrafiteAst.Fourier loc
290 | GrafiteAst.Generalize (loc,pattern,ident) ->
291 let pattern = disambiguate_pattern status_ref pattern in
292 GrafiteAst.Generalize (loc,pattern,ident)
293 | GrafiteAst.Goal (loc, g) -> GrafiteAst.Goal (loc, g)
294 | GrafiteAst.IdTac loc -> GrafiteAst.IdTac loc
295 | GrafiteAst.Injection (loc, term) ->
296 let term = disambiguate_term status_ref goal term in
297 GrafiteAst.Injection (loc,term)
298 | GrafiteAst.Intros (loc, num, names) -> GrafiteAst.Intros (loc, num, names)
299 | GrafiteAst.LApply (loc, depth, to_what, what, ident) ->
301 let term = disambiguate_term status_ref goal term in
304 let to_what = List.fold_right f to_what [] in
305 let what = disambiguate_term status_ref goal what in
306 GrafiteAst.LApply (loc, depth, to_what, what, ident)
307 | GrafiteAst.Left loc -> GrafiteAst.Left loc
308 | GrafiteAst.LetIn (loc, term, name) ->
309 let term = disambiguate_term status_ref goal term in
310 GrafiteAst.LetIn (loc,term,name)
311 | GrafiteAst.Reduce (loc, red_kind, pattern) ->
312 let pattern = disambiguate_pattern status_ref pattern in
313 let red_kind = disambiguate_reduction_kind status_ref red_kind in
314 GrafiteAst.Reduce(loc, red_kind, pattern)
315 | GrafiteAst.Reflexivity loc -> GrafiteAst.Reflexivity loc
316 | GrafiteAst.Replace (loc, pattern, with_what) ->
317 let pattern = disambiguate_pattern status_ref pattern in
318 let with_what = disambiguate_lazy_term status_ref with_what in
319 GrafiteAst.Replace (loc, pattern, with_what)
320 | GrafiteAst.Rewrite (loc, dir, t, pattern) ->
321 let term = disambiguate_term status_ref goal t in
322 let pattern = disambiguate_pattern status_ref pattern in
323 GrafiteAst.Rewrite (loc, dir, term, pattern)
324 | GrafiteAst.Right loc -> GrafiteAst.Right loc
325 | GrafiteAst.Ring loc -> GrafiteAst.Ring loc
326 | GrafiteAst.Split loc -> GrafiteAst.Split loc
327 | GrafiteAst.Symmetry loc -> GrafiteAst.Symmetry loc
328 | GrafiteAst.Transitivity (loc, term) ->
329 let cic = disambiguate_term status_ref goal term in
330 GrafiteAst.Transitivity (loc, cic)
334 let reorder_metasenv start refine tactic goals current_goal always_opens_a_goal=
335 let module PEH = ProofEngineHelpers in
336 (* let print_m name metasenv =
337 prerr_endline (">>>>> " ^ name);
338 prerr_endline (CicMetaSubst.ppmetasenv [] metasenv)
340 (* phase one calculates:
341 * new_goals_from_refine: goals added by refine
342 * head_goal: the first goal opened by ythe tactic
343 * other_goals: other goals opened by the tactic
345 let new_goals_from_refine = PEH.compare_metasenvs start refine in
346 let new_goals_from_tactic = PEH.compare_metasenvs refine tactic in
347 let head_goal, other_goals, goals =
349 | [] -> None,[],goals
351 (* assert (List.mem hd new_goals_from_tactic);
352 * invalidato dalla goal_tac
354 Some hd, List.filter ((<>) hd) new_goals_from_tactic, List.filter ((<>)
359 | None -> new_goals_from_refine @ other_goals
360 | Some x -> x :: new_goals_from_refine @ other_goals
362 (* extract the metas generated by refine and tactic *)
363 let metas_for_tactic_head =
366 | Some head_goal -> List.filter (fun (n,_,_) -> n = head_goal) tactic in
367 let metas_for_tactic_goals =
369 (fun x -> List.find (fun (metano,_,_) -> metano = x) tactic)
372 let metas_for_refine_goals =
373 List.filter (fun (n,_,_) -> List.mem n new_goals_from_refine) tactic in
374 let produced_metas, goals =
376 if always_opens_a_goal then
377 metas_for_tactic_head @ metas_for_refine_goals @
378 metas_for_tactic_goals
380 (* print_m "metas_for_refine_goals" metas_for_refine_goals;
381 print_m "metas_for_tactic_head" metas_for_tactic_head;
382 print_m "metas_for_tactic_goals" metas_for_tactic_goals; *)
383 metas_for_refine_goals @ metas_for_tactic_head @
384 metas_for_tactic_goals
387 let goals = List.map (fun (metano, _, _) -> metano) produced_metas in
388 produced_metas, goals
390 (* residual metas, preserving the original order *)
395 | (metano, _, _) :: tl when metano = e ->
396 [], List.map (fun (x,_,_) -> x) tl
397 | (metano, _, _) :: tl -> let b, a = split e tl in metano :: b, a
399 let find n metasenv =
401 Some (List.find (fun (metano, _, _) -> metano = n) metasenv)
402 with Not_found -> None
407 match find n tactic with
411 let before_l, after_l = split current_goal start in
413 List.filter (fun x -> not (List.mem x produced_goals)) before_l in
415 List.filter (fun x -> not (List.mem x produced_goals)) after_l in
416 let before = extract before_l in
417 let after = extract after_l in
421 print_m "BEGIN" start;
422 prerr_endline ("goal was: " ^ string_of_int current_goal);
423 prerr_endline ("and metas from refine are:");
425 (fun t -> prerr_string (" " ^ string_of_int t))
426 new_goals_from_refine;
428 print_m "before" before;
429 print_m "metas_for_tactic_head" metas_for_tactic_head;
430 print_m "metas_for_refine_goals" metas_for_refine_goals;
431 print_m "metas_for_tactic_goals" metas_for_tactic_goals;
432 print_m "produced_metas" produced_metas;
433 print_m "after" after;
434 |+ FINE DEBUG CODE +| *)
435 before @ produced_metas @ after, goals
437 (* maybe we only need special cases for apply and goal *)
438 let classify_tactic tactic =
440 (* tactics that can't close the goal (return a goal we want to "select") *)
441 | GrafiteAst.Rewrite _
443 | GrafiteAst.Replace _
444 | GrafiteAst.Reduce _
445 | GrafiteAst.Injection _
447 | GrafiteAst.Generalize _
450 | GrafiteAst.Decompose _ -> true, true
451 (* tactics we don't want to reorder goals. I think only Goal needs this. *)
452 | GrafiteAst.Goal _ -> false, true
453 (* tactics like apply *)
456 let apply_tactic tactic (status, goal) =
457 (* prerr_endline "apply_tactic"; *)
458 (* prerr_endline (Continuationals.Stack.pp (MatitaTypes.get_stack status)); *)
459 let starting_metasenv = MatitaTypes.get_proof_metasenv status in
460 let before = List.map (fun g, _, _ -> g) starting_metasenv in
461 (* prerr_endline "disambiguate"; *)
462 let status_ref, tactic = disambiguate_tactic status goal tactic in
463 let metasenv_after_refinement = MatitaTypes.get_proof_metasenv !status_ref in
464 let proof = MatitaTypes.get_current_proof !status_ref in
465 let proof_status = proof, goal in
466 let needs_reordering, always_opens_a_goal = classify_tactic tactic in
467 let tactic = tactic_of_ast tactic in
468 (* apply tactic will change the status pointed by status_ref ... *)
469 (* prerr_endline "apply_tactic bassa"; *)
470 let (proof, opened) = ProofEngineTypes.apply_tactic tactic proof_status in
471 let after = ProofEngineTypes.goals_of_proof proof in
472 let opened_goals, closed_goals = Tacticals.goals_diff ~before ~after ~opened in
473 (* prerr_endline("before: " ^ String.concat ", " (List.map string_of_int before));
474 prerr_endline("after: " ^ String.concat ", " (List.map string_of_int after));
475 prerr_endline("opened: " ^ String.concat ", " (List.map string_of_int opened)); *)
476 (* prerr_endline("opened_goals: " ^ String.concat ", " (List.map string_of_int opened_goals));
477 prerr_endline("closed_goals: " ^ String.concat ", " (List.map string_of_int closed_goals)); *)
478 let proof, opened_goals =
479 if needs_reordering then begin
480 let uri, metasenv_after_tactic, t, ty = proof in
481 (* prerr_endline ("goal prima del riordino: " ^ String.concat " " (List.map string_of_int (ProofEngineTypes.goals_of_proof proof))); *)
482 let reordered_metasenv, opened_goals =
485 metasenv_after_refinement metasenv_after_tactic
486 opened goal always_opens_a_goal
488 let proof' = uri, reordered_metasenv, t, ty in
489 (* prerr_endline ("goal dopo il riordino: " ^ String.concat " " (List.map string_of_int (ProofEngineTypes.goals_of_proof proof'))); *)
495 let incomplete_proof =
496 match !status_ref.proof_status with
497 | Incomplete_proof p -> p
500 { !status_ref with proof_status =
501 Incomplete_proof { incomplete_proof with proof = proof } },
502 opened_goals, closed_goals
504 module MatitaStatus =
506 type input_status = MatitaTypes.status * ProofEngineTypes.goal
509 MatitaTypes.status * ProofEngineTypes.goal list * ProofEngineTypes.goal list
511 type tactic = input_status -> output_status
513 let id_tactic = apply_tactic (GrafiteAst.IdTac DisambiguateTypes.dummy_floc)
514 let mk_tactic tac = tac
515 let apply_tactic tac = tac
516 let goals (_, opened, closed) = opened, closed
517 let set_goals (opened, closed) (status, _, _) = (status, opened, closed)
518 let get_stack (status, _) = MatitaTypes.get_stack status
520 let set_stack stack (status, opened, closed) =
521 MatitaTypes.set_stack stack status, opened, closed
523 let inject (status, _) = (status, [], [])
524 let focus goal (status, _, _) = (status, goal)
527 module MatitaTacticals = Tacticals.Make (MatitaStatus)
529 let eval_tactical status tac =
530 let rec tactical_of_ast l tac =
532 | GrafiteAst.Tactic (loc, tactic) ->
533 MatitaTacticals.tactic (MatitaStatus.mk_tactic (apply_tactic tactic))
534 | GrafiteAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
536 MatitaTacticals.seq ~tactics:(List.map (tactical_of_ast (l+1)) tacticals)
537 | GrafiteAst.Do (loc, n, tactical) ->
538 MatitaTacticals.do_tactic ~n ~tactic:(tactical_of_ast (l+1) tactical)
539 | GrafiteAst.Repeat (loc, tactical) ->
540 MatitaTacticals.repeat_tactic ~tactic:(tactical_of_ast (l+1) tactical)
541 | GrafiteAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
543 MatitaTacticals.thens ~start:(tactical_of_ast (l+1) tactical)
544 ~continuations:(List.map (tactical_of_ast (l+1)) tacticals)
545 | GrafiteAst.First (loc, tacticals) ->
546 MatitaTacticals.first
547 ~tactics:(List.map (fun t -> "", tactical_of_ast (l+1) t) tacticals)
548 | GrafiteAst.Try (loc, tactical) ->
549 MatitaTacticals.try_tactic ~tactic:(tactical_of_ast (l+1) tactical)
550 | GrafiteAst.Solve (loc, tacticals) ->
551 MatitaTacticals.solve_tactics
552 ~tactics:(List.map (fun t -> "", tactical_of_ast (l+1) t) tacticals)
554 | GrafiteAst.Skip loc -> MatitaTacticals.skip
555 | GrafiteAst.Dot loc -> MatitaTacticals.dot
556 | GrafiteAst.Semicolon loc -> MatitaTacticals.semicolon
557 | GrafiteAst.Branch loc -> MatitaTacticals.branch
558 | GrafiteAst.Shift loc -> MatitaTacticals.shift
559 | GrafiteAst.Pos (loc, i) -> MatitaTacticals.pos i
560 | GrafiteAst.Merge loc -> MatitaTacticals.merge
561 | GrafiteAst.Focus (loc, goals) -> MatitaTacticals.focus goals
562 | GrafiteAst.Unfocus loc -> MatitaTacticals.unfocus
564 let status, _, _ = tactical_of_ast 0 tac (status, ~-1) in
565 let status = (* is proof completed? *)
566 match status.proof_status with
567 | Incomplete_proof { stack = stack; proof = proof }
568 when Continuationals.Stack.is_empty stack ->
569 { status with proof_status = Proof proof }
574 let eval_coercion status ~add_composites coercion =
575 let uri = CicUtil.uri_of_term coercion in
576 let status = MatitaSync.add_coercion status ~add_composites uri in
577 {status with proof_status = No_proof}
579 (* to avoid a long list of recursive functions *)
580 let eval_from_moo_ref = ref (fun _ _ _ -> assert false);;
582 let disambiguate_obj status obj =
585 | CicNotationPt.Inductive (_,(name,_,_,_)::_)
586 | CicNotationPt.Record (_,name,_,_) ->
587 Some (UriManager.uri_of_string (MatitaTypes.qualify status name ^ ".ind"))
588 | CicNotationPt.Inductive _ -> assert false
589 | CicNotationPt.Theorem _ -> None in
590 let (diff, metasenv, cic, _) =
592 (MatitaDisambiguator.disambiguate_obj ~dbd:(LibraryDb.instance ())
593 ~aliases:status.aliases ~universe:(Some status.multi_aliases) ~uri obj)
596 match status.proof_status with
597 | No_proof -> Intermediate metasenv
599 | Proof _ -> command_error "imbricated proofs not allowed"
600 | Intermediate _ -> assert false
602 let status = { status with proof_status = proof_status } in
603 let status = MatitaSync.set_proof_aliases status diff in
606 let disambiguate_command status =
609 | GrafiteAst.Default _
612 | GrafiteAst.Include _
613 | GrafiteAst.Interpretation _
614 | GrafiteAst.Metadata _
615 | GrafiteAst.Notation _
617 | GrafiteAst.Render _
618 | GrafiteAst.Set _ as cmd ->
620 | GrafiteAst.Coercion (loc, term, add_composites) ->
621 let status_ref = ref status in
622 let term = disambiguate_term ~context:[] status_ref ~-1 term in
623 !status_ref, GrafiteAst.Coercion (loc,term,add_composites)
624 | GrafiteAst.Obj (loc,obj) ->
625 let status,obj = disambiguate_obj status obj in
626 status, GrafiteAst.Obj (loc,obj)
628 let make_absolute paths path =
629 if path = "coq.ma" then path
631 let rec aux = function
632 | [] -> ignore (Unix.stat path); path
634 let path = p ^ "/" ^ path in
636 ignore (Unix.stat path); path
637 with Unix.Unix_error _ -> aux tl
641 with Unix.Unix_error _ -> raise (UnableToInclude path)
644 let eval_command opts status cmd =
645 let status,cmd = disambiguate_command status cmd in
646 let cmd,notation_ids' = CicNotation.process_notation cmd in
648 { status with notation_ids = notation_ids' @ status.notation_ids } in
649 let basedir = Helm_registry.get "matita.basedir" in
651 | GrafiteAst.Default (loc, what, uris) as cmd ->
652 LibraryObjects.set_default what uris;
653 add_moo_content [cmd] status
654 | GrafiteAst.Include (loc, path) ->
655 let absolute_path = make_absolute opts.include_paths path in
656 let moopath = MatitaMisc.obj_file_of_script ~basedir absolute_path in
657 let status = ref status in
658 if not (Sys.file_exists moopath) then
659 raise (IncludedFileNotCompiled moopath);
660 !eval_from_moo_ref status moopath (fun _ _ -> ());
662 | GrafiteAst.Metadata (loc, m) ->
664 | GrafiteAst.Dependency uri -> MatitaTypes.add_moo_metadata [m] status
665 | GrafiteAst.Baseuri _ -> status)
666 | GrafiteAst.Set (loc, name, value) ->
668 if name = "baseuri" then begin
670 let v = MatitaMisc.strip_trailing_slash value in
672 ignore (String.index v ' ');
673 command_error "baseuri can't contain spaces"
676 if not (MatitaMisc.is_empty value) && opts.clean_baseuri then begin
677 HLog.warn ("baseuri " ^ value ^ " is not empty");
678 HLog.message ("cleaning baseuri " ^ value);
679 LibraryClean.clean_baseuris ~basedir [value]
681 add_moo_metadata [GrafiteAst.Baseuri value] status
685 set_option status name value
686 | GrafiteAst.Drop loc -> raise Drop
687 | GrafiteAst.Qed loc ->
688 let uri, metasenv, bo, ty =
689 match status.proof_status with
690 | Proof (Some uri, metasenv, body, ty) ->
691 uri, metasenv, body, ty
692 | Proof (None, metasenv, body, ty) ->
694 ("Someone allows to start a thm without giving the "^
695 "name/uri. This should be fixed!")
696 | _-> command_error "You can't Qed an incomplete theorem"
698 if metasenv <> [] then
699 command_error "Proof not completed! metasenv is not empty!";
700 let name = UriManager.name_of_uri uri in
701 let obj = Cic.Constant (name,Some bo,ty,[],[]) in
702 MatitaSync.add_obj uri obj {status with proof_status = No_proof}
703 | GrafiteAst.Coercion (loc, coercion, add_composites) ->
704 eval_coercion status ~add_composites coercion
705 | GrafiteAst.Alias (loc, spec) ->
707 (*CSC: Warning: this code should be factorized with the corresponding
708 code in DisambiguatePp *)
710 | GrafiteAst.Ident_alias (id,uri) ->
711 [DisambiguateTypes.Id id,
712 (uri,(fun _ _ _-> CicUtil.term_of_uri(UriManager.uri_of_string uri)))]
713 | GrafiteAst.Symbol_alias (symb, instance, desc) ->
714 [DisambiguateTypes.Symbol (symb,instance),
715 DisambiguateChoices.lookup_symbol_by_dsc symb desc]
716 | GrafiteAst.Number_alias (instance,desc) ->
717 [DisambiguateTypes.Num instance,
718 DisambiguateChoices.lookup_num_by_dsc desc]
720 MatitaSync.set_proof_aliases status diff
721 | GrafiteAst.Render _ -> assert false (* ZACK: to be removed *)
722 | GrafiteAst.Dump _ -> assert false (* ZACK: to be removed *)
723 | GrafiteAst.Interpretation (_, dsc, (symbol, _), cic_appl_pattern) as stm ->
724 let status = add_moo_content [stm] status in
727 (fun uri -> GrafiteAst.Dependency (UriManager.buri_of_uri uri))
728 (CicNotationUtil.find_appl_pattern_uris cic_appl_pattern)
731 [DisambiguateTypes.Symbol (symbol, 0),
732 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc]
734 let status = MatitaSync.set_proof_aliases status diff in
735 let status = MatitaTypes.add_moo_metadata uris status in
737 | GrafiteAst.Notation _ as stm -> add_moo_content [stm] status
738 | GrafiteAst.Obj (loc,obj) ->
741 Cic.Constant (name,_,_,_,_)
742 | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
743 | Cic.InductiveDefinition (types,_,_,_) ->
745 (match types with (name,_,_,_)::_ -> name | _ -> assert false)
746 | _ -> assert false in
748 UriManager.uri_of_string (MatitaTypes.qualify status name ^ ext)
750 let metasenv = MatitaTypes.get_proof_metasenv status in
752 | Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
753 let name = UriManager.name_of_uri uri in
754 if not(CicPp.check name ty) then
755 HLog.error ("Bad name: " ^ name);
756 if opts.do_heavy_checks then
758 let dbd = LibraryDb.instance () in
759 let similar = Whelp.match_term ~dbd ty in
760 let similar_len = List.length similar in
761 if similar_len> 30 then
763 ("Duplicate check will compare your theorem with " ^
764 string_of_int similar_len ^
765 " theorems, this may take a while."));
769 let t = CicUtil.term_of_uri u in
771 CicTypeChecker.type_of_aux'
772 metasenv' [] t CicUniv.empty_ugraph
774 fst(CicReduction.are_convertible [] ty' ty g))
777 (match convertible with
781 ("Theorem already proved: " ^ UriManager.string_of_uri x ^
782 "\nPlease use a variant."));
784 assert (metasenv = metasenv');
786 match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false
788 let initial_proof = (Some uri, metasenv, bo, ty) in
789 let initial_stack = Continuationals.Stack.of_metasenv metasenv in
790 { status with proof_status =
791 Incomplete_proof { proof = initial_proof; stack = initial_stack } }
793 if metasenv <> [] then
795 "metasenv not empty while giving a definition with body: " ^
796 CicMetaSubst.ppmetasenv [] metasenv);
797 MatitaSync.add_obj uri obj {status with proof_status = No_proof}
799 let eval_executable opts status ex =
801 | GrafiteAst.Tactical (_, tac, None) -> eval_tactical status tac
802 | GrafiteAst.Tactical (_, tac, Some punct) ->
803 let status = eval_tactical status tac in
804 eval_tactical status punct
805 | GrafiteAst.Command (_, cmd) -> eval_command opts status cmd
806 | GrafiteAst.Macro (_, mac) ->
807 command_error (sprintf "The macro %s can't be in a script"
808 (GrafiteAstPp.pp_macro_ast mac))
810 let eval_comment status c = status
813 ?(do_heavy_checks=false) ?(include_paths=[]) ?(clean_baseuri=true) status st
816 do_heavy_checks = do_heavy_checks ;
817 include_paths = include_paths;
818 clean_baseuri = clean_baseuri }
821 | GrafiteAst.Executable (_,ex) -> eval_executable opts status ex
822 | GrafiteAst.Comment (_,c) -> eval_comment status c
824 let eval_from_moo ?do_heavy_checks ?include_paths ?clean_baseuri status fname cb
827 GrafiteAst.Executable (DisambiguateTypes.dummy_floc,
828 GrafiteAst.Command (DisambiguateTypes.dummy_floc,
829 (GrafiteAst.reash_cmd_uris cmd)))
831 let moo, metadata = GrafiteMarshal.load_moo fname in
834 let ast = ast_of_cmd ast in
837 eval_ast ?do_heavy_checks ?include_paths ?clean_baseuri !status ast)
842 ast_of_cmd (GrafiteAst.Metadata (DisambiguateTypes.dummy_floc, m))
846 eval_ast ?do_heavy_checks ?include_paths ?clean_baseuri !status ast)
850 ?do_heavy_checks ?include_paths ?clean_baseuri status str cb
854 let ast = GrafiteParser.parse_statement str in
857 eval_ast ?do_heavy_checks ?include_paths ?clean_baseuri !status ast
859 with End_of_file -> ()
861 (* to avoid a long list of recursive functions *)
862 let _ = eval_from_moo_ref := eval_from_moo
864 let eval_from_stream_greedy
865 ?do_heavy_checks ?include_paths ?clean_baseuri status str cb
868 print_string "matita> ";
870 let ast = GrafiteParser.parse_statement str in
872 status := eval_ast ?do_heavy_checks ?include_paths ?clean_baseuri !status ast
876 let eval_string ?do_heavy_checks ?include_paths ?clean_baseuri status str =
878 ?do_heavy_checks ?include_paths ?clean_baseuri status
879 (Ulexing.from_utf8_string str) (fun _ _ -> ())
881 let default_options () = no_options
885 aliases = DisambiguateTypes.Environment.empty;
886 multi_aliases = DisambiguateTypes.Environment.empty;
887 moo_content_rev = [], [];
888 proof_status = No_proof;
889 options = default_options ();