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/
28 (* mo file name, ma file name *)
29 exception NMacro of GrafiteAst.loc * GrafiteAst.nmacro
31 type 'a disambiguator_input = string * int * 'a
34 do_heavy_checks: bool ;
37 let basic_eval_unification_hint (t,n) status =
38 NCicUnifHint.add_user_provided_hint status t n
41 let inject_unification_hint =
42 let basic_eval_unification_hint (t,n)
43 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
46 if not alias_only then
47 let t = refresh_uri_in_term (status :> NCic.status) t in
48 basic_eval_unification_hint (t,n) status
52 GrafiteTypes.Serializer.register#run "unification_hints"
53 basic_eval_unification_hint
56 let eval_unification_hint status t n =
57 let metasenv,subst,status,t =
58 GrafiteDisambiguate.disambiguate_nterm status `XTSort [] [] [] ("",0,t) in
60 let t = NCicUntrusted.apply_subst status subst [] t in
61 let status = basic_eval_unification_hint (t,n) status in
62 NCicLibrary.dump_obj status (inject_unification_hint (t,n))
65 let basic_index_obj l (status:#NCic.status) =
69 List.fold_left (fun t k ->
70 NDiscriminationTree.DiscriminationTree.index t k v)
75 let basic_eval_interpretation ~alias_only (dsc, (symbol, args), cic_appl_pattern) status =
77 if not alias_only then
78 Interpretations.add_interpretation status dsc (symbol, args) cic_appl_pattern
82 let mode = GrafiteAst.WithPreferences (*assert false*) in (* MATITA 1.0 VEDI SOTTO *)
84 [DisambiguateTypes.Symbol (symbol, 0), GrafiteAst.Symbol_alias (symbol,0,dsc)]
86 GrafiteDisambiguate.set_proof_aliases status ~implicit_aliases:false mode diff
89 let inject_interpretation =
90 let basic_eval_interpretation (dsc, (symbol, args), cic_appl_pattern)
91 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
96 NotationPt.NRefPattern (NReference.Ref (uri,spec)) ->
97 NotationPt.NRefPattern
98 (NReference.reference_of_spec (NCicLibrary.refresh_uri uri) spec)
99 | NotationPt.VarPattern _
100 | NotationPt.ImplicitPattern as x -> x
101 | NotationPt.ApplPattern l -> NotationPt.ApplPattern (List.map refresh l)
103 let cic_appl_pattern = refresh cic_appl_pattern in
104 basic_eval_interpretation ~alias_only (dsc, (symbol, args), cic_appl_pattern)
106 GrafiteTypes.Serializer.register#run "interpretation"
107 basic_eval_interpretation
110 let eval_interpretation status data=
111 let status = basic_eval_interpretation ~alias_only:false data status in
112 NCicLibrary.dump_obj status (inject_interpretation data)
115 let basic_eval_alias (mode,diff) status =
116 GrafiteDisambiguate.set_proof_aliases status ~implicit_aliases:false mode diff
120 let basic_eval_alias (mode,diff) ~refresh_uri_in_universe ~refresh_uri_in_term
121 ~refresh_uri_in_reference ~alias_only =
122 basic_eval_alias (mode,diff)
124 GrafiteTypes.Serializer.register#run "alias" basic_eval_alias
127 let eval_alias status data=
128 let status = basic_eval_alias data status in
129 NCicLibrary.dump_obj status (inject_alias data)
132 let basic_eval_input_notation (l1,l2) status =
133 GrafiteParser.extend status l1
135 NotationPt.AttributedTerm
136 (`Loc loc,TermContentPres.instantiate_level2 status env l2))
139 let inject_input_notation =
140 let basic_eval_input_notation (l1,l2)
141 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
144 if not alias_only then
146 CicNotationParser.refresh_uri_in_checked_l1_pattern
147 ~refresh_uri_in_term:(refresh_uri_in_term (status:>NCic.status))
148 ~refresh_uri_in_reference l1 in
149 let l2 = NotationUtil.refresh_uri_in_term
150 ~refresh_uri_in_term:(refresh_uri_in_term (status:>NCic.status))
151 ~refresh_uri_in_reference l2
153 basic_eval_input_notation (l1,l2) status
157 GrafiteTypes.Serializer.register#run "input_notation"
158 basic_eval_input_notation
161 let eval_input_notation status data=
162 let status = basic_eval_input_notation data status in
163 NCicLibrary.dump_obj status (inject_input_notation data)
166 let basic_eval_output_notation (l1,l2) status =
167 TermContentPres.add_pretty_printer status l2 l1
170 let inject_output_notation =
171 let basic_eval_output_notation (l1,l2)
172 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
175 if not alias_only then
177 CicNotationParser.refresh_uri_in_checked_l1_pattern
178 ~refresh_uri_in_term:(refresh_uri_in_term (status:>NCic.status))
179 ~refresh_uri_in_reference l1 in
180 let l2 = NotationUtil.refresh_uri_in_term
181 ~refresh_uri_in_term:(refresh_uri_in_term (status:>NCic.status))
182 ~refresh_uri_in_reference l2
184 basic_eval_output_notation (l1,l2) status
188 GrafiteTypes.Serializer.register#run "output_notation"
189 basic_eval_output_notation
192 let eval_output_notation status data=
193 let status = basic_eval_output_notation data status in
194 NCicLibrary.dump_obj status (inject_output_notation data)
197 let record_index_obj =
198 let aux l ~refresh_uri_in_universe
199 ~refresh_uri_in_term ~refresh_uri_in_reference ~alias_only status
201 let refresh_uri_in_term = refresh_uri_in_term (status:>NCic.status) in
202 if not alias_only then
205 (fun ks,v -> List.map refresh_uri_in_term ks, refresh_uri_in_term v)
210 GrafiteTypes.Serializer.register#run "index_obj" aux
213 let compute_keys status uri height kind =
214 let mk_item ty spec =
215 let orig_ty = NTacStatus.mk_cic_term [] ty in
216 let status,keys = NnAuto.keys_of_type status orig_ty in
220 snd (NTacStatus.term_of_cic_term status t (NTacStatus.ctx_of t)))
223 keys,NCic.Const(NReference.reference_of_spec uri spec)
227 | NCic.Fixpoint (ind,ifl,_) ->
229 (fun (_,_,rno,ty,_) i ->
230 if ind then mk_item ty (NReference.Fix (i,rno,height))
231 else mk_item ty (NReference.CoFix height)) ifl
232 | NCic.Inductive (b,lno,itl,_) ->
234 (fun (_,_,ty,_) i -> mk_item ty (NReference.Ind (b,i,lno))) itl
236 List.map (fun ((_,_,ty),i,j) -> mk_item ty (NReference.Con (i,j+1,lno)))
237 (List.flatten (HExtlib.list_mapi
238 (fun (_,_,_,cl) i -> HExtlib.list_mapi (fun x j-> x,i,j) cl)
240 | NCic.Constant (_,_,Some _, ty, _) ->
241 [ mk_item ty (NReference.Def height) ]
242 | NCic.Constant (_,_,None, ty, _) ->
243 [ mk_item ty NReference.Decl ]
247 let keys = List.filter
250 | (NCic.Appl (NCic.Meta _::_)) -> false
256 HLog.debug ("Indexing:" ^
257 status#ppterm ~metasenv:[] ~subst:[] ~context:[] t);
258 HLog.debug ("With keys:" ^ String.concat "\n" (List.map (fun t ->
259 status#ppterm ~metasenv:[] ~subst:[] ~context:[] t) keys));
264 HLog.debug ("Not indexing:" ^
265 status#ppterm ~metasenv:[] ~subst:[] ~context:[] t);
271 let index_obj_for_auto status (uri, height, _, _, kind) =
272 (*prerr_endline (string_of_int height);*)
273 let data = compute_keys status uri height kind in
274 let status = basic_index_obj data status in
275 NCicLibrary.dump_obj status (record_index_obj data)
278 let basic_extract_obj obj status =
280 ignore (Helm_registry.get "extract_haskell");
281 let status,(msg,infos) = NCicExtraction.haskell_of_obj status obj in
285 Helm_registry.Key_not_found _ -> status,NCicExtraction.empty_info
286 | exn -> prerr_endline ("EXTRACTION FAILURE: " ^ Printexc.to_string exn); assert false
289 let basic_extract_ocaml_obj obj status =
291 let status = OcamlExtraction.print_ocaml_of_obj status obj in
292 let infos,status = status#get_info in
295 exn -> prerr_endline ("EXTRACTION FAILURE: " ^ Printexc.to_string exn); assert false
298 let inject_extract_obj =
299 let basic_extract_obj info
300 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
303 let info= NCicExtraction.refresh_uri_in_info ~refresh_uri_in_reference info in
304 status#set_extraction_db
305 (NCicExtraction.register_infos status#extraction_db info)
307 GrafiteTypes.Serializer.register#run "extraction" basic_extract_obj
310 let inject_extract_ocaml_obj =
311 let basic_extract_ocaml_obj info
312 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
315 let info= OcamlExtractionTable.refresh_uri_in_info ~refresh_uri_in_reference ~refresh_uri:NCicLibrary.refresh_uri info in
316 status#set_ocaml_extraction_db
317 (OcamlExtractionTable.register_infos status#ocaml_extraction_db info)
319 GrafiteTypes.Serializer.register#run "ocaml_extraction" basic_extract_ocaml_obj
322 let eval_extract_obj status obj =
323 let status,infos = basic_extract_obj obj status in
324 NCicLibrary.dump_obj status (inject_extract_obj infos)
327 let eval_extract_ocaml_obj status obj =
328 let status,infos = basic_extract_ocaml_obj obj status in
329 NCicLibrary.dump_obj status (inject_extract_ocaml_obj infos)
341 let index_eq print uri (status:#NCic.status) =
342 let eq_status = status#eq_cache in
343 let eq_status,clause = NCicParamod.index_obj status eq_status uri in
345 ((*let module NCicBlob =
347 include NCicBlob.NCicBlob(EmptyC)
348 let pp t = status#ppterm ~metasenv:[] ~subst:[] ~context:[] t
350 let module Pp = Pp.Pp(NCicBlob) in*)
352 | Some (*clause*) (_,Terms.Equation (_,_,_,o),_,_) ->
353 HLog.debug ("Indexed with orientation: " ^ Pp.string_of_comparison o);
354 (*HLog.debug ("Indexed as:" ^ Pp.pp_unit_clause clause)*)
355 | _ -> HLog.debug "Not indexed (i.e. pruned)")) ;
356 status#set_eq_cache eq_status
359 let record_index_eq =
360 let basic_index_eq uri
361 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
363 = if not alias_only then index_eq false (NCicLibrary.refresh_uri uri) status
367 GrafiteTypes.Serializer.register#run "index_eq" basic_index_eq
370 let index_eq_for_auto status uri =
371 if NnAuto.is_a_fact_obj status uri then
372 let newstatus = index_eq true uri status in
373 if newstatus#eq_cache == status#eq_cache then status
375 ((*prerr_endline ("recording " ^ (NUri.string_of_uri uri));*)
376 NCicLibrary.dump_obj newstatus (record_index_eq uri))
378 ((*prerr_endline "Not a fact";*)
382 let inject_constraint =
383 let basic_eval_add_constraint (acyclic,u1,u2)
384 ~refresh_uri_in_universe ~refresh_uri_in_term ~refresh_uri_in_reference
387 if not alias_only then
388 let u1 = refresh_uri_in_universe u1 in
389 let u2 = refresh_uri_in_universe u2 in
390 (* NCicLibrary.add_constraint adds the constraint to the NCicEnvironment
391 * and also to the storage (for undo/redo). During inclusion of compiled
392 * files the storage must not be touched. *)
393 NCicEnvironment.add_lt_constraint acyclic u1 u2;
398 GrafiteTypes.Serializer.register#run "constraints" basic_eval_add_constraint
401 let eval_add_constraint status acyclic u1 u2 =
402 let status = NCicLibrary.add_constraint status acyclic u1 u2 in
403 NCicLibrary.dump_obj status (inject_constraint (acyclic,u1,u2))
406 let eval_ng_tac tac =
407 let just_to_tacstatus_just just text prefix_len =
409 | `Term t -> `Term (text,prefix_len,t)
410 | `Auto (l,params) ->
413 | None -> `Auto (None,params)
414 | Some l -> `Auto (Some (List.map (fun t -> (text,prefix_len,t)) l),params)
417 let rec aux f (text, prefix_len, tac) =
419 | GrafiteAst.NApply (_loc, t) -> NTactics.apply_tac (text,prefix_len,t)
420 | GrafiteAst.NSmartApply (_loc, t) ->
421 NnAuto.smart_apply_tac (text,prefix_len,t)
422 | GrafiteAst.NAssert (_loc, seqs) ->
425 (function (hyps,concl) ->
428 (id,`Decl t) -> id,`Decl (text,prefix_len,t)
429 |(id,`Def (b,t))->id,`Def((text,prefix_len,b),(text,prefix_len,t))
431 (text,prefix_len,concl))
433 | GrafiteAst.NAuto (_loc, (None,a)) ->
434 NnAuto.auto_tac ~params:(None,a) ?trace_ref:None
435 | GrafiteAst.NAuto (_loc, (Some l,a)) ->
437 ~params:(Some List.map (fun x -> "",0,x) l,a) ?trace_ref:None
438 | GrafiteAst.NBranch _ -> NTactics.branch_tac ~force:false
439 | GrafiteAst.NCases (_loc, what, where) ->
441 ~what:(text,prefix_len,what)
442 ~where:(text,prefix_len,where)
443 | GrafiteAst.NCase1 (_loc,n) -> NTactics.case1_tac n
444 | GrafiteAst.NChange (_loc, pat, ww) ->
446 ~where:(text,prefix_len,pat) ~with_what:(text,prefix_len,ww)
447 | GrafiteAst.NConstructor (_loc,num,args) ->
448 NTactics.constructor_tac
449 ?num ~args:(List.map (fun x -> text,prefix_len,x) args)
450 | GrafiteAst.NCut (_loc, t) -> NTactics.cut_tac (text,prefix_len,t)
451 (*| GrafiteAst.NDiscriminate (_,what) -> NDestructTac.discriminate_tac ~what:(text,prefix_len,what)
452 | GrafiteAst.NSubst (_,what) -> NDestructTac.subst_tac ~what:(text,prefix_len,what)*)
453 | GrafiteAst.NClear (_loc, l) -> NTactics.clear_tac l
454 | GrafiteAst.NDestruct (_,dom,skip) -> NDestructTac.destruct_tac dom skip
455 | GrafiteAst.NDot _ -> NTactics.dot_tac
456 | GrafiteAst.NElim (_loc, what, where) ->
458 ~what:(text,prefix_len,what)
459 ~where:(text,prefix_len,where)
460 | GrafiteAst.NFocus (_,l) -> NTactics.focus_tac l
461 | GrafiteAst.NGeneralize (_loc, where) ->
462 NTactics.generalize_tac ~where:(text,prefix_len,where)
463 | GrafiteAst.NId _ -> (fun x -> x)
464 | GrafiteAst.NIntro (_loc,n) -> NTactics.intro_tac n
465 | GrafiteAst.NIntros (_loc,ns) -> NTactics.intros_tac ns
466 | GrafiteAst.NInversion (_loc, what, where) ->
467 NTactics.inversion_tac
468 ~what:(text,prefix_len,what)
469 ~where:(text,prefix_len,where)
470 | GrafiteAst.NLApply (_loc, t) -> NTactics.lapply_tac (text,prefix_len,t)
471 | GrafiteAst.NLetIn (_loc,where,what,name) ->
472 NTactics.letin_tac ~where:(text,prefix_len,where)
473 ~what:(text,prefix_len,what) name
474 | GrafiteAst.NMerge _ -> NTactics.merge_tac
475 | GrafiteAst.NPos (_,l) -> NTactics.pos_tac l
476 | GrafiteAst.NPosbyname (_,s) -> NTactics.case_tac s
477 | GrafiteAst.NReduce (_loc, reduction, where) ->
478 NTactics.reduce_tac ~reduction ~where:(text,prefix_len,where)
479 | GrafiteAst.NRewrite (_loc,dir,what,where) ->
480 NTactics.rewrite_tac ~dir ~what:(text,prefix_len,what)
481 ~where:(text,prefix_len,where)
482 | GrafiteAst.NSemicolon _ -> fun x -> x
483 | GrafiteAst.NShift _ -> NTactics.shift_tac
484 | GrafiteAst.NSkip _ -> NTactics.skip_tac
485 | GrafiteAst.NUnfocus _ -> NTactics.unfocus_tac
486 | GrafiteAst.NWildcard _ -> NTactics.wildcard_tac
487 | GrafiteAst.NTry (_,tac) -> NTactics.try_tac
488 (f f (text, prefix_len, tac))
489 | GrafiteAst.NAssumption _ -> NTactics.assumption_tac
490 | GrafiteAst.NBlock (_,l) ->
491 NTactics.block_tac (List.map (fun x -> aux f (text,prefix_len,x)) l)
492 |GrafiteAst.NRepeat (_,tac) ->
493 NTactics.repeat_tac (f f (text, prefix_len, tac))
494 |GrafiteAst.Assume (_,id,t,t1) -> Declarative.assume id (text,prefix_len,t) (match t1 with None ->
495 None | Some t1 -> (Some (text,prefix_len,t1)))
496 |GrafiteAst.Suppose (_,t,id,t1) -> Declarative.suppose (text,prefix_len,t) id (match t1 with None
497 -> None | Some t1 -> (Some (text,prefix_len,t1)))
498 |GrafiteAst.By_just_we_proved (_,j,t1,s,t2) -> Declarative.by_just_we_proved
499 (just_to_tacstatus_just j text prefix_len) (text,prefix_len,t1) s (match t2 with None -> None |
500 Some t2 -> (Some (text,prefix_len,t2)))
501 |GrafiteAst.We_need_to_prove (_, t, id, t2) -> Declarative.we_need_to_prove (text,prefix_len,t) id
502 (match t2 with None -> None | Some t2 -> Some (text,prefix_len,t2))
503 | GrafiteAst.Bydone (_, j) -> Declarative.bydone (just_to_tacstatus_just j text prefix_len)
505 | GrafiteAst.ExistsElim (_, just, id1, t1, t2, id2) ->
506 Declarative.existselim just id1 t1 t2 id2
507 | GrafiteAst.AndElim(_,just,t1,id1,t2,id2) ->
508 Declarative.andelim just t1 id1 t2 id2
511 aux aux tac (* trick for non uniform recursion call *)
514 let subst_metasenv_and_fix_names status =
515 let u,h,metasenv, subst,o = status#obj in
517 NCicUntrusted.map_obj_kind ~skip_body:true
518 (NCicUntrusted.apply_subst status subst []) o
520 status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv status subst metasenv,subst,o)
523 let is_proof_irrelevant status context ty =
525 NCicReduction.whd status ~subst:[] context
526 (NCicTypeChecker.typeof status ~subst:[] ~metasenv:[] context ty)
528 NCic.Sort NCic.Prop -> true
529 | NCic.Sort _ -> false
533 let rec relevance_of status ?(context=[]) ty =
534 match NCicReduction.whd status ~subst:[] context ty with
536 not (is_proof_irrelevant status context s) ::
537 relevance_of status ~context:((n,NCic.Decl s)::context) t
541 let compute_relevance status uri =
543 NCic.Constant (_,name,bo,ty,attrs) ->
544 let relevance = relevance_of status ty in
545 NCic.Constant (relevance,name,bo,ty,attrs)
546 | NCic.Fixpoint (ind,funs,attrs) ->
549 (fun (_,name,recno,ty,bo) ->
550 let relevance = relevance_of status ty in
551 relevance,name,recno,ty,bo
554 NCic.Fixpoint (ind,funs,attrs)
555 | NCic.Inductive (ind,leftno,tys,attrs) ->
557 List.rev_map (fun (_,name,arity,_) -> name,NCic.Decl arity) tys in
558 let tysno = List.length tys in
561 (fun (_,name,arity,cons) ->
562 let relevance = relevance_of status arity in
567 NCicTypeChecker.debruijn status uri tysno ~subst:[] [] ty in
568 let relevance = relevance_of status ~context dety in
572 (relevance,name,arity,cons)
575 NCic.Inductive (ind,leftno,tys,attrs)
578 let extract_uris status uris =
581 let obj = NCicEnvironment.get_checked_obj status uri in
582 let status = eval_extract_obj status obj in
583 eval_extract_ocaml_obj status obj
587 let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
589 | GrafiteAst.Include (loc, mode, fname) ->
590 let _root, baseuri, fullpath, _rrelpath =
591 Librarian.baseuri_of_script ~include_paths fname in
592 let baseuri = NUri.uri_of_string baseuri in
593 (* MATITA 1.0: keep WithoutPreferences? *)
594 let alias_only = (mode = GrafiteAst.OnlyPreferences) in
596 GrafiteTypes.Serializer.require
597 ~alias_only ~baseuri ~fname:fullpath status in
598 OcamlExtraction.print_open status
599 (NCicLibrary.get_transitively_included status)
600 | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
601 | GrafiteAst.NCoercion (loc, name, compose, None) ->
602 let status, t, ty, source, target =
603 let o_t = NotationPt.Ident (name,None) in
604 let metasenv,subst, status,t =
605 GrafiteDisambiguate.disambiguate_nterm
606 status `XTNone [] [] [] ("",0,o_t) in
607 assert( metasenv = [] && subst = []);
608 let ty = NCicTypeChecker.typeof status [] [] [] t in
611 | NCic.Appl (NCic.Const _ as r :: l) ->
612 NotationPt.Appl (NotationPt.NCic r ::
613 List.map (fun _ -> NotationPt.Implicit `JustOne)l)
614 | NCic.Const _ as r -> NotationPt.NCic r
615 | NCic.Sort _ as r -> NotationPt.NCic r (* FG: missing case *)
618 let rec aux = function
619 | NCic.Prod (_,_, (NCic.Prod _ as rest)) -> aux rest
620 | NCic.Prod (name, src, tgt) -> (name, clean src), clean tgt
625 status, o_t, NotationPt.NCic ty, source, target
628 GrafiteAst.NCoercion (loc, name, compose, Some (t, ty, source, target))
630 eval_ncommand ~include_paths opts status (text,prefix_len,cmd)
631 | GrafiteAst.NCoercion (loc, name, compose, Some (t, ty, source, target)) ->
632 let status, composites =
633 NCicCoercDeclaration.eval_ncoercion status name compose t ty source
635 let status = extract_uris status composites in
636 let mode = GrafiteAst.WithPreferences in (* MATITA 1.0: fixme *)
637 let aliases = GrafiteDisambiguate.aliases_for_objs status composites in
638 eval_alias status (mode,aliases)
639 | GrafiteAst.NQed (loc,index) ->
640 if status#ng_mode <> `ProofMode then
641 raise (GrafiteTypes.Command_error "Not in proof mode")
643 let uri,height,menv,subst,obj_kind = status#obj in
646 (GrafiteTypes.Command_error"You can't Qed an incomplete theorem")
649 NCicUntrusted.map_obj_kind
650 (NCicUntrusted.apply_subst status subst []) obj_kind in
651 let height = NCicTypeChecker.height_of_obj_kind status uri [] obj_kind in
652 (* fix the height inside the object *)
653 let rec fix () = function
654 | NCic.Const (NReference.Ref (u,spec)) when NUri.eq u uri ->
655 NCic.Const (NReference.reference_of_spec u
657 | NReference.Def _ -> NReference.Def height
658 | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
659 | NReference.CoFix i -> NReference.CoFix i
660 | NReference.Ind _ | NReference.Con _
661 | NReference.Decl as s -> s))
662 | t -> NCicUtils.map status (fun _ () -> ()) () fix t
667 NCicUntrusted.map_obj_kind (fix ()) obj_kind
670 let obj_kind = compute_relevance status uri obj_kind in
671 let obj = uri,height,[],[],obj_kind in
672 let old_status = status in
673 let status = NCicLibrary.add_obj status obj in
674 let status = eval_extract_obj status obj in
675 let status = eval_extract_ocaml_obj status obj in
677 let index_obj = index &&
679 NCic.Constant (_,_,_,_,(_,`Example,_))
680 | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
685 let status = index_obj_for_auto status obj in
686 (try index_eq_for_auto status uri
688 Sys.Break as exn -> raise exn
692 (* purge tinycals stack *)
693 let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
694 let status = status#set_stack ninitial_stack in
698 with _ -> prerr_endline "got an exception"; status
700 (* prerr_endline (status#ppobj obj); *)
701 HLog.message ("New object: " ^ NUri.string_of_uri uri);
702 (*prerr_endline (status#ppobj obj);*)
703 let boxml = NCicElim.mk_elims status obj in
704 let boxml = boxml @ NCicElim.mk_projections status obj in
705 let status = status#set_ng_mode `CommandMode in
706 let xxaliases = GrafiteDisambiguate.aliases_for_objs status [uri] in
707 let mode = GrafiteAst.WithPreferences in (* MATITA 1.0: fixme *)
708 let status = eval_alias status (mode,xxaliases) in
714 eval_ncommand ~include_paths opts status
715 ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml,true))
717 if nstatus#ng_mode <> `CommandMode then
719 (*HLog.warn "error in generating projection/eliminator";*)
720 assert(status#ng_mode = `CommandMode);
726 | MultiPassDisambiguator.DisambiguationError _ ->
727 HLog.warn "error in disambiguating projection/eliminator";
729 | NCicTypeChecker.TypeCheckerFailure _ ->
730 HLog.warn "error in typechecking projection/eliminator";
733 let _,_,_,_,nobj = obj in
734 (* attempting to generate an inversion principle on the maximum
735 * universe can take a long time to fail: this code removes maximal
736 * sorts from the universe list *)
737 let universes = NCicEnvironment.get_universes () in
738 let max_univ = List.fold_left max [] universes in
740 List.filter (fun x -> NCicEnvironment.universe_lt x max_univ)
743 let status = match nobj with
744 NCic.Inductive (is_ind,leftno,itl,_) ->
745 List.fold_left (fun status it ->
746 (let _,ind_name,ty,cl = it in
748 (fun status outsort ->
749 let status = status#set_ng_mode `ProofMode in
752 NInversion.mk_inverter
753 (ind_name ^ "_inv_" ^
754 (snd (NCicElim.ast_of_sort outsort)))
755 is_ind it leftno outsort status status#baseuri in
756 let _,_,menv,_,_ = invobj in
758 [] -> eval_ncommand ~include_paths opts status
759 ("",0,GrafiteAst.NQed (Stdpp.dummy_loc,false))
763 Sys.Break as exn -> raise exn
764 | _ -> (*HLog.warn "error in generating inversion principle"; *)
765 let status = status#set_ng_mode `CommandMode in status)
768 List.map (fun s -> NCic.Type s) universes))) status itl
771 let status = match nobj with
772 NCic.Inductive (is_ind,leftno,itl,_) ->
774 let status' = List.fold_left
776 let _,ind_name,ty,cl = it in
777 let status = status#set_ng_mode `ProofMode in
780 NDestructTac.mk_discriminator ~use_jmeq:false
781 (ind_name ^ "_discr")
782 it leftno status status#baseuri in
783 let _,_,menv,_,_ = invobj in
785 [] -> eval_ncommand ~include_paths opts status
786 ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
790 | NDestructTac.ConstructorTooBig k ->
791 HLog.warn (Printf.sprintf
792 "unable to generate leibniz discrimination principle (constructor %s too big)"
794 let status = status#set_ng_mode `CommandMode in status
795 | _ -> (*HLog.warn "error in generating discrimination principle"; *)
796 let status = status#set_ng_mode `CommandMode in
803 let _,ind_name,ty,cl = it in
804 let status = status#set_ng_mode `ProofMode in
807 NDestructTac.mk_discriminator ~use_jmeq:true
808 (ind_name ^ "_jmdiscr")
809 it leftno status status#baseuri in
810 let _,_,menv,_,_ = invobj in
812 [] -> eval_ncommand ~include_paths opts status
813 ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
817 Sys.Break as exn -> raise exn
818 | _ -> (*HLog.warn "error in generating discrimination principle"; *)
819 let status = status#set_ng_mode `CommandMode in
826 _,_,_,_,NCic.Inductive
827 (true,leftno,[_,_,_,[_,_,_]],(_,`Record fields))
830 (fun (name,is_coercion,arity) ->
831 if is_coercion then Some(name,leftno,arity) else None) fields
835 (fun status (name,cpos,arity) ->
837 let metasenv,subst,status,t =
838 GrafiteDisambiguate.disambiguate_nterm status `XTNone [] [] []
839 ("",0,NotationPt.Ident (name,None)) in
840 assert (metasenv = [] && subst = []);
842 NCicCoercDeclaration.
843 basic_eval_and_record_ncoercion_from_t_cpos_arity
844 status (name,true,t,cpos,arity) in
845 let aliases = GrafiteDisambiguate.aliases_for_objs status nuris in
846 let status = extract_uris status nuris in
847 eval_alias status (mode,aliases)
848 with MultiPassDisambiguator.DisambiguationError _->
849 HLog.warn ("error in generating coercion: "^name);
856 NCicLibrary.time_travel old_status;
858 | GrafiteAst.NCopy (log,tgt,src_uri, map) ->
859 if status#ng_mode <> `CommandMode then
860 raise (GrafiteTypes.Command_error "Not in command mode")
862 let tgt_uri_ext, old_ok =
863 match NCicEnvironment.get_checked_obj status src_uri with
864 | _,_,[],[], (NCic.Inductive _ as ok) -> ".ind", ok
865 | _,_,[],[], (NCic.Fixpoint _ as ok) -> ".con", ok
866 | _,_,[],[], (NCic.Constant _ as ok) -> ".con", ok
869 let tgt_uri = NUri.uri_of_string (status#baseuri^"/"^tgt^tgt_uri_ext) in
870 let map = (src_uri, tgt_uri) :: map in
872 let rec subst () = function
873 | NCic.Meta _ -> assert false
874 | NCic.Const (NReference.Ref (u,spec)) as t ->
876 (NReference.reference_of_spec (List.assoc u map)spec)
878 | t -> NCicUtils.map status (fun _ _ -> ()) () subst t
880 NCicUntrusted.map_obj_kind ~skip_body:false (subst ()) old_ok
882 let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
883 let status = status#set_obj (tgt_uri,0,[],[],ok) in
884 (*prerr_endline (status#ppobj (tgt_uri,0,[],[],ok));*)
885 let status = status#set_stack ninitial_stack in
886 let status = subst_metasenv_and_fix_names status in
887 let status = status#set_ng_mode `ProofMode in
888 eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
889 | GrafiteAst.NObj (loc,obj,index) ->
890 if status#ng_mode <> `CommandMode then
891 raise (GrafiteTypes.Command_error "Not in command mode")
894 GrafiteDisambiguate.disambiguate_nobj status
895 ~baseuri:status#baseuri (text,prefix_len,obj) in
896 let uri,height,nmenv,nsubst,nobj = obj in
897 let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
898 let status = status#set_obj obj in
899 let status = status#set_stack ninitial_stack in
900 let status = subst_metasenv_and_fix_names status in
901 let status = status#set_ng_mode `ProofMode in
904 eval_ncommand ~include_paths opts status
905 ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,index))
907 | GrafiteAst.NDiscriminator (loc, indty) ->
908 if status#ng_mode <> `CommandMode then
909 raise (GrafiteTypes.Command_error "Not in command mode")
911 let status = status#set_ng_mode `ProofMode in
912 let metasenv,subst,status,indty =
913 GrafiteDisambiguate.disambiguate_nterm status `XTNone [] [] [] (text,prefix_len,indty) in
914 let indtyno, (_,_,tys,_,_),leftno = match indty with
915 NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,leftno))) as r) ->
916 indtyno, NCicEnvironment.get_checked_indtys status r, leftno
917 | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
918 let (_,ind_name,_,_ as it) = List.nth tys indtyno in
920 NDestructTac.mk_discriminator ~use_jmeq:true ~force:true (ind_name ^ "_jmdiscr")
921 it leftno status status#baseuri in
922 let _,_,menv,_,_ = obj in
924 [] -> eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
925 | _ -> prerr_endline ("Discriminator: non empty metasenv");
927 | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
928 if status#ng_mode <> `CommandMode then
929 raise (GrafiteTypes.Command_error "Not in command mode")
931 let metasenv,subst,status,sort = match sort with
932 | None -> [],[],status,NCic.Sort NCic.Prop
934 GrafiteDisambiguate.disambiguate_nterm status `XTSort [] [] []
937 assert (metasenv = []);
938 let sort = NCicReduction.whd status ~subst [] sort in
943 raise (Invalid_argument (Printf.sprintf
944 "ninverter: found target %s, which is not a sort"
945 (status#ppterm ~metasenv ~subst ~context:[] sort))) in
946 let status = status#set_ng_mode `ProofMode in
947 let metasenv,subst,status,indty =
948 GrafiteDisambiguate.disambiguate_nterm status `XTNone [] [] subst
949 (text,prefix_len,indty) in
950 let indtyno,(_,leftno,tys,_,_) =
952 NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
953 indtyno, NCicEnvironment.get_checked_indtys status r
955 prerr_endline ("engine: indty =" ^ status#ppterm ~metasenv:[]
956 ~subst:[] ~context:[] indty);
958 let it = List.nth tys indtyno in
960 NInversion.mk_inverter name true it leftno ?selection sort
961 status status#baseuri in
962 let _,_,menv,_,_ = obj in
965 eval_ncommand ~include_paths opts status
966 ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
968 | GrafiteAst.NUnivConstraint (loc,acyclic,u1,u2) ->
969 eval_add_constraint status acyclic [`Type,u1] [`Type,u2]
970 (* ex lexicon commands *)
971 | GrafiteAst.Interpretation (loc, dsc, (symbol, args), cic_appl_pattern) ->
972 let cic_appl_pattern =
973 GrafiteDisambiguate.disambiguate_cic_appl_pattern status args
976 eval_interpretation status (dsc,(symbol, args),cic_appl_pattern)
977 | GrafiteAst.Notation (loc, dir, l1, associativity, precedence, l2) ->
979 CicNotationParser.check_l1_pattern
980 l1 (dir = Some `RightToLeft) precedence associativity
983 if dir <> Some `RightToLeft then eval_input_notation status (l1,l2)
986 if dir <> Some `LeftToRight then eval_output_notation status (l1,l2)
988 | GrafiteAst.Alias (loc, spec) ->
990 (*CSC: Warning: this code should be factorized with the corresponding
991 code in DisambiguatePp *)
993 | GrafiteAst.Ident_alias (id,uri) ->
994 [DisambiguateTypes.Id id,spec]
995 | GrafiteAst.Symbol_alias (symb, instance, desc) ->
996 [DisambiguateTypes.Symbol (symb,instance),spec]
997 | GrafiteAst.Number_alias (instance,desc) ->
998 [DisambiguateTypes.Num instance,spec]
1000 let mode = GrafiteAst.WithPreferences in(*assert false in (* VEDI SOPRA *) MATITA 1.0*)
1001 eval_alias status (mode,diff)
1004 let eval_comment opts status (text,prefix_len,c) = status
1006 let rec eval_executable ~include_paths opts status (text,prefix_len,ex) =
1008 | GrafiteAst.NTactic (_(*loc*), tacl) ->
1009 if status#ng_mode <> `ProofMode then
1010 raise (GrafiteTypes.Command_error "Not in proof mode")
1015 let time0 = Unix.gettimeofday () in
1016 let status = eval_ng_tac (text,prefix_len,tac) status in
1017 let time3 = Unix.gettimeofday () in
1018 HLog.debug ("... eval_ng_tac done in " ^ string_of_float (time3 -. time0) ^ "s");
1019 let status = subst_metasenv_and_fix_names status in
1020 let time3 = Unix.gettimeofday () in
1021 HLog.debug ("... subst_metasenv_and_fix_names done in " ^ string_of_float (time3 -. time0) ^ "s"); status)
1025 | GrafiteAst.NCommand (_, cmd) ->
1026 eval_ncommand ~include_paths opts status (text,prefix_len,cmd)
1027 | GrafiteAst.NMacro (loc, macro) ->
1028 raise (NMacro (loc,macro))
1030 and eval_ast ~include_paths ?(do_heavy_checks=false) status (text,prefix_len,st)
1032 let opts = { do_heavy_checks = do_heavy_checks ; } in
1034 | GrafiteAst.Executable (_,ex) ->
1035 eval_executable ~include_paths opts status (text,prefix_len,ex)
1036 | GrafiteAst.Comment (_,c) ->
1037 eval_comment opts status (text,prefix_len,c)