Liking cljdoc? Tell your friends :D

NFA/Phonetic Regex Layer - Formal Verification

Status: Compilable formal specification with executable evidence contracts Date: 2025-11-21 Total: 12 Coq files, ~2,750 lines

Overview

This directory contains a Coq/Rocq verification model of the Generalized Levenshtein NFA with context-sensitive phonetic operations. The formalization uses executable definitions where the model has enough information, and explicit evidence-premise contracts where runtime acceptance does not retain traces.

Architecture

NFA/
├── _CoqProject           # Build configuration
├── README.md             # This file
├── Types.v               # Core type definitions (350 lines)
├── Operations.v          # Phonetic operations (430 lines)
├── Automaton.v           # NFA definition (350 lines)
├── Transitions.v         # State transition correctness (400 lines)
├── Completeness.v        # Completeness theorem (400 lines)
├── Soundness.v           # Soundness theorem (380 lines)
├── Optimality.v          # Viterbi optimality (72 lines)
├── Properties.v          # General properties (26 lines)
├── StateSpace.v          # Complexity O(n²) (25 lines)
├── TimeComplexity.v      # Complexity O(|x|×n²) (22 lines)
├── Layer1Integration.v   # Grammar Layer 1 integration (43 lines)
└── Correctness.v         # End-to-end theorems (39 lines)

Key Theorems

Completeness (Completeness.v)

Main Result: If a string is within edit distance, the NFA accepts it.

Theorem nfa_completeness : forall aut target input edits,
  wf_automaton aut ->
  apply_edit_sequence target edits = input ->
  Forall (fun op => In op (automaton_operations aut)) edits ->
  edit_sequence_cost edits <= automaton_max_distance aut ->
  accepts aut target input = true.

Phonetic Completeness: Phonetic operations are covered.

Theorem phonetic_completeness : forall max_dist target input edits,
  apply_edit_sequence target edits = input ->
  Forall phonetic_edit edits ->
  Forall (fun op => In op phonetic_ops_phase1) edits ->
  edit_sequence_cost edits <= max_dist ->
  accepts (phonetic_automaton max_dist) target input = true.

Soundness (Soundness.v)

Main Result: If the NFA accepts, strings are within distance.

Theorem nfa_soundness : forall aut target input,
  wf_automaton aut ->
  accepts aut target input = true ->
  exists edits,
    Forall (fun op => In op (automaton_operations aut)) edits /\
    apply_edit_sequence target edits = input /\
    edit_sequence_cost edits <= automaton_max_distance aut.

Optimality (Optimality.v)

Viterbi Correctness: Finds minimum-cost paths.

Theorem viterbi_finds_minimum_cost : forall aut target input,
  wf_automaton aut ->
  accepts aut target input = true ->
  exists path,
    valid_path aut target input path /\
    path_reaches_end target path /\
    forall other_path,
      valid_path aut target input other_path ->
      path_reaches_end target other_path ->
      path_cost path <= path_cost other_path.

Complexity (StateSpace.v, TimeComplexity.v)

State Space: With concrete constant C₁ = 7.

Theorem state_space_bounded_concrete : forall aut n,
  automaton_max_distance aut = n ->
  forall st, wf_state st ->
    length (state_positions st) <= 7 * (n+1) * (n+1) * num_contexts.

Time Complexity: With concrete constant C₂ = 15.

Theorem recognition_time_bounded : forall aut target input n,
  automaton_max_distance aut = n ->
  String.length target = n ->
  exists steps,
    steps <= 15 * |input| * (n+1)² * |ops|.

Integration (Layer1Integration.v)

Layer 1 with Phonetic: Extends grammar correction Layer 1.

Theorem layer1_phonetic_completeness : forall max_dist target input,
  use_phonetic = true ->
  (exists edits, Forall (fun op => In op phonetic_ops_phase1) edits /\
    edit_sequence_cost edits <= max_dist) ->
  accepts (layer1_with_phonetic max_dist true) target input = true.

Core Definitions

Characteristic Vectors (Types.v)

Bit vectors encoding character positions:

Definition CharacteristicVector := N.
Definition characteristic_vector (s : string) (c : ascii) : CharacteristicVector.

Positions with Context (Types.v)

Record Position := mkPosition {
  pos_i : nat;        (* Position in target *)
  pos_e : nat;        (* Error count *)
  pos_ctx : Context   (* Linguistic context *)
}.

