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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
"""
pl/core.py β€” Propagation Logic Core
=====================================

P / G β†’ Q

The single primitive operator of Propagation Logic.

A loaded pattern P = (v_P, L_P) propagates through gradient field G
in context C = (Ξ“_C, ΞΈ_C) to produce updated pattern Q.

Everything in this module is derived from that operator.
Nothing is assumed that is not forced by the mechanism.

Key insight (PL v13, Section 2.6):
    G is not a different kind of thing from P.
    G is P occupying the gradient role contextually.
    All G is P post-boundary-imposition.
    There is no view from outside.
    This file is also P / G β†’ Q.
"""

from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
import math


# =============================================================================
# LOADED PATTERN
# =============================================================================

@dataclass
class Pattern:
    """
    P = (v_P, L_P)

    Definition 2.1 (PL v13):
        P = (v_P, H_P) with v_P ∈ V, H_P a propagation history.
        V is the carrier set.
        L_P = |H_P| β‰₯ 0 is the informational load.

    v_P : designation component β€” what the pattern currently designates.
          In logic: 0 or 1.
          In arithmetic: a number.
          In language: a token or sentence.
          In physics: a field value.

    L_P : informational load β€” magnitude of accumulated propagation history.
          L_P = 0 : seed state. No history. Propagates freely.
          L_P > 0 : loaded. Has been through gradient fields.
                    The more it has been through, the harder to propagate further.

    history : the full qualitative record (L_P is its magnitude).
              Conceptually rich; computationally we track both.
    """
    v: Any
    L: float = 0.0
    history: List[str] = field(default_factory=list)

    def __repr__(self) -> str:
        h = " β†’ ".join(self.history[-3:]) if self.history else "βˆ…"
        return f"P(v={self.v!r}, L={self.L:.3f}, hist=[{h}])"

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Pattern):
            return False
        return self.v == other.v and abs(self.L - other.L) < 1e-9

    def __hash__(self) -> int:
        return hash((repr(self.v), round(self.L, 9)))

    def is_seed(self) -> bool:
        """L_P = 0: no propagation history."""
        return self.L == 0.0

    def copy_with(self, v=None, L=None, history=None) -> "Pattern":
        return Pattern(
            v=v if v is not None else self.v,
            L=L if L is not None else self.L,
            history=history if history is not None else self.history.copy(),
        )


def seed(v: Any) -> Pattern:
    """Convenience: create a seed pattern with no history."""
    return Pattern(v=v, L=0.0)


# =============================================================================
# CONTEXT
# =============================================================================

@dataclass
class Context:
    """
    C = (Ξ“_C, ΞΈ_C)

    Definition 2.2 (PL v13):
        A context is a pair C = (Ξ“_C, ΞΈ_C) where:
            Ξ“_C : the set of gradient fields available in C
            ΞΈ_C : the coherence threshold

    Key derived quantities:
        support(P, C) = min(L_P, ΞΈ_C)
        demand(P, C)  = max(0, L_P βˆ’ ΞΈ_C)

        demand = 0  β†’ coherent (pattern is fully supported)
        demand > 0  β†’ incoherent (pattern needs more context than available)

    Theorem 2.1 (the propagation rate theorem):
        Among incoherent patterns, rate ∝ 1/L_P.
        Simpler patterns propagate faster.
        This is Zipf's law, natural selection, why e^x is its own derivative β€”
        all one theorem.
    """
    gradients: List["Gradient"]
    theta: float = 1.0
    name: str = "C"

    def support(self, p: Pattern) -> float:
        """support(P, C) = min(L_P, ΞΈ_C)"""
        return min(p.L, self.theta)

    def demand(self, p: Pattern) -> float:
        """demand(P, C) = max(0, L_P βˆ’ ΞΈ_C)"""
        return max(0.0, p.L - self.theta)

    def is_coherent(self, p: Pattern) -> bool:
        """demand = 0: pattern is fully supported by context."""
        return self.demand(p) == 0.0

    def is_valid(self, p: Pattern, designated: Set = None) -> bool:
        """
        valid = designated AND coherent.
        designated: values that count as "true" in this carrier.
        Default: {1, True} (classical logic convention).
        """
        if designated is None:
            designated = {1, True, 1.0}
        return p.v in designated and self.is_coherent(p)

    def propagation_rate(self, p: Pattern) -> float:
        """
        Theorem 2.1: rate = 1/L_P for incoherent patterns.
        Rate = inf for coherent patterns (already there).
        """
        if self.is_coherent(p):
            return float("inf")
        return 1.0 / p.L if p.L > 0 else float("inf")


# =============================================================================
# GRADIENT
# =============================================================================

