(* ||M|| This file is part of HELM, an Hypertextual, Electronic ||A|| Library of Mathematics, developed at the Computer Science ||T|| Department, University of Bologna, Italy. ||I|| ||T|| HELM is free software; you can redistribute it and/or ||A|| modify it under the terms of the GNU General Public License \ / version 2 or (at your option) any later version. \ / This software is distributed as is, NO WARRANTY. V_______________________________________________________________ *) (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *) exception UnificationFailure of string Lazy.t;; module Founif (B : Terms.Blob) = struct module Subst = FoSubst (*.Subst(B)*) module U = FoUtils.Utils(B) let unification vars locked_vars t1 t2 = let lookup = Subst.lookup_subst in let rec occurs_check subst what where = match where with | Terms.Var i when i = what -> true | Terms.Var i -> let t = lookup i subst in if not (U.eq_foterm t where) then occurs_check subst what t else false | Terms.Node l -> List.exists (occurs_check subst what) l | _ -> false in let rec unif subst s t = let s = match s with Terms.Var i -> lookup i subst | _ -> s and t = match t with Terms.Var i -> lookup i subst | _ -> t in match s, t with | s, t when U.eq_foterm s t -> subst | Terms.Var i, Terms.Var j when (List.mem i locked_vars) &&(List.mem j locked_vars) -> raise (UnificationFailure (lazy "Inference.unification.unif")) | Terms.Var i, Terms.Var j when (List.mem i locked_vars) -> unif subst t s | Terms.Var i, Terms.Var j when (i > j) && not (List.mem j locked_vars) -> unif subst t s | Terms.Var i, t when occurs_check subst i t -> raise (UnificationFailure (lazy "Inference.unification.unif")) | Terms.Var i, t when (List.mem i locked_vars) -> raise (UnificationFailure (lazy "Inference.unification.unif")) | Terms.Var i, t -> let subst = Subst.build_subst i t subst in subst | _, Terms.Var _ -> unif subst t s | Terms.Node l1, Terms.Node l2 -> ( try List.fold_left2 (fun subst' s t -> unif subst' s t) subst l1 l2 with Invalid_argument _ -> raise (UnificationFailure (lazy "Inference.unification.unif")) ) | _, _ -> raise (UnificationFailure (lazy "Inference.unification.unif")) in let subst = unif Subst.id_subst t1 t2 in let vars = Subst.filter subst vars in subst, vars end