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 (_, dir, t, pattern) ->
89 EqualityTactics.rewrite_tac ~where:pattern ~term:t ()
91 EqualityTactics.rewrite_back_tac ~where:pattern ~term:t ()
92 | TacticAst.Right _ -> Tactics.right
93 | TacticAst.Ring _ -> Tactics.ring
94 | TacticAst.Split _ -> Tactics.split
95 | TacticAst.Symmetry _ -> Tactics.symmetry
96 | TacticAst.Transitivity (_, term) -> Tactics.transitivity term
98 let eval_tactical status tac =
99 let apply_tactic tactic =
101 ProofEngineTypes.apply_tactic tactic (MatitaMisc.get_proof_status status)
106 let (_,metasenv,_,_) = proof in
109 | (ng,_,_)::_ -> Incomplete_proof (proof,ng))
110 | ng::_ -> Incomplete_proof (proof, ng)
112 { status with proof_status = new_status }
114 let rec tactical_of_ast = function
115 | TacticAst.Tactic (loc, tactic) -> tactic_of_ast tactic
116 | TacticAst.Do (loc, num, tactical) ->
117 Tacticals.do_tactic num (tactical_of_ast tactical)
118 | TacticAst.Repeat (loc, tactical) ->
119 Tacticals.repeat_tactic (tactical_of_ast tactical)
120 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
121 Tacticals.seq (List.map tactical_of_ast tacticals)
122 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
123 Tacticals.thens (tactical_of_ast tactical)
124 (List.map tactical_of_ast tacticals)
125 | TacticAst.Tries (loc, tacticals) ->
126 Tacticals.try_tactics
127 (List.map (fun t -> "", tactical_of_ast t) tacticals)
128 | TacticAst.Try (loc, tactical) ->
129 Tacticals.try_tactic (tactical_of_ast tactical)
131 apply_tactic (tactical_of_ast tac)
133 let eval_coercion status coercion =
134 let coer_uri,coer_ty =
139 CicEnvironment.get_obj CicUniv.empty_ugraph uri
142 | Cic.Constant (_,_,ty,_,_)
143 | Cic.Variable (_,_,ty,_,_) ->
146 | Cic.MutConstruct (uri,t,c,_) ->
148 CicEnvironment.get_obj CicUniv.empty_ugraph uri
151 | Cic.InductiveDefinition (l,_,_,_) ->
152 let (_,_,_,cl) = List.nth l t in
153 let (_,cty) = List.nth cl c in
158 (* we have to get the source and the tgt type uri
159 * in Coq syntax we have already their names, but
160 * since we don't support Funclass and similar I think
161 * all the coercion should be of the form
163 * So we should be able to extract them from the coercion type
165 let extract_last_two_p ty =
166 let rec aux = function
167 | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))
168 | Cic.Prod( _, src, tgt) -> src, tgt
173 let ty_src,ty_tgt = extract_last_two_p coer_ty in
176 let ty_src = CicReduction.whd context ty_src in
177 CicUtil.uri_of_term ty_src
180 let ty_tgt = CicReduction.whd context ty_tgt in
181 CicUtil.uri_of_term ty_tgt
184 (* also adds them to the Db *)
185 CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri in
187 List.fold_left (fun s (uri,o,ugraph) -> MatitaSync.add_obj uri o status)
188 status new_coercions in
189 {status with proof_status = No_proof}
191 let generate_elimination_principles uri status =
192 let elim sort status =
194 let uri,obj = CicElim.elim_of ~sort uri 0 in
195 MatitaSync.add_obj uri obj status
196 with CicElim.Can_t_eliminate -> status
198 List.fold_left (fun status sort -> elim sort status) status
199 [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ]
201 let generate_projections uri fields status =
202 let projections = CicRecord.projections_of uri fields in
204 (fun status (uri, name, bo) ->
207 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
208 let bo = Unshare.unshare bo in
209 let ty = Unshare.unshare ty in
210 let attrs = [`Class `Projection; `Generated] in
211 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
212 MatitaSync.add_obj uri obj status
214 CicTypeChecker.TypeCheckerFailure s ->
216 ("Unable to create projection " ^ name ^ " cause: " ^ s);
218 | CicEnvironment.Object_not_found uri ->
219 let depend = UriManager.name_of_uri uri in
221 ("Unable to create projection " ^ name ^ " because it requires " ^ depend);
225 let eval_command status cmd =
227 | TacticAst.Set (loc, name, value) -> set_option status name value
228 | TacticAst.Drop loc -> raise Drop
229 | TacticAst.Qed loc ->
230 let uri, metasenv, bo, ty =
231 match status.proof_status with
232 | Proof (Some uri, metasenv, body, ty) ->
233 uri, metasenv, body, ty
234 | Proof (None, metasenv, body, ty) ->
236 ("Someone allows to start a thm without giving the "^
237 "name/uri. This should be fixed!")
238 | _-> command_error "You can't qed an uncomplete theorem"
240 let suri = UriManager.string_of_uri uri in
241 if metasenv <> [] then
242 command_error "Proof not completed! metasenv is not empty!";
243 let name = UriManager.name_of_uri uri in
244 let obj = Cic.Constant (name,Some bo,ty,[],[]) in
245 MatitaSync.add_obj uri obj status
246 | TacticAst.Coercion (loc, coercion) ->
247 eval_coercion status coercion
248 | TacticAst.Alias (loc, spec) ->
250 | TacticAst.Ident_alias (id,uri) ->
251 {status with aliases =
252 DisambiguateTypes.Environment.add
253 (DisambiguateTypes.Id id)
254 ("boh?",(fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string uri)))
256 | TacticAst.Symbol_alias (symb, instance, desc) ->
257 {status with aliases =
258 DisambiguateTypes.Environment.add
259 (DisambiguateTypes.Symbol (symb,instance))
260 (DisambiguateChoices.lookup_symbol_by_dsc symb desc)
262 | TacticAst.Number_alias (instance,desc) ->
263 {status with aliases =
264 DisambiguateTypes.Environment.add
265 (DisambiguateTypes.Num instance)
266 (DisambiguateChoices.lookup_num_by_dsc desc) status.aliases })
267 | TacticAst.Obj (loc,obj) ->
270 Cic.Constant (name,_,_,_,_)
271 | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
272 | Cic.InductiveDefinition (types,_,_,_) ->
274 (match types with (name,_,_,_)::_ -> name | _ -> assert false)
275 | _ -> assert false in
277 UriManager.uri_of_string (MatitaMisc.qualify status name ^ ext)
279 let metasenv = MatitaMisc.get_proof_metasenv status in
281 Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
282 assert (metasenv = metasenv');
284 match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false in
285 let initial_proof = (Some uri, metasenv, bo, ty) in
286 { status with proof_status = Incomplete_proof (initial_proof,goalno)}
288 if metasenv <> [] then
290 "metasenv not empty while giving a definition with body: " ^
291 CicMetaSubst.ppmetasenv metasenv []);
292 let status = MatitaSync.add_obj uri obj status in
294 Cic.Constant _ -> status
295 | Cic.InductiveDefinition (_,_,_,attrs) ->
296 let status = generate_elimination_principles uri status in
297 let rec get_record_attrs =
300 | (`Class (`Record fields))::_ -> Some fields
301 | _::tl -> get_record_attrs tl
303 (match get_record_attrs attrs with
304 None -> status (* not a record *)
305 | Some fields -> generate_projections uri fields status)
307 | Cic.Variable _ -> assert false
309 let eval_executable status ex =
311 | TacticAst.Tactical (_, tac) -> eval_tactical status tac
312 | TacticAst.Command (_, cmd) -> eval_command status cmd
313 | TacticAst.Macro (_, mac) ->
314 command_error (sprintf "The macro %s can't be in a script"
315 (TacticAstPp.pp_macro_cic mac))
317 let eval_comment status c = status
321 | TacticAst.Executable (_,ex) -> eval_executable status ex
322 | TacticAst.Comment (_,c) -> eval_comment status c
324 let disambiguate_term status term =
325 let (aliases, metasenv, cic, _) =
327 MatitaDisambiguator.disambiguate_term ~dbd:(MatitaDb.instance ())
328 ~aliases:(status.aliases) ~context:(MatitaMisc.get_proof_context status)
329 ~metasenv:(MatitaMisc.get_proof_metasenv status) term
335 match status.proof_status with
336 | No_proof -> Intermediate metasenv
337 | Incomplete_proof ((uri, _, proof, ty), goal) ->
338 Incomplete_proof ((uri, metasenv, proof, ty), goal)
339 | Intermediate _ -> Intermediate metasenv
340 | Proof _ -> assert false
345 proof_status = proof_status }
349 let disambiguate_obj status obj =
352 TacticAst.Inductive (_,(name,_,_,_)::_)
353 | TacticAst.Record (_,name,_,_) ->
354 Some (UriManager.uri_of_string (MatitaMisc.qualify status name ^ ".ind"))
355 | TacticAst.Inductive _ -> assert false
357 let (aliases, metasenv, cic, _) =
359 MatitaDisambiguator.disambiguate_obj ~dbd:(MatitaDb.instance ())
360 ~aliases:(status.aliases) ~uri obj
366 match status.proof_status with
367 | No_proof -> Intermediate metasenv
370 | Proof _ -> assert false
375 proof_status = proof_status }
379 let disambiguate_pattern status (wanted, hyp_paths, goal_path) =
380 let interp path = Disambiguate.interpretate_path [] status.aliases path in
381 let goal_path = interp goal_path in
382 let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
387 let status,wanted = disambiguate_term status wanted in
390 status, (wanted, hyp_paths ,goal_path)
392 let disambiguate_tactic status = function
393 | TacticAst.Apply (loc, term) ->
394 let status, cic = disambiguate_term status term in
395 status, TacticAst.Apply (loc, cic)
396 | TacticAst.Absurd (loc, term) ->
397 let status, cic = disambiguate_term status term in
398 status, TacticAst.Absurd (loc, cic)
399 | TacticAst.Assumption loc -> status, TacticAst.Assumption loc
400 | TacticAst.Auto (loc,depth,width) -> status, TacticAst.Auto (loc,depth,width)
401 | TacticAst.Change (loc, pattern, with_what) ->
402 let status, with_what = disambiguate_term status with_what in
403 let status, pattern = disambiguate_pattern status pattern in
404 status, TacticAst.Change (loc, pattern, with_what)
405 | TacticAst.Clear (loc,id) -> status,TacticAst.Clear (loc,id)
406 | TacticAst.ClearBody (loc,id) -> status,TacticAst.ClearBody (loc,id)
407 | TacticAst.Compare (loc,term) ->
408 let status, term = disambiguate_term status term in
409 status, TacticAst.Compare (loc,term)
410 | TacticAst.Constructor (loc,n) ->
411 status, TacticAst.Constructor (loc,n)
412 | TacticAst.Contradiction loc ->
413 status, TacticAst.Contradiction loc
414 | TacticAst.Cut (loc, ident, term) ->
415 let status, cic = disambiguate_term status term in
416 status, TacticAst.Cut (loc, ident, cic)
417 | TacticAst.DecideEquality loc ->
418 status, TacticAst.DecideEquality loc
419 | TacticAst.Decompose (loc,term) ->
420 let status,term = disambiguate_term status term in
421 status, TacticAst.Decompose(loc,term)
422 | TacticAst.Discriminate (loc,term) ->
423 let status,term = disambiguate_term status term in
424 status, TacticAst.Discriminate(loc,term)
425 | TacticAst.Exact (loc, term) ->
426 let status, cic = disambiguate_term status term in
427 status, TacticAst.Exact (loc, cic)
428 | TacticAst.Elim (loc, term, Some term') ->
429 let status, cic1 = disambiguate_term status term in
430 let status, cic2 = disambiguate_term status term' in
431 status, TacticAst.Elim (loc, cic1, Some cic2)
432 | TacticAst.Elim (loc, term, None) ->
433 let status, cic = disambiguate_term status term in
434 status, TacticAst.Elim (loc, cic, None)
435 | TacticAst.ElimType (loc, term) ->
436 let status, cic = disambiguate_term status term in
437 status, TacticAst.ElimType (loc, cic)
438 | TacticAst.Exists loc -> status, TacticAst.Exists loc
439 | TacticAst.Fail loc -> status,TacticAst.Fail loc
440 | TacticAst.Fold (loc,reduction_kind, pattern) ->
441 let status, pattern = disambiguate_pattern status pattern in
442 status, TacticAst.Fold (loc,reduction_kind, pattern)
443 | TacticAst.FwdSimpl (loc, term) ->
444 let status, term = disambiguate_term status term in
445 status, TacticAst.FwdSimpl (loc, term)
446 | TacticAst.Fourier loc -> status, TacticAst.Fourier loc
447 | TacticAst.Generalize (loc,pattern,ident) ->
448 let status, pattern = disambiguate_pattern status pattern in
449 status, TacticAst.Generalize(loc,pattern,ident)
450 | TacticAst.Goal (loc, g) -> status, TacticAst.Goal (loc, g)
451 | TacticAst.IdTac loc -> status,TacticAst.IdTac loc
452 | TacticAst.Injection (loc,term) ->
453 let status, term = disambiguate_term status term in
454 status, TacticAst.Injection (loc,term)
455 | TacticAst.Intros (loc, num, names) ->
456 status, TacticAst.Intros (loc, num, names)
457 | TacticAst.LApply (loc, to_what, what, ident) ->
458 let status, to_what =
462 let status, to_what = disambiguate_term status to_what in
465 let status, what = disambiguate_term status what in
466 status, TacticAst.LApply (loc, to_what, what, ident)
467 | TacticAst.Left loc -> status, TacticAst.Left loc
468 | TacticAst.LetIn (loc, term, name) ->
469 let status, term = disambiguate_term status term in
470 status, TacticAst.LetIn (loc,term,name)
471 | TacticAst.Reduce (loc, reduction_kind, pattern) ->
472 let status, pattern = disambiguate_pattern status pattern in
473 status, TacticAst.Reduce(loc, reduction_kind, pattern)
474 | TacticAst.Reflexivity loc -> status, TacticAst.Reflexivity loc
475 | TacticAst.Replace (loc, pattern, with_what) ->
476 let status, pattern = disambiguate_pattern status pattern in
477 let status, with_what = disambiguate_term status with_what in
478 status, TacticAst.Replace (loc, pattern, with_what)
479 | TacticAst.Rewrite (loc, dir, t, pattern) ->
480 let status, term = disambiguate_term status t in
481 let status, pattern = disambiguate_pattern status pattern in
482 status, TacticAst.Rewrite (loc, dir, term, pattern)
483 | TacticAst.Right loc -> status, TacticAst.Right loc
484 | TacticAst.Ring loc -> status, TacticAst.Ring loc
485 | TacticAst.Split loc -> status, TacticAst.Split loc
486 | TacticAst.Symmetry loc -> status, TacticAst.Symmetry loc
487 | TacticAst.Transitivity (loc, term) ->
488 let status, cic = disambiguate_term status term in
489 status, TacticAst.Transitivity (loc, cic)
491 let rec disambiguate_tactical status = function
492 | TacticAst.Tactic (loc, tactic) ->
493 let status, tac = disambiguate_tactic status tactic in
494 status, TacticAst.Tactic (loc, tac)
495 | TacticAst.Do (loc, num, tactical) ->
496 let status, tac = disambiguate_tactical status tactical in
497 status, TacticAst.Do (loc, num, tac)
498 | TacticAst.Repeat (loc, tactical) ->
499 let status, tac = disambiguate_tactical status tactical in
500 status, TacticAst.Repeat (loc, tac)
501 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
502 let status, tacticals = disambiguate_tacticals status tacticals in
503 let tacticals = List.rev tacticals in
504 status, TacticAst.Seq (loc, tacticals)
505 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
506 let status, tactical = disambiguate_tactical status tactical in
507 let status, tacticals = disambiguate_tacticals status tacticals in
508 status, TacticAst.Then (loc, tactical, tacticals)
509 | TacticAst.Tries (loc, tacticals) ->
510 let status, tacticals = disambiguate_tacticals status tacticals in
511 status, TacticAst.Tries (loc, tacticals)
512 | TacticAst.Try (loc, tactical) ->
513 let status, tactical = disambiguate_tactical status tactical in
514 status, TacticAst.Try (loc, tactical)
516 and disambiguate_tacticals status tacticals =
517 let status, tacticals =
519 (fun (status, tacticals) tactical ->
520 let status, tac = disambiguate_tactical status tactical in
521 status, tac :: tacticals)
525 let tacticals = List.rev tacticals in
528 let disambiguate_command status = function
529 | TacticAst.Coercion (loc, term) ->
530 let status, term = disambiguate_term status term in
531 status, TacticAst.Coercion (loc,term)
532 | (TacticAst.Set _ | TacticAst.Qed _ | TacticAst.Drop _ ) as cmd ->
534 | TacticAst.Alias _ as x -> status, x
535 | TacticAst.Obj (loc,obj) ->
536 let status,obj = disambiguate_obj status obj in
537 status, TacticAst.Obj (loc,obj)
539 let disambiguate_executable status ex =
541 | TacticAst.Tactical (loc, tac) ->
542 let status, tac = disambiguate_tactical status tac in
543 status, (TacticAst.Tactical (loc, tac))
544 | TacticAst.Command (loc, cmd) ->
545 let status, cmd = disambiguate_command status cmd in
546 status, (TacticAst.Command (loc, cmd))
547 | TacticAst.Macro (_, mac) ->
548 command_error (sprintf "The macro %s can't be in a script"
549 (TacticAstPp.pp_macro_ast mac))
551 let disambiguate_comment status c =
553 | TacticAst.Note (loc,n) -> status, TacticAst.Note (loc,n)
554 | TacticAst.Code (loc,ex) ->
555 let status, ex = disambiguate_executable status ex in
556 status, TacticAst.Code (loc,ex)
558 let disambiguate_statement status statement =
560 | TacticAst.Comment (loc,c) ->
561 let status, c = disambiguate_comment status c in
562 status, TacticAst.Comment (loc,c)
563 | TacticAst.Executable (loc,ex) ->
564 let status, ex = disambiguate_executable status ex in
565 status, TacticAst.Executable (loc,ex)
567 let eval_ast status ast =
568 let status,st = disambiguate_statement status ast in
569 (* this disambiguation step should be deferred to support tacticals *)
572 let eval_from_stream status str cb =
573 let stl = CicTextualParser2.parse_statements str in
574 List.iter (fun ast -> cb !status ast;status := eval_ast !status ast) stl
576 let eval_from_stream_greedy status str cb =
578 print_string "matita> ";
580 let ast = CicTextualParser2.parse_statement str in
582 status := eval_ast !status ast
585 let eval_string status str =
586 eval_from_stream status (Stream.of_string str) (fun _ _ -> ())
588 let default_options () =
591 StringMap.add "baseuri"
593 (Helm_registry.get "matita.baseuri" ^ Helm_registry.get "matita.owner"))
598 StringMap.add "basedir"
599 (String (Helm_registry.get "matita.basedir" ))
606 aliases = DisambiguateTypes.empty_environment;
607 proof_status = No_proof;
608 options = default_options ();