8 let debug_print = if debug then prerr_endline else ignore ;;
10 (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
11 * names as long as they are available, then it fallbacks to name generation
12 * using FreshNamesGenerator module *)
14 let len = List.length names in
16 fun metasenv context name ~typ ->
17 if !count < len then begin
18 let name = Cic.Name (List.nth names !count) in
22 FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context name ~typ
24 let tactic_of_ast = function
25 | TacticAst.Absurd (_, term) -> Tactics.absurd term
26 | TacticAst.Apply (_, term) -> Tactics.apply term
27 | TacticAst.Assumption _ -> Tactics.assumption
28 | TacticAst.Auto (_,depth,width) ->
29 AutoTactic.auto_tac ?depth ?width ~dbd:(MatitaDb.instance ()) ()
30 | TacticAst.Change (_, pattern, with_what) ->
31 Tactics.change ~pattern with_what
32 | TacticAst.Clear (_,id) -> Tactics.clear id
33 | TacticAst.ClearBody (_,id) -> Tactics.clearbody id
34 | TacticAst.Contradiction _ -> Tactics.contradiction
35 | TacticAst.Compare (_, term) -> Tactics.compare term
36 | TacticAst.Constructor (_, n) -> Tactics.constructor n
37 | TacticAst.Cut (_, ident, term) ->
38 let names = match ident with None -> [] | Some id -> [id] in
39 Tactics.cut ~mk_fresh_name_callback:(namer_of names) term
40 | TacticAst.DecideEquality _ -> Tactics.decide_equality
41 | TacticAst.Decompose (_,term) -> Tactics.decompose term
42 | TacticAst.Discriminate (_,term) -> Tactics.discriminate term
43 | TacticAst.Elim (_, term, _) ->
44 Tactics.elim_intros term
45 | TacticAst.ElimType (_, term) -> Tactics.elim_type term
46 | TacticAst.Exact (_, term) -> Tactics.exact term
47 | TacticAst.Exists _ -> Tactics.exists
48 | TacticAst.Fail _ -> Tactics.fail
49 | TacticAst.Fold (_, reduction_kind, pattern) ->
51 match reduction_kind with
52 | `Normalize -> CicReduction.normalize ~delta:false ~subst:[]
53 | `Reduce -> ProofEngineReduction.reduce
54 | `Simpl -> ProofEngineReduction.simpl
55 | `Whd -> CicReduction.whd ~delta:false ~subst:[]
57 Tactics.fold ~reduction ~pattern
58 | TacticAst.Fourier _ -> Tactics.fourier
59 | TacticAst.FwdSimpl (_, term) ->
60 Tactics.fwd_simpl ~what:term ~dbd:(MatitaDb.instance ())
61 | TacticAst.Generalize (_,pattern,ident) ->
62 let names = match ident with None -> [] | Some id -> [id] in
63 Tactics.generalize ~mk_fresh_name_callback:(namer_of names) pattern
64 | TacticAst.Goal (_, n) -> Tactics.set_goal n
65 | TacticAst.IdTac _ -> Tactics.id
66 | TacticAst.Injection (_,term) -> Tactics.injection term
67 | TacticAst.Intros (_, None, names) ->
68 PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
69 | TacticAst.Intros (_, Some num, names) ->
70 PrimitiveTactics.intros_tac ~howmany:num
71 ~mk_fresh_name_callback:(namer_of names) ()
72 | TacticAst.LApply (_, to_what, what, ident) ->
73 let names = match ident with None -> [] | Some id -> [id] in
74 Tactics.lapply ~mk_fresh_name_callback:(namer_of names) ?to_what what
75 | TacticAst.Left _ -> Tactics.left
76 | TacticAst.LetIn (loc,term,name) ->
77 Tactics.letin term ~mk_fresh_name_callback:(namer_of [name])
78 | TacticAst.Reduce (_, reduction_kind, pattern) ->
79 (match reduction_kind with
80 | `Normalize -> Tactics.normalize ~pattern
81 | `Reduce -> Tactics.reduce ~pattern
82 | `Simpl -> Tactics.simpl ~pattern
83 | `Whd -> Tactics.whd ~pattern)
84 | TacticAst.Reflexivity _ -> Tactics.reflexivity
85 | TacticAst.Replace (_, pattern, with_what) ->
86 Tactics.replace ~pattern ~with_what
87 | TacticAst.Rewrite (_, direction, t, pattern) ->
88 EqualityTactics.rewrite_tac ~direction ~pattern t
89 | TacticAst.Right _ -> Tactics.right
90 | TacticAst.Ring _ -> Tactics.ring
91 | TacticAst.Split _ -> Tactics.split
92 | TacticAst.Symmetry _ -> Tactics.symmetry
93 | TacticAst.Transitivity (_, term) -> Tactics.transitivity term
95 let eval_tactical status tac =
96 let apply_tactic tactic =
98 ProofEngineTypes.apply_tactic tactic (MatitaMisc.get_proof_status status)
103 let (_,metasenv,_,_) = proof in
106 | (ng,_,_)::_ -> Incomplete_proof (proof,ng))
107 | ng::_ -> Incomplete_proof (proof, ng)
109 { status with proof_status = new_status }
111 let rec tactical_of_ast = function
112 | TacticAst.Tactic (loc, tactic) -> tactic_of_ast tactic
113 | TacticAst.Do (loc, num, tactical) ->
114 Tacticals.do_tactic num (tactical_of_ast tactical)
115 | TacticAst.Repeat (loc, tactical) ->
116 Tacticals.repeat_tactic (tactical_of_ast tactical)
117 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
118 Tacticals.seq (List.map tactical_of_ast tacticals)
119 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
120 Tacticals.thens (tactical_of_ast tactical)
121 (List.map tactical_of_ast tacticals)
122 | TacticAst.Tries (loc, tacticals) ->
123 Tacticals.try_tactics
124 (List.map (fun t -> "", tactical_of_ast t) tacticals)
125 | TacticAst.Try (loc, tactical) ->
126 Tacticals.try_tactic (tactical_of_ast tactical)
128 apply_tactic (tactical_of_ast tac)
130 let eval_coercion status coercion =
131 let coer_uri,coer_ty =
136 CicEnvironment.get_obj CicUniv.empty_ugraph uri
139 | Cic.Constant (_,_,ty,_,_)
140 | Cic.Variable (_,_,ty,_,_) ->
143 | Cic.MutConstruct (uri,t,c,_) ->
145 CicEnvironment.get_obj CicUniv.empty_ugraph uri
148 | Cic.InductiveDefinition (l,_,_,_) ->
149 let (_,_,_,cl) = List.nth l t in
150 let (_,cty) = List.nth cl c in
155 (* we have to get the source and the tgt type uri
156 * in Coq syntax we have already their names, but
157 * since we don't support Funclass and similar I think
158 * all the coercion should be of the form
160 * So we should be able to extract them from the coercion type
162 let extract_last_two_p ty =
163 let rec aux = function
164 | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))
165 | Cic.Prod( _, src, tgt) -> src, tgt
170 let ty_src,ty_tgt = extract_last_two_p coer_ty in
173 let ty_src = CicReduction.whd context ty_src in
174 CicUtil.uri_of_term ty_src
177 let ty_tgt = CicReduction.whd context ty_tgt in
178 CicUtil.uri_of_term ty_tgt
181 (* also adds them to the Db *)
182 CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri in
184 List.fold_left (fun s (uri,o,ugraph) -> MatitaSync.add_obj uri o status)
185 status new_coercions in
186 {status with proof_status = No_proof}
188 let generate_elimination_principles uri status =
189 let elim sort status =
191 let uri,obj = CicElim.elim_of ~sort uri 0 in
192 MatitaSync.add_obj uri obj status
193 with CicElim.Can_t_eliminate -> status
195 List.fold_left (fun status sort -> elim sort status) status
196 [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ]
198 let generate_projections uri fields status =
199 let projections = CicRecord.projections_of uri fields in
201 (fun status (uri, name, bo) ->
204 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
205 let bo = Unshare.unshare bo in
206 let ty = Unshare.unshare ty in
207 let attrs = [`Class `Projection; `Generated] in
208 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
209 MatitaSync.add_obj uri obj status
211 CicTypeChecker.TypeCheckerFailure s ->
213 ("Unable to create projection " ^ name ^ " cause: " ^ s);
215 | CicEnvironment.Object_not_found uri ->
216 let depend = UriManager.name_of_uri uri in
218 ("Unable to create projection " ^ name ^ " because it requires " ^ depend);
222 let eval_command status cmd =
224 | TacticAst.Set (loc, name, value) ->
226 if name = "baseuri" then
227 MatitaMisc.strip_trailing_slash value
231 set_option status name value
232 | TacticAst.Drop loc -> raise Drop
233 | TacticAst.Qed loc ->
234 let uri, metasenv, bo, ty =
235 match status.proof_status with
236 | Proof (Some uri, metasenv, body, ty) ->
237 uri, metasenv, body, ty
238 | Proof (None, metasenv, body, ty) ->
240 ("Someone allows to start a thm without giving the "^
241 "name/uri. This should be fixed!")
242 | _-> command_error "You can't qed an uncomplete theorem"
244 let suri = UriManager.string_of_uri uri in
245 if metasenv <> [] then
246 command_error "Proof not completed! metasenv is not empty!";
247 let name = UriManager.name_of_uri uri in
248 let obj = Cic.Constant (name,Some bo,ty,[],[]) in
249 MatitaSync.add_obj uri obj status
250 | TacticAst.Coercion (loc, coercion) ->
251 eval_coercion status coercion
252 | TacticAst.Alias (loc, spec) ->
254 | TacticAst.Ident_alias (id,uri) ->
255 {status with aliases =
256 DisambiguateTypes.Environment.add
257 (DisambiguateTypes.Id id)
258 ("boh?",(fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string uri)))
260 | TacticAst.Symbol_alias (symb, instance, desc) ->
261 {status with aliases =
262 DisambiguateTypes.Environment.add
263 (DisambiguateTypes.Symbol (symb,instance))
264 (DisambiguateChoices.lookup_symbol_by_dsc symb desc)
266 | TacticAst.Number_alias (instance,desc) ->
267 {status with aliases =
268 DisambiguateTypes.Environment.add
269 (DisambiguateTypes.Num instance)
270 (DisambiguateChoices.lookup_num_by_dsc desc) status.aliases })
271 | TacticAst.Obj (loc,obj) ->
274 Cic.Constant (name,_,_,_,_)
275 | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
276 | Cic.InductiveDefinition (types,_,_,_) ->
278 (match types with (name,_,_,_)::_ -> name | _ -> assert false)
279 | _ -> assert false in
281 UriManager.uri_of_string (MatitaMisc.qualify status name ^ ext)
283 let metasenv = MatitaMisc.get_proof_metasenv status in
285 Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
286 assert (metasenv = metasenv');
288 match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false in
289 let initial_proof = (Some uri, metasenv, bo, ty) in
290 { status with proof_status = Incomplete_proof (initial_proof,goalno)}
292 if metasenv <> [] then
294 "metasenv not empty while giving a definition with body: " ^
295 CicMetaSubst.ppmetasenv metasenv []);
296 let status = MatitaSync.add_obj uri obj status in
298 Cic.Constant _ -> status
299 | Cic.InductiveDefinition (_,_,_,attrs) ->
300 let status = generate_elimination_principles uri status in
301 let rec get_record_attrs =
304 | (`Class (`Record fields))::_ -> Some fields
305 | _::tl -> get_record_attrs tl
307 (match get_record_attrs attrs with
308 None -> status (* not a record *)
309 | Some fields -> generate_projections uri fields status)
311 | Cic.Variable _ -> assert false
313 let eval_executable status ex =
315 | TacticAst.Tactical (_, tac) -> eval_tactical status tac
316 | TacticAst.Command (_, cmd) -> eval_command status cmd
317 | TacticAst.Macro (_, mac) ->
318 command_error (sprintf "The macro %s can't be in a script"
319 (TacticAstPp.pp_macro_cic mac))
321 let eval_comment status c = status
325 | TacticAst.Executable (_,ex) -> eval_executable status ex
326 | TacticAst.Comment (_,c) -> eval_comment status c
328 let disambiguate_term status term =
329 let (aliases, metasenv, cic, _) =
331 MatitaDisambiguator.disambiguate_term ~dbd:(MatitaDb.instance ())
332 ~aliases:(status.aliases) ~context:(MatitaMisc.get_proof_context status)
333 ~metasenv:(MatitaMisc.get_proof_metasenv status) term
339 match status.proof_status with
340 | No_proof -> Intermediate metasenv
341 | Incomplete_proof ((uri, _, proof, ty), goal) ->
342 Incomplete_proof ((uri, metasenv, proof, ty), goal)
343 | Intermediate _ -> Intermediate metasenv
344 | Proof _ -> assert false
349 proof_status = proof_status }
353 let disambiguate_obj status obj =
356 TacticAst.Inductive (_,(name,_,_,_)::_)
357 | TacticAst.Record (_,name,_,_) ->
358 Some (UriManager.uri_of_string (MatitaMisc.qualify status name ^ ".ind"))
359 | TacticAst.Inductive _ -> assert false
361 let (aliases, metasenv, cic, _) =
363 MatitaDisambiguator.disambiguate_obj ~dbd:(MatitaDb.instance ())
364 ~aliases:(status.aliases) ~uri obj
370 match status.proof_status with
371 | No_proof -> Intermediate metasenv
374 | Proof _ -> assert false
379 proof_status = proof_status }
383 let disambiguate_pattern status (wanted, hyp_paths, goal_path) =
384 let interp path = Disambiguate.interpretate_path [] status.aliases path in
385 let goal_path = interp goal_path in
386 let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
391 let status,wanted = disambiguate_term status wanted in
394 status, (wanted, hyp_paths ,goal_path)
396 let disambiguate_tactic status = function
397 | TacticAst.Apply (loc, term) ->
398 let status, cic = disambiguate_term status term in
399 status, TacticAst.Apply (loc, cic)
400 | TacticAst.Absurd (loc, term) ->
401 let status, cic = disambiguate_term status term in
402 status, TacticAst.Absurd (loc, cic)
403 | TacticAst.Assumption loc -> status, TacticAst.Assumption loc
404 | TacticAst.Auto (loc,depth,width) -> status, TacticAst.Auto (loc,depth,width)
405 | TacticAst.Change (loc, pattern, with_what) ->
406 let status, with_what = disambiguate_term status with_what in
407 let status, pattern = disambiguate_pattern status pattern in
408 status, TacticAst.Change (loc, pattern, with_what)
409 | TacticAst.Clear (loc,id) -> status,TacticAst.Clear (loc,id)
410 | TacticAst.ClearBody (loc,id) -> status,TacticAst.ClearBody (loc,id)
411 | TacticAst.Compare (loc,term) ->
412 let status, term = disambiguate_term status term in
413 status, TacticAst.Compare (loc,term)
414 | TacticAst.Constructor (loc,n) ->
415 status, TacticAst.Constructor (loc,n)
416 | TacticAst.Contradiction loc ->
417 status, TacticAst.Contradiction loc
418 | TacticAst.Cut (loc, ident, term) ->
419 let status, cic = disambiguate_term status term in
420 status, TacticAst.Cut (loc, ident, cic)
421 | TacticAst.DecideEquality loc ->
422 status, TacticAst.DecideEquality loc
423 | TacticAst.Decompose (loc,term) ->
424 let status,term = disambiguate_term status term in
425 status, TacticAst.Decompose(loc,term)
426 | TacticAst.Discriminate (loc,term) ->
427 let status,term = disambiguate_term status term in
428 status, TacticAst.Discriminate(loc,term)
429 | TacticAst.Exact (loc, term) ->
430 let status, cic = disambiguate_term status term in
431 status, TacticAst.Exact (loc, cic)
432 | TacticAst.Elim (loc, term, Some term') ->
433 let status, cic1 = disambiguate_term status term in
434 let status, cic2 = disambiguate_term status term' in
435 status, TacticAst.Elim (loc, cic1, Some cic2)
436 | TacticAst.Elim (loc, term, None) ->
437 let status, cic = disambiguate_term status term in
438 status, TacticAst.Elim (loc, cic, None)
439 | TacticAst.ElimType (loc, term) ->
440 let status, cic = disambiguate_term status term in
441 status, TacticAst.ElimType (loc, cic)
442 | TacticAst.Exists loc -> status, TacticAst.Exists loc
443 | TacticAst.Fail loc -> status,TacticAst.Fail loc
444 | TacticAst.Fold (loc,reduction_kind, pattern) ->
445 let status, pattern = disambiguate_pattern status pattern in
446 status, TacticAst.Fold (loc,reduction_kind, pattern)
447 | TacticAst.FwdSimpl (loc, term) ->
448 let status, term = disambiguate_term status term in
449 status, TacticAst.FwdSimpl (loc, term)
450 | TacticAst.Fourier loc -> status, TacticAst.Fourier loc
451 | TacticAst.Generalize (loc,pattern,ident) ->
452 let status, pattern = disambiguate_pattern status pattern in
453 status, TacticAst.Generalize(loc,pattern,ident)
454 | TacticAst.Goal (loc, g) -> status, TacticAst.Goal (loc, g)
455 | TacticAst.IdTac loc -> status,TacticAst.IdTac loc
456 | TacticAst.Injection (loc,term) ->
457 let status, term = disambiguate_term status term in
458 status, TacticAst.Injection (loc,term)
459 | TacticAst.Intros (loc, num, names) ->
460 status, TacticAst.Intros (loc, num, names)
461 | TacticAst.LApply (loc, to_what, what, ident) ->
462 let status, to_what =
466 let status, to_what = disambiguate_term status to_what in
469 let status, what = disambiguate_term status what in
470 status, TacticAst.LApply (loc, to_what, what, ident)
471 | TacticAst.Left loc -> status, TacticAst.Left loc
472 | TacticAst.LetIn (loc, term, name) ->
473 let status, term = disambiguate_term status term in
474 status, TacticAst.LetIn (loc,term,name)
475 | TacticAst.Reduce (loc, reduction_kind, pattern) ->
476 let status, pattern = disambiguate_pattern status pattern in
477 status, TacticAst.Reduce(loc, reduction_kind, pattern)
478 | TacticAst.Reflexivity loc -> status, TacticAst.Reflexivity loc
479 | TacticAst.Replace (loc, pattern, with_what) ->
480 let status, pattern = disambiguate_pattern status pattern in
481 let status, with_what = disambiguate_term status with_what in
482 status, TacticAst.Replace (loc, pattern, with_what)
483 | TacticAst.Rewrite (loc, dir, t, pattern) ->
484 let status, term = disambiguate_term status t in
485 let status, pattern = disambiguate_pattern status pattern in
486 status, TacticAst.Rewrite (loc, dir, term, pattern)
487 | TacticAst.Right loc -> status, TacticAst.Right loc
488 | TacticAst.Ring loc -> status, TacticAst.Ring loc
489 | TacticAst.Split loc -> status, TacticAst.Split loc
490 | TacticAst.Symmetry loc -> status, TacticAst.Symmetry loc
491 | TacticAst.Transitivity (loc, term) ->
492 let status, cic = disambiguate_term status term in
493 status, TacticAst.Transitivity (loc, cic)
495 let rec disambiguate_tactical status = function
496 | TacticAst.Tactic (loc, tactic) ->
497 let status, tac = disambiguate_tactic status tactic in
498 status, TacticAst.Tactic (loc, tac)
499 | TacticAst.Do (loc, num, tactical) ->
500 let status, tac = disambiguate_tactical status tactical in
501 status, TacticAst.Do (loc, num, tac)
502 | TacticAst.Repeat (loc, tactical) ->
503 let status, tac = disambiguate_tactical status tactical in
504 status, TacticAst.Repeat (loc, tac)
505 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
506 let status, tacticals = disambiguate_tacticals status tacticals in
507 let tacticals = List.rev tacticals in
508 status, TacticAst.Seq (loc, tacticals)
509 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
510 let status, tactical = disambiguate_tactical status tactical in
511 let status, tacticals = disambiguate_tacticals status tacticals in
512 status, TacticAst.Then (loc, tactical, tacticals)
513 | TacticAst.Tries (loc, tacticals) ->
514 let status, tacticals = disambiguate_tacticals status tacticals in
515 status, TacticAst.Tries (loc, tacticals)
516 | TacticAst.Try (loc, tactical) ->
517 let status, tactical = disambiguate_tactical status tactical in
518 status, TacticAst.Try (loc, tactical)
520 and disambiguate_tacticals status tacticals =
521 let status, tacticals =
523 (fun (status, tacticals) tactical ->
524 let status, tac = disambiguate_tactical status tactical in
525 status, tac :: tacticals)
529 let tacticals = List.rev tacticals in
532 let disambiguate_command status = function
533 | TacticAst.Coercion (loc, term) ->
534 let status, term = disambiguate_term status term in
535 status, TacticAst.Coercion (loc,term)
536 | (TacticAst.Set _ | TacticAst.Qed _ | TacticAst.Drop _ ) as cmd ->
538 | TacticAst.Alias _ as x -> status, x
539 | TacticAst.Obj (loc,obj) ->
540 let status,obj = disambiguate_obj status obj in
541 status, TacticAst.Obj (loc,obj)
543 let disambiguate_executable status ex =
545 | TacticAst.Tactical (loc, tac) ->
546 let status, tac = disambiguate_tactical status tac in
547 status, (TacticAst.Tactical (loc, tac))
548 | TacticAst.Command (loc, cmd) ->
549 let status, cmd = disambiguate_command status cmd in
550 status, (TacticAst.Command (loc, cmd))
551 | TacticAst.Macro (_, mac) ->
552 command_error (sprintf "The macro %s can't be in a script"
553 (TacticAstPp.pp_macro_ast mac))
555 let disambiguate_comment status c =
557 | TacticAst.Note (loc,n) -> status, TacticAst.Note (loc,n)
558 | TacticAst.Code (loc,ex) ->
559 let status, ex = disambiguate_executable status ex in
560 status, TacticAst.Code (loc,ex)
562 let disambiguate_statement status statement =
564 | TacticAst.Comment (loc,c) ->
565 let status, c = disambiguate_comment status c in
566 status, TacticAst.Comment (loc,c)
567 | TacticAst.Executable (loc,ex) ->
568 let status, ex = disambiguate_executable status ex in
569 status, TacticAst.Executable (loc,ex)
571 let eval_ast status ast =
572 let status,st = disambiguate_statement status ast in
573 (* this disambiguation step should be deferred to support tacticals *)
576 let eval_from_stream status str cb =
577 let stl = CicTextualParser2.parse_statements str in
578 List.iter (fun ast -> cb !status ast;status := eval_ast !status ast) stl
580 let eval_from_stream_greedy status str cb =
582 print_string "matita> ";
584 let ast = CicTextualParser2.parse_statement str in
586 status := eval_ast !status ast
589 let eval_string status str =
590 eval_from_stream status (Stream.of_string str) (fun _ _ -> ())
592 let default_options () =
595 StringMap.add "baseuri"
597 (Helm_registry.get "matita.baseuri" ^ Helm_registry.get "matita.owner"))
602 StringMap.add "basedir"
603 (String (Helm_registry.get "matita.basedir" ))
610 aliases = DisambiguateTypes.empty_environment;
611 proof_status = No_proof;
612 options = default_options ();