use std::collections::HashMap;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ProofCertId(pub u64);
impl ProofCertId {
pub fn new(id: u64) -> Self {
ProofCertId(id)
}
pub fn raw(self) -> u64 {
self.0
}
}
impl std::fmt::Display for ProofCertId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "cert#{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ProofStep {
Beta {
redex_depth: u32,
},
Delta {
name: String,
},
Zeta,
Iota {
recursor: String,
ctor_idx: u32,
},
Eta,
SubstLevel {
params: Vec<String>,
},
Assumption,
}
impl std::fmt::Display for ProofStep {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProofStep::Beta { redex_depth } => write!(f, "Beta(depth={})", redex_depth),
ProofStep::Delta { name } => write!(f, "Delta({})", name),
ProofStep::Zeta => write!(f, "Zeta"),
ProofStep::Iota { recursor, ctor_idx } => {
write!(f, "Iota({}, ctor={})", recursor, ctor_idx)
}
ProofStep::Eta => write!(f, "Eta"),
ProofStep::SubstLevel { params } => write!(f, "SubstLevel({:?})", params),
ProofStep::Assumption => write!(f, "Assumption"),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProofCertificate {
pub id: ProofCertId,
pub decl_name: String,
pub type_hash: u64,
pub proof_hash: u64,
pub reduction_steps: Vec<ProofStep>,
pub verified_at: u64,
}
impl ProofCertificate {
pub fn step_count(&self) -> usize {
self.reduction_steps.len()
}
pub fn is_trivial(&self) -> bool {
self.reduction_steps.is_empty()
}
}
impl std::fmt::Display for ProofCertificate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"ProofCert {{ id: {}, decl: {}, type_hash: {:016x}, proof_hash: {:016x}, steps: {} }}",
self.id,
self.decl_name,
self.type_hash,
self.proof_hash,
self.reduction_steps.len()
)
}
}
#[derive(Clone, Debug, Default)]
pub struct CertificateStore {
pub certs: HashMap<String, ProofCertificate>,
}
impl CertificateStore {
pub fn new() -> Self {
CertificateStore {
certs: HashMap::new(),
}
}
pub fn len(&self) -> usize {
self.certs.len()
}
pub fn is_empty(&self) -> bool {
self.certs.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&String, &ProofCertificate)> {
self.certs.iter()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CertCheckResult {
Valid,
HashMismatch {
expected: u64,
actual: u64,
},
MissingDecl(String),
InvalidSteps,
}
impl std::fmt::Display for CertCheckResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CertCheckResult::Valid => write!(f, "Valid"),
CertCheckResult::HashMismatch { expected, actual } => {
write!(
f,
"HashMismatch {{ expected: {:016x}, actual: {:016x} }}",
expected, actual
)
}
CertCheckResult::MissingDecl(name) => write!(f, "MissingDecl({})", name),
CertCheckResult::InvalidSteps => write!(f, "InvalidSteps"),
}
}
}