Skip to content

Commit 3b6a4dc

Browse files
committed
REFACTOR: Reduce duplication in walking backward link models.
Also, test that the new models validate.
1 parent cf96c18 commit 3b6a4dc

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

packages/catlog/src/stdlib/models.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,60 +89,38 @@ pub fn walking_attr(th: Rc<DiscreteDblTheory>) -> DiscreteDblModel {
8989
/// morphism back to the morphism itself.
9090
///
9191
/// In system dynamics jargon, a backward link defines a "reinforcing loop,"
92-
/// assuming the link has a positive effect on the flow. An example is an infection
93-
/// flow in a model of an infectious disease, where increasing the number of
94-
/// infectives increases the rate of infection of the remaining susceptibles (other
95-
/// things equal).
92+
/// assuming the link has a positive effect on the flow. An example is an
93+
/// infection flow an infectious disease model, where increasing the number of
94+
/// infectives increases the rate of infection of the remaining susceptibles
95+
/// (other things equal).
9696
pub fn backward_link(th: Rc<DiscreteTabTheory>) -> DiscreteTabModel {
97-
let ob_type = TabObType::Basic(name("Object"));
98-
let mut model = DiscreteTabModel::new(th.clone());
99-
model.add_ob(name("x"), ob_type.clone());
100-
model.add_ob(name("y"), ob_type.clone());
101-
model.add_mor(name("f"), name("x").into(), name("y").into(), th.hom_type(ob_type));
102-
model.add_mor(
103-
name("link"),
104-
name("y").into(),
105-
model.tabulated_gen(name("f")),
106-
TabMorType::Basic(name("Link")),
107-
);
108-
model
97+
backward_link_of_type(th, TabMorType::Basic(name("Link")))
10998
}
11099

111100
/// The "walking" backward positive link.
112101
///
113102
/// This is the free category with signed links that has a positive link from
114103
/// the codomain of a morphism back to the morphism itself.
115104
pub fn positive_backward_link(th: Rc<DiscreteTabTheory>) -> DiscreteTabModel {
116-
let ob_type = TabObType::Basic(name("Object"));
117-
let mut model = DiscreteTabModel::new(th.clone());
118-
model.add_ob(name("x"), ob_type.clone());
119-
model.add_ob(name("y"), ob_type.clone());
120-
model.add_mor(name("f"), name("x").into(), name("y").into(), th.hom_type(ob_type));
121-
model.add_mor(
122-
name("link"),
123-
name("y").into(),
124-
model.tabulated_gen(name("f")),
125-
TabMorType::Basic(name("Link")),
126-
);
127-
model
105+
// The type for positive links is just `Link`.
106+
backward_link_of_type(th, TabMorType::Basic(name("Link")))
128107
}
129108

130109
/// The "walking" backward negative link.
131110
///
132111
/// This is the free category with signed links that has a negative link from
133112
/// the codomain of a morphism back to the morphism itself.
134113
pub fn negative_backward_link(th: Rc<DiscreteTabTheory>) -> DiscreteTabModel {
114+
backward_link_of_type(th, TabMorType::Basic(name("NegativeLink")))
115+
}
116+
117+
fn backward_link_of_type(th: Rc<DiscreteTabTheory>, link_type: TabMorType) -> DiscreteTabModel {
135118
let ob_type = TabObType::Basic(name("Object"));
136119
let mut model = DiscreteTabModel::new(th.clone());
137120
model.add_ob(name("x"), ob_type.clone());
138121
model.add_ob(name("y"), ob_type.clone());
139122
model.add_mor(name("f"), name("x").into(), name("y").into(), th.hom_type(ob_type));
140-
model.add_mor(
141-
name("link"),
142-
name("y").into(),
143-
model.tabulated_gen(name("f")),
144-
TabMorType::Basic(name("NegativeLink")),
145-
);
123+
model.add_mor(name("link"), name("y").into(), model.tabulated_gen(name("f")), link_type);
146124
model
147125
}
148126

@@ -230,6 +208,13 @@ mod tests {
230208
assert!(backward_link(th).validate().is_ok());
231209
}
232210

211+
#[test]
212+
fn categories_with_signed_links() {
213+
let th = Rc::new(th_category_signed_links());
214+
assert!(positive_backward_link(th.clone()).validate().is_ok());
215+
assert!(negative_backward_link(th.clone()).validate().is_ok());
216+
}
217+
233218
#[test]
234219
fn sym_monoidal_categories() {
235220
let th = Rc::new(th_sym_monoidal_category());

0 commit comments

Comments
 (0)