- let match_term status ctx (wanted : cic_term) t =
- let rec aux ctx status t =
- let b, status = found status ctx t wanted in
- if b then
- let status, (_,_,t) =
- mk_meta status ~name:in_scope_tag (`Ctx ctx) (`Def (None,ctx,t))
- in
- status, t
- else NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
- in
- aux ctx status t
- in
- let rec select status ctx pat cic =
- match pat, cic with
- | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
- let status, t = select status ctx t1 t2 in
- let status, s = select status ctx s1 s2 in
- let ctx = (n, NCic.Def (s2,t2)) :: ctx in
- let status, b = select status ctx b1 b2 in
- status, NCic.LetIn (n,t,s,b)
- | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
- let status, s = select status ctx s1 s2 in
- let ctx = (n, NCic.Decl s2) :: ctx in
- let status, t = select status ctx t1 t2 in
- status, NCic.Lambda (n,s,t)
- | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
- let status, s = select status ctx s1 s2 in
- let ctx = (n, NCic.Decl s2) :: ctx in
- let status, t = select status ctx t1 t2 in
- status, NCic.Prod (n,s,t)
- | NCic.Appl l1, NCic.Appl l2 ->
- let status, l =
- List.fold_left2
- (fun (status,l) x y ->
- let status, x = select status ctx x y in
- status, x::l)
- (status,[]) l1 l2
- in
- status, NCic.Appl (List.rev l)
- | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
- let status, t = select status ctx t1 t2 in
- let status, ot = select status ctx ot1 ot2 in
- let status, pl =
- List.fold_left2
- (fun (status,l) x y ->
- let status, x = select status ctx x y in
- status, x::l)
- (status,[]) pl1 pl2
- in
- status, NCic.Match (u,ot,t,List.rev pl)
- | NCic.Implicit `Hole, t ->
- (match wanted with
- | Some wanted ->
- let status, wanted = disambiguate status wanted None (`Ctx ctx) in
- match_term status ctx wanted t
- | None -> match_term status ctx (None,ctx,t) t)
- | NCic.Implicit _, t -> status, t
- | _,t ->
- fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
- ~context:[] ~subst:[] pat))
+ aux 1 context
+;;
+
+let clear_tac names =
+ if names = [] then id_tac
+ else
+ distribute_tac (fun status goal ->
+ let goalty = get_goalty status goal in
+ let js =
+ List.map
+ (fun name ->
+ try find_in_context name (ctx_of goalty)
+ with Not_found ->
+ fail (lazy ("hypothesis '" ^ name ^ "' not found")))
+ names
+ in
+ let n,h,metasenv,subst,o = status#obj in
+ let metasenv,subst,_ = NCicMetaSubst.restrict metasenv subst goal js in
+ status#set_obj (n,h,metasenv,subst,o))
+;;
+
+let generalize0_tac args =
+ if args = [] then id_tac
+ else exact_tac ("",0,Ast.Appl (Ast.Implicit :: args))
+;;
+
+let select0_tac ~where:(wanted,hyps,where) ~job =
+ let found, postprocess =
+ match job with
+ | `Substexpand argsno -> mk_in_scope, mk_out_scope argsno
+ | `Collect l -> (fun s t -> l := t::!l; mk_in_scope s t), mk_out_scope 1
+ | `ChangeWith f -> f,(fun s t -> s, t)
+ in
+ distribute_tac (fun status goal ->
+ let goalty = get_goalty status goal in
+ let path =
+ match where with None -> NCic.Implicit `Term | Some where -> where
+ in
+ let status, newgoalctx =
+ List.fold_right
+ (fun (name,d as entry) (status,ctx) ->
+ try
+ let path = List.assoc name hyps in
+ match d with
+ NCic.Decl ty ->
+ let status,ty =
+ select_term status ~found ~postprocess (mk_cic_term ctx ty)
+ (wanted,path) in
+ let status,ty = term_of_cic_term status ty ctx in
+ status,(name,NCic.Decl ty)::ctx
+ | NCic.Def (bo,ty) ->
+ let status,bo =
+ select_term status ~found ~postprocess (mk_cic_term ctx bo)
+ (wanted,path) in
+ let status,bo = term_of_cic_term status bo ctx in
+ status,(name,NCic.Def (bo,ty))::ctx
+ with
+ Not_found -> status, entry::ctx
+ ) (ctx_of goalty) (status,[])
+ in
+ let status, newgoalty =
+ select_term status ~found ~postprocess goalty (wanted,path) in
+ (* WARNING: the next two lines simply change the context of newgoalty
+ from the old to the new one. Otherwise mk_meta will do that herself,
+ calling relocate that calls delift. However, newgoalty is now
+ ?[out_scope] and thus the delift would trigger the special unification
+ case, which is wrong now :-( *)
+ let status,newgoalty = term_of_cic_term status newgoalty (ctx_of goalty) in
+ let newgoalty = mk_cic_term newgoalctx newgoalty in
+
+ let status, instance =
+ mk_meta status newgoalctx (`Decl newgoalty)
+ in
+ instantiate status goal instance)
+;;
+
+let select_tac ~where ~job move_down_hyps =
+ let (wanted,hyps,where) = GrafiteDisambiguate.disambiguate_npattern where in
+ let path =
+ match where with None -> NCic.Implicit `Term | Some where -> where in
+ if not move_down_hyps then
+ select0_tac ~where:(wanted,hyps,Some path) ~job
+ else
+ let path =
+ List.fold_left
+ (fun path (name,path_name) -> NCic.Prod ("_",path_name,path))
+ path (List.rev hyps)