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
//! Enhanced Lean 4 Proof Export with Complete Proof Terms.
//!
//! This module provides full proof term generation for Lean 4, including
//! tactics, term mode proofs, and theory-specific reasoning.

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

/// Enhanced Lean 4 proof exporter.
#[derive(Debug)]
pub struct EnhancedLeanExporter {
    /// Mapping from proof nodes to Lean identifiers
    node_to_ident: FxHashMap<ProofNodeId, String>,
    /// Mapping from proof nodes to their proof terms
    node_to_proof_term: FxHashMap<ProofNodeId, LeanProofTerm>,
    /// Counter for generating unique names
    name_counter: usize,
    /// Use term mode (vs tactic mode)
    #[allow(dead_code)]
    prefer_term_mode: bool,
}

/// A Lean 4 proof term.
#[derive(Debug, Clone)]
pub enum LeanProofTerm {
    /// Variable reference
    Var(String),
    /// Function application
    App {
        function: Box<LeanProofTerm>,
        args: Vec<LeanProofTerm>,
    },
    /// Lambda abstraction
    Lambda {
        params: Vec<(String, LeanType)>,
        body: Box<LeanProofTerm>,
    },
    /// Tactic proof
    Tactic(LeanTactic),
    /// Have statement
    Have {
        name: String,
        ty: Option<LeanType>,
        proof: Box<LeanProofTerm>,
        body: Box<LeanProofTerm>,
    },
}

/// Lean 4 type representation.
#[derive(Debug, Clone)]
pub enum LeanType {
    /// Prop
    Prop,
    /// Type universe
    Type,
    /// Sort universe
    Sort,
    /// Function type (A → B)
    Arrow(Box<LeanType>, Box<LeanType>),
    /// Named type
    Named(String),
    /// Type application
    App(Box<LeanType>, Vec<LeanType>),
}

/// Lean 4 tactic.
#[derive(Debug, Clone)]
pub enum LeanTactic {
    /// apply tactic
    Apply(String),
    /// exact tactic
    Exact(Box<LeanProofTerm>),
    /// intro tactic
    Intro(Vec<String>),
    /// cases tactic
    Cases(String),
    /// split tactic
    Split,
    /// left/right tactic
    Constructor(usize),
    /// rfl (reflexivity)
    Rfl,
    /// simp
    Simp { lemmas: Vec<String> },
    /// omega (arithmetic)
    Omega,
    /// decide (decidable propositions)
    Decide,
    /// sequence of tactics
    Seq(Vec<LeanTactic>),
}

impl LeanProofTerm {
    /// Convert proof term to Lean 4 syntax
    pub fn to_lean(&self) -> String {
        match self {
            LeanProofTerm::Var(v) => v.clone(),
            LeanProofTerm::App { function, args } => {
                let func_str = function.to_lean();
                if args.is_empty() {
                    func_str
                } else {
                    let args_str = args
                        .iter()
                        .map(|a| a.to_lean())
                        .collect::<Vec<_>>()
                        .join(" ");
                    format!("({} {})", func_str, args_str)
                }
            }
            LeanProofTerm::Lambda { params, body } => {
                let params_str = params
                    .iter()
                    .map(|(name, ty)| format!("({} : {})", name, ty.to_lean()))
                    .collect::<Vec<_>>()
                    .join(" ");
                format!("fun {} => {}", params_str, body.to_lean())
            }
            LeanProofTerm::Tactic(tactic) => format!("by\n  {}", tactic.to_lean()),
            LeanProofTerm::Have {
                name,
                ty,
                proof,
                body,
            } => {
                let ty_str = ty
                    .as_ref()
                    .map(|t| format!(" : {}", t.to_lean()))
                    .unwrap_or_default();
                format!(
                    "have {}{} := {}\n{}",
                    name,
                    ty_str,
                    proof.to_lean(),
                    body.to_lean()
                )
            }
        }
    }
}

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

