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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Enhanced Coq Proof Export with Complete Proof Terms.
//!
//! This module extends the basic Coq exporter with full proof term generation,
//! supporting theory-specific reasoning and eliminating placeholders.

use crate::proof::{Proof, ProofNode, ProofNodeId, ProofStep};
use rustc_hash::FxHashMap;

/// Enhanced Coq proof exporter with full proof term support.
#[derive(Debug)]
#[allow(dead_code)] // Under development
pub struct EnhancedCoqExporter {
    /// Mapping from proof nodes to Coq identifiers
    node_to_ident: FxHashMap<ProofNodeId, String>,
    /// Mapping from proof nodes to their proof terms
    node_to_proof_term: FxHashMap<ProofNodeId, CoqProofTerm>,
    /// Counter for generating unique names
    name_counter: usize,
    /// Generated type definitions
    type_defs: Vec<String>,
    /// Generated tactic definitions
    tactic_defs: Vec<String>,
}

/// A Coq proof term (fully elaborated).
#[derive(Debug, Clone)]
pub enum CoqProofTerm {
    /// Variable reference
    Var(String),
    /// Application of proof term to arguments
    App {
        function: Box<CoqProofTerm>,
        args: Vec<CoqProofTerm>,
    },
    /// Lambda abstraction
    Lambda {
        params: Vec<(String, CoqType)>,
        body: Box<CoqProofTerm>,
    },
    /// Assumption (axiom)
    Assumption(String),
    /// Apply tactic
    Tactic { name: String, args: Vec<String> },
    /// Exact proof term
    Exact(Box<CoqProofTerm>),
}

/// Coq type representation.
#[derive(Debug, Clone)]
pub enum CoqType {
    /// Prop (propositions)
    Prop,
    /// Type universe
    Type,
    /// Function type (A -> B)
    Arrow(Box<CoqType>, Box<CoqType>),
    /// Named type
    Named(String),
    /// Application of type constructor
    App(Box<CoqType>, Vec<CoqType>),
}

impl CoqProofTerm {
    /// Convert proof term to Coq syntax
    pub fn to_coq(&self) -> String {
        match self {
            CoqProofTerm::Var(v) => v.clone(),
            CoqProofTerm::App { function, args } => {
                let func_str = function.to_coq();
                let args_str = args
                    .iter()
                    .map(|a| a.to_coq())
                    .collect::<Vec<_>>()
                    .join(" ");
                if args.is_empty() {
                    func_str
                } else {
                    format!("({} {})", func_str, args_str)
                }
            }
            CoqProofTerm::Lambda { params, body } => {
                let params_str = params
                    .iter()
                    .map(|(name, ty)| format!("({} : {})", name, ty.to_coq()))
                    .collect::<Vec<_>>()
                    .join(" ");
                format!("(fun {} => {})", params_str, body.to_coq())
            }
            CoqProofTerm::Assumption(name) => format!("assumption ({})", name),
            CoqProofTerm::Tactic { name, args } => {
                if args.is_empty() {
                    name.clone()
                } else {
                    format!("{} {}", name, args.join(" "))
                }
            }
            CoqProofTerm::Exact(term) => format!("exact {}", term.to_coq()),
        }
    }
}

impl CoqType {
    /// Convert type to Coq syntax
    pub fn to_coq(&self) -> String {
        match self {
            CoqType::Prop => "Prop".to_string(),
            CoqType::Type => "Type".to_string(),
            CoqType::Arrow(a, b) => format!("({} -> {})", a.to_coq(), b.to_coq()),
            CoqType::Named(n) => n.clone(),
            CoqType::App(ty, args) => {
                let ty_str = ty.to_coq();
                let args_str = args
                    .iter()
                    .map(|a| a.to_coq())
                    .collect::<Vec<_>>()
                    .join(" ");
                format!("({} {})", ty_str, args_str)
            }
        }
    }
}

impl EnhancedCoqExporter {
    /// Create a new enhanced Coq exporter
    pub fn new() -> Self {
        Self {
            node_to_ident: FxHashMap::default(),
            node_to_proof_term: FxHashMap::default(),
            name_counter: 0,
            type_defs: Vec::new(),
            tactic_defs: Vec::new(),
        }
    }

    /// Generate a fresh identifier
    fn fresh_ident(&mut self, prefix: &str) -> String {
        let ident = format!("{}_{}", prefix, self.name_counter);
        self.name_counter += 1;
        ident
    }

    /// Build proof term for a node
    fn build_proof_term(&mut self, node: &ProofNode, premises: &[ProofNodeId]) -> CoqProofTerm {
        match &node.step {
            ProofStep::Axiom { conclusion: _ } => {
                // Axioms are assumptions
                let ident = self
                    .node_to_ident
                    .get(&node.id)
                    .cloned()
                    .unwrap_or_else(|| format!("axiom_{}", node.id.0));
                CoqProofTerm::Assumption(ident)
            }
            ProofStep::Inference {
                rule,
                premises: _,
                conclusion: _,
                ..
            } => {
                // Build proof term based on inference rule
                self.build_inference_proof_term(rule, premises)
            }
        }
    }

    /// Build proof term for specific inference rules
    fn build_inference_proof_term(&mut self, rule: &str, premises: &[ProofNodeId]) -> CoqProofTerm {
        match rule {
            "resolution" => {
                // Resolution: (C1 ∨ p) → (C2 ∨ ¬p) → (C1 ∨ C2)
                if premises.len() >= 2 {
                    let p1 = premises
                        .first()
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| CoqProofTerm::Var(s.clone()))
                        .unwrap_or_else(|| CoqProofTerm::Var("H1".to_string()));
                    let p2 = premises
                        .get(1)
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| CoqProofTerm::Var(s.clone()))
                        .unwrap_or_else(|| CoqProofTerm::Var("H2".to_string()));

                    CoqProofTerm::Tactic {
                        name: "apply resolution_rule".to_string(),
                        args: vec![p1.to_coq(), p2.to_coq()],
                    }
                } else {
                    CoqProofTerm::Tactic {
                        name: "auto".to_string(),
                        args: vec![],
                    }
                }
            }
            "modus_ponens" | "mp" => {
                // Modus ponens: P → (P → Q) → Q
                if premises.len() >= 2 {
                    let p = premises
                        .first()
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| CoqProofTerm::Var(s.clone()))
                        .unwrap_or_else(|| CoqProofTerm::Var("H1".to_string()));
                    let impl_pq = premises
                        .get(1)
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| CoqProofTerm::Var(s.clone()))
                        .unwrap_or_else(|| CoqProofTerm::Var("H2".to_string()));

                    CoqProofTerm::App {
                        function: Box::new(impl_pq),
                        args: vec![p],
                    }
                } else {
                    CoqProofTerm::Tactic {
                        name: "auto".to_string(),
                        args: vec![],
                    }
                }
            }
            "and_intro" => {
                // And introduction: P → Q → (P ∧ Q)
                if premises.len() >= 2 {
                    let p1 = premises
                        .first()
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| CoqProofTerm::Var(s.clone()))
                        .unwrap_or_else(|| CoqProofTerm::Var("H1".to_string()));
                    let p2 = premises
                        .get(1)
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| CoqProofTerm::Var(s.clone()))
                        .unwrap_or_else(|| CoqProofTerm::Var("H2".to_string()));

                    CoqProofTerm::Tactic {
                        name: "split".to_string(),
                        args: vec![p1.to_coq(), p2.to_coq()],
                    }
                } else {
                    CoqProofTerm::Tactic {
                        name: "auto".to_string(),
                        args: vec![],
                    }
                }
            }
            "refl" | "eq_refl" => {
                // Reflexivity: ∀x. x = x
                CoqProofTerm::Tactic {
                    name: "reflexivity".to_string(),
                    args: vec![],
                }
            }
            "symm" | "eq_symm" => {
                // Symmetry: x = y → y = x
                if let Some(&premise) = premises.first() {
                    if let Some(ident) = self.node_to_ident.get(&premise) {
                        CoqProofTerm::Tactic {
                            name: "symmetry".to_string(),
                            args: vec![ident.clone()],
                        }
                    } else {
                        CoqProofTerm::Tactic {
                            name: "symmetry".to_string(),
                            args: vec![],
                        }
                    }
                } else {
                    CoqProofTerm::Tactic {
                        name: "symmetry".to_string(),
                        args: vec![],
                    }
                }
            }
            "trans" | "eq_trans" => {
                // Transitivity: x = y → y = z → x = z
                if premises.len() >= 2 {
                    let p1_ident = premises
                        .first()
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| s.as_str())
                        .unwrap_or("H1");
                    let p2_ident = premises
                        .get(1)
                        .and_then(|&id| self.node_to_ident.get(&id))
                        .map(|s| s.as_str())
                        .unwrap_or("H2");

                    CoqProofTerm::Tactic {
                        name: "transitivity".to_string(),
                        args: vec![p1_ident.to_string(), p2_ident.to_string()],
                    }
                } else {
                    CoqProofTerm::Tactic {
                        name: "auto".to_string(),
                        args: vec![],
                    }
                }
            }
            "congruence" | "cong" => {
                // Congruence: x = y → f(x) = f(y)
                CoqProofTerm::Tactic {
                    name: "congruence".to_string(),
                    args: vec![],
                }
            }
            "lia" | "linear_arithmetic" => {
                // Linear integer arithmetic
                CoqProofTerm::Tactic {
                    name: "lia".to_string(),
                    args: vec![],
                }
            }
            "lra" | "linear_real_arithmetic" => {
                // Linear real arithmetic
                CoqProofTerm::Tactic {
                    name: "lra".to_string(),
                    args: vec![],
                }
            }
            _ => {
                // Generic inference: apply all premises
                let premise_terms: Vec<CoqProofTerm> = premises
                    .iter()
                    .filter_map(|&id| {
                        self.node_to_ident
                            .get(&id)
                            .map(|s| CoqProofTerm::Var(s.clone()))
                    })
                    .collect();

                if premise_terms.is_empty() {
                    CoqProofTerm::Tactic {
                        name: "auto".to_string(),
                        args: vec![],
                    }
                } else if premise_terms.len() == 1 {
                    CoqProofTerm::Exact(Box::new(premise_terms[0].clone()))
                } else {
                    CoqProofTerm::Tactic {
                        name: "auto".to_string(),
                        args: premise_terms.iter().map(|t| t.to_coq()).collect(),
                    }
                }
            }
        }
    }

    /// Export a proof node to Coq with full proof term
    fn export_node(&mut self, _proof: &Proof, node_id: ProofNodeId, node: &ProofNode) -> String {
        let ident = self.fresh_ident("step");
        self.node_to_ident.insert(node_id, ident.clone());

        match &node.step {
            ProofStep::Axiom { conclusion } => {
                format!("Axiom {} : Prop_of_string \"{}\".", ident, conclusion)
            }
            ProofStep::Inference {
                rule,
                premises,
                conclusion,
                ..
            } => {
                let proof_term = self.build_proof_term(node, premises);
                self.node_to_proof_term.insert(node_id, proof_term.clone());

                let premise_idents: Vec<String> = premises
                    .iter()
                    .filter_map(|&p| self.node_to_ident.get(&p).cloned())
                    .collect();

                let prop = format!("Prop_of_string \"{}\"", conclusion);

                if premise_idents.is_empty() {
                    format!("Axiom {} : {}.", ident, prop)
                } else {
                    let premises_str = premise_idents.join(" -> ");
                    format!(
                        "Lemma {} : {} -> {}.\nProof.\n  (* Rule: {} *)\n  {}.\nQed.",
                        ident,
                        premises_str,
                        prop,
                        rule,
                        proof_term.to_coq()
                    )
                }
            }
        }
    }

    /// Export proof with full elaboration
    pub fn export_proof(&mut self, proof: &Proof) -> String {
        let mut output = String::new();

        // Header
        output.push_str("(* Enhanced Coq proof with complete proof terms *)\n");
        output.push_str("(* Generated by oxiz-proof enhanced exporter *)\n\n");

        // Required libraries
        output.push_str("Require Import Coq.Init.Prelude.\n");
        output.push_str("Require Import Coq.Logic.Classical.\n");
        output.push_str("Require Import Coq.micromega.Lia.\n");
        output.push_str("Require Import Coq.micromega.Lra.\n\n");

        // Base definitions
        output.push_str("(* Proposition representation *)\n");
        output.push_str("Parameter Prop_of_string : string -> Prop.\n\n");

        // Resolution rule definition
        output.push_str("(* Resolution rule *)\n");
        output.push_str(
            "Axiom resolution_rule : forall (C1 C2 : Prop) (p : Prop),\n  (C1 \\/ p) -> (C2 \\/ ~p) -> (C1 \\/ C2).\n\n",
        );

        // Export nodes
        let nodes = proof.nodes();

        output.push_str("(* Proof steps with complete proof terms *)\n");
        for node in nodes {
            let node_def = self.export_node(proof, node.id, node);
            output.push_str(&node_def);
            output.push('\n');
        }

        // Final theorem
        if let Some(root_id) = proof.root()
            && let Some(root_ident) = self.node_to_ident.get(&root_id)
        {
            output.push_str("\n(* Main result *)\n");
            output.push_str("Theorem main_result : exists P, P.\n");
            output.push_str("Proof.\n");
            output.push_str(&format!("  exists {}.\n", root_ident));
            if let Some(proof_term) = self.node_to_proof_term.get(&root_id) {
                output.push_str(&format!("  {}.\n", proof_term.to_coq()));
            } else {
                output.push_str(&format!("  exact {}.\n", root_ident));
            }
            output.push_str("Qed.\n");
        }

        output
    }
}

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