class Gradient:
    """
    G β€” a gradient field.

    G is P occupying the gradient role contextually (PL v13, Section 2.6).
    Every G has loaded history. Every G was constituted by prior propagation.
    G can become P in a higher-order event.

    The propagation event:
        P / G β†’ Q
        v_Q = transform(v_P)          β€” designation changes
        L_Q = L_P + cost(P)           β€” load accumulates
        H_Q = H_P + [G.name]          β€” history records this gradient

    cost: by default 1.0 per propagation step.
          Zero-cost gradients (identity, observation) can be specified.
          Variable-cost gradients can depend on P.
    """

    def __init__(
        self,
        name: str,
        transform: Callable[[Any], Any],
        cost: Union[float, Callable[["Pattern"], float]] = 1.0,
        domain: Optional[Set] = None,
        description: str = "",
    ):
        self.name = name
        self._transform = transform
        self._cost_fn = (cost if callable(cost) else (lambda p, c=cost: c))
        self.domain = domain          # None = any carrier
        self.description = description

    def transform(self, v: Any) -> Any:
        """Apply the designation transformation."""
        return self._transform(v)

    def cost(self, p: Pattern) -> float:
        """Load cost for propagating this pattern through this gradient."""
        return self._cost_fn(p)

    def propagate(self, p: Pattern) -> Pattern:
        """
        P / G β†’ Q

        The primitive operation. Everything else is derived from this.
        """
        v_Q = self._transform(p.v)
        L_Q = p.L + self._cost_fn(p)
        hist_Q = p.history + [self.name]
        return Pattern(v=v_Q, L=L_Q, history=hist_Q)

    def __call__(self, p: Pattern) -> Pattern:
        return self.propagate(p)

    def __repr__(self) -> str:
        return f"G[{self.name}]"

    def is_closed_on(self, carrier: Set) -> Tuple[bool, List]:
        """
        Does this gradient keep the carrier closed?
        Returns (is_closed, violations)
        where violations = [(v_in, v_out_of_carrier), ...]
        """
        violations = []
        for v in carrier:
            out = self._transform(v)
            if out not in carrier:
                violations.append((v, out))
        return len(violations) == 0, violations

    def fixed_points(self, carrier: Set) -> List:
        """Values v ∈ V where G(v) = v."""
        return [v for v in carrier if self._transform(v) == v]

    def orbit(self, v: Any, max_steps: int = 64) -> List:
        """
        The orbit of v under repeated application of G.
        Returns the full cycle if found, truncated at max_steps otherwise.

        If the orbit escapes the carrier (transform raises ValueError),
        returns the visited list so far β€” indicating no cycle within V.
        This is the correct result for closure violations:
        orbits that exit V have no cycle within V.
        """
        visited = [v]
        current = v
        for _ in range(max_steps):
            try:
                current = self._transform(current)
            except (ValueError, KeyError):
                # Orbit has escaped the carrier β€” no cycle within V.
                return visited
            if current == v:
                return visited   # full cycle
            visited.append(current)
        return visited           # truncated (no cycle found within max_steps)

    def cycle_length(self, v: Any, max_steps: int = 64) -> Optional[int]:
        """
        Length of the orbit cycle. None if no cycle found.
        None is also correct when the orbit escapes the carrier (closure violation).
        """
        o = self.orbit(v, max_steps)
        if not o:
            return None
        current = o[-1]
        try:
            next_v = self._transform(current)
        except (ValueError, KeyError):
            return None   # orbit escapes carrier, no cycle
        if next_v == o[0]:
            return len(o)
        return None


# =============================================================================
# GRADIENT FAMILIES
# Standard gradient families for known carriers.
# Each family is itself a Pattern in a higher-order carrier.
# =============================================================================

# ── Classical Logic: V = {0, 1} ────────────────────────────────────────────

def G_neg() -> Gradient:
    """
    Classical negation on {0, 1}.
    G_neg flips the designation but does NOT change the load.
    Negating "all dogs bark" costs the same as asserting it.
    """
    return Gradient(
        name="neg",
        transform=lambda v: 1 - v,
        domain={0, 1},
        description="Classical negation: v β†’ 1 - v",
    )

def G_and() -> Gradient:
    """
    Conjunction as a binary gradient.
    Requires tuple input (v_P, v_Q); returns conjunction value.
    """
    return Gradient(
        name="and",
        transform=lambda v: int(v[0] and v[1]) if isinstance(v, tuple) else v,
        domain=None,
        description="Classical conjunction",
    )

def G_or() -> Gradient:
    """Disjunction."""
    return Gradient(
        name="or",
        transform=lambda v: int(v[0] or v[1]) if isinstance(v, tuple) else v,
        domain=None,
        description="Classical disjunction",
    )

def G_id() -> Gradient:
    """
    Identity: zero-cost, leaves everything unchanged.
    The simplest gradient. Fixed point of the gradient-family gradient family.
    """
    return Gradient(
        name="id",
        transform=lambda v: v,
        cost=0.0,
        description="Identity: v β†’ v, zero cost",
    )

