1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#[allow(unused_imports)]
use alloc::{boxed::Box, format, string::String, string::ToString, vec, vec::Vec};
use core::marker::PhantomData;
use super::arrow::Arrow;
use super::category::Category;
/// The opposite (dual) category of `C`.
///
/// Mac Lane (1971), *Categories for the Working Mathematician*, Ch. II §2.
///
/// `Op<C>` has the same objects as `C` but every morphism is reversed:
///
/// - An arrow `f: A → B` in `C` becomes an arrow `f^op: B → A` in `Op<C>`.
/// - Composition order is reversed: `compose_op(f, g) = compose_C(g, f)`.
///
/// This lets a contravariant construction (e.g., abductive inference from
/// observation back to cause) be expressed as a covariant `Functor` impl —
/// the Rust side of what category theorists write as `F: C^op → D`.
///
/// # Laws
///
/// When `C` satisfies the category laws, `Op<C>` does automatically. Verified
/// by [`crate::category::laws::category_law_axioms`] applied to `Op<C>`.
pub struct Op<C>(PhantomData<C>);
/// A morphism in `Op<C>`: the same underlying `C::Morphism`, but [`source`](Arrow::source)
/// and [`target`](Arrow::target) are swapped.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OpMorphism<M>(pub M);
impl<M: Arrow> Arrow for OpMorphism<M> {
type Object = M::Object;
type Kind = M::Kind;
fn source(&self) -> Self::Object {
self.0.target()
}
fn target(&self) -> Self::Object {
self.0.source()
}
fn kind(&self) -> Self::Kind {
self.0.kind()
}
}
impl<C: Category> Category for Op<C> {
type Object = C::Object;
type Morphism = OpMorphism<C::Morphism>;
fn identity(obj: &Self::Object) -> Self::Morphism {
// id_A^op == (id_A)^op — identity is its own opposite.
OpMorphism(C::identity(obj))
}
fn compose(f: &Self::Morphism, g: &Self::Morphism) -> Option<Self::Morphism> {
// In Op<C>:
// f: A → B means f.0: B → A in C
// g: B → C means g.0: C → B in C
// We want Op::compose(f, g): A → C in Op<C>, i.e. an underlying
// morphism C → A in C. That's the composition g.0 (C→B) then f.0 (B→A)
// — in diagrammatic order: C::compose(&g.0, &f.0).
C::compose(&g.0, &f.0).map(OpMorphism)
}
fn morphisms() -> Vec<Self::Morphism> {
C::morphisms().into_iter().map(OpMorphism).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::category::laws::assert_category_laws;
use crate::category::{Arrow, Concept, FinitelyGenerated};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Light {
Red,
Green,
}
impl Concept for Light {}
impl FinitelyGenerated for Light {
fn variants() -> Vec<Self> {
vec![Light::Red, Light::Green]
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct LightEdge {
from: Light,
to: Light,
}
impl Arrow for LightEdge {
type Object = Light;
type Kind = ();
fn source(&self) -> Light {
self.from
}
fn target(&self) -> Light {
self.to
}
fn kind(&self) {}
}
struct LightCat;
impl Category for LightCat {
type Object = Light;
type Morphism = LightEdge;
fn identity(obj: &Light) -> LightEdge {
LightEdge {
from: *obj,
to: *obj,
}
}
fn compose(f: &LightEdge, g: &LightEdge) -> Option<LightEdge> {
if f.to != g.from {
return None;
}
Some(LightEdge {
from: f.from,
to: g.to,
})
}
fn morphisms() -> Vec<LightEdge> {
vec![
LightEdge {
from: Light::Red,
to: Light::Red,
},
LightEdge {
from: Light::Green,
to: Light::Green,
},
LightEdge {
from: Light::Red,
to: Light::Green,
},
LightEdge {
from: Light::Green,
to: Light::Red,
},
]
}
}
#[test]
fn op_preserves_category_laws() {
assert_category_laws::<LightCat>();
assert_category_laws::<Op<LightCat>>();
}
#[test]
fn op_flips_source_and_target() {
let m = LightEdge {
from: Light::Red,
to: Light::Green,
};
let m_op = OpMorphism(m);
assert_eq!(m_op.source(), Light::Green);
assert_eq!(m_op.target(), Light::Red);
}
#[test]
fn op_composition_reverses_order() {
// In C: Red → Green composed with Green → Red gives Red → Red.
let r_to_g = LightEdge {
from: Light::Red,
to: Light::Green,
};
let g_to_r = LightEdge {
from: Light::Green,
to: Light::Red,
};
// In Op<C>: OpMorphism(r_to_g) has source Green, target Red.
// OpMorphism(g_to_r) has source Red, target Green.
// Composing OpMorphism(g_to_r): Red → Green with OpMorphism(r_to_g): Green → Red
// should produce a morphism Red → Red in Op<C>.
let composed = <Op<LightCat>>::compose(&OpMorphism(g_to_r), &OpMorphism(r_to_g)).unwrap();
assert_eq!(composed.source(), Light::Red);
assert_eq!(composed.target(), Light::Red);
}
}