1use serde::{Deserialize, Serialize};
2use std::{
3 fmt,
4 hash::{Hash, Hasher},
5 marker::PhantomData,
6};
7
8#[derive(PartialEq, Eq, Hash)]
10pub struct Id<T>(pub(super) u32, PhantomData<fn() -> T>);
11
12impl<T> Id<T> {
13 pub(super) const fn new(i: u32) -> Self {
14 Self(i, PhantomData)
15 }
16
17 #[must_use]
19 pub const fn index(self) -> usize {
20 self.0 as usize
21 }
22}
23
24impl<T> Clone for Id<T> {
25 fn clone(&self) -> Self {
26 *self
27 }
28}
29
30impl<T> Copy for Id<T> {}
31
32impl<T> fmt::Debug for Id<T> {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "#{}", self.0)
35 }
36}
37
38pub type NameId = Id<Name>;
39
40pub type LevelId = Id<Level>;
41
42pub type ExprId = Id<Expr>;
43
44pub const ANON: NameId = Id::new(0);
45
46pub const ZERO: LevelId = Id::new(0);
47
48#[derive(Debug, Clone, PartialEq, Eq, Hash)]
49pub enum Name {
50 Anon,
51 Str(NameId, Box<str>),
52 Num(NameId, u64),
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
56pub enum Level {
57 Zero,
58 Succ(LevelId),
59 Max(LevelId, LevelId),
60 IMax(LevelId, LevelId),
61 Param(NameId),
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
65#[serde(rename_all = "camelCase")]
66pub enum Binder {
67 Default,
68 Implicit,
69 StrictImplicit,
70 InstImplicit,
71}
72
73#[derive(Debug, Clone)]
76pub enum Expr {
77 BVar(u64),
78 Sort(LevelId),
79 Const(NameId, Box<[LevelId]>),
80 App(ExprId, ExprId),
81 Lam(NameId, ExprId, ExprId, Binder),
82 Pi(NameId, ExprId, ExprId, Binder),
83 Let(NameId, ExprId, ExprId, ExprId),
84 Nat(Box<str>),
85 Str(Box<str>),
86 Proj(NameId, u64, ExprId),
87}
88
89impl PartialEq for Expr {
90 fn eq(&self, other: &Self) -> bool {
91 match (self, other) {
92 (Self::BVar(a), Self::BVar(b)) => a == b,
93 (Self::Sort(a), Self::Sort(b)) => a == b,
94 (Self::Const(n, us), Self::Const(m, vs)) => n == m && us == vs,
95 (Self::App(f, a), Self::App(g, b)) => f == g && a == b,
96 (Self::Lam(_, t, b, _), Self::Lam(_, u, c, _))
97 | (Self::Pi(_, t, b, _), Self::Pi(_, u, c, _)) => t == u && b == c,
98 (Self::Let(_, t, v, b), Self::Let(_, u, w, c)) => t == u && v == w && b == c,
99 (Self::Nat(a), Self::Nat(b)) | (Self::Str(a), Self::Str(b)) => a == b,
100 (Self::Proj(n, i, x), Self::Proj(m, j, y)) => n == m && i == j && x == y,
101 _ => false,
102 }
103 }
104}
105
106impl Eq for Expr {}
107
108impl Hash for Expr {
109 fn hash<H: Hasher>(&self, h: &mut H) {
110 let pack = |a: u32, b: u32| u64::from(a) << 32 | u64::from(b);
111 match self {
112 Self::BVar(i) => h.write_u64(*i),
113 Self::Sort(l) => h.write_u32(l.0),
114 Self::Const(n, us) => {
115 h.write_u32(n.0);
116 us.iter().for_each(|u| h.write_u32(u.0));
117 }
118 Self::App(f, a) => h.write_u64(pack(f.0, a.0)),
119 Self::Lam(_, t, b, _) | Self::Pi(_, t, b, _) => h.write_u64(pack(t.0, b.0)),
120 Self::Let(_, t, v, b) => {
121 h.write_u64(pack(t.0, v.0));
122 h.write_u32(b.0);
123 }
124 Self::Nat(v) | Self::Str(v) => h.write(v.as_bytes()),
125 Self::Proj(n, i, x) => {
126 h.write_u64(pack(n.0, x.0));
127 h.write_u64(*i);
128 }
129 }
130 h.write_u8(self.tag());
131 }
132}
133
134impl Expr {
135 const fn tag(&self) -> u8 {
136 match self {
137 Self::BVar(_) => 0,
138 Self::Sort(_) => 1,
139 Self::Const(..) => 2,
140 Self::App(..) => 3,
141 Self::Lam(..) => 4,
142 Self::Pi(..) => 5,
143 Self::Let(..) => 6,
144 Self::Nat(_) => 7,
145 Self::Str(_) => 8,
146 Self::Proj(..) => 9,
147 }
148 }
149}
150
151#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
152#[serde(rename_all = "lowercase")]
153pub enum Hints {
154 Opaque,
155 Abbrev,
156 Regular(u32),
157}
158
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
160#[serde(rename_all = "lowercase")]
161pub enum QuotKind {
162 Type,
163 Ctor,
164 Lift,
165 Ind,
166}
167
168#[derive(Debug)]
169pub struct Rule {
170 pub ctor: NameId,
171 pub nfields: u64,
172 pub rhs: ExprId,
173}
174
175#[derive(Debug)]
176pub struct Defn {
177 pub value: ExprId,
178 pub hints: Hints,
179 pub all: Vec<NameId>,
180}
181
182#[derive(Debug)]
184pub struct Body {
185 pub value: ExprId,
186 pub all: Vec<NameId>,
187}
188
189#[derive(Debug)]
191pub struct Theorem {
192 pub value: Option<ExprId>,
193 pub all: Vec<NameId>,
194}
195
196#[derive(Debug)]
197pub struct Inductive {
198 pub num_params: u64,
199 pub num_indices: u64,
200 pub all: Vec<NameId>,
201 pub ctors: Vec<NameId>,
202 pub num_nested: u64,
203 pub is_rec: bool,
204 pub is_reflexive: bool,
205}
206
207#[derive(Debug)]
208pub struct Ctor {
209 pub induct: NameId,
210 pub cidx: u64,
211 pub num_params: u64,
212 pub num_fields: u64,
213}
214
215#[derive(Debug)]
216pub struct Rec {
217 pub all: Vec<NameId>,
218 pub num_params: u64,
219 pub num_indices: u64,
220 pub num_motives: u64,
221 pub num_minors: u64,
222 pub rules: Vec<Rule>,
223 pub k: bool,
224}
225
226#[derive(Debug)]
227pub enum Kind {
228 Axiom,
229 Defn(Defn),
230 Thm(Theorem),
231 Opaque(Body),
232 Quot(QuotKind),
233 Induct(Inductive),
234 Ctor(Ctor),
235 Rec(Rec),
236}
237
238#[derive(Debug)]
240pub struct Const {
241 pub name: NameId,
242 pub level_params: Vec<NameId>,
243 pub ty: ExprId,
244 pub kind: Kind,
245}