Skip to content

Commit 32a00be

Browse files
authored
Merge pull request #2835 from fermga/copilot/update-grammar-to-use-unified
[Grammar] Phase 2.1: Update grammar.py to delegate to unified_grammar.py
2 parents 298bd83 + 3c16f43 commit 32a00be

File tree

3 files changed

+591
-1
lines changed

3 files changed

+591
-1
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env python
2+
"""Example demonstrating migration from C1-C3 to unified U1-U4 grammar.
3+
4+
This script shows how to update code from the old C1-C3 grammar system
5+
to the new unified U1-U4 grammar system.
6+
"""
7+
8+
import warnings
9+
10+
# ============================================================================
11+
# OLD APPROACH (C1-C3) - Still works but deprecated
12+
# ============================================================================
13+
14+
print("=" * 70)
15+
print("OLD APPROACH: Using deprecated C1-C3 functions")
16+
print("=" * 70)
17+
18+
from tnfr.operators.grammar import (
19+
validate_c1_existence,
20+
validate_c2_boundedness,
21+
validate_c3_threshold,
22+
)
23+
24+
# Enable deprecation warnings
25+
warnings.simplefilter('always', DeprecationWarning)
26+
27+
# Example sequence
28+
sequence = ['emission', 'dissonance', 'coherence', 'mutation', 'silence']
29+
30+
print(f"\nValidating sequence: {sequence}\n")
31+
32+
# Old way - separate C1-C3 checks (will emit warnings)
33+
c1_valid = validate_c1_existence(sequence)
34+
print(f" C1 (Existence & Closure): {c1_valid}")
35+
36+
c2_valid = validate_c2_boundedness(sequence)
37+
print(f" C2 (Boundedness): {c2_valid}")
38+
39+
c3_valid = validate_c3_threshold(sequence)
40+
print(f" C3 (Threshold Physics): {c3_valid}")
41+
42+
print(f"\n Overall valid (C1-C3): {c1_valid and c2_valid and c3_valid}")
43+
44+
# ============================================================================
45+
# NEW APPROACH (U1-U4) - Recommended for new code
46+
# ============================================================================
47+
48+
print("\n" + "=" * 70)
49+
print("NEW APPROACH: Using unified U1-U4 grammar")
50+
print("=" * 70)
51+
52+
from tnfr.operators.grammar import UnifiedGrammarValidator, validate_unified
53+
from tnfr.operators.definitions import (
54+
Emission, Dissonance, Coherence, Mutation, Silence
55+
)
56+
57+
# Create operator instances
58+
ops = [Emission(), Dissonance(), Coherence(), Mutation(), Silence()]
59+
60+
print(f"\nValidating sequence with unified grammar:\n")
61+
62+
# New way - unified validation with detailed messages
63+
valid, messages = UnifiedGrammarValidator.validate(ops, epi_initial=0.0)
64+
65+
print(" Unified validation messages:")
66+
for msg in messages:
67+
print(f" {msg}")
68+
69+
print(f"\n Overall valid (U1-U4): {valid}")
70+
71+
# Convenience function for simple boolean result
72+
print(f"\n Using validate_unified(): {validate_unified(ops, epi_initial=0.0)}")
73+
74+
# ============================================================================
75+
# UNDERSTANDING THE MAPPING
76+
# ============================================================================
77+
78+
print("\n" + "=" * 70)
79+
print("C1-C3 → U1-U4 MAPPING")
80+
print("=" * 70)
81+
82+
mapping = """
83+
Old Grammar (C1-C3) → Unified Grammar (U1-U4)
84+
───────────────────────────────────────────────────────────────────
85+
C1: EXISTENCE & CLOSURE → U1: STRUCTURAL INITIATION & CLOSURE
86+
- Must start with generator - U1a: Start with generator if EPI=0
87+
- Must end with closure - U1b: End with closure operator
88+
89+
C2: BOUNDEDNESS → U2: CONVERGENCE & BOUNDEDNESS
90+
- Destabilizers need - If destabilizers present, include
91+
stabilizers stabilizers (IL or THOL)
92+
93+
C3: THRESHOLD PHYSICS → U4: BIFURCATION DYNAMICS
94+
- Transformers need context - U4a: Bifurcation triggers need handlers
95+
- U4b: Transformers need recent destabilizer
96+
97+
NEW: Not in C1-C3 → U3: RESONANT COUPLING
98+
- Coupling/resonance need phase check
99+
"""
100+
101+
print(mapping)
102+
103+
# ============================================================================
104+
# OPERATOR SETS
105+
# ============================================================================
106+
107+
print("=" * 70)
108+
print("OPERATOR SETS")
109+
print("=" * 70)
110+
111+
from tnfr.operators.grammar import (
112+
UNIFIED_GENERATORS,
113+
UNIFIED_CLOSURES,
114+
UNIFIED_STABILIZERS,
115+
UNIFIED_DESTABILIZERS,
116+
)
117+
118+
print(f"""
119+
Generators (U1a): {sorted(UNIFIED_GENERATORS)}
120+
Closures (U1b): {sorted(UNIFIED_CLOSURES)}
121+
Stabilizers (U2): {sorted(UNIFIED_STABILIZERS)}
122+
Destabilizers (U2): {sorted(UNIFIED_DESTABILIZERS)}
123+
""")
124+
125+
# ============================================================================
126+
# MIGRATION CHECKLIST
127+
# ============================================================================
128+
129+
print("=" * 70)
130+
print("MIGRATION CHECKLIST")
131+
print("=" * 70)
132+
133+
checklist = """
134+
✓ Replace separate C1-C3 checks with UnifiedGrammarValidator.validate()
135+
✓ Use validate_unified() for simple boolean validation
136+
✓ Import operator sets from unified_grammar (UNIFIED_GENERATORS, etc.)
137+
✓ Update documentation to reference UNIFIED_GRAMMAR_RULES.md
138+
✓ Test with actual operator instances (not just string names)
139+
✓ Review U3 (Resonant Coupling) - new constraint not in C1-C3
140+
141+
For complete migration guide, see:
142+
- UNIFIED_GRAMMAR_RULES.md
143+
- src/tnfr/operators/unified_grammar.py
144+
"""
145+
146+
print(checklist)

0 commit comments

Comments
 (0)