/// Rewrites the given model by applying a set of rules to all its constraints, until no more rules can be applied.
/// - Rule engine statistics (e.g. number of rule applications, run time) are collected and stored in the new model's context.
/// 2. If multiple rules can be applied to the same expression, the higher priority one goes first.
/// - This function may panic if the model's context is unavailable or if there is an issue with locking the context.
/// - [`rewrite_iteration`]: Executes a single iteration of rewriting the model using the specified rules.
/// Attempts to apply a set of rules to the given expression and its sub-expressions in the model.
/// 3. If no rules can be applied to the top-level expression, recurses into its sub-expressions.
/// The `Reduction` contains the new expression and any side-effects (e.g., new constraints, variables).
/// - `rules`: A max-heap of [`RuleData`] containing rules, priorities, and metadata. Ordered by rule priority.
/// - `apply_optimizations`: If `true`, skip already "clean" expressions to avoid redundant work.
/// - `Some(<Reduction>)`: If a rule is successfully applied to the expression or any of its sub-expressions.
/// 6. No more rules can be applied to this expression. Mark it as clean and return a pure `Reduction`.
/// We create a list of `RuleResult`s containing the reductions and pass it to `choose_rewrite` to select one to apply.
/// - `stats`: A mutable reference to [`RewriterStats`] used to track the number of rule applications and other statistics.
/// - The function updates the provided `stats` with the number of rule application attempts and successful applications.
/// - Debug or trace logging may be performed to track which rules were applicable or not for a given expression.
/// - [`choose_rewrite`]: Chooses a single reduction from the rule results provided by `apply_all_rules`.
// assert!(!red.new_expression.children().iter().any(|c| c.is_clean()), "Rule that caused assertion to fail: {:?}", rule.name);
// assert!(!red.new_expression.children().iter().any(|c| c.children().iter().any(|c| c.is_clean())));
/// - `Some(<Reduction>)`: If there is at least one successful rule application, returns a [`Reduction`] to apply.