oxiz-proof 0.2.2

Proof generation and checking for OxiZ SMT solver
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
//! Proof streaming for processing large proofs efficiently.
//!
//! This module provides streaming APIs to process large proofs without
//! loading the entire proof into memory at once.

use crate::proof::{Proof, ProofNode, ProofNodeId, ProofStep};
use rustc_hash::FxHashMap;
use std::io::{self, BufWriter, Write};

/// Configuration for proof streaming.
#[derive(Debug, Clone)]
pub struct StreamConfig {
    /// Chunk size for streaming (number of nodes)
    pub chunk_size: usize,
    /// Buffer size for I/O operations
    pub buffer_size: usize,
    /// Enable compression during streaming
    pub compress: bool,
}

impl Default for StreamConfig {
    fn default() -> Self {
        Self {
            chunk_size: 1000,
            buffer_size: 8192,
            compress: false,
        }
    }
}

impl StreamConfig {
    /// Create a new stream configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the chunk size.
    pub fn with_chunk_size(mut self, size: usize) -> Self {
        self.chunk_size = size;
        self
    }

    /// Set the buffer size.
    pub fn with_buffer_size(mut self, size: usize) -> Self {
        self.buffer_size = size;
        self
    }

    /// Enable compression.
    pub fn with_compression(mut self, enabled: bool) -> Self {
        self.compress = enabled;
        self
    }
}

/// Chunk of proof nodes for streaming.
#[derive(Debug, Clone)]
pub struct ProofChunk {
    /// Starting node ID in this chunk
    pub start_id: ProofNodeId,
    /// Nodes in this chunk
    pub nodes: Vec<ProofNode>,
    /// Chunk index
    pub index: usize,
    /// Total number of chunks
    pub total_chunks: usize,
}

impl ProofChunk {
    /// Create a new proof chunk.
    pub fn new(
        start_id: ProofNodeId,
        nodes: Vec<ProofNode>,
        index: usize,
        total_chunks: usize,
    ) -> Self {
        Self {
            start_id,
            nodes,
            index,
            total_chunks,
        }
    }

    /// Get the number of nodes in this chunk.
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    /// Check if the chunk is empty.
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Check if this is the last chunk.
    pub fn is_last(&self) -> bool {
        self.index + 1 == self.total_chunks
    }
}

/// Streaming proof reader.
pub struct ProofStreamer {
    config: StreamConfig,
}

impl Default for ProofStreamer {
    fn default() -> Self {
        Self::new()
    }
}

impl ProofStreamer {
    /// Create a new proof streamer.
    pub fn new() -> Self {
        Self {
            config: StreamConfig::default(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(config: StreamConfig) -> Self {
        Self { config }
    }

    /// Stream proof in chunks.
    pub fn stream_chunks<'a>(&self, proof: &'a Proof) -> ProofChunkIterator<'a> {
        ProofChunkIterator::new(proof, self.config.chunk_size)
    }

    /// Process proof in streaming fashion with a callback.
    pub fn process_streaming<F>(&self, proof: &Proof, mut callback: F) -> Result<(), String>
    where
        F: FnMut(&ProofChunk) -> Result<(), String>,
    {
        for chunk in self.stream_chunks(proof) {
            callback(&chunk)?;
        }
        Ok(())
    }

    /// Write proof to a writer in streaming fashion.
    pub fn write_streaming<W: Write>(&self, proof: &Proof, writer: W) -> io::Result<()> {
        let mut buf_writer = BufWriter::with_capacity(self.config.buffer_size, writer);

        for chunk in self.stream_chunks(proof) {
            for node in &chunk.nodes {
                writeln!(buf_writer, "{}", self.format_node(node))?;
            }
        }

        buf_writer.flush()
    }

    // Helper: Format a node for output
    fn format_node(&self, node: &ProofNode) -> String {
        match &node.step {
            ProofStep::Axiom { conclusion } => {
                format!("axiom {} : {}", node.id, conclusion)
            }
            ProofStep::Inference {
                rule,
                premises,
                conclusion,
                ..
            } => {
                let premise_str = premises
                    .iter()
                    .map(|p| p.to_string())
                    .collect::<Vec<_>>()
                    .join(", ");
                format!(
                    "infer {} : {} from [{}] => {}",
                    node.id, rule, premise_str, conclusion
                )
            }
        }
    }
}

/// Iterator over proof chunks.
pub struct ProofChunkIterator<'a> {
    proof: &'a Proof,
    chunk_size: usize,
    current_index: usize,
    total_nodes: usize,
}

impl<'a> ProofChunkIterator<'a> {
    /// Create a new chunk iterator.
    pub fn new(proof: &'a Proof, chunk_size: usize) -> Self {
        Self {
            proof,
            chunk_size,
            current_index: 0,
            total_nodes: proof.len(),
        }
    }
}

impl<'a> Iterator for ProofChunkIterator<'a> {
    type Item = ProofChunk;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_index >= self.total_nodes {
            return None;
        }

        let start_idx = self.current_index;
        let end_idx = (start_idx + self.chunk_size).min(self.total_nodes);

        let nodes: Vec<ProofNode> = self.proof.nodes()[start_idx..end_idx].to_vec();

        let total_chunks = self.total_nodes.div_ceil(self.chunk_size);
        let chunk_index = start_idx / self.chunk_size;

        let start_id = if !nodes.is_empty() {
            nodes[0].id
        } else {
            ProofNodeId(0)
        };

