elicitation 0.8.0

Conversational elicitation of strongly-typed Rust values via MCP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Mechanism-level verification contracts for elicitation methods.
//!
//! These contracts verify that elicitation *mechanisms* work correctly,
//! independent of the types being elicited.
//!
//! # Key Insight
//!
//! We verify two layers:
//! 1. **Mechanism layer**: Survey returns valid enum variant
//! 2. **Type layer**: u32 is non-zero
//!
//! These compose: If mechanism proven AND type proven, the whole chain is proven.

use crate::traits::Elicitation;
use crate::verification::Contract;

// ============================================================================
// Survey Mechanism Contracts
// ============================================================================

/// Contract for Survey mechanism: Returns valid enum variant.
///
/// Verifies that Survey elicitation returns one of the declared enum variants.
///
/// # Type Parameters
///
/// * `E` - Enum type being surveyed
///
/// # Properties
///
/// - **Precondition**: None (always callable)
/// - **Postcondition**: Output is a valid variant of E
/// - **Invariant**: E has at least one variant
///
/// # Example
///
/// ```rust,ignore
/// enum Priority { Low, Medium, High }
///
/// let contract = SurveyReturnsValidVariant::<Priority>;
/// let result = Priority::Low;
/// assert!(SurveyReturnsValidVariant::<Priority>::ensures(&(), &result));
/// ```
#[derive(Debug, Clone, Copy)]
pub struct SurveyReturnsValidVariant<E> {
    _phantom: std::marker::PhantomData<E>,
}

impl<E> SurveyReturnsValidVariant<E> {
    /// Create new Survey mechanism contract.
    pub const fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<E> Default for SurveyReturnsValidVariant<E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<E> Contract for SurveyReturnsValidVariant<E>
where
    E: Elicitation + Clone + std::fmt::Debug + Send + PartialEq,
{
    type Input = E;
    type Output = E;

    fn requires(_input: &E) -> bool {
        // No precondition - Survey always callable
        true
    }

    fn ensures(_input: &E, output: &E) -> bool {
        // Postcondition: output is a valid instance of E
        // For enums, this is trivially true (Rust's type system guarantees it)
        // But we make it explicit for formal verification

        // We can't enumerate variants generically in Rust,
        // but the type system guarantees output is a valid E
        let _ = output;
        true
    }

    fn invariant(&self) -> bool {
        // Invariant: Type E exists and is constructible
        true
    }
}

// ============================================================================
// Affirm Mechanism Contracts
// ============================================================================

/// Contract for Affirm mechanism: Returns boolean.
///
/// Verifies that Affirm elicitation returns a valid boolean value.
///
/// # Properties
///
/// - **Precondition**: None (always callable)
/// - **Postcondition**: Output is true or false
/// - **Invariant**: Boolean domain is {true, false}
///
/// # Example
///
/// ```rust,ignore
/// let contract = AffirmReturnsBoolean;
/// assert!(AffirmReturnsBoolean::ensures(&(), &true));
/// assert!(AffirmReturnsBoolean::ensures(&(), &false));
/// ```
#[derive(Debug, Clone, Copy)]
pub struct AffirmReturnsBoolean;

impl Contract for AffirmReturnsBoolean {
    type Input = bool;
    type Output = bool;

    fn requires(_input: &bool) -> bool {
        true
    }

    fn ensures(_input: &bool, _output: &bool) -> bool {
        // Postcondition: always satisfied (bool is always valid)
        true
    }

    fn invariant(&self) -> bool {
        true
    }
}

// ============================================================================
// Text Mechanism Contracts
// ============================================================================

/// Contract for Text mechanism: Returns string.
///
/// Verifies that Text elicitation returns a string value.
///
/// # Variants
///
/// - `TextReturnsString`: Any string (including empty)
/// - `TextReturnsNonEmpty`: Non-empty string only
///
/// # Properties
///
/// - **Precondition**: None (always callable)
/// - **Postcondition**: Output is a valid string
/// - **Invariant**: String is UTF-8 encoded
#[derive(Debug, Clone, Copy)]
pub struct TextReturnsString;

impl Contract for TextReturnsString {
    type Input = String;
    type Output = String;

    fn requires(_input: &String) -> bool {
        true
    }

    fn ensures(_input: &String, output: &String) -> bool {
        // Postcondition: output is a valid string (always satisfied)
        let _ = output;
        true
    }

    fn invariant(&self) -> bool {
        true
    }
}

/// Contract for Text mechanism: Returns non-empty string.
///
/// Stronger variant that guarantees non-empty output.
#[derive(Debug, Clone, Copy)]
pub struct TextReturnsNonEmpty;

impl Contract for TextReturnsNonEmpty {
    type Input = String;
    type Output = String;

    fn requires(_input: &String) -> bool {
        true
    }

    fn ensures(_input: &String, output: &String) -> bool {
        // Postcondition: output is non-empty
        !output.is_empty()
    }

