V_______________________________________________________________ *)
(* $Id$ *)
+
+exception UnificationFailure of string Lazy.t;;
+exception Uncertain of string Lazy.t;;
+exception AssertFailure of string Lazy.t;;
+
+let (===) x y = Pervasives.compare x y = 0 ;;
+
+let fail_exc metasenv subst context t1 t2 =
+ UnificationFailure (lazy (
+ "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
+ " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2))
+;;
+
+let unify metasenv subst context t1 t2 =
+ (* are_convertible?? *)
+ let rec aux test_eq_only metasenv subst context t1 t2 =
+ let fo_unif test_eq_only t1 t2 =
+ if t1 === t2 then
+ metasenv, subst
+ else
+ match (t1,t2) with
+ | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only ->
+ if NCicEnvironment.universe_leq a b then metasenv, subst
+ else raise (fail_exc metasenv subst context t1 t2)
+ | (C.Sort (C.Type a), C.Sort (C.Type b)) ->
+ if NCicEnvironment.universe_eq a b then metasenv, subst
+ else raise (fail_exc metasenv subst context t1 t2)
+ | (C.Sort C.Prop,C.Sort (C.Type _)) ->
+ if (not test_eq_only) then metasenv, subst
+ else raise (fail_exc metasenv subst context t1 t2)
+
+ | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2))
+ | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
+ let metasenv, subst = aux true metasenv subst context s1 s2 in
+ aux test_eq_only metasenv subst ((name1, C.Decl s1)::context) t1 t2
+ | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
+ let metasenv,subst=aux test_eq_only metasenv subst context ty1 ty2 in
+ let metasenv,subst=aux test_eq_only metasenv subst context s1 s2 in
+ let context = (name1, C.Def (s1,ty1))::context in
+ aux test_eq_only metasenv subst context t1 t2
+
+ | (C.Meta (n1,(s1, C.Irl _)), C.Meta (n2,(s2, C.Irl _)))
+ when n1 = n2 && s1 = s2 -> true
+ | (C.Meta (n1,(s1, l1)), C.Meta (n2,(s2, l2))) when n1 = n2 &&
+ let l1 = NCicUtils.expand_local_context l1 in
+ let l2 = NCicUtils.expand_local_context l2 in
+ (try List.for_all2
+ (fun t1 t2 -> aux test_eq_only context
+ (NCicSubstitution.lift s1 t1)
+ (NCicSubstitution.lift s2 t2))
+ l1 l2
+ with Invalid_argument _ -> assert false) -> true
+
+ | C.Meta (n1,l1), _ ->
+ (try
+ let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
+ let term = NCicSubstitution.subst_meta l1 term in
+ aux test_eq_only context term t2
+ with NCicUtils.Subst_not_found _ -> false)
+ | _, C.Meta (n2,l2) ->
+ (try
+ let _,_,term,_ = NCicUtils.lookup_subst n2 subst in
+ let term = NCicSubstitution.subst_meta l2 term in
+ aux test_eq_only context t1 term
+ with NCicUtils.Subst_not_found _ -> false)
+
+ | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2))
+ when (Ref.eq r1 r2 &&
+ List.length (E.get_relevance r1) >= List.length tl1) ->
+ let relevance = E.get_relevance r1 in
+ let relevance = match r1 with
+ | Ref.Ref (_,Ref.Con (_,_,lno)) ->
+ let _,relevance = HExtlib.split_nth lno relevance in
+ HExtlib.mk_list false lno @ relevance
+ | _ -> relevance
+ in
+ let fail = ref ~-1 in
+ let res = (try
+ HExtlib.list_forall_default3
+ (fun t1 t2 b -> fail := !fail+1; not b || aux test_eq_only context t1 t2)
+ tl1 tl2 true relevance
+ with Invalid_argument _ -> false)
+ in res
+ (* if res then true
+ else
+ let relevance = get_relevance_p ~subst context _hd1 tl1 in
+ let _,relevance = HExtlib.split_nth !fail relevance in
+ let b,relevance = (match relevance with
+ | [] -> assert false
+ | b::tl -> b,tl) in
+ let _,tl1 = HExtlib.split_nth (!fail+1) tl1 in
+ let _,tl2 = HExtlib.split_nth (!fail+1) tl2 in
+ if (not b) then
+ (dance ();
+ try
+ HExtlib.list_forall_default3
+ (fun t1 t2 b -> not b || aux test_eq_only context t1 t2)
+ tl1 tl2 true relevance
+ with Invalid_argument _ -> false)
+ else false *)
+ | (C.Appl (hd1::tl1), C.Appl (hd2::tl2)) ->
+ aux test_eq_only context hd1 hd2 &&
+ let relevance = get_relevance ~subst context hd1 tl1 in
+ (try
+ HExtlib.list_forall_default3
+ (fun t1 t2 b -> not b || aux test_eq_only context t1 t2)
+ tl1 tl2 true relevance
+ with Invalid_argument _ -> false)
+
+ | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
+ C.Match (ref2,outtype2,term2,pl2)) ->
+ let _,_,itl,_,_ = E.get_checked_indtys ref1 in
+ let _,_,ty,_ = List.nth itl tyno in
+ let rec remove_prods ~subst context ty =
+ let ty = 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
+ let rec remove_prods ~subst context ty =
+ let ty = 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
+ if not (Ref.eq ref1 ref2) then
+ raise (uncert_exc metasenv subst context t1 t2)
+ else
+ let metasenv, subst =
+ aux test_eq_only metasenv subst context outtype1 outtype2 in
+ let metasenv, subst =
+ try aux test_eq_only metasenv subst context term1 term2
+ with UnificationFailure _ | Uncertain _ when is_prop ->
+ metasenv, subst
+ in
+ try
+ List.fold_left2
+ (fun (metasenv,subst) -> aux test_eq_only metasenv subst context)
+ (metasenv, subst) pl1 pl2
+ with Invalid_argument _ ->
+ raise (uncert_exc metasenv subst context t1 t2)
+ | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
+ | _ -> raise (uncert_exc metasenv subst context t1 t2)
+ in
+ let unif_machines ...
+
+ in
+ try fo_unif
+ with Uncertain msg as exn ->
+ try unif_machines
+ with
+ | UnificationFailure _ -> raise (UnificationFailure msg)
+ | Uncertain _ -> raise exn
+ in
+ aux false metasenv subst context t1 t2
+
+let are_convertible ?(subst=[]) get_relevance =
+ let rec aux test_eq_only metasenv subst context t1 t2 =
+ in
+ if alpha_eq test_eq_only t1 t2 then
+ true
+ else
+ let height_of = function
+ | C.Const (Ref.Ref (_,Ref.Def h))
+ | C.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
+ | C.Appl(C.Const(Ref.Ref(_,Ref.Def h))::_)
+ | C.Appl(C.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
+ | _ -> 0
+ in
+ let small_delta_step (_,_,t1,_ as m1) (_,_,t2,_ as m2) =
+ let h1 = height_of t1 in
+ let h2 = height_of t2 in
+ let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
+ R.reduce ~delta ~subst context m1,
+ R.reduce ~delta ~subst context m2,
+ delta
+ in
+ let rec convert_machines ((k1,e1,t1,s1 as m1),(k2,e2,t2,s2 as m2),delta) =
+ (alpha_eq test_eq_only
+ (R.unwind (k1,e1,t1,[])) (R.unwind (k2,e2,t2,[])) &&
+ let relevance =
+ match t1 with
+ C.Const r -> NCicEnvironment.get_relevance r
+ | _ -> [] in
+ try
+ HExtlib.list_forall_default3
+ (fun t1 t2 b ->
+ not b ||
+ let t1 = RS.from_stack t1 in
+ let t2 = RS.from_stack t2 in
+ convert_machines (small_delta_step t1 t2)) s1 s2 true relevance
+ with Invalid_argument _ -> false) ||
+ (delta > 0 &&
+ let delta = delta - 1 in
+ let red = R.reduce ~delta ~subst context in
+ convert_machines (red m1,red m2,delta))
+ in
+ convert_machines (small_delta_step (0,[],t1,[]) (0,[],t2,[]))
+ in
+ aux false
+;;
+
+
+
(*
open Printf