csw_derive/
type_system.rs

1//! Type system representation.
2//!
3//! This module defines the data structures that represent a derived type system,
4//! including type constructors, term constructors, typing rules, and reduction rules.
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9/// A complete derived type system.
10///
11/// This is the main output of the derivation process.
12#[derive(Clone, Debug, Default)]
13#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
14pub struct TypeSystem {
15    /// Name of the type system
16    pub name: String,
17
18    /// Type constructors (how to form types)
19    pub type_constructors: Vec<TypeConstructor>,
20
21    /// Term constructors (how to form terms)
22    pub term_constructors: Vec<TermConstructor>,
23
24    /// Typing rules
25    pub typing_rules: Vec<TypingRule>,
26
27    /// Reduction rules (β-reduction)
28    pub reduction_rules: Vec<ReductionRule>,
29
30    /// Equivalence rules (η-expansion)
31    pub equivalence_rules: Vec<EquivalenceRule>,
32
33    /// Structural rules available
34    pub structural: StructuralRules,
35}
36
37/// A type constructor in the derived type system.
38#[derive(Clone, Debug)]
39#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
40pub struct TypeConstructor {
41    /// Internal name
42    pub name: String,
43
44    /// Display symbol (e.g., "×", "→", "⊗")
45    pub symbol: String,
46
47    /// Number of type arguments
48    pub arity: usize,
49
50    /// Kind of type constructor
51    pub kind: TypeConstructorKind,
52}
53
54/// The kind of a type constructor.
55#[derive(Clone, Debug, PartialEq, Eq)]
56#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
57pub enum TypeConstructorKind {
58    /// Base/primitive type
59    Base,
60    /// Unit type (terminal object)
61    Unit,
62    /// Empty type (initial object)
63    Empty,
64    /// Product type (A × B)
65    Product,
66    /// Coproduct/sum type (A + B)
67    Coproduct,
68    /// Function type (A → B)
69    Exponential,
70    /// Tensor product (A ⊗ B)
71    Tensor,
72    /// Linear function (A ⊸ B)
73    LinearHom,
74}
75
76/// A term constructor in the derived type system.
77#[derive(Clone, Debug)]
78#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
79pub struct TermConstructor {
80    /// Internal name
81    pub name: String,
82
83    /// Display symbol or template
84    pub symbol: String,
85
86    /// Kind of term constructor
87    pub kind: TermConstructorKind,
88}
89
90/// The kind of a term constructor.
91#[derive(Clone, Debug, PartialEq, Eq)]
92#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
93pub enum TermConstructorKind {
94    /// Variable reference
95    Variable,
96    /// Unit value introduction
97    UnitIntro,
98    /// Pair introduction
99    PairIntro,
100    /// First projection
101    PairElimFst,
102    /// Second projection
103    PairElimSnd,
104    /// Lambda abstraction
105    Abstraction,
106    /// Function application
107    Application,
108    /// Left injection (sum types)
109    InjLeft,
110    /// Right injection (sum types)
111    InjRight,
112    /// Case analysis (sum types)
113    Case,
114    /// Absurd elimination (empty type)
115    Absurd,
116    /// Let binding for pairs
117    LetPair,
118}
119
120/// A typing rule in the type system.
121#[derive(Clone, Debug)]
122#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
123pub struct TypingRule {
124    /// Name of the rule (e.g., "abs-intro", "app-elim")
125    pub name: String,
126
127    /// Premises (judgments above the line)
128    pub premises: Vec<String>,
129
130    /// Conclusion (judgment below the line)
131    pub conclusion: String,
132
133    /// Side conditions
134    pub side_conditions: Vec<String>,
135}
136
137/// A reduction rule (β-reduction).
138#[derive(Clone, Debug)]
139#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
140pub struct ReductionRule {
141    /// Name of the rule (e.g., "beta", "fst-beta")
142    pub name: String,
143
144    /// Left-hand side (redex)
145    pub lhs: String,
146
147    /// Right-hand side (reduct)
148    pub rhs: String,
149}
150
151/// An equivalence rule (η-expansion).
152#[derive(Clone, Debug)]
153#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
154pub struct EquivalenceRule {
155    /// Name of the rule
156    pub name: String,
157
158    /// Left-hand side
159    pub lhs: String,
160
161    /// Right-hand side
162    pub rhs: String,
163
164    /// Type condition (when this rule applies)
165    pub condition: Option<String>,
166}
167
168/// Structural rules available in the type system.
169#[derive(Clone, Debug, Default)]
170#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
171pub struct StructuralRules {
172    /// Weakening: can ignore variables
173    pub weakening: bool,
174
175    /// Contraction: can use variables multiple times
176    pub contraction: bool,
177
178    /// Exchange: variable order doesn't matter
179    pub exchange: bool,
180}
181
182impl std::fmt::Display for TypeSystem {
183    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
184        writeln!(f, "# {} Type System", self.name)?;
185        writeln!(f)?;
186
187        // Types
188        writeln!(f, "## Types")?;
189        writeln!(f)?;
190        for tc in &self.type_constructors {
191            writeln!(f, "  {} ({})", tc.symbol, tc.name)?;
192        }
193        writeln!(f)?;
194
195        // Terms
196        writeln!(f, "## Terms")?;
197        writeln!(f)?;
198        for tc in &self.term_constructors {
199            writeln!(f, "  {} ({})", tc.symbol, tc.name)?;
200        }
201        writeln!(f)?;
202
203        // Typing Rules
204        writeln!(f, "## Typing Rules")?;
205        writeln!(f)?;
206        for rule in &self.typing_rules {
207            writeln!(f, "### {}", rule.name)?;
208            for premise in &rule.premises {
209                writeln!(f, "  {}", premise)?;
210            }
211            if !rule.premises.is_empty() {
212                writeln!(f, "  ────────────────────")?;
213            }
214            writeln!(f, "  {}", rule.conclusion)?;
215            writeln!(f)?;
216        }
217
218        // Reduction Rules
219        writeln!(f, "## Reduction Rules")?;
220        writeln!(f)?;
221        for rule in &self.reduction_rules {
222            writeln!(f, "  {} → {}  ({})", rule.lhs, rule.rhs, rule.name)?;
223        }
224        writeln!(f)?;
225
226        // Structural Rules
227        writeln!(f, "## Structural Rules")?;
228        writeln!(f)?;
229        writeln!(f, "  Weakening:   {}", if self.structural.weakening { "✓" } else { "✗" })?;
230        writeln!(f, "  Contraction: {}", if self.structural.contraction { "✓" } else { "✗" })?;
231        writeln!(f, "  Exchange:    {}", if self.structural.exchange { "✓" } else { "✗" })?;
232
233        Ok(())
234    }
235}