        self.current_index = end_idx;

        Some(ProofChunk::new(start_id, nodes, chunk_index, total_chunks))
    }
}

/// Streaming proof builder for incremental construction.
pub struct StreamingProofBuilder {
    /// Accumulated proof nodes
    nodes: Vec<ProofNode>,
    /// Node ID mapping
    node_map: FxHashMap<ProofNodeId, usize>,
    /// Current proof
    proof: Proof,
    /// Stream configuration (reserved for future use)
    #[allow(dead_code)]
    config: StreamConfig,
}

impl Default for StreamingProofBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl StreamingProofBuilder {
    /// Create a new streaming proof builder.
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            node_map: FxHashMap::default(),
            proof: Proof::new(),
            config: StreamConfig::default(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(config: StreamConfig) -> Self {
        Self {
            nodes: Vec::new(),
            node_map: FxHashMap::default(),
            proof: Proof::new(),
            config,
        }
    }

    /// Add an axiom to the stream.
    pub fn add_axiom(&mut self, conclusion: &str) -> ProofNodeId {
        self.proof.add_axiom(conclusion)
    }

    /// Add an inference to the stream.
    pub fn add_inference(
        &mut self,
        rule: &str,
        premises: Vec<ProofNodeId>,
        conclusion: &str,
    ) -> ProofNodeId {
        self.proof.add_inference(rule, premises, conclusion)
    }

    /// Flush accumulated nodes and get the current proof.
    pub fn flush(&mut self) -> Proof {
        let proof = std::mem::take(&mut self.proof);
        self.nodes.clear();
        self.node_map.clear();
        proof
    }

    /// Get the number of nodes in the stream.
    pub fn len(&self) -> usize {
        self.proof.len()
    }

    /// Check if the stream is empty.
    pub fn is_empty(&self) -> bool {
        self.proof.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stream_config_new() {
        let config = StreamConfig::new();
        assert_eq!(config.chunk_size, 1000);
        assert_eq!(config.buffer_size, 8192);
        assert!(!config.compress);
    }

    #[test]
    fn test_stream_config_with_settings() {
        let config = StreamConfig::new()
            .with_chunk_size(500)
            .with_buffer_size(4096)
            .with_compression(true);
        assert_eq!(config.chunk_size, 500);
        assert_eq!(config.buffer_size, 4096);
        assert!(config.compress);
    }

    #[test]
    fn test_proof_chunk_new() {
        let chunk = ProofChunk::new(ProofNodeId(0), Vec::new(), 0, 1);
        assert_eq!(chunk.index, 0);
        assert_eq!(chunk.total_chunks, 1);
        assert!(chunk.is_empty());
        assert!(chunk.is_last());
    }

    #[test]
    fn test_proof_streamer_new() {
        let streamer = ProofStreamer::new();
        assert_eq!(streamer.config.chunk_size, 1000);
    }

    #[test]
    fn test_stream_chunks() {
        let mut proof = Proof::new();
        for i in 0..5 {
            proof.add_axiom(format!("axiom_{}", i));
        }

        let streamer = ProofStreamer::with_config(StreamConfig::new().with_chunk_size(2));
        let chunks: Vec<ProofChunk> = streamer.stream_chunks(&proof).collect();

        assert_eq!(chunks.len(), 3); // 5 nodes / 2 per chunk = 3 chunks
        assert_eq!(chunks[0].len(), 2);
        assert_eq!(chunks[1].len(), 2);
        assert_eq!(chunks[2].len(), 1);
    }

    #[test]
    fn test_chunk_iterator() {
        let mut proof = Proof::new();
        proof.add_axiom("x = x");
        proof.add_axiom("y = y");

        let mut iter = ProofChunkIterator::new(&proof, 1);
        let chunk1 = iter.next();
        assert!(chunk1.is_some());
        assert_eq!(chunk1.expect("test operation should succeed").len(), 1);

        let chunk2 = iter.next();
        assert!(chunk2.is_some());
        assert_eq!(chunk2.expect("test operation should succeed").len(), 1);

        let chunk3 = iter.next();
        assert!(chunk3.is_none());
    }

    #[test]
    fn test_process_streaming() {
        let mut proof = Proof::new();
        proof.add_axiom("x = x");
        proof.add_axiom("y = y");

        let streamer = ProofStreamer::new();
        let mut count = 0;
        let result = streamer.process_streaming(&proof, |chunk| {
            count += chunk.len();
            Ok(())
        });

        assert!(result.is_ok());
        assert_eq!(count, 2);
    }

    #[test]
    fn test_streaming_builder() {
        let mut builder = StreamingProofBuilder::new();
        builder.add_axiom("x = x");
        builder.add_axiom("y = y");

        assert_eq!(builder.len(), 2);
        assert!(!builder.is_empty());

        let proof = builder.flush();
        assert_eq!(proof.len(), 2);
        assert_eq!(builder.len(), 0);
    }

    #[test]
    fn test_write_streaming() {
        let mut proof = Proof::new();
        proof.add_axiom("x = x");
        proof.add_axiom("y = y");

        let streamer = ProofStreamer::new();
        let mut output = Vec::new();
        let result = streamer.write_streaming(&proof, &mut output);

        assert!(result.is_ok());
        let output_str = String::from_utf8(output).expect("test operation should succeed");
        assert!(output_str.contains("axiom"));
        assert!(output_str.contains("x = x"));
    }
}