+ 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))
+ | 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)) when Ref.eq ref1 ref2 ->
+ 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
+ in
+ (try
+ List.fold_left2
+ (fun (metasenv,subst) t1 t2 ->
+ unify rdb test_eq_only metasenv subst context t1 t2 swap)
+ (metasenv, subst) pl1 pl2
+ with Invalid_argument _ -> assert false)
+ | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
+ | _ when norm1 && norm2 ->
+ if (could_reduce t1 || could_reduce t2) then
+ raise (Uncertain (mk_msg metasenv subst context t1 t2))
+ else
+ raise (UnificationFailure (mk_msg metasenv subst context t1 t2))
+ | _ -> raise (KeepReducing (mk_msg metasenv subst context t1 t2))
+ (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
+ in
+ let fo_unif test_eq_only metasenv subst (norm1,t1 as nt1) (norm2,t2 as nt2)=
+ try fo_unif test_eq_only metasenv subst nt1 nt2
+ with
+ UnificationFailure _ | Uncertain _ when not norm1 || not norm2 ->
+ raise (KeepReducing (mk_msg metasenv subst context t1 t2))
+ in
+ let try_hints metasenv subst (_,t1 as mt1) (_,t2 as mt2) (* exc*) =
+ if NCicUntrusted.metas_of_term subst context t1 = [] &&
+ NCicUntrusted.metas_of_term subst context t2 = []
+ then None
+ else begin
+ (*D*) inside 'H'; try let rc =
+ pp(lazy ("\nProblema:\n" ^
+ ppterm ~metasenv ~subst ~context t1 ^ " =?= " ^
+ ppterm ~metasenv ~subst ~context t2));
+ let candidates =
+ NCicUnifHint.look_for_hint rdb metasenv subst context t1 t2
+ in
+ let rec cand_iter = function
+ | [] -> None (* raise exc *)
+ | (metasenv,(c1,c2),premises)::tl ->
+ pp (lazy ("\nProvo il candidato:\n" ^
+ String.concat "\n"
+ (List.map
+ (fun (a,b) ->
+ ppterm ~metasenv ~subst ~context a ^ " =?= " ^
+ ppterm ~metasenv ~subst ~context b) premises) ^
+ "\n-------------------------------------------\n"^
+ ppterm ~metasenv ~subst ~context c1 ^ " = " ^
+ ppterm ~metasenv ~subst ~context c2));
+ try
+ (*D*) inside 'K'; try let rc =
+ let metasenv,subst =
+ fo_unif test_eq_only metasenv subst mt1 (false,c1) in
+ let metasenv,subst =
+ fo_unif test_eq_only metasenv subst (false,c2) mt2 in
+ let metasenv,subst =
+ List.fold_left
+ (fun (metasenv, subst) (x,y) ->
+ unify rdb test_eq_only metasenv subst context x y false)
+ (metasenv, subst) (List.rev premises)
+ in
+ pp(lazy("FUNZIONA!"));
+ Some (metasenv, subst)
+ (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
+ with
+ KeepReducing _ | UnificationFailure _ | Uncertain _ -> cand_iter tl
+ | KeepReducingThis _ -> assert false
+ in
+ cand_iter candidates
+ (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
+ end
+ in
+ let put_in_whd m1 m2 =
+ NCicReduction.reduce_machine ~delta:max_int ~subst context m1,
+ NCicReduction.reduce_machine ~delta:max_int ~subst context m2
+ in
+ let fo_unif_w_hints test_eq_only metasenv subst (_,t1 as m1) (_,t2 as m2) =
+ try fo_unif test_eq_only metasenv subst m1 m2
+ with
+ | UnificationFailure _ as exn -> raise exn
+ | KeepReducing _ | Uncertain _ as exn ->
+ let (t1,norm1 as tm1),(t2,norm2 as tm2) =
+ put_in_whd (0,[],t1,[]) (0,[],t2,[])
+ in
+ match
+ try_hints metasenv subst
+ (norm1,NCicReduction.unwind t1) (norm2,NCicReduction.unwind t2)
+ with
+ | Some x -> x
+ | None ->
+ match exn with
+ | KeepReducing msg -> raise (KeepReducingThis (msg,tm1,tm2))
+ | Uncertain _ as exn -> raise exn
+ | _ -> assert false
+ in
+ let fo_unif_heads_and_cont_or_unwind_and_hints
+ test_eq_only metasenv subst m1 m2 cont hm1 hm2
+ =
+ let ms, continuation =
+ (* calling the continuation inside the outermost try is an option
+ and makes unification stronger, but looks not frequent to me
+ that heads unify but not the arguments and that an hints can fix
+ that *)
+ try fo_unif test_eq_only metasenv subst m1 m2, cont
+ with
+ | UnificationFailure _
+ | KeepReducing _ | Uncertain _ as exn ->
+ let (t1,norm1),(t2,norm2) = hm1, hm2 in
+ match
+ try_hints metasenv subst
+ (norm1,NCicReduction.unwind t1) (norm2,NCicReduction.unwind t2)
+ with
+ | Some x -> x, fun x -> x
+ | None ->
+ match exn with
+ | KeepReducing msg -> raise (KeepReducingThis (msg,hm1,hm2))
+ | UnificationFailure _ | Uncertain _ as exn -> raise exn
+ | _ -> assert false
+ in
+ continuation ms