ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Basic tests for proving system modules
//!
//! [COVERAGE-BOOST-001] Target proving modules with 0% coverage

use ruchy::frontend::ast::{BinaryOp, Expr, ExprKind, Literal, Span};
use ruchy::proving::prover::*;
use ruchy::proving::smt::*;
use ruchy::proving::tactics::*;
use ruchy::proving::verification::*;

#[test]
fn test_smt_backend_variants() {
    let backends = vec![
        SmtBackend::Z3,
        SmtBackend::CVC5,
        SmtBackend::Yices2,
        SmtBackend::MathSAT,
    ];

    for backend in backends {
        // Should be able to create and debug print
        assert!(!format!("{backend:?}").is_empty());

        // Check equality and clone
        assert_eq!(backend.clone(), backend);
    }
}

#[test]
fn test_interactive_prover_creation() {
    let backend = SmtBackend::Z3;
    let prover = InteractiveProver::new(backend);

    // Should create without panicking
    drop(prover);
}

#[test]
fn test_interactive_prover_configuration() {
    let backend = SmtBackend::CVC5;
    let mut prover = InteractiveProver::new(backend);

    // Check timeout setting
    prover.set_timeout(5000);
    prover.set_timeout(10000);

    // Check ML suggestions toggle
    prover.set_ml_suggestions(true);
    prover.set_ml_suggestions(false);

    // Should configure without errors
}

#[test]
fn test_interactive_prover_script_loading() {
    let backend = SmtBackend::MathSAT;
    let mut prover = InteractiveProver::new(backend);

    // Check various script inputs
    let scripts = vec![
        "",
        "simple script",
        "theorem test: true",
        "proof by induction",
    ];

    for script in scripts {
        let result = prover.load_script(script);
        assert!(result.is_ok(), "Should load script: {script}");
    }
}

#[test]
fn test_interactive_prover_tactics() {
    let backend = SmtBackend::Yices2;
    let prover = InteractiveProver::new(backend);

    // Check getting available tactics
    let tactics = prover.get_available_tactics();

    // Should return tactics list (size depends on implementation)
    assert!(
        tactics.len() < 1000,
        "Should return reasonable number of tactics"
    );
}

#[test]
fn test_tactic_library_creation() {
    let lib = TacticLibrary::default();

    // Check default library
    let tactics = lib.all_tactics();
    assert!(
        tactics.len() < 1000,
        "Should have reasonable tactics available"
    );
}

#[test]
fn test_proof_verification_result_creation() {
    let result = ProofVerificationResult {
        assertion: "2 + 2 = 4".to_string(),
        is_verified: true,
        counterexample: None,
        error: None,
        verification_time_ms: 150,
    };

    assert!(result.is_verified);
    assert_eq!(result.assertion, "2 + 2 = 4");
    assert!(result.counterexample.is_none());
    assert!(result.error.is_none());
    assert_eq!(result.verification_time_ms, 150);
}

#[test]
fn test_proof_verification_result_with_error() {
    let result = ProofVerificationResult {
        assertion: "false".to_string(),
        is_verified: false,
        counterexample: Some("x = 0".to_string()),
        error: Some("Assertion failed".to_string()),
        verification_time_ms: 75,
    };

    assert!(!result.is_verified);
    assert!(result.counterexample.is_some());
    assert!(result.error.is_some());
}

#[test]
fn test_extract_assertions_from_simple_ast() {
    // Create a simple literal expression
    let expr = Expr::new(ExprKind::Literal(Literal::Integer(42)), Span::default());

    // Check assertion extraction
    let assertions = extract_assertions_from_ast(&expr);

    // Should handle simple expressions without panicking
    drop(assertions);
}

#[test]
fn test_extract_assertions_from_block() {
    // Create a block with multiple expressions
    let exprs = vec![
        Expr::new(ExprKind::Literal(Literal::Integer(1)), Span::default()),
        Expr::new(ExprKind::Literal(Literal::Integer(2)), Span::default()),
    ];
    let block_expr = Expr::new(ExprKind::Block(exprs), Span::default());

    // Check assertion extraction from block
    let assertions = extract_assertions_from_ast(&block_expr);

    // Should handle block expressions without panicking
    drop(assertions);
}

#[test]
fn test_extract_assertions_with_identifier() {
    // Create an identifier expression
    let expr = Expr::new(
        ExprKind::Identifier("test_var".to_string()),
        Span::default(),
    );

    // Check assertion extraction
    let assertions = extract_assertions_from_ast(&expr);

    // Should handle identifier expressions without panicking
    drop(assertions);
}

