use crate::{
env::{Binder, Hints, QuotKind},
error::Result,
};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Record<'a> {
Meta(#[serde(borrow)] Cow<'a, str>),
NameStr {
pre: u32,
#[serde(borrow)]
str: Cow<'a, str>,
},
NameNum {
pre: u32,
i: u64,
},
Succ(u32),
Max(u32, u32),
IMax(u32, u32),
Param(u32),
BVar(u64),
Sort(u32),
Const {
name: u32,
us: Vec<u32>,
},
App {
fun: u32,
arg: u32,
},
Lam(Binding),
Pi(Binding),
Let {
name: u32,
ty: u32,
value: u32,
body: u32,
nondep: bool,
},
Nat(#[serde(borrow)] Cow<'a, str>),
Str(#[serde(borrow)] Cow<'a, str>),
Proj {
type_name: u32,
idx: u64,
struct_: u32,
},
MData(u32),
Decl(Decl),
End(Counts),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Binding {
pub info: Binder,
pub name: u32,
pub ty: u32,
pub body: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Counts {
#[serde(with = "postcard::fixint::le")]
pub names: u32,
#[serde(with = "postcard::fixint::le")]
pub levels: u32,
#[serde(with = "postcard::fixint::le")]
pub exprs: u32,
}
impl Default for Counts {
fn default() -> Self {
Self {
names: 1,
levels: 1,
exprs: 0,
}
}
}
impl Record<'_> {
#[must_use]
pub const fn table(&self) -> Option<Table> {
match self {
Self::NameStr { .. } | Self::NameNum { .. } => Some(Table::Names),
Self::Succ(_) | Self::Max(..) | Self::IMax(..) | Self::Param(_) => Some(Table::Levels),
Self::Meta(_) | Self::Decl(_) | Self::End(_) => None,
_ => Some(Table::Exprs),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Table {
Names,
Levels,
Exprs,
}
impl Counts {
pub fn assign(&mut self, r: &Record<'_>) -> Option<u32> {
let next = match r.table()? {
Table::Names => &mut self.names,
Table::Levels => &mut self.levels,
Table::Exprs => &mut self.exprs,
};
*next += 1;
Some(*next - 1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Safety {
Safe,
Unsafe,
Partial,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Head {
pub name: u32,
pub level_params: Vec<u32>,
pub ty: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Decl {
Axiom {
head: Head,
is_unsafe: bool,
all: Option<Vec<u32>>,
},
Def {
head: Head,
value: u32,
hints: Hints,
safety: Safety,
all: Vec<u32>,
},
Thm {
head: Head,
value: u32,
all: Vec<u32>,
},
Opaque {
head: Head,
value: u32,
is_unsafe: bool,
all: Option<Vec<u32>>,
},
Quot {
head: Head,
kind: QuotKind,
},
Inductive {
types: Vec<IndType>,
ctors: Vec<IndCtor>,
recs: Vec<IndRec>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndType {
pub head: Head,
pub is_unsafe: bool,
pub num_params: u64,
pub num_indices: u64,
pub all: Vec<u32>,
pub ctors: Vec<u32>,
pub num_nested: u64,
pub is_rec: bool,
pub is_reflexive: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndCtor {
pub head: Head,
pub is_unsafe: bool,
pub induct: u32,
pub cidx: u64,
pub num_params: u64,
pub num_fields: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndRec {
pub head: Head,
pub is_unsafe: bool,
pub all: Vec<u32>,
pub num_params: u64,
pub num_indices: u64,
pub num_motives: u64,
pub num_minors: u64,
pub rules: Vec<IndRule>,
pub k: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndRule {
pub ctor: u32,
pub nfields: u64,
pub rhs: u32,
}
pub trait Sink {
type Output;
fn record(&mut self, id: Option<u32>, r: &Record<'_>) -> Result<()>;
fn finish(self) -> Result<Self::Output>;
}