← App. E Appendix F App. G →

Appendix F: Source Code Snippets for the Syntactic Reality Engine Prototype

F.1 Overview

The Syntactic Reality Engine (SRE) is an open‑source Python library for exploring the Syntactic Token Calculus. This appendix provides key code snippets that implement:

  1. Token reduction (Calling and Crossing).
  2. Cross‑ratio calculation.
  3. Normal‑form enumeration.
  4. Validation suite.

F.2 Token Reduction Algorithm


# sre/reduction.py

def reduce_expr(expr: str) -> str:
    """
    Reduce a syntactic expression to its normal form using
    Calling (## → #) and Crossing ([[A]] → A).
    
    Parameters
    ----------
    expr : str
        A string containing only '#', '[', and ']'.
        Brackets must be balanced.
    
    Returns
    -------
    str
        The unique normal form.
    """
    # Step 1: eliminate all '##' (Calling)
    while '##' in expr:
        expr = expr.replace('##', '#')
    
    # Step 2: eliminate all '[[...]]' (Crossing)
    # Use a stack to find innermost double brackets
    old = ''
    while old != expr:
        old = expr
        stack = []
        i = 0
        while i < len(expr):
            if expr[i] == '[':
                stack.append(i)
                i += 1
            elif expr[i] == ']':
                if len(stack) >= 2:
                    start2 = stack.pop()  # inner '['
                    start1 = stack.pop()  # outer '['
                    # Check that we have '[[ ... ]]' with exactly two '[' at start
                    if (expr[start1:start1+2] == '[[' and
                        expr[i-1:i+1] == ']]' and
                        expr[start1+2] != '['):  # ensure not triple bracket
                        # Extract inner expression A
                        inner = expr[start2+1:i-1]
                        # Replace '[[A]]' with 'A'
                        expr = expr[:start1] + inner + expr[i+1:]
                        # Restart scanning because indices changed
                        break
                    else:
                        # Not a valid double bracket, restore stack
                        stack.append(start1)
                        stack.append(start2)
                i += 1
            else:  # '#'
                i += 1
        else:
            # No replacement happened in this pass
            break
    
    return expr


def is_normal_form(expr: str) -> bool:
    """Check if an expression is irreducible."""
    # Quick checks
    if '##' in expr:
        return False
    # Look for '[[...]]' pattern
    stack = []
    for i, ch in enumerate(expr):
        if ch == '[':
            stack.append(i)
        elif ch == ']':
            if len(stack) >= 2:
                start2 = stack.pop()
                start1 = stack.pop()
                if expr[start1:start1+2] == '[[' and expr[i-1:i+1] == ']]':
                    return False
                # restore
                stack.append(start1)
                stack.append(start2)
    return True

F.3 Cross‑Ratio Calculator


# sre/cross_ratio.py

from .reduction import reduce_expr

def syntactic_cross_ratio(A: str, B: str, C: str, D: str) -> str:
    """
    Compute the syntactic cross‑ratio χ(A,B,C,D).
    
    Returns the normal form of [ [ A B ] [ C D ] ].
    """
    # Build the arrangement
    expr = f'[[{A}{B}][{C}{D}]]'  # juxtaposition = concatenation
    # Ensure proper bracketing: each pair inside its own enclosure
    expr = f'[ [{A}{B}] [{C}{D}] ]'
    return reduce_expr(expr)


def mass_pattern(P: str) -> str:
    """Mass pattern χ(P, #, blank, #)."""
    # blank is empty string
    return syntactic_cross_ratio(P, '#', '', '#')


def charge_pattern(P: str) -> str:
    """Charge pattern χ(P, [#], #, #)."""
    return syntactic_cross_ratio(P, '[#]', '#', '#')


def spin_pattern(P: str) -> str:
    """Spin pattern χ(P, P, blank, #)."""
    return syntactic_cross_ratio(P, P, '', '#')

F.4 First‑Generation Particle Validation


# sre/validation.py

from .cross_ratio import mass_pattern, charge_pattern, spin_pattern

FIRST_GEN = {
    'photon': '[#]',
    'electron': '[# [#]]',
    'up_quark': '[[#] #]',
    'down_quark': '[[#] [#] #]',
    'W_boson': '[[#] [#]]',
    'Z_boson': '[[#] [#] [#]]',
}

def validate_first_generation():
    """Compute property patterns for all first‑gen particles."""
    results = {}
    for name, pattern in FIRST_GEN.items():
        results[name] = {
            'pattern': pattern,
            'mass': mass_pattern(pattern),
            'charge': charge_pattern(pattern),
            'spin': spin_pattern(pattern),
        }
    return results


def check_identities():
    """Verify key identities (e.g., photon charge = spin)."""
    data = validate_first_generation()
    # Photon charge == spin
    assert data['photon']['charge'] == data['photon']['spin'], \
        "Photon charge ≠ spin"
    # Z and Higgs share same pattern (already enforced by dict)
    print("All identities hold.")

F.5 Normal‑Form Enumeration


# sre/enumeration.py

from itertools import product

