MPP

Mħπ Rules System

The Mħπ rules system provides a flexible and extensible way to define and apply mathematical transformations. This system is at the core of the symbolic computation engine, allowing for everything from basic algebraic simplification to complex operations specific to mathematical physics.

Rules are defined to match specific patterns in an expression and then apply a transformation. This allows for a declarative style of programming where the mathematical properties of the system are expressed as a set of rules.

Implemented Rules

The following is a list of the rules that have been implemented in the new system so far, categorized by their domain.

Algebraic Rules (src/rules/algebraic.rs)

These rules define fundamental algebraic properties of operations.

Evaluative Rules (src/rules/evaluative.rs)

These rules compute definite numeric results from expressions.

Calculus Rules (src/rules/calculus.rs)

Trigonometric Rules (src/rules/trigonometric.rs)

Quantum & Gauge Rules (src/rules/quantum.rs, src/rules/gauge.rs)

These rules are specific to quantum mechanics, QFT, and gauge theories.

Adding New Rules

To extend the capabilities of the Mħπ engine, you can add new rules. This section details the properties of a rule and the process for implementing a new one.

Rule Properties

Each rule has several properties that define its behavior and its place in the simplification process. These are defined by the SimplificationRule trait.

Implementation

To implement a new rule, you need to create a Rust struct and implement the SimplificationRule trait for it.

Here is a template for a new rule:

use crate::rules::{SimplificationRule, RuleKind, RuleDomain, ApplicationPass};
use crate::expr::SymbolicExpr;
use crate::calculus::Context;

pub struct MyNewRule;

impl SimplificationRule for MyNewRule {
    fn name(&self) -> &'static str { "MyNewRule" }
    fn description(&self) -> &'static str { "A brief description of my new rule." }
    fn long_description(&self) -> &'static str {
        "A more detailed description of what this rule does and how it works."
    }

    fn kind(&self) -> RuleKind { RuleKind::Simplification }
    fn domain(&self) -> RuleDomain { RuleDomain::Algebraic }

    fn priority(&self) -> u32 { 100 } // Higher numbers run first
    fn passes(&self) -> Vec<ApplicationPass> { vec![ApplicationPass::Main] }

    fn applies_to(&self, expr: &SymbolicExpr, ctx: &Context) -> bool {
        // Logic to check if the rule can be applied to the given expression.
        // For example, pattern matching on the expression structure.
        // This check should be as fast as possible.
        true // Placeholder
    }

    fn apply(&self, expr: &SymbolicExpr, ctx: &mut Context) -> Option<SymbolicExpr> {
        // Logic to transform the expression.
        // If the rule applies, return Some(new_expr).
        // Otherwise, return None.
        None // Placeholder
    }
}

Steps to add a new rule:

  1. Create a new file for your rule in the appropriate module (e.g., src/rules/algebraic.rs), or add to an existing one.
  2. Define your rule struct and implement the SimplificationRule trait.
  3. Fill in the properties for your rule, including name, kind, domain, priority, and passes.
  4. Implement the applies_to method to define the pattern your rule should match. This is crucial for performance, as it prevents the apply method from being called unnecessarily.
  5. Implement the apply method to define the transformation that your rule performs.
  6. Register your rule in the RuleRegistry::new() function in src/rules/mod.rs.
  7. Add your rule’s name to the appropriate preset list(s) in src/rules/presets.rs.
  8. Write tests for your new rule, ensuring it works correctly and doesn’t cause unintended side effects. By following this structure, you can seamlessly integrate new mathematical knowledge into the Mħπ system.