1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 (* mo file name, ma file name *)
30 exception NMacro of GrafiteAst.loc * GrafiteAst.nmacro
32 type 'a disambiguator_input = string * int * 'a
35 do_heavy_checks: bool ;
38 let basic_eval_unification_hint (t,n) status =
39 NCicUnifHint.add_user_provided_hint status t n
42 let inject_unification_hint =
43 let basic_eval_unification_hint (t,n)
44 ~refresh_uri_in_universe
47 let t = refresh_uri_in_term t in basic_eval_unification_hint (t,n)
49 GrafiteTypes.Serializer.register#run "unification_hints"
50 basic_eval_unification_hint
53 let eval_unification_hint status t n =
54 let metasenv,subst,status,t =
55 GrafiteDisambiguate.disambiguate_nterm None status [] [] [] ("",0,t) in
57 let t = NCicUntrusted.apply_subst subst [] t in
58 let status = basic_eval_unification_hint (t,n) status in
59 let dump = inject_unification_hint (t,n)::status#dump in
60 let status = status#set_dump dump in
64 let basic_index_obj l status =
68 List.fold_left (fun t k ->
69 NDiscriminationTree.DiscriminationTree.index t k v)
74 let record_index_obj =
76 ~refresh_uri_in_universe
81 (fun ks,v -> List.map refresh_uri_in_term ks, refresh_uri_in_term v)
84 GrafiteTypes.Serializer.register#run "index_obj" aux
87 let compute_keys status uri height kind =
89 let orig_ty = NTacStatus.mk_cic_term [] ty in
90 let status,keys = NnAuto.keys_of_type status orig_ty in
94 snd (NTacStatus.term_of_cic_term status t (NTacStatus.ctx_of t)))
97 keys,NCic.Const(NReference.reference_of_spec uri spec)
101 | NCic.Fixpoint (ind,ifl,_) ->
103 (fun (_,_,rno,ty,_) i ->
104 if ind then mk_item ty (NReference.Fix (i,rno,height))
105 else mk_item ty (NReference.CoFix height)) ifl
106 | NCic.Inductive (b,lno,itl,_) ->
108 (fun (_,_,ty,_) i -> mk_item ty (NReference.Ind (b,i,lno))) itl
110 List.map (fun ((_,_,ty),i,j) -> mk_item ty (NReference.Con (i,j+1,lno)))
111 (List.flatten (HExtlib.list_mapi
112 (fun (_,_,_,cl) i -> HExtlib.list_mapi (fun x j-> x,i,j) cl)
114 | NCic.Constant (_,_,Some _, ty, _) ->
115 [ mk_item ty (NReference.Def height) ]
116 | NCic.Constant (_,_,None, ty, _) ->
117 [ mk_item ty NReference.Decl ]
121 let keys = List.filter
124 | (NCic.Appl (NCic.Meta _::_)) -> false
130 HLog.debug ("Indexing:" ^
131 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
132 HLog.debug ("With keys:" ^ String.concat "\n" (List.map (fun t ->
133 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t) keys));
138 HLog.debug ("Not indexing:" ^
139 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
145 let index_obj_for_auto status (uri, height, _, _, kind) =
146 (*prerr_endline (string_of_int height);*)
147 let data = compute_keys status uri height kind in
148 let status = basic_index_obj data status in
149 let dump = record_index_obj data :: status#dump in
153 let index_eq uri status =
154 let eq_status = status#eq_cache in
155 let eq_status1 = NCicParamod.index_obj eq_status uri in
156 status#set_eq_cache eq_status1
159 let record_index_eq =
160 let basic_index_eq uri
161 ~refresh_uri_in_universe
163 = index_eq (NCicLibrary.refresh_uri uri)
165 GrafiteTypes.Serializer.register#run "index_eq" basic_index_eq
168 let index_eq_for_auto status uri =
169 if NnAuto.is_a_fact_obj status uri then
170 let newstatus = index_eq uri status in
171 if newstatus#eq_cache == status#eq_cache then status
173 ((*prerr_endline ("recording " ^ (NUri.string_of_uri uri));*)
174 let dump = record_index_eq uri :: newstatus#dump
175 in newstatus#set_dump dump)
177 ((*prerr_endline "Not a fact";*)
181 let basic_eval_add_constraint (u1,u2) status =
182 NCicLibrary.add_constraint status u1 u2
185 let inject_constraint =
186 let basic_eval_add_constraint (u1,u2)
187 ~refresh_uri_in_universe
190 let u1 = refresh_uri_in_universe u1 in
191 let u2 = refresh_uri_in_universe u2 in
192 basic_eval_add_constraint (u1,u2)
194 GrafiteTypes.Serializer.register#run "constraints" basic_eval_add_constraint
197 let eval_add_constraint status u1 u2 =
198 let status = basic_eval_add_constraint (u1,u2) status in
199 let dump = inject_constraint (u1,u2)::status#dump in
200 let status = status#set_dump dump in
204 let eval_ng_tac tac =
205 let rec aux f (text, prefix_len, tac) =
207 | GrafiteAst.NApply (_loc, t) -> NTactics.apply_tac (text,prefix_len,t)
208 | GrafiteAst.NSmartApply (_loc, t) ->
209 NnAuto.smart_apply_tac (text,prefix_len,t)
210 | GrafiteAst.NAssert (_loc, seqs) ->
213 (function (hyps,concl) ->
216 (id,`Decl t) -> id,`Decl (text,prefix_len,t)
217 |(id,`Def (b,t))->id,`Def((text,prefix_len,b),(text,prefix_len,t))
219 (text,prefix_len,concl))
221 | GrafiteAst.NAuto (_loc, (None,a)) ->
222 NnAuto.auto_tac ~params:(None,a) ?trace_ref:None
223 | GrafiteAst.NAuto (_loc, (Some l,a)) ->
225 ~params:(Some List.map (fun x -> "",0,x) l,a) ?trace_ref:None
226 | GrafiteAst.NBranch _ -> NTactics.branch_tac ~force:false
227 | GrafiteAst.NCases (_loc, what, where) ->
229 ~what:(text,prefix_len,what)
230 ~where:(text,prefix_len,where)
231 | GrafiteAst.NCase1 (_loc,n) -> NTactics.case1_tac n
232 | GrafiteAst.NChange (_loc, pat, ww) ->
234 ~where:(text,prefix_len,pat) ~with_what:(text,prefix_len,ww)
235 | GrafiteAst.NConstructor (_loc,num,args) ->
236 NTactics.constructor_tac
237 ?num ~args:(List.map (fun x -> text,prefix_len,x) args)
238 | GrafiteAst.NCut (_loc, t) -> NTactics.cut_tac (text,prefix_len,t)
239 (*| GrafiteAst.NDiscriminate (_,what) -> NDestructTac.discriminate_tac ~what:(text,prefix_len,what)
240 | GrafiteAst.NSubst (_,what) -> NDestructTac.subst_tac ~what:(text,prefix_len,what)*)
241 | GrafiteAst.NDestruct (_,dom,skip) -> NDestructTac.destruct_tac dom skip
242 | GrafiteAst.NDot _ -> NTactics.dot_tac
243 | GrafiteAst.NElim (_loc, what, where) ->
245 ~what:(text,prefix_len,what)
246 ~where:(text,prefix_len,where)
247 | GrafiteAst.NFocus (_,l) -> NTactics.focus_tac l
248 | GrafiteAst.NGeneralize (_loc, where) ->
249 NTactics.generalize_tac ~where:(text,prefix_len,where)
250 | GrafiteAst.NId _ -> (fun x -> x)
251 | GrafiteAst.NIntro (_loc,n) -> NTactics.intro_tac n
252 | GrafiteAst.NIntros (_loc,ns) -> NTactics.intros_tac ns
253 | GrafiteAst.NInversion (_loc, what, where) ->
254 NTactics.inversion_tac
255 ~what:(text,prefix_len,what)
256 ~where:(text,prefix_len,where)
257 | GrafiteAst.NLApply (_loc, t) -> NTactics.lapply_tac (text,prefix_len,t)
258 | GrafiteAst.NLetIn (_loc,where,what,name) ->
259 NTactics.letin_tac ~where:(text,prefix_len,where)
260 ~what:(text,prefix_len,what) name
261 | GrafiteAst.NMerge _ -> NTactics.merge_tac
262 | GrafiteAst.NPos (_,l) -> NTactics.pos_tac l
263 | GrafiteAst.NPosbyname (_,s) -> NTactics.case_tac s
264 | GrafiteAst.NReduce (_loc, reduction, where) ->
265 NTactics.reduce_tac ~reduction ~where:(text,prefix_len,where)
266 | GrafiteAst.NRewrite (_loc,dir,what,where) ->
267 NTactics.rewrite_tac ~dir ~what:(text,prefix_len,what)
268 ~where:(text,prefix_len,where)
269 | GrafiteAst.NSemicolon _ -> fun x -> x
270 | GrafiteAst.NShift _ -> NTactics.shift_tac
271 | GrafiteAst.NSkip _ -> NTactics.skip_tac
272 | GrafiteAst.NUnfocus _ -> NTactics.unfocus_tac
273 | GrafiteAst.NWildcard _ -> NTactics.wildcard_tac
274 | GrafiteAst.NTry (_,tac) -> NTactics.try_tac
275 (aux f (text, prefix_len, tac))
276 | GrafiteAst.NAssumption _ -> NTactics.assumption_tac
277 | GrafiteAst.NBlock (_,l) ->
278 NTactics.block_tac (List.map (fun x -> aux f (text,prefix_len,x)) l)
279 |GrafiteAst.NRepeat (_,tac) ->
280 NTactics.repeat_tac (f f (text, prefix_len, tac))
282 aux aux tac (* trick for non uniform recursion call *)
285 let subst_metasenv_and_fix_names status =
286 let u,h,metasenv, subst,o = status#obj in
288 NCicUntrusted.map_obj_kind ~skip_body:true
289 (NCicUntrusted.apply_subst subst []) o
291 status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv subst metasenv,subst,o)
295 let rec eval_ncommand opts status (text,prefix_len,cmd) =
297 | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
298 | GrafiteAst.NCoercion (loc, name, t, ty, source, target) ->
299 NCicCoercDeclaration.eval_ncoercion status name t ty source target
300 | GrafiteAst.NQed loc ->
301 if status#ng_mode <> `ProofMode then
302 raise (GrafiteTypes.Command_error "Not in proof mode")
304 let uri,height,menv,subst,obj_kind = status#obj in
307 (GrafiteTypes.Command_error"You can't Qed an incomplete theorem")
310 NCicUntrusted.map_obj_kind
311 (NCicUntrusted.apply_subst subst []) obj_kind in
312 let height = NCicTypeChecker.height_of_obj_kind uri [] obj_kind in
313 (* fix the height inside the object *)
314 let rec fix () = function
315 | NCic.Const (NReference.Ref (u,spec)) when NUri.eq u uri ->
316 NCic.Const (NReference.reference_of_spec u
318 | NReference.Def _ -> NReference.Def height
319 | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
320 | NReference.CoFix _ -> NReference.CoFix height
321 | NReference.Ind _ | NReference.Con _
322 | NReference.Decl as s -> s))
323 | t -> NCicUtils.map (fun _ () -> ()) () fix t
328 NCicUntrusted.map_obj_kind (fix ()) obj_kind
331 let obj = uri,height,[],[],obj_kind in
332 let old_status = status in
333 let status = NCicLibrary.add_obj status obj in
336 NCic.Constant (_,_,_,_,(_,`Example,_))
337 | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
342 let status = index_obj_for_auto status obj in
343 (try index_eq_for_auto status uri
350 with _ -> prerr_endline "got an exception"; status
352 (* prerr_endline (NCicPp.ppobj obj); *)
353 HLog.message ("New object: " ^ NUri.string_of_uri uri);
355 (*prerr_endline (NCicPp.ppobj obj);*)
356 let boxml = NCicElim.mk_elims obj in
357 let boxml = boxml @ NCicElim.mk_projections obj in
360 let timestamp,uris_rev =
362 (fun (status,uris_rev) (uri,_,_,_,_) as obj ->
363 let status = NCicLibrary.add_obj status obj in
365 ) (status,[]) objs in
366 let uris = uri::List.rev uris_rev in
368 let status = status#set_ng_mode `CommandMode in
369 let status = LexiconSync.add_aliases_for_objs status [uri] in
372 (fun (status,uris) boxml ->
375 eval_ncommand opts status
376 ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml))
378 if nstatus#ng_mode <> `CommandMode then
380 (*HLog.warn "error in generating projection/eliminator";*)
381 assert(status#ng_mode = `CommandMode);
387 | MultiPassDisambiguator.DisambiguationError _
388 | NCicTypeChecker.TypeCheckerFailure _ ->
389 (*HLog.warn "error in generating projection/eliminator";*)
391 ) (status,[] (* uris *)) boxml in
392 let _,_,_,_,nobj = obj in
393 let status = match nobj with
394 NCic.Inductive (is_ind,leftno,[it],_) ->
395 let _,ind_name,ty,cl = it in
397 (fun status outsort ->
398 let status = status#set_ng_mode `ProofMode in
401 NInversion.mk_inverter
402 (ind_name ^ "_inv_" ^
403 (snd (NCicElim.ast_of_sort outsort)))
404 is_ind it leftno outsort status status#baseuri in
405 let _,_,menv,_,_ = invobj in
407 [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
410 with _ -> (*HLog.warn "error in generating inversion principle"; *)
411 let status = status#set_ng_mode `CommandMode in status)
414 List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
419 _,_,_,_,NCic.Inductive
420 (true,leftno,[_,_,_,[_,_,_]],(_,`Record fields))
423 (fun (name,is_coercion,arity) ->
424 if is_coercion then Some(name,leftno,arity) else None) fields
428 (fun (status,uris) (name,cpos,arity) ->
430 let metasenv,subst,status,t =
431 GrafiteDisambiguate.disambiguate_nterm None status [] [] []
432 ("",0,NotationPt.Ident (name,None)) in
433 assert (metasenv = [] && subst = []);
435 NCicCoercDeclaration.
436 basic_eval_and_record_ncoercion_from_t_cpos_arity
437 status (name,t,cpos,arity)
439 let uris = nuris@uris in
441 with MultiPassDisambiguator.DisambiguationError _->
442 HLog.warn ("error in generating coercion: "^name);
444 (status,uris) coercions
449 NCicLibrary.time_travel old_status;
451 | GrafiteAst.NCopy (log,tgt,src_uri, map) ->
452 if status#ng_mode <> `CommandMode then
453 raise (GrafiteTypes.Command_error "Not in command mode")
455 let tgt_uri_ext, old_ok =
456 match NCicEnvironment.get_checked_obj src_uri with
457 | _,_,[],[], (NCic.Inductive _ as ok) -> ".ind", ok
458 | _,_,[],[], (NCic.Fixpoint _ as ok) -> ".con", ok
459 | _,_,[],[], (NCic.Constant _ as ok) -> ".con", ok
462 let tgt_uri = NUri.uri_of_string (status#baseuri^"/"^tgt^tgt_uri_ext) in
463 let map = (src_uri, tgt_uri) :: map in
465 let rec subst () = function
466 | NCic.Meta _ -> assert false
467 | NCic.Const (NReference.Ref (u,spec)) as t ->
469 (NReference.reference_of_spec (List.assoc u map)spec)
471 | t -> NCicUtils.map (fun _ _ -> ()) () subst t
473 NCicUntrusted.map_obj_kind ~skip_body:false (subst ()) old_ok
475 let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
476 let status = status#set_obj (tgt_uri,0,[],[],ok) in
477 (*prerr_endline (NCicPp.ppobj (tgt_uri,0,[],[],ok));*)
478 let status = status#set_stack ninitial_stack in
479 let status = subst_metasenv_and_fix_names status in
480 let status = status#set_ng_mode `ProofMode in
481 eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
482 | GrafiteAst.NObj (loc,obj) ->
483 if status#ng_mode <> `CommandMode then
484 raise (GrafiteTypes.Command_error "Not in command mode")
487 GrafiteDisambiguate.disambiguate_nobj status
488 ~baseuri:status#baseuri (text,prefix_len,obj) in
489 let uri,height,nmenv,nsubst,nobj = obj in
490 let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
491 let status = status#set_obj obj in
492 let status = status#set_stack ninitial_stack in
493 let status = subst_metasenv_and_fix_names status in
494 let status = status#set_ng_mode `ProofMode in
497 eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
499 | GrafiteAst.NDiscriminator (_,_) -> assert false (*(loc, indty) ->
500 if status#ng_mode <> `CommandMode then
501 raise (GrafiteTypes.Command_error "Not in command mode")
503 let status = status#set_ng_mode `ProofMode in
504 let metasenv,subst,status,indty =
505 GrafiteDisambiguate.disambiguate_nterm None status [] [] [] (text,prefix_len,indty) in
506 let indtyno, (_,_,tys,_,_) = match indty with
507 NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
508 indtyno, NCicEnvironment.get_checked_indtys r
509 | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
510 let it = List.nth tys indtyno in
511 let status,obj = NDestructTac.mk_discriminator it status in
512 let _,_,menv,_,_ = obj in
514 [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
515 | _ -> prerr_endline ("Discriminator: non empty metasenv");
517 | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
518 if status#ng_mode <> `CommandMode then
519 raise (GrafiteTypes.Command_error "Not in command mode")
521 let metasenv,subst,status,sort = match sort with
522 | None -> [],[],status,NCic.Sort NCic.Prop
523 | Some s -> GrafiteDisambiguate.disambiguate_nterm None status [] [] []
526 assert (metasenv = []);
527 let sort = NCicReduction.whd ~subst [] sort in
528 let sort = match sort with
530 | _ -> raise (Invalid_argument (Printf.sprintf "ninverter: found target %s, which is not a sort"
531 (NCicPp.ppterm ~metasenv ~subst ~context:[] sort)))
533 let status = status#set_ng_mode `ProofMode in
534 let metasenv,subst,status,indty =
535 GrafiteDisambiguate.disambiguate_nterm None status [] [] subst (text,prefix_len,indty) in
536 let indtyno,(_,leftno,tys,_,_) = match indty with
537 NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
538 indtyno, NCicEnvironment.get_checked_indtys r
539 | _ -> prerr_endline ("engine: indty =" ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] indty) ; assert false in
540 let it = List.nth tys indtyno in
541 let status,obj = NInversion.mk_inverter name true it leftno ?selection sort
542 status status#baseuri in
543 let _,_,menv,_,_ = obj in
546 eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
548 | GrafiteAst.NUnivConstraint (loc,u1,u2) ->
549 eval_add_constraint status [`Type,u1] [`Type,u2]
552 let eval_comment opts status (text,prefix_len,c) = status, []
554 let rec eval_command opts status (text,prefix_len,cmd) =
557 | GrafiteAst.Include (loc, baseuri) ->
559 GrafiteTypes.Serializer.require ~baseuri:(NUri.uri_of_string baseuri)
561 let status = status#set_dump (obj::status#dump) in
563 | GrafiteAst.Print (_,_) -> status,[]
564 | GrafiteAst.Set (loc, name, value) -> status, []
568 and eval_executable opts status (text,prefix_len,ex) =
570 | GrafiteAst.NTactic (_(*loc*), tacl) ->
571 if status#ng_mode <> `ProofMode then
572 raise (GrafiteTypes.Command_error "Not in proof mode")
577 let status = eval_ng_tac (text,prefix_len,tac) status in
578 subst_metasenv_and_fix_names status)
582 | GrafiteAst.Command (_, cmd) ->
583 eval_command opts status (text,prefix_len,cmd)
584 | GrafiteAst.NCommand (_, cmd) ->
585 eval_ncommand opts status (text,prefix_len,cmd)
586 | GrafiteAst.NMacro (loc, macro) ->
587 raise (NMacro (loc,macro))
589 and eval_ast ?(do_heavy_checks=false) status (text,prefix_len,st) =
590 let opts = { do_heavy_checks = do_heavy_checks ; } in
592 | GrafiteAst.Executable (_,ex) ->
593 eval_executable opts status (text,prefix_len,ex)
594 | GrafiteAst.Comment (_,c) ->
595 eval_comment opts status (text,prefix_len,c)