# ── Fuzzy Logic: V = {0, 0.5, 1} ───────────────────────────────────────────

def G_fuzzy_neg() -> Gradient:
    """
    Fuzzy negation: v β†’ 1 - v.
    Same formula as classical neg, but on a richer carrier.
    On V={0,0.5,1}: 0.5 is a fixed point. Excluded middle fails at 0.5.
    This is DERIVED from the carrier structure, not assumed.
    """
    return Gradient(
        name="fuzzy_neg",
        transform=lambda v: 1 - v,
        domain={0, 0.5, 1},
        description="Fuzzy negation: v β†’ 1-v on {0, 0.5, 1}",
    )

def G_lukasiewicz_and() -> Gradient:
    """Łukasiewicz conjunction: max(0, v[0] + v[1] - 1)."""
    return Gradient(
        name="luk_and",
        transform=lambda v: max(0, v[0] + v[1] - 1) if isinstance(v, tuple) else v,
        description="Łukasiewicz conjunction",
    )

# ── Arithmetic: V = β„•, β„€, β„š, ℝ ─────────────────────────────────────────────

def G_succ() -> Gradient:
    """
    Successor: n β†’ n + 1.
    On β„•: always closed.
    No fixed points (nothing maps to itself under +1).
    """
    return Gradient(
        name="succ",
        transform=lambda v: v + 1,
        description="Successor: v β†’ v + 1",
    )

def G_pred() -> Gradient:
    """
    Predecessor: n β†’ n - 1.
    On β„• = {0, 1, 2, ...}: NOT closed (0 β†’ -1 βˆ‰ β„•).
    Closure violation FORCES extension to β„€.
    This is how β„• β†’ β„€ is derived, not assumed.
    """
    return Gradient(
        name="pred",
        transform=lambda v: v - 1,
        description="Predecessor: v β†’ v - 1 (forces β„•β†’β„€ extension)",
    )

def G_double() -> Gradient:
    """v β†’ 2v. On β„€: closed. Reveals even/odd structure."""
    return Gradient(
        name="double",
        transform=lambda v: 2 * v,
        description="Doubling: v β†’ 2v",
    )

def G_halve() -> Gradient:
    """
    v β†’ v/2.
    On β„€: NOT closed for odd integers (1/2 βˆ‰ β„€).
    Forces extension to β„š.
    This is how β„€ β†’ β„š is derived.
    """
    return Gradient(
        name="halve",
        transform=lambda v: v / 2,
        description="Halving: v β†’ v/2 (forces β„€β†’β„š extension)",
    )

def G_sqrt() -> Gradient:
    """
    v β†’ √v.
    On β„šβΊ: NOT closed (√2 βˆ‰ β„š).
    Forces extension to ℝ.
    This is how β„š β†’ ℝ is derived.
    """
    return Gradient(
        name="sqrt",
        transform=lambda v: v ** 0.5,
        description="Square root: v β†’ √v (forces β„šβ†’β„ extension)",
    )

def G_neg_sqrt() -> Gradient:
    """
    v β†’ √(-v) for v < 0, else √v.
    On ℝ: NOT closed for negative values.
    Forces extension to β„‚.
    This is how ℝ β†’ β„‚ is derived.
    """
    return Gradient(
        name="neg_sqrt",
        transform=lambda v: complex(0, (-v)**0.5) if v < 0 else v**0.5,
        description="Negative sqrt: forces ℝ→ℂ extension",
    )

# ── Modular arithmetic ──────────────────────────────────────────────────────

def G_mod(n: int, op: str = "add1") -> Gradient:
    """Modular arithmetic on β„€/nβ„€."""
    ops = {
        "add1": lambda v: (v + 1) % n,
        "add2": lambda v: (v + 2) % n,
        "neg":  lambda v: (-v) % n,
        "double": lambda v: (2 * v) % n,
    }
    if op not in ops:
        raise ValueError(f"Unknown op {op!r}. Choose from {list(ops)}")
    return Gradient(
        name=f"mod{n}_{op}",
        transform=ops[op],
        domain=set(range(n)),
        description=f"Mod-{n} {op}",
    )

# ── Custom gradient ─────────────────────────────────────────────────────────

def G_custom(name: str, mapping: Dict[Any, Any], cost: float = 1.0) -> Gradient:
    """
    Build a gradient from an explicit mapping.
    Any carrier, any domain. The engine derives what it forces.
    This is the 'novel carrier' entry point for boundary condition extrapolation.
    """
    def _transform(v: Any) -> Any:
        if v not in mapping:
            raise ValueError(
                f"G[{name}]: value {v!r} not in mapping {set(mapping.keys())}. "
                f"Carrier extension may be required."
            )
        return mapping[v]

    return Gradient(
        name=name,
        transform=_transform,
        cost=cost,
        domain=set(mapping.keys()),
        description=f"Custom gradient with mapping {mapping}",
    )


