I'm trying to build a library that can simplify mathematical expressions. I have an ExpressionPrt type, that is the base for all the different parts of an expression. Different expression parts include Constant, Variable, and Add (addition operator/function). Theses three are probably the most common, so they serve more as warning of future problems rather than problems themselves. But ultimately I want to have more complicated operations such as exponentiation and possibly derivation.

The problem is how to build a dependency tree so that the user of this template library does not have to include the whole thing.

In the case of addition, Add can simplify and organize statements contained in addition expressions, in order. So it can simplifyy+3+x+7to3+7+x+y. It can identify that 3 and 7 are potentially addable (cause they are both constants). But in order to add the two numbers I need a component that knows about both the concepts of addition and of constants. How can I have such a component, without forcing the user to include all addable expression parts whenever he wants to add, and without forcing the user to include the operations that can be preformed on a constant whenever he wants to use constants. Similarly, how do I ensure that a default addition operation exists for those expression parts that cannot be added (that being to not simplify).

My present solution is to just dump all functions that need to know about more than 1 expression part/concept into the base ExpressionPrt class. Obviously this is a bad solution because a)Adding more expression parts requires me to modify the ExpressionPrt class, and b) the default behavior for such operations often requires me to include the associated operation before ExpressionPrt, thus effectively forcing the user to include the entire library in each file that uses any small part.

Help me find a better solution.