Mathematical Foundations of Qualitative Reasoning