Eliminate infinite AI debugging loops with dependent types
A production-ready boilerplate for AI-assisted code generation using Idris2's dependent type system.
English | 한국어
Natural Language → AI → Code → Bug → Fix → New Bug → Fix → ... ∞
Traditional AI code generation suffers from:
- ❌ Ambiguous specifications
- ❌ No compile-time verification
- ❌ Infinite debugging loops
- ❌ Missing edge cases
Natural Language → AI → Idris2 (Dependent Types) → Python + Tests
↑ ↓
└───────── Compiler ──────────┘
Use dependent types as a specification language for code generation.
git clone https://github.com/twoLoop-40/idris2-python-boilerplate.git
cd idris2-python-boilerplate# Install Idris2
brew install idris2 # macOS
# or see: https://idris2.readthedocs.io/
# Install Python dependencies
pip install -r requirements.txtbash .claude/setup_project.sh# Example 1: Basic functions
cd examples/01_basic
idris2 -o func func.idr && ./build/exec/func
python func.py
pytest test_func.py -v
# Example 2: Dependent types
cd ../02_dependent_types
idris2 -o safelist SafeList.idr && ./build/exec/safelist
python safe_list.py
pytest test_safe_list.py -v # 45 tests, all passing!Write specifications in Idris2 with dependent types:
-- Type guarantees non-empty vector
safeHead : Vect (S n) a -> a
-- Bounded indexing (out-of-bounds impossible!)
safeIndex : Fin n -> Vect n a -> a
-- Matrix dimensions in types
matAdd : Matrix r c Int -> Matrix r c Int -> Matrix r c IntTypes become runtime checks:
def safe_head(vec: List[T]) -> T:
"""Type: Vect (S n) a -> a"""
assert len(vec) >= 1, "requires non-empty vector"
return vec[0]
def mat_add(mat1: Matrix, mat2: Matrix) -> Matrix:
"""Type: Matrix r c Int -> Matrix r c Int -> Matrix r c Int"""
assert mat1.rows == mat2.rows
assert mat1.cols == mat2.cols
# ...Type signatures → test suites:
# From type: Vect (S n) requires non-empty
@pytest.mark.precondition
def test_safe_head_rejects_empty():
with pytest.raises(AssertionError):
safe_head([])
# From type: Vect (n + m) → Vect n
@given(n=st.integers(0, 50), m=st.integers(0, 50))
@pytest.mark.property
def test_length_property(n, m):
vec = list(range(n + m))
result = safe_take(n, vec)
assert len(result) == nExample 1: Basic Functions (details)
Simple workflow demonstration with:
- Public/private functions
- Basic type signatures
- Auto-generated tests
Perfect for: Getting started
Example 2: Dependent Types (details)
Advanced features including:
- Vect (length-indexed vectors)
- Fin (bounded naturals)
- Matrix operations with dimension tracking
- 45 auto-generated tests (100% passing)
Perfect for: Understanding the power of dependent types
cp -r idris2-python-boilerplate ~/my-project
cd ~/my-project
bash .claude/setup_project.shcd /path/to/your/project
cp -r idris2-python-boilerplate/.claude .
bash .claude/setup_project.shEdit .claude/project_config.yaml:
project:
name: "MyProject"
target:
language: "python" # or typescript, rust
domain_types:
UserId:
python: "uuid.UUID"
EmailAddress:
python: "pydantic.EmailStr"-- src/UserValidation.idr
data ValidUser : Type where
MkValid : (email : Email)
-> (age : Nat)
-> {auto prf : age >= 18}
-> ValidUser/convert src/UserValidation.idr
# generated/python/user_validation.py
@dataclass
class ValidUser:
email: str
age: int
def __post_init__(self):
assert '@' in self.email
assert self.age >= 18
# generated/tests/test_user_validation.py
def test_underage_rejected():
with pytest.raises(AssertionError):
ValidUser("test@example.com", 17)- ✅ Precise, unambiguous specifications
- ✅ Immediate compiler feedback
- ✅ Drastically reduced debugging cycles
- ✅ Automatic edge case discovery
- ✅ Comprehensive runtime checks
- ✅ Self-documenting code
- ✅ High test coverage (auto-generated)
- ✅ Provable correctness properties
- ✅ Faster iteration
- ✅ Higher confidence
- ✅ Easier refactoring
- ✅ Better maintainability
Before (Traditional AI Generation):
Write spec (ambiguous) → AI generates code → Bug found
→ AI fixes bug → New bug appears → AI fixes → Original bug returns
→ 10+ iterations → Still buggy
After (Type-Driven Generation):
Write Idris2 types (precise) → Compiler verifies → Generate Python + tests
→ Tests pass → Done in 1-2 iterations
Reduction in debugging time: ~80%
- Business logic with complex validation
- Data transformation pipelines
- APIs with strict contracts
- Safety-critical code
- Refactoring legacy systems
- Prototype/throwaway code
- Simple CRUD operations
- UI/presentation layer
- Examples Guide - Learn from working examples
- Template Setup - Use this boilerplate in your project
- Full Specification - Complete methodology
- Project Configuration - Customization options
Contributions welcome! Especially:
-
New Examples
- Web API validation
- Parser combinators
- State machines
- Business rule engines
-
Target Languages
- TypeScript support
- Rust support
- Java support
-
Documentation
- Tutorials
- Best practices
- Pattern library
Built on the shoulders of giants:
- Idris2 and the dependent types community
- Claude Code for AI-assisted development
- Hypothesis for property-based testing
MIT License - see LICENSE file for details.
Free to use in commercial and open-source projects.
Made with ❤️ for type-driven development
⭐ Star this repo to bookmark it for your next project!