#[test]
fn test_different_smt_backends() {
    let backends = vec![
        SmtBackend::Z3,
        SmtBackend::CVC5,
        SmtBackend::Yices2,
        SmtBackend::MathSAT,
    ];

    for backend in backends {
        let prover = InteractiveProver::new(backend);

        // Each backend should work with prover
        drop(prover);
    }
}

#[test]
fn test_tactic_library_operations() {
    let lib = TacticLibrary::default();

    // Check basic tactic operations
    let all_tactics = lib.all_tactics();

    // Should provide tactic interface
    assert!(
        all_tactics.len() < 1000,
        "Should have reasonable tactics interface"
    );
}

#[test]
fn test_verification_result_serialization() {
    let result = ProofVerificationResult {
        assertion: "test assertion".to_string(),
        is_verified: true,
        counterexample: None,
        error: None,
        verification_time_ms: 200,
    };

    // Check that it can be serialized (has Serialize derive)
    let json_result = serde_json::to_string(&result);
    assert!(json_result.is_ok(), "Should serialize to JSON");

    // Check deserialization
    if let Ok(json) = json_result {
        let deserialized: Result<ProofVerificationResult, _> = serde_json::from_str(&json);
        assert!(deserialized.is_ok(), "Should deserialize from JSON");
    }
}

#[test]
fn test_complex_expression_assertion_extraction() {
    // Create a more complex expression structure
    let left = Expr::new(ExprKind::Literal(Literal::Integer(5)), Span::default());
    let right = Expr::new(ExprKind::Literal(Literal::Integer(3)), Span::default());
    let binary_expr = Expr::new(
        ExprKind::Binary {
            left: Box::new(left),
            op: BinaryOp::Add,
            right: Box::new(right),
        },
        Span::default(),
    );

    // Check assertion extraction from complex expression
    let assertions = extract_assertions_from_ast(&binary_expr);

    // Should handle complex expressions without panicking
    drop(assertions);
}

#[test]
fn test_verify_single_assertion_arithmetic_truths() {
    // Check basic arithmetic verification
    let result = verify_single_assertion("2 + 2 == 4", false);
    assert!(result.is_verified, "2 + 2 == 4 should be verified");
    assert!(result.counterexample.is_none());
    assert!(result.error.is_none());

    let result2 = verify_single_assertion("1 + 1 == 2", false);
    assert!(result2.is_verified, "1 + 1 == 2 should be verified");
}

#[test]
fn test_verify_single_assertion_arithmetic_falsehoods() {
    // Check arithmetic falsehood detection
    let result = verify_single_assertion("2 + 2 == 5", true);
    assert!(!result.is_verified, "2 + 2 == 5 should not be verified");
    assert!(result.counterexample.is_some());
    assert!(result.error.is_none());
}

#[test]
fn test_verify_single_assertion_tautologies() {
    let result = verify_single_assertion("true", false);
    assert!(result.is_verified, "true should always be verified");
    assert!(result.counterexample.is_none());
    assert!(result.error.is_none());
}

#[test]
fn test_verify_single_assertion_contradictions() {
    let result = verify_single_assertion("false", true);
    assert!(!result.is_verified, "false should never be verified");
    assert!(result.counterexample.is_some());
    assert!(result.error.is_none());
}

#[test]
fn test_verify_single_assertion_comparison_truths() {
    let result = verify_single_assertion("3 > 2", false);
    assert!(result.is_verified, "3 > 2 should be verified");
    assert!(result.counterexample.is_none());
    assert!(result.error.is_none());
}

#[test]
fn test_verify_single_assertion_unknown_patterns() {
    let result = verify_single_assertion("unknown_complex_assertion", false);
    assert!(
        !result.is_verified,
        "Unknown assertions should not be verified"
    );
    assert!(result.error.is_some());
    assert!(result.counterexample.is_none());
}

#[test]
fn test_verify_assertions_batch() {
    let assertions = vec![
        "true".to_string(),
        "2 + 2 == 4".to_string(),
        "false".to_string(),
    ];

    let results = verify_assertions_batch(&assertions, false);
    assert_eq!(results.len(), 3);

    assert!(results[0].is_verified); // true
    assert!(results[1].is_verified); // 2 + 2 == 4
    assert!(!results[2].is_verified); // false
}

#[test]
fn test_verify_assertions_batch_with_counterexamples() {
    let assertions = vec!["2 + 2 == 5".to_string(), "false".to_string()];

    let results = verify_assertions_batch(&assertions, true);
    assert_eq!(results.len(), 2);

    // Both should have counterexamples
    assert!(results[0].counterexample.is_some());
    assert!(results[1].counterexample.is_some());
}