    fn invariant(&self) -> bool {
        true
    }
}

// ============================================================================
// Numeric Mechanism Contracts
// ============================================================================

/// Contract for Numeric mechanism: Returns number in valid range.
///
/// Verifies that numeric elicitation returns value within type bounds.
///
/// # Type Parameters
///
/// * `T` - Numeric type (i32, u64, etc.)
///
/// # Properties
///
/// - **Precondition**: None (always callable)
/// - **Postcondition**: Output is within T::MIN..=T::MAX
/// - **Invariant**: Type T has defined bounds
#[derive(Debug, Clone, Copy)]
pub struct NumericReturnsValid<T> {
    _phantom: std::marker::PhantomData<T>,
}

impl<T> NumericReturnsValid<T> {
    /// Create new Numeric mechanism contract.
    pub const fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T> Default for NumericReturnsValid<T> {
    fn default() -> Self {
        Self::new()
    }
}

// Implement for signed integers
macro_rules! impl_numeric_contract {
    ($t:ty) => {
        impl Contract for NumericReturnsValid<$t> {
            type Input = $t;
            type Output = $t;

            fn requires(_input: &$t) -> bool {
                true
            }

            fn ensures(_input: &$t, output: &$t) -> bool {
                // Postcondition: output is valid $t (always satisfied by type system)
                *output >= <$t>::MIN && *output <= <$t>::MAX
            }

            fn invariant(&self) -> bool {
                true
            }
        }
    };
}

impl_numeric_contract!(i8);
impl_numeric_contract!(i16);
impl_numeric_contract!(i32);
impl_numeric_contract!(i64);
impl_numeric_contract!(i128);
impl_numeric_contract!(isize);
impl_numeric_contract!(u8);
impl_numeric_contract!(u16);
impl_numeric_contract!(u32);
impl_numeric_contract!(u64);
impl_numeric_contract!(u128);
impl_numeric_contract!(usize);

// ============================================================================
// Composition: Mechanism + Type Contracts
// ============================================================================

/// Composed contract: Mechanism guarantees + Type constraints.
///
/// Verifies both that:
/// 1. The elicitation mechanism works correctly
/// 2. The resulting value satisfies type-specific constraints
///
/// # Example
///
/// ```rust,ignore
/// // Mechanism: Numeric returns valid i32
/// // Type: i32 must be positive
/// let contract = MechanismWithType::new(
///     NumericReturnsValid::<i32>::new(),
///     I32Positive
/// );
/// ```
#[derive(Debug, Clone, Copy)]
pub struct MechanismWithType<M, T> {
    /// Mechanism-level contract
    pub mechanism: M,
    /// Type-level contract
    pub type_contract: T,
}

impl<M, T> MechanismWithType<M, T> {
    /// Create new composed mechanism + type contract.
    pub const fn new(mechanism: M, type_contract: T) -> Self {
        Self {
            mechanism,
            type_contract,
        }
    }
}

impl<M, T, I, O> Contract for MechanismWithType<M, T>
where
    M: Contract<Input = I, Output = O>,
    T: Contract<Input = I, Output = O>,
    I: Elicitation + Clone + std::fmt::Debug + Send,
    O: Elicitation + Clone + std::fmt::Debug + Send,
{
    type Input = I;
    type Output = O;

    fn requires(input: &I) -> bool {
        // Precondition: Both contracts must accept the input
        M::requires(input) && T::requires(input)
    }

    fn ensures(input: &I, output: &O) -> bool {
        // Postcondition: BOTH contracts must hold
        // 1. Mechanism contract (output is valid for mechanism)
        M::ensures(input, output) &&
        // 2. Type contract (output satisfies type constraints)
        T::ensures(input, output)
    }

    fn invariant(&self) -> bool {
        // Invariant: Both contracts maintain their invariants
        self.mechanism.invariant() && self.type_contract.invariant()
    }
}

// ============================================================================
// Input Mechanism Contracts
// ============================================================================

/// Contract for Input mechanism: Returns non-empty input.
#[derive(Debug, Clone, Copy)]
pub struct InputNonEmpty;

impl Contract for InputNonEmpty {
    type Input = String;
    type Output = String;

    fn requires(_input: &String) -> bool {
        true
    }

    fn ensures(_input: &String, output: &String) -> bool {
        !output.is_empty()
    }

    fn invariant(&self) -> bool {
        true
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::verification::contracts::I32Positive;

    #[test]
    fn test_affirm_mechanism() {
        let input = true;
        assert!(AffirmReturnsBoolean::requires(&input));
        assert!(AffirmReturnsBoolean::ensures(&input, &true));
        assert!(AffirmReturnsBoolean::ensures(&input, &false));
    }

    #[test]
    fn test_text_mechanism() {
        let input = String::new();
        assert!(TextReturnsString::requires(&input));
        assert!(TextReturnsString::ensures(&input, &String::from("hello")));
        assert!(TextReturnsString::ensures(&input, &String::new()));
    }

    #[test]
    fn test_text_non_empty_mechanism() {
        let input = String::new();
        assert!(TextReturnsNonEmpty::requires(&input));
        assert!(TextReturnsNonEmpty::ensures(&input, &String::from("hello")));
        assert!(!TextReturnsNonEmpty::ensures(&input, &String::new()));
    }

    #[test]
    fn test_numeric_mechanism() {
        let input = 0i32;
        assert!(NumericReturnsValid::<i32>::requires(&input));
        assert!(NumericReturnsValid::<i32>::ensures(&input, &42));
        assert!(NumericReturnsValid::<i32>::ensures(&input, &-1));
        assert!(NumericReturnsValid::<i32>::ensures(&input, &i32::MIN));
        assert!(NumericReturnsValid::<i32>::ensures(&input, &i32::MAX));
    }

    #[test]
    fn test_mechanism_with_type_composition() {
        let mechanism = NumericReturnsValid::<i32>::new();
        let type_contract = I32Positive;
        let contract = MechanismWithType::new(mechanism, type_contract);

        // Positive values pass both mechanism and type contracts
        let positive = 42i32;
        assert!(
            MechanismWithType::<NumericReturnsValid<i32>, I32Positive>::ensures(
                &positive, &positive
            )
        );

        // Negative values fail type contract (but pass mechanism)
        let negative = -1i32;
        assert!(
            !MechanismWithType::<NumericReturnsValid<i32>, I32Positive>::ensures(
                &negative, &negative
            )
        );

        assert!(contract.invariant());
    }
}