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 NCicLibrary.Serializer.register#run "unification_hints"
50 object(_ : 'a NCicLibrary.register_type)
51 method run = basic_eval_unification_hint
55 let eval_unification_hint status t n =
56 let metasenv,subst,status,t =
57 GrafiteDisambiguate.disambiguate_nterm None status [] [] [] ("",0,t) in
59 let t = NCicUntrusted.apply_subst subst [] t in
60 let status = basic_eval_unification_hint (t,n) status in
61 let dump = inject_unification_hint (t,n)::status#dump in
62 let status = status#set_dump dump in
66 let basic_index_obj l status =
70 List.fold_left (fun t k ->
71 NDiscriminationTree.DiscriminationTree.index t k v)
76 let record_index_obj =
78 ~refresh_uri_in_universe
83 (fun ks,v -> List.map refresh_uri_in_term ks, refresh_uri_in_term v)
86 NCicLibrary.Serializer.register#run "index_obj"
87 object(_ : 'a NCicLibrary.register_type)
92 let compute_keys status uri height kind =
94 let orig_ty = NTacStatus.mk_cic_term [] ty in
95 let status,keys = NnAuto.keys_of_type status orig_ty in
99 snd (NTacStatus.term_of_cic_term status t (NTacStatus.ctx_of t)))
102 keys,NCic.Const(NReference.reference_of_spec uri spec)
106 | NCic.Fixpoint (ind,ifl,_) ->
108 (fun (_,_,rno,ty,_) i ->
109 if ind then mk_item ty (NReference.Fix (i,rno,height))
110 else mk_item ty (NReference.CoFix height)) ifl
111 | NCic.Inductive (b,lno,itl,_) ->
113 (fun (_,_,ty,_) i -> mk_item ty (NReference.Ind (b,i,lno))) itl
115 List.map (fun ((_,_,ty),i,j) -> mk_item ty (NReference.Con (i,j+1,lno)))
116 (List.flatten (HExtlib.list_mapi
117 (fun (_,_,_,cl) i -> HExtlib.list_mapi (fun x j-> x,i,j) cl)
119 | NCic.Constant (_,_,Some _, ty, _) ->
120 [ mk_item ty (NReference.Def height) ]
121 | NCic.Constant (_,_,None, ty, _) ->
122 [ mk_item ty NReference.Decl ]
126 let keys = List.filter
129 | (NCic.Appl (NCic.Meta _::_)) -> false
135 HLog.debug ("Indexing:" ^
136 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
137 HLog.debug ("With keys:" ^ String.concat "\n" (List.map (fun t ->
138 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t) keys));
143 HLog.debug ("Not indexing:" ^
144 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
150 let index_obj_for_auto status (uri, height, _, _, kind) =
151 (*prerr_endline (string_of_int height);*)
152 let data = compute_keys status uri height kind in
153 let status = basic_index_obj data status in
154 let dump = record_index_obj data :: status#dump in
158 let index_eq uri status =
159 let eq_status = status#eq_cache in
160 let eq_status1 = NCicParamod.index_obj eq_status uri in
161 status#set_eq_cache eq_status1
164 let record_index_eq =
165 let basic_index_eq uri
166 ~refresh_uri_in_universe
168 = index_eq (NCicLibrary.refresh_uri uri)
170 NCicLibrary.Serializer.register#run "index_eq"
171 object(_ : 'a NCicLibrary.register_type)
172 method run = basic_index_eq
176 let index_eq_for_auto status uri =
177 if NnAuto.is_a_fact_obj status uri then
178 let newstatus = index_eq uri status in
179 if newstatus#eq_cache == status#eq_cache then status
181 ((*prerr_endline ("recording " ^ (NUri.string_of_uri uri));*)
182 let dump = record_index_eq uri :: newstatus#dump
183 in newstatus#set_dump dump)
185 ((*prerr_endline "Not a fact";*)
189 let basic_eval_add_constraint (u1,u2) status =
190 NCicLibrary.add_constraint status u1 u2
193 let inject_constraint =
194 let basic_eval_add_constraint (u1,u2)
195 ~refresh_uri_in_universe
198 let u1 = refresh_uri_in_universe u1 in
199 let u2 = refresh_uri_in_universe u2 in
200 basic_eval_add_constraint (u1,u2)
202 NCicLibrary.Serializer.register#run "constraints"
203 object(_:'a NCicLibrary.register_type)
204 method run = basic_eval_add_constraint
208 let eval_add_constraint status u1 u2 =
209 let status = basic_eval_add_constraint (u1,u2) status in
210 let dump = inject_constraint (u1,u2)::status#dump in
211 let status = status#set_dump dump in
215 let eval_ng_tac tac =
216 let rec aux f (text, prefix_len, tac) =
218 | GrafiteAst.NApply (_loc, t) -> NTactics.apply_tac (text,prefix_len,t)
219 | GrafiteAst.NSmartApply (_loc, t) ->
220 NnAuto.smart_apply_tac (text,prefix_len,t)
221 | GrafiteAst.NAssert (_loc, seqs) ->
224 (function (hyps,concl) ->
227 (id,`Decl t) -> id,`Decl (text,prefix_len,t)
228 |(id,`Def (b,t))->id,`Def((text,prefix_len,b),(text,prefix_len,t))
230 (text,prefix_len,concl))
232 | GrafiteAst.NAuto (_loc, (None,a)) ->
233 NnAuto.auto_tac ~params:(None,a) ?trace_ref:None
234 | GrafiteAst.NAuto (_loc, (Some l,a)) ->
236 ~params:(Some List.map (fun x -> "",0,x) l,a) ?trace_ref:None
237 | GrafiteAst.NBranch _ -> NTactics.branch_tac ~force:false
238 | GrafiteAst.NCases (_loc, what, where) ->
240 ~what:(text,prefix_len,what)
241 ~where:(text,prefix_len,where)
242 | GrafiteAst.NCase1 (_loc,n) -> NTactics.case1_tac n
243 | GrafiteAst.NChange (_loc, pat, ww) ->
245 ~where:(text,prefix_len,pat) ~with_what:(text,prefix_len,ww)
246 | GrafiteAst.NConstructor (_loc,num,args) ->
247 NTactics.constructor_tac
248 ?num ~args:(List.map (fun x -> text,prefix_len,x) args)
249 | GrafiteAst.NCut (_loc, t) -> NTactics.cut_tac (text,prefix_len,t)
250 (*| GrafiteAst.NDiscriminate (_,what) -> NDestructTac.discriminate_tac ~what:(text,prefix_len,what)
251 | GrafiteAst.NSubst (_,what) -> NDestructTac.subst_tac ~what:(text,prefix_len,what)*)
252 | GrafiteAst.NDestruct (_,dom,skip) -> NDestructTac.destruct_tac dom skip
253 | GrafiteAst.NDot _ -> NTactics.dot_tac
254 | GrafiteAst.NElim (_loc, what, where) ->
256 ~what:(text,prefix_len,what)
257 ~where:(text,prefix_len,where)
258 | GrafiteAst.NFocus (_,l) -> NTactics.focus_tac l
259 | GrafiteAst.NGeneralize (_loc, where) ->
260 NTactics.generalize_tac ~where:(text,prefix_len,where)
261 | GrafiteAst.NId _ -> (fun x -> x)
262 | GrafiteAst.NIntro (_loc,n) -> NTactics.intro_tac n
263 | GrafiteAst.NIntros (_loc,ns) -> NTactics.intros_tac ns
264 | GrafiteAst.NInversion (_loc, what, where) ->
265 NTactics.inversion_tac
266 ~what:(text,prefix_len,what)
267 ~where:(text,prefix_len,where)
268 | GrafiteAst.NLApply (_loc, t) -> NTactics.lapply_tac (text,prefix_len,t)
269 | GrafiteAst.NLetIn (_loc,where,what,name) ->
270 NTactics.letin_tac ~where:(text,prefix_len,where)
271 ~what:(text,prefix_len,what) name
272 | GrafiteAst.NMerge _ -> NTactics.merge_tac
273 | GrafiteAst.NPos (_,l) -> NTactics.pos_tac l
274 | GrafiteAst.NPosbyname (_,s) -> NTactics.case_tac s
275 | GrafiteAst.NReduce (_loc, reduction, where) ->
276 NTactics.reduce_tac ~reduction ~where:(text,prefix_len,where)
277 | GrafiteAst.NRewrite (_loc,dir,what,where) ->
278 NTactics.rewrite_tac ~dir ~what:(text,prefix_len,what)
279 ~where:(text,prefix_len,where)
280 | GrafiteAst.NSemicolon _ -> fun x -> x
281 | GrafiteAst.NShift _ -> NTactics.shift_tac
282 | GrafiteAst.NSkip _ -> NTactics.skip_tac
283 | GrafiteAst.NUnfocus _ -> NTactics.unfocus_tac
284 | GrafiteAst.NWildcard _ -> NTactics.wildcard_tac
285 | GrafiteAst.NTry (_,tac) -> NTactics.try_tac
286 (aux f (text, prefix_len, tac))
287 | GrafiteAst.NAssumption _ -> NTactics.assumption_tac
288 | GrafiteAst.NBlock (_,l) ->
289 NTactics.block_tac (List.map (fun x -> aux f (text,prefix_len,x)) l)
290 |GrafiteAst.NRepeat (_,tac) ->
291 NTactics.repeat_tac (f f (text, prefix_len, tac))
293 aux aux tac (* trick for non uniform recursion call *)
296 let subst_metasenv_and_fix_names status =
297 let u,h,metasenv, subst,o = status#obj in
299 NCicUntrusted.map_obj_kind ~skip_body:true
300 (NCicUntrusted.apply_subst subst []) o
302 status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv subst metasenv,subst,o)
306 let rec eval_ncommand opts status (text,prefix_len,cmd) =
308 | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
309 | GrafiteAst.NCoercion (loc, name, t, ty, source, target) ->
310 NCicCoercDeclaration.eval_ncoercion status name t ty source target
311 | GrafiteAst.NQed loc ->
312 if status#ng_mode <> `ProofMode then
313 raise (GrafiteTypes.Command_error "Not in proof mode")
315 let uri,height,menv,subst,obj_kind = status#obj in
318 (GrafiteTypes.Command_error"You can't Qed an incomplete theorem")
321 NCicUntrusted.map_obj_kind
322 (NCicUntrusted.apply_subst subst []) obj_kind in
323 let height = NCicTypeChecker.height_of_obj_kind uri [] obj_kind in
324 (* fix the height inside the object *)
325 let rec fix () = function
326 | NCic.Const (NReference.Ref (u,spec)) when NUri.eq u uri ->
327 NCic.Const (NReference.reference_of_spec u
329 | NReference.Def _ -> NReference.Def height
330 | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
331 | NReference.CoFix _ -> NReference.CoFix height
332 | NReference.Ind _ | NReference.Con _
333 | NReference.Decl as s -> s))
334 | t -> NCicUtils.map (fun _ () -> ()) () fix t
339 NCicUntrusted.map_obj_kind (fix ()) obj_kind
342 let obj = uri,height,[],[],obj_kind in
343 prerr_endline ("pp new obj \n"^NCicPp.ppobj obj);
344 let old_status = status in
345 let status = NCicLibrary.add_obj status obj in
348 NCic.Constant (_,_,_,_,(_,`Example,_))
349 | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
354 let status = index_obj_for_auto status obj in
355 (try index_eq_for_auto status uri
362 with _ -> prerr_endline "got an exception"; status
364 (* prerr_endline (NCicPp.ppobj obj); *)
365 HLog.message ("New object: " ^ NUri.string_of_uri uri);
367 (*prerr_endline (NCicPp.ppobj obj);*)
368 let boxml = NCicElim.mk_elims obj in
369 let boxml = boxml @ NCicElim.mk_projections obj in
372 let timestamp,uris_rev =
374 (fun (status,uris_rev) (uri,_,_,_,_) as obj ->
375 let status = NCicLibrary.add_obj status obj in
377 ) (status,[]) objs in
378 let uris = uri::List.rev uris_rev in
380 let status = status#set_ng_mode `CommandMode in
381 let status = LexiconSync.add_aliases_for_objs status [uri] in
384 (fun (status,uris) boxml ->
387 eval_ncommand opts status
388 ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml))
390 if nstatus#ng_mode <> `CommandMode then
392 (*HLog.warn "error in generating projection/eliminator";*)
398 | MultiPassDisambiguator.DisambiguationError _
399 | NCicTypeChecker.TypeCheckerFailure _ ->
400 (*HLog.warn "error in generating projection/eliminator";*)
402 ) (status,[] (* uris *)) boxml in
403 let _,_,_,_,nobj = obj in
404 let status = match nobj with
405 NCic.Inductive (is_ind,leftno,[it],_) ->
406 let _,ind_name,ty,cl = it in
408 (fun status outsort ->
409 let status = status#set_ng_mode `ProofMode in
412 NInversion.mk_inverter
413 (ind_name ^ "_inv_" ^
414 (snd (NCicElim.ast_of_sort outsort)))
415 is_ind it leftno outsort status status#baseuri in
416 let _,_,menv,_,_ = invobj in
418 [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
421 with _ -> (*HLog.warn "error in generating inversion principle"; *)
422 let status = status#set_ng_mode `CommandMode in status)
425 List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
430 _,_,_,_,NCic.Inductive
431 (true,leftno,[_,_,_,[_,_,_]],(_,`Record fields))
434 (fun (name,is_coercion,arity) ->
435 if is_coercion then Some(name,leftno,arity) else None) fields
439 (fun (status,uris) (name,cpos,arity) ->
441 let metasenv,subst,status,t =
442 GrafiteDisambiguate.disambiguate_nterm None status [] [] []
443 ("",0,NotationPt.Ident (name,None)) in
444 assert (metasenv = [] && subst = []);
446 NCicCoercDeclaration.
447 basic_eval_and_record_ncoercion_from_t_cpos_arity
448 status (name,t,cpos,arity)
450 let uris = nuris@uris in
452 with MultiPassDisambiguator.DisambiguationError _->
453 HLog.warn ("error in generating coercion: "^name);
455 (status,uris) coercions
460 NCicLibrary.time_travel old_status;
462 | GrafiteAst.NCopy (log,tgt,src_uri, map) ->
463 if status#ng_mode <> `CommandMode then
464 raise (GrafiteTypes.Command_error "Not in command mode")
466 let tgt_uri_ext, old_ok =
467 match NCicEnvironment.get_checked_obj src_uri with
468 | _,_,[],[], (NCic.Inductive _ as ok) -> ".ind", ok
469 | _,_,[],[], (NCic.Fixpoint _ as ok) -> ".con", ok
470 | _,_,[],[], (NCic.Constant _ as ok) -> ".con", ok
473 let tgt_uri = NUri.uri_of_string (status#baseuri^"/"^tgt^tgt_uri_ext) in
474 let map = (src_uri, tgt_uri) :: map in
476 let rec subst () = function
477 | NCic.Meta _ -> assert false
478 | NCic.Const (NReference.Ref (u,spec)) as t ->
480 (NReference.reference_of_spec (List.assoc u map)spec)
482 | t -> NCicUtils.map (fun _ _ -> ()) () subst t
484 NCicUntrusted.map_obj_kind ~skip_body:false (subst ()) old_ok
486 let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
487 let status = status#set_obj (tgt_uri,0,[],[],ok) in
488 (*prerr_endline (NCicPp.ppobj (tgt_uri,0,[],[],ok));*)
489 let status = status#set_stack ninitial_stack in
490 let status = subst_metasenv_and_fix_names status in
491 let status = status#set_ng_mode `ProofMode in
492 eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
493 | GrafiteAst.NObj (loc,obj) ->
494 if status#ng_mode <> `CommandMode then
495 raise (GrafiteTypes.Command_error "Not in command mode")
498 GrafiteDisambiguate.disambiguate_nobj status
499 ~baseuri:status#baseuri (text,prefix_len,obj) in
500 let uri,height,nmenv,nsubst,nobj = obj in
501 let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
502 let status = status#set_obj obj in
503 let status = status#set_stack ninitial_stack in
504 let status = subst_metasenv_and_fix_names status in
505 let status = status#set_ng_mode `ProofMode in
508 eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
510 | GrafiteAst.NDiscriminator (_,_) -> assert false (*(loc, indty) ->
511 if status#ng_mode <> `CommandMode then
512 raise (GrafiteTypes.Command_error "Not in command mode")
514 let status = status#set_ng_mode `ProofMode in
515 let metasenv,subst,status,indty =
516 GrafiteDisambiguate.disambiguate_nterm None status [] [] [] (text,prefix_len,indty) in
517 let indtyno, (_,_,tys,_,_) = match indty with
518 NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
519 indtyno, NCicEnvironment.get_checked_indtys r
520 | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
521 let it = List.nth tys indtyno in
522 let status,obj = NDestructTac.mk_discriminator it status in
523 let _,_,menv,_,_ = obj in
525 [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
526 | _ -> prerr_endline ("Discriminator: non empty metasenv");
528 | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
529 if status#ng_mode <> `CommandMode then
530 raise (GrafiteTypes.Command_error "Not in command mode")
532 let metasenv,subst,status,sort = match sort with
533 | None -> [],[],status,NCic.Sort NCic.Prop
534 | Some s -> GrafiteDisambiguate.disambiguate_nterm None status [] [] []
537 assert (metasenv = []);
538 let sort = NCicReduction.whd ~subst [] sort in
539 let sort = match sort with
541 | _ -> raise (Invalid_argument (Printf.sprintf "ninverter: found target %s, which is not a sort"
542 (NCicPp.ppterm ~metasenv ~subst ~context:[] sort)))
544 let status = status#set_ng_mode `ProofMode in
545 let metasenv,subst,status,indty =
546 GrafiteDisambiguate.disambiguate_nterm None status [] [] subst (text,prefix_len,indty) in
547 let indtyno,(_,leftno,tys,_,_) = match indty with
548 NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
549 indtyno, NCicEnvironment.get_checked_indtys r
550 | _ -> prerr_endline ("engine: indty =" ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] indty) ; assert false in
551 let it = List.nth tys indtyno in
552 let status,obj = NInversion.mk_inverter name true it leftno ?selection sort
553 status status#baseuri in
554 let _,_,menv,_,_ = obj in
557 eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
559 | GrafiteAst.NUnivConstraint (loc,u1,u2) ->
560 eval_add_constraint status [`Type,u1] [`Type,u2]
563 let eval_comment ~disambiguate_command opts status (text,prefix_len,c) =
566 let rec eval_command ~disambiguate_command opts status (text,prefix_len,cmd) =
567 let status,cmd = disambiguate_command status (text,prefix_len,cmd) in
570 | GrafiteAst.Include (loc, baseuri) ->
571 NCicLibrary.Serializer.require ~baseuri:(NUri.uri_of_string baseuri)
573 | GrafiteAst.Print (_,_) -> status,[]
574 | GrafiteAst.Set (loc, name, value) -> status, []
578 and eval_executable ~disambiguate_command opts status (text,prefix_len,ex) =
580 | GrafiteAst.NTactic (_(*loc*), tacl) ->
581 if status#ng_mode <> `ProofMode then
582 raise (GrafiteTypes.Command_error "Not in proof mode")
587 let status = eval_ng_tac (text,prefix_len,tac) status in
588 subst_metasenv_and_fix_names status)
592 | GrafiteAst.Command (_, cmd) ->
593 eval_command ~disambiguate_command opts status (text,prefix_len,cmd)
594 | GrafiteAst.NCommand (_, cmd) ->
595 eval_ncommand opts status (text,prefix_len,cmd)
596 | GrafiteAst.NMacro (loc, macro) ->
597 raise (NMacro (loc,macro))
599 and eval_ast ~disambiguate_command ?(do_heavy_checks=false) status
602 let opts = { do_heavy_checks = do_heavy_checks ; } in
604 | GrafiteAst.Executable (_,ex) ->
605 eval_executable ~disambiguate_command opts status (text,prefix_len,ex)
606 | GrafiteAst.Comment (_,c) ->
607 eval_comment ~disambiguate_command opts status (text,prefix_len,c)