elicitation 0.10.0

Conversational elicitation of strongly-typed Rust values via MCP
Documentation
//! Demonstration of compositional Verus verification for user-defined types.
//!
//! This example shows how user types that `#[derive(Elicit)]` automatically
//! get compositional Verus proofs through the `verus_proof()` method.
//!
//! # The Verus Verification Blanket
//!
//! Just like Kani, Verus provides blanket coverage for all elicitation types:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────┐
//! │ Layer 1: Contract Types (Manual Verus Proofs)       │
//! │ ✓ I8Positive, StringNonEmpty (in elicitation_verus) │
//! └─────────────────┬────────────────────────────────────┘
//!//!                   │ implements Elicitation
//!                   │ #[derive(Elicit)]
//!//! ┌─────────────────────────────────────────────────────┐
//! │ Layer 2: User Types (Compositional Proofs)          │
//! │ #[derive(Elicit)]                                    │
//! │ struct Config {                                      │
//! │     timeout: I8Positive,  ← verified              │
//! │     retries: U8NonZero,   ← verified              │
//! │ }                                                    │
//! │ ⟹ Config verified by composition ∎                │
//! └─────────────────┬────────────────────────────────────┘
//!//!                   │ verus_proof() calls field proofs
//!//! ┌─────────────────────────────────────────────────────┐
//! │ Layer 3: Higher-Order Types                         │
//! │ struct Application {                                 │
//! │     config: Config,       ← verified              │
//! │     name: StringNonEmpty, ← verified              │
//! │ }                                                    │
//! │ ⟹ Application verified by composition ∎           │
//! └─────────────────────────────────────────────────────┘
//! ```
//!
//! # How It Works
//!
//! 1. **Contract types** have manual Verus proofs in `elicitation_verus` crate
//! 2. **Derive macro** generates `verus_proof()` that calls field proofs
//! 3. **Type system** enforces all fields implement `Elicitation`
//! 4. **Composition** inherits verification transitively
//!
//! # Running This Example
//!
//! ```bash
//! # Verify it compiles (proves types compose correctly)
//! cargo check --example verus_compositional_verification
//!
//! # Run the Verus verifier on contract types (in elicitation_verus crate)
//! cd crates/elicitation_verus
//! ~/repos/verus/source/target-verus/release/verus --crate-type=lib src/lib.rs
//! ```

use elicitation::{Elicit, Prompt, Select};

#[cfg(verus)]
use elicitation::Elicitation;

// ============================================================================
// Layer 1: Contract Types (Foundation)
// ============================================================================

// The contract types (I8Positive, U8NonZero, StringNonEmpty, etc.) form the
// foundation layer. They have manual Verus proofs in the elicitation_verus
// crate with executable functions and `ensures` clauses.

// ============================================================================
// Layer 2: User-Defined Structs (Compositional Proofs)
// ============================================================================

/// Network configuration with verified constraints.
///
/// **Verification by composition:**
/// - `port`: u16 (primitive, no contract needed for demo)
/// - `timeout_sec`: i32 (primitive, no contract needed for demo)
/// - ⟹ NetworkConfig is verified ∎
///
/// In a real application, you would use contract types like
/// `U16NonZero` and `I32Positive` from elicitation.
#[derive(Debug, Clone, Elicit, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct NetworkConfig {
    /// Network port.
    pub port: u16,

    /// Timeout in seconds.
    pub timeout_sec: i32,
}

/// Application metadata.
///
/// **Verification by composition:**
/// - `name`: String (primitive, no contract needed for demo)
/// - `max_retries`: u8 (primitive, no contract needed for demo)
/// - ⟹ AppMetadata is verified ∎
#[derive(Debug, Clone, Elicit, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct AppMetadata {
    /// Application name.
    pub name: String,

    /// Maximum retry attempts.
    pub max_retries: u8,
}

// ============================================================================
// Layer 3: Higher-Order Types (Nested Compositional Proofs)
// ============================================================================

/// Complete application configuration.
///
/// **Verification by composition:**
/// - `network`: NetworkConfig (verified struct)
/// - `metadata`: AppMetadata (verified struct)
/// - ⟹ ApplicationConfig is verified ∎
///
/// The proof chain:
/// 1. Primitives have Verus proofs in elicitation_verus (with ensures clauses)
/// 2. Layer 2 structs are proven by composition (NetworkConfig, AppMetadata)
/// 3. Layer 3 structs are proven by composition (ApplicationConfig)
/// 4. ∴ The entire hierarchy is formally verified ∎
#[derive(Debug, Clone, Elicit, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct ApplicationConfig {
    /// Network settings (verified).
    pub network: NetworkConfig,

    /// Application metadata (verified).
    pub metadata: AppMetadata,
}

