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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! Schedule trait — temporal structure in Information Universes.
//!
//! Per the Mathematical Constitution:
//! K is the update schedule — a rule specifying the order and
//! selection of transformations at each timestep. Two universes
//! differing only in schedule are distinct objects of study.
//!
//! # The Schedule trait
//!
//! A schedule determines how rules are applied over time. It answers:
//! which rules fire, in what order, and with what concurrency?
//! The schedule is a first-class component — changing the schedule
//! can change whether computation is detected.
//!
//! # Schedule Semantics
//!
//! Schedules are classified along two axes:
//!
//! **Timing**
//! - *Synchronous*: All updates within a timestep are computed from
//! the same pre-timestep state. No update within the timestep
//! sees any other update.
//! - *Asynchronous*: Updates are applied immediately. Later updates
//! within the same timestep see the results of earlier updates.
//!
//! **Selection**
//! - *Exhaustive*: Every update site is visited exactly once per
//! timestep.
//! - *Stochastic*: Sites are sampled probabilistically.
//! - *Priority*: Sites are ordered by a fixed criterion.
//!
//! # Quick start
//!
//! Implement `Schedule` to define the temporal order of rule
//! application. The schedule creates rule contexts internally —
//! users never construct contexts manually:
//!
//! ```rust
//! use arco::state::State;
//! use arco::rules::{Rule, NoContext};
//! use arco::schedule::Schedule;
//! use rand::{Rng, rngs::StdRng, SeedableRng};
//!
//! #[derive(Clone, PartialEq, Eq, Hash, Debug)]
//! struct MyState { count: u32 }
//!
//! impl State for MyState {
//! type Encoding = Vec<u8>;
//! fn canonical_encoding(&self) -> Self::Encoding { self.count.to_le_bytes().to_vec() }
//! fn distance(&self, other: &Self) -> u32 {
//! if self.count == other.count { 0 } else { 1 }
//! }
//! }
//!
//! #[derive(Debug, Clone)]
//! struct AddOne;
//! impl Rule<MyState> for AddOne {
//! type Context = NoContext;
//! fn name(&self) -> &str { "AddOne" }
//! fn apply(&self, state: &MyState, _ctx: &NoContext, _rng: &mut dyn Rng) -> MyState {
//! MyState { count: state.count + 1 }
//! }
//! }
//!
//! /// Apply all rules in sequence, once per timestep.
//! #[derive(Debug, Clone)]
//! struct AllRulesSchedule;
//!
//! impl Schedule<MyState, AddOne> for AllRulesSchedule {
//! fn name(&self) -> &str { "all_rules" }
//! fn timing(&self) -> &str { "asynchronous" }
//! fn selection(&self) -> &str { "exhaustive" }
//!
//! fn step(&self, state: &MyState, rules: &[AddOne], rng: &mut dyn Rng) -> MyState {
//! let mut current = state.clone();
//! for rule in rules {
//! current = rule.apply(¤t, &NoContext, rng);
//! }
//! current
//! }
//! }
//!
//! let state = MyState { count: 0 };
//! let schedule = AllRulesSchedule;
//! let rules = vec![AddOne, AddOne, AddOne];
//! let mut rng = StdRng::seed_from_u64(42);
//! let result = schedule.step(&state, &rules, &mut rng);
//! assert_eq!(result.count, 3);
//! ```
//!
//! # Generic schedules (substrate-independent)
//!
//! These schedules work with any rule type that uses [`NoContext`].
//! Use them when you don't need substrate-specific scheduling logic.
//!
//! ```rust
//! use arco::state::State;
//! use arco::rules::{Rule, NoContext};
//! use arco::schedule::{Schedule, SequentialSchedule, RandomRuleSchedule};
//! use rand::{Rng, SeedableRng, rngs::StdRng};
//!
//! #[derive(Clone, PartialEq, Eq, Hash, Debug)]
//! struct Counter { value: u32 }
//!
//! impl State for Counter {
//! type Encoding = Vec<u8>;
//! fn canonical_encoding(&self) -> Self::Encoding { self.value.to_le_bytes().to_vec() }
//! fn distance(&self, other: &Self) -> u32 {
//! if self.value == other.value { 0 } else { 1 }
//! }
//! }
//!
//! #[derive(Debug, Clone)]
//! struct Increment;
//! impl Rule<Counter> for Increment {
//! type Context = NoContext;
//! fn name(&self) -> &str { "Increment" }
//! fn apply(&self, state: &Counter, _ctx: &NoContext, _rng: &mut dyn Rng) -> Counter {
//! Counter { value: state.value + 1 }
//! }
//! }
//!
//! let state = Counter { value: 0 };
//! let rules = vec![Increment, Increment, Increment];
//! let mut rng = StdRng::seed_from_u64(42);
//!
//! // SequentialSchedule: applies every rule in order
//! let sequential = SequentialSchedule::new();
//! let result = sequential.step(&state, &rules, &mut rng);
//! assert_eq!(result.value, 3);
//!
//! // RandomRuleSchedule: picks one rule at random per timestep
//! let random = RandomRuleSchedule::new();
//! let result = random.step(&state, &rules, &mut rng);
//! assert!(result.value == 1); // one increment applied
//! ```
//!
//! # Built-in schedules
//!
//! Substrate-specific schedules live in their substrate modules.
//! See [`arco::substrates::graph::AllVerticesSchedule`] for the
//! asynchronous exhaustive schedule used in the Binary Graph Universe.
//!
//! Generic schedules (sequential, random) can be defined in
//! [`arco::schedule::generic`] and reused across substrates.
use crate;
use crateState;
use ;
use Debug;
/// The temporal structure of an Information Universe.
///
/// A schedule determines the order and selection of rule applications
/// at each timestep.
///
/// # Type parameters
///
/// - `S: State` — The state type.
/// - `R: Rule<S>` — The rule type.
///
/// # Design contracts
///
/// - **Determinism**: Given the same state, rules, and RNG seed,
/// the schedule must produce the same next state. The RNG is
/// the sole source of stochasticity.
/// - **Immutability**: The schedule returns a new state. The input
/// state is not modified.
/// - **Send + Sync**: Schedules must be shareable across threads.
///
/// /// # Example
///
/// ```rust
/// use arco::state::State;
/// use arco::rules::{NoContext, Rule};
/// use arco::schedule::Schedule;
/// use rand::{Rng, rngs::StdRng, SeedableRng};
///
/// #[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 }
/// }
/// }
///
/// #[derive(Debug, Clone)]
/// struct FlipRule;
///
/// impl Rule<BitState> for FlipRule {
/// type Context = NoContext;
/// fn name(&self) -> &str { "Flip" }
/// fn apply(&self, state: &BitState, _ctx: &NoContext, _rng: &mut dyn Rng) -> BitState {
/// BitState { value: 1 - state.value }
/// }
/// }
///
/// /// A schedule that applies the first rule once per timestep.
/// #[derive(Debug, Clone)]
/// struct FirstRuleSchedule;
///
/// impl Schedule<BitState, FlipRule> for FirstRuleSchedule {
/// fn name(&self) -> &str { "first_rule" }
/// fn timing(&self) -> &str { "asynchronous" }
/// fn selection(&self) -> &str { "priority" }
///
/// fn step(&self, state: &BitState, rules: &[FlipRule], rng: &mut dyn Rng) -> BitState {
/// if let Some(rule) = rules.first() {
/// rule.apply(state, &NoContext, rng)
/// } else {
/// state.clone()
/// }
/// }
/// }
///
/// let state = BitState { value: 0 };
/// let schedule = FirstRuleSchedule;
/// let rules = vec![FlipRule];
/// let mut rng = StdRng::seed_from_u64(42);
/// let new_state = schedule.step(&state, &rules, &mut rng);
/// assert_eq!(new_state.value, 1);
/// ```
// ===================================================================
// Generic schedules (substrate-independent)
// ===================================================================
/// A schedule that applies all rules in sequence.
///
/// Every rule is applied exactly once per timestep, in the order
/// they appear in the rule set. Updates are asynchronous — each
/// rule sees the results of previous rules within the same timestep.
///
/// # Semantics
/// - Timing: asynchronous
/// - Selection: exhaustive (all rules fire, in order)
///
/// This is the simplest possible schedule and works with any rule
/// type.
;
/// A schedule that selects one rule at random per timestep.
///
/// At each timestep, a single rule is chosen uniformly at random
/// from the rule set and applied once. The same rule may be selected
/// across multiple timesteps — each timestep is an independent
/// random draw.
///
/// This models stochastic asynchronous dynamics where one
/// transformation event occurs per timestep.
///
/// # Semantics
/// - Timing: asynchronous (single update per timestep)
/// - Selection: stochastic (one rule drawn uniformly per timestep)
;