A NetHack Domain Specific Language (DSL) implemented in Lean 4 with formal verification, AI algorithms, and ASCII visualization.
LeanNetHack provides a mathematically rigorous foundation for NetHack game mechanics using Lean 4's theorem proving capabilities. It combines game development, formal verification, and AI research in a unified framework.
- Complete NetHack mechanics: Movement, combat, items, terrain, monsters
- Formal state representation: Position, stats, inventory, dungeon levels
- Action system: Move, attack, pickup, wait, descend stairs
- Terrain system: Walls, floors, corridors, stairs, doors
- Proven game properties: Movement preserves bounds, HP constraints
- State transition correctness: Verified action sequences
- Safety guarantees: Invalid moves are blocked, HP never exceeds max
- Policy verification: Greedy strategy maintains non-negative HP
- Pathfinding: A* and breadth-first search algorithms
- Minimax AI: Alpha-beta pruning for tactical decisions
- Policy evaluation: Reward-based optimization framework
- Multi-objective optimization: Survival, exploration, efficiency
- ASCII art renderer: Professional dungeon visualization with Unicode borders
- Frame-by-frame debugging: See exact state transitions
- Scriptable CLI: Rapid iteration and testing
- File snapshots: Save/load game states for analysis
- Lean 4 (nightly-2025-06-19 or later)
- Lake (Lean build system)
git clone https://github.com/alok/LeanNetHack.git
cd LeanNetHack
lake build
# Quick visualization test
lake env lean --run Scripts/QuickTest.lean
# Full feature demo
lake env lean --run Scripts/SimpleDemo.lean
# Interactive iteration
# Edit Scripts/IterateScript.lean and re-run for instant feedback
lake env lean --run Scripts/IterateScript.lean
🎮 NetHack Visualization System - WORKING DEMO
📍 INITIAL STATE:
┌────────┐
│########│
│#......#│
│#.@...$#│ <- @ = player, $ = gold, r = rat
│#...r..#│
│#....>.#│ <- > = downstairs
│########│
└────────┘
📍 AFTER MOVE EAST:
┌────────┐
│########│
│#......#│
│#..@..$#│ <- Player moved east
│#...r..#│
│#....>.#│
│########│
└────────┘
LeanNetHack/
├── LeanNetHack/
│ ├── Basic.lean # Core game types and mechanics
│ ├── Renderer.lean # ASCII visualization system
│ └── Delaborator.lean # Lean 4 delaborator (WIP)
├── Scripts/
│ ├── QuickTest.lean # Fast iteration testing
│ ├── SimpleDemo.lean # Full feature demonstration
│ └── IterateScript.lean # Interactive development
└── Main.lean # Executable entry point
- Wall collision: Players cannot move through walls
- Monster blocking: Cannot move onto occupied tiles
- Bounds checking: Positions must be within dungeon bounds
- Terrain awareness: Different movement rules for terrain types
- Damage calculation:
max(1, attacker_strength - defender_armor/2)
- Monster health: Persistent HP tracking with max HP limits
- Death handling: Monsters removed when HP ≤ 0
- Pickup mechanics: Items collected into inventory
- Multiple item types: Weapons, armor, potions, scrolls, gold
- Inventory management: Type-safe item representation
- Game rule verification: Prove properties about game mechanics
- State space analysis: Formal reasoning about reachable states
- Correctness guarantees: Mathematically verified implementations
- Algorithm testing: Benchmark pathfinding and decision making
- Policy optimization: Reward function design and evaluation
- Multi-agent scenarios: Future extensions for multiple entities
- ASCII art generation: Algorithmic dungeon rendering
- Real-time debugging: Interactive state exploration
- Performance profiling: Efficient visualization techniques
- Core game mechanics with proper collision detection
- ASCII visualization with Unicode borders
- Formal proofs for safety properties
- Scriptable CLI interface for rapid development
- Pathfinding and minimax AI algorithms
- Delaborator for inline visual representation
- Neural network integration
- Advanced AI algorithms (MCTS, deep RL)
- Extended monster/item systems
- Integration with actual NetHack game engine
- Machine learning model training infrastructure
- Multiplayer and network game support
- Advanced visualization modes
Contributions welcome! Areas of interest:
- Lean 4 delaborator implementation (following lean4-maze pattern)
- Extended game mechanics (spells, dungeon generation, etc.)
- AI algorithm implementations (deep RL, planning, etc.)
- Visualization enhancements (colors, animations, etc.)
MIT License - See LICENSE file for details.
- lean4-maze project for delaborator inspiration
- NetHack DevTeam for the original game
- Lean 4 community for theorem proving tools