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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! Observation trait — epistemic access to Information Universes.
//!
//! Per the Mathematical Constitution:
//! O is a set of functions from S to Y, where Y is an observation
//! space. Observation operators must be dynamically sufficient
//! for the transformation dynamics.
//!
//! # The Observation trait
//!
//! An observation operator defines what an external observer can
//! "see" when looking at a state. The same universe can appear to
//! have high or low emergence depending on how it is observed.
//! This is not a bug — it is the observer-relative nature of
//! emergence, made explicit.
//!
//! # Granularity
//!
//! Observation operators range from the identity observation
//! (distinguishes every state) to coarse aggregates (e.g., counting
//! the number of active elements). Coarser observations have smaller
//! output alphabets, reducing estimator bias but potentially missing
//! information preserved in the details.
//!
//! # Dynamic Sufficiency
//!
//! An observation operator is dynamically sufficient if switching
//! from the identity observation to this operator does not
//! qualitatively change emergence metrics. If a coarser observation
//! reports zero storage while the identity observation reports
//! nonzero storage, the coarser observation is insufficient — it
//! misses the information being preserved.
//!
//! # Quick start
//!
//! Implement `Observation` for your observer type. The `Output` type
//! must be hashable and comparable — this is required for mutual
//! information estimation:
//!
//! ```rust
//! use arco::state::State;
//! use arco::observation::{Observation, IdentityObserver};
//!
//! #[derive(Clone, PartialEq, Eq, Hash, Debug)]
//! struct MyState { data: Vec<u8> }
//!
//! impl State for MyState {
//! type Encoding = Vec<u8>;
//! fn canonical_encoding(&self) -> Self::Encoding { self.data.clone() }
//! fn distance(&self, other: &Self) -> u32 {
//! self.data.iter().zip(other.data.iter())
//! .map(|(a,b)| if a != b { 1 } else { 0 }).sum()
//! }
//! }
//!
//! // A coarse observer: only the first byte.
//! #[derive(Debug, Clone)]
//! struct FirstByteObserver;
//!
//! impl Observation<MyState> for FirstByteObserver {
//! type Output = u8;
//! fn observe(&self, state: &MyState) -> Self::Output {
//! state.data.first().copied().unwrap_or(0)
//! }
//! }
//!
//! // Or use the built-in identity observer for maximum detail.
//! let state = MyState { data: vec![1, 2, 3] };
//! let coarse = FirstByteObserver;
//! let identity = IdentityObserver;
//!
//! assert_eq!(coarse.observe(&state), 1);
//! assert_eq!(identity.observe(&state), vec![1, 2, 3]);
//! ```
//!
//! # Built-in observers
//!
//! Substrate-specific observation operators live in their substrate
//! modules. See [`arco::substrates::graph::observation`] for Binary
//! Graph observers (full state, label vector, label sum, compound).
use crateState;
use Debug;
use Hash;
/// An observation operator for an Information Universe.
///
/// Defines how an external observer perceives states. The observation
/// output type must be hashable and comparable — this is required
/// for mutual information estimation, which groups observations
/// by equality.
///
/// # Type parameters
///
/// - `S: State` — The state type being observed.
/// - `Output` — The type of observation values. Must be hashable,
/// comparable, cloneable, and thread-safe.
///
/// # Determinism
///
/// Observation operators must be deterministic: the same state
/// always produces the same observation. No RNG, no timestamps,
/// no memory addresses.
///
/// # Statelessness
///
/// Observation operators should be stateless. If stateful observation
/// is needed (e.g., windowed observation over multiple timesteps),
/// the state should be managed externally and passed as context.
///
/// # Example
///
/// ```rust
/// use arco::state::State;
/// use arco::observation::Observation;
///
/// #[derive(Clone, PartialEq, Eq, Hash, Debug)]
/// struct BitState { value: u8 }
///
/// impl State for BitState {
/// type Encoding = Vec<u8>;
/// fn canonical_encoding(&self) -> Self::Encoding { vec![self.value] }
/// fn distance(&self, other: &Self) -> u32 {
/// if self.value == other.value { 0 } else { 1 }
/// }
/// }
///
/// /// Observe only the parity of the value.
/// #[derive(Debug, Clone)]
/// struct ParityObserver;
///
/// impl Observation<BitState> for ParityObserver {
/// type Output = u8;
///
/// fn observe(&self, state: &BitState) -> Self::Output {
/// state.value % 2
/// }
/// }
///
/// let state = BitState { value: 3 };
/// let observer = ParityObserver;
/// assert_eq!(observer.observe(&state), 1);
/// ```
/// The identity observation operator for any state type.
///
/// Returns the canonical encoding — the maximally dynamically
/// sufficient observation. Distinguishes every distinct state.
/// Use this as the baseline when testing whether coarser
/// observations are sufficient.
///
/// # Example
///
/// ```rust
/// use arco::state::State;
/// use arco::observation::{IdentityObserver, Observation};
///
/// #[derive(Clone, PartialEq, Eq, Debug, Hash)]
/// struct MyState { data: Vec<u8> }
/// impl State for MyState {
/// type Encoding = Vec<u8>;
/// fn canonical_encoding(&self) -> Self::Encoding { self.data.clone() }
/// fn distance(&self, other: &Self) -> u32 {
/// self.data.iter().zip(other.data.iter())
/// .map(|(a, b)| if a != b { 1 } else { 0 }).sum()
/// }
/// }
///
/// let state = MyState { data: vec![0, 1, 0, 1] };
/// // For any State type, IdentityObserver returns the canonical encoding.
/// let observer = IdentityObserver;
/// let encoding = observer.observe(&state);
/// assert_eq!(encoding, vec![0, 1, 0, 1]);
/// ```
;
/// A windowed observation wraps a single-state observer to observe
/// multiple consecutive states.
///
/// Windowed observation enables detection of temporal patterns
/// (oscillations, propagation delays) that are invisible at the
/// single-step level.
///
/// # Type parameters
///
/// - `S: State` — The state type.
/// - `O: Observation<S>` — The inner single-state observer.
///
/// # Example
///
/// ```rust
/// use arco::state::State;
/// use arco::observation::{WindowedObserver, IdentityObserver};
///
/// #[derive(Clone, PartialEq, Eq, Debug, Hash)]
/// struct MyState { data: Vec<u8> }
/// impl State for MyState {
/// type Encoding = Vec<u8>;
/// fn canonical_encoding(&self) -> Self::Encoding { self.data.clone() }
/// fn distance(&self, other: &Self) -> u32 {
/// self.data.iter().zip(other.data.iter())
/// .map(|(a, b)| if a != b { 1 } else { 0 }).sum()
/// }
/// }
///
/// let state = MyState { data: vec![0, 1, 0, 1] };
/// let observer = WindowedObserver::new(IdentityObserver, 3);
/// // observes all three states
/// let result = observer.observe_window(&[state.clone(), state.clone(), state.clone()]);
/// assert_eq!(result, [[0, 1, 0, 1]; 3].to_vec());
/// ```