ruchy 5.0.0-beta.2

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
//! Prove Command Handler
//!
//! Handles interactive theorem proving and formal verification commands.

use anyhow::Result;
use std::path::Path;

/// Handle interactive theorem prover (RUCHY-0820)
///
/// Delegates to the refactored handlers_modules::prove module.
///
/// # Arguments
/// * `file` - Optional path to file to prove
/// * `backend` - Prover backend to use
/// * `ml_suggestions` - Enable ML-powered suggestions
/// * `timeout` - Proof timeout in seconds
/// * `script` - Optional proof script file
/// * `export` - Optional path to export proofs
/// * `check` - Check mode only
/// * `counterexample` - Generate counterexamples
/// * `verbose` - Enable verbose output
/// * `format` - Output format
///
/// # Errors
/// Returns error if proving fails
pub fn handle_prove_command(
    file: Option<&Path>,
    backend: &str,
    ml_suggestions: bool,
    timeout: u64,
    script: Option<&Path>,
    export: Option<&Path>,
    check: bool,
    counterexample: bool,
    verbose: bool,
    format: &str,
) -> Result<()> {
    // Delegate to refactored module with ≤10 complexity
    super::handlers_modules::prove::handle_prove_command(
        file,
        backend,
        ml_suggestions,
        timeout,
        script,
        export,
        check,
        counterexample,
        verbose,
        format,
    )
}

/// Verify proofs extracted from AST
///
/// Delegates to the prove_helpers module for AST-based proof verification.
///
/// # Arguments
/// * `ast` - The AST expression to verify
/// * `file_path` - Path to the source file
/// * `format` - Output format
/// * `counterexample` - Generate counterexamples
/// * `verbose` - Enable verbose output
///
/// # Errors
/// Returns error if proof verification fails
pub fn verify_proofs_from_ast(
    ast: &ruchy::frontend::ast::Expr,
    file_path: &Path,
    format: &str,
    counterexample: bool,
    verbose: bool,
) -> Result<()> {
    super::handlers_modules::prove_helpers::verify_proofs_from_ast(
        ast,
        file_path,
        format,
        counterexample,
        verbose,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;
    use tempfile::TempDir;

    // PMAT-104: `handle_prove_command` with `file: None` and `check: false`
    // enters the interactive prover, which reads the *process* stdin. Under
    // `cargo test` that never returns, so every such test blocked the whole
    // `--bin ruchy` target. These tests exercise the same handler through
    // `handle_prove_command_with_input`, which takes the reader explicitly, so
    // the real interactive path is still covered but terminates at EOF.
    use super::super::handlers_modules::prove::handle_prove_command_with_input;

    /// An interactive session that is immediately at end of input.
    fn eof_input() -> Cursor<Vec<u8>> {
        Cursor::new(Vec::new())
    }

    #[test]
    fn test_prove_handler_stub() {
        // Prove handler tests are in handlers_modules::prove
        // This is a placeholder for the delegation layer
    }

    // ===== EXTREME TDD Round 146 - Prove Handler Tests =====

    #[test]
    fn test_handle_prove_command_no_file() {
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            30,
            None,
            None,
            false,
            false,
            false,
            "text",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_nonexistent_file() {
        let result = handle_prove_command(
            Some(Path::new("/nonexistent/file.ruchy")),
            "default",
            false,
            30,
            None,
            None,
            false,
            false,
            false,
            "text",
        );
        assert!(
            result.is_err(),
            "proving a file that does not exist must be an error"
        );
    }

    #[test]
    fn test_handle_prove_command_with_ml_suggestions() {
        let result = handle_prove_command_with_input(
            None,
            "default",
            true, // ml_suggestions
            60,
            None,
            None,
            false,
            false,
            false,
            "text",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_check_mode() {
        let result = handle_prove_command(
            None, "default", false, 30, None, None, true, // check
            false, false, "text",
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_counterexample() {
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            30,
            None,
            None,
            false,
            true, // counterexample
            false,
            "text",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_verbose() {
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            30,
            None,
            None,
            false,
            false,
            true, // verbose
            "text",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_json_format() {
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            30,
            None,
            None,
            false,
            false,
            false,
            "json",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_various_backends() {
        let backends = ["default", "z3", "smt", "custom"];
        for backend in &backends {
            let result = handle_prove_command_with_input(
                None,
                backend,
                false,
                30,
                None,
                None,
                false,
                false,
                false,
                "text",
                &mut eof_input(),
            );
            assert!(result.is_ok(), "backend {backend} must be accepted");
        }
    }

    #[test]
    fn test_handle_prove_command_various_timeouts() {
        let timeouts = [1, 10, 30, 60, 300, 3600];
        for timeout in &timeouts {
            let result = handle_prove_command_with_input(
                None,
                "default",
                false,
                *timeout,
                None,
                None,
                false,
                false,
                false,
                "text",
                &mut eof_input(),
            );
            assert!(result.is_ok(), "timeout {timeout} must be accepted");
        }
    }

    #[test]
    fn test_handle_prove_command_with_script() {
        let temp_dir = TempDir::new().unwrap();
        let script_path = temp_dir.path().join("proof_script.txt");
        std::fs::write(&script_path, "intro\napply simplify").unwrap();
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            30,
            Some(&script_path),
            None,
            false,
            false,
            false,
            "text",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_with_export() {
        let temp_dir = TempDir::new().unwrap();
        let export_path = temp_dir.path().join("proof_export.txt");
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            30,
            None,
            Some(&export_path),
            false,
            false,
            false,
            "text",
            &mut eof_input(),
        );
        assert!(result.is_ok());
        assert!(
            export_path.exists(),
            "an export path must be written when the session ends"
        );
    }

    #[test]
    fn test_handle_prove_command_all_options() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.ruchy");
        std::fs::write(&file_path, "42").unwrap();
        let result = handle_prove_command(
            Some(&file_path),
            "z3",
            true,
            120,
            None,
            None,
            true,
            true,
            true,
            "json",
        );
        assert!(result.is_ok());
    }

    // ===== EXTREME TDD Round 153 - Prove Handler Tests =====

    #[test]
    fn test_handle_prove_command_with_function() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("func.ruchy");
        std::fs::write(&file_path, "fun add(a, b) { a + b }").unwrap();
        let result = handle_prove_command(
            Some(&file_path),
            "default",
            false,
            30,
            None,
            None,
            false,
            false,
            false,
            "text",
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_zero_timeout() {
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            0, // zero timeout
            None,
            None,
            false,
            false,
            false,
            "text",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_all_true_flags() {
        let result = handle_prove_command(
            None, "default", true, // ml_suggestions
            30, None, None, true, // check
            true, // counterexample
            true, // verbose
            "json",
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_handle_prove_command_xml_format() {
        let result = handle_prove_command_with_input(
            None,
            "default",
            false,
            30,
            None,
            None,
            false,
            false,
            false,
            "xml",
            &mut eof_input(),
        );
        assert!(result.is_ok());
    }

    /// The interactive session must also honour an explicit `quit` command
    /// coming from a non-tty reader, not just EOF.
    #[test]
    fn test_handle_prove_command_quit_from_reader() {
        let mut input = Cursor::new(b"help\nquit\n".to_vec());
        let result = handle_prove_command_with_input(
            None, "default", false, 30, None, None, false, false, false, "text", &mut input,
        );
        assert!(result.is_ok());
    }
}