6 let debug_print = if debug then prerr_endline else ignore ;;
8 (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
9 * names as long as they are available, then it fallbacks to name generation
10 * using FreshNamesGenerator module *)
12 let len = List.length names in
14 fun metasenv context name ~typ ->
15 if !count < len then begin
16 let name = Cic.Name (List.nth names !count) in
20 FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context name ~typ
22 let tactic_of_ast = function
23 | TacticAst.Absurd (_, term) -> Tactics.absurd term
24 | TacticAst.Apply (_, term) -> Tactics.apply term
25 | TacticAst.Assumption _ -> Tactics.assumption
26 | TacticAst.Auto (_,depth,width) ->
27 AutoTactic.auto_tac ?depth ?width ~dbd:(MatitaDb.instance ()) ()
28 | TacticAst.Change (_, what, with_what, _) -> Tactics.change ~what ~with_what
29 | TacticAst.Contradiction _ -> Tactics.contradiction
30 | TacticAst.Compare (_, term) -> Tactics.compare term
31 | TacticAst.Constructor (_, n) -> Tactics.constructor n
32 | TacticAst.Cut (_, ident, term) ->
33 let names = match ident with None -> [] | Some id -> [id] in
34 Tactics.cut ~mk_fresh_name_callback:(namer_of names) term
35 | TacticAst.DecideEquality _ -> Tactics.decide_equality
36 | TacticAst.Decompose (_,term) -> Tactics.decompose term
37 | TacticAst.Discriminate (_,term) -> Tactics.discriminate term
38 | TacticAst.Elim (_, term, _) ->
39 Tactics.elim_intros term
40 | TacticAst.ElimType (_, term) -> Tactics.elim_type term
41 | TacticAst.Exact (_, term) -> Tactics.exact term
42 | TacticAst.Exists _ -> Tactics.exists
43 | TacticAst.Fold (_, reduction_kind ,term) ->
45 match reduction_kind with
46 | `Normalize -> CicReduction.normalize ~delta:false ~subst:[]
47 | `Reduce -> ProofEngineReduction.reduce
48 | `Simpl -> ProofEngineReduction.simpl
49 | `Whd -> CicReduction.whd ~delta:false ~subst:[]
51 Tactics.fold ~reduction ~also_in_hypotheses:false ~term
52 | TacticAst.Fourier _ -> Tactics.fourier
53 | TacticAst.FwdSimpl (_, term) ->
54 Tactics.fwd_simpl ~what:term ~dbd:(MatitaDb.instance ())
55 | TacticAst.Generalize (_,term,ident,pat) ->
56 let names = match ident with None -> [] | Some id -> [id] in
57 Tactics.generalize ~term ~mk_fresh_name_callback:(namer_of names) pat
58 | TacticAst.Goal (_, n) -> Tactics.set_goal n
59 | TacticAst.Injection (_,term) -> Tactics.injection term
60 | TacticAst.Intros (_, None, names) ->
61 PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
62 | TacticAst.Intros (_, Some num, names) ->
63 PrimitiveTactics.intros_tac ~howmany:num
64 ~mk_fresh_name_callback:(namer_of names) ()
65 | TacticAst.LApply (_, to_what, what) ->
66 Tactics.lapply ?to_what what
67 | TacticAst.Left _ -> Tactics.left
68 | TacticAst.LetIn (loc,term,name) ->
69 Tactics.letin term ~mk_fresh_name_callback:(namer_of [name])
70 | TacticAst.Reduce (_, reduction_kind, pattern) ->
71 (match reduction_kind with
72 | `Normalize -> Tactics.normalize ~pattern
73 | `Reduce -> Tactics.reduce ~pattern
74 | `Simpl -> Tactics.simpl ~pattern
75 | `Whd -> Tactics.whd ~pattern)
76 | TacticAst.Reflexivity _ -> Tactics.reflexivity
77 | TacticAst.Replace (_, what, with_what) -> Tactics.replace ~what ~with_what
78 | TacticAst.Rewrite (_, dir, t, pattern) ->
80 EqualityTactics.rewrite_tac ~where:pattern ~term:t ()
82 EqualityTactics.rewrite_back_tac ~where:pattern ~term:t ()
83 | TacticAst.Right _ -> Tactics.right
84 | TacticAst.Ring _ -> Tactics.ring
85 | TacticAst.Split _ -> Tactics.split
86 | TacticAst.Symmetry _ -> Tactics.symmetry
87 | TacticAst.Transitivity (_, term) -> Tactics.transitivity term
89 let eval_tactical status tac =
90 let apply_tactic tactic =
92 ProofEngineTypes.apply_tactic tactic (MatitaMisc.get_proof_status status)
97 let (_,metasenv,_,_) = proof in
100 | (ng,_,_)::_ -> Incomplete_proof (proof,ng))
101 | ng::_ -> Incomplete_proof (proof, ng)
103 { status with proof_status = new_status }
105 let rec tactical_of_ast = function
106 | TacticAst.Tactic (loc, tactic) -> tactic_of_ast tactic
107 | TacticAst.Fail loc -> Tacticals.fail
108 | TacticAst.Do (loc, num, tactical) ->
109 Tacticals.do_tactic num (tactical_of_ast tactical)
110 | TacticAst.IdTac loc -> Tacticals.id_tac
111 | TacticAst.Repeat (loc, tactical) ->
112 Tacticals.repeat_tactic (tactical_of_ast tactical)
113 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
114 Tacticals.seq (List.map tactical_of_ast tacticals)
115 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
116 Tacticals.thens (tactical_of_ast tactical)
117 (List.map tactical_of_ast tacticals)
118 | TacticAst.Tries (loc, tacticals) ->
119 Tacticals.try_tactics
120 (List.map (fun t -> "", tactical_of_ast t) tacticals)
121 | TacticAst.Try (loc, tactical) ->
122 Tacticals.try_tactic (tactical_of_ast tactical)
124 apply_tactic (tactical_of_ast tac)
126 let eval_coercion status coercion =
127 let coer_uri,coer_ty =
132 CicEnvironment.get_obj CicUniv.empty_ugraph uri
135 | Cic.Constant (_,_,ty,_,_)
136 | Cic.Variable (_,_,ty,_,_) ->
139 | Cic.MutConstruct (uri,t,c,_) ->
141 CicEnvironment.get_obj CicUniv.empty_ugraph uri
144 | Cic.InductiveDefinition (l,_,_,_) ->
145 let (_,_,_,cl) = List.nth l t in
146 let (_,cty) = List.nth cl c in
151 (* we have to get the source and the tgt type uri
152 * in Coq syntax we have already their names, but
153 * since we don't support Funclass and similar I think
154 * all the coercion should be of the form
156 * So we should be able to extract them from the coercion type
158 let extract_last_two_p ty =
159 let rec aux = function
160 | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))
161 | Cic.Prod( _, src, tgt) -> src, tgt
166 let ty_src,ty_tgt = extract_last_two_p coer_ty in
169 let ty_src = CicReduction.whd context ty_src in
170 CicUtil.uri_of_term ty_src
173 let ty_tgt = CicReduction.whd context ty_tgt in
174 CicUtil.uri_of_term ty_tgt
177 (* also adds them to the Db *)
178 CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri in
180 List.fold_left (fun s (uri,o,ugraph) -> MatitaSync.add_obj uri o status)
181 status new_coercions in
182 {status with proof_status = No_proof}
184 let generate_elimination_principles uri status =
185 let elim sort status =
187 let uri,obj = CicElim.elim_of ~sort uri 0 in
188 MatitaSync.add_obj uri obj status
189 with CicElim.Can_t_eliminate -> status
191 List.fold_left (fun status sort -> elim sort status) status
192 [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ]
194 let generate_projections uri fields status =
195 let projections = CicRecord.projections_of uri fields in
197 (fun status (uri, name, bo) ->
200 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
201 let bo = Unshare.unshare bo in
202 let ty = Unshare.unshare ty in
203 let attrs = [`Class `Projection; `Generated] in
204 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
205 MatitaSync.add_obj uri obj status
207 CicTypeChecker.TypeCheckerFailure s ->
209 ("Unable to create projection " ^ name ^ " cause: " ^ s);
211 | CicEnvironment.Object_not_found uri ->
212 let depend = UriManager.name_of_uri uri in
214 ("Unable to create projection " ^ name ^ " because it requires " ^ depend);
218 let eval_command status cmd =
220 | TacticAst.Set (loc, name, value) -> set_option status name value
221 | TacticAst.Qed loc ->
222 let uri, metasenv, bo, ty =
223 match status.proof_status with
224 | Proof (Some uri, metasenv, body, ty) ->
225 uri, metasenv, body, ty
226 | Proof (None, metasenv, body, ty) ->
228 ("Someone allows to start a thm without giving the "^
229 "name/uri. This should be fixed!")
230 | _-> command_error "You can't qed an uncomplete theorem"
232 let suri = UriManager.string_of_uri uri in
233 if metasenv <> [] then
234 command_error "Proof not completed! metasenv is not empty!";
235 let name = UriManager.name_of_uri uri in
236 let obj = Cic.Constant (name,Some bo,ty,[],[]) in
237 MatitaSync.add_obj uri obj status
238 | TacticAst.Coercion (loc, coercion) ->
239 eval_coercion status coercion
240 | TacticAst.Alias (loc, spec) ->
242 | TacticAst.Ident_alias (id,uri) ->
243 {status with aliases =
244 DisambiguateTypes.Environment.add
245 (DisambiguateTypes.Id id)
246 ("boh?",(fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string uri)))
248 | TacticAst.Symbol_alias (symb, instance, desc) ->
249 {status with aliases =
250 DisambiguateTypes.Environment.add
251 (DisambiguateTypes.Symbol (symb,instance))
252 (DisambiguateChoices.lookup_symbol_by_dsc symb desc)
254 | TacticAst.Number_alias (instance,desc) ->
255 {status with aliases =
256 DisambiguateTypes.Environment.add
257 (DisambiguateTypes.Num instance)
258 (DisambiguateChoices.lookup_num_by_dsc desc) status.aliases })
259 | TacticAst.Obj (loc,obj) ->
262 Cic.Constant (name,_,_,_,_)
263 | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
264 | Cic.InductiveDefinition (types,_,_,_) ->
266 (match types with (name,_,_,_)::_ -> name | _ -> assert false)
267 | _ -> assert false in
269 UriManager.uri_of_string (MatitaMisc.qualify status name ^ ext)
271 let metasenv = MatitaMisc.get_proof_metasenv status in
273 Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
274 assert (metasenv = metasenv');
276 match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false in
277 let initial_proof = (Some uri, metasenv, bo, ty) in
278 { status with proof_status = Incomplete_proof (initial_proof,goalno)}
280 if metasenv <> [] then
282 "metasenv not empty while giving a definition with body: " ^
283 CicMetaSubst.ppmetasenv metasenv []);
284 let status = MatitaSync.add_obj uri obj status in
286 Cic.Constant _ -> status
287 | Cic.InductiveDefinition (_,_,_,attrs) ->
288 let status = generate_elimination_principles uri status in
289 let rec get_record_attrs =
292 | (`Class (`Record fields))::_ -> Some fields
293 | _::tl -> get_record_attrs tl
295 (match get_record_attrs attrs with
296 None -> status (* not a record *)
297 | Some fields -> generate_projections uri fields status)
299 | Cic.Variable _ -> assert false
301 let eval_executable status ex =
303 | TacticAst.Tactical (_, tac) -> eval_tactical status tac
304 | TacticAst.Command (_, cmd) -> eval_command status cmd
305 | TacticAst.Macro (_, mac) ->
306 command_error (sprintf "The macro %s can't be in a script"
307 (TacticAstPp.pp_macro_cic mac))
309 let eval_comment status c = status
313 | TacticAst.Executable (_,ex) -> eval_executable status ex
314 | TacticAst.Comment (_,c) -> eval_comment status c
316 let disambiguate_term status term =
317 let (aliases, metasenv, cic, _) =
319 MatitaDisambiguator.disambiguate_term ~dbd:(MatitaDb.instance ())
320 ~aliases:(status.aliases) ~context:(MatitaMisc.get_proof_context status)
321 ~metasenv:(MatitaMisc.get_proof_metasenv status) term
327 match status.proof_status with
328 | No_proof -> Intermediate metasenv
329 | Incomplete_proof ((uri, _, proof, ty), goal) ->
330 Incomplete_proof ((uri, metasenv, proof, ty), goal)
331 | Intermediate _ -> Intermediate metasenv
332 | Proof _ -> assert false
337 proof_status = proof_status }
341 let disambiguate_obj status obj =
344 TacticAst.Inductive (_,(name,_,_,_)::_)
345 | TacticAst.Record (_,name,_,_) ->
346 Some (UriManager.uri_of_string (MatitaMisc.qualify status name ^ ".ind"))
347 | TacticAst.Inductive _ -> assert false
349 let (aliases, metasenv, cic, _) =
351 MatitaDisambiguator.disambiguate_obj ~dbd:(MatitaDb.instance ())
352 ~aliases:(status.aliases) ~uri obj
358 match status.proof_status with
359 | No_proof -> Intermediate metasenv
362 | Proof _ -> assert false
367 proof_status = proof_status }
371 let disambiguate_pattern aliases (hyp_paths ,goal_path) =
372 let interp path = Disambiguate.interpretate_path [] aliases path in
376 | Some path -> Some (interp path) in
377 let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
378 (hyp_paths ,goal_path)
380 let disambiguate_tactic status = function
381 | TacticAst.Apply (loc, term) ->
382 let status, cic = disambiguate_term status term in
383 status, TacticAst.Apply (loc, cic)
384 | TacticAst.Absurd (loc, term) ->
385 let status, cic = disambiguate_term status term in
386 status, TacticAst.Absurd (loc, cic)
387 | TacticAst.Assumption loc -> status, TacticAst.Assumption loc
388 | TacticAst.Auto (loc,depth,width) -> status, TacticAst.Auto (loc,depth,width)
389 | TacticAst.Change (loc, what, with_what, pattern) ->
390 let status, cic1 = disambiguate_term status what in
391 let status, cic2 = disambiguate_term status with_what in
392 let pattern = disambiguate_pattern status.aliases pattern in
393 status, TacticAst.Change (loc, cic1, cic2, pattern)
394 | TacticAst.Compare (loc,term) ->
395 let status, term = disambiguate_term status term in
396 status, TacticAst.Compare (loc,term)
397 | TacticAst.Constructor (loc,n) ->
398 status, TacticAst.Constructor (loc,n)
399 | TacticAst.Contradiction loc ->
400 status, TacticAst.Contradiction loc
401 | TacticAst.Cut (loc, ident, term) ->
402 let status, cic = disambiguate_term status term in
403 status, TacticAst.Cut (loc, ident, cic)
404 | TacticAst.DecideEquality loc ->
405 status, TacticAst.DecideEquality loc
406 | TacticAst.Decompose (loc,term) ->
407 let status,term = disambiguate_term status term in
408 status, TacticAst.Decompose(loc,term)
409 | TacticAst.Discriminate (loc,term) ->
410 let status,term = disambiguate_term status term in
411 status, TacticAst.Discriminate(loc,term)
412 | TacticAst.Exact (loc, term) ->
413 let status, cic = disambiguate_term status term in
414 status, TacticAst.Exact (loc, cic)
415 | TacticAst.Elim (loc, term, Some term') ->
416 let status, cic1 = disambiguate_term status term in
417 let status, cic2 = disambiguate_term status term' in
418 status, TacticAst.Elim (loc, cic1, Some cic2)
419 | TacticAst.Elim (loc, term, None) ->
420 let status, cic = disambiguate_term status term in
421 status, TacticAst.Elim (loc, cic, None)
422 | TacticAst.ElimType (loc, term) ->
423 let status, cic = disambiguate_term status term in
424 status, TacticAst.ElimType (loc, cic)
425 | TacticAst.Exists loc -> status, TacticAst.Exists loc
426 | TacticAst.Fold (loc,reduction_kind, term) ->
427 let status, term = disambiguate_term status term in
428 status, TacticAst.Fold (loc,reduction_kind, term)
429 | TacticAst.FwdSimpl (loc, term) ->
430 let status, term = disambiguate_term status term in
431 status, TacticAst.FwdSimpl (loc, term)
432 | TacticAst.Fourier loc -> status, TacticAst.Fourier loc
433 | TacticAst.Generalize (loc,term,ident,pattern) ->
434 let status,term = disambiguate_term status term in
435 let pattern = disambiguate_pattern status.aliases pattern in
436 status, TacticAst.Generalize(loc,term,ident,pattern)
437 | TacticAst.Goal (loc, g) -> status, TacticAst.Goal (loc, g)
438 | TacticAst.Injection (loc,term) ->
439 let status, term = disambiguate_term status term in
440 status, TacticAst.Injection (loc,term)
441 | TacticAst.Intros (loc, num, names) ->
442 status, TacticAst.Intros (loc, num, names)
443 | TacticAst.LApply (loc, to_what, what) ->
444 let status, to_what =
448 let status, to_what = disambiguate_term status to_what in
451 let status, what = disambiguate_term status what in
452 status, TacticAst.LApply (loc, to_what, what)
453 | TacticAst.Left loc -> status, TacticAst.Left loc
454 | TacticAst.LetIn (loc, term, name) ->
455 let status, term = disambiguate_term status term in
456 status, TacticAst.LetIn (loc,term,name)
457 | TacticAst.Reduce (loc, reduction_kind, pattern) ->
458 let pattern = disambiguate_pattern status.aliases pattern in
459 status, TacticAst.Reduce(loc, reduction_kind, pattern)
460 | TacticAst.Reflexivity loc -> status, TacticAst.Reflexivity loc
461 | TacticAst.Replace (loc, what, with_what) ->
462 let status, cic1 = disambiguate_term status what in
463 let status, cic2 = disambiguate_term status with_what in
464 status, TacticAst.Replace (loc, cic1, cic2)
465 | TacticAst.Rewrite (loc, dir, t, pattern) ->
466 let status, term = disambiguate_term status t in
467 let pattern = disambiguate_pattern status.aliases pattern in
468 status, TacticAst.Rewrite (loc, dir, term, pattern)
469 | TacticAst.Right loc -> status, TacticAst.Right loc
470 | TacticAst.Ring loc -> status, TacticAst.Ring loc
471 | TacticAst.Split loc -> status, TacticAst.Split loc
472 | TacticAst.Symmetry loc -> status, TacticAst.Symmetry loc
473 | TacticAst.Transitivity (loc, term) ->
474 let status, cic = disambiguate_term status term in
475 status, TacticAst.Transitivity (loc, cic)
477 let rec disambiguate_tactical status = function
478 | TacticAst.Tactic (loc, tactic) ->
479 let status, tac = disambiguate_tactic status tactic in
480 status, TacticAst.Tactic (loc, tac)
481 | TacticAst.Do (loc, num, tactical) ->
482 let status, tac = disambiguate_tactical status tactical in
483 status, TacticAst.Do (loc, num, tac)
484 | TacticAst.Repeat (loc, tactical) ->
485 let status, tac = disambiguate_tactical status tactical in
486 status, TacticAst.Repeat (loc, tac)
487 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
488 let status, tacticals = disambiguate_tacticals status tacticals in
489 let tacticals = List.rev tacticals in
490 status, TacticAst.Seq (loc, tacticals)
491 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
492 let status, tactical = disambiguate_tactical status tactical in
493 let status, tacticals = disambiguate_tacticals status tacticals in
494 status, TacticAst.Then (loc, tactical, tacticals)
495 | TacticAst.Tries (loc, tacticals) ->
496 let status, tacticals = disambiguate_tacticals status tacticals in
497 status, TacticAst.Tries (loc, tacticals)
498 | TacticAst.Try (loc, tactical) ->
499 let status, tactical = disambiguate_tactical status tactical in
500 status, TacticAst.Try (loc, tactical)
501 | (TacticAst.IdTac _ | TacticAst.Fail _) as tac ->
504 and disambiguate_tacticals status tacticals =
505 let status, tacticals =
507 (fun (status, tacticals) tactical ->
508 let status, tac = disambiguate_tactical status tactical in
509 status, tac :: tacticals)
513 let tacticals = List.rev tacticals in
516 let disambiguate_command status = function
517 | TacticAst.Coercion (loc, term) ->
518 let status, term = disambiguate_term status term in
519 status, TacticAst.Coercion (loc,term)
520 | (TacticAst.Set _ | TacticAst.Qed _) as cmd ->
522 | TacticAst.Alias _ as x -> status, x
523 | TacticAst.Obj (loc,obj) ->
524 let status,obj = disambiguate_obj status obj in
525 status, TacticAst.Obj (loc,obj)
527 let disambiguate_executable status ex =
529 | TacticAst.Tactical (loc, tac) ->
530 let status, tac = disambiguate_tactical status tac in
531 status, (TacticAst.Tactical (loc, tac))
532 | TacticAst.Command (loc, cmd) ->
533 let status, cmd = disambiguate_command status cmd in
534 status, (TacticAst.Command (loc, cmd))
535 | TacticAst.Macro (_, mac) ->
536 command_error (sprintf "The macro %s can't be in a script"
537 (TacticAstPp.pp_macro_ast mac))
539 let disambiguate_comment status c =
541 | TacticAst.Note (loc,n) -> status, TacticAst.Note (loc,n)
542 | TacticAst.Code (loc,ex) ->
543 let status, ex = disambiguate_executable status ex in
544 status, TacticAst.Code (loc,ex)
546 let disambiguate_statement status statement =
548 | TacticAst.Comment (loc,c) ->
549 let status, c = disambiguate_comment status c in
550 status, TacticAst.Comment (loc,c)
551 | TacticAst.Executable (loc,ex) ->
552 let status, ex = disambiguate_executable status ex in
553 status, TacticAst.Executable (loc,ex)
555 let eval_ast status ast =
556 let status,st = disambiguate_statement status ast in
557 (* this disambiguation step should be deferred to support tacticals *)
560 let eval_from_stream status str cb =
561 let stl = CicTextualParser2.parse_statements str in
563 (fun status ast -> cb status ast;eval_ast status ast) status
566 let eval_string status str =
567 eval_from_stream status (Stream.of_string str) (fun _ _ -> ())
569 let default_options () =
571 StringMap.add "baseuri"
573 (Helm_registry.get "matita.baseuri" ^ Helm_registry.get "matita.owner"))
577 StringMap.add "basedir"
578 (String (Helm_registry.get "matita.basedir" ))
585 aliases = DisambiguateTypes.empty_environment;
586 proof_status = No_proof;
587 options = default_options ();