From b68325537e9e42c5da370c9f053fa99dba8a55cd Mon Sep 17 00:00:00 2001 From: Enrico Tassi Date: Wed, 24 Sep 2008 14:48:49 +0000 Subject: [PATCH] ... --- .../components/ng_refiner/nCicMetaSubst.ml | 12 +- .../components/ng_refiner/nCicUnification.ml | 211 ++++++++++++++++++ .../components/ng_refiner/nCicUnification.mli | 9 + 3 files changed, 227 insertions(+), 5 deletions(-) diff --git a/helm/software/components/ng_refiner/nCicMetaSubst.ml b/helm/software/components/ng_refiner/nCicMetaSubst.ml index c7d66dd06..5267796ef 100644 --- a/helm/software/components/ng_refiner/nCicMetaSubst.ml +++ b/helm/software/components/ng_refiner/nCicMetaSubst.ml @@ -222,7 +222,7 @@ let rec force_does_not_occur metasenv subst restrictions t = restrict metasenv subst n restrictions_for_n in (metasenv, subst), NCic.Meta (newmeta, l') - | t -> NCicUtils.map_term_fold_a (fun _ k -> k+1) k aux ms t + | t -> NCicUntrusted.map_term_fold_a (fun _ k -> k+1) k aux ms t in aux 0 (metasenv,subst) t @@ -380,7 +380,7 @@ let delift metasenv subst context n l t = let metasenv, subst, newmeta = restrict metasenv subst i to_be_r in (metasenv, subst), NCic.Meta(newmeta,l1)) - | t -> NCicUtils.map_term_fold_a (fun _ k -> k+1) k aux ms t + | t -> NCicUntrusted.map_term_fold_a (fun _ k -> k+1) k aux ms t in try aux 0 (metasenv,subst) t with NotInTheList -> @@ -395,11 +395,13 @@ let delift metasenv subst context n l t = ~subst ~context) (let shift, lc = l in List.map (NCicSubstitution.lift shift) (NCicUtils.expand_local_context lc)))))) in + let shift, lc = l in + let lc = NCicUtils.expand_local_context lc in + let l = List.map (NCicSubstitution.lift shift) lc in if List.exists - (function - Some t -> CicUtil.is_meta_closed (apply_subst subst t) - | None -> true) l + (fun t -> NCicUntrusted.metas_of_term subst context t = []) + l then raise (Uncertain msg) else diff --git a/helm/software/components/ng_refiner/nCicUnification.ml b/helm/software/components/ng_refiner/nCicUnification.ml index 2643ce843..9c456cd87 100644 --- a/helm/software/components/ng_refiner/nCicUnification.ml +++ b/helm/software/components/ng_refiner/nCicUnification.ml @@ -10,6 +10,217 @@ 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 diff --git a/helm/software/components/ng_refiner/nCicUnification.mli b/helm/software/components/ng_refiner/nCicUnification.mli index d5a742279..97976b7a3 100644 --- a/helm/software/components/ng_refiner/nCicUnification.mli +++ b/helm/software/components/ng_refiner/nCicUnification.mli @@ -11,6 +11,15 @@ (* $Id$ *) +exception UnificationFailure of string Lazy.t;; +exception Uncertain of string Lazy.t;; +exception AssertFailure of string Lazy.t;; + +val unify : + NCic.metasenv -> NCic.substitution -> NCic.context -> + NCic.term -> NCic.term -> + NCic.metasenv * NCic.substitution + (* exception UnificationFailure of string Lazy.t;; exception Uncertain of string Lazy.t;; -- 2.39.2