Liking cljdoc? Tell your friends :D

Contextual Completion - Formal Verification Documentation

Category: Contextual Completion Correctness Status: Proof-sketch documentation complete; contextual Rocq modules are not present in this repository yet Started: 2025-01-21 Dependencies: Core dictionary backends (Phase 10)


Table of Contents

  1. Overview
  2. Motivation
  3. Theorem Summary
  4. Reading Order
  5. Dependencies
  6. Implementation
  7. References

Overview

This category contains formal verification documentation for the contextual completion engine in liblevenshtein-rust. Contextual completion provides hierarchical, scope-aware symbol completion with draft buffers, undo/redo support, and efficient incremental updates.

Key Features Verified

  1. Hierarchical Contexts: Parent-child relationships with inheritance
  2. Draft Buffers: Incremental character-by-character updates
  3. Checkpoint Stacks: Undo/redo with exact state restoration
  4. Query Fusion: Correct merging of draft and finalized symbols
  5. Distance Calculation: Naive Levenshtein distance correctness
  6. Visibility Rules: Proper symbol visibility across context boundaries
  7. Finalization Atomicity: All-or-nothing draft→dictionary transitions

Motivation

The Problem

The rholang-language-server depends critically on liblevenshtein's contextual completion for:

  • LSP Code Completion: Scope-aware symbol suggestions while typing
  • Incremental Updates: Sub-millisecond response to keystrokes
  • Nested Scopes: Correct symbol visibility in contract/block hierarchies

Without formal verification, subtle bugs in context management could cause:

  • Missing completions: Symbols not visible in child scopes
  • Leaked symbols: Private variables visible in wrong scopes
  • Inconsistent state: Draft buffer desynchronization
  • Performance degradation: 10,000x slowdown if incremental updates break

The Solution

Formal verification provides mathematical certainty that:

  1. Context hierarchies correctly implement lexical scoping
  2. Draft buffers maintain UTF-8 validity and consistency
  3. Undo/redo operations are idempotent and complete
  4. Query results are sound and complete (no false positives/negatives)
  5. Distance calculations match formal Levenshtein definition
  6. Symbol visibility follows strict parent-child rules
  7. Finalization is atomic (no partial states observable)

Downstream Impact

These theorems unblock formal verification of:

  • rholang-language-server scope detection (depends on Theorems 1, 2, 4, 6)
  • LSP goto-definition (depends on Theorem 1, 6)
  • LSP refactoring/rename (depends on Theorem 3, 6, 7)
  • Code completion ranking (depends on Theorem 4, 5)

Theorem Summary

#TheoremPropertyStatusPages
1Context Tree Visibilityvisible_contexts(ctx) returns all ancestors in order🚧 Documenting4
2Draft Buffer ConsistencyInsert/delete maintain valid UTF-8 boundaries🚧 Documenting3
3Checkpoint Stack CorrectnessUndo restores exact previous state🚧 Documenting3
4Query Fusion Completenesscomplete() returns union of draft + finalized🚧 Documenting4
5Levenshtein Distance CorrectnessNaive $\mathcal{O}(n\cdot m)$ matches formal definition🚧 Documenting5
6Hierarchical Visibility SoundnessChildren see parents, parents don't see children🚧 Documenting3
7Finalization AtomicityDraft→dictionary is all-or-nothing🚧 Documenting4

Total: 7 main theorems, ~26 pages of formal documentation


Reading Order

For LSP Developers (Minimal Path)

Focused on properties needed for rholang-language-server scope detection:

  1. Theorem 1 (Context Visibility) - How context trees work
  2. Theorem 2 (Draft Consistency) - Incremental updates
  3. Theorem 4 (Query Fusion) - What complete() guarantees
  4. Theorem 6 (Hierarchical Visibility) - Scope isolation rules

Time: ~1 hour (14 pages)

For Library Maintainers (Complete Understanding)

