1#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, Default)]
13#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
14pub struct TypeSystem {
15 pub name: String,
17
18 pub type_constructors: Vec<TypeConstructor>,
20
21 pub term_constructors: Vec<TermConstructor>,
23
24 pub typing_rules: Vec<TypingRule>,
26
27 pub reduction_rules: Vec<ReductionRule>,
29
30 pub equivalence_rules: Vec<EquivalenceRule>,
32
33 pub structural: StructuralRules,
35}
36
37#[derive(Clone, Debug)]
39#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
40pub struct TypeConstructor {
41 pub name: String,
43
44 pub symbol: String,
46
47 pub arity: usize,
49
50 pub kind: TypeConstructorKind,
52}
53
54#[derive(Clone, Debug, PartialEq, Eq)]
56#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
57pub enum TypeConstructorKind {
58 Base,
60 Unit,
62 Empty,
64 Product,
66 Coproduct,
68 Exponential,
70 Tensor,
72 LinearHom,
74}
75
76#[derive(Clone, Debug)]
78#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
79pub struct TermConstructor {
80 pub name: String,
82
83 pub symbol: String,
85
86 pub kind: TermConstructorKind,
88}
89
90#[derive(Clone, Debug, PartialEq, Eq)]
92#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
93pub enum TermConstructorKind {
94 Variable,
96 UnitIntro,
98 PairIntro,
100 PairElimFst,
102 PairElimSnd,
104 Abstraction,
106 Application,
108 InjLeft,
110 InjRight,
112 Case,
114 Absurd,
116 LetPair,
118}
119
120#[derive(Clone, Debug)]
122#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
123pub struct TypingRule {
124 pub name: String,
126
127 pub premises: Vec<String>,
129
130 pub conclusion: String,
132
133 pub side_conditions: Vec<String>,
135}
136
137#[derive(Clone, Debug)]
139#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
140pub struct ReductionRule {
141 pub name: String,
143
144 pub lhs: String,
146
147 pub rhs: String,
149}
150
151#[derive(Clone, Debug)]
153#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
154pub struct EquivalenceRule {
155 pub name: String,
157
158 pub lhs: String,
160
161 pub rhs: String,
163
164 pub condition: Option<String>,
166}
167
168#[derive(Clone, Debug, Default)]
170#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
171pub struct StructuralRules {
172 pub weakening: bool,
174
175 pub contraction: bool,
177
178 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 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 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 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 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 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}