+ let t1 = NCicReduction.whd ~subst context t1 in
+ let j, lj =
+ match t1 with NCic.Meta (j,l) -> j, l | _ -> assert false
+ in
+ let metasenv, subst =
+ instantiate rdb test_eq_only metasenv subst context j lj t2 true
+ in
+ (* We need to remove the out_scope_tags to avoid propagation of
+ them that triggers again the ad-hoc case *)
+ let subst =
+ List.map (fun (i,(tag,ctx,bo,ty)) ->
+ let tag =
+ List.filter
+ (function `InScope | `OutScope _ -> false | _ -> true) tag
+ in
+ i,(tag,ctx,bo,ty)
+ ) subst
+ in
+ (try
+ let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
+ let term = eta_reduce subst term in
+ let subst = List.filter (fun (j,_) -> j <> i) subst in
+ metasenv, ((i, (name, ctx, term, ty)) :: subst)
+ with Not_found -> assert false))
+ | NCic.Meta (n, _), _ when is_locked n subst && swap ->
+ unify rdb test_eq_only metasenv subst context t2 t1 false
+
+ | t, C.Meta (n,lc) when List.mem_assoc n subst ->
+ let _,_,term,_ = NCicUtils.lookup_subst n subst in
+ let term = NCicSubstitution.subst_meta lc term in
+ unify rdb test_eq_only metasenv subst context t term swap
+ | C.Meta (n,_), _ when List.mem_assoc n subst ->
+ unify rdb test_eq_only metasenv subst context t2 t1 (not swap)
+
+ | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
+ let _,_,term,_ = NCicUtils.lookup_subst i subst in
+ let term = NCicSubstitution.subst_meta l term in
+ unify rdb test_eq_only metasenv subst context t1
+ (mk_appl ~upto:(List.length args) term args) swap
+ | NCic.Appl (NCic.Meta (i,_)::_), _ when List.mem_assoc i subst ->
+ unify rdb test_eq_only metasenv subst context t2 t1 (not swap)
+
+ | C.Meta (n,_), C.Meta (m,lc') when
+ let _,cc1,_ = NCicUtils.lookup_meta n metasenv in
+ let _,cc2,_ = NCicUtils.lookup_meta m metasenv in
+ (n < m && cc1 = cc2) ||
+ let len1 = List.length cc1 in
+ let len2 = List.length cc2 in
+ len2 < len1 && cc2 = fst (HExtlib.split_nth len2 cc1) ->
+ instantiate rdb test_eq_only metasenv subst context m lc'
+ (NCicReduction.head_beta_reduce ~subst t1) (not swap)
+ | C.Meta (n,lc), t ->
+ instantiate rdb test_eq_only metasenv subst context n lc
+ (NCicReduction.head_beta_reduce ~subst t) swap
+ | t, C.Meta (n,lc) ->
+ instantiate rdb test_eq_only metasenv subst context n lc
+ (NCicReduction.head_beta_reduce ~subst t) (not swap)
+
+ | NCic.Appl (NCic.Meta (i,l) as meta :: args), _ ->
+ let metasenv, lambda_Mj =
+ lambda_intros rdb metasenv subst context (List.length args)
+ (NCicTypeChecker.typeof ~metasenv ~subst context meta)
+ in
+ let metasenv, subst =
+ try
+ unify rdb test_eq_only metasenv subst context
+ (C.Meta (i,l)) lambda_Mj swap
+ with UnificationFailure msg | Uncertain msg when not norm2->
+ (* failure: let's try again argument vs argument *)
+ raise (KeepReducing msg)
+ in
+ let metasenv, subst =
+ unify rdb test_eq_only metasenv subst context t1 t2 swap
+ in
+ (try
+ let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
+ let term = eta_reduce subst term in
+ let subst = List.filter (fun (j,_) -> j <> i) subst in
+ metasenv, ((i, (name, ctx, term, ty)) :: subst)
+ with Not_found -> assert false)
+ | _, NCic.Appl (NCic.Meta (_,_) :: _) ->
+ unify rdb test_eq_only metasenv subst context t2 t1 (not swap)
+
+ (* processing this case here we avoid a useless small delta step *)
+ | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2))
+ when Ref.eq r1 r2 ->
+ let relevance = NCicEnvironment.get_relevance r1 in
+ let metasenv, subst, _ =
+ try
+ List.fold_left2
+ (fun (metasenv, subst, relevance) t1 t2 ->
+ let b, relevance =
+ match relevance with b::tl -> b,tl | _ -> true, [] in
+ let metasenv, subst =
+ try unify rdb test_eq_only metasenv subst context t1 t2
+ swap
+ with UnificationFailure _ | Uncertain _ when not b ->
+ metasenv, subst
+ in
+ metasenv, subst, relevance)
+ (metasenv, subst, relevance) tl1 tl2
+ with
+ Invalid_argument _ ->
+ raise (Uncertain (mk_msg metasenv subst context t1 t2))
+ | UnificationFailure _ | Uncertain _ when not (norm1 && norm2) ->
+ raise (KeepReducing (mk_msg metasenv subst context t1 t2))
+ | KeepReducing _ | KeepReducingThis _ -> assert false
+ in
+ metasenv, subst
+
+ | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
+ C.Match (ref2,outtype2,term2,pl2)) ->
+ let _,_,itl,_,_ = NCicEnvironment.get_checked_indtys ref1 in
+ let _,_,ty,_ = List.nth itl tyno in
+ let rec remove_prods ~subst context ty =
+ let ty = NCicReduction.whd ~subst context ty in
+ match ty with
+ | C.Sort _ -> ty
+ | C.Prod (name,so,ta) ->
+ remove_prods ~subst ((name,(C.Decl so))::context) ta
+ | _ -> assert false
+ in
+ let is_prop =
+ match remove_prods ~subst [] ty with
+ | C.Sort C.Prop -> true
+ | _ -> false
+ in
+ if not (Ref.eq ref1 ref2) then
+ raise (Uncertain (mk_msg metasenv subst context t1 t2))
+ else
+ let metasenv, subst =
+ unify rdb test_eq_only metasenv subst context outtype1 outtype2 swap in
+ let metasenv, subst =
+ try unify rdb test_eq_only metasenv subst context term1 term2 swap
+ with UnificationFailure _ | Uncertain _ when is_prop ->
+ metasenv, subst