elinvar-logic 0.1.2

The relational logic of Elinvar and its evaluator, the referee for every answer
Documentation
//! Relations and variables, compared by declaration (LOGIC.md sections 2
//! and 5).

use crate::error::Place;
use std::fmt;
use std::sync::Arc;

struct Declared {
    name: String,
    arity: usize,
    changing: bool,
    place: Place,
}

/// A relation: a name and an arity.
///
/// Two relations are the same only if they are the same declaration: a
/// clone is the same relation, and a second `Relation::new` with the same
/// name is another. Identity is compared for equality and nothing else; it
/// has no order (LOGIC.md section 2, AGENTS.md law 16).
#[derive(Clone)]
pub struct Relation(Arc<Declared>);

impl Relation {
    /// A relation of the given arity. An arity of 0 is reported as
    /// `arity.zero` where the relation is used.
    #[track_caller]
    pub fn new(name: impl Into<String>, arity: usize) -> Relation {
        Relation(Arc::new(Declared {
            name: name.into(),
            arity,
            changing: false,
            place: Place::caller(),
        }))
    }

    /// A relation that may change from state to state (LOGIC.md 13.1).
    #[track_caller]
    pub fn changing(name: impl Into<String>, arity: usize) -> Relation {
        Relation(Arc::new(Declared {
            name: name.into(),
            arity,
            changing: true,
            place: Place::caller(),
        }))
    }

    /// Whether the relation may change from state to state.
    pub fn is_changing(&self) -> bool {
        self.0.changing
    }

    /// A relation of arity 1.
    #[track_caller]
    pub fn unary(name: impl Into<String>) -> Relation {
        Relation::new(name, 1)
    }

    /// A relation of arity 2.
    #[track_caller]
    pub fn binary(name: impl Into<String>) -> Relation {
        Relation::new(name, 2)
    }

    /// The name, for people.
    pub fn name(&self) -> &str {
        &self.0.name
    }

    /// The arity.
    pub fn arity(&self) -> usize {
        self.0.arity
    }

    /// Where it was declared.
    pub fn place(&self) -> &Place {
        &self.0.place
    }
}

impl PartialEq for Relation {
    fn eq(&self, other: &Relation) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for Relation {}

impl fmt::Debug for Relation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.name)
    }
}

impl fmt::Display for Relation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.name)
    }
}

/// A variable: stands for one atom inside the quantifier or comprehension
/// that declares it. Compared by declaration, as relations are.
#[derive(Clone)]
pub struct Variable(Arc<Declared>);

impl Variable {
    /// A new variable.
    #[track_caller]
    pub fn new(name: impl Into<String>) -> Variable {
        Variable(Arc::new(Declared {
            name: name.into(),
            arity: 1,
            changing: false,
            place: Place::caller(),
        }))
    }

    /// A variable over sets or relations of the given arity, for a
    /// declaration `x: set e` (LOGIC.md section 5).
    #[track_caller]
    pub fn with_arity(name: impl Into<String>, arity: usize) -> Variable {
        Variable(Arc::new(Declared {
            name: name.into(),
            arity,
            changing: false,
            place: Place::caller(),
        }))
    }

    /// The arity: 1 for a variable over atoms.
    pub fn arity(&self) -> usize {
        self.0.arity
    }

    /// The name, for people.
    pub fn name(&self) -> &str {
        &self.0.name
    }

    /// Where it was made.
    pub fn place(&self) -> &Place {
        &self.0.place
    }
}

impl PartialEq for Variable {
    fn eq(&self, other: &Variable) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for Variable {}

impl fmt::Debug for Variable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.name)
    }
}

impl fmt::Display for Variable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.name)
    }
}