chess-startpos-rs 0.1.1

Generate, count, and sample chess back-rank arrangements under composable constraints (Chess960, Chess2880, custom presets)
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Constraint primitives and combinators.

use crate::{ColorKind, PieceKind};

/// Colour of a square — the default binary partition used by chess
/// (and the default type parameter `C` for [`Constraint`] /
/// [`crate::Problem`]).
///
/// For N-way colour partitions, define your own enum and use it as
/// the `C` type parameter. Any `Copy + Eq + Hash + Debug` type
/// satisfies [`ColorKind`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum SquareColor {
    /// Light-coloured square.
    Light,
    /// Dark-coloured square.
    Dark,
}

/// Comparison operator for count constraints.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum CountOp {
    /// Equal.
    Eq,
    /// Not equal.
    NotEq,
    /// Less than or equal.
    Le,
    /// Less than.
    Lt,
    /// Greater than or equal.
    Ge,
    /// Greater than.
    Gt,
}

impl CountOp {
    /// Returns whether `lhs` and `rhs` satisfy this comparison.
    ///
    /// Generic over any [`Ord`] type, so the same operator works for
    /// counts (`usize`) and signed positional offsets (`i32`).
    #[must_use]
    pub fn check<T: Ord>(self, lhs: T, rhs: T) -> bool {
        match self {
            Self::Eq => lhs == rhs,
            Self::NotEq => lhs != rhs,
            Self::Le => lhs <= rhs,
            Self::Lt => lhs < rhs,
            Self::Ge => lhs >= rhs,
            Self::Gt => lhs > rhs,
        }
    }
}