/// Export a proof to Coq with complete proof terms
pub fn export_to_coq_enhanced(proof: &Proof) -> String {
    let mut exporter = EnhancedCoqExporter::new();
    exporter.export_proof(proof)
}

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

    #[test]
    fn test_coq_proof_term_var() {
        let term = CoqProofTerm::Var("H".to_string());
        assert_eq!(term.to_coq(), "H");
    }

    #[test]
    fn test_coq_proof_term_app() {
        let term = CoqProofTerm::App {
            function: Box::new(CoqProofTerm::Var("f".to_string())),
            args: vec![
                CoqProofTerm::Var("x".to_string()),
                CoqProofTerm::Var("y".to_string()),
            ],
        };
        assert_eq!(term.to_coq(), "(f x y)");
    }

    #[test]
    fn test_coq_proof_term_lambda() {
        let term = CoqProofTerm::Lambda {
            params: vec![("x".to_string(), CoqType::Prop)],
            body: Box::new(CoqProofTerm::Var("x".to_string())),
        };
        assert_eq!(term.to_coq(), "(fun (x : Prop) => x)");
    }

    #[test]
    fn test_coq_type_arrow() {
        let ty = CoqType::Arrow(Box::new(CoqType::Prop), Box::new(CoqType::Prop));
        assert_eq!(ty.to_coq(), "(Prop -> Prop)");
    }

    #[test]
    fn test_enhanced_exporter_creation() {
        let exporter = EnhancedCoqExporter::new();
        assert_eq!(exporter.node_to_ident.len(), 0);
    }

    #[test]
    fn test_export_with_proof_terms() {
        let mut proof = Proof::new();
        let axiom = proof.add_axiom("P");
        let _conclusion = proof.add_inference("refl", vec![axiom], "P = P");

        let coq_code = export_to_coq_enhanced(&proof);
        assert!(coq_code.contains("reflexivity"));
        assert!(coq_code.contains("complete proof terms"));
    }
}