Full coverage of all correctness properties:

  1. Theorem 1 → Establishes context tree foundation
  2. Theorem 6 → Builds on Theorem 1 for visibility rules
  3. Theorem 2 → Draft buffer operations
  4. Theorem 3 → Builds on Theorem 2 for undo/redo
  5. Theorem 7 → Builds on Theorem 2 for finalization
  6. Theorem 4 → Combines all context/draft/finalized properties
  7. Theorem 5 → Independent distance calculation verification

Time: ~2-3 hours (26 pages)

For Formal Verification Engineers

Start with the proof-sketch pages, then compare their assumptions with the existing Rocq core under rocq/liblevenshtein/:

  1. Read all markdown theorems first (overview)
  2. Study the checked non-contextual Rocq modules for naming, invariant, and proof-structure conventions
  3. If these sketches are promoted, build contextual modules in dependency order:
    • Visibility.v (Theorems 1, 6)
    • DraftBuffer.v (Theorems 2, 3)
    • Query.v (Theorems 4, 5)
    • Finalization.v (Theorem 7)

Dependencies

Theorem Dependencies (Within This Category)

Theorem 1 (Context Visibility)
    ↓
Theorem 6 (Hierarchical Visibility)
    ↓
Theorem 4 (Query Fusion) ← Theorem 2 (Draft Consistency)
                           ↑
                       Theorem 3 (Checkpoint) ← Theorem 2
                       Theorem 7 (Finalization) ← Theorem 2

Theorem 5 (Distance) - Independent

External Dependencies

These theorems assume correct dictionary backends:

  • Theorem 4 assumes Theorem 8 (Trie Reachability) from Category 07
  • Theorem 4 assumes Theorem 17 (Prefix Traversal) from Category 08
  • Theorem 7 assumes Theorem 10 (Dynamic Consistency) from Category 07

See docs/formal-verification/proofs/07_dictionary_backends/README.md for backend theorems.


Implementation

Source Files

Core Implementation (src/collection/dawg/contextual_completion/):

  • context.rs (lines 1-350) - Context tree structure

    • Context type definition
    • visible_contexts() method (Theorem 1, 6)
    • Parent-child management
  • draft_buffer.rs (lines 1-280) - Draft buffer operations

    • DraftBuffer type
    • insert_char(), delete_char() methods (Theorem 2)
    • UTF-8 boundary handling
  • checkpoint.rs (lines 1-180) - Checkpoint stack

    • Checkpoint type
    • save(), restore() methods (Theorem 3)
    • Stack invariants
  • engine.rs (lines 1-650) - Main completion engine

    • DynamicContextualCompletionEngine type
    • complete() method (Theorem 4)
    • levenshtein_distance() helper (Theorem 5)
    • finalize() method (Theorem 7)

Test Coverage

