Skip to content

[Automation] Auto-generate property manifest from Lean files #72

@Th0rgal

Description

@Th0rgal

Summary

Automate property manifest generation from Lean theorem declarations, eliminating manual maintenance and sync errors.

Problem

Current State:

  • Property manifest (property_manifest.json) manually maintained
  • Prone to sync errors between code and manifest
  • check_property_manifest.py validates but doesn't generate
  • Manual overhead when adding new theorems
  • Easy to forget to update manifest

Example Error:

# Developer adds theorem
theorem new_property : ... := by ...

# Forgets to update manifest
# CI fails on next push

Proposed Solution

Create scripts/extract_properties.py

#!/usr/bin/env python3
"""Auto-extract theorem names from Lean proof files"""

import re
import json
from pathlib import Path

def extract_theorems(lean_file: Path) -> list[str]:
    """Parse Lean file for theorem declarations"""
    content = lean_file.read_text()
    
    # Pattern matches: theorem <name> ...
    pattern = r'^\s*theorem\s+(\w+)\s*[:(]'
    theorems = re.findall(pattern, content, re.MULTILINE)
    
    return theorems

def generate_manifest() -> dict:
    """Auto-generate property manifest from all proof files"""
    contracts = [
        'SimpleStorage',
        'Counter', 
        'Ledger',
        'SimpleToken',
        'Authorization',
        'CryptoHash',
        'Marketplace'
    ]
    
    manifest = {}
    
    for contract in contracts:
        proof_file = Path(f'DumbContracts/Specs/{contract}/Proofs.lean')
        
        if not proof_file.exists():
            continue
            
        theorems = extract_theorems(proof_file)
        
        manifest[contract] = {
            'file': str(proof_file),
            'theorems': theorems,
            'count': len(theorems)
        }
    
    return manifest

def main():
    manifest = generate_manifest()
    print(json.dumps(manifest, indent=2))

if __name__ == '__main__':
    main()

Update CI Workflow

# .github/workflows/verify.yml
- name: Generate property manifest
  run: python3 scripts/extract_properties.py > property_manifest_auto.json

- name: Check manifest is up to date
  run: |
    if ! diff property_manifest.json property_manifest_auto.json; then
      echo "ERROR: Property manifest is out of sync"
      echo "Run: python3 scripts/extract_properties.py > property_manifest.json"
      exit 1
    fi

Enhanced Features

Feature 1: Extract property tags

# Parse docstrings for property metadata
def extract_property_metadata(lean_file: Path) -> dict:
    """Extract theorem + metadata from docstrings"""
    pattern = r'/--\s*@property\s+(\w+).*?-/\s*theorem\s+(\w+)'
    # Returns: {theorem_name: {tags: [...], description: "..."}}

Feature 2: Validate test coverage

def check_test_coverage(manifest: dict) -> bool:
    """Ensure every theorem has corresponding test"""
    for contract, data in manifest.items():
        test_file = f'test/Property{contract}.t.sol'
        for theorem in data['theorems']:
            if not has_test(test_file, theorem):
                print(f"Missing test for {contract}::{theorem}")
                return False
    return True

Feature 3: Generate coverage report

def generate_coverage_report(manifest: dict) -> str:
    """Generate markdown coverage report"""
    total = sum(data['count'] for data in manifest.values())
    return f"Total Properties: {total}\n" + ...

Benefits

✅ Eliminates manual errors
✅ Automatically detects new theorems
✅ Reduces maintenance burden
✅ Improves CI reliability
✅ Real-time coverage tracking
✅ Enforces documentation standards

Implementation Plan

Phase 1: Basic Extraction (2 days)

  • Implement extract_theorems()
  • Generate manifest JSON
  • CLI tool

Phase 2: CI Integration (1 day)

  • Add to GitHub Actions workflow
  • Fail on out-of-sync manifest
  • Auto-commit option

Phase 3: Enhanced Features (2 days)

  • Metadata extraction
  • Test coverage validation
  • Coverage report generation

Files Affected

Create:

  • scripts/extract_properties.py

Update:

  • .github/workflows/verify.yml
  • scripts/check_property_manifest.py (use auto-generation)
  • property_manifest.json (becomes auto-generated)

Dependencies

None - uses standard Python libraries

Estimated Effort

1 week

Success Criteria

  • Script extracts all theorems correctly
  • CI validates manifest sync
  • Eliminates manual manifest updates
  • Coverage report auto-generated
  • Documentation updated

Related Issues

Future Enhancements

  • Extract proof strategies from comments
  • Identify sorry placeholders
  • Estimate proof complexity
  • Generate proof skeleton

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions