1use crate::{
8 env::{Binder, Hints, QuotKind},
9 error::Result,
10};
11use serde::{Deserialize, Serialize};
12use std::borrow::Cow;
13
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
17pub enum Record<'a> {
18 Meta(#[serde(borrow)] Cow<'a, str>),
20 NameStr {
21 pre: u32,
22 #[serde(borrow)]
23 str: Cow<'a, str>,
24 },
25 NameNum {
26 pre: u32,
27 i: u64,
28 },
29 Succ(u32),
30 Max(u32, u32),
31 IMax(u32, u32),
32 Param(u32),
33 BVar(u64),
34 Sort(u32),
35 Const {
36 name: u32,
37 us: Vec<u32>,
38 },
39 App {
40 fun: u32,
41 arg: u32,
42 },
43 Lam(Binding),
44 Pi(Binding),
45 Let {
46 name: u32,
47 ty: u32,
48 value: u32,
49 body: u32,
50 nondep: bool,
51 },
52 Nat(#[serde(borrow)] Cow<'a, str>),
54 Str(#[serde(borrow)] Cow<'a, str>),
55 Proj {
56 type_name: u32,
57 idx: u64,
58 struct_: u32,
59 },
60 MData(u32),
62 Decl(Decl),
63 End(Counts),
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
68pub struct Binding {
69 pub info: Binder,
70 pub name: u32,
71 pub ty: u32,
72 pub body: u32,
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78pub struct Counts {
79 #[serde(with = "postcard::fixint::le")]
80 pub names: u32,
81 #[serde(with = "postcard::fixint::le")]
82 pub levels: u32,
83 #[serde(with = "postcard::fixint::le")]
84 pub exprs: u32,
85}
86
87impl Default for Counts {
88 fn default() -> Self {
89 Self {
90 names: 1,
91 levels: 1,
92 exprs: 0,
93 }
94 }
95}
96
97impl Record<'_> {
98 #[must_use]
100 pub const fn table(&self) -> Option<Table> {
101 match self {
102 Self::NameStr { .. } | Self::NameNum { .. } => Some(Table::Names),
103 Self::Succ(_) | Self::Max(..) | Self::IMax(..) | Self::Param(_) => Some(Table::Levels),
104 Self::Meta(_) | Self::Decl(_) | Self::End(_) => None,
105 _ => Some(Table::Exprs),
106 }
107 }
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111pub enum Table {
112 Names,
113 Levels,
114 Exprs,
115}
116
117impl Counts {
118 pub fn assign(&mut self, r: &Record<'_>) -> Option<u32> {
120 let next = match r.table()? {
121 Table::Names => &mut self.names,
122 Table::Levels => &mut self.levels,
123 Table::Exprs => &mut self.exprs,
124 };
125 *next += 1;
126 Some(*next - 1)
127 }
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
131pub enum Safety {
132 Safe,
133 Unsafe,
134 Partial,
135}
136
137#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
139pub struct Head {
140 pub name: u32,
141 pub level_params: Vec<u32>,
142 pub ty: u32,
143}
144
145#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
146pub enum Decl {
147 Axiom {
149 head: Head,
150 is_unsafe: bool,
151 all: Option<Vec<u32>>,
152 },
153 Def {
154 head: Head,
155 value: u32,
156 hints: Hints,
157 safety: Safety,
158 all: Vec<u32>,
159 },
160 Thm {
161 head: Head,
162 value: u32,
163 all: Vec<u32>,
164 },
165 Opaque {
166 head: Head,
167 value: u32,
168 is_unsafe: bool,
169 all: Option<Vec<u32>>,
170 },
171 Quot {
172 head: Head,
173 kind: QuotKind,
174 },
175 Inductive {
176 types: Vec<IndType>,
177 ctors: Vec<IndCtor>,
178 recs: Vec<IndRec>,
179 },
180}
181
182#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
183pub struct IndType {
184 pub head: Head,
185 pub is_unsafe: bool,
186 pub num_params: u64,
187 pub num_indices: u64,
188 pub all: Vec<u32>,
189 pub ctors: Vec<u32>,
190 pub num_nested: u64,
191 pub is_rec: bool,
192 pub is_reflexive: bool,
193}
194
195#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
196pub struct IndCtor {
197 pub head: Head,
198 pub is_unsafe: bool,
199 pub induct: u32,
200 pub cidx: u64,
201 pub num_params: u64,
202 pub num_fields: u64,
203}
204
205#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
206pub struct IndRec {
207 pub head: Head,
208 pub is_unsafe: bool,
209 pub all: Vec<u32>,
210 pub num_params: u64,
211 pub num_indices: u64,
212 pub num_motives: u64,
213 pub num_minors: u64,
214 pub rules: Vec<IndRule>,
215 pub k: bool,
216}
217
218#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
219pub struct IndRule {
220 pub ctor: u32,
221 pub nfields: u64,
222 pub rhs: u32,
223}
224
225pub trait Sink {
228 type Output;
229 fn record(&mut self, id: Option<u32>, r: &Record<'_>) -> Result<()>;
230 fn finish(self) -> Result<Self::Output>;
231}