'Unify representations of two algebraic numbers.'

def unify(f, g):

    if ((not isinstance(g, ANP)) or (f.mod != g.mod)):
        raise UnificationFailed(("can't    unify    %s    with    %s" % (f, g)))
    if (f.dom == g.dom):
        return (f.dom, f.per, f.rep, g.rep, f.mod)
    else:
        dom = f.dom.unify(g.dom)
        F = dup_convert(f.rep, f.dom, dom)
        G = dup_convert(g.rep, g.dom, dom)
        if ((dom != f.dom) and (dom != g.dom)):
            mod = dup_convert(f.mod, f.dom, dom)
        elif (dom == f.dom):
            mod = f.mod
        else:
            mod = g.mod
        per = (lambda rep: ANP(rep, mod, dom))
    return (dom, per, F, G, mod)
