Foundations of a general theory of sets