Skip to content

Latest commit

 

History

History
327 lines (238 loc) · 7.47 KB

File metadata and controls

327 lines (238 loc) · 7.47 KB

Idris2 Type-Driven Code Generation Boilerplate

Eliminate infinite AI debugging loops with dependent types

License: MIT Idris2 Python

A production-ready boilerplate for AI-assisted code generation using Idris2's dependent type system.

English | 한국어

🎯 The Problem

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

✨ Our Solution

Natural Language → AI → Idris2 (Dependent Types) → Python + Tests
                   ↑                             ↓
                   └───────── Compiler ──────────┘

Use dependent types as a specification language for code generation.

🚀 Quick Start

1. Clone This Repository

git clone https://github.com/twoLoop-40/idris2-python-boilerplate.git
cd idris2-python-boilerplate

2. Install Dependencies

# Install Idris2
brew install idris2  # macOS
# or see: https://idris2.readthedocs.io/

# Install Python dependencies
pip install -r requirements.txt

3. Run Setup

bash .claude/setup_project.sh

4. Try the Examples

# 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!

💡 Key Features

Type-Driven Development

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 Int

Automatic Python Conversion

Types 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
    # ...

Comprehensive Test Generation

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) == n

🎓 Examples

Example 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

🛠️ Use in Your Project

Option 1: Copy Template

cp -r idris2-python-boilerplate ~/my-project
cd ~/my-project
bash .claude/setup_project.sh

Option 2: Copy Configuration Only

cd /path/to/your/project
cp -r idris2-python-boilerplate/.claude .
bash .claude/setup_project.sh

Customize

Edit .claude/project_config.yaml:

project:
  name: "MyProject"

target:
  language: "python"  # or typescript, rust

domain_types:
  UserId:
    python: "uuid.UUID"
  EmailAddress:
    python: "pydantic.EmailStr"

🔧 Workflow

1. Write Idris2 Specification

-- src/UserValidation.idr
data ValidUser : Type where
  MkValid : (email : Email)
         -> (age : Nat)
         -> {auto prf : age >= 18}
         -> ValidUser

2. Convert to Python (in Claude Code)

/convert src/UserValidation.idr

3. Get Generated Code + Tests

# 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)

🌟 Benefits

For AI Code Generation

  • ✅ Precise, unambiguous specifications
  • ✅ Immediate compiler feedback
  • ✅ Drastically reduced debugging cycles
  • ✅ Automatic edge case discovery

For Code Quality

  • ✅ Comprehensive runtime checks
  • ✅ Self-documenting code
  • ✅ High test coverage (auto-generated)
  • ✅ Provable correctness properties

For Development

  • ✅ Faster iteration
  • ✅ Higher confidence
  • ✅ Easier refactoring
  • ✅ Better maintainability

📊 Real-World Impact

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%

🎯 Use Cases

✅ Perfect For

  • Business logic with complex validation
  • Data transformation pipelines
  • APIs with strict contracts
  • Safety-critical code
  • Refactoring legacy systems

⚠️ Less Ideal For

  • Prototype/throwaway code
  • Simple CRUD operations
  • UI/presentation layer

📖 Documentation

🤝 Contributing

Contributions welcome! Especially:

  1. New Examples

    • Web API validation
    • Parser combinators
    • State machines
    • Business rule engines
  2. Target Languages

    • TypeScript support
    • Rust support
    • Java support
  3. Documentation

    • Tutorials
    • Best practices
    • Pattern library

📚 Learn More

🙏 Credits

Built on the shoulders of giants:

📄 License

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!