elinvar_logic/
relation.rs1use 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#[derive(Clone)]
22pub struct Relation(Arc<Declared>);
23
24impl Relation {
25 #[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 #[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 pub fn is_changing(&self) -> bool {
50 self.0.changing
51 }
52
53 #[track_caller]
55 pub fn unary(name: impl Into<String>) -> Relation {
56 Relation::new(name, 1)
57 }
58
59 #[track_caller]
61 pub fn binary(name: impl Into<String>) -> Relation {
62 Relation::new(name, 2)
63 }
64
65 pub fn name(&self) -> &str {
67 &self.0.name
68 }
69
70 pub fn arity(&self) -> usize {
72 self.0.arity
73 }
74
75 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#[derive(Clone)]
104pub struct Variable(Arc<Declared>);
105
106impl Variable {
107 #[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 #[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 pub fn arity(&self) -> usize {
132 self.0.arity
133 }
134
135 pub fn name(&self) -> &str {
137 &self.0.name
138 }
139
140 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}