-let fo_unif_new metasenv context t1 t2 =
- let module C = Cic in
- let module R = CicReduction in
- let module S = CicSubstitution in
- let rec fo_unif_aux subst k t1 t2 =
- match (t1, t2) with
- (C.Meta n, C.Meta m) -> if n == m then subst
- else let subst'=
- let tn = try List.assoc n subst
- with Not_found -> C.Meta n in
- let tm = try List.assoc m subst
- with Not_found -> C.Meta m in
- (match (tn, tm) with
- (C.Meta n, C.Meta m) -> if n==m then subst
- else if n<m
- then (m, C.Meta n)::subst
- else (n, C.Meta m)::subst
- | (C.Meta n, tm) -> (n, tm)::subst
- | (tn, C.Meta m) -> (m, tn)::subst
- | (tn,tm) -> fo_unif_aux subst 0 tn tm) in
- (* unify types first *)
- let tyn = List.assoc n metasenv in
- let tym = List.assoc m metasenv in
- fo_unif_aux subst' 0 tyn tym
- | (C.Meta n, t)
- | (t, C.Meta n) -> (* unify types first *)
- let t' = delift k t in
- let subst' =
- (try fo_unif_aux subst 0 (List.assoc n subst) t'
- with Not_found -> (n, t')::subst) in
- let tyn = List.assoc n metasenv in
- let tyt = CicTypeChecker.type_of_aux' metasenv context t' in
- fo_unif_aux subst' 0 tyn tyt
- | (C.Rel _, _)
- | (_, C.Rel _)
- | (C.Var _, _)
- | (_, C.Var _)
- | (C.Sort _ ,_)
- | (_, C.Sort _)
- | (C.Implicit, _)
- | (_, C.Implicit) -> if R.are_convertible t1 t2 then subst
- else raise UnificationFailed
- | (C.Cast (te,ty), t2) -> fo_unif_aux subst k te t2
- | (t1, C.Cast (te,ty)) -> fo_unif_aux subst k t1 te
- | (C.Prod (_,s1,t1), C.Prod (_,s2,t2)) ->
- let subst' = fo_unif_aux subst k s1 s2 in
- fo_unif_aux subst' (k+1) t1 t2
- | (C.Lambda (_,s1,t1), C.Lambda (_,s2,t2)) ->
- let subst' = fo_unif_aux subst k s1 s2 in
- fo_unif_aux subst' (k+1) t1 t2
- | (C.LetIn (_,s1,t1), t2) -> fo_unif_aux subst k (S.subst s1 t1) t2
- | (t1, C.LetIn (_,s2,t2)) -> fo_unif_aux subst k t1 (S.subst s2 t2)
- | (C.Appl l1, C.Appl l2) ->
- let lr1 = List.rev l1 in
- let lr2 = List.rev l2 in
- let rec fo_unif_l subst = function
- [],_
- | _,[] -> assert false
- | ([h1],[h2]) -> fo_unif_aux subst k h1 h2
- | ([h],l)
- | (l,[h]) -> fo_unif_aux subst k h (C.Appl l)
- | ((h1::l1),(h2::l2)) ->
- let subst' = fo_unif_aux subst k h1 h2 in
- fo_unif_l subst' (l1,l2)
- in
- fo_unif_l subst (lr1, lr2)
- | (C.Const _, _)
- | (_, C.Const _)
- | (C.Abst _, _)
- | (_, C.Abst _)
- | (C.MutInd _, _)
- | (_, C.MutInd _)
- | (C.MutConstruct _, _)
- | (_, C.MutConstruct _) -> if R.are_convertible t1 t2 then subst
- else raise UnificationFailed
- | (C.MutCase (_,_,_,outt1,t1,pl1), C.MutCase (_,_,_,outt2,t2,pl2))->
- let subst' = fo_unif_aux subst k outt1 outt2 in
- let subst'' = fo_unif_aux subst' k t1 t2 in
- List.fold_left2 (function subst -> fo_unif_aux subst k) subst'' pl1 pl2
- | (C.Fix _, _)
- | (_, C.Fix _)
- | (C.CoFix _, _)
- | (_, C.CoFix _) -> if R.are_convertible t1 t2 then subst
- else raise UnificationFailed
- | (_,_) -> raise UnificationFailed
- in fo_unif_aux [] 0 t1 t2;;
+let rec fo_unif_subst subst context metasenv t1 t2 =
+ let module C = Cic in
+ let module R = CicReduction in
+ let module S = CicSubstitution in
+ match (t1, t2) with
+ (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
+ let ok =
+ List.fold_left2
+ (fun b t1 t2 ->
+ b &&
+ match t1,t2 with
+ None,_
+ | _,None -> true
+ | Some t1', Some t2' ->
+ (* First possibility: restriction *)
+ (* Second possibility: unification *)
+ (* Third possibility: convertibility *)
+ R.are_convertible context t1' t2'
+ ) true ln lm
+ in
+ if ok then subst,metasenv else raise UnificationFailed
+ | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
+ fo_unif_subst subst context metasenv t2 t1
+ | (C.Meta (n,l), t)
+ | (t, C.Meta (n,l)) ->
+ let subst',metasenv' =
+ try
+ let oldt = (List.assoc n subst) in
+ let lifted_oldt = S.lift_meta l oldt in
+ fo_unif_subst subst context metasenv lifted_oldt t
+ with Not_found ->
+ let t',metasenv' = delift context metasenv l t in
+ (n, t')::subst, metasenv'
+ in
+ let (_,_,meta_type) =
+ List.find (function (m,_,_) -> m=n) metasenv' in
+ let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
+ fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
+ | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
+ | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
+ if UriManager.eq uri1 uri2 then
+ fo_unif_subst_exp_named_subst subst context metasenv
+ exp_named_subst1 exp_named_subst2
+ else
+ raise UnificationFailed
+ | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
+ if UriManager.eq uri1 uri2 && i1 = i2 then
+ fo_unif_subst_exp_named_subst subst context metasenv
+ exp_named_subst1 exp_named_subst2
+ else
+ raise UnificationFailed
+ | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
+ C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
+ if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
+ fo_unif_subst_exp_named_subst subst context metasenv
+ exp_named_subst1 exp_named_subst2
+ else
+ raise UnificationFailed
+ | (C.Rel _, _)
+ | (_, C.Rel _)
+ | (C.Var _, _)
+ | (_, C.Var _)
+ | (C.Sort _ ,_)
+ | (_, C.Sort _)
+ | (C.Implicit, _)
+ | (_, C.Implicit) ->
+ if R.are_convertible context t1 t2 then
+ subst, metasenv
+ else
+ raise UnificationFailed
+ | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
+ | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
+ | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
+ let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
+ fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
+ | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
+ let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
+ fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
+ | (C.LetIn (_,s1,t1), t2)
+ | (t2, C.LetIn (_,s1,t1)) ->
+ fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
+ | (C.Appl l1, C.Appl l2) ->
+ let lr1 = List.rev l1 in
+ let lr2 = List.rev l2 in
+ let rec fo_unif_l subst metasenv =
+ function
+ [],_
+ | _,[] -> assert false
+ | ([h1],[h2]) ->
+ fo_unif_subst subst context metasenv h1 h2
+ | ([h],l)
+ | (l,[h]) ->
+ fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
+ | ((h1::l1),(h2::l2)) ->
+ let subst', metasenv' =
+ fo_unif_subst subst context metasenv h1 h2
+ in
+ fo_unif_l subst' metasenv' (l1,l2)
+ in
+ fo_unif_l subst metasenv (lr1, lr2)
+ | (C.Const _, _)
+ | (_, C.Const _)
+ | (C.MutInd _, _)
+ | (_, C.MutInd _)
+ | (C.MutConstruct _, _)
+ | (_, C.MutConstruct _) ->
+ if R.are_convertible context t1 t2 then
+ subst, metasenv
+ else
+ raise UnificationFailed
+ | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
+ let subst', metasenv' =
+ fo_unif_subst subst context metasenv outt1 outt2 in
+ let subst'',metasenv'' =
+ fo_unif_subst subst' context metasenv' t1 t2 in
+ List.fold_left2
+ (function (subst,metasenv) ->
+ fo_unif_subst subst context metasenv
+ ) (subst'',metasenv'') pl1 pl2
+ | (C.Fix _, _)
+ | (_, C.Fix _)
+ | (C.CoFix _, _)
+ | (_, C.CoFix _) ->
+ if R.are_convertible context t1 t2 then
+ subst, metasenv
+ else
+ raise UnificationFailed
+ | (_,_) ->
+ if R.are_convertible context t1 t2 then
+ subst, metasenv
+ else
+ raise UnificationFailed
+
+and fo_unif_subst_exp_named_subst subst context metasenv
+ exp_named_subst1 exp_named_subst2
+=
+try
+ List.fold_left2
+ (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
+ assert (uri1=uri2) ;
+ fo_unif_subst subst context metasenv t1 t2
+ ) (subst,metasenv) exp_named_subst1 exp_named_subst2
+with
+e ->
+let uri = UriManager.uri_of_string "cic:/dummy.var" in
+prerr_endline ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
+" <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
+;;
+
+(*CSC: ???????????????
+(* m is the index of a metavariable to restrict, k is nesting depth
+of the occurrence m, and l is its relocation list. canonical_context
+is the context of the metavariable we are instantiating - containing
+m - Only rel in the domain of canonical_context are accessible.
+This function takes in input a metasenv and gives back a metasenv.
+A rel(j) in the canonical context of m, is rel(List.nth l j) for the
+instance of m under consideration, that is rel (List.nth l j) - k
+in canonical_context. *)
+
+let restrict canonical_context m k l =
+ let rec erase i =
+ function
+ [] -> []
+ | None::tl -> None::(erase (i+1) tl)
+ | he::tl ->
+ let i' = (List.nth l (i-1)) in
+ if i' <= k
+ then he::(erase (i+1) tl) (* local variable *)
+ else
+ let acc =
+ (try List.nth canonical_context (i'-k-1)
+ with Failure _ -> None) in
+ if acc = None
+ then None::(erase (i+1) tl)
+ else he::(erase (i+1) tl) in
+ let rec aux =
+ function
+ [] -> []
+ | (n,context,t)::tl when n=m -> (n,erase 1 context,t)::tl
+ | hd::tl -> hd::(aux tl)
+ in
+ aux
+;;