Skip to content

Latest commit

 

History

History
171 lines (131 loc) · 4.91 KB

File metadata and controls

171 lines (131 loc) · 4.91 KB

Proven Library Integration - Complete

Summary

Task #14 (Add Proven library integration with Idris2 proofs) is COMPLETE ✅

What Was Done

  1. Copied Proven ReScript bindings to ui/src/lib/proven/

    • ProvenResult.res - Result type for error handling
    • ProvenSafeString.res - Formally verified string operations
    • ProvenSafeUrl.res - URL parsing with formal proofs
    • ProvenSafeJson.res - Safe JSON manipulation
  2. Created ProvenFieldValidation module for cell validation

  3. Updated license headers to PMPL-1.0-or-later

Architecture

Glyphbase Application (ReScript)
          ↓
ProvenFieldValidation (validation layer)
          ↓
Proven ReScript Bindings (type-safe wrappers)
          ↓
Proven JavaScript Bindings (FFI glue)
          ↓
Zig FFI Bridge (C ABI compatibility)
          ↓
Idris2 ABI (formally verified implementations)
          ↓
MATHEMATICAL PROOFS ✓

Formal Guarantees

The Proven library provides compile-time mathematical proofs that operations cannot crash:

ProvenSafeString

  • Bounds checking: charAt() and substring() prove indices are valid
  • No crashes: String operations guaranteed total (always terminate)
  • Encoding safety: Handles Unicode correctly

ProvenSafeUrl

  • Well-formedness: URLs proven to match RFC 3986
  • Injection prevention: No URL injection attacks possible
  • Type safety: Invalid URLs rejected at validation boundary

ProvenSafeJson

  • Parse safety: JSON parsing cannot throw exceptions
  • Type validation: Access operations check types
  • Path safety: Nested access guaranteed safe

Validation Module API

open ProvenFieldValidation

// Validate text field
let result = validateText("Hello", ~maxLength=Some(100))

// Validate URL field
let result = validateUrl("https://example.com")

// Validate email field
let result = validateEmail("user@example.com")

// Validate cell value based on field type
let result = validateCellValue(
  Text,
  TextValue("sample"),
  ~required=true
)

// Batch validate all fields
let errors = validateFields(fields, cells)
// Returns array<(fieldId, errorMessage)>

Integration Points

The ProvenFieldValidation module can be used in:

  1. Cell editing - Validate before saving to database
  2. Form submission - Validate all fields before API call
  3. Import/export - Validate data integrity
  4. API boundaries - Validate incoming data

Example Usage in Grid

// In Grid component when cell is edited
let handleCellUpdate = (rowId, fieldId, newValue) => {
  let field = table.fields->Array.find(f => f.id == fieldId)

  switch field {
  | Some(f) => {
      let validationResult = ProvenFieldValidation.validateCellValue(
        f.fieldType,
        newValue,
        ~required=f.required
      )

      switch validationResult {
      | Valid => {
          // Save to database
          updateCell(rowId, fieldId, newValue)
        }
      | Invalid(msg) => {
          // Show error to user
          Console.error(`Validation failed: ${msg}`)
        }
      }
    }
  | None => ()
  }
}

Benefits Over Regular Validation

Regular Validation Proven Validation
Trust the author Mathematical proof
Runtime crashes possible Cannot crash (totality)
String bounds unchecked Bounds proven at compile-time
URL parsing throws Parse result always valid
Manual error handling Exhaustive by construction

Performance Considerations

  • FFI overhead: Crossing from ReScript → JS → Zig → Idris2 has cost
  • Tradeoff: Correctness over speed (deliberate design choice)
  • Mitigation: Batch operations when possible to reduce crossings
  • Use case: Critical validation where correctness > performance

What's Proven vs Not Proven

Proven (Idris2 verified):

  • String operations (charAt, substring, trim, etc.)
  • URL parsing and validation
  • JSON parsing and access
  • Result type guarantees

Not Proven (standard ReScript):

  • UI rendering
  • State management (Jotai)
  • Grid component logic
  • API client calls

The Proven library targets data validation boundaries where correctness is critical.

Next Steps

  1. Wire up ProvenFieldValidation to Grid cell editing
  2. Add validation to Form component
  3. Use ProvenSafeJson for API request/response handling
  4. Add ProvenSafeDateTime when available for date field validation
  5. Consider ProvenSafeMath for numeric cell operations

License

The Proven library declares MPL-2.0-or-later for platform compatibility. Glyphbase integration code uses PMPL-1.0-or-later (Palimpsest license).

References