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
//! State trait — the fundamental abstraction for ARCO.
//!
//! Per the Mathematical Constitution:
//! A state space S is a set equipped with a canonical encoding
//! function, a distance function, and a cardinality bound.
//! States must be distinguishable via observation operators.
//!
//! # The State trait
//!
//! A type implementing `State` represents a single configuration of
//! an Information Universe at a point in time. States are immutable:
//! mutation methods return new states rather than modifying in place.
//!
//! # Requirements
//!
//! Implementors must provide:
//!
//! - **Canonical encoding**: A deterministic, unique representation
//! of the state as a hashable, comparable value. This is the
//! identity observation — the maximally dynamically sufficient
//! observation operator. Two states are equal if and only if
//! their canonical encodings are equal.
//!
//! - **Distance metric**: A function satisfying identity
//! (d(s, s) = 0), symmetry (d(s1, s2) = d(s2, s1)), and the
//! triangle inequality (d(s1, s3) ≤ d(s1, s2) + d(s2, s3)).
//! Used for perturbation analysis and error resilience metrics.
//!
//! # Design contract
//!
//! - States are immutable. Mutation operations return new state
//! objects.
//! - The canonical encoding must be independent of runtime state
//! (memory addresses, RNG seeds, platform-specific layout) and
//! must be identical across runs.
//! - Equality and hashing must be consistent with the canonical
//! encoding: `s1 == s2` if and only if
//! `s1.canonical_encoding() == s2.canonical_encoding()`.
//!
//! # Implementing State
//!
//! ```rust
//! use arco::state::State;
//!
//! #[derive(Clone, Debug, PartialEq, Eq, 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()
//! }
//! }
//!
//! fn main() {
//! let state = MyState {
//! data: vec![0, 1, 0, 0, 0, 1],
//! };
//!
//! assert_eq!(state.canonical_encoding(), vec![0, 1, 0, 0, 0, 1]);
//! assert_eq!(state.distance(&state), 0);
//! }
//! ```
//!
//! # Substrate-specific state types
//!
//! Concrete state implementations live in substrate modules:
//!
//! - [`arco::substrates::graph::BinaryGraphState`] — directed graphs
//! with binary vertex labels and binary edge labels.
//!
//! Users define their own state types by implementing this trait.
use Debug;
use Hash;
/// The fundamental abstraction for a state in an Information Universe.
///
/// A state represents a single configuration at a point in time.
/// States are immutable and must provide a canonical encoding for
/// identity, a distance metric for perturbation analysis, and
/// support cloning, equality, hashing, and thread-safe sharing.
///
/// # Type parameter
///
/// - `Encoding`: The type of the canonical encoding. Must be
/// hashable, comparable, cloneable, and thread-safe. Common
/// choices are `Vec<u8>`, `(Vec<u8>, Vec<u8>)`, or a custom
/// deterministic representation.
///
/// # Examples
///
/// ```rust
/// use arco::state::State;
///
/// fn main() {
/// let state1 = CounterState { value: 0 };
/// let state2 = CounterState { value: 1 };
/// let state3 = CounterState { value: 0 };
/// let state4 = state2.clone();
///
/// assert_eq!(state2.canonical_encoding(), state4.canonical_encoding());
/// assert_eq!(state1.canonical_encoding(), state3.canonical_encoding());
/// assert!(state1.canonical_encoding() != state2.canonical_encoding());
///
/// // Not identity d(s, s) != 0
/// assert_eq!(state1.distance(&state1), 1);
/// // Symetry d(s2, s1) = d(s1, s2)
/// assert_eq!(state2.distance(&state1), state1.distance(&state2));
///
/// println!("{:?}", state1);
/// println!("{:?}", state2);
/// }
///
/// #[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// struct CounterState {
/// value: u8,
/// }
///
/// impl State for CounterState {
/// type Encoding = Vec<u8>;
///
/// fn canonical_encoding(&self) -> Self::Encoding {
/// vec![self.value]
/// }
///
/// fn distance(&self, other: &Self) -> u32 {
/// if self.value == other.value { 1 } else { 0 }
/// }
/// }
/// ```