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
//! Rule trait — transformations in Information Universes.
//!
//! Per the Mathematical Constitution:
//! T is a set of maps from S to S. Rules define how states
//! evolve over time.
//!
//! # Rule context
//!
//! Different substrates need different information to apply a rule:
//!
//! - Cellular automata rules need nothing beyond the state itself.
//! - Graph rewrite rules need a vertex index and match information.
//! - Symbolic rewrite rules need a pattern location.
//!
//! The [`RuleContext`] trait and the associated `Context` type on
//! [`Rule`] allow each substrate to define what context its rules
//! require. The schedule creates the appropriate context internally;
//! users never need to construct contexts manually.
//!
//! # Implementing Rule
//!
//! ```rust
//! use arco::state::State;
//! use arco::rules::{Rule, RuleContext, NoContext};
//! use rand::Rng;
//!
//! // A CA rule needs no context
//! #[derive(Debug)]
//! struct CARule;
//!
//! impl<CAState: State> Rule<CAState> for CARule {
//! type Context = NoContext;
//! fn name(&self) -> &str { "Rule 110" }
//! fn apply(&self, state: &CAState, _ctx: &NoContext, _rng: &mut dyn Rng) -> CAState {
//! state.clone()
//! }
//! }
//!
//! // A graph rule needs match info
//! #[derive(Debug)]
//! struct RewriteRule;
//!
//! impl<BinaryGraphState: State> Rule<BinaryGraphState> for RewriteRule {
//! type Context = MatchInfo;
//! fn name(&self) -> &str { "NAND" }
//! fn apply(&self, state: &BinaryGraphState, _ctx: &MatchInfo, _rng: &mut dyn Rng) -> BinaryGraphState {
//! state.clone()
//! }
//! }
//!
//! #[derive(Clone, Debug)]
//! struct MatchInfo;
//!
//! impl RuleContext for MatchInfo {}
//!
//! fn main() {
//! let ca = CARule;
//! let re = RewriteRule;
//!
//! println!("rules: {:?}, {:?}", ca, re);
//! }
//! ```
use crateState;
use Rng;
use Debug;
/// Marker trait for rule context types.
///
/// A rule context carries substrate-specific information needed to
/// apply a rule. Each substrate defines its own context type.
///
/// # Examples
///
/// - [`NoContext`] — for rules that need nothing beyond the state.
/// - Graph substrates use match information (vertex, neighbors).
/// - Symbolic substrates use pattern locations (start, end).
/// A context for rules that need no additional information.
///
/// Use this when a rule can be applied to a state without knowing
/// which part of the state to modify. Cellular automata rules and
/// global transformation rules use `NoContext`.
;
/// A transformation rule for an Information Universe.
///
/// Rules define how states evolve. Each rule specifies a [`Context`]
/// type that carries the information needed to apply it.
///
/// # Type parameters
///
/// - `S: State` — The state type this rule operates on.
/// - `Context: RuleContext` — The context type needed for application.
///
/// # Design contracts
///
/// - **Immutability**: Rules return new states. They never modify
/// the input state in place.
/// - **Purity**: Rule application should be a pure function of the
/// state, context, and RNG.
/// - **Send + Sync**: Rules must be shareable across threads.
///
/// # Examples
///
/// ```
/// use arco::{
/// rules::{NoContext, Rule},
/// state::State,
/// };
/// use rand::{Rng, SeedableRng, rngs::StdRng};
///
/// #[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// struct CounterState {
/// value: u8,
/// }
///
/// impl CounterState {
/// fn new(value: u8) -> Self {
/// Self { value }
/// }
/// }
///
/// 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 }
/// }
/// }
///
/// #[derive(Debug)]
/// struct IncrementRule;
///
/// impl Rule<CounterState> for IncrementRule {
/// type Context = NoContext;
/// fn name(&self) -> &str {
/// "Increment"
/// }
///
/// fn apply(
/// &self,
/// state: &CounterState,
/// _context: &Self::Context,
/// _rng: &mut dyn Rng,
/// ) -> CounterState {
/// CounterState {
/// value: state.value.wrapping_add(1),
/// }
/// }
/// }
///
/// fn main() {
/// let mut rng = StdRng::seed_from_u64(42);
///
/// let mut state = CounterState::new(1);
/// println!("{:?}", state);
///
/// let rule = IncrementRule;
/// let context = NoContext;
///
/// for _ in 1..=254 {
/// state = rule.apply(&state, &context, &mut rng);
/// }
///
/// println!("{:?}", state);
/// }
/// ```