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 (_, term) ->
81 Tactics.fwd_simpl ~what:term ~dbd:(MatitaDb.instance ())
82 | TacticAst.LApply (_, to_what, what) ->
83 Tactics.lapply ?to_what what
85 let eval_tactical status tac =
86 let apply_tactic tactic =
88 ProofEngineTypes.apply_tactic tactic (MatitaMisc.get_proof_status status)
93 let (_,metasenv,_,_) = proof in
96 | (ng,_,_)::_ -> Incomplete_proof (proof,ng))
97 | ng::_ -> Incomplete_proof (proof, ng)
99 { status with proof_status = new_status }
101 let rec tactical_of_ast = function
102 | TacticAst.Tactic (loc, tactic) -> tactic_of_ast tactic
103 | TacticAst.Fail loc -> Tacticals.fail
104 | TacticAst.Do (loc, num, tactical) ->
105 Tacticals.do_tactic num (tactical_of_ast tactical)
106 | TacticAst.IdTac loc -> Tacticals.id_tac
107 | TacticAst.Repeat (loc, tactical) ->
108 Tacticals.repeat_tactic (tactical_of_ast tactical)
109 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
110 Tacticals.seq (List.map tactical_of_ast tacticals)
111 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
112 Tacticals.thens (tactical_of_ast tactical)
113 (List.map tactical_of_ast tacticals)
114 | TacticAst.Tries (loc, tacticals) ->
115 Tacticals.try_tactics
116 (List.map (fun t -> "", tactical_of_ast t) tacticals)
117 | TacticAst.Try (loc, tactical) ->
118 Tacticals.try_tactic (tactical_of_ast tactical)
120 apply_tactic (tactical_of_ast tac)
122 let eval_coercion status coercion =
123 let coer_uri,coer_ty =
128 CicEnvironment.get_obj CicUniv.empty_ugraph uri
131 | Cic.Constant (_,_,ty,_,_)
132 | Cic.Variable (_,_,ty,_,_) ->
135 | Cic.MutConstruct (uri,t,c,_) ->
137 CicEnvironment.get_obj CicUniv.empty_ugraph uri
140 | Cic.InductiveDefinition (l,_,_,_) ->
141 let (_,_,_,cl) = List.nth l t in
142 let (_,cty) = List.nth cl c in
147 (* we have to get the source and the tgt type uri
148 * in Coq syntax we have already their names, but
149 * since we don't support Funclass and similar I think
150 * all the coercion should be of the form
152 * So we should be able to extract them from the coercion type
154 let extract_last_two_p ty =
155 let rec aux = function
156 | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))
157 | Cic.Prod( _, src, tgt) -> src, tgt
162 let ty_src,ty_tgt = extract_last_two_p coer_ty in
165 let ty_src = CicReduction.whd context ty_src in
166 CicUtil.uri_of_term ty_src
169 let ty_tgt = CicReduction.whd context ty_tgt in
170 CicUtil.uri_of_term ty_tgt
173 (* also adds them to the Db *)
174 CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri in
176 List.fold_left (fun s (uri,o,ugraph) -> MatitaSync.add_obj uri o status)
177 status new_coercions in
178 {status with proof_status = No_proof}
180 let generate_elimination_principles uri status =
181 let elim sort status =
183 let uri,obj = CicElim.elim_of ~sort uri 0 in
184 MatitaSync.add_obj uri obj status
185 with CicElim.Can_t_eliminate -> status
187 List.fold_left (fun status sort -> elim sort status) status
188 [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ]
190 let generate_projections uri fields status =
191 let projections = CicRecord.projections_of uri fields in
193 (fun status (uri, name, bo) ->
196 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
197 let bo = Unshare.unshare bo in
198 let ty = Unshare.unshare ty in
199 let attrs = [`Class `Projection; `Generated] in
200 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
201 MatitaSync.add_obj uri obj status
203 CicTypeChecker.TypeCheckerFailure s ->
205 ("Unable to create projection " ^ name ^ " cause: " ^ s);
207 | CicEnvironment.Object_not_found uri ->
208 let depend = UriManager.name_of_uri uri in
210 ("Unable to create projection " ^ name ^ " because it requires " ^ depend);
214 let eval_command status cmd =
216 | TacticAst.Set (loc, name, value) -> set_option status name value
217 | TacticAst.Qed loc ->
218 let uri, metasenv, bo, ty =
219 match status.proof_status with
220 | Proof (Some uri, metasenv, body, ty) ->
221 uri, metasenv, body, ty
222 | Proof (None, metasenv, body, ty) ->
224 ("Someone allows to start a thm without giving the "^
225 "name/uri. This should be fixed!")
226 | _-> command_error "You can't qed an uncomplete theorem"
228 let suri = UriManager.string_of_uri uri in
229 if metasenv <> [] then
230 command_error "Proof not completed! metasenv is not empty!";
231 let name = UriManager.name_of_uri uri in
232 let obj = Cic.Constant (name,Some bo,ty,[],[]) in
233 MatitaSync.add_obj uri obj status
234 | TacticAst.Coercion (loc, coercion) ->
235 eval_coercion status coercion
236 | TacticAst.Alias (loc, spec) ->
238 | TacticAst.Ident_alias (id,uri) ->
239 {status with aliases =
240 DisambiguateTypes.Environment.add
241 (DisambiguateTypes.Id id)
242 ("boh?",(fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string uri)))
244 | TacticAst.Symbol_alias (symb, instance, desc) ->
245 {status with aliases =
246 DisambiguateTypes.Environment.add
247 (DisambiguateTypes.Symbol (symb,instance))
248 (DisambiguateChoices.lookup_symbol_by_dsc symb desc)
250 | TacticAst.Number_alias (instance,desc) ->
251 {status with aliases =
252 DisambiguateTypes.Environment.add
253 (DisambiguateTypes.Num instance)
254 (DisambiguateChoices.lookup_num_by_dsc desc) status.aliases })
255 | TacticAst.Obj (loc,obj) ->
258 Cic.Constant (name,_,_,_,_)
259 | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
260 | Cic.InductiveDefinition (types,_,_,_) ->
262 (match types with (name,_,_,_)::_ -> name | _ -> assert false)
263 | _ -> assert false in
265 UriManager.uri_of_string (MatitaMisc.qualify status name ^ ext)
267 let metasenv = MatitaMisc.get_proof_metasenv status in
269 Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
270 assert (metasenv = metasenv');
272 match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false in
273 let initial_proof = (Some uri, metasenv, bo, ty) in
274 { status with proof_status = Incomplete_proof (initial_proof,goalno)}
276 if metasenv <> [] then
278 "metasenv not empty while giving a definition with body: " ^
279 CicMetaSubst.ppmetasenv metasenv []);
280 let status = MatitaSync.add_obj uri obj status in
282 Cic.Constant _ -> status
283 | Cic.InductiveDefinition (_,_,_,attrs) ->
284 let status = generate_elimination_principles uri status in
285 let rec get_record_attrs =
288 | (`Class (`Record fields))::_ -> Some fields
289 | _::tl -> get_record_attrs tl
291 (match get_record_attrs attrs with
292 None -> status (* not a record *)
293 | Some fields -> generate_projections uri fields status)
295 | Cic.Variable _ -> assert false
297 let eval_executable status ex =
299 | TacticAst.Tactical (_, tac) -> eval_tactical status tac
300 | TacticAst.Command (_, cmd) -> eval_command status cmd
301 | TacticAst.Macro (_, mac) ->
302 command_error (sprintf "The macro %s can't be in a script"
303 (TacticAstPp.pp_macro_cic mac))
305 let eval_comment status c = status
309 | TacticAst.Executable (_,ex) -> eval_executable status ex
310 | TacticAst.Comment (_,c) -> eval_comment status c
312 let disambiguate_term status term =
313 let (aliases, metasenv, cic, _) =
315 MatitaDisambiguator.disambiguate_term ~dbd:(MatitaDb.instance ())
316 ~aliases:(status.aliases) ~context:(MatitaMisc.get_proof_context status)
317 ~metasenv:(MatitaMisc.get_proof_metasenv status) term
323 match status.proof_status with
324 | No_proof -> Intermediate metasenv
325 | Incomplete_proof ((uri, _, proof, ty), goal) ->
326 Incomplete_proof ((uri, metasenv, proof, ty), goal)
327 | Intermediate _ -> Intermediate metasenv
328 | Proof _ -> assert false
333 proof_status = proof_status }
337 let disambiguate_obj status obj =
340 TacticAst.Inductive (_,(name,_,_,_)::_)
341 | TacticAst.Record (_,name,_,_) ->
342 Some (UriManager.uri_of_string (MatitaMisc.qualify status name ^ ".ind"))
343 | TacticAst.Inductive _ -> assert false
345 let (aliases, metasenv, cic, _) =
347 MatitaDisambiguator.disambiguate_obj ~dbd:(MatitaDb.instance ())
348 ~aliases:(status.aliases) ~uri obj
354 match status.proof_status with
355 | No_proof -> Intermediate metasenv
358 | Proof _ -> assert false
363 proof_status = proof_status }
367 let disambiguate_pattern aliases (hyp_paths ,goal_path) =
368 let interp path = Disambiguate.interpretate_path [] aliases path in
372 | Some path -> Some (interp path) in
373 let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
374 (hyp_paths ,goal_path)
376 let disambiguate_tactic status = function
377 | TacticAst.Transitivity (loc, term) ->
378 let status, cic = disambiguate_term status term in
379 status, TacticAst.Transitivity (loc, cic)
380 | TacticAst.Apply (loc, term) ->
381 let status, cic = disambiguate_term status term in
382 status, TacticAst.Apply (loc, cic)
383 | TacticAst.Absurd (loc, term) ->
384 let status, cic = disambiguate_term status term in
385 status, TacticAst.Absurd (loc, cic)
386 | TacticAst.Exact (loc, term) ->
387 let status, cic = disambiguate_term status term in
388 status, TacticAst.Exact (loc, cic)
389 | TacticAst.Cut (loc, term) ->
390 let status, cic = disambiguate_term status term in
391 status, TacticAst.Cut (loc, cic)
392 | TacticAst.Elim (loc, term, Some term') ->
393 let status, cic1 = disambiguate_term status term in
394 let status, cic2 = disambiguate_term status term' in
395 status, TacticAst.Elim (loc, cic1, Some cic2)
396 | TacticAst.Elim (loc, term, None) ->
397 let status, cic = disambiguate_term status term in
398 status, TacticAst.Elim (loc, cic, None)
399 | TacticAst.ElimType (loc, term) ->
400 let status, cic = disambiguate_term status term in
401 status, TacticAst.ElimType (loc, cic)
402 | TacticAst.Replace (loc, what, with_what) ->
403 let status, cic1 = disambiguate_term status what in
404 let status, cic2 = disambiguate_term status with_what in
405 status, TacticAst.Replace (loc, cic1, cic2)
406 | TacticAst.Change (loc, what, with_what, pattern) ->
407 let status, cic1 = disambiguate_term status what in
408 let status, cic2 = disambiguate_term status with_what in
409 let pattern = disambiguate_pattern status.aliases pattern in
410 status, TacticAst.Change (loc, cic1, cic2, pattern)
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, term) ->
452 let status, term = disambiguate_term status term in
453 status, TacticAst.FwdSimpl (loc, term)
454 | TacticAst.LApply (loc, Some to_what, what) ->
455 let status, to_what = disambiguate_term status to_what in
456 let status, what = disambiguate_term status what in
457 status, TacticAst.LApply (loc, Some to_what, what)
458 | TacticAst.LApply (loc, None, what) ->
459 let status, what = disambiguate_term status what in
460 status, TacticAst.LApply (loc, None, what)
462 let rec disambiguate_tactical status = function
463 | TacticAst.Tactic (loc, tactic) ->
464 let status, tac = disambiguate_tactic status tactic in
465 status, TacticAst.Tactic (loc, tac)
466 | TacticAst.Do (loc, num, tactical) ->
467 let status, tac = disambiguate_tactical status tactical in
468 status, TacticAst.Do (loc, num, tac)
469 | TacticAst.Repeat (loc, tactical) ->
470 let status, tac = disambiguate_tactical status tactical in
471 status, TacticAst.Repeat (loc, tac)
472 | TacticAst.Seq (loc, tacticals) -> (* tac1; tac2; ... *)
473 let status, tacticals = disambiguate_tacticals status tacticals in
474 let tacticals = List.rev tacticals in
475 status, TacticAst.Seq (loc, tacticals)
476 | TacticAst.Then (loc, tactical, tacticals) -> (* tac; [ tac1 | ... ] *)
477 let status, tactical = disambiguate_tactical status tactical in
478 let status, tacticals = disambiguate_tacticals status tacticals in
479 status, TacticAst.Then (loc, tactical, tacticals)
480 | TacticAst.Tries (loc, tacticals) ->
481 let status, tacticals = disambiguate_tacticals status tacticals in
482 status, TacticAst.Tries (loc, tacticals)
483 | TacticAst.Try (loc, tactical) ->
484 let status, tactical = disambiguate_tactical status tactical in
485 status, TacticAst.Try (loc, tactical)
486 | (TacticAst.IdTac _ | TacticAst.Fail _) as tac ->
489 and disambiguate_tacticals status tacticals =
490 let status, tacticals =
492 (fun (status, tacticals) tactical ->
493 let status, tac = disambiguate_tactical status tactical in
494 status, tac :: tacticals)
498 let tacticals = List.rev tacticals in
501 let disambiguate_command status = function
502 | TacticAst.Coercion (loc, term) ->
503 let status, term = disambiguate_term status term in
504 status, TacticAst.Coercion (loc,term)
505 | (TacticAst.Set _ | TacticAst.Qed _) as cmd ->
507 | TacticAst.Alias _ as x -> status, x
508 | TacticAst.Obj (loc,obj) ->
509 let status,obj = disambiguate_obj status obj in
510 status, TacticAst.Obj (loc,obj)
512 let disambiguate_executable status ex =
514 | TacticAst.Tactical (loc, tac) ->
515 let status, tac = disambiguate_tactical status tac in
516 status, (TacticAst.Tactical (loc, tac))
517 | TacticAst.Command (loc, cmd) ->
518 let status, cmd = disambiguate_command status cmd in
519 status, (TacticAst.Command (loc, cmd))
520 | TacticAst.Macro (_, mac) ->
521 command_error (sprintf "The macro %s can't be in a script"
522 (TacticAstPp.pp_macro_ast mac))
524 let disambiguate_comment status c =
526 | TacticAst.Note (loc,n) -> status, TacticAst.Note (loc,n)
527 | TacticAst.Code (loc,ex) ->
528 let status, ex = disambiguate_executable status ex in
529 status, TacticAst.Code (loc,ex)
531 let disambiguate_statement status statement =
533 | TacticAst.Comment (loc,c) ->
534 let status, c = disambiguate_comment status c in
535 status, TacticAst.Comment (loc,c)
536 | TacticAst.Executable (loc,ex) ->
537 let status, ex = disambiguate_executable status ex in
538 status, TacticAst.Executable (loc,ex)
540 let eval_ast status ast =
541 let status,st = disambiguate_statement status ast in
542 (* this disambiguation step should be deferred to support tacticals *)
545 let eval_from_stream status str cb =
546 let stl = CicTextualParser2.parse_statements str in
548 (fun status ast -> cb status ast;eval_ast status ast) status
551 let eval_string status str =
552 eval_from_stream status (Stream.of_string str) (fun _ _ -> ())
554 let default_options () =
556 StringMap.add "baseuri"
558 (Helm_registry.get "matita.baseuri" ^ Helm_registry.get "matita.owner"))
562 StringMap.add "basedir"
563 (String (Helm_registry.get "matita.basedir" ))
570 aliases = DisambiguateTypes.empty_environment;
571 proof_status = No_proof;
572 options = default_options ();