Type inference computes an AST object’s type from surrounding context, rather than only from the object and its descendants. Ancestors supply that context, letting a type system determine a number’s type from the attribute that contains it. section 13.1 section 13.4
Implement lightweight contextual inference by:
- Pass an ancestor array through typeOf while traversing AST objects. section 13.1
- Inspect enclosing objects when a local expression lacks enough type information, such as a Number contained directly in an Attribute. section 13.1
- Return a sensible best-effort type when possible; otherwise return untype so an uncertain result does not spread mismatches through the expression. section 13.4
- Keep type logic in the type system, then call it from projection code instead of relying on a fixed AST shape. section 13.1
Rental price expressions
A rental-price expression can combine numeric values with “-” and “of”. When projecting a Number, typeOf receives its ancestors and infers the number type from its containing attribute, allowing the editor to project it without embedding attribute-specific logic. section 13.1
Figure 13.5 The type computation of the rental price expression, in terms of the AST
Inspect the rental-price type computation as an AST to see where ancestor context complements subexpression-based type computation. section 13.1
Constraint-based inference can go further: MPS produces type equalities and inequalities and solves them algorithmically, inferring types where a direct computation would return untype. That power requires more implementation code. section 15.4.2