# =============================================================================
# GRADIENT FAMILY REGISTRY
# Standard (V, Ξ“) configurations and what they force.
# Used by the engine and the data generator.
# =============================================================================

KNOWN_SYSTEMS = {
    "classical_logic": {
        "description": "Classical two-valued logic",
        "carrier": {0, 1},
        "gradients": lambda: [G_neg(), G_id()],
        "designated": {1},
        "forced": ["closure", "involution", "excluded_middle", "double_negation"],
    },
    "three_valued_logic": {
        "description": "Three-valued (Łukasiewicz) logic",
        "carrier": {0, 0.5, 1},
        "gradients": lambda: [G_fuzzy_neg(), G_id()],
        "designated": {1},
        "forced": ["closure", "middle_value_fixed", "excluded_middle_fails"],
    },
    "natural_numbers": {
        "description": "β„• with successor (closed) and predecessor (not closed)",
        "carrier": set(range(10)),    # finite sample; full β„• is unbounded
        "gradients": lambda: [G_succ(), G_id()],
        "designated": {1},
        "forced": ["closure_under_succ", "no_fixed_points_succ"],
    },
    "integers_forced": {
        "description": "β„• βˆͺ predecessor β†’ forces β„€",
        "carrier": {0, 1, 2, 3, 4},
        "gradients": lambda: [G_pred(), G_id()],
        "designated": {1},
        "forced": ["closure_violation", "negative_extension_forced"],
    },
    "rationals_forced": {
        "description": "β„€ βˆͺ halving β†’ forces β„š",
        "carrier": {-2, -1, 0, 1, 2, 3, 4},
        "gradients": lambda: [G_halve(), G_id()],
        "designated": {1},
        "forced": ["closure_violation", "rational_extension_forced"],
    },
    "Z4": {
        "description": "Cyclic group β„€/4β„€",
        "carrier": {0, 1, 2, 3},
        "gradients": lambda: [G_mod(4, "add1"), G_id()],
        "designated": {0},
        "forced": ["closure", "uniform_4_cycle", "no_fixed_points"],
    },
    "Z2": {
        "description": "Cyclic group β„€/2β„€ (bit flip)",
        "carrier": {0, 1},
        "gradients": lambda: [G_mod(2, "add1"), G_id()],
        "designated": {0},
        "forced": ["closure", "involution"],
    },
}


# =============================================================================
# PROPAGATION CHAIN
# Records a sequence of P / G β†’ Q steps.
# This is the training unit for the mechanism-first model.
# =============================================================================

@dataclass
class PropagationChain:
    """
    A recorded sequence of propagation steps.

    Step 0: P_0 / G_0 β†’ P_1
    Step 1: P_1 / G_1 β†’ P_2
    ...
    Step n: P_n / G_n β†’ P_{n+1}

    This is the fundamental training example:
    not prose about the mechanism, but the mechanism running.
    """
    steps: List[Tuple[Pattern, Gradient, Pattern]] = field(default_factory=list)
    context: Optional[Context] = None
    carrier: Optional[Set] = None

    def add(self, p_in: Pattern, g: Gradient, p_out: Pattern):
        self.steps.append((p_in, g, p_out))

    def run(self, initial: Pattern, gradients: List[Gradient]) -> "PropagationChain":
        """Run a chain of propagation steps from initial pattern."""
        chain = PropagationChain(context=self.context, carrier=self.carrier)
        current = initial
        for g in gradients:
            next_p = g.propagate(current)
            chain.add(current, g, next_p)
            current = next_p
        return chain

    def as_text(self) -> str:
        """
        Render as training text.
        This is the format the LM learns to predict.
        """
        lines = []
        if self.carrier:
            lines.append(f"CARRIER: {sorted(self.carrier, key=str)}")
        for i, (p_in, g, p_out) in enumerate(self.steps):
            lines.append(
                f"STEP {i}: P(v={p_in.v!r}, L={p_in.L:.1f}) / G[{g.name}] "
                f"β†’ Q(v={p_out.v!r}, L={p_out.L:.1f})"
            )
        return "\n".join(lines)

    def demand_profile(self) -> List[float]:
        """
        How does demand change across the chain?
        Increasing demand: moving away from coherence (incoherence accumulating).
        Decreasing demand: moving toward coherence (gradient is solving something).
        """
        if not self.context:
            return []
        return [self.context.demand(p_out) for _, _, p_out in self.steps]

    def __len__(self) -> int:
        return len(self.steps)

    def __repr__(self) -> str:
        return f"Chain({len(self.steps)} steps)"