use crate::smt::{self, Pred, Verdict};
use core::marker::PhantomData;
pub struct Truth;
pub enum Falsity {}
pub struct And<P, Q>(PhantomData<(P, Q)>);
pub struct Imp<P, Q>(PhantomData<(P, Q)>);
pub struct Equals<A, B>(PhantomData<(A, B)>);
pub trait Theorem {
type Proof;
}
#[derive(Debug, Clone, Copy)]
pub struct TruthProof;
#[derive(Debug, Clone, Copy)]
pub struct AndProof<A, B> {
pub left: A,
pub right: B,
}
#[derive(Clone, Copy)]
pub struct ImpProof<A, B> {
pub run: fn(A) -> B,
}
#[derive(Debug, Clone, Copy)]
pub struct Refl;
impl Theorem for Truth {
type Proof = TruthProof;
}
impl<P: Theorem, Q: Theorem> Theorem for And<P, Q> {
type Proof = AndProof<P::Proof, Q::Proof>;
}
impl<P: Theorem, Q: Theorem> Theorem for Imp<P, Q> {
type Proof = ImpProof<P::Proof, Q::Proof>;
}
impl<A> Theorem for Equals<A, A> {
type Proof = Refl;
}
pub fn conj<P: Theorem, Q: Theorem>(p: P::Proof, q: Q::Proof) -> AndProof<P::Proof, Q::Proof> {
AndProof { left: p, right: q }
}
pub fn conj_left<P: Theorem, Q: Theorem>(p: AndProof<P::Proof, Q::Proof>) -> P::Proof {
p.left
}
pub fn conj_right<P: Theorem, Q: Theorem>(p: AndProof<P::Proof, Q::Proof>) -> Q::Proof {
p.right
}
pub fn imp_intro<P: Theorem, Q: Theorem>(f: fn(P::Proof) -> Q::Proof) -> ImpProof<P::Proof, Q::Proof> {
ImpProof { run: f }
}
pub fn modus_ponens<P: Theorem, Q: Theorem>(
imp: ImpProof<P::Proof, Q::Proof>,
p: P::Proof,
) -> Q::Proof {
(imp.run)(p)
}
pub fn refl<A>() -> Refl {
Refl
}
pub fn id_imp<P: Theorem>() -> ImpProof<P::Proof, P::Proof> {
ImpProof { run: |p| p }
}
pub fn truth_proof() -> TruthProof {
TruthProof
}
#[derive(Debug, Clone, Copy)]
pub struct Certificate {
statement: Pred,
generation: u32,
}
impl Certificate {
pub fn discharge(statement: Pred) -> Option<Certificate> {
match smt::prove(statement) {
Verdict::Valid => Some(Certificate { statement, generation: smt::generation() }),
_ => None,
}
}
pub fn statement(&self) -> Option<Pred> {
if self.is_current() {
Some(self.statement)
} else {
None
}
}
pub fn is_current(&self) -> bool {
self.generation == smt::generation()
}
pub fn and(self, other: Certificate) -> Option<Certificate> {
if !self.is_current() || !other.is_current() {
return None;
}
Some(Certificate {
statement: smt::and(self.statement, other.statement),
generation: self.generation,
})
}
pub fn weaken_to(self, weaker: Pred) -> Option<Certificate> {
if !self.is_current() {
return None;
}
match smt::prove_implies(self.statement, weaker) {
Verdict::Valid => {
Some(Certificate { statement: weaker, generation: self.generation })
}
_ => None,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Implication {
pre: Pred,
post: Pred,
generation: u32,
}
impl Implication {
pub fn discharge(pre: Pred, post: Pred) -> Option<Implication> {
match smt::prove_implies(pre, post) {
Verdict::Valid => {
Some(Implication { pre, post, generation: smt::generation() })
}
_ => None,
}
}
pub fn is_current(&self) -> bool {
self.generation == smt::generation()
}
pub fn antecedent(&self) -> Option<Pred> {
if self.is_current() {
Some(self.pre)
} else {
None
}
}
pub fn consequent(&self) -> Option<Pred> {
if self.is_current() {
Some(self.post)
} else {
None
}
}
pub fn apply(self, premise: Certificate) -> Option<Certificate> {
if !self.is_current() || !premise.is_current() {
return None;
}
match smt::prove_equiv(premise.statement, self.pre) {
Verdict::Valid => Some(Certificate {
statement: self.post,
generation: self.generation,
}),
_ => None,
}
}
pub fn then(self, next: Implication) -> Option<Implication> {
if !self.is_current() || !next.is_current() {
return None;
}
match smt::prove_equiv(self.post, next.pre) {
Verdict::Valid => Some(Implication {
pre: self.pre,
post: next.post,
generation: self.generation,
}),
_ => None,
}
}
}