use crate::error::Place;
use std::fmt;
use std::sync::Arc;
struct Declared {
name: String,
arity: usize,
changing: bool,
place: Place,
}
#[derive(Clone)]
pub struct Relation(Arc<Declared>);
impl Relation {
#[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(),
}))
}
#[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(),
}))
}
pub fn is_changing(&self) -> bool {
self.0.changing
}
#[track_caller]
pub fn unary(name: impl Into<String>) -> Relation {
Relation::new(name, 1)
}
#[track_caller]
pub fn binary(name: impl Into<String>) -> Relation {
Relation::new(name, 2)
}
pub fn name(&self) -> &str {
&self.0.name
}
pub fn arity(&self) -> usize {
self.0.arity
}
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)
}
}
#[derive(Clone)]
pub struct Variable(Arc<Declared>);
impl 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(),
}))
}
#[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(),
}))
}
pub fn arity(&self) -> usize {
self.0.arity
}
pub fn name(&self) -> &str {
&self.0.name
}
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)
}
}