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
33 | TacticAst.Discriminate (_,term) -> Tactics.discriminate term
34 | TacticAst.Exists _ -> Tactics.exists
35 | TacticAst.Fourier _ -> Tactics.fourier
36 | TacticAst.Generalize (_,term,pat) -> Tactics.generalize term pat
37 | TacticAst.Goal (_, n) -> Tactics.set_goal n
38 | TacticAst.Left _ -> Tactics.left
39 | TacticAst.Right _ -> Tactics.right
40 | TacticAst.Ring _ -> Tactics.ring
41 | TacticAst.Split _ -> Tactics.split
42 | TacticAst.Symmetry _ -> Tactics.symmetry
43 | TacticAst.Transitivity (_, term) -> Tactics.transitivity term
44 | TacticAst.Apply (_, term) -> Tactics.apply term
45 | TacticAst.Absurd (_, term) -> Tactics.absurd term
46 | TacticAst.Exact (_, term) -> Tactics.exact term
47 | TacticAst.Cut (_, term) -> Tactics.cut term
48 | TacticAst.Elim (_, term, _) ->
49 (* TODO Zack implement "using" argument *)
50 (* old: Tactics.elim_intros_simpl term *)
51 Tactics.elim_intros term
52 | TacticAst.ElimType (_, term) -> Tactics.elim_type term
53 | TacticAst.Replace (_, what, with_what) -> Tactics.replace ~what ~with_what
54 | TacticAst.Auto (_,depth) ->
55 (* AutoTactic.auto_tac ~num (MatitaDb.instance ()) *)
56 AutoTactic.auto_tac_new ?depth ~dbd:(MatitaDb.instance ()) ()
57 | TacticAst.Change (_, what, with_what, _) -> Tactics.change ~what ~with_what
59 (* TODO Zack a lot more of tactics to be implemented here ... *)
60 | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
61 | TacticAst.Change of 'term * 'term * 'ident option
62 | TacticAst.Decompose of 'ident * 'ident list
63 | TacticAst.Fold of reduction_kind * 'term
64 | TacticAst.Injection of 'ident
65 | TacticAst.Replace_pattern of 'term pattern * 'term
67 | TacticAst.LetIn (loc,term,name) ->
68 Tactics.letin term ~mk_fresh_name_callback:(namer_of [name])
69 | TacticAst.Reduce (_, reduction_kind, pattern) ->
70 (match reduction_kind with
71 | `Normalize -> Tactics.normalize ~pattern
72 | `Reduce -> Tactics.reduce ~pattern
73 | `Simpl -> Tactics.simpl ~pattern
74 | `Whd -> Tactics.whd ~pattern)
75 | TacticAst.Rewrite (_, dir, t, pattern) ->
77 EqualityTactics.rewrite_tac ~where:pattern ~term:t ()
79 EqualityTactics.rewrite_back_tac ~where:pattern ~term:t ()
80 | TacticAst.FwdSimpl (_, name) ->
81 Tactics.fwd_simpl ~hyp:(Cic.Name name) ~dbd:(MatitaDb.instance ())
82 | TacticAst.LApply (_, term, substs) ->
83 let f (name, term) = Cic.Name name, term in
84 Tactics.lapply ~substs:(List.map f substs) term
86 let eval_tactical status tac =
87 let apply_tactic tactic =
89 ProofEngineTypes.apply_tactic tactic (MatitaMisc.get_proof_status status)
94 let (_,metasenv,_,_) = proof in
97 | (ng,_,_)::_ -> Incomplete_proof (proof,ng))
98 | ng::_ -> Incomplete_proof (proof, ng)
100 { status with proof_status = new_status }
102 let rec tactical_of_ast = function
103 | TacticAst.Tactic (loc, tactic) -> tactic_of_ast tactic
104 | TacticAst.Fail loc -> Tacticals.fail
105 | TacticAst.Do (loc, num, tactical) ->
106 Tacticals.do_tactic num (tactical_of_ast tactical)
107 | TacticAst.IdTac loc -> Tacticals.id_tac
108 | TacticAst.Repeat (loc, tactical) ->
109 Tacticals.repeat_tactic (tactical_of_ast tactical)
110 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
111 Tacticals.seq (List.map tactical_of_ast tacticals)
112 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
113 Tacticals.thens (tactical_of_ast tactical)
114 (List.map tactical_of_ast tacticals)
115 | TacticAst.Tries (loc, tacticals) ->
116 Tacticals.try_tactics
117 (List.map (fun t -> "", tactical_of_ast t) tacticals)
118 | TacticAst.Try (loc, tactical) ->
119 Tacticals.try_tactic (tactical_of_ast tactical)
121 apply_tactic (tactical_of_ast tac)
123 let eval_coercion status coercion =
124 let coer_uri,coer_ty =
129 CicEnvironment.get_obj CicUniv.empty_ugraph uri
132 | Cic.Constant (_,_,ty,_,_)
133 | Cic.Variable (_,_,ty,_,_) ->
136 | Cic.MutConstruct (uri,t,c,_) ->
138 CicEnvironment.get_obj CicUniv.empty_ugraph uri
141 | Cic.InductiveDefinition (l,_,_,_) ->
142 let (_,_,_,cl) = List.nth l t in
143 let (_,cty) = List.nth cl c in
148 (* we have to get the source and the tgt type uri
149 * in Coq syntax we have already their names, but
150 * since we don't support Funclass and similar I think
151 * all the coercion should be of the form
153 * So we should be able to extract them from the coercion type
155 let extract_last_two_p ty =
156 let rec aux = function
157 | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))
158 | Cic.Prod( _, src, tgt) -> src, tgt
163 let ty_src,ty_tgt = extract_last_two_p coer_ty in
166 let ty_src = CicReduction.whd context ty_src in
167 CicUtil.uri_of_term ty_src
170 let ty_tgt = CicReduction.whd context ty_tgt in
171 CicUtil.uri_of_term ty_tgt
174 (* also adds them to the Db *)
175 CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri in
177 List.fold_left (fun s (uri,o,ugraph) -> MatitaSync.add_obj uri o status)
178 status new_coercions in
179 {status with proof_status = No_proof}
181 let generate_elimination_principles uri status =
182 let elim sort status =
184 let uri,obj = CicElim.elim_of ~sort uri 0 in
185 MatitaSync.add_obj uri obj status
186 with CicElim.Can_t_eliminate -> status
188 List.fold_left (fun status sort -> elim sort status) status
189 [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ]
191 let generate_projections uri fields status =
192 let projections = CicRecord.projections_of uri fields in
194 (fun status (uri, name, bo) ->
197 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
198 let bo = Unshare.unshare bo in
199 let ty = Unshare.unshare ty in
200 let attrs = [`Class `Projection; `Generated] in
201 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
202 MatitaSync.add_obj uri obj status
204 CicTypeChecker.TypeCheckerFailure s ->
206 ("Unable to create projection " ^ name ^ " cause: " ^ s);
208 | CicEnvironment.Object_not_found uri ->
209 let depend = UriManager.name_of_uri uri in
211 ("Unable to create projection " ^ name ^ " because it requires " ^ depend);
215 let eval_command status cmd =
217 | TacticAst.Set (loc, name, value) -> set_option status name value
218 | TacticAst.Qed loc ->
219 let uri, metasenv, bo, ty =
220 match status.proof_status with
221 | Proof (Some uri, metasenv, body, ty) ->
222 uri, metasenv, body, ty
223 | Proof (None, metasenv, body, ty) ->
225 ("Someone allows to start a thm without giving the "^
226 "name/uri. This should be fixed!")
227 | _-> command_error "You can't qed an uncomplete theorem"
229 let suri = UriManager.string_of_uri uri in
230 if metasenv <> [] then
231 command_error "Proof not completed! metasenv is not empty!";
232 let name = UriManager.name_of_uri uri in
233 let obj = Cic.Constant (name,Some bo,ty,[],[]) in
234 MatitaSync.add_obj uri obj status
235 | TacticAst.Coercion (loc, coercion) ->
236 eval_coercion status coercion
237 | TacticAst.Alias (loc, spec) ->
239 | TacticAst.Ident_alias (id,uri) ->
240 {status with aliases =
241 DisambiguateTypes.Environment.add
242 (DisambiguateTypes.Id id)
243 ("boh?",(fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string uri)))
245 | TacticAst.Symbol_alias (symb, instance, desc) ->
246 {status with aliases =
247 DisambiguateTypes.Environment.add
248 (DisambiguateTypes.Symbol (symb,instance))
249 (DisambiguateChoices.lookup_symbol_by_dsc symb desc)
251 | TacticAst.Number_alias (instance,desc) ->
252 {status with aliases =
253 DisambiguateTypes.Environment.add
254 (DisambiguateTypes.Num instance)
255 (DisambiguateChoices.lookup_num_by_dsc desc) status.aliases })
256 | TacticAst.Obj (loc,obj) ->
259 Cic.Constant (name,_,_,_,_)
260 | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
261 | Cic.InductiveDefinition (types,_,_,_) ->
263 (match types with (name,_,_,_)::_ -> name | _ -> assert false)
264 | _ -> assert false in
266 UriManager.uri_of_string (MatitaMisc.qualify status name ^ ext)
268 let metasenv = MatitaMisc.get_proof_metasenv status in
270 Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
271 assert (metasenv = metasenv');
273 match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false in
274 let initial_proof = (Some uri, metasenv, bo, ty) in
275 { status with proof_status = Incomplete_proof (initial_proof,goalno)}
277 if metasenv <> [] then
279 "metasenv not empty while giving a definition with body: " ^
280 CicMetaSubst.ppmetasenv metasenv []);
281 let status = MatitaSync.add_obj uri obj status in
283 Cic.Constant _ -> status
284 | Cic.InductiveDefinition (_,_,_,attrs) ->
285 let status = generate_elimination_principles uri status in
286 let rec get_record_attrs =
289 | (`Class (`Record fields))::_ -> Some fields
290 | _::tl -> get_record_attrs tl
292 (match get_record_attrs attrs with
293 None -> status (* not a record *)
294 | Some fields -> generate_projections uri fields status)
296 | Cic.Variable _ -> assert false
298 let eval_executable status ex =
300 | TacticAst.Tactical (_, tac) -> eval_tactical status tac
301 | TacticAst.Command (_, cmd) -> eval_command status cmd
302 | TacticAst.Macro (_, mac) ->
303 command_error (sprintf "The macro %s can't be in a script"
304 (TacticAstPp.pp_macro_cic mac))
306 let eval_comment status c = status
310 | TacticAst.Executable (_,ex) -> eval_executable status ex
311 | TacticAst.Comment (_,c) -> eval_comment status c
313 let disambiguate_term status term =
314 let (aliases, metasenv, cic, _) =
316 MatitaDisambiguator.disambiguate_term ~dbd:(MatitaDb.instance ())
317 ~aliases:(status.aliases) ~context:(MatitaMisc.get_proof_context status)
318 ~metasenv:(MatitaMisc.get_proof_metasenv status) term
324 match status.proof_status with
325 | No_proof -> Intermediate metasenv
326 | Incomplete_proof ((uri, _, proof, ty), goal) ->
327 Incomplete_proof ((uri, metasenv, proof, ty), goal)
328 | Intermediate _ -> Intermediate metasenv
329 | Proof _ -> assert false
334 proof_status = proof_status }
338 let disambiguate_obj status obj =
341 TacticAst.Inductive (_,(name,_,_,_)::_)
342 | TacticAst.Record (_,name,_,_) ->
343 Some (UriManager.uri_of_string (MatitaMisc.qualify status name ^ ".ind"))
344 | TacticAst.Inductive _ -> assert false
346 let (aliases, metasenv, cic, _) =
348 MatitaDisambiguator.disambiguate_obj ~dbd:(MatitaDb.instance ())
349 ~aliases:(status.aliases) ~uri obj
355 match status.proof_status with
356 | No_proof -> Intermediate metasenv
359 | Proof _ -> assert false
364 proof_status = proof_status }
368 let disambiguate_pattern aliases (hyp_paths ,goal_path) =
369 let interp path = Disambiguate.interpretate_path [] aliases path in
373 | Some path -> Some (interp path) in
374 let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
375 (hyp_paths ,goal_path)
377 let disambiguate_tactic status = function
378 | TacticAst.Transitivity (loc, term) ->
379 let status, cic = disambiguate_term status term in
380 status, TacticAst.Transitivity (loc, cic)
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.Exact (loc, term) ->
388 let status, cic = disambiguate_term status term in
389 status, TacticAst.Exact (loc, cic)
390 | TacticAst.Cut (loc, term) ->
391 let status, cic = disambiguate_term status term in
392 status, TacticAst.Cut (loc, cic)
393 | TacticAst.Elim (loc, term, Some term') ->
394 let status, cic1 = disambiguate_term status term in
395 let status, cic2 = disambiguate_term status term' in
396 status, TacticAst.Elim (loc, cic1, Some cic2)
397 | TacticAst.Elim (loc, term, None) ->
398 let status, cic = disambiguate_term status term in
399 status, TacticAst.Elim (loc, cic, None)
400 | TacticAst.ElimType (loc, term) ->
401 let status, cic = disambiguate_term status term in
402 status, TacticAst.ElimType (loc, cic)
403 | TacticAst.Replace (loc, what, with_what) ->
404 let status, cic1 = disambiguate_term status what in
405 let status, cic2 = disambiguate_term status with_what in
406 status, TacticAst.Replace (loc, cic1, cic2)
407 | TacticAst.Change (loc, what, with_what, ident) ->
408 let status, cic1 = disambiguate_term status what in
409 let status, cic2 = disambiguate_term status with_what in
410 status, TacticAst.Change (loc, cic1, cic2, ident)
411 | TacticAst.Generalize (loc,term,pattern) ->
412 let status,term = disambiguate_term status term in
413 let pattern = disambiguate_pattern status.aliases pattern in
414 status, TacticAst.Generalize(loc,term,pattern)
415 | TacticAst.Discriminate (loc,term) ->
416 let status,term = disambiguate_term status term in
417 status, TacticAst.Discriminate(loc,term)
419 (* TODO Zack a lot more of tactics to be implemented here ... *)
420 | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
421 | TacticAst.Change of 'term * 'term * 'ident option
422 | TacticAst.Decompose of 'ident * 'ident list
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 ();