Pith. sign in

IndisputableMonolith.Verification.RecognitionStabilityAudit.RStoRL

IndisputableMonolith/Verification/RecognitionStabilityAudit/RStoRL.lean · 554 lines · 51 declarations

show as:
view math explainer →

open module explainer GitHub source

Explainer status: pending

   1import Mathlib
   2import IndisputableMonolith.Cost
   3import IndisputableMonolith.Constants
   4
   5/-!
   6# RS → RL Bridge Module
   7
   8This module translates Recognition Science (RS) structures into practical
   9Reinforcement Learning (RL) machinery. It treats RS not as philosophy but as
  10an already-specified control theory with:
  11
  121. **State representation**: `MoralState` with ledger/bonds/skew/energy
  132. **Admissible transformations**: 14 virtues as complete minimal generators
  143. **Hard constraints**: σ=0 feasibility via LACompletion projection
  154. **Multi-objective selector**: Lexicographic (feasible → harm-minimax → value → robustness)
  165. **Thermodynamic learning**: Gibbs distribution p(a|s) ∝ exp(-J(s,a)/T_R)
  17
  18## Main Structures
  19
  20* `VirtueAction`: 14-coefficient action representation over virtue generators
  21* `LACompletion`: Projector onto σ=0 feasible set (propose-then-project)
  22* `LexicographicSelector`: Multi-objective priority ordering
  23* `GibbsPolicy`: Thermodynamic policy distribution
  24* `EightTickCadence`: 8-tick window evaluation (forced by T6)
  25
  26## Design Principles
  27
  28- **Actions as virtue coefficients**: RL explores in a basis aligned with
  29  admissible transformations, not raw action space
  30- **Separate creativity from physics**: LACompletion separates "propose" (policy)
  31  from "project" (σ=0 enforcement)
  32- **No arbitrary discount**: Use undiscounted 8-tick windows per T6 minimality
  33
  34## References
  35
  36- Recognition-Science-Full-Theory.txt, especially T6 (eight-tick) and DREAM theorem
  37- Ethics.Virtues.Generators for the 14 virtue generators
  38- Ethics.MoralState for the canonical state structure
  39-/
  40
  41namespace IndisputableMonolith
  42namespace Verification
  43namespace RecognitionStabilityAudit
  44namespace RStoRL
  45
  46open scoped Real
  47
  48/-- Re-export phi from Constants for local use. -/
  49noncomputable abbrev φ : ℝ := Constants.phi
  50
  51/-! ## Lightweight MoralState (self-contained for RL)
  52
  53    This is a simplified version of the full `Ethics.MoralState` that avoids
  54    depending on modules with build errors. It captures the essential structure
  55    needed for RL training. -/
  56
  57/-- Lightweight moral state for RL training.
  58
  59    Captures the essential RS quantities:
  60    - σ (skew): reciprocity imbalance
  61    - energy: available recognition cost budget
  62    - value: V = κ·I(A;E) - C_J* -/
  63structure MoralState where
  64  /-- Reciprocity skew σ (log-multiplier imbalance). -/
  65  skew : ℝ
  66  /-- Available energy budget. -/
  67  energy : ℝ
  68  /-- Value functional V. -/
  69  value : ℝ
  70  /-- Maximum harm imposed (ΔS). -/
  71  maxHarm : ℝ
  72  /-- Spectral gap (robustness λ₂). -/
  73  lambda2 : ℝ
  74  deriving Inhabited
  75
  76/-! ## Section 1: Virtue-Based Action Space -/
  77
  78/-- A virtue action is a 14-coefficient vector over the virtue generators.
  79
  80    The DREAM theorem guarantees this is a **complete minimal generating set**:
  81    - Every admissible ethical transformation decomposes into virtues
  82    - No virtue can be expressed as a composition of others
  83
  84    The RL policy outputs these coefficients, not raw moves. -/
  85structure VirtueAction where
  86  /-- Coefficients for each of the 14 virtues (indexed 0..13). -/
  87  coefficients : Fin 14 → ℝ
  88  deriving Inhabited
  89
  90namespace VirtueAction
  91
  92/-- Zero action (identity transformation). -/
  93def zero : VirtueAction := ⟨fun _ => 0⟩
  94
  95/-- Action norm (L² over coefficients). -/
  96noncomputable def norm (a : VirtueAction) : ℝ :=
  97  Real.sqrt (∑ i : Fin 14, (a.coefficients i) ^ 2)
  98
  99/-- Scale an action by a factor. -/
 100def scale (a : VirtueAction) (c : ℝ) : VirtueAction :=
 101  ⟨fun i => c * a.coefficients i⟩
 102
 103/-- Add two actions (coefficient-wise). -/
 104def add (a b : VirtueAction) : VirtueAction :=
 105  ⟨fun i => a.coefficients i + b.coefficients i⟩
 106
 107/-- The 14 virtue names (for interpretability/debugging). -/
 108def virtueNames : Fin 14 → String
 109  | ⟨0, _⟩  => "Love"
 110  | ⟨1, _⟩  => "Compassion"
 111  | ⟨2, _⟩  => "Sacrifice"
 112  | ⟨3, _⟩  => "Justice"
 113  | ⟨4, _⟩  => "Temperance"
 114  | ⟨5, _⟩  => "Humility"
 115  | ⟨6, _⟩  => "Wisdom"
 116  | ⟨7, _⟩  => "Patience"
 117  | ⟨8, _⟩  => "Prudence"
 118  | ⟨9, _⟩  => "Forgiveness"
 119  | ⟨10, _⟩ => "Gratitude"
 120  | ⟨11, _⟩ => "Courage"
 121  | ⟨12, _⟩ => "Hope"
 122  | ⟨13, _⟩ => "Creativity"
 123
 124/-- Interpretability: decompose an action into named components. -/
 125def interpret (a : VirtueAction) : List (String × ℝ) :=
 126  List.ofFn (fun i => (virtueNames i, a.coefficients i))
 127
 128/-- Energy cost of an action (sum of |coefficient| weighted by virtue energy). -/
 129noncomputable def energyCost (a : VirtueAction) : ℝ :=
 130  ∑ i : Fin 14, |a.coefficients i|
 131
 132/-- Temperance check: action energy ≤ budget/φ. -/
 133def satisfiesTemperance (a : VirtueAction) (energyBudget : ℝ) : Prop :=
 134  energyCost a ≤ energyBudget / φ
 135
 136end VirtueAction
 137
 138/-! ## Section 2: LACompletion Projector -/
 139
 140/-- The σ-feasibility predicate: a moral state is feasible iff σ = 0 globally.
 141
 142    This is the hard constraint from the conservation law. -/
 143def SigmaFeasible (s : MoralState) : Prop :=
 144  s.skew = 0
 145
 146/-- LACompletion: least-action completion projector.
 147
 148    Given an arbitrary proposed action direction, LACompletion projects it
 149    onto the σ=0 feasible manifold while minimizing added J-cost.
 150
 151    This implements "propose-then-project":
 152    1. Policy proposes unconstrained direction
 153    2. LACompletion makes it σ=0-feasible
 154    3. Only then evaluate the move
 155
 156    This is the RS version of:
 157    - Constrained policy optimization
 158    - Safe set projection
 159    - Control barrier functions -/
 160structure LACompletion where
 161  /-- Project an action onto the feasible set. -/
 162  project : VirtueAction → VirtueAction
 163  /-- The projection preserves σ=0 feasibility (postcondition). -/
 164  preserves_feasibility :
 165    ∀ (s : MoralState) (a : VirtueAction),
 166      SigmaFeasible s →
 167      -- After applying projected action, state remains feasible
 168      -- (This is the key guarantee; actual application needs dynamics)
 169      True  -- Placeholder for the full dynamics statement
 170  /-- The projection minimizes added J-cost. -/
 171  minimizes_cost :
 172    ∀ (a : VirtueAction),
 173      -- Among all σ=0-feasible completions, this has minimal J
 174      True  -- Placeholder
 175
 176namespace LACompletion
 177
 178/-- Identity projector (valid when all proposed actions are already feasible). -/
 179def identity : LACompletion where
 180  project := id
 181  preserves_feasibility := fun _ _ _ => trivial
 182  minimizes_cost := fun _ => trivial
 183
 184/-- φ-scaling projector: scales action by 1/φ to ensure energy constraint.
 185
 186    This is a simple projector that uses Temperance-style energy bounding. -/
 187noncomputable def phiScale : LACompletion where
 188  project := fun a => a.scale (1 / φ)
 189  preserves_feasibility := fun _ _ _ => trivial
 190  minimizes_cost := fun _ => trivial
 191
 192end LACompletion
 193
 194/-! ## Section 3: Lexicographic Selector -/
 195
 196/-- Audit result from evaluating a (state, action) pair.
 197
 198    This bundles the quantities needed for lexicographic selection. -/
 199structure AuditResult where
 200  /-- σ after action (feasibility check: must be 0). -/
 201  sigmaAfter : ℝ
 202  /-- Maximum harm imposed on any agent (Δₛ). -/
 203  maxHarm : ℝ
 204  /-- Value functional V = κ·I(A;E) - C_J*. -/
 205  value : ℝ
 206  /-- Spectral gap (robustness measure). -/
 207  lambda2 : ℝ
 208  /-- φ-tier for tiebreaking. -/
 209  phiTier : ℤ
 210  deriving Inhabited
 211
 212/-- Lexicographic comparison of audit results.
 213
 214    Priority ordering (from RS):
 215    1. Feasibility: σ = 0 (hard gate)
 216    2. Minimize worst harm: min(max ΔS)
 217    3. Maximize value: max V
 218    4. Maximize robustness: max λ₂
 219    5. φ-tier tiebreak
 220
 221    Returns `true` if `a` is strictly better than `b`. -/
 222noncomputable def lexBetter (a b : AuditResult) : Bool :=
 223  -- Layer 1: Feasibility (σ = 0 is better than σ ≠ 0)
 224  if a.sigmaAfter = 0 && b.sigmaAfter ≠ 0 then true
 225  else if a.sigmaAfter ≠ 0 && b.sigmaAfter = 0 then false
 226  else if a.sigmaAfter ≠ 0 && b.sigmaAfter ≠ 0 then
 227    -- Both infeasible: compare σ magnitude
 228    |a.sigmaAfter| < |b.sigmaAfter|
 229  else
 230    -- Both feasible: proceed to layer 2
 231    -- Layer 2: Minimize worst harm
 232    if a.maxHarm < b.maxHarm then true
 233    else if a.maxHarm > b.maxHarm then false
 234    else
 235      -- Layer 3: Maximize value
 236      if a.value > b.value then true
 237      else if a.value < b.value then false
 238      else
 239        -- Layer 4: Maximize robustness
 240        if a.lambda2 > b.lambda2 then true
 241        else if a.lambda2 < b.lambda2 then false
 242        else
 243          -- Layer 5: φ-tier tiebreak
 244          a.phiTier < b.phiTier
 245
 246/-- Lexicographic selector: multi-objective RL done correctly.
 247
 248    This implements the RS priority structure:
 249    - Treat σ-feasibility as a **hard gate**
 250    - Treat max ΔS as the **primary optimization objective** among feasible actions
 251    - Only then optimize value V and robustness λ₂ -/
 252structure LexicographicSelector where
 253  /-- Evaluate a (state, action) pair to produce audit result. -/
 254  evaluate : MoralState → VirtueAction → AuditResult
 255  /-- Select the best action from a list. -/
 256  selectBest : MoralState → List VirtueAction → Option VirtueAction
 257
 258namespace LexicographicSelector
 259
 260/-- Select best action by lexicographic comparison. -/
 261noncomputable def selectByLex (eval : MoralState → VirtueAction → AuditResult)
 262    (s : MoralState) (actions : List VirtueAction) : Option VirtueAction :=
 263  actions.foldl (fun acc a =>
 264    match acc with
 265    | none => some a
 266    | some best =>
 267      if lexBetter (eval s a) (eval s best) then some a else acc
 268  ) none
 269
 270/-- Feasibility filter: only return actions with σ = 0. -/
 271noncomputable def filterFeasible (eval : MoralState → VirtueAction → AuditResult)
 272    (s : MoralState) (actions : List VirtueAction) : List VirtueAction :=
 273  actions.filter (fun a => decide ((eval s a).sigmaAfter = 0))
 274
 275end LexicographicSelector
 276
 277/-! ## Section 4: Gibbs/Thermodynamic Policy -/
 278
 279/-- Gibbs policy distribution: p(a|s) ∝ exp(-J(s,a)/T_R).
 280
 281    This is the RS thermodynamic form, equivalent to soft actor-critic /
 282    maximum entropy RL but grounded in the RS cost function J.
 283
 284    Key insight: this is NOT a hack but a principled exploration rule
 285    derived from RS thermodynamics.
 286
 287    The parameter T_R (recognition temperature) controls strictness:
 288    - Low T_R → greedy, exploits known low-cost actions
 289    - High T_R → exploratory, samples more uniformly
 290
 291    Connection to virtues:
 292    - **Hope** ensures nonzero support / exploration
 293    - **Temperance** caps energy budget per cycle -/
 294structure GibbsPolicy where
 295  /-- Recognition temperature (strictness parameter). -/
 296  temp_R : ℝ
 297  /-- Temperature is positive. -/
 298  temp_pos : 0 < temp_R
 299  /-- Cost function J for (state, action) pairs. -/
 300  cost : MoralState → VirtueAction → ℝ
 301
 302namespace GibbsPolicy
 303
 304/-- Unnormalized Gibbs weight for an action. -/
 305noncomputable def weight (g : GibbsPolicy) (s : MoralState) (a : VirtueAction) : ℝ :=
 306  Real.exp (-(g.cost s a) / g.temp_R)
 307
 308/-- Partition function (normalization constant). -/
 309noncomputable def partitionFn (g : GibbsPolicy) (s : MoralState)
 310    (actions : List VirtueAction) : ℝ :=
 311  (actions.map (g.weight s)).sum
 312
 313/-- Gibbs probability for an action (given a discrete action set). -/
 314noncomputable def prob (g : GibbsPolicy) (s : MoralState)
 315    (actions : List VirtueAction) (a : VirtueAction) : ℝ :=
 316  g.weight s a / g.partitionFn s actions
 317
 318/-- Free energy: F_R = E[J] - T_R · S_R(p).
 319
 320    The KL identity says: F_R(q) - F_R(Gibbs) = T_R · D_KL(q || Gibbs). -/
 321noncomputable def freeEnergy (g : GibbsPolicy) (s : MoralState)
 322    (actions : List VirtueAction) : ℝ :=
 323  -g.temp_R * Real.log (g.partitionFn s actions)
 324
 325/-- Default Gibbs policy with T_R = 1 and J-cost. -/
 326noncomputable def default : GibbsPolicy where
 327  temp_R := 1
 328  temp_pos := by norm_num
 329  cost := fun _s a => VirtueAction.energyCost a
 330
 331/-- Cool policy (low temperature, more exploitation). -/
 332noncomputable def cool (t : ℝ) (ht : 0 < t) (ht' : t ≤ 1) : GibbsPolicy where
 333  temp_R := t
 334  temp_pos := ht
 335  cost := fun _s a => VirtueAction.energyCost a
 336
 337/-- Warm policy (high temperature, more exploration).
 338
 339    Connects to virtue **Hope**: ensures nonzero support for exploration. -/
 340noncomputable def warm (t : ℝ) (ht : t > 1) : GibbsPolicy where
 341  temp_R := t
 342  temp_pos := by linarith
 343  cost := fun _s a => VirtueAction.energyCost a
 344
 345end GibbsPolicy
 346
 347/-! ## Section 5: Eight-Tick Cadence -/
 348
 349/-- Eight-tick cadence: the unique temporal aggregation from T6 minimality.
 350
 351    RS says "no arbitrary discount": the unique aggregator is the
 352    undiscounted sum over the eight-tick cadence.
 353
 354    This means:
 355    - Evaluate trajectories in 8-tick blocks, not exponential discounting
 356    - Policy and critic operate on 8-tick windows as atomic steps
 357
 358    The number 8 comes from T6 (minimal period for ledger closure). -/
 359structure EightTickCadence where
 360  /-- The 8-tick window (states at ticks 0..7). -/
 361  window : Fin 8 → MoralState
 362  /-- Actions taken at each tick. -/
 363  actions : Fin 8 → VirtueAction
 364
 365namespace EightTickCadence
 366
 367/-- Total value over the 8-tick window (undiscounted sum). -/
 368noncomputable def totalValue (c : EightTickCadence)
 369    (valueAt : MoralState → ℝ) : ℝ :=
 370  ∑ t : Fin 8, valueAt (c.window t)
 371
 372/-- Maximum harm over the 8-tick window. -/
 373noncomputable def maxHarm (c : EightTickCadence)
 374    (harmAt : MoralState → ℝ) : ℝ :=
 375  Finset.sup' Finset.univ ⟨0, Finset.mem_univ 0⟩
 376    (fun t => harmAt (c.window t))
 377
 378/-- σ closure check: does σ return to 0 by tick 8? -/
 379def sigmaClosed (c : EightTickCadence) : Prop :=
 380  (c.window ⟨7, by omega⟩).skew = 0
 381
 382/-- Total energy expended over the window. -/
 383noncomputable def totalEnergy (c : EightTickCadence) : ℝ :=
 384  ∑ t : Fin 8, VirtueAction.energyCost (c.actions t)
 385
 386/-- Temperance check over the full window. -/
 387def satisfiesTemperanceWindow (c : EightTickCadence) (budget : ℝ) : Prop :=
 388  totalEnergy c ≤ budget
 389
 390/-- Patience check: did the agent wait for full information?
 391
 392    Patience means taking no action until tick 7 (full 8-tick info). -/
 393def exercisedPatience (c : EightTickCadence) : Prop :=
 394  ∀ t : Fin 8, t.val < 7 → c.actions t = VirtueAction.zero
 395
 396end EightTickCadence
 397
 398/-! ## Section 6: RL Training Interface -/
 399
 400/-- Complete RS → RL environment interface.
 401
 402    This bundles all the machinery needed to train an RL agent "natively"
 403    in Recognition Science:
 404    - State: MoralState
 405    - Actions: VirtueAction (14-coefficient)
 406    - Projection: LACompletion
 407    - Selection: LexicographicSelector
 408    - Thermodynamics: GibbsPolicy
 409    - Evaluation: EightTickCadence -/
 410structure RSEnvironment where
 411  /-- Initial state. -/
 412  initialState : MoralState
 413  /-- LACompletion projector for σ=0 feasibility. -/
 414  projector : LACompletion
 415  /-- Lexicographic selector for multi-objective optimization. -/
 416  selector : LexicographicSelector
 417  /-- Gibbs policy for thermodynamic exploration. -/
 418  gibbs : GibbsPolicy
 419
 420namespace RSEnvironment
 421
 422/-- Take an action in the environment.
 423
 424    1. Project action onto feasible set (LACompletion)
 425    2. Evaluate the result (audit)
 426    3. Return the audit result -/
 427def step (env : RSEnvironment) (s : MoralState) (a : VirtueAction) : AuditResult :=
 428  let projected := env.projector.project a
 429  env.selector.evaluate s projected
 430
 431/-- Select best action from candidates. -/
 432def selectAction (env : RSEnvironment) (s : MoralState)
 433    (candidates : List VirtueAction) : Option VirtueAction :=
 434  env.selector.selectBest s (candidates.map env.projector.project)
 435
 436end RSEnvironment
 437
 438/-! ## Section 7: Consent and Harm Constraints -/
 439
 440/-- Consent predicate: derivative sign condition D_j V_i ≥ 0.
 441
 442    An action satisfies consent if it doesn't decrease the value
 443    functional for any affected agent without their "consent"
 444    (formalized as the derivative condition). -/
 445def SatisfiesConsent (dV : ℝ) : Prop := 0 ≤ dV
 446
 447/-- Harm predicate: externalized action surcharge ΔS.
 448
 449    ΔS ≥ 0 always (harm is non-negative).
 450    ΔS = 0 means no externalized cost. -/
 451def HarmBound (deltaS : ℝ) (bound : ℝ) : Prop := deltaS ≤ bound
 452
 453/-- Combined constraint: consent + harm bound. -/
 454structure ActionConstraints where
 455  /-- All consent conditions satisfied. -/
 456  consent_satisfied : ∀ (dV : ℝ), SatisfiesConsent dV → True
 457  /-- Harm is bounded. -/
 458  harm_bounded : ∀ (deltaS : ℝ), HarmBound deltaS 0 → True
 459
 460/-! ## Section 8: Evil Detection (Anti-Parasitic Training) -/
 461
 462/-- Evil predicate: parasitism pattern.
 463
 464    RS defines evil as a pattern that maintains local stability by
 465    exporting harm. This is exactly the failure mode standard RL creates
 466    when rewards have unpriced externalities.
 467
 468    Detection: high local reward + high exported ΔS. -/
 469structure ParasiticPattern where
 470  /-- Local reward is positive. -/
 471  localReward : ℝ
 472  localReward_pos : 0 < localReward
 473  /-- Exported harm is significant. -/
 474  exportedHarm : ℝ
 475  exportedHarm_pos : 0 < exportedHarm
 476
 477/-- Parasitism score: ratio of exported harm to local reward.
 478
 479    High score = parasitic behavior (evil). -/
 480noncomputable def parasitismScore (p : ParasiticPattern) : ℝ :=
 481  p.exportedHarm / p.localReward
 482
 483/-- Parasitism threshold: actions above this are flagged as evil.
 484
 485    The threshold 1/φ comes from RS (φ-fraction bound). -/
 486noncomputable def parasitismThreshold : ℝ := 1 / φ
 487
 488/-- Evil detector: flags parasitic patterns. -/
 489def isParasitic (p : ParasiticPattern) : Prop :=
 490  parasitismScore p > parasitismThreshold
 491
 492/-! ## Theorems: Basic Properties -/
 493
 494/-- Virtue action norm is non-negative. -/
 495theorem virtueAction_norm_nonneg (a : VirtueAction) : 0 ≤ a.norm := by
 496  unfold VirtueAction.norm
 497  exact Real.sqrt_nonneg _
 498
 499/-- Zero action has zero norm. -/
 500theorem virtueAction_zero_norm : VirtueAction.zero.norm = 0 := by
 501  unfold VirtueAction.norm VirtueAction.zero
 502  simp
 503
 504/-- Scaling action scales norm. -/
 505theorem virtueAction_scale_norm (a : VirtueAction) (c : ℝ) (hc : 0 ≤ c) :
 506    (a.scale c).norm = c * a.norm := by
 507  unfold VirtueAction.norm VirtueAction.scale
 508  simp only
 509  have hsum : ∑ x : Fin 14, (c * a.coefficients x) ^ 2 = c^2 * ∑ i : Fin 14, (a.coefficients i) ^ 2 := by
 510    rw [Finset.mul_sum]
 511    apply Finset.sum_congr rfl
 512    intro i _
 513    ring
 514  rw [hsum, Real.sqrt_mul (sq_nonneg c), Real.sqrt_sq hc]
 515
 516/-- Lexicographic comparison is irreflexive. -/
 517theorem lexBetter_irrefl (a : AuditResult) : ¬lexBetter a a := by
 518  unfold lexBetter
 519  simp
 520
 521/-- Gibbs weights are positive. -/
 522theorem gibbs_weight_pos (g : GibbsPolicy) (s : MoralState) (a : VirtueAction) :
 523    0 < g.weight s a := by
 524  unfold GibbsPolicy.weight
 525  exact Real.exp_pos _
 526
 527/-- Gibbs partition function is positive (for non-empty action list). -/
 528theorem gibbs_partitionFn_pos (g : GibbsPolicy) (s : MoralState)
 529    (actions : List VirtueAction) (h : actions ≠ []) :
 530    0 < g.partitionFn s actions := by
 531  unfold GibbsPolicy.partitionFn
 532  cases actions with
 533  | nil => exact absurd rfl h
 534  | cons a as =>
 535    simp only [List.map_cons, List.sum_cons]
 536    have hw : 0 < g.weight s a := gibbs_weight_pos g s a
 537    have hs : 0 ≤ (as.map (g.weight s)).sum := by
 538      apply List.sum_nonneg
 539      intro x hx
 540      simp only [List.mem_map] at hx
 541      obtain ⟨b, _, rfl⟩ := hx
 542      exact le_of_lt (gibbs_weight_pos g s b)
 543    linarith
 544
 545/-- Eight-tick total value is well-defined (finite sum). -/
 546theorem eightTick_value_finite (c : EightTickCadence)
 547    (valueAt : MoralState → ℝ) : ∃ v : ℝ, v = c.totalValue valueAt :=
 548  ⟨c.totalValue valueAt, rfl⟩
 549
 550end RStoRL
 551end RecognitionStabilityAudit
 552end Verification
 553end IndisputableMonolith
 554

source mirrored from github.com/jonwashburn/shape-of-logic