-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistinction_calculus.py
More file actions
192 lines (157 loc) · 5.32 KB
/
Copy pathdistinction_calculus.py
File metadata and controls
192 lines (157 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
Distinction Calculus — Spencer-Brown's primitives for Python.
Marks (#), Enclosures ([ ]), Calling (## → #), Crossing ([[A]] → A).
No floating-point. Pure syntactic pattern operations.
"""
from typing import List, Tuple, Optional, Set, Dict
from dataclasses import dataclass
from enum import Enum
import re
from collections import defaultdict
class Token(Enum):
MARK = "#" # The mark: "something is here"
ENCLOSE_L = "[" # Left enclosure boundary
ENCLOSE_R = "]" # Right enclosure boundary
VOID = "" # The absence of any mark
@dataclass
class Expr:
"""A syntactic expression — marks and nested enclosures."""
raw: str # The string representation, e.g. "[# [#]]"
def __post_init__(self):
self._validate()
def _validate(self):
depth = 0
for c in self.raw:
if c == '[':
depth += 1
elif c == ']':
depth -= 1
if depth < 0:
raise ValueError(f"Unbalanced enclosures in: {self.raw}")
if depth != 0:
raise ValueError(f"Unclosed enclosures in: {self.raw}")
@property
def depth(self) -> int:
"""Maximum nesting depth."""
current = 0
max_depth = 0
for c in self.raw:
if c == '[':
current += 1
max_depth = max(max_depth, current)
elif c == ']':
current -= 1
return max_depth
@property
def mark_count(self) -> int:
"""Number of marks (#) in the expression."""
return self.raw.count('#')
def reduce_calling(self) -> 'Expr':
"""Apply Calling: ## → #"""
result = self.raw
while '##' in result:
result = result.replace('##', '#')
return Expr(result)
def reduce_crossing(self) -> 'Expr':
"""Apply Crossing: [[A]] → A (remove redundant double enclosures)."""
result = self.raw
# Pattern: [[ followed by anything balanced, then ]]
pattern = re.compile(r'\[\[([^\[\]]*(?:\[[^\]]*\][^\[\]]*)*)\]\]')
prev = None
while prev != result:
prev = result
result = pattern.sub(r'\1', result)
return Expr(result)
def normal_form(self) -> 'Expr':
"""Reduce to normal form by exhaustive Calling + Crossing."""
current = self.reduce_calling().reduce_crossing()
prev = None
while prev != current.raw:
prev = current.raw
current = current.reduce_calling().reduce_crossing()
return current
def is_stable(self) -> bool:
"""Is this expression a stable (irreducible) pattern?"""
nf = self.normal_form()
return nf.raw == self.raw
def to_tree_path(self) -> List[int]:
"""
Convert to a tree path (sequence of nesting levels).
Each # contributes a mark at the current nesting depth.
"""
path = []
depth = 0
i = 0
while i < len(self.raw):
c = self.raw[i]
if c == '[':
depth += 1
elif c == ']':
depth -= 1
elif c == '#':
path.append(depth)
i += 1
return path
def __str__(self):
return self.raw
def __repr__(self):
return f"Expr({self.raw!r})"
def juxtapose(*exprs: Expr) -> Expr:
"""Juxtapose multiple expressions side by side."""
combined = ''.join(e.raw for e in exprs)
return Expr(combined)
def enclose(expr: Expr) -> Expr:
"""Wrap an expression in an enclosure."""
return Expr(f"[{expr.raw}]")
def mark() -> Expr:
"""Create a single mark."""
return Expr("#")
# Known stable particle patterns (from QLoF monograph)
STABLE_PATTERNS = {
"photon": Expr("[#]"),
"electron": Expr("[# [#]]"),
"up_quark": Expr("[[#] #]"),
"down_quark": Expr("[[#] [#] #]"),
"w_boson": Expr("[[#] [#]]"),
"z_higgs": Expr("[[#] [#] [#]]"),
}
# Semantic primes encoded as patterns
SEMANTIC_PRIME_PATTERNS = {
"good": Expr("[#]"),
"bad": Expr("[# [#]]"),
"not": Expr("[[#]]"),
"very": Expr("[##]"),
"but": Expr("[# [#] [#]]"),
}
def tree_distance(e1: Expr, e2: Expr) -> int:
"""
Compute ultrametric distance between two expressions
as the number of Crossing steps to reach their LCA.
"""
p1 = e1.to_tree_path()
p2 = e2.to_tree_path()
# Pad shorter path with zeros
max_len = max(len(p1), len(p2))
p1 = p1 + [0] * (max_len - len(p1))
p2 = p2 + [0] * (max_len - len(p2))
# Find LCA depth: first position where paths diverge
lca_depth = 0
for a, b in zip(p1, p2):
if a == b and a > 0:
lca_depth = max(lca_depth, a)
# Distance = sum of depths to reach LCA
d1 = sum(1 for d in p1 if d > lca_depth)
d2 = sum(1 for d in p2 if d > lca_depth)
return d1 + d2
def strong_triangle_inequality(e1: Expr, e2: Expr, e3: Expr) -> bool:
"""
Check the strong triangle inequality (ultrametric):
d(a,b) <= max(d(a,c), d(b,c))
"""
d12 = tree_distance(e1, e2)
d13 = tree_distance(e1, e3)
d23 = tree_distance(e2, e3)
return d12 <= max(d13, d23)
__all__ = ['Expr', 'Token', 'mark', 'enclose', 'juxtapose',
'tree_distance', 'strong_triangle_inequality',
'STABLE_PATTERNS', 'SEMANTIC_PRIME_PATTERNS']