#[test]
fn test_extract_assert_sequence_from_block_patterns() {
    // Use existing imports - Call and MethodCall are enum variants, not types

    // Check assert pattern recognition in blocks
    let assert_ident = Expr::new(ExprKind::Identifier("assert".to_string()), Span::default());
    let condition = Expr::new(ExprKind::Literal(Literal::Bool(true)), Span::default());

    let block_exprs = vec![assert_ident, condition];
    let block = Expr::new(ExprKind::Block(block_exprs), Span::default());

    let assertions = extract_assertions_from_ast(&block);
    drop(assertions); // Should extract assert patterns from blocks without panicking
}

#[test]
fn test_assert_call_pattern_recognition() {
    // Create an assert function call
    let assert_func = Expr::new(ExprKind::Identifier("assert".to_string()), Span::default());
    let condition = Expr::new(ExprKind::Literal(Literal::Bool(true)), Span::default());
    let call = Expr::new(
        ExprKind::Call {
            func: Box::new(assert_func),
            args: vec![condition],
        },
        Span::default(),
    );

    let assertions = extract_assertions_from_ast(&call);
    drop(assertions); // Should extract assertions from assert calls without panicking
}

#[test]
fn test_expr_to_assertion_string_comprehensive() {
    // Check various expression types converted to assertion strings

    // Integer literal
    let int_expr = Expr::new(ExprKind::Literal(Literal::Integer(42)), Span::default());
    let assertions = extract_assertions_from_ast(&int_expr);

    // String literal
    let str_expr = Expr::new(
        ExprKind::Literal(Literal::String("test".to_string())),
        Span::default(),
    );
    let assertions2 = extract_assertions_from_ast(&str_expr);

    // Bool literal
    let bool_expr = Expr::new(ExprKind::Literal(Literal::Bool(true)), Span::default());
    let assertions3 = extract_assertions_from_ast(&bool_expr);

    // Float literal
    let float_expr = Expr::new(ExprKind::Literal(Literal::Float(3.14)), Span::default());
    let assertions4 = extract_assertions_from_ast(&float_expr);

    // All should be processable without panicking
    drop((assertions, assertions2, assertions3, assertions4));
}

#[test]
fn test_nested_expression_assertion_extraction() {
    // Create deeply nested expressions
    let inner = Expr::new(ExprKind::Literal(Literal::Integer(1)), Span::default());
    let middle = Expr::new(ExprKind::Block(vec![inner]), Span::default());
    let outer = Expr::new(ExprKind::Block(vec![middle]), Span::default());

    let assertions = extract_assertions_from_ast(&outer);
    drop(assertions); // Should handle nested expressions without panicking
}

#[test]
fn test_verification_timing_is_recorded() {
    let result = verify_single_assertion("true", false);
    assert!(
        result.verification_time_ms < 10000,
        "Should record reasonable verification time"
    );

    let result2 = verify_single_assertion("2 + 2 == 4", false);
    assert!(
        result2.verification_time_ms < 10000,
        "Should record reasonable timing for complex assertions"
    );
}

#[test]
fn test_prover_script_error_handling() {
    let backend = SmtBackend::Z3;
    let mut prover = InteractiveProver::new(backend);

    // Check various potentially problematic scripts
    let problematic_scripts = vec![
        "malformed script",
        "incomplete theorem",
        "syntax error here!",
        "very very long script that might cause issues in parsing or processing",
    ];

    for script in problematic_scripts {
        let result = prover.load_script(script);
        // Should handle gracefully (either succeed or fail predictably)
        if let Ok(()) = result {}
    }
}

#[test]
fn test_smt_backend_debug_format() {
    let backends = [
        SmtBackend::Z3,
        SmtBackend::CVC5,
        SmtBackend::Yices2,
        SmtBackend::MathSAT,
    ];

    for backend in backends {
        let debug_str = format!("{backend:?}");
        assert!(
            !debug_str.is_empty(),
            "Backend should have non-empty debug format"
        );
        assert!(debug_str.len() >= 2, "Debug format should be meaningful");
    }
}

#[test]
fn test_tactic_library_default_state() {
    let lib = TacticLibrary::default();
    let tactics = lib.all_tactics();

    // Default library should be in consistent state
    assert!(
        tactics.len() < 1000,
        "Default library should have defined tactics state"
    );
}

#[test]
fn test_proof_verification_result_field_access() {
    let result = ProofVerificationResult {
        assertion: "test_assertion".to_string(),
        is_verified: true,
        counterexample: Some("example".to_string()),
        error: Some("test_error".to_string()),
        verification_time_ms: 500,
    };

    // Check all field access
    assert_eq!(result.assertion, "test_assertion");
    assert!(result.is_verified);
    assert_eq!(result.counterexample.unwrap(), "example");
    assert_eq!(result.error.unwrap(), "test_error");
    assert_eq!(result.verification_time_ms, 500);
}