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.Intros (_, None, names) ->
24 (* TODO Zack implement intros length *)
25 PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
26 | TacticAst.Intros (_, Some num, names) ->
27 (* TODO Zack implement intros length *)
28 PrimitiveTactics.intros_tac ~howmany:num
29 ~mk_fresh_name_callback:(namer_of names) ()
30 | TacticAst.Reflexivity _ -> Tactics.reflexivity
31 | TacticAst.Assumption _ -> Tactics.assumption
32 | TacticAst.Contradiction _ -> Tactics.contradiction
34 | TacticAst.Discriminate (_,id) -> Tactics.discriminate id
36 | TacticAst.Exists _ -> Tactics.exists
37 | TacticAst.Fourier _ -> Tactics.fourier
38 | TacticAst.Generalize (_,term,pat) -> Tactics.generalize term pat
39 | TacticAst.Goal (_, n) -> Tactics.set_goal n
40 | TacticAst.Left _ -> Tactics.left
41 | TacticAst.Right _ -> Tactics.right
42 | TacticAst.Ring _ -> Tactics.ring
43 | TacticAst.Split _ -> Tactics.split
44 | TacticAst.Symmetry _ -> Tactics.symmetry
45 | TacticAst.Transitivity (_, term) -> Tactics.transitivity term
46 | TacticAst.Apply (_, term) -> Tactics.apply term
47 | TacticAst.Absurd (_, term) -> Tactics.absurd term
48 | TacticAst.Exact (_, term) -> Tactics.exact term
49 | TacticAst.Cut (_, term) -> Tactics.cut term
50 | TacticAst.Elim (_, term, _) ->
51 (* TODO Zack implement "using" argument *)
52 (* old: Tactics.elim_intros_simpl term *)
53 Tactics.elim_intros term
54 | TacticAst.ElimType (_, term) -> Tactics.elim_type term
55 | TacticAst.Replace (_, what, with_what) -> Tactics.replace ~what ~with_what
56 | TacticAst.Auto (_,depth) ->
57 (* AutoTactic.auto_tac ~num (MatitaDb.instance ()) *)
58 AutoTactic.auto_tac_new ?depth ~dbd:(MatitaDb.instance ()) ()
59 | TacticAst.Change (_, what, with_what, _) -> Tactics.change ~what ~with_what
61 (* TODO Zack a lot more of tactics to be implemented here ... *)
62 | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
63 | TacticAst.Change of 'term * 'term * 'ident option
64 | TacticAst.Decompose of 'ident * 'ident list
65 | TacticAst.Fold of reduction_kind * 'term
66 | TacticAst.Injection of 'ident
67 | TacticAst.Replace_pattern of 'term pattern * 'term
69 | TacticAst.LetIn (loc,term,name) ->
70 Tactics.letin term ~mk_fresh_name_callback:(namer_of [name])
71 | TacticAst.Reduce (_, reduction_kind, pattern) ->
72 (match reduction_kind with
73 | `Normalize -> Tactics.normalize ~pattern
74 | `Reduce -> Tactics.reduce ~pattern
75 | `Simpl -> Tactics.simpl ~pattern
76 | `Whd -> Tactics.whd ~pattern)
77 | TacticAst.Rewrite (_, dir, t, pattern) ->
79 EqualityTactics.rewrite_tac ~where:pattern ~term:t ()
81 EqualityTactics.rewrite_back_tac ~where:pattern ~term:t ()
82 | TacticAst.FwdSimpl (_, name) ->
83 Tactics.fwd_simpl ~hyp:(Cic.Name name) ~dbd:(MatitaDb.instance ())
84 | TacticAst.LApply (_, term, substs) ->
85 let f (name, term) = Cic.Name name, term in
86 Tactics.lapply ~substs:(List.map f substs) term
88 let eval_tactical status tac =
89 let apply_tactic tactic =
91 ProofEngineTypes.apply_tactic tactic (MatitaMisc.get_proof_status status)
96 let (_,metasenv,_,_) = proof in
99 | (ng,_,_)::_ -> Incomplete_proof (proof,ng))
100 | ng::_ -> Incomplete_proof (proof, ng)
102 { status with proof_status = new_status }
104 let rec tactical_of_ast = function
105 | TacticAst.Tactic (loc, tactic) -> tactic_of_ast tactic
106 | TacticAst.Fail loc -> Tacticals.fail
107 | TacticAst.Do (loc, num, tactical) ->
108 Tacticals.do_tactic num (tactical_of_ast tactical)
109 | TacticAst.IdTac loc -> Tacticals.id_tac
110 | TacticAst.Repeat (loc, tactical) ->
111 Tacticals.repeat_tactic (tactical_of_ast tactical)
112 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
113 Tacticals.seq (List.map tactical_of_ast tacticals)
114 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
115 Tacticals.thens (tactical_of_ast tactical)
116 (List.map tactical_of_ast tacticals)
117 | TacticAst.Tries (loc, tacticals) ->
118 Tacticals.try_tactics
119 (List.map (fun t -> "", tactical_of_ast t) tacticals)
120 | TacticAst.Try (loc, tactical) ->
121 Tacticals.try_tactic (tactical_of_ast tactical)
123 apply_tactic (tactical_of_ast tac)
125 let eval_coercion status coercion =
126 let coer_uri,coer_ty =
131 CicEnvironment.get_obj CicUniv.empty_ugraph uri
134 | Cic.Constant (_,_,ty,_,_)
135 | Cic.Variable (_,_,ty,_,_) ->
138 | Cic.MutConstruct (uri,t,c,_) ->
140 CicEnvironment.get_obj CicUniv.empty_ugraph uri
143 | Cic.InductiveDefinition (l,_,_,_) ->
144 let (_,_,_,cl) = List.nth l t in
145 let (_,cty) = List.nth cl c in
150 (* we have to get the source and the tgt type uri
151 * in Coq syntax we have already their names, but
152 * since we don't support Funclass and similar I think
153 * all the coercion should be of the form
155 * So we should be able to extract them from the coercion type
157 let extract_last_two_p ty =
158 let rec aux = function
159 | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))
160 | Cic.Prod( _, src, tgt) -> src, tgt
165 let ty_src,ty_tgt = extract_last_two_p coer_ty in
168 let ty_src = CicReduction.whd context ty_src in
169 CicUtil.uri_of_term ty_src
172 let ty_tgt = CicReduction.whd context ty_tgt in
173 CicUtil.uri_of_term ty_tgt
176 (* also adds them to the Db *)
177 CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri in
179 List.fold_left (fun s (uri,o,ugraph) -> MatitaSync.add_obj uri o status)
180 status new_coercions in
181 {status with proof_status = No_proof}
183 let generate_elimination_principles uri status =
184 let elim sort status =
186 let uri,obj = CicElim.elim_of ~sort uri 0 in
187 MatitaSync.add_obj uri obj status
188 with CicElim.Can_t_eliminate -> status
190 List.fold_left (fun status sort -> elim sort status) status
191 [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ]
193 let generate_projections uri fields status =
194 let projections = CicRecord.projections_of uri fields in
196 (fun status (uri, name, bo) ->
199 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
200 let bo = Unshare.unshare bo in
201 let ty = Unshare.unshare ty in
202 let attrs = [`Class `Projection; `Generated] in
203 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
204 MatitaSync.add_obj uri obj status
206 CicTypeChecker.TypeCheckerFailure s ->
208 ("Unable to create projection " ^ name ^ " cause: " ^ s);
210 | CicEnvironment.Object_not_found uri ->
211 let depend = UriManager.name_of_uri uri in
213 ("Unable to create projection " ^ name ^ " because it requires " ^ depend);
217 let eval_command status cmd =
219 | TacticAst.Set (loc, name, value) -> set_option status name value
220 | TacticAst.Qed loc ->
221 let uri, metasenv, bo, ty =
222 match status.proof_status with
223 | Proof (Some uri, metasenv, body, ty) ->
224 uri, metasenv, body, ty
225 | Proof (None, metasenv, body, ty) ->
227 ("Someone allows to start a thm without giving the "^
228 "name/uri. This should be fixed!")
229 | _-> command_error "You can't qed an uncomplete theorem"
231 let suri = UriManager.string_of_uri uri in
232 if metasenv <> [] then
233 command_error "Proof not completed! metasenv is not empty!";
234 let name = UriManager.name_of_uri uri in
235 let obj = Cic.Constant (name,Some bo,ty,[],[]) in
236 MatitaSync.add_obj uri obj status
237 | TacticAst.Coercion (loc, coercion) ->
238 eval_coercion status coercion
239 | TacticAst.Alias (loc, spec) ->
241 | TacticAst.Ident_alias (id,uri) ->
242 {status with aliases =
243 DisambiguateTypes.Environment.add
244 (DisambiguateTypes.Id id)
245 ("boh?",(fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string uri)))
247 | TacticAst.Symbol_alias (symb, instance, desc) ->
248 {status with aliases =
249 DisambiguateTypes.Environment.add
250 (DisambiguateTypes.Symbol (symb,instance))
251 (DisambiguateChoices.lookup_symbol_by_dsc symb desc)
253 | TacticAst.Number_alias (instance,desc) ->
254 {status with aliases =
255 DisambiguateTypes.Environment.add
256 (DisambiguateTypes.Num instance)
257 (DisambiguateChoices.lookup_num_by_dsc desc) status.aliases })
258 | TacticAst.Obj (loc,obj) ->
261 Cic.Constant (name,_,_,_,_)
262 | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
263 | Cic.InductiveDefinition (types,_,_,_) ->
265 (match types with (name,_,_,_)::_ -> name | _ -> assert false)
266 | _ -> assert false in
268 UriManager.uri_of_string (MatitaMisc.qualify status name ^ ext)
270 let metasenv = MatitaMisc.get_proof_metasenv status in
272 Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
273 assert (metasenv = metasenv');
275 match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false in
276 let initial_proof = (Some uri, metasenv, bo, ty) in
277 { status with proof_status = Incomplete_proof (initial_proof,goalno)}
279 if metasenv <> [] then
281 "metasenv not empty while giving a definition with body: " ^
282 CicMetaSubst.ppmetasenv metasenv []);
283 let status = MatitaSync.add_obj uri obj status in
285 Cic.Constant _ -> status
286 | Cic.InductiveDefinition (_,_,_,attrs) ->
287 let status = generate_elimination_principles uri status in
288 let rec get_record_attrs =
291 | (`Class (`Record fields))::_ -> Some fields
292 | _::tl -> get_record_attrs tl
294 (match get_record_attrs attrs with
295 None -> status (* not a record *)
296 | Some fields -> generate_projections uri fields status)
298 | Cic.Variable _ -> assert false
300 let eval_executable status ex =
302 | TacticAst.Tactical (_, tac) -> eval_tactical status tac
303 | TacticAst.Command (_, cmd) -> eval_command status cmd
304 | TacticAst.Macro (_, mac) ->
305 command_error (sprintf "The macro %s can't be in a script"
306 (TacticAstPp.pp_macro_cic mac))
308 let eval_comment status c = status
312 | TacticAst.Executable (_,ex) -> eval_executable status ex
313 | TacticAst.Comment (_,c) -> eval_comment status c
315 let disambiguate_term status term =
316 let (aliases, metasenv, cic, _) =
318 MatitaDisambiguator.disambiguate_term ~dbd:(MatitaDb.instance ())
319 ~aliases:(status.aliases) ~context:(MatitaMisc.get_proof_context status)
320 ~metasenv:(MatitaMisc.get_proof_metasenv status) term
326 match status.proof_status with
327 | No_proof -> Intermediate metasenv
328 | Incomplete_proof ((uri, _, proof, ty), goal) ->
329 Incomplete_proof ((uri, metasenv, proof, ty), goal)
330 | Intermediate _ -> Intermediate metasenv
331 | Proof _ -> assert false
336 proof_status = proof_status }
340 let disambiguate_obj status obj =
343 TacticAst.Inductive (_,(name,_,_,_)::_)
344 | TacticAst.Record (_,name,_,_) ->
345 Some (UriManager.uri_of_string (MatitaMisc.qualify status name ^ ".ind"))
346 | TacticAst.Inductive _ -> assert false
348 let (aliases, metasenv, cic, _) =
350 MatitaDisambiguator.disambiguate_obj ~dbd:(MatitaDb.instance ())
351 ~aliases:(status.aliases) ~uri obj
357 match status.proof_status with
358 | No_proof -> Intermediate metasenv
361 | Proof _ -> assert false
366 proof_status = proof_status }
370 let disambiguate_pattern aliases (hyp_paths ,goal_path) =
371 let interp path = Disambiguate.interpretate_path [] aliases path in
375 | Some path -> Some (interp path) in
376 let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
377 (hyp_paths ,goal_path)
379 let disambiguate_tactic status = function
380 | TacticAst.Transitivity (loc, term) ->
381 let status, cic = disambiguate_term status term in
382 status, TacticAst.Transitivity (loc, cic)
383 | TacticAst.Apply (loc, term) ->
384 let status, cic = disambiguate_term status term in
385 status, TacticAst.Apply (loc, cic)
386 | TacticAst.Absurd (loc, term) ->
387 let status, cic = disambiguate_term status term in
388 status, TacticAst.Absurd (loc, cic)
389 | TacticAst.Exact (loc, term) ->
390 let status, cic = disambiguate_term status term in
391 status, TacticAst.Exact (loc, cic)
392 | TacticAst.Cut (loc, term) ->
393 let status, cic = disambiguate_term status term in
394 status, TacticAst.Cut (loc, cic)
395 | TacticAst.Elim (loc, term, Some term') ->
396 let status, cic1 = disambiguate_term status term in
397 let status, cic2 = disambiguate_term status term' in
398 status, TacticAst.Elim (loc, cic1, Some cic2)
399 | TacticAst.Elim (loc, term, None) ->
400 let status, cic = disambiguate_term status term in
401 status, TacticAst.Elim (loc, cic, None)
402 | TacticAst.ElimType (loc, term) ->
403 let status, cic = disambiguate_term status term in
404 status, TacticAst.ElimType (loc, cic)
405 | TacticAst.Replace (loc, what, with_what) ->
406 let status, cic1 = disambiguate_term status what in
407 let status, cic2 = disambiguate_term status with_what in
408 status, TacticAst.Replace (loc, cic1, cic2)
409 | TacticAst.Change (loc, what, with_what, ident) ->
410 let status, cic1 = disambiguate_term status what in
411 let status, cic2 = disambiguate_term status with_what in
412 status, TacticAst.Change (loc, cic1, cic2, ident)
413 | TacticAst.Generalize (loc,term,pattern) ->
414 let status,term = disambiguate_term status term in
415 let pattern = disambiguate_pattern status.aliases pattern in
416 status, TacticAst.Generalize(loc,term,pattern)
418 (* TODO Zack a lot more of tactics to be implemented here ... *)
419 | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
420 | TacticAst.Change of 'term * 'term * 'ident option
421 | TacticAst.Decompose of 'ident * 'ident list
422 | TacticAst.Discriminate of 'ident
423 | TacticAst.Fold of reduction_kind * 'term
424 | TacticAst.Injection of 'ident
425 | TacticAst.Replace_pattern of 'term pattern * 'term
427 | TacticAst.LetIn (loc, term, name) ->
428 let status, term = disambiguate_term status term in
429 status, TacticAst.LetIn (loc,term,name)
430 | TacticAst.Reduce (loc, reduction_kind, pattern) ->
431 let pattern = disambiguate_pattern status.aliases pattern in
432 status, TacticAst.Reduce(loc, reduction_kind, pattern)
433 | TacticAst.Rewrite (loc, dir, t, pattern) ->
434 let status, term = disambiguate_term status t in
435 let pattern = disambiguate_pattern status.aliases pattern in
436 status, TacticAst.Rewrite (loc, dir, term, pattern)
437 | TacticAst.Intros (loc, num, names) ->
438 status, TacticAst.Intros (loc, num, names)
439 | TacticAst.Auto (loc,num) -> status, TacticAst.Auto (loc,num)
440 | TacticAst.Reflexivity loc -> status, TacticAst.Reflexivity loc
441 | TacticAst.Assumption loc -> status, TacticAst.Assumption loc
442 | TacticAst.Contradiction loc -> status, TacticAst.Contradiction loc
443 | TacticAst.Exists loc -> status, TacticAst.Exists loc
444 | TacticAst.Fourier loc -> status, TacticAst.Fourier loc
445 | TacticAst.Left loc -> status, TacticAst.Left loc
446 | TacticAst.Right loc -> status, TacticAst.Right loc
447 | TacticAst.Ring loc -> status, TacticAst.Ring loc
448 | TacticAst.Split loc -> status, TacticAst.Split loc
449 | TacticAst.Symmetry loc -> status, TacticAst.Symmetry loc
450 | TacticAst.Goal (loc, g) -> status, TacticAst.Goal (loc, g)
451 | TacticAst.FwdSimpl (loc, name) -> status, TacticAst.FwdSimpl (loc, name)
452 | TacticAst.LApply (loc, term, substs) ->
453 let f (status, substs) (name, term) =
454 let status, term = disambiguate_term status term in
455 status, (name, term) :: substs
457 let status, term = disambiguate_term status term in
458 let status, substs = List.fold_left f (status, []) substs in
459 status, TacticAst.LApply (loc, term, substs)
461 let rec disambiguate_tactical status = function
462 | TacticAst.Tactic (loc, tactic) ->
463 let status, tac = disambiguate_tactic status tactic in
464 status, TacticAst.Tactic (loc, tac)
465 | TacticAst.Do (loc, num, tactical) ->
466 let status, tac = disambiguate_tactical status tactical in
467 status, TacticAst.Do (loc, num, tac)
468 | TacticAst.Repeat (loc, tactical) ->
469 let status, tac = disambiguate_tactical status tactical in
470 status, TacticAst.Repeat (loc, tac)
471 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
472 let status, tacticals = disambiguate_tacticals status tacticals in
473 let tacticals = List.rev tacticals in
474 status, TacticAst.Seq (loc, tacticals)
475 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
476 let status, tactical = disambiguate_tactical status tactical in
477 let status, tacticals = disambiguate_tacticals status tacticals in
478 status, TacticAst.Then (loc, tactical, tacticals)
479 | TacticAst.Tries (loc, tacticals) ->
480 let status, tacticals = disambiguate_tacticals status tacticals in
481 status, TacticAst.Tries (loc, tacticals)
482 | TacticAst.Try (loc, tactical) ->
483 let status, tactical = disambiguate_tactical status tactical in
484 status, TacticAst.Try (loc, tactical)
485 | (TacticAst.IdTac _ | TacticAst.Fail _) as tac ->
488 and disambiguate_tacticals status tacticals =
489 let status, tacticals =
491 (fun (status, tacticals) tactical ->
492 let status, tac = disambiguate_tactical status tactical in
493 status, tac :: tacticals)
497 let tacticals = List.rev tacticals in
500 let disambiguate_command status = function
501 | TacticAst.Coercion (loc, term) ->
502 let status, term = disambiguate_term status term in
503 status, TacticAst.Coercion (loc,term)
504 | (TacticAst.Set _ | TacticAst.Qed _) as cmd ->
506 | TacticAst.Alias _ as x -> status, x
507 | TacticAst.Obj (loc,obj) ->
508 let status,obj = disambiguate_obj status obj in
509 status, TacticAst.Obj (loc,obj)
511 let disambiguate_executable status ex =
513 | TacticAst.Tactical (loc, tac) ->
514 let status, tac = disambiguate_tactical status tac in
515 status, (TacticAst.Tactical (loc, tac))
516 | TacticAst.Command (loc, cmd) ->
517 let status, cmd = disambiguate_command status cmd in
518 status, (TacticAst.Command (loc, cmd))
519 | TacticAst.Macro (_, mac) ->
520 command_error (sprintf "The macro %s can't be in a script"
521 (TacticAstPp.pp_macro_ast mac))
523 let disambiguate_comment status c =
525 | TacticAst.Note (loc,n) -> status, TacticAst.Note (loc,n)
526 | TacticAst.Code (loc,ex) ->
527 let status, ex = disambiguate_executable status ex in
528 status, TacticAst.Code (loc,ex)
530 let disambiguate_statement status statement =
532 | TacticAst.Comment (loc,c) ->
533 let status, c = disambiguate_comment status c in
534 status, TacticAst.Comment (loc,c)
535 | TacticAst.Executable (loc,ex) ->
536 let status, ex = disambiguate_executable status ex in
537 status, TacticAst.Executable (loc,ex)
539 let eval_ast status ast =
540 let status,st = disambiguate_statement status ast in
541 (* this disambiguation step should be deferred to support tacticals *)
544 let eval_from_stream status str cb =
545 let stl = CicTextualParser2.parse_statements str in
547 (fun status ast -> cb status ast;eval_ast status ast) status
550 let eval_string status str =
551 eval_from_stream status (Stream.of_string str) (fun _ _ -> ())
553 let default_options () =
555 StringMap.add "baseuri"
557 (Helm_registry.get "matita.baseuri" ^ Helm_registry.get "matita.owner"))
561 StringMap.add "basedir"
562 (String (Helm_registry.get "matita.basedir" ))
569 aliases = DisambiguateTypes.empty_environment;
570 proof_status = No_proof;
571 options = default_options ();