-type cic_term = NCic.conjecture (* name, context, term *)
-let ctx_of (_,c,_) = c ;;
-
-let relocate status destination (name,source,t as orig) =
- if source == destination then status, orig else
- let rec lcp j i = function
- | (n1, t1)::cl1, (n2, t2)::cl2 ->
- if n1 = n2 && t1 = t2 then
- NCic.Rel i :: lcp (j-1) (i-1) (cl1,cl2)
- else
- HExtlib.mk_list (NCic.Appl
- [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
- | _::_, [] ->
- HExtlib.mk_list (NCic.Appl
- [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
- | _ -> []
- in
- let lc =
- lcp (List.length destination) (List.length source)
- (List.rev destination, List.rev source)
- in
- let lc = (0,NCic.Ctx (List.rev lc)) in
- let u, d, metasenv, subst, o = status.pstatus in
- let db = NCicUnifHint.db () in (* XXX fixme *)
- let (metasenv, subst), t =
- NCicMetaSubst.delift
- ~unify:(fun m s c t1 t2 ->
- try Some (NCicUnification.unify db m s c t1 t2)
- with
- | NCicUnification.UnificationFailure _
- | NCicUnification.Uncertain _ -> None)
- metasenv subst source 0 lc t
- in
- let status = { status with pstatus = u, d, metasenv, subst, o } in
- status, (name, destination, t)
+let ppcontext status c =
+ let uri,height,metasenv,subst,obj = status#obj in
+ NCicPp.ppcontext ~metasenv ~subst c
+;;
+
+let ppterm_and_context status t =
+ let uri,height,metasenv,subst,obj = status#obj in
+ let context,t = t in
+ NCicPp.ppcontext ~metasenv ~subst context ^ "\n ⊢ "^
+ NCicPp.ppterm ~metasenv ~subst ~context t
+;;
+
+let relocate status destination (source,t as orig) =
+ pp(lazy("relocate:\n" ^ ppterm_and_context status orig));
+ pp(lazy("relocate in:\n" ^ ppcontext status destination));
+ let rc =
+ if source == destination then status, orig else
+ let u, d, metasenv, subst, o = status#obj in
+ let cons x (a,b) = a, x::b in
+ let rec lcp ctx j i = function
+ | (n1, NCic.Decl t1 as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
+ if n1 = n2 &&
+ NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
+ cons (NCic.Rel i) (lcp (e::ctx)(j-1) (i-1) (cl1,cl2))
+ else
+ j, []
+ | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Def (b2,t2))::cl2 ->
+ if n1 = n2 &&
+ NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 &&
+ NCicReduction.are_convertible ctx ~subst ~metasenv b1 b2 then
+ cons (NCic.Rel i) (lcp (e::ctx)(j-1) (i-1) (cl1,cl2))
+ else
+ j, []
+ | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
+ if n1 = n2 &&
+ NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
+ cons (NCic.Rel i) (lcp (e::ctx)(j-1) (i-1) (cl1,cl2))
+ else
+ j, []
+ | (n1, NCic.Decl _)::cl1, (n2, NCic.Def _)::cl2 -> assert false
+ | _::_, [] -> j, []
+ | _ -> 0, []
+ in
+ let shift, lc =
+ lcp [] (List.length destination) (List.length source)
+ (List.rev destination, List.rev source)
+ in
+ let lc = (shift,NCic.Ctx (List.rev lc)) in
+ pp(lazy("delifting as " ^
+ NCicPp.ppterm ~metasenv ~subst ~context:source
+ (NCic.Meta (0,lc))));
+ let (metasenv, subst), t =
+ NCicMetaSubst.delift
+ ~unify:(fun m s c t1 t2 ->
+ try Some (NCicUnification.unify status m s c t1 t2)
+ with
+ | NCicUnification.UnificationFailure _
+ | NCicUnification.Uncertain _ -> None)
+ metasenv subst source 0 lc t
+ in
+ let status = status#set_obj (u, d, metasenv, subst, o) in
+ status, (destination, t)
+ in
+ pp(lazy("relocated: " ^ ppterm (fst rc) (snd rc)));
+ rc