A Theory of Markets, Institutions, and Endogenous Preferences