MPP

๐Ÿ”ฃ MPP-TeX Language

MPP-TeX is a TeX-inspired symbolic math language designed for writing ฯ€- and P-based expressions with a focus on fundamental physical constants and mathematical structures.

๐Ÿ“˜ Syntax

\let r := \P
\let A := \pi r^2
\derive{A}{r}
\ket{\psi} := \frac{1}{\sqrt{2}}(\ket{0} + \ket{1})
\interval{\tau}{0}{0}{0}

๐Ÿ“‘ Tokens and Constants

Token Meaning
\pi Symbol for pi (geometry constant)
\P Symbol for Planck length
\tau Planck time constant
\c Speed of light constant (defined as P/ฯ„)
\Delta, \ptu Planck Time Unit (defined as P/c)
\hbar Reduced Planck constant
\G Gravitational constant
\k Boltzmann constant
\second Second (legacy unit)

๐Ÿ“ Operations

Operation Syntax Description
Assignment \let x := expr Assign value to variable
Addition a + b Add expressions
Subtraction a - b Subtract expressions
Multiplication a \cdot b, a * b, a b Multiply expressions (implicit)
Division a / b, \frac{a}{b} Divide expressions
Exponentiation a^b Raise to power
Derivative \derive{expr}{var} Derivative of expr with respect to var
Integration \integral{expr}{var} Indefinite integral of expr with respect to var

๐Ÿงช Quantum Mechanics

Operation Syntax Description
Ket vector \ket{ฯˆ} Quantum state vector
Bra vector \bra{ฯˆ} Dual vector
Inner product \braket{ฯˆ}{ฯ†} Inner product between two states
Commutator \commutator{A}{B} Commutator [A,B] = AB - BA
Anti-commutator \anticommutator{A}{B} Anti-commutator {A,B} = AB + BA

๐Ÿš€ Physics Operations

Operation Syntax Description
Lagrangian \lagrangian{L} Lagrangian function
Hamiltonian \hamiltonian{H} Hamiltonian function
Vector notation \vec{v} Vector notation
Spacetime interval \interval{t}{x}{y}{z} 4D spacetime interval

๐Ÿ› ๏ธ Utility Operations

Operation Syntax Description
Text \text{comment} Text for documentation
Assert \assert{expr} Assertion for validation
Solve \solve{expr}{var} Solve equation for variable

๐Ÿ” Parser Implementation

The MPP-TeX parser is implemented using the nom parsing library, which provides:

  1. Efficient parser combinators
  2. Comprehensive error reporting
  3. Robust handling of ambiguous syntax
  4. Support for complex grammar with nested structures

The parser processes input in multiple stages:

  1. Lexical analysis (tokenization)
  2. Syntactic analysis (parsing tokens into an abstract syntax tree)
  3. Semantic analysis (interpreting the AST into SymbolicExpr objects)

Error Handling

The parser provides detailed error messages including:

Example error:

Parse error at line 3, column 7: Expected ')' but found '+'

๐Ÿ“ Formal Grammar and Precedence

The grammar is designed around a standard operator hierarchy, from lowest to highest precedence. This ensures that expressions like a + b * c are parsed unambiguously as a + (b * c).

Operator Precedence Table

Precedence Operator(s) Associativity Description
1 (Lowest) := - Assignment
2 +, - Left Addition / Subtraction
3 *, /, (implicit) Left Multiplication / Division
4 +, - (unary) Right Unary Plus / Minus
5 (Highest) ^ Right Exponentiation

Parentheses () and braces {} are used to override the natural precedence.

EBNF Grammar Specification

This Extended Backus-Naur Form (EBNF) grammar formally defines the syntactic structure of MPP-TeX.

(* --- Top Level --- *)
program      ::= statement*
statement    ::= assignment | expression

(* --- Core Expressions --- *)
assignment   ::= '\let' identifier ':=' expression
expression   ::= additive_expr

(* --- Binary Operations (by precedence) --- *)
additive_expr ::= multiplicative_expr ( ( '+' | '-' ) multiplicative_expr )*

multiplicative_expr ::= unary_expr ( ( '*' | '\cdot' | '/' | implicit_mul ) unary_expr )*

unary_expr    ::= ( '+' | '-' ) unary_expr
                | power_expr

power_expr    ::= primary_expr ( '^' power_expr )?  (* Right-associative *)

(* --- Primitives and Grouping --- *)
primary_expr  ::= literal
                | identifier
                | command
                | function_call
                | '(' expression ')'

(* --- Specific Forms --- *)
literal       ::= rational | integer
rational      ::= integer '/' integer
integer       ::= [0-9]+

function_call ::= command ( group )+
group         ::= '{' expression '}'
identifier    ::= [a-zA-Z][a-zA-Z0-9_]*

(* --- Implicit Multiplication Rule --- *)
(* This is a contextual rule: whitespace or juxtaposition between two `unary_expr` *)
(* that are not separated by another operator implies multiplication. *)
(* e.g., "2\pi", "x y", "(a+b)(c+d)" *)
implicit_mul  ::= (* e.g., whitespace between valid multiplicative terms *)

Examples

Basic Mathematics

\let r := 5 \cdot \P
\let area := \pi r^2
\derive{area}{r}  // Should yield 2\pi r

Quantum Physics

\let \psi := \frac{1}{\sqrt{2}}(\ket{0} + \ket{1})
\let \phi := \frac{1}{\sqrt{2}}(\ket{0} - \ket{1})
\braket{\psi}{\phi}  // Should yield 0

Relativity

\let dt := \c \cdot \tau
\let dx := 0
\let dy := 0
\let dz := 0
\interval{dt}{dx}{dy}{dz}  // Timelike interval

Classical Mechanics

\let L := \frac{1}{2}m\vec{v}^2 - V(x)
\lagrangian{L}
\hamiltonian{p^2/(2m) + V(x)}