Unit Tests (src/collection/dawg/contextual_completion/mod.rs::#[cfg(test)]):

  • test_context_hierarchy_* - Context tree operations
  • test_draft_buffer_* - Incremental updates
  • test_checkpoint_* - Undo/redo
  • test_complete_* - Query fusion
  • test_distance_* - Levenshtein calculation
  • test_visibility_* - Hierarchical scoping
  • test_finalize_* - Atomicity guarantees

Integration Tests (rholang-language-server):

  • /home/dylon/Workspace/f1r3fly.io/rholang-language-server/tests/test_completion.rs
    • test_nested_scope_priority_inner - Tests Theorem 1, 4, 6
    • test_local_symbol_priority_inner - Tests Theorem 6
    • Completion performance tests - Test Theorem 2, 4

Property-test specifications:

  • Theorem 1, 6: Context visibility is transitive
  • Theorem 2: UTF-8 boundary preservation
  • Theorem 3: Undo-redo idempotence
  • Theorem 4: Query result set properties
  • Theorem 5: Distance triangle inequality

References

Theory Documents

Contextual Completion:

  • docs/algorithms/07-contextual-completion/README.md - Algorithm overview
  • docs/algorithms/07-contextual-completion/patterns/ - Design patterns
  • docs/algorithms/07-contextual-completion/implementation/ - Implementation notes

Related Algorithms:

  • docs/algorithms/06-zipper-navigation/README.md - PrefixZipper (Theorem 4 depends on this)
  • docs/algorithms/01-dictionary-layer/README.md - Dictionary backends (Theorem 7 depends on this)

Implementation Documentation

API Documentation:

  • Run cargo doc --open and navigate to:
    • liblevenshtein::collection::dawg::contextual_completion
    • liblevenshtein::collection::dawg::contextual_completion::engine::DynamicContextualCompletionEngine

Developer Guide:

  • docs/developer-guide/ - Contribution guidelines
  • docs/examples/06-contextual/ - Usage examples

Academic Background

Contextual Completion (general concept):

Levenshtein Distance:

Formal Verification:

  • /var/tmp/debug/f1r3node/docs/formal-verification/coq/ - Similar Rocq proofs for Rholang
  • Software Foundations - Coq tutorial
  • CompCert - Verified C compiler (similar methodology)

Downstream Projects

rholang-language-server:

  • /home/dylon/Workspace/f1r3fly.io/rholang-language-server/docs/completion_performance_summary.md
    • Documents how LSP uses contextual completion
    • Performance benchmarks (depends on Theorem 2, 4)
    • Phase 9 optimization (uses PrefixZipper from Theorem 17)

Scope-detection specifications:

  • /home/dylon/Workspace/f1r3fly.io/rholang-language-server/docs/formal-verification/scope-detection.md
    • Scope detection verification target
    • Depends on Theorems 1, 2, 4, 6 from this category

Roadmap

Phase 9.1: Documentation

Goal: Create markdown documentation for all 7 theorems

Status: Complete proof-sketch documentation

Progress:

  • [x] Category README.md (this file)
  • [x] Theorem 1: Context Visibility
  • [x] Theorem 2: Draft Consistency
  • [x] Theorem 3: Checkpoint Stack
  • [x] Theorem 4: Query Fusion
  • [x] Theorem 5: Distance Correctness
  • [x] Theorem 6: Hierarchical Visibility
  • [x] Theorem 7: Finalization Atomicity

Rocq Promotion Targets

Goal: Formalize types and theorems in Rocq

Candidate modules:

  • rocq/liblevenshtein/ContextualCompletion/Core.v - Type definitions
  • rocq/liblevenshtein/ContextualCompletion/Visibility.v - Theorems 1, 6
  • rocq/liblevenshtein/ContextualCompletion/DraftBuffer.v - Theorems 2, 3
  • rocq/liblevenshtein/ContextualCompletion/Query.v - Theorems 4, 5
  • rocq/liblevenshtein/ContextualCompletion/Finalization.v - Theorem 7

Priority order: Theorems 1, 2, 4 are the most critical for LSP integration.

Property-Test Promotion Targets

Goal: Add property-based tests matching formal specifications

Candidate coverage:

  • Property tests for all 7 theorems using proptest
  • Integration with Rust test suite
  • CI enforcement of properties

Contributing

Adding Proofs

  1. Document first: Create markdown file in this directory
  2. Formalize (optional): Add theorem to Rocq files
  3. Test: Add property-based tests to Rust implementation
  4. Review: Submit PR with all three components

Style Guidelines

Follow the established pattern from docs/formal-verification/proofs/01_subsumption_properties.md:

  • Sections: Overview, Definitions, Theorem Statement, Proof Sketch, Implementation, Test Coverage
  • Code blocks: Use Coq for formal statements, Rust for implementation
  • Diagrams: ASCII art where helpful
  • Cross-references: Link to source files (with line numbers) and tests

Changelog

2025-01-21 - Phase 9 Started

Added:

  • Category structure (proofs/06_contextual_completion/)
  • This README
  • Theorem documentation planned (7 files)

Status: Proof-sketch documentation complete; contextual Rocq modules are not present in this repository yet.


License

This formal verification work is part of the liblevenshtein-rust project and follows the same license (MIT/Apache-2.0).


Last Updated: 2025-01-21 Promotion Policy: These pages are proof-sketch artifacts unless matching checked Rocq modules are added to the manifest.

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