lolli_codegen/
lib.rs

1//! # lolli-codegen
2//!
3//! Code generation for the Lolli linear logic workbench.
4//!
5//! This crate generates Rust code from linear logic proofs,
6//! translating formulas to types and terms to code.
7
8#![warn(missing_docs)]
9#![warn(clippy::all)]
10
11pub use lolli_core::{Formula, Proof, Term};
12
13/// Rust code generator (placeholder).
14pub struct RustCodegen {
15    indent: usize,
16}
17
18impl Default for RustCodegen {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl RustCodegen {
25    /// Create a new code generator.
26    pub fn new() -> Self {
27        Self { indent: 0 }
28    }
29
30    /// Generate a Rust type from a linear logic formula.
31    pub fn formula_to_type(&self, formula: &Formula) -> String {
32        match formula {
33            Formula::Atom(a) => a.clone(),
34            Formula::NegAtom(a) => format!("{}Dual", a),
35            Formula::Tensor(a, b) => {
36                format!("({}, {})", self.formula_to_type(a), self.formula_to_type(b))
37            }
38            Formula::Par(a, b) => {
39                format!("Par<{}, {}>", self.formula_to_type(a), self.formula_to_type(b))
40            }
41            Formula::Lolli(a, b) => {
42                format!(
43                    "impl FnOnce({}) -> {}",
44                    self.formula_to_type(a),
45                    self.formula_to_type(b)
46                )
47            }
48            Formula::With(a, b) => {
49                format!("With<{}, {}>", self.formula_to_type(a), self.formula_to_type(b))
50            }
51            Formula::Plus(a, b) => {
52                format!(
53                    "Either<{}, {}>",
54                    self.formula_to_type(a),
55                    self.formula_to_type(b)
56                )
57            }
58            Formula::OfCourse(a) => format!("Rc<{}>", self.formula_to_type(a)),
59            Formula::WhyNot(a) => format!("Demand<{}>", self.formula_to_type(a)),
60            Formula::One => "()".to_string(),
61            Formula::Bottom => "!".to_string(),
62            Formula::Top => "Top".to_string(),
63            Formula::Zero => "Void".to_string(),
64        }
65    }
66
67    /// Generate Rust code from an extracted term.
68    pub fn term_to_code(&mut self, term: &Term) -> String {
69        match term {
70            Term::Var(v) => v.clone(),
71            Term::Unit => "()".to_string(),
72            Term::Pair(a, b) => format!("({}, {})", self.term_to_code(a), self.term_to_code(b)),
73            Term::Abs(x, body) => format!("|{}| {}", x, self.term_to_code(body)),
74            Term::App(f, a) => format!("{}({})", self.term_to_code(f), self.term_to_code(a)),
75            Term::Inl(a) => format!("Either::Left({})", self.term_to_code(a)),
76            Term::Inr(b) => format!("Either::Right({})", self.term_to_code(b)),
77            Term::Fst(p) => format!("{}.0", self.term_to_code(p)),
78            Term::Snd(p) => format!("{}.1", self.term_to_code(p)),
79            Term::Promote(a) => format!("Rc::new({})", self.term_to_code(a)),
80            _ => "/* TODO */".to_string(),
81        }
82    }
83}