impl LeanTactic {
    /// Convert tactic to Lean 4 syntax
    pub fn to_lean(&self) -> String {
        match self {
            LeanTactic::Apply(name) => format!("apply {}", name),
            LeanTactic::Exact(term) => format!("exact {}", term.to_lean()),
            LeanTactic::Intro(names) => {
                if names.is_empty() {
                    "intro".to_string()
                } else {
                    format!("intro {}", names.join(" "))
                }
            }
            LeanTactic::Cases(name) => format!("cases {}", name),
            LeanTactic::Split => "split".to_string(),
            LeanTactic::Constructor(n) => {
                if *n == 0 {
                    "constructor".to_string()
                } else {
                    format!("apply Constructor.mk{}", n)
                }
            }
            LeanTactic::Rfl => "rfl".to_string(),
            LeanTactic::Simp { lemmas } => {
                if lemmas.is_empty() {
                    "simp".to_string()
                } else {
                    format!("simp [{}]", lemmas.join(", "))
                }
            }
            LeanTactic::Omega => "omega".to_string(),
            LeanTactic::Decide => "decide".to_string(),
            LeanTactic::Seq(tactics) => tactics
                .iter()
                .map(|t| t.to_lean())
                .collect::<Vec<_>>()
                .join("\n  "),
        }
    }
}

impl EnhancedLeanExporter {
    /// Create a new enhanced Lean exporter
    pub fn new() -> Self {
        Self {
            node_to_ident: FxHashMap::default(),
            node_to_proof_term: FxHashMap::default(),
            name_counter: 0,
            prefer_term_mode: false,
        }
    }

    /// Create exporter that prefers term mode
    pub fn with_term_mode() -> Self {
        Self {
            node_to_ident: FxHashMap::default(),
            node_to_proof_term: FxHashMap::default(),
            name_counter: 0,
            prefer_term_mode: true,
        }
    }

    /// 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 an inference rule
    fn build_inference_proof_term(
        &mut self,
        rule: &str,
        premises: &[ProofNodeId],
    ) -> LeanProofTerm {
        let tactic = match rule {
            "resolution" => {
                // Use custom resolution tactic
                LeanTactic::Apply("resolution_rule".to_string())
            }
            "modus_ponens" | "mp" => {
                // Modus ponens: apply the implication to the premise
                if premises.len() >= 2 {
                    if let Some(impl_ident) =
                        premises.get(1).and_then(|id| self.node_to_ident.get(id))
                    {
                        LeanTactic::Apply(impl_ident.clone())
                    } else {
                        LeanTactic::Apply("mp_rule".to_string())
                    }
                } else {
                    LeanTactic::Apply("mp_rule".to_string())
                }
            }
            "and_intro" => LeanTactic::Seq(vec![
                LeanTactic::Split,
                LeanTactic::Apply("And.intro".to_string()),
            ]),
            "and_elim_left" => LeanTactic::Apply("And.left".to_string()),
            "and_elim_right" => LeanTactic::Apply("And.right".to_string()),
            "or_intro_left" => LeanTactic::Apply("Or.inl".to_string()),
            "or_intro_right" => LeanTactic::Apply("Or.inr".to_string()),
            "refl" | "eq_refl" => LeanTactic::Rfl,
            "symm" | "eq_symm" => LeanTactic::Apply("Eq.symm".to_string()),
            "trans" | "eq_trans" => LeanTactic::Apply("Eq.trans".to_string()),
            "congruence" | "cong" => LeanTactic::Apply("congrArg".to_string()),
            "lia" | "linear_arithmetic" => LeanTactic::Omega,
            "lra" | "linear_real_arithmetic" => LeanTactic::Omega,
            "decide" => LeanTactic::Decide,
            _ => LeanTactic::Simp { lemmas: vec![] },
        };

        LeanProofTerm::Tactic(tactic)
    }

