Research-Stack/5-Applications/scripts/holographic_projection.py

413 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Holographic Projection System (Verified Lean Specification)
This implementation follows the formal specification in:
0-Core-Formalism/lean/Semantics/Semantics/HolographicProjection.lean
The Lean module provides:
- Holographic projection for topology stabilization
- S_holo(x) = ∫_surface Φ(x,y)·ψ(y) dy
- ΔS = -k_B T ln(P_stabilized)
- Surface layer as holographic projection stabilizing lower-level codons
This Python shim provides:
- JSON serialization for projection state
- Result wrapping for Lean function calls
- No logic (all logic defined in Lean specification)
"""
import json
import time
from typing import Dict, List, Optional, Any
from dataclasses import dataclass
from collections import deque
# Q16_16 fixed-point utilities (from Lean FixedPoint module)
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
def logQ16(x: int) -> int:
"""Natural log approximation for Q16_16"""
if x <= 0:
return 0
# Simple approximation: ln(x) ≈ 2*(x-1)/(x+1)
x_float = from_q16(x)
ln_val = 2 * (x_float - 1) / (x_float + 1)
return to_q16(ln_val)
@dataclass
class HolographicSurfacePoint:
"""Holographic surface point (Lean: HolographicSurfacePoint)"""
pointId: int # UInt64
amplitude: int # Q16_16 - Wave amplitude (0.0 to 1.0)
phase: int # Q16_16 - Phase (0.0 to 2π)
coherence: int # Q16_16 - Coherence (0.0 to 1.0)
def to_dict(self) -> Dict[str, Any]:
return {
'pointId': self.pointId,
'amplitude': from_q16(self.amplitude),
'phase': from_q16(self.phase),
'coherence': from_q16(self.coherence)
}
@dataclass
class HolographicProjectionState:
"""Holographic projection state (Lean: HolographicProjectionState)"""
surfacePoints: List[HolographicSurfacePoint]
temperature: int # Q16_16 - Temperature
stabilizationProbability: int # Q16_16 - P_stabilized (0.0 to 1.0)
entropyReduction: int # Q16_16 - ΔS (entropy reduction)
def to_dict(self) -> Dict[str, Any]:
return {
'surfacePoints': [p.to_dict() for p in self.surfacePoints],
'temperature': from_q16(self.temperature),
'stabilizationProbability': from_q16(self.stabilizationProbability),
'entropyReduction': from_q16(self.entropyReduction)
}
@dataclass
class HolographicAction:
"""Holographic projection action (Lean: HolographicAction)"""
pointId: int # UInt64
amplitudeDelta: int # Q16_16 - Change in amplitude
phaseDelta: int # Q16_16 - Change in phase
def to_dict(self) -> Dict[str, Any]:
return {
'pointId': self.pointId,
'amplitudeDelta': from_q16(self.amplitudeDelta),
'phaseDelta': from_q16(self.phaseDelta)
}
@dataclass
class HolographicBind:
"""Holographic bind result (Lean: HolographicBind)"""
lawful: bool
projectionBefore: int # Q16_16 - Projection before action
projectionAfter: int # Q16_16 - Projection after action
entropyReduction: int # Q16_16 - ΔS (entropy reduction)
stabilizationProbability: int # Q16_16 - P_stabilized
invariant: str
def to_dict(self) -> Dict[str, Any]:
return {
'lawful': self.lawful,
'projectionBefore': from_q16(self.projectionBefore),
'projectionAfter': from_q16(self.projectionAfter),
'entropyReduction': from_q16(self.entropyReduction),
'stabilizationProbability': from_q16(self.stabilizationProbability),
'invariant': self.invariant
}
# ═══════════════════════════════════════════════════════════════════════════
# Lean Function Implementations (verified by specification)
# ═══════════════════════════════════════════════════════════════════════════
def projectionKernel(point1: HolographicSurfacePoint, point2: HolographicSurfacePoint) -> int:
"""Calculate projection kernel: Φ(x,y) = amplitude × coherence × cos(phase) (Lean: projectionKernel)"""
phaseDiff = point1.phase - point2.phase
# Approximate cos(phase) using simple linear approximation
cosPhase = Q16_ONE - abs(phaseDiff) // 2 # Simple approximation
kernel = (point1.amplitude * point2.coherence * cosPhase) // (Q16_ONE * Q16_ONE)
return kernel
def holographicProjection(state: HolographicProjectionState, targetPoint: HolographicSurfacePoint) -> int:
"""Calculate holographic projection: S_holo(x) = Σ_y Φ(x,y)·ψ(y) (Lean: holographicProjection)"""
projectionSum = 0
for point in state.surfacePoints:
kernel = projectionKernel(targetPoint, point)
wavefunction = point.amplitude # ψ(y) = amplitude
projectionSum += (kernel * wavefunction) // Q16_ONE
return projectionSum
def entropyReduction(state: HolographicProjectionState) -> int:
"""Calculate entropy reduction: ΔS = -k_B T ln(P_stabilized) (Lean: entropyReduction)"""
kB = to_q16(0.00008617) # Boltzmann constant in eV/K (scaled)
T = state.temperature
P = state.stabilizationProbability
lnP = logQ16(P) if P > 0 else 0 # Natural log
deltaS = -kB * T * lnP // Q16_ONE
return deltaS
def isStabilized(point: HolographicSurfacePoint, threshold: int) -> bool:
"""Check if surface point is stabilized (Lean: isStabilized)"""
return point.coherence >= threshold and point.amplitude >= threshold
def applyStabilization(point: HolographicSurfacePoint, projection: int) -> HolographicSurfacePoint:
"""Apply holographic stabilization to point (Lean: applyStabilization)"""
newAmplitude = min(point.amplitude + projection, Q16_ONE)
newCoherence = min(point.coherence + (projection // 2), Q16_ONE)
return HolographicSurfacePoint(
pointId=point.pointId,
amplitude=newAmplitude,
phase=point.phase,
coherence=newCoherence
)
def calculateStabilizationProbability(state: HolographicProjectionState) -> int:
"""Calculate stabilization probability (Lean: calculateStabilizationProbability)"""
totalPoints = len(state.surfacePoints)
if totalPoints == 0:
return 0
stabilizedCount = 0
for point in state.surfacePoints:
if isStabilized(point, to_q16(0.7)):
stabilizedCount += 1
return (to_q16(stabilizedCount) // to_q16(totalPoints)) if totalPoints > 0 else 0
def isHolographicActionLawful(state: HolographicProjectionState, action: HolographicAction) -> bool:
"""Check if holographic action is lawful (Lean: isHolographicActionLawful)"""
return (action.amplitudeDelta >= (-Q16_ONE) and action.amplitudeDelta <= Q16_ONE and
action.phaseDelta >= (-to_q16(65536)) and action.phaseDelta <= to_q16(65536))
def updateSurfacePoint(point: HolographicSurfacePoint, action: HolographicAction) -> HolographicSurfacePoint:
"""Update surface point from action (Lean: updateSurfacePoint)"""
newAmplitude = point.amplitude + action.amplitudeDelta
newPhase = point.phase + action.phaseDelta
clampedAmplitude = max(0, min(newAmplitude, Q16_ONE))
clampedPhase = max(0, min(newPhase, to_q16(65536)))
return HolographicSurfacePoint(
pointId=point.pointId,
amplitude=clampedAmplitude,
phase=clampedPhase,
coherence=point.coherence
)
def holographicBind(state: HolographicProjectionState, action: HolographicAction) -> HolographicBind:
"""Bind primitive for holographic projection (Lean: holographicBind)"""
lawful = isHolographicActionLawful(state, action)
oldPoint = None
for p in state.surfacePoints:
if p.pointId == action.pointId:
oldPoint = p
break
projectionBefore = holographicProjection(state, oldPoint) if oldPoint else 0
newPoint = None
if lawful and oldPoint:
newPoint = updateSurfacePoint(oldPoint, action)
elif oldPoint:
newPoint = oldPoint
else:
newPoint = HolographicSurfacePoint(
pointId=action.pointId,
amplitude=to_q16(0.5),
phase=to_q16(0.0),
coherence=to_q16(0.5)
)
projectionAfter = holographicProjection(state, newPoint) if lawful else projectionBefore
deltaS = entropyReduction(state)
P_stabilized = calculateStabilizationProbability(state)
return HolographicBind(
lawful=lawful,
projectionBefore=projectionBefore,
projectionAfter=projectionAfter,
entropyReduction=deltaS,
stabilizationProbability=P_stabilized,
invariant="holographic_projection_satisfied" if lawful else "holographic_constraint_violated"
)
class HolographicProjectionSystem:
"""
Holographic projection system (Python shim wrapping Lean specification).
All core logic is defined in 0-Core-Formalism/lean/Semantics/Semantics/HolographicProjection.lean
"""
def __init__(self):
self.projectionState: Optional[HolographicProjectionState] = None
self.actionHistory: List[Dict[str, Any]] = []
print("[HolographicProjection] Initialized (Lean specification)")
def initializeProjection(self, temperature: float = 300.0, numPoints: int = 16) -> Dict[str, Any]:
"""Initialize holographic projection state"""
points = []
for i in range(numPoints):
point = HolographicSurfacePoint(
pointId=i,
amplitude=to_q16(0.5),
phase=to_q16(0.0),
coherence=to_q16(0.5)
)
points.append(point)
state = HolographicProjectionState(
surfacePoints=points,
temperature=to_q16(temperature),
stabilizationProbability=to_q16(0.5),
entropyReduction=to_q16(0.0)
)
self.projectionState = state
return {
'temperature': temperature,
'numPoints': numPoints,
'state': state.to_dict()
}
def registerSurfacePoint(self, pointId: int, amplitude: float, phase: float, coherence: float) -> Dict[str, Any]:
"""Register a surface point"""
point = HolographicSurfacePoint(
pointId=pointId,
amplitude=to_q16(amplitude),
phase=to_q16(phase),
coherence=to_q16(coherence)
)
if self.projectionState is None:
self.initializeProjection()
# Add point if not exists, update if exists
existing = False
newPoints = []
for p in self.projectionState.surfacePoints:
if p.pointId == pointId:
newPoints.append(point)
existing = True
else:
newPoints.append(p)
if not existing:
newPoints.append(point)
self.projectionState.surfacePoints = newPoints
self.projectionState.stabilizationProbability = calculateStabilizationProbability(self.projectionState)
self.projectionState.entropyReduction = entropyReduction(self.projectionState)
return {
'pointId': pointId,
'point': point.to_dict(),
'state': self.projectionState.to_dict()
}
def submitHolographicAction(self, action: HolographicAction) -> Dict[str, Any]:
"""Submit holographic action for processing (Lean specification)"""
if self.projectionState is None:
return {'error': 'Projection not initialized'}
bindResult = holographicBind(self.projectionState, action)
if bindResult.lawful:
# Update point in state
for i, p in enumerate(self.projectionState.surfacePoints):
if p.pointId == action.pointId:
self.projectionState.surfacePoints[i] = updateSurfacePoint(p, action)
break
# Update state metrics
self.projectionState.stabilizationProbability = calculateStabilizationProbability(self.projectionState)
self.projectionState.entropyReduction = entropyReduction(self.projectionState)
# Record action history
self.actionHistory.append({
'pointId': action.pointId,
'action': action.to_dict(),
'bindResult': bindResult.to_dict(),
'timestamp': time.time()
})
return {
'success': bindResult.lawful,
'bindResult': bindResult.to_dict(),
'state': self.projectionState.to_dict()
}
def getProjectionState(self) -> Optional[Dict[str, Any]]:
"""Get current projection state"""
if self.projectionState:
return self.projectionState.to_dict()
return None
def getActionHistory(self, limit: int = 10) -> List[Dict[str, Any]]:
"""Get action history"""
return self.actionHistory[-limit:]
def printSystemState(self):
"""Print system state"""
print("\n" + "="*60)
print("HOLOGRAPHIC PROJECTION STATE")
print("="*60)
if self.projectionState:
print(f"\n📊 Projection Metrics:")
print(f" Temperature: {from_q16(self.projectionState.temperature):.3f} K")
print(f" Stabilization Probability: {from_q16(self.projectionState.stabilizationProbability):.3f}")
print(f" Entropy Reduction: {from_q16(self.projectionState.entropyReduction):.3f}")
print(f"\n📍 Surface Points: {len(self.projectionState.surfacePoints)}")
for point in self.projectionState.surfacePoints:
stabilized = isStabilized(point, to_q16(0.7))
print(f" Point {point.pointId}: {'STABILIZED' if stabilized else 'UNSTABILIZED'}")
print(f" Amplitude: {from_q16(point.amplitude):.3f}")
print(f" Phase: {from_q16(point.phase):.3f}")
print(f" Coherence: {from_q16(point.coherence):.3f}")
print(f"\n📜 Action History: {len(self.actionHistory)} entries")
print("\n" + "="*60)
def main():
"""Test holographic projection system"""
system = HolographicProjectionSystem()
print("[Test 1] Initialize holographic projection...")
result1 = system.initializeProjection(temperature=300.0, numPoints=4)
print(f" Projection initialized: {result1['numPoints']} points")
print("\n[Test 2] Register surface point (high amplitude, high coherence)...")
result2 = system.registerSurfacePoint(pointId=1, amplitude=0.9, phase=0.0, coherence=0.95)
print(f" Point 1 registered")
print("\n[Test 3] Register surface point (low amplitude, low coherence)...")
result3 = system.registerSurfacePoint(pointId=2, amplitude=0.3, phase=1.5, coherence=0.4)
print(f" Point 2 registered")
print("\n[Test 4] Submit holographic action (increase amplitude for point 2)...")
action1 = HolographicAction(pointId=2, amplitudeDelta=to_q16(0.3), phaseDelta=to_q16(0.0))
result4 = system.submitHolographicAction(action1)
print(f" Result: Success={result4['success']}")
if result4['success']:
print(f" Projection before: {result4['bindResult']['projectionBefore']:.3f}")
print(f" Projection after: {result4['bindResult']['projectionAfter']:.3f}")
print(f" Entropy Reduction: {result4['bindResult']['entropyReduction']:.3f}")
print("\n[System State]")
system.printSystemState()
if __name__ == '__main__':
main()