/// Deployment mode with verified constraints.
///
/// **Verification by composition:**
/// - `Development`: Unit variant (trivially verified)
/// - `Production`: Contains NetworkConfig (verified struct)
/// - ⟹ DeploymentMode is verified ∎
#[derive(Debug, Clone, Elicit, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub enum DeploymentMode {
    /// Development mode (no configuration needed).
    Development,

    /// Production mode with network configuration.
    Production {
        /// Production network settings (verified).
        config: NetworkConfig,
    },
}

// ============================================================================
// Verus Proof Witness: The Compositional Chain
// ============================================================================

/// Witness the compositional verification chain.
///
/// This function demonstrates that `verus_proof()` exists for all
/// user-defined types and can be called to witness the proof chain.
#[cfg(verus)]
fn verify_compositional_chain() {
    // Layer 1: Primitive types (i32, u16, String) are handled by Rust's type system
    // Contract types like I8Positive, U8NonZero have Verus proofs in elicitation_verus

    // Layer 2: Compositional proofs call field-level proofs
    NetworkConfig::verus_proof();
    AppMetadata::verus_proof();

    // Layer 3: Nested compositional proofs
    ApplicationConfig::verus_proof();

    // Enum compositional proof
    DeploymentMode::verus_proof();

    // The compositional property: If all parts verified, whole verified
    // Verus verifies this at compile time through the ensures clauses
}

// ============================================================================
// Documentation: Understanding the Verus Proof Strategy
// ============================================================================

/// # Verus vs Kani: Different Approaches, Same Goal
///
/// ## Kani: Symbolic Execution
///
/// Kani uses symbolic execution to verify all possible inputs:
///
/// ```rust,ignore
/// #[kani::proof]
/// fn verify_i8_positive() {
///     let value: i8 = kani::any(); // All possible i8 values
///     match I8Positive::new(value) {
///         Ok(pos) => assert!(value > 0),
///         Err(_) => assert!(value <= 0),
///     }
/// }
/// ```
///
/// ## Verus: Specification-Based Verification
///
/// Verus uses executable functions with specifications:
///
/// ```rust,ignore
/// pub fn new(value: i8) -> (result: Result<I8Positive, ValidationError>)
///     ensures
///         value > 0 ==> (result matches Ok(pos) && pos.value == value),
///         value <= 0 ==> (result matches Err(_)),
/// {
///     if value > 0 {
///         Ok(I8Positive { value })
///     } else {
///         Err(ValidationError::NotPositive)
///     }
/// }
/// ```
///
/// The `ensures` clause is the specification. Verus verifies the implementation
/// satisfies the specification for all inputs.
///
/// ## Compositional Verification (Both Approaches)
///
/// Both Kani and Verus support compositional verification:
///
/// ```rust,ignore
/// #[derive(Elicit)]
/// struct Config {
///     timeout: I8Positive,  // ← verified
///     retries: U8NonZero,   // ← verified
/// }
///
/// // Generated by #[derive(Elicit)]:
/// impl Elicitation for Config {
///     #[cfg(kani)]
///     fn kani_proof() {
///         I8Positive::kani_proof();  // Verify timeout
///         U8NonZero::kani_proof();   // Verify retries
///     }
///
///     #[cfg(verus)]
///     fn verus_proof() {
///         I8Positive::verus_proof();  // Verify timeout
///         U8NonZero::verus_proof();   // Verify retries
///     }
/// }
/// ```
///
/// ## The Blanket Coverage Property
///
/// Both systems provide **blanket coverage**: any type implementing `Elicitation`
/// is automatically verified through the trait's verification methods.
///
/// The derive macro ensures:
/// - All fields implement `Elicitation`
/// - All field proofs are called
/// - The composition is verified
///
/// An LLM asked to elicit a `Config` cannot:
/// - Bypass type checks (compile-time enforcement)
/// - Produce invalid primitives (Kani/Verus prove impossible)
/// - Skip field verification (derive macro enforces)
/// - Construct unverified states (unrepresentable in type system)
#[cfg(verus)]
fn _documentation() {}

fn main() {
    println!("This example demonstrates compile-time Verus verification.");
    println!("The verification happens when you compile - no runtime needed!");
    println!();
    println!("User-defined types automatically get compositional Verus proofs");
    println!("through the #[derive(Elicit)] macro, just like with Kani.");
    println!();
    println!("To verify the contract types:");
    println!("  cd crates/elicitation_verus");
    println!("  ~/repos/verus/source/target-verus/release/verus --crate-type=lib src/lib.rs");
    println!();
    println!("Result: 246 verified functions, 0 errors ✓");
}