File size: 2,514 Bytes
99d3d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) 2026 ERRANT DIGITAL INSTITUTE OF TECHNOLOGY (SEIT NGO) % SNAPKITTY COLLECTIVE
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// See LICENSE-AGPL and LICENSE-COMMERCIAL in this repository for license terms.

//! Everything the kernel's judgments can fail with. Every variant is a
//! reported outcome, never a panic: per this project's hard
//! requirements, the kernel fails closed on anything it cannot
//! positively establish, rather than assuming the best.
use std::fmt;
use vf_core::{ArenaError, Symbol, TermId};
use vf_reducer::ReduceError;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KernelError {
    Arena(ArenaError),
    Reduce(ReduceError),
    /// A [`vf_core::Term::BoundVar`] escaped every binder that should
    /// have opened it. A term in this state is not well-scoped and is
    /// rejected outright -- it is never a valid axiom/definition/proof
    /// body regardless of what it claims to establish.
    UnexpectedLooseBoundVar,
    UnboundVariable(Symbol),
    UnknownConstant(Symbol),
    NotAFunction {
        head_type: TermId,
    },
    NotASort {
        got: TermId,
    },
    TypeMismatch {
        expected: TermId,
        found: TermId,
    },
}

impl From<ArenaError> for KernelError {
    fn from(e: ArenaError) -> Self {
        KernelError::Arena(e)
    }
}
impl From<ReduceError> for KernelError {
    fn from(e: ReduceError) -> Self {
        KernelError::Reduce(e)
    }
}

impl fmt::Display for KernelError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            KernelError::Arena(e) => write!(f, "{e}"),
            KernelError::Reduce(e) => write!(f, "{e}"),
            KernelError::UnexpectedLooseBoundVar => {
                write!(
                    f,
                    "encountered a bound variable outside any binder (not well-scoped)"
                )
            }
            KernelError::UnboundVariable(_) => write!(f, "unbound local variable"),
            KernelError::UnknownConstant(_) => write!(f, "reference to an undeclared constant"),
            KernelError::NotAFunction { .. } => {
                write!(f, "applied a term whose type is not a Pi (function) type")
            }
            KernelError::NotASort { .. } => {
                write!(f, "expected a type (a term of sort Prop or Type(n))")
            }
            KernelError::TypeMismatch { .. } => write!(f, "type mismatch"),
        }
    }
}
impl std::error::Error for KernelError {}