def generate_normal_forms(max_complexity: int):
    """
    Generate all normal forms up to given complexity.
    
    Complexity = number of marks + number of bracket pairs.
    """
    # We'll generate expressions by constructing all strings of
    # '#', '[', ']' up to length L, then filter balanced and irreducible.
    # This is brute‑force and only feasible for small complexities.
    
    forms = []
    for length in range(1, max_complexity + 3):  # extra for brackets
        # Generate all strings of length L over alphabet {'#','[',']'}
        for chars in product('#[]', repeat=length):
            expr = ''.join(chars)
            # Quick filter: brackets must balance
            if expr.count('[') != expr.count(']'):
                continue
            # Check balance properly (stack)
            stack = 0
            ok = True
            for ch in expr:
                if ch == '[':
                    stack += 1
                elif ch == ']':
                    stack -= 1
                    if stack < 0:
                        ok = False
                        break
            if not ok or stack != 0:
                continue
            # Reduce and see if irreducible
            from .reduction import reduce_expr, is_normal_form
            nf = reduce_expr(expr)
            if is_normal_form(nf):
                forms.append(nf)
    # Remove duplicates and sort by complexity
    forms = list(set(forms))
    forms.sort(key=lambda x: (x.count('#') + x.count('['), x))
    return forms


def enumerate_particles(max_complexity=10):
    """Enumerate normal forms and compute their properties."""
    forms = generate_normal_forms(max_complexity)
    results = []
    for expr in forms:
        results.append({
            'pattern': expr,
            'complexity': expr.count('#') + expr.count('['),
            'mass': mass_pattern(expr),
            'charge': charge_pattern(expr),
            'spin': spin_pattern(expr),
        })
    return results

F.6 UltraCluster Library (Core Functions)


# ultrastic/ultrametric.py

import numpy as np
from scipy.spatial.distance import squareform

def strong_triangle_inequality_holds(D):
    """
    Check whether a distance matrix D satisfies the strong triangle inequality.
    
    Parameters
    ----------
    D : array_like, shape (n, n)
        Symmetric distance matrix.
    
    Returns
    -------
    bool
        True if D is ultrametric.
    """
    n = D.shape[0]
    for i in range(n):
        for j in range(n):
            for k in range(n):
                if D[i, k] > max(D[i, j], D[j, k]):
                    return False
    return True


def ultrametricity_coefficient(D):
    """
    Compute the fraction of triples that satisfy the strong triangle inequality.
    """
    n = D.shape[0]
    total = 0
    satisfied = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                total += 1
                if D[i, k] <= max(D[i, j], D[j, k]):
                    satisfied += 1
    return satisfied / total if total > 0 else 1.0


def p_adric_embedding(D, p=2):
    """
    Embed data into a p‑adic tree (Bruhat‑Tits tree) using hierarchical clustering.
    
    Returns tree structure (dendrogram) and p‑adic coordinates.
    """
    from scipy.cluster.hierarchy import linkage, dendrogram
    # Perform single‑linkage clustering (ultrametric)
    Z = linkage(squareform(D), method='single')
    # Extract ultrametric distances
    ultrametric_dist = cophenet(Z, squareform(D))
    # Convert to p‑adic coordinates (simplified)
    # Each leaf gets a p‑adic integer representing its path from root
    n = D.shape[0]
    coords = []
    # ... (implementation details omitted)
    return Z, coords

F.7 Unit Tests


# tests/test_reduction.py

import pytest
from sre.reduction import reduce_expr, is_normal_form

def test_calling():
    assert reduce_expr('##') == '#'
    assert reduce_expr('###') == '#'
    assert reduce_expr('# ## #') == '##'  # becomes '##'? Wait, '##' reduces to '#', so '# #' -> '##'? Actually '# ## #' = '#', '##', '#'. Reduce inner '##' to '#', giving '# # #' -> '###' -> '#'.
    # Let's write proper tests:
    assert reduce_expr('##') == '#'
    assert reduce_expr('###') == '#'
    assert reduce_expr('# ## #') == '#'

def test_crossing():
    assert reduce_expr('[[]]') == ''
    assert reduce_expr('[[#]]') == '#'
    assert reduce_expr('[[[#]]]') == '[#]'
    assert reduce_expr('[[[[#]]]]') == '#'

def test_mixed():
    assert reduce_expr('[# [#]]') == '[# [#]]'  # irreducible
    assert reduce_expr('[[#] [#]]') == '[[#] [#]]'  # irreducible

def test_normal_form():
    assert is_normal_form('[#]')
    assert is_normal_form('[# [#]]')
    assert not is_normal_form('##')
    assert not is_normal_form('[[#]]')

F.8 Installation and Usage

Install from GitHub:


git clone 
cd sre
pip install -e .

Basic usage in a Jupyter notebook:


from sre import reduce_expr, mass_pattern, enumerate_particles

# Reduce an expression
print(reduce_expr('[[#] [#]]'))  # → '[[#] [#]]' (W boson)

# Compute mass pattern for electron
print(mass_pattern('[# [#]]'))

# Enumerate particles up to complexity 8
particles = enumerate_particles(8)
for p in particles[:5]:
    print(p['pattern'], p['complexity'])

← App. E Appendix F App. G →