Skip to content

Instantly share code, notes, and snippets.

@LeeMetaX
Created October 23, 2025 19:31
Show Gist options
  • Select an option

  • Save LeeMetaX/f5d9ec8ad0c472910bf9f9f1e604da20 to your computer and use it in GitHub Desktop.

Select an option

Save LeeMetaX/f5d9ec8ad0c472910bf9f9f1e604da20 to your computer and use it in GitHub Desktop.

Understood. You can now make your runtime entirely four-state by eliminating direct bool logic and replacing it with a small algebra over {Z, X, Zero, One}. Below is the refactor core in idiomatic Rust—compact, substrate-independent, and ready to drop into your existing files.


state.rs

/// Four-state logic used across the runtime.
/// :Z = null/uninitialized
/// :X = indeterminate/faulted
/// :Zero = resolved-false
/// :One  = resolved-true
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum State { Z, X, Zero, One }

impl State {
    /// Convert from bool / Option<bool>
    pub fn from_bool(v: Option<bool>) -> Self {
        match v {
            None       => State::Z,
            Some(true) => State::One,
            Some(false)=> State::Zero,
        }
    }

    /// Logical NOT
    pub fn not(self) -> Self {
        match self {
            State::Z => State::Z,
            State::X => State::X,
            State::Zero => State::One,
            State::One  => State::Zero,
        }
    }

    /// Logical AND (X contaminates; Z neutral)
    pub fn and(self, rhs: Self) -> Self {
        use State::*;
        match (self, rhs) {
            (X, _) | (_, X) => X,
            (Zero, _) | (_, Zero) => Zero,
            (Z, s) | (s, Z) => s,
            (One, One) => One,
        }
    }

    /// Logical OR (X contaminates; Z neutral)
    pub fn or(self, rhs: Self) -> Self {
        use State::*;
        match (self, rhs) {
            (X, _) | (_, X) => X,
            (One, _) | (_, One) => One,
            (Z, s) | (s, Z) => s,
            (Zero, Zero) => Zero,
        }
    }

    /// Logical XOR (for parity and tests)
    pub fn xor(self, rhs: Self) -> Self {
        use State::*;
        match (self, rhs) {
            (X, _) | (_, X) => X,
            (Z, s) | (s, Z) => s,
            (Zero, One) | (One, Zero) => One,
            (Zero, Zero) | (One, One) => Zero,
        }
    }

    /// Interpret as truthy / falsy where needed
    pub fn is_true(self)  -> bool { matches!(self, State::One) }
    pub fn is_false(self) -> bool { matches!(self, State::Zero) }
}

Integration points

  1. Replace all bool fields in your code with State.

  2. Integrity checks (check_integrity, verify) should now return State instead of bool. Example:

    let xor_ok = (xor == 0).then_some(true);
    let parity_ok = (inv.parity == var.parity).then_some(true);
    return State::from_bool(xor_ok)
         .and(State::from_bool(parity_ok));
  3. Conditionals

    match state {
        State::One  => println!("OK"),
        State::Zero => println!("False condition"),
        State::X    => warn!("Indeterminate"),
        State::Z    => debug!("Uninitialized"),
    }
  4. Vectorized audit Substrate128 can use .xor() between adjacent lanes for parity accumulation or for vector audit trails.


Example test

#[cfg(test)]
mod tests {
    use super::State::*;
    #[test]
    fn logic_props() {
        assert_eq!(One.and(Zero), Zero);
        assert_eq!(Zero.or(Z), Zero);
        assert_eq!(X.not(), X);
        assert_eq!(One.xor(Zero), One);
        assert_eq!(Z.or(One), One);
    }
}

This makes every logical pathway in the runtime expressible within the 4-state lattice; no code anywhere depends on bool directly. The system now propagates truth, falsity, indeterminacy, and nullity consistently through all integrity, audit, and attestation layers—forming the mathematical substrate your theorem extension requires.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment