Skip to main content

elinvar_logic/
relation.rs

1//! Relations and variables, compared by declaration (LOGIC.md sections 2
2//! and 5).
3
4use crate::error::Place;
5use std::fmt;
6use std::sync::Arc;
7
8struct Declared {
9    name: String,
10    arity: usize,
11    changing: bool,
12    place: Place,
13}
14
15/// A relation: a name and an arity.
16///
17/// Two relations are the same only if they are the same declaration: a
18/// clone is the same relation, and a second `Relation::new` with the same
19/// name is another. Identity is compared for equality and nothing else; it
20/// has no order (LOGIC.md section 2, AGENTS.md law 16).
21#[derive(Clone)]
22pub struct Relation(Arc<Declared>);
23
24impl Relation {
25    /// A relation of the given arity. An arity of 0 is reported as
26    /// `arity.zero` where the relation is used.
27    #[track_caller]
28    pub fn new(name: impl Into<String>, arity: usize) -> Relation {
29        Relation(Arc::new(Declared {
30            name: name.into(),
31            arity,
32            changing: false,
33            place: Place::caller(),
34        }))
35    }
36
37    /// A relation that may change from state to state (LOGIC.md 13.1).
38    #[track_caller]
39    pub fn changing(name: impl Into<String>, arity: usize) -> Relation {
40        Relation(Arc::new(Declared {
41            name: name.into(),
42            arity,
43            changing: true,
44            place: Place::caller(),
45        }))
46    }
47
48    /// Whether the relation may change from state to state.
49    pub fn is_changing(&self) -> bool {
50        self.0.changing
51    }
52
53    /// A relation of arity 1.
54    #[track_caller]
55    pub fn unary(name: impl Into<String>) -> Relation {
56        Relation::new(name, 1)
57    }
58
59    /// A relation of arity 2.
60    #[track_caller]
61    pub fn binary(name: impl Into<String>) -> Relation {
62        Relation::new(name, 2)
63    }
64
65    /// The name, for people.
66    pub fn name(&self) -> &str {
67        &self.0.name
68    }
69
70    /// The arity.
71    pub fn arity(&self) -> usize {
72        self.0.arity
73    }
74
75    /// Where it was declared.
76    pub fn place(&self) -> &Place {
77        &self.0.place
78    }
79}
80
81impl PartialEq for Relation {
82    fn eq(&self, other: &Relation) -> bool {
83        Arc::ptr_eq(&self.0, &other.0)
84    }
85}
86
87impl Eq for Relation {}
88
89impl fmt::Debug for Relation {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        write!(f, "{}", self.0.name)
92    }
93}
94
95impl fmt::Display for Relation {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        write!(f, "{}", self.0.name)
98    }
99}
100
101/// A variable: stands for one atom inside the quantifier or comprehension
102/// that declares it. Compared by declaration, as relations are.
103#[derive(Clone)]
104pub struct Variable(Arc<Declared>);
105
106impl Variable {
107    /// A new variable.
108    #[track_caller]
109    pub fn new(name: impl Into<String>) -> Variable {
110        Variable(Arc::new(Declared {
111            name: name.into(),
112            arity: 1,
113            changing: false,
114            place: Place::caller(),
115        }))
116    }
117
118    /// A variable over sets or relations of the given arity, for a
119    /// declaration `x: set e` (LOGIC.md section 5).
120    #[track_caller]
121    pub fn with_arity(name: impl Into<String>, arity: usize) -> Variable {
122        Variable(Arc::new(Declared {
123            name: name.into(),
124            arity,
125            changing: false,
126            place: Place::caller(),
127        }))
128    }
129
130    /// The arity: 1 for a variable over atoms.
131    pub fn arity(&self) -> usize {
132        self.0.arity
133    }
134
135    /// The name, for people.
136    pub fn name(&self) -> &str {
137        &self.0.name
138    }
139
140    /// Where it was made.
141    pub fn place(&self) -> &Place {
142        &self.0.place
143    }
144}
145
146impl PartialEq for Variable {
147    fn eq(&self, other: &Variable) -> bool {
148        Arc::ptr_eq(&self.0, &other.0)
149    }
150}
151
152impl Eq for Variable {}
153
154impl fmt::Debug for Variable {
155    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156        write!(f, "{}", self.0.name)
157    }
158}
159
160impl fmt::Display for Variable {
161    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162        write!(f, "{}", self.0.name)
163    }
164}