/// A single constraint over an arrangement of pieces.
///
/// Primitive constraints test a property of the arrangement;
/// combinator constraints (`And` / `Or` / `Not`) compose them.
///
/// `P` is the piece kind. `C` is the colour kind (defaults to
/// [`SquareColor`] — the binary light/dark partition).
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(bound(
        serialize = "P: serde::Serialize, C: serde::Serialize",
        deserialize = "P: serde::Deserialize<'de>, C: serde::Deserialize<'de>"
    ))
)]
#[non_exhaustive]
pub enum Constraint<P, C = SquareColor> {
    /// Number of occurrences of `piece` across the arrangement
    /// satisfies `(op, value)`.
    Count {
        /// Piece kind to count.
        piece: P,
        /// Comparison operator.
        op: CountOp,
        /// Right-hand value.
        value: usize,
    },
    /// Number of occurrences of `piece` on squares of the given colour
    /// satisfies `(op, value)`. Used for e.g. "bishops on opposite
    /// colours" by requiring one bishop on each colour.
    CountOnColor {
        /// Piece kind to count.
        piece: P,
        /// Square colour to count on.
        color: C,
        /// Comparison operator.
        op: CountOp,
        /// Right-hand value.
        value: usize,
    },
    /// The `piece` kind must occupy the given square index.
    /// Satisfied if any occurrence of `piece` is at `square`.
    At {
        /// Piece kind.
        piece: P,
        /// Square index.
        square: usize,
    },
    /// The `piece` kind must NOT occupy the given square index.
    NotAt {
        /// Piece kind.
        piece: P,
        /// Square index.
        square: usize,
    },
    /// Strict positional ordering: the indexed instances of the listed
    /// pieces must appear in strictly increasing square order.
    ///
    /// `Order(vec![(Rook, 0), (King, 0), (Rook, 1)])` is read as
    /// `rook[0] < king[0] < rook[1]`, i.e. the king lies strictly
    /// between the two rooks.
    ///
    /// If any `(piece, instance_idx)` in the chain references an
    /// instance that does not exist in the arrangement (e.g.
    /// `(Bishop, 2)` when only two bishops were declared via
    /// `Constraint::Count { Eq, 2 }`), the constraint is
    /// **unsatisfied** for that arrangement. The chain silently
    /// fails — it does not panic. For stricter upfront checking,
    /// call [`crate::Problem::validate`] (or use
    /// [`crate::ProblemBuilder::try_build`]).
    Order(Vec<(P, usize)>),
    /// Relative positional constraint between two specific piece
    /// instances:
    ///
    /// ```text
    /// (lhs.0[lhs.1].square as i32 - rhs.0[rhs.1].square as i32) op offset
    /// ```
    ///
    /// `Relative { lhs: (King, 0), rhs: (Queen, 0), op: CountOp::Eq, offset: 2 }`
    /// reads as "the king is exactly 2 squares to the right of the
    /// queen". Absolute distance `<= k` between two instances can be
    /// expressed as `And([Relative { op: Le, offset: k }, Relative { op:
    /// Ge, offset: -k }])` with matching lhs / rhs.
    ///
    /// If either `lhs.1` or `rhs.1` references an instance that
    /// doesn't exist in the arrangement, the constraint is
    /// **unsatisfied** for that arrangement (same convention as
    /// [`Constraint::Order`]).
    ///
    /// `offset` is `i32` because the difference of two `usize`
    /// squares is signed. Square indices are cast to `i32` before
    /// the subtraction, so boards with `num_squares > i32::MAX` are
    /// not supported by this constraint (chess uses 8).
    ///
    /// ```
    /// use chess_startpos_rs::{chess, Constraint, CountOp};
    ///
    /// // King exactly two squares to the right of the queen.
    /// let _ = Constraint::<chess::Piece>::Relative {
    ///     lhs: (chess::Piece::King, 0),
    ///     rhs: (chess::Piece::Queen, 0),
    ///     op: CountOp::Eq,
    ///     offset: 2,
    /// };
    /// ```
    Relative {
        /// Left-hand piece instance.
        lhs: (P, usize),
        /// Right-hand piece instance.
        rhs: (P, usize),
        /// Comparison operator applied to `(lhs - rhs) op offset`.
        op: CountOp,
        /// Signed offset on the right-hand side.
        offset: i32,
    },
    /// Logical AND: all child constraints must hold.
    ///
    /// `And(vec![])` is vacuously **true** (empty conjunction).
    /// `Constraint::And(vec![])` is the natural "always-true"
    /// constraint when you want to construct a problem with no
    /// filtering beyond the alphabet and counts.
    And(Vec<Constraint<P, C>>),
    /// Logical OR: at least one child constraint must hold.
    ///
    /// `Or(vec![])` is vacuously **false** (empty disjunction).
    /// Use [`Constraint::And`] with an empty vector for the
    /// always-true constraint instead.
    Or(Vec<Constraint<P, C>>),
    /// Logical NOT: child constraint must not hold.
    Not(Box<Constraint<P, C>>),
}

impl<P: PieceKind, C: ColorKind> Constraint<P, C> {
    /// Returns whether `arrangement` satisfies this constraint.
    ///
    /// `arrangement.len()` and `colors.len()` must agree.
    #[must_use]
    pub fn evaluate(&self, arrangement: &[P], colors: &[C]) -> bool {
        match self {
            Self::Count { piece, op, value } => {
                let n = arrangement.iter().filter(|p| *p == piece).count();
                op.check(n, *value)
            }
            Self::CountOnColor {
                piece,
                color,
                op,
                value,
            } => {
                let n = arrangement
                    .iter()
                    .zip(colors.iter())
                    .filter(|(p, c)| *p == piece && *c == color)
                    .count();
                op.check(n, *value)
            }
            Self::At { piece, square } => arrangement.get(*square) == Some(piece),
            Self::NotAt { piece, square } => arrangement.get(*square) != Some(piece),
            Self::Relative {
                lhs,
                rhs,
                op,
                offset,
            } => {
                let lhs_pos = nth_position(arrangement, &lhs.0, lhs.1);
                let rhs_pos = nth_position(arrangement, &rhs.0, rhs.1);
                match (lhs_pos, rhs_pos) {
                    (Some(l), Some(r)) => {
                        let diff = (l as i32) - (r as i32);
                        op.check(diff, *offset)
                    }
                    _ => false,
                }
            }
            Self::Order(chain) => {
                let mut positions: Vec<usize> = Vec::with_capacity(chain.len());
                for (piece_kind, instance_idx) in chain {
                    let occurrence = nth_position(arrangement, piece_kind, *instance_idx);
                    match occurrence {
                        Some(pos) => positions.push(pos),
                        None => return false,
                    }
                }
                positions.windows(2).all(|w| w[0] < w[1])
            }
            Self::And(children) => children.iter().all(|c| c.evaluate(arrangement, colors)),
            Self::Or(children) => children.iter().any(|c| c.evaluate(arrangement, colors)),
            Self::Not(inner) => !inner.evaluate(arrangement, colors),
        }
    }