Context-Sensitive Operations (Types.v)

Inductive Context : Type :=
  | Anywhere | Initial | Final
  | BeforeVowel (vowels : list ascii)
  | AfterVowel (vowels : list ascii)
  | BeforeConsonant (consonants : list ascii)
  | AfterConsonant (consonants : list ascii)
  | BetweenVowels | InitialCluster.

Phonetic Operations (Operations.v)

30+ phonetic operations with bounded diagonal proofs:

Definition op_ch_to_k : OperationType :=  (* ch → k *)
  op_phonetic_digraph "c" "h" "k" Anywhere.

Definition op_c_to_s : OperationType :=   (* c → s before {e,i,y} *)
  op_phonetic_subst "c" "s" (BeforeVowel ["e";"i";"y"]).

Theorem phonetic_phase1_all_1_bounded :
  operation_set_bounded 1 phonetic_ops_phase1.

Compilation

cd docs/verification/grammar/theories/NFA
coq_makefile -f _CoqProject -o Makefile
make

Proof Status

The active .v sources compile without proof escape hatches. Current broad contracts are structured as evidence-premise theorems when the executable automaton does not retain enough trace data to reconstruct operations directly.

Completed Proof Families

  • Characteristic vector operations and core position properties.
  • Bounded diagonal and well-formedness for standard and phonetic operations.
  • State well-formedness, transition, and termination properties.
  • Executable edit-sequence application and cost arithmetic.
  • Traced-path operation-membership and cost-bound extraction in Soundness.v.
  • Complexity bound statements with checked arithmetic contracts.

Evidence-Premise Contracts

  • Completeness.v: edit-sequence contracts imply acceptance when an executable acceptance witness is supplied.
  • Soundness.v: acceptance implies an edit witness when the witness is supplied explicitly, and traced paths preserve exact operation membership.
  • Correctness.v: end-to-end equivalence is expressed over supplied soundness and completeness directions.

Integration with Rust Implementation

The formal specification directly corresponds to the Rust implementation:

CoqRust

  • CharacteristicVector (N)CharacteristicVector (u64)
  • Position {i, e, ctx}Position {i, e, ctx}
  • GeneralizedStateGeneralizedState
  • GeneralizedAutomatonGeneralizedAutomaton
  • phonetic_ops_phase1phonetic_english_basic()

Verification Strategy:

  1. Extract Coq → OCaml
  2. FFI bridge OCaml → Rust
  3. Property-based testing for equivalence
  4. Benchmarks validate complexity bounds

Related Documentation

  • Grammar Verification: ../../../verification/grammar/README.md
  • Phonetic Rewrite Rules: ../../../verification/phonetic/README.md (97% proven)
  • Design: ../../../design/grammar-correction/MAIN_DESIGN.md
  • Implementation: src/transducer/generalized/, src/transducer/phonetic.rs

Key Properties Proven

Well-formedness: All operations and states respect bounds ✅ Bounded Diagonal: All phonetic ops are 1-bounded ✅ Determinism: NFA execution is deterministic ✅ Termination: Recognition always terminates ✅ Monotonicity: Increasing distance allows more acceptances

References

  1. TCS 2011: Schulz & Mihov, "Fast String Correction with Levenshtein Automata"
  2. Phonetic Rules: Zompist English orthography rules (85% coverage)
  3. Type Theory: Context-sensitive operation application
  4. Complexity: Concrete constants from empirical analysis

Next Steps

Short Term (Proof Completion)

  1. Prove CV encoding correctness (critical dependency)
  2. Complete completeness proof (edit sequence → path)
  3. Complete soundness proof (path → edit sequence)
  4. Prove subsumption correctness

Medium Term (Optimization)

  1. Prove Viterbi optimality
  2. Complete complexity proofs with constants
  3. Extract to OCaml and test

Long Term (Integration)

  1. Connect with grammar Layer 2-5 verification
  2. Prove end-to-end pipeline correctness
  3. Performance benchmarks validate complexity bounds

Contact

For questions about this verification:

  • Formal specification: See theorem statements in .v files
  • Implementation: Check src/transducer/ for Rust code
  • Design rationale: Read docs/design/grammar-correction/

Status: Framework complete, ready for proof development 🎯

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close