    /// Export a proof node to Lean
    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 {} : PropOf \"{}\"", ident, conclusion)
            }
            ProofStep::Inference {
                rule,
                premises,
                conclusion,
                ..
            } => {
                let proof_term = self.build_inference_proof_term(rule, 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!("PropOf \"{}\"", conclusion);

                if premise_idents.is_empty() {
                    format!("axiom {} : {}", ident, prop)
                } else {
                    let premises_str = premise_idents.join("");
                    format!(
                        "theorem {} : {}{} :=\n  -- Rule: {}\n  {}",
                        ident,
                        premises_str,
                        prop,
                        rule,
                        proof_term.to_lean()
                    )
                }
            }
        }
    }

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

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

        // Imports
        output.push_str("import Std.Logic\n");
        output.push_str("import Std.Data.Int.Basic\n");
        output.push_str("import Std.Data.Rat.Basic\n");
        output.push_str("import Std.Tactic.Omega\n\n");

        // Base definitions
        output.push_str("-- Proposition representation\n");
        output.push_str("def PropOf (s : String) : Prop := True\n\n");

        // Helper lemmas
        output.push_str("-- Resolution rule\n");
        output.push_str(
            "axiom resolution_rule : ∀ (C1 C2 p : Prop), (C1 ∨ p) → (C2 ∨ ¬p) → (C1 ∨ C2)\n\n",
        );
        output.push_str("-- Modus ponens\n");
        output.push_str("axiom mp_rule : ∀ (P Q : Prop), P → (P → Q) → Q\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_str("\n\n");
        }

        // Final theorem
        if let Some(root_id) = proof.root()
            && let Some(root_ident) = self.node_to_ident.get(&root_id)
        {
            output.push_str("-- Main result\n");
            output.push_str("theorem main_result : ∃ P, P := by\n");
            output.push_str(&format!("  use {}\n", root_ident));
            if let Some(proof_term) = self.node_to_proof_term.get(&root_id)
                && let LeanProofTerm::Tactic(tactic) = proof_term
            {
                output.push_str(&format!("  {}\n", tactic.to_lean()));
            }
        }

        output
    }

    /// Export theory proof
    pub fn export_theory_proof(&mut self, theory_proof: &TheoryProof) -> String {
        let mut output = String::new();

        output.push_str("-- Enhanced Lean 4 theory proof\n\n");
        output.push_str("import Std.Data.Int.Basic\n");
        output.push_str("import Std.Data.Rat.Basic\n");
        output.push_str("import Std.Tactic.Omega\n\n");

        output.push_str("-- Theory axioms and lemmas\n");
        for step in theory_proof.steps() {
            let step_name = self.fresh_ident("theory_step");
            output.push_str(&format!("-- Step {}: {:?}\n", step.id.0, step.rule));
            output.push_str(&format!("axiom {} : Prop\n\n", step_name));
        }

        output.push_str("-- Theory proof complete\n");
        output
    }
}

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

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

/// Export a proof to Lean 4 with term mode preference
pub fn export_to_lean_term_mode(proof: &Proof) -> String {
    let mut exporter = EnhancedLeanExporter::with_term_mode();
    exporter.export_proof(proof)
}

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

    #[test]
    fn test_lean_proof_term_var() {
        let term = LeanProofTerm::Var("H".to_string());
        assert_eq!(term.to_lean(), "H");
    }

    #[test]
    fn test_lean_proof_term_lambda() {
        let term = LeanProofTerm::Lambda {
            params: vec![("x".to_string(), LeanType::Prop)],
            body: Box::new(LeanProofTerm::Var("x".to_string())),
        };
        assert_eq!(term.to_lean(), "fun (x : Prop) => x");
    }

    #[test]
    fn test_lean_tactic_rfl() {
        let tactic = LeanTactic::Rfl;
        assert_eq!(tactic.to_lean(), "rfl");
    }

    #[test]
    fn test_lean_tactic_apply() {
        let tactic = LeanTactic::Apply("foo".to_string());
        assert_eq!(tactic.to_lean(), "apply foo");
    }

    #[test]
    fn test_enhanced_exporter_creation() {
        let exporter = EnhancedLeanExporter::new();
        assert!(!exporter.prefer_term_mode);
    }

    #[test]
    fn test_term_mode_exporter() {
        let exporter = EnhancedLeanExporter::with_term_mode();
        assert!(exporter.prefer_term_mode);
    }

    #[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 lean_code = export_to_lean_enhanced(&proof);
        assert!(lean_code.contains("rfl"));
        assert!(lean_code.contains("complete proof terms"));
    }
}