    /// Collects every `Constraint::Count { piece, op: Eq, value }`
    /// keyed by `piece` from `self` and its top-level `And`-nested
    /// children. Used by the solver to derive the per-piece counts
    /// that define the multiset for the fast-path enumeration.
    pub(crate) fn collect_eq_counts(&self) -> Vec<(P, usize)> {
        let mut out = Vec::new();
        self.collect_eq_counts_into(&mut out);
        out
    }

    fn collect_eq_counts_into(&self, out: &mut Vec<(P, usize)>) {
        match self {
            Self::Count {
                piece,
                op: CountOp::Eq,
                value,
            } => out.push((*piece, *value)),
            Self::And(children) => {
                for c in children {
                    c.collect_eq_counts_into(out);
                }
            }
            _ => {}
        }
    }

    /// Walks the constraint tree, invoking `visitor` on every node.
    /// Used by validation to check that all `piece` / `color` /
    /// `square` references are consistent with the problem
    /// declarations.
    pub(crate) fn walk(&self, visitor: &mut impl FnMut(&Self)) {
        visitor(self);
        match self {
            Self::And(children) | Self::Or(children) => {
                for c in children {
                    c.walk(visitor);
                }
            }
            Self::Not(inner) => inner.walk(visitor),
            _ => {}
        }
    }

    /// Returns a logically-equivalent constraint with redundant
    /// structure removed.
    ///
    /// Every arrangement that satisfies `self` satisfies the
    /// returned constraint and vice-versa — semantics are preserved
    /// exactly. The rules applied bottom-up are:
    ///
    /// * `And([single])` → `single` (collapse single-child conjunctions).
    /// * `Or([single])`  → `single` (collapse single-child disjunctions).
    /// * `Not(Not(x))`   → `x` (eliminate double negation).
    /// * `Not(And([]))`  → `Or([])` (negation of vacuous truth).
    /// * `Not(Or([]))`   → `And([])` (negation of vacuous falsity).
    /// * If any `And` child simplifies to `Or([])` (vacuously false),
    ///   the whole `And` becomes `Or([])`.
    /// * If any `Or` child simplifies to `And([])` (vacuously true),
    ///   the whole `Or` becomes `And([])`.
    /// * Drop neutral children: `And([])` from inside another `And`,
    ///   `Or([])` from inside another `Or`.
    /// * Leaf constraints clone as-is.
    ///
    /// The method is idempotent: `c.simplify() == c.simplify().simplify()`.
    ///
    /// `simplify` does *not* impose editor-friendly semantics —
    /// `Or([])` remains the empty disjunction (always false). Callers
    /// that want to treat in-progress empty branches as no-ops should
    /// rewrite their tree before calling `simplify`.
    ///
    /// # Examples
    ///
    /// ```
    /// use chess_startpos_rs::{chess::Piece, Constraint, CountOp};
    ///
    /// let leaf = Constraint::Count {
    ///     piece: Piece::King,
    ///     op: CountOp::Eq,
    ///     value: 1,
    /// };
    /// // `And([leaf])` collapses to `leaf`.
    /// assert_eq!(
    ///     Constraint::And(vec![leaf.clone()]).simplify(),
    ///     leaf
    /// );
    /// // Double negation eliminates.
    /// assert_eq!(
    ///     Constraint::Not(Box::new(Constraint::Not(Box::new(leaf.clone())))).simplify(),
    ///     leaf
    /// );
    /// // An `Or([])` child collapses the whole `And` to `Or([])`.
    /// assert_eq!(
    ///     Constraint::And(vec![leaf.clone(), Constraint::Or(vec![])]).simplify(),
    ///     Constraint::<Piece>::Or(vec![]),
    /// );
    /// ```
    #[must_use]
    pub fn simplify(&self) -> Self {
        match self {
            Self::And(children) => {
                let mut acc: Vec<Self> = Vec::with_capacity(children.len());
                for c in children {
                    let c = c.simplify();
                    if is_false(&c) {
                        return Self::Or(Vec::new());
                    }
                    if is_true(&c) {
                        continue;
                    }
                    acc.push(c);
                }
                if acc.len() == 1 {
                    acc.pop().expect("len == 1")
                } else {
                    Self::And(acc)
                }
            }
            Self::Or(children) => {
                let mut acc: Vec<Self> = Vec::with_capacity(children.len());
                for c in children {
                    let c = c.simplify();
                    if is_true(&c) {
                        return Self::And(Vec::new());
                    }
                    if is_false(&c) {
                        continue;
                    }
                    acc.push(c);
                }
                if acc.len() == 1 {
                    acc.pop().expect("len == 1")
                } else {
                    Self::Or(acc)
                }
            }
            Self::Not(inner) => {
                let inner = inner.simplify();
                if is_true(&inner) {
                    Self::Or(Vec::new())
                } else if is_false(&inner) {
                    Self::And(Vec::new())
                } else if let Self::Not(grandchild) = inner {
                    *grandchild
                } else {
                    Self::Not(Box::new(inner))
                }
            }
            leaf => leaf.clone(),
        }
    }
}

#[inline]
fn is_true<P, C>(c: &Constraint<P, C>) -> bool {
    matches!(c, Constraint::And(v) if v.is_empty())
}

#[inline]
fn is_false<P, C>(c: &Constraint<P, C>) -> bool {
    matches!(c, Constraint::Or(v) if v.is_empty())
}

/// Returns the square index of the `instance_idx`-th occurrence of
/// `piece` in `arrangement`, or `None` if fewer than `instance_idx + 1`
/// occurrences exist.
fn nth_position<P: PieceKind>(arrangement: &[P], piece: &P, instance_idx: usize) -> Option<usize> {
    arrangement
        .iter()
        .enumerate()
        .filter_map(|(i, p)| (p == piece).then_some(i))
        .nth(instance_idx)
}

#[cfg(test)]
mod simplify_tests {
    use super::*;
    use crate::chess::Piece;

    fn leaf() -> Constraint<Piece> {
        Constraint::Count {
            piece: Piece::King,
            op: CountOp::Eq,
            value: 1,
        }
    }

    fn leaf2() -> Constraint<Piece> {
        Constraint::At {
            piece: Piece::Queen,
            square: 3,
        }
    }

    const fn true_<P, C>() -> Constraint<P, C> {
        Constraint::And(Vec::new())
    }

    const fn false_<P, C>() -> Constraint<P, C> {
        Constraint::Or(Vec::new())
    }

    #[test]
    fn leaves_simplify_to_themselves() {
        assert_eq!(leaf().simplify(), leaf());
        assert_eq!(leaf2().simplify(), leaf2());
    }

    #[test]
    fn empty_and_is_identity() {
        let c: Constraint<Piece> = true_();
        assert_eq!(c.simplify(), true_());
    }

    #[test]
    fn empty_or_is_identity() {
        let c: Constraint<Piece> = false_();
        assert_eq!(c.simplify(), false_());
    }

    #[test]
    fn single_child_and_unwraps() {
        let c = Constraint::And(vec![leaf()]);
        assert_eq!(c.simplify(), leaf());
    }

    #[test]
    fn single_child_or_unwraps() {
        let c = Constraint::Or(vec![leaf()]);
        assert_eq!(c.simplify(), leaf());
    }

    #[test]
    fn double_negation_eliminates() {
        let c = Constraint::Not(Box::new(Constraint::Not(Box::new(leaf()))));
        assert_eq!(c.simplify(), leaf());
    }

    #[test]
    fn triple_negation_collapses_to_single() {
        let c = Constraint::Not(Box::new(Constraint::Not(Box::new(Constraint::Not(
            Box::new(leaf()),
        )))));
        assert_eq!(c.simplify(), Constraint::Not(Box::new(leaf())));
    }

    #[test]
    fn not_of_true_is_false() {
        let c: Constraint<Piece> = Constraint::Not(Box::new(true_()));
        assert_eq!(c.simplify(), false_());
    }

    #[test]
    fn not_of_false_is_true() {
        let c: Constraint<Piece> = Constraint::Not(Box::new(false_()));
        assert_eq!(c.simplify(), true_());
    }

    #[test]
    fn false_propagates_through_and() {
        let c = Constraint::And(vec![leaf(), false_()]);
        assert_eq!(c.simplify(), false_());
    }

    #[test]
    fn true_propagates_through_or() {
        let c = Constraint::Or(vec![leaf(), true_()]);
        assert_eq!(c.simplify(), true_());
    }

    #[test]
    fn neutral_true_drops_from_and() {
        let c = Constraint::And(vec![leaf(), true_()]);
        assert_eq!(c.simplify(), leaf());
    }

    #[test]
    fn neutral_false_drops_from_or() {
        let c = Constraint::Or(vec![leaf(), false_()]);
        assert_eq!(c.simplify(), leaf());
    }

    #[test]
    fn nested_editor_worst_case() {
        // And([leaf, Or([Not(Not(leaf2)), Or([])])])
        // → And([leaf, Or([leaf2])])      (inner Or([]) drops; double Not folds)
        // → And([leaf, leaf2])             (single-child Or unwraps)
        let c = Constraint::And(vec![
            leaf(),
            Constraint::Or(vec![
                Constraint::Not(Box::new(Constraint::Not(Box::new(leaf2())))),
                false_(),
            ]),
        ]);
        let expected = Constraint::And(vec![leaf(), leaf2()]);
        assert_eq!(c.simplify(), expected);
    }

    #[test]
    fn simplify_is_idempotent() {
        let trees: [Constraint<Piece>; 6] = [
            leaf(),
            Constraint::And(vec![leaf(), Constraint::Not(Box::new(false_()))]),
            Constraint::Or(vec![false_(), Constraint::And(vec![leaf2()])]),
            Constraint::Not(Box::new(Constraint::Not(Box::new(leaf())))),
            Constraint::And(vec![Constraint::Or(vec![leaf(), false_()])]),
            true_(),
        ];
        for t in trees {
            let once = t.simplify();
            let twice = once.simplify();
            assert_eq!(once, twice, "not idempotent for {t:?}");
        }
    }

    #[test]
    fn deeply_nested_unsatisfiable_propagates_to_root() {
        // Or([And([leaf, Or([])])]) → Or([Or([])]) → Or([]) (false drops from Or)
        let c = Constraint::Or(vec![Constraint::And(vec![leaf(), false_()])]);
        assert_eq!(c.simplify(), false_());
    }

    #[test]
    fn nested_truth_propagates_to_root() {
        // And([Or([Not(Or([])), leaf])]) → And([Or([And([]), leaf])])
        // → And([And([])]) (true child collapses Or) → And([]) (single child unwrap)
        let c = Constraint::And(vec![Constraint::Or(vec![
            Constraint::Not(Box::new(false_())),
            leaf(),
        ])]);
        assert_eq!(c.simplify(), true_());
    }
}