From e617877dd58c5440bfc41bfe2c9b009767656731 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Fri, 28 Jul 2023 10:02:38 -0400 Subject: [PATCH 1/3] Add type system chess code --- .../type-system-chess/Cargo.toml | 9 + .../type-system-chess/src/board_rep.rs | 4 + .../type-system-chess/src/board_rep/board.rs | 93 ++++ .../src/board_rep/board/idx.rs | 175 +++++++ .../src/board_rep/board/write.rs | 282 +++++++++++ .../type-system-chess/src/board_rep/color.rs | 24 + .../type-system-chess/src/board_rep/piece.rs | 102 ++++ .../type-system-chess/src/board_rep/square.rs | 183 +++++++ .../src/board_rep/square/file.rs | 258 ++++++++++ .../src/board_rep/square/offset.rs | 111 +++++ .../src/board_rep/square/offset/file.rs | 129 +++++ .../src/board_rep/square/offset/rank.rs | 129 +++++ .../src/board_rep/square/rank.rs | 258 ++++++++++ .../src/board_rep/square/set.rs | 454 ++++++++++++++++++ .../type-system-chess/src/main.rs | 31 ++ .../type-system-chess/src/move_gen.rs | 76 +++ .../src/move_gen/attacked.rs | 117 +++++ .../type-system-chess/src/move_gen/bishop.rs | 36 ++ .../type-system-chess/src/move_gen/cast.rs | 67 +++ .../type-system-chess/src/move_gen/castle.rs | 206 ++++++++ .../type-system-chess/src/move_gen/check.rs | 125 +++++ .../type-system-chess/src/move_gen/king.rs | 51 ++ .../type-system-chess/src/move_gen/knight.rs | 51 ++ .../type-system-chess/src/move_gen/list.rs | 71 +++ .../src/move_gen/ml_from_sl.rs | 109 +++++ .../type-system-chess/src/move_gen/moves.rs | 89 ++++ .../type-system-chess/src/move_gen/pawn.rs | 433 +++++++++++++++++ .../type-system-chess/src/move_gen/pmoves.rs | 171 +++++++ .../type-system-chess/src/move_gen/queen.rs | 48 ++ .../type-system-chess/src/move_gen/rook.rs | 36 ++ .../type-system-chess/src/state.rs | 301 ++++++++++++ .../type-system-chess/src/state/outcome.rs | 99 ++++ .../type-system-chess/src/test.rs | 3 + .../type-system-chess/src/ui.rs | 223 +++++++++ .../type-system-chess/src/util.rs | 88 ++++ .../type-system-chess/src/values.rs | 416 ++++++++++++++++ 36 files changed, 5058 insertions(+) create mode 100644 collector/compile-benchmarks/type-system-chess/Cargo.toml create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/board.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/board/idx.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/board/write.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/color.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/piece.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/square.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/square/file.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/file.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/rank.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/square/rank.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/board_rep/square/set.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/main.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/attacked.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/bishop.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/cast.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/castle.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/check.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/king.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/knight.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/list.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/ml_from_sl.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/moves.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/pawn.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/pmoves.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/queen.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/move_gen/rook.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/state.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/state/outcome.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/test.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/ui.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/util.rs create mode 100644 collector/compile-benchmarks/type-system-chess/src/values.rs diff --git a/collector/compile-benchmarks/type-system-chess/Cargo.toml b/collector/compile-benchmarks/type-system-chess/Cargo.toml new file mode 100644 index 000000000..fb2d35399 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" +license = "MIT" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep.rs new file mode 100644 index 000000000..35330406f --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep.rs @@ -0,0 +1,4 @@ +pub mod board; +pub mod color; +pub mod piece; +pub mod square; diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/board.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/board.rs new file mode 100644 index 000000000..8e4a5f0ce --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/board.rs @@ -0,0 +1,93 @@ +use super::piece::ColoredPieceTy; +use crate::values; +use std::marker::PhantomData; + +pub mod idx; +pub mod write; + +pub(crate) trait CellEn { + fn reify() -> values::Cell; +} +pub(crate) struct Empty; +pub(crate) struct Filled(PhantomData

); + +impl CellEn for Empty { + fn reify() -> values::Cell { + values::Cell::Empty + } +} +impl CellEn for Filled

{ + fn reify() -> values::Cell { + values::Cell::Filled(P::reify()) + } +} + +pub(crate) trait BoardRankTy { + fn reify() -> values::BoardRank; +} +pub(crate) struct BoardRank< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, +>(PhantomData<(A, B, C, D, E, F, G, H)>); + +impl + BoardRankTy for BoardRank +{ + fn reify() -> values::BoardRank { + values::BoardRank { + a: A::reify(), + b: B::reify(), + c: C::reify(), + d: D::reify(), + e: E::reify(), + f: F::reify(), + g: G::reify(), + h: H::reify(), + } + } +} + +pub(crate) trait BoardTy { + fn reify() -> values::Board; +} +pub(crate) struct Board< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, +>(PhantomData<(R1, R2, R3, R4, R5, R6, R7, R8)>); + +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > BoardTy for Board +{ + fn reify() -> values::Board { + values::Board { + r1: R1::reify(), + r2: R2::reify(), + r3: R3::reify(), + r4: R4::reify(), + r5: R5::reify(), + r6: R6::reify(), + r7: R7::reify(), + r8: R8::reify(), + } + } +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/board/idx.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/board/idx.rs new file mode 100644 index 000000000..f77e51fa6 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/board/idx.rs @@ -0,0 +1,175 @@ +use super::{Board, BoardRank, BoardRankTy, BoardTy, CellEn}; +use crate::board_rep::square::{ + file::{self, FileEn}, + rank::{self, RankEn}, + Square, SquareTy, +}; + +pub(crate) trait RunIdxRank { + type Output: CellEn; +} +pub(crate) type IdxRank = >::Output; + +impl + RunIdxRank for BoardRank +{ + type Output = A; +} +impl + RunIdxRank for BoardRank +{ + type Output = B; +} +impl + RunIdxRank for BoardRank +{ + type Output = C; +} +impl + RunIdxRank for BoardRank +{ + type Output = D; +} +impl + RunIdxRank for BoardRank +{ + type Output = E; +} +impl + RunIdxRank for BoardRank +{ + type Output = F; +} +impl + RunIdxRank for BoardRank +{ + type Output = G; +} +impl + RunIdxRank for BoardRank +{ + type Output = H; +} + +pub(crate) trait RunIdxBoardRank { + type Output: BoardRankTy; +} +pub(crate) type IdxBoardRank = >::Output; + +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R1; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R2; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R3; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R4; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R5; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R6; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R7; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + > RunIdxBoardRank for Board +{ + type Output = R8; +} + +pub(crate) trait RunIdxBoard { + type Output: CellEn; +} +pub(crate) type IdxBoard = >::Output; + +impl RunIdxBoard> for B +where + B: RunIdxBoardRank, + IdxBoardRank: RunIdxRank, +{ + type Output = IdxRank, F>; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/board/write.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/board/write.rs new file mode 100644 index 000000000..bf0cbc2ca --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/board/write.rs @@ -0,0 +1,282 @@ +use crate::board_rep::square::{ + file::{self, FileEn}, + offset::{MaybeSquare, NoSquare, SomeSquare}, + rank, Square, SquareTy, +}; + +use super::{Board, BoardRank, BoardRankTy, BoardTy, CellEn}; + +pub(crate) trait RunWriteToBoardRank: BoardRankTy { + type Output: BoardRankTy; +} +pub(crate) type WriteToBoardRank = >::Output; + +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} +impl< + A: CellEn, + B: CellEn, + C: CellEn, + D: CellEn, + E: CellEn, + F: CellEn, + G: CellEn, + H: CellEn, + CE: CellEn, + > RunWriteToBoardRank for BoardRank +{ + type Output = BoardRank; +} + +pub(crate) trait RunWriteToBoard: BoardTy { + type Output: BoardTy; +} +pub(crate) type WriteToBoard = >::Output; + +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R1: RunWriteToBoardRank, +{ + type Output = Board, R2, R3, R4, R5, R6, R7, R8>; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R2: RunWriteToBoardRank, +{ + type Output = Board, R3, R4, R5, R6, R7, R8>; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R3: RunWriteToBoardRank, +{ + type Output = Board, R4, R5, R6, R7, R8>; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R4: RunWriteToBoardRank, +{ + type Output = Board, R5, R6, R7, R8>; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R5: RunWriteToBoardRank, +{ + type Output = Board, R6, R7, R8>; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R6: RunWriteToBoardRank, +{ + type Output = Board, R7, R8>; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R7: RunWriteToBoardRank, +{ + type Output = Board, R8>; +} +impl< + R1: BoardRankTy, + R2: BoardRankTy, + R3: BoardRankTy, + R4: BoardRankTy, + R5: BoardRankTy, + R6: BoardRankTy, + R7: BoardRankTy, + R8: BoardRankTy, + F: FileEn, + C: CellEn, + > RunWriteToBoard, C> for Board +where + R8: RunWriteToBoardRank, +{ + type Output = Board>; +} + +pub(crate) trait RunMaybeWriteToBoard: BoardTy { + type Output: BoardTy; +} +pub(crate) type MaybeWriteToBoard = >::Output; + +impl RunMaybeWriteToBoard for B { + type Output = B; +} +impl RunMaybeWriteToBoard, C> for B +where + B: RunWriteToBoard, +{ + type Output = WriteToBoard; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/color.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/color.rs new file mode 100644 index 000000000..4b1df34a3 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/color.rs @@ -0,0 +1,24 @@ +use crate::values; + +pub(crate) trait ColorEn { + type Other: ColorEn; + + fn reify() -> values::Color; +} +pub(crate) struct White; +pub(crate) struct Black; + +impl ColorEn for White { + type Other = Black; + + fn reify() -> values::Color { + values::Color::White + } +} +impl ColorEn for Black { + type Other = White; + + fn reify() -> values::Color { + values::Color::Black + } +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/piece.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/piece.rs new file mode 100644 index 000000000..4b8019aa9 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/piece.rs @@ -0,0 +1,102 @@ +use crate::{values, util::{Bool, False, True}}; + +use super::color::ColorEn; +use std::marker::PhantomData; + +pub(crate) trait PieceEn { + fn reify() -> values::Piece; +} +pub(crate) struct Pawn; +pub(crate) struct Bishop; +pub(crate) struct Knight; +pub(crate) struct Rook; +pub(crate) struct Queen; +pub(crate) struct King; + +impl PieceEn for Pawn { + fn reify() -> values::Piece { + values::Piece::Pawn + } +} +impl PieceEn for Bishop { + fn reify() -> values::Piece { + values::Piece::Bishop + } +} +impl PieceEn for Knight { + fn reify() -> values::Piece { + values::Piece::Knight + } +} +impl PieceEn for Rook { + fn reify() -> values::Piece { + values::Piece::Rook + } +} +impl PieceEn for Queen { + fn reify() -> values::Piece { + values::Piece::Queen + } +} +impl PieceEn for King { + fn reify() -> values::Piece { + values::Piece::King + } +} + +pub(crate) trait ColoredPieceTy { + fn reify() -> values::ColoredPiece; +} +pub(crate) struct ColoredPiece(PhantomData<(P, C)>); + +impl ColoredPieceTy for ColoredPiece { + fn reify() -> values::ColoredPiece { + values::ColoredPiece { + piece: P::reify(), + color: C::reify(), + } + } +} + +pub(crate) trait RunPieceEq: PieceEn { + type Output: Bool; +} +pub(crate) type PieceEq = >::Output; + +impl RunPieceEq for Pawn { type Output = True; } +impl RunPieceEq for Bishop { type Output = False; } +impl RunPieceEq for Knight { type Output = False; } +impl RunPieceEq for Rook { type Output = False; } +impl RunPieceEq for Queen { type Output = False; } +impl RunPieceEq for King { type Output = False; } +impl RunPieceEq for Pawn { type Output = False; } +impl RunPieceEq for Bishop { type Output = True; } +impl RunPieceEq for Knight { type Output = False; } +impl RunPieceEq for Rook { type Output = False; } +impl RunPieceEq for Queen { type Output = False; } +impl RunPieceEq for King { type Output = False; } +impl RunPieceEq for Pawn { type Output = False; } +impl RunPieceEq for Bishop { type Output = False; } +impl RunPieceEq for Knight { type Output = True; } +impl RunPieceEq for Rook { type Output = False; } +impl RunPieceEq for Queen { type Output = False; } +impl RunPieceEq for King { type Output = False; } +impl RunPieceEq for Pawn { type Output = False; } +impl RunPieceEq for Bishop { type Output = False; } +impl RunPieceEq for Knight { type Output = False; } +impl RunPieceEq for Rook { type Output = True; } +impl RunPieceEq for Queen { type Output = False; } +impl RunPieceEq for King { type Output = False; } +impl RunPieceEq for Pawn { type Output = False; } +impl RunPieceEq for Bishop { type Output = False; } +impl RunPieceEq for Knight { type Output = False; } +impl RunPieceEq for Rook { type Output = False; } +impl RunPieceEq for Queen { type Output = True; } +impl RunPieceEq for King { type Output = False; } +impl RunPieceEq for Pawn { type Output = False; } +impl RunPieceEq for Bishop { type Output = False; } +impl RunPieceEq for Knight { type Output = False; } +impl RunPieceEq for Rook { type Output = False; } +impl RunPieceEq for Queen { type Output = False; } +impl RunPieceEq for King { type Output = True; } + diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/square.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/square.rs new file mode 100644 index 000000000..6ded1ad88 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/square.rs @@ -0,0 +1,183 @@ +pub mod file; +pub mod offset; +pub mod rank; +pub mod set; + +use self::{ + file::{FileEn, FileEq, RunFileEq}, + rank::{RankEn, RankEq, RunRankEq}, +}; +use crate::{ + move_gen::list::{SLCons, SLNil}, + util::{And, Bool, RunAnd}, + values, +}; +use std::marker::PhantomData; + +pub(crate) trait SquareTy { + fn reify() -> values::Square; +} +pub(crate) struct Square(PhantomData<(R, F)>); + +impl SquareTy for Square { + fn reify() -> values::Square { + values::Square { + rank: R::reify(), + file: F::reify(), + } + } +} + +pub(crate) trait RunSquareEq: SquareTy { + type Output: Bool; +} +pub(crate) type SquareEq = >::Output; + +impl RunSquareEq> for Square +where + R1: RunRankEq, + F1: RunFileEq, + RankEq: RunAnd>, +{ + type Output = And, FileEq>; +} + +// I can't think of a more elegant way to do this. +pub(crate) type AllSqs = +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLCons, +SLNil, +>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>; + +pub(crate) mod all_sqs { + #![allow(unused)] + + use super::{file, rank, Square}; + + pub(crate) type A1 = Square; + pub(crate) type B1 = Square; + pub(crate) type C1 = Square; + pub(crate) type D1 = Square; + pub(crate) type E1 = Square; + pub(crate) type F1 = Square; + pub(crate) type G1 = Square; + pub(crate) type H1 = Square; + pub(crate) type A2 = Square; + pub(crate) type B2 = Square; + pub(crate) type C2 = Square; + pub(crate) type D2 = Square; + pub(crate) type E2 = Square; + pub(crate) type F2 = Square; + pub(crate) type G2 = Square; + pub(crate) type H2 = Square; + pub(crate) type A3 = Square; + pub(crate) type B3 = Square; + pub(crate) type C3 = Square; + pub(crate) type D3 = Square; + pub(crate) type E3 = Square; + pub(crate) type F3 = Square; + pub(crate) type G3 = Square; + pub(crate) type H3 = Square; + pub(crate) type A4 = Square; + pub(crate) type B4 = Square; + pub(crate) type C4 = Square; + pub(crate) type D4 = Square; + pub(crate) type E4 = Square; + pub(crate) type F4 = Square; + pub(crate) type G4 = Square; + pub(crate) type H4 = Square; + pub(crate) type A5 = Square; + pub(crate) type B5 = Square; + pub(crate) type C5 = Square; + pub(crate) type D5 = Square; + pub(crate) type E5 = Square; + pub(crate) type F5 = Square; + pub(crate) type G5 = Square; + pub(crate) type H5 = Square; + pub(crate) type A6 = Square; + pub(crate) type B6 = Square; + pub(crate) type C6 = Square; + pub(crate) type D6 = Square; + pub(crate) type E6 = Square; + pub(crate) type F6 = Square; + pub(crate) type G6 = Square; + pub(crate) type H6 = Square; + pub(crate) type A7 = Square; + pub(crate) type B7 = Square; + pub(crate) type C7 = Square; + pub(crate) type D7 = Square; + pub(crate) type E7 = Square; + pub(crate) type F7 = Square; + pub(crate) type G7 = Square; + pub(crate) type H7 = Square; + pub(crate) type A8 = Square; + pub(crate) type B8 = Square; + pub(crate) type C8 = Square; + pub(crate) type D8 = Square; + pub(crate) type E8 = Square; + pub(crate) type F8 = Square; + pub(crate) type G8 = Square; + pub(crate) type H8 = Square; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/square/file.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/file.rs new file mode 100644 index 000000000..a5d722c5c --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/file.rs @@ -0,0 +1,258 @@ +use crate::{ + util::{Bool, False, True}, + values, +}; + +pub(crate) trait FileEn { + fn reify() -> values::File; +} +pub(crate) struct FA; +pub(crate) struct FB; +pub(crate) struct FC; +pub(crate) struct FD; +pub(crate) struct FE; +pub(crate) struct FF; +pub(crate) struct FG; +pub(crate) struct FH; + +impl FileEn for FA { + fn reify() -> values::File { + values::File::A + } +} +impl FileEn for FB { + fn reify() -> values::File { + values::File::B + } +} +impl FileEn for FC { + fn reify() -> values::File { + values::File::C + } +} +impl FileEn for FD { + fn reify() -> values::File { + values::File::D + } +} +impl FileEn for FE { + fn reify() -> values::File { + values::File::E + } +} +impl FileEn for FF { + fn reify() -> values::File { + values::File::F + } +} +impl FileEn for FG { + fn reify() -> values::File { + values::File::G + } +} +impl FileEn for FH { + fn reify() -> values::File { + values::File::H + } +} + +pub(crate) trait RunFileEq: FileEn { + type Output: Bool; +} +pub(crate) type FileEq = >::Output; + +// Its disturbing that I can't think of a better way to do this +// It seems like we would need some sort of negative impl or +// specificity. +impl RunFileEq for FA { + type Output = True; +} +impl RunFileEq for FB { + type Output = False; +} +impl RunFileEq for FC { + type Output = False; +} +impl RunFileEq for FD { + type Output = False; +} +impl RunFileEq for FE { + type Output = False; +} +impl RunFileEq for FF { + type Output = False; +} +impl RunFileEq for FG { + type Output = False; +} +impl RunFileEq for FH { + type Output = False; +} +impl RunFileEq for FA { + type Output = False; +} +impl RunFileEq for FB { + type Output = True; +} +impl RunFileEq for FC { + type Output = False; +} +impl RunFileEq for FD { + type Output = False; +} +impl RunFileEq for FE { + type Output = False; +} +impl RunFileEq for FF { + type Output = False; +} +impl RunFileEq for FG { + type Output = False; +} +impl RunFileEq for FH { + type Output = False; +} +impl RunFileEq for FA { + type Output = False; +} +impl RunFileEq for FB { + type Output = False; +} +impl RunFileEq for FC { + type Output = True; +} +impl RunFileEq for FD { + type Output = False; +} +impl RunFileEq for FE { + type Output = False; +} +impl RunFileEq for FF { + type Output = False; +} +impl RunFileEq for FG { + type Output = False; +} +impl RunFileEq for FH { + type Output = False; +} +impl RunFileEq for FA { + type Output = False; +} +impl RunFileEq for FB { + type Output = False; +} +impl RunFileEq for FC { + type Output = False; +} +impl RunFileEq for FD { + type Output = True; +} +impl RunFileEq for FE { + type Output = False; +} +impl RunFileEq for FF { + type Output = False; +} +impl RunFileEq for FG { + type Output = False; +} +impl RunFileEq for FH { + type Output = False; +} +impl RunFileEq for FA { + type Output = False; +} +impl RunFileEq for FB { + type Output = False; +} +impl RunFileEq for FC { + type Output = False; +} +impl RunFileEq for FD { + type Output = False; +} +impl RunFileEq for FE { + type Output = True; +} +impl RunFileEq for FF { + type Output = False; +} +impl RunFileEq for FG { + type Output = False; +} +impl RunFileEq for FH { + type Output = False; +} +impl RunFileEq for FA { + type Output = False; +} +impl RunFileEq for FB { + type Output = False; +} +impl RunFileEq for FC { + type Output = False; +} +impl RunFileEq for FD { + type Output = False; +} +impl RunFileEq for FE { + type Output = False; +} +impl RunFileEq for FF { + type Output = True; +} +impl RunFileEq for FG { + type Output = False; +} +impl RunFileEq for FH { + type Output = False; +} +impl RunFileEq for FA { + type Output = False; +} +impl RunFileEq for FB { + type Output = False; +} +impl RunFileEq for FC { + type Output = False; +} +impl RunFileEq for FD { + type Output = False; +} +impl RunFileEq for FE { + type Output = False; +} +impl RunFileEq for FF { + type Output = False; +} +impl RunFileEq for FG { + type Output = True; +} +impl RunFileEq for FH { + type Output = False; +} +impl RunFileEq for FA { + type Output = False; +} +impl RunFileEq for FB { + type Output = False; +} +impl RunFileEq for FC { + type Output = False; +} +impl RunFileEq for FD { + type Output = False; +} +impl RunFileEq for FE { + type Output = False; +} +impl RunFileEq for FF { + type Output = False; +} +impl RunFileEq for FG { + type Output = False; +} +impl RunFileEq for FH { + type Output = True; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset.rs new file mode 100644 index 000000000..f5e03c530 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset.rs @@ -0,0 +1,111 @@ +use crate::values; +use std::marker::PhantomData; + +use self::{ + file::{NoFile, OffsetFile, RunOffsetFile, SomeFile}, + rank::{NoRank, OffsetRank, RunOffsetRank, SomeRank}, +}; +use super::{file::FileEn, rank::RankEn, Square, SquareTy}; + +pub mod file; +pub mod rank; + +pub(crate) trait Offset1DEn { + fn reify() -> i8; +} +pub(crate) struct Neg2; +pub(crate) struct Neg1; +pub(crate) struct Zero; +pub(crate) struct Pos1; +pub(crate) struct Pos2; + +impl Offset1DEn for Neg2 { + fn reify() -> i8 { + -2 + } +} +impl Offset1DEn for Neg1 { + fn reify() -> i8 { + -1 + } +} +impl Offset1DEn for Zero { + fn reify() -> i8 { + 0 + } +} +impl Offset1DEn for Pos1 { + fn reify() -> i8 { + 1 + } +} +impl Offset1DEn for Pos2 { + fn reify() -> i8 { + 2 + } +} + +pub(crate) trait OffsetTy { + fn reify() -> values::Offset; +} +pub(crate) struct Offset(PhantomData<(R, F)>); + +impl OffsetTy for Offset { + fn reify() -> values::Offset { + values::Offset { + rank: R::reify(), + file: F::reify(), + } + } +} + +pub(crate) trait MaybeSquare { + fn reify() -> Option; +} + +pub(crate) struct NoSquare; +pub(crate) struct SomeSquare(PhantomData); + +impl MaybeSquare for NoSquare { + fn reify() -> Option { + None + } +} +impl MaybeSquare for SomeSquare { + fn reify() -> Option { + Some(S::reify()) + } +} + +pub(crate) trait RunSquareFromMaybeTuple { + type Output: MaybeSquare; +} + +impl RunSquareFromMaybeTuple for (NoRank, NoFile) { + type Output = NoSquare; +} +impl RunSquareFromMaybeTuple for (SomeRank, NoFile) { + type Output = NoSquare; +} +impl RunSquareFromMaybeTuple for (NoRank, SomeFile) { + type Output = NoSquare; +} +impl RunSquareFromMaybeTuple for (SomeRank, SomeFile) { + type Output = SomeSquare>; +} + +pub(crate) trait RunOffsetSquare { + type Output: MaybeSquare; +} +pub(crate) type OffsetSquare = >::Output; + +impl RunOffsetSquare> for Square +where + RO: Offset1DEn, + FO: Offset1DEn, + R: RunOffsetRank, + F: RunOffsetFile, + (OffsetRank, OffsetFile): RunSquareFromMaybeTuple, +{ + type Output = <(OffsetRank, OffsetFile) as RunSquareFromMaybeTuple>::Output; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/file.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/file.rs new file mode 100644 index 000000000..d033044c4 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/file.rs @@ -0,0 +1,129 @@ +use super::{Neg1, Neg2, Offset1DEn, Pos1, Pos2, Zero}; +use crate::{ + board_rep::square::file::{self, FileEn}, + values, +}; +use std::marker::PhantomData; + +pub(crate) trait MaybeFile { + fn reify() -> Option; +} + +pub(crate) struct NoFile; +pub(crate) struct SomeFile(PhantomData); + +impl MaybeFile for NoFile { + fn reify() -> Option { + None + } +} +impl MaybeFile for SomeFile { + fn reify() -> Option { + Some(R::reify()) + } +} + +pub(crate) trait RunOffsetFile: FileEn { + type Output: MaybeFile; +} +pub(crate) type OffsetFile = >::Output; + +impl RunOffsetFile for file::FA { + type Output = NoFile; +} +impl RunOffsetFile for file::FB { + type Output = NoFile; +} +impl RunOffsetFile for file::FC { + type Output = SomeFile; +} +impl RunOffsetFile for file::FD { + type Output = SomeFile; +} +impl RunOffsetFile for file::FE { + type Output = SomeFile; +} +impl RunOffsetFile for file::FF { + type Output = SomeFile; +} +impl RunOffsetFile for file::FG { + type Output = SomeFile; +} +impl RunOffsetFile for file::FH { + type Output = SomeFile; +} +impl RunOffsetFile for file::FA { + type Output = NoFile; +} +impl RunOffsetFile for file::FB { + type Output = SomeFile; +} +impl RunOffsetFile for file::FC { + type Output = SomeFile; +} +impl RunOffsetFile for file::FD { + type Output = SomeFile; +} +impl RunOffsetFile for file::FE { + type Output = SomeFile; +} +impl RunOffsetFile for file::FF { + type Output = SomeFile; +} +impl RunOffsetFile for file::FG { + type Output = SomeFile; +} +impl RunOffsetFile for file::FH { + type Output = SomeFile; +} +impl RunOffsetFile for F { + type Output = SomeFile; +} +impl RunOffsetFile for file::FA { + type Output = SomeFile; +} +impl RunOffsetFile for file::FB { + type Output = SomeFile; +} +impl RunOffsetFile for file::FC { + type Output = SomeFile; +} +impl RunOffsetFile for file::FD { + type Output = SomeFile; +} +impl RunOffsetFile for file::FE { + type Output = SomeFile; +} +impl RunOffsetFile for file::FF { + type Output = SomeFile; +} +impl RunOffsetFile for file::FG { + type Output = SomeFile; +} +impl RunOffsetFile for file::FH { + type Output = NoFile; +} +impl RunOffsetFile for file::FA { + type Output = SomeFile; +} +impl RunOffsetFile for file::FB { + type Output = SomeFile; +} +impl RunOffsetFile for file::FC { + type Output = SomeFile; +} +impl RunOffsetFile for file::FD { + type Output = SomeFile; +} +impl RunOffsetFile for file::FE { + type Output = SomeFile; +} +impl RunOffsetFile for file::FF { + type Output = SomeFile; +} +impl RunOffsetFile for file::FG { + type Output = NoFile; +} +impl RunOffsetFile for file::FH { + type Output = NoFile; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/rank.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/rank.rs new file mode 100644 index 000000000..3bf8f0b59 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/offset/rank.rs @@ -0,0 +1,129 @@ +use super::{Neg1, Neg2, Offset1DEn, Pos1, Pos2, Zero}; +use crate::{ + board_rep::square::rank::{self, RankEn}, + values, +}; +use std::marker::PhantomData; + +pub(crate) trait MaybeRank { + fn reify() -> Option; +} + +pub(crate) struct NoRank; +pub(crate) struct SomeRank(PhantomData); + +impl MaybeRank for NoRank { + fn reify() -> Option { + None + } +} +impl MaybeRank for SomeRank { + fn reify() -> Option { + Some(R::reify()) + } +} + +pub(crate) trait RunOffsetRank: RankEn { + type Output: MaybeRank; +} +pub(crate) type OffsetRank = >::Output; + +impl RunOffsetRank for rank::R1 { + type Output = NoRank; +} +impl RunOffsetRank for rank::R2 { + type Output = NoRank; +} +impl RunOffsetRank for rank::R3 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R4 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R5 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R6 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R7 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R8 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R1 { + type Output = NoRank; +} +impl RunOffsetRank for rank::R2 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R3 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R4 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R5 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R6 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R7 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R8 { + type Output = SomeRank; +} +impl RunOffsetRank for R { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R1 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R2 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R3 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R4 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R5 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R6 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R7 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R8 { + type Output = NoRank; +} +impl RunOffsetRank for rank::R1 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R2 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R3 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R4 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R5 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R6 { + type Output = SomeRank; +} +impl RunOffsetRank for rank::R7 { + type Output = NoRank; +} +impl RunOffsetRank for rank::R8 { + type Output = NoRank; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/square/rank.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/rank.rs new file mode 100644 index 000000000..0de03f25f --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/rank.rs @@ -0,0 +1,258 @@ +use crate::{ + util::{Bool, False, True}, + values, +}; + +pub(crate) trait RankEn { + fn reify() -> values::Rank; +} +pub(crate) struct R1; +pub(crate) struct R2; +pub(crate) struct R3; +pub(crate) struct R4; +pub(crate) struct R5; +pub(crate) struct R6; +pub(crate) struct R7; +pub(crate) struct R8; + +impl RankEn for R1 { + fn reify() -> values::Rank { + values::Rank::R1 + } +} +impl RankEn for R2 { + fn reify() -> values::Rank { + values::Rank::R2 + } +} +impl RankEn for R3 { + fn reify() -> values::Rank { + values::Rank::R3 + } +} +impl RankEn for R4 { + fn reify() -> values::Rank { + values::Rank::R4 + } +} +impl RankEn for R5 { + fn reify() -> values::Rank { + values::Rank::R5 + } +} +impl RankEn for R6 { + fn reify() -> values::Rank { + values::Rank::R6 + } +} +impl RankEn for R7 { + fn reify() -> values::Rank { + values::Rank::R7 + } +} +impl RankEn for R8 { + fn reify() -> values::Rank { + values::Rank::R8 + } +} + +pub(crate) trait RunRankEq: RankEn { + type Output: Bool; +} +pub(crate) type RankEq = >::Output; + +// Its disturbing that I can't think of a better way to do this +// It seems like we would need some sort of negative impl or +// specificity. +impl RunRankEq for R1 { + type Output = True; +} +impl RunRankEq for R2 { + type Output = False; +} +impl RunRankEq for R3 { + type Output = False; +} +impl RunRankEq for R4 { + type Output = False; +} +impl RunRankEq for R5 { + type Output = False; +} +impl RunRankEq for R6 { + type Output = False; +} +impl RunRankEq for R7 { + type Output = False; +} +impl RunRankEq for R8 { + type Output = False; +} +impl RunRankEq for R1 { + type Output = False; +} +impl RunRankEq for R2 { + type Output = True; +} +impl RunRankEq for R3 { + type Output = False; +} +impl RunRankEq for R4 { + type Output = False; +} +impl RunRankEq for R5 { + type Output = False; +} +impl RunRankEq for R6 { + type Output = False; +} +impl RunRankEq for R7 { + type Output = False; +} +impl RunRankEq for R8 { + type Output = False; +} +impl RunRankEq for R1 { + type Output = False; +} +impl RunRankEq for R2 { + type Output = False; +} +impl RunRankEq for R3 { + type Output = True; +} +impl RunRankEq for R4 { + type Output = False; +} +impl RunRankEq for R5 { + type Output = False; +} +impl RunRankEq for R6 { + type Output = False; +} +impl RunRankEq for R7 { + type Output = False; +} +impl RunRankEq for R8 { + type Output = False; +} +impl RunRankEq for R1 { + type Output = False; +} +impl RunRankEq for R2 { + type Output = False; +} +impl RunRankEq for R3 { + type Output = False; +} +impl RunRankEq for R4 { + type Output = True; +} +impl RunRankEq for R5 { + type Output = False; +} +impl RunRankEq for R6 { + type Output = False; +} +impl RunRankEq for R7 { + type Output = False; +} +impl RunRankEq for R8 { + type Output = False; +} +impl RunRankEq for R1 { + type Output = False; +} +impl RunRankEq for R2 { + type Output = False; +} +impl RunRankEq for R3 { + type Output = False; +} +impl RunRankEq for R4 { + type Output = False; +} +impl RunRankEq for R5 { + type Output = True; +} +impl RunRankEq for R6 { + type Output = False; +} +impl RunRankEq for R7 { + type Output = False; +} +impl RunRankEq for R8 { + type Output = False; +} +impl RunRankEq for R1 { + type Output = False; +} +impl RunRankEq for R2 { + type Output = False; +} +impl RunRankEq for R3 { + type Output = False; +} +impl RunRankEq for R4 { + type Output = False; +} +impl RunRankEq for R5 { + type Output = False; +} +impl RunRankEq for R6 { + type Output = True; +} +impl RunRankEq for R7 { + type Output = False; +} +impl RunRankEq for R8 { + type Output = False; +} +impl RunRankEq for R1 { + type Output = False; +} +impl RunRankEq for R2 { + type Output = False; +} +impl RunRankEq for R3 { + type Output = False; +} +impl RunRankEq for R4 { + type Output = False; +} +impl RunRankEq for R5 { + type Output = False; +} +impl RunRankEq for R6 { + type Output = False; +} +impl RunRankEq for R7 { + type Output = True; +} +impl RunRankEq for R8 { + type Output = False; +} +impl RunRankEq for R1 { + type Output = False; +} +impl RunRankEq for R2 { + type Output = False; +} +impl RunRankEq for R3 { + type Output = False; +} +impl RunRankEq for R4 { + type Output = False; +} +impl RunRankEq for R5 { + type Output = False; +} +impl RunRankEq for R6 { + type Output = False; +} +impl RunRankEq for R7 { + type Output = False; +} +impl RunRankEq for R8 { + type Output = True; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/board_rep/square/set.rs b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/set.rs new file mode 100644 index 000000000..b1cca2629 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/board_rep/square/set.rs @@ -0,0 +1,454 @@ +use super::{ + file::{self, FileEn}, + rank::{self, RankEn}, + Square, SquareTy, +}; +use crate::{ + move_gen::list::{SLCons, SLNil, SquareListTy}, + util::{Bool, False, True}, + values, +}; +use std::marker::PhantomData; + +pub(crate) trait SquareSetTy { + fn reify() -> values::SquareSet; +} +pub(crate) struct SquareSet< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, +>(PhantomData<(R1, R2, R3, R4, R5, R6, R7, R8)>); + +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > SquareSetTy for SquareSet +{ + fn reify() -> values::SquareSet { + values::SquareSet { + r1: R1::reify(), + r2: R2::reify(), + r3: R3::reify(), + r4: R4::reify(), + r5: R5::reify(), + r6: R6::reify(), + r7: R7::reify(), + r8: R8::reify(), + } + } +} + +pub(crate) trait SquareSetRankTy { + fn reify() -> values::SquareSetRank; +} +pub(crate) struct SquareSetRank< + A: Bool, + B: Bool, + C: Bool, + D: Bool, + E: Bool, + F: Bool, + G: Bool, + H: Bool, +>(PhantomData<(A, B, C, D, E, F, G, H)>); + +impl SquareSetRankTy + for SquareSetRank +{ + fn reify() -> values::SquareSetRank { + values::SquareSetRank { + a: A::reify(), + b: B::reify(), + c: C::reify(), + d: D::reify(), + e: E::reify(), + f: F::reify(), + g: G::reify(), + h: H::reify(), + } + } +} + +pub(crate) trait RunIdxSSRank: SquareSetRankTy { + type Output: Bool; +} +pub(crate) type IdxSSRank = >::Output; +impl RunIdxSSRank + for SquareSetRank +{ + type Output = A; +} +impl RunIdxSSRank + for SquareSetRank +{ + type Output = B; +} +impl RunIdxSSRank + for SquareSetRank +{ + type Output = C; +} +impl RunIdxSSRank + for SquareSetRank +{ + type Output = D; +} +impl RunIdxSSRank + for SquareSetRank +{ + type Output = E; +} +impl RunIdxSSRank + for SquareSetRank +{ + type Output = F; +} +impl RunIdxSSRank + for SquareSetRank +{ + type Output = G; +} +impl RunIdxSSRank + for SquareSetRank +{ + type Output = H; +} + +pub(crate) trait RunIdxSSForRank: SquareSetTy { + type Output: SquareSetRankTy; +} +pub(crate) type IdxSSforRank = >::Output; +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R1; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R2; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R3; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R4; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R5; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R6; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R7; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + > RunIdxSSForRank for SquareSet +{ + type Output = R8; +} + +pub(crate) trait RunIsOccupied: SquareSetTy { + type Output: Bool; +} +pub(crate) type IsOccupied = >::Output; + +impl RunIsOccupied> for SS +where + SS: RunIdxSSForRank, + IdxSSforRank: RunIdxSSRank, +{ + type Output = IdxSSRank, F>; +} + +pub(crate) type EmptySSRank = SquareSetRank; +pub(crate) type EmptySS = SquareSet< + EmptySSRank, + EmptySSRank, + EmptySSRank, + EmptySSRank, + EmptySSRank, + EmptySSRank, + EmptySSRank, + EmptySSRank, +>; + +pub(crate) trait RunAddToSSRank: SquareSetRankTy { + type Output: SquareSetRankTy; +} +pub(crate) type AddToSSRank = >::Output; +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} +impl + RunAddToSSRank for SquareSetRank +{ + type Output = SquareSetRank; +} + +pub(crate) trait RunAddToSS: SquareSetTy { + type Output: SquareSetTy; +} +pub(crate) type AddToSS = >::Output; +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R1: RunAddToSSRank, +{ + type Output = SquareSet, R2, R3, R4, R5, R6, R7, R8>; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R2: RunAddToSSRank, +{ + type Output = SquareSet, R3, R4, R5, R6, R7, R8>; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R3: RunAddToSSRank, +{ + type Output = SquareSet, R4, R5, R6, R7, R8>; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R4: RunAddToSSRank, +{ + type Output = SquareSet, R5, R6, R7, R8>; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R5: RunAddToSSRank, +{ + type Output = SquareSet, R6, R7, R8>; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R6: RunAddToSSRank, +{ + type Output = SquareSet, R7, R8>; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R7: RunAddToSSRank, +{ + type Output = SquareSet, R8>; +} +impl< + R1: SquareSetRankTy, + R2: SquareSetRankTy, + R3: SquareSetRankTy, + R4: SquareSetRankTy, + R5: SquareSetRankTy, + R6: SquareSetRankTy, + R7: SquareSetRankTy, + R8: SquareSetRankTy, + F: FileEn, + > RunAddToSS> for SquareSet +where + R8: RunAddToSSRank, +{ + type Output = SquareSet>; +} + +pub(crate) trait RunAddToSSFromL: SquareSetTy { + type Output: SquareSetTy; +} +pub(crate) type AddToSSFromL = >::Output; + +impl RunAddToSSFromL for SS { + type Output = SS; +} +impl RunAddToSSFromL> for SS +where + SS: RunAddToSS, + AddToSS: RunAddToSSFromL, +{ + type Output = AddToSSFromL, Next>; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/main.rs b/collector/compile-benchmarks/type-system-chess/src/main.rs new file mode 100644 index 000000000..ad187e563 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/main.rs @@ -0,0 +1,31 @@ +#![allow(clippy::type_complexity)] +#![recursion_limit = "256"] + +mod board_rep; +mod move_gen; +mod state; +mod ui; +mod util; +mod values; + +fn main() { + use ui::prelude::*; + use move_gen::{moves::Moves, list::MoveListTy}; + use state::EmptyCa; + + type B = Board< + // AA BB CC DD EE FF GG HH + BoardRank<__, __, __, __, __, __, __, __>, // 1 + BoardRank<__, __, __, __, WP, __, WP, __>, // 2 + BoardRank<__, __, __, __, __, __, __, __>, // 3 + BoardRank<__, WR, __, __, __, BP, __, BK>, // 4 + BoardRank, // 5 + BoardRank<__, __, __, BP, __, __, __, __>, // 6 + BoardRank<__, __, BP, __, __, __, __, __>, // 7 + BoardRank<__, __, __, __, __, __, __, __>, // 8 + >; + type S = State; + type Ms = Moves; + + assert_eq!(Ms::reify().0.len(), 14); +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen.rs new file mode 100644 index 000000000..26d85cf56 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen.rs @@ -0,0 +1,76 @@ +use crate::{ + board_rep::{ + piece::ColoredPieceTy, + square::{ + offset::{MaybeSquare, NoSquare}, + SquareTy, + }, + }, + values, +}; +use std::marker::PhantomData; + +pub mod attacked; +pub mod bishop; +pub mod cast; +pub mod castle; +pub mod check; +pub mod king; +pub mod knight; +pub mod list; +pub mod ml_from_sl; +pub mod moves; +pub mod pawn; +pub mod pmoves; +pub mod queen; +pub mod rook; + +pub(crate) trait MoveTy { + fn reify() -> values::Move; +} +pub(crate) struct Move< + From: SquareTy, + To: SquareTy, + P: ColoredPieceTy, + EP: MaybeSquare = NoSquare, + RFrom: MaybeSquare = NoSquare, + RTo: MaybeSquare = NoSquare, +>(PhantomData<(From, To, P, EP, RFrom, RTo)>); + +pub(crate) trait MaybeMove { + fn reify() -> Option; +} +pub(crate) struct NoMove; +pub(crate) struct SomeMove(PhantomData); + +impl MaybeMove for NoMove { + fn reify() -> Option { + None + } +} +impl MaybeMove for SomeMove { + fn reify() -> Option { + Some(M::reify()) + } +} + +impl< + From: SquareTy, + To: SquareTy, + P: ColoredPieceTy, + EP: MaybeSquare, + RFrom: MaybeSquare, + RTo: MaybeSquare, + > MoveTy for Move +{ + fn reify() -> values::Move { + values::Move { + from: From::reify(), + to: To::reify(), + piece: P::reify(), + ep: EP::reify(), + rook_from: RFrom::reify(), + rook_to: RTo::reify(), + } + } +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/attacked.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/attacked.rs new file mode 100644 index 000000000..34f7ac695 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/attacked.rs @@ -0,0 +1,117 @@ +use super::{ + bishop::{BishopMoveSqs, RunBishopMoveSqs}, + king::{KingMoveSqs, RunKingMoveSqs}, + knight::{KnightMoveSqs, RunKnightMoveSqs}, + list::{SLCons, SLNil, SquareListTy}, + pawn::{PawnAttackSqs, RunPawnAttackSqs}, + queen::{QueenMoveSqs, RunQueenMoveSqs}, + rook::{RookMoveSqs, RunRookMoveSqs}, +}; +use crate::board_rep::{ + board::{ + idx::{IdxBoard, RunIdxBoard}, + BoardTy, CellEn, Empty, Filled, + }, + color::{Black, ColorEn, White}, + piece::{Bishop, ColoredPiece, King, Knight, Pawn, PieceEn, Queen, Rook}, + square::{ + set::{AddToSSFromL, EmptySS, RunAddToSSFromL, SquareSetTy}, + AllSqs, SquareTy, + }, +}; + +pub(crate) trait RunAttacked: BoardTy { + type Output: SquareSetTy; +} +pub(crate) type Attacked = >::Output; + +impl RunAttacked for B +where + B: RunAttacksForSqL, +{ + type Output = >::Output; +} + +pub(crate) trait RunAttacksForSqL: BoardTy { + type Output: SquareSetTy; +} +pub(crate) type AttacksForSqL = >::Output; + +impl RunAttacksForSqL for B { + type Output = EmptySS; +} +impl + RunAttacksForSqL> for B +where + B: RunAttacksForSqL, + S: RunAttacksForSq, + AttacksForSqL: RunAddToSSFromL>, +{ + type Output = AddToSSFromL, AttacksForSq>; +} + +// This squares that contain pieces of the mover's color but it doesn't +// matter because we only use this to check for checks. +pub(crate) trait RunAttacksForSq: SquareTy { + type Output: SquareListTy; +} +pub(crate) type AttacksForSq = >::Output; + +impl RunAttacksForSq for S +where + B: RunIdxBoard, + S: RunAttacksForTypeAtSq>, +{ + type Output = >>::Output; +} + +pub(crate) trait RunAttacksForTypeAtSq: + SquareTy +{ + type Output: SquareListTy; +} + +impl RunAttacksForTypeAtSq for S { + type Output = SLNil; +} +impl + RunAttacksForTypeAtSq>> for S +{ + type Output = SLNil; +} +impl + RunAttacksForTypeAtSq>> for S +{ + type Output = SLNil; +} + +impl, B: BoardTy, C: ColorEn> + RunAttacksForTypeAtSq>> for S +{ + type Output = PawnAttackSqs; +} +impl + RunAttacksForTypeAtSq>> for S +{ + type Output = KnightMoveSqs; +} +impl, B: BoardTy, C: ColorEn> + RunAttacksForTypeAtSq>> for S +{ + type Output = BishopMoveSqs; +} +impl, B: BoardTy, C: ColorEn> + RunAttacksForTypeAtSq>> for S +{ + type Output = RookMoveSqs; +} +impl, B: BoardTy, C: ColorEn> + RunAttacksForTypeAtSq>> for S +{ + type Output = QueenMoveSqs; +} +impl + RunAttacksForTypeAtSq>> for S +{ + type Output = KingMoveSqs; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/bishop.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/bishop.rs new file mode 100644 index 000000000..cd1dc75bd --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/bishop.rs @@ -0,0 +1,36 @@ +use super::{ + cast::{AddCastSqs, RunAddCastSqs}, + list::{SLNil, SquareListTy}, +}; +use crate::board_rep::{ + board::BoardTy, + square::{ + offset::{Neg1, Offset, Pos1}, + SquareTy, + }, +}; + +pub(crate) trait RunBishopMoveSqs: SquareTy { + type Output: SquareListTy; +} +pub(crate) type BishopMoveSqs = >::Output; + +type O1 = Offset; +type O2 = Offset; +type O3 = Offset; +type O4 = Offset; + +type L1 = AddCastSqs; +type L2 = AddCastSqs, B, S, O2>; +type L3 = AddCastSqs, B, S, O3>; +type L4 = AddCastSqs, B, S, O4>; + +impl RunBishopMoveSqs for S +where + SLNil: RunAddCastSqs, + L1: RunAddCastSqs, + L2: RunAddCastSqs, + L3: RunAddCastSqs, +{ + type Output = L4; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/cast.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/cast.rs new file mode 100644 index 000000000..ddd996382 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/cast.rs @@ -0,0 +1,67 @@ +use super::list::{SLCons, SquareListTy}; +use crate::board_rep::{ + board::{ + idx::{IdxBoard, RunIdxBoard}, + BoardTy, CellEn, Empty, Filled, + }, + color::ColorEn, + piece::{ColoredPiece, PieceEn}, + square::{ + offset::{MaybeSquare, NoSquare, OffsetSquare, OffsetTy, RunOffsetSquare, SomeSquare}, + SquareTy, + }, +}; + +pub(crate) trait RunAddCastSqs: + SquareListTy +{ + type Output: SquareListTy; +} +pub(crate) type AddCastSqs = >::Output; + +impl RunAddCastSqs for L +where + S: RunOffsetSquare, + L: RunCastMaybeSq, O>, +{ + // We offset the square to start because the initial square is on top of the moving piece. + type Output = , O>>::Output; +} + +pub(crate) trait RunCastMaybeSq: + SquareListTy +{ + type Output: SquareListTy; +} + +impl RunCastMaybeSq for L { + type Output = L; +} +impl RunCastMaybeSq, O> + for L +where + B: RunIdxBoard, + L: RunCastSqWithContent>, +{ + type Output = >>::Output; +} + +pub(crate) trait RunCastSqWithContent: + SquareListTy +{ + type Output: SquareListTy; +} + +impl + RunCastSqWithContent>> for L +{ + type Output = SLCons; +} +impl RunCastSqWithContent + for L +where + S: RunOffsetSquare, + L: RunCastMaybeSq, O>, +{ + type Output = SLCons, O>>::Output>; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/castle.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/castle.rs new file mode 100644 index 000000000..015b76bef --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/castle.rs @@ -0,0 +1,206 @@ +use super::{ + attacked::{Attacked, RunAttacked}, + MaybeMove, Move, MoveTy, NoMove, SomeMove, +}; +use crate::{ + board_rep::{ + board::{ + idx::{IdxBoard, RunIdxBoard}, + BoardTy, CellEn, Empty, Filled, + }, + color::{Black, ColorEn, White}, + piece::{ColoredPiece, ColoredPieceTy, King}, + square::{ + file, + offset::{NoSquare, SomeSquare}, + rank::{self, RankEn}, + set::{IsOccupied, RunIsOccupied, SquareSetTy}, + Square, SquareTy, + }, + }, + util::{And, Bool, False, Not, Or, RunAnd, RunNot, RunOr, True}, +}; + +pub(crate) trait RunIsEmpty: BoardTy { + type Output: Bool; +} +pub(crate) type IsEmpty = >::Output; + +impl RunIsEmpty for B +where + B: RunIdxBoard, + IdxBoard: RunCellIsEmpty, +{ + type Output = as RunCellIsEmpty>::Output; +} + +pub(crate) trait RunCellIsEmpty: CellEn { + type Output: Bool; +} +impl RunCellIsEmpty for Empty { + type Output = True; +} +impl RunCellIsEmpty for Filled { + type Output = False; +} + +pub(crate) type SA = Square; +pub(crate) type SB = Square; +pub(crate) type SC = Square; +pub(crate) type SD = Square; +pub(crate) type SE = Square; +pub(crate) type SF = Square; +pub(crate) type SG = Square; +pub(crate) type SH = Square; + +pub(crate) trait RunMoveIfPasses: MoveTy { + type Output: MaybeMove; +} +pub(crate) type MoveIfPasses = >::Output; + +impl RunMoveIfPasses for M { + type Output = SomeMove; +} +impl RunMoveIfPasses for M { + type Output = NoMove; +} + +pub(crate) trait RunKingsideCastle: BoardTy { + type Output: MaybeMove; +} +pub(crate) type KingsideCastle = + >::Output; + +impl RunKingsideCastle for B { + type Output = NoMove; +} +impl RunKingsideCastle for B { + type Output = NoMove; +} +impl RunKingsideCastle for B +where + B: RunAttacked, + B: RunKingsideCastleWX>, +{ + type Output = KingsideCastleWX>; +} +impl RunKingsideCastle for B +where + B: RunAttacked, + B: RunKingsideCastleWX>, +{ + type Output = KingsideCastleWX>; +} + +pub(crate) trait RunKingsideCastleWX: + BoardTy +{ + type Output: MaybeMove; +} +pub(crate) type KingsideCastleWX = + >::Output; + +impl RunKingsideCastleWX for B +where + B: RunIsEmpty> + RunIsEmpty>, + A: RunIsOccupied> + RunIsOccupied> + RunIsOccupied>, + IsEmpty>: RunAnd>>, + IsOccupied>: RunOr>>, + Or>, IsOccupied>>: RunOr>>, + Or>, IsOccupied>>, IsOccupied>>: RunNot, + And>, IsEmpty>>: + RunAnd>, IsOccupied>>, IsOccupied>>>>, + Move, SG, ColoredPiece, NoSquare, SomeSquare>, SomeSquare>>: + RunMoveIfPasses< + And< + And>, IsEmpty>>, + Not>, IsOccupied>>, IsOccupied>>>, + >, + >, +{ + type Output = MoveIfPasses< + Move< + SE, + SG, + ColoredPiece, + NoSquare, + SomeSquare>, + SomeSquare>, + >, + And< + And>, IsEmpty>>, + Not>, IsOccupied>>, IsOccupied>>>, + >, + >; +} + +pub(crate) trait RunQueensideCastle: BoardTy { + type Output: MaybeMove; +} +pub(crate) type QueensideCastle = + >::Output; + +impl RunQueensideCastle for B { + type Output = NoMove; +} +impl RunQueensideCastle for B { + type Output = NoMove; +} +impl RunQueensideCastle for B +where + B: RunAttacked, + B: RunQueensideCastleWX>, +{ + type Output = QueensideCastleWX>; +} +impl RunQueensideCastle for B +where + B: RunAttacked, + B: RunQueensideCastleWX>, +{ + type Output = QueensideCastleWX>; +} + +pub(crate) trait RunQueensideCastleWX: + BoardTy +{ + type Output: MaybeMove; +} +pub(crate) type QueensideCastleWX = + >::Output; + +impl RunQueensideCastleWX + for B +where + B: RunIsEmpty> + RunIsEmpty> + RunIsEmpty>, + A: RunIsOccupied> + RunIsOccupied> + RunIsOccupied>, + IsEmpty>: RunAnd>>, + And>, IsEmpty>>: RunAnd>>, + IsOccupied>: RunOr>>, + Or>, IsOccupied>>: RunOr>>, + Or>, IsOccupied>>, IsOccupied>>: RunNot, + And>, IsEmpty>>, IsEmpty>>: + RunAnd>, IsOccupied>>, IsOccupied>>>>, + Move, SC, ColoredPiece, NoSquare, SomeSquare>, SomeSquare>>: + RunMoveIfPasses< + And< + And>, IsEmpty>>, IsEmpty>>, + Not>, IsOccupied>>, IsOccupied>>>, + >, + >, +{ + type Output = MoveIfPasses< + Move< + SE, + SC, + ColoredPiece, + NoSquare, + SomeSquare>, + SomeSquare>, + >, + And< + And>, IsEmpty>>, IsEmpty>>, + Not>, IsOccupied>>, IsOccupied>>>, + >, + >; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/check.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/check.rs new file mode 100644 index 000000000..8d3d77f70 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/check.rs @@ -0,0 +1,125 @@ +use super::{ + attacked::{Attacked, RunAttacked}, + list::{SLCons, SLNil, SquareListTy}, +}; +use crate::{ + board_rep::{ + board::{ + idx::{IdxBoard, RunIdxBoard}, + BoardTy, CellEn, Empty, Filled, + }, + color::{Black, ColorEn, White}, + piece::{Bishop, ColoredPiece, King, Knight, Pawn, PieceEn, Queen, Rook}, + square::{ + offset::MaybeSquare, + set::{IsOccupied, RunIsOccupied, SquareSetTy}, + AllSqs, SquareTy, + }, + }, + state::{CastleStateTy, State, StateTy}, + util::{Bool, False, Or, RunOr, True}, +}; + +pub(crate) trait RunIsCheck: StateTy { + type Output: Bool; +} +pub(crate) type IsCheck = >::Output; + +impl RunIsCheck + for State +where + B: RunAttacked, + B: RunSqLIsCheck, MoverC, AllSqs>, +{ + type Output = , MoverC, AllSqs>>::Output; +} + +pub(crate) trait RunSqLIsCheck: + BoardTy +{ + type Output: Bool; +} +pub(crate) type SqLIsCheck = >::Output; + +impl RunSqLIsCheck + for B +{ + type Output = False; +} +impl + RunSqLIsCheck> for B +where + B: RunSqIsCheck, + B: RunSqLIsCheck, + SqIsCheck: RunOr>, +{ + type Output = Or, SqLIsCheck>; +} + +pub(crate) trait RunSqIsCheck: + BoardTy +{ + type Output: Bool; +} +pub(crate) type SqIsCheck = >::Output; + +impl RunSqIsCheck for B +where + B: RunIdxBoard, + A: RunIsOccupied, + (): RunSqIsCheckWC, IdxBoard>, +{ + type Output = <() as RunSqIsCheckWC, IdxBoard>>::Output; +} + +pub(crate) trait RunSqIsCheckWC { + type Output: Bool; +} + +impl RunSqIsCheckWC for () { + type Output = False; +} +impl RunSqIsCheckWC for () { + type Output = False; +} +impl RunSqIsCheckWC>> + for () +{ + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = True; +} +impl RunSqIsCheckWC>> for () { + type Output = True; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} +impl RunSqIsCheckWC>> for () { + type Output = False; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/king.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/king.rs new file mode 100644 index 000000000..99535c331 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/king.rs @@ -0,0 +1,51 @@ +use super::list::{AppendMaybeSquare, RunAppendMaybeSquare, SLNil, SquareListTy}; +use crate::board_rep::square::{ + offset::{Neg1, Offset, OffsetSquare, Pos1, RunOffsetSquare, Zero}, + SquareTy, +}; + +pub(crate) trait RunKingMoveSqs: SquareTy { + type Output: SquareListTy; +} +pub(crate) type KingMoveSqs = ::Output; + +type O1 = Offset; +type O2 = Offset; +type O3 = Offset; +type O4 = Offset; +type O5 = Offset; +type O6 = Offset; +type O7 = Offset; +type O8 = Offset; + +type L1 = AppendMaybeSquare>; +type L2 = AppendMaybeSquare, OffsetSquare>; +type L3 = AppendMaybeSquare, OffsetSquare>; +type L4 = AppendMaybeSquare, OffsetSquare>; +type L5 = AppendMaybeSquare, OffsetSquare>; +type L6 = AppendMaybeSquare, OffsetSquare>; +type L7 = AppendMaybeSquare, OffsetSquare>; +type L8 = AppendMaybeSquare, OffsetSquare>; + +impl RunKingMoveSqs for S +where + S: SquareTy + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare, + SLNil: RunAppendMaybeSquare>, + L1: RunAppendMaybeSquare>, + L2: RunAppendMaybeSquare>, + L3: RunAppendMaybeSquare>, + L4: RunAppendMaybeSquare>, + L5: RunAppendMaybeSquare>, + L6: RunAppendMaybeSquare>, + L7: RunAppendMaybeSquare>, +{ + type Output = L8; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/knight.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/knight.rs new file mode 100644 index 000000000..1e7ced19f --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/knight.rs @@ -0,0 +1,51 @@ +use super::list::{AppendMaybeSquare, RunAppendMaybeSquare, SLNil, SquareListTy}; +use crate::board_rep::square::{ + offset::{Neg1, Neg2, Offset, OffsetSquare, Pos1, Pos2, RunOffsetSquare}, + SquareTy, +}; + +pub(crate) trait RunKnightMoveSqs: SquareTy { + type Output: SquareListTy; +} +pub(crate) type KnightMoveSqs = ::Output; + +type O1 = Offset; +type O2 = Offset; +type O3 = Offset; +type O4 = Offset; +type O5 = Offset; +type O6 = Offset; +type O7 = Offset; +type O8 = Offset; + +type L1 = AppendMaybeSquare>; +type L2 = AppendMaybeSquare, OffsetSquare>; +type L3 = AppendMaybeSquare, OffsetSquare>; +type L4 = AppendMaybeSquare, OffsetSquare>; +type L5 = AppendMaybeSquare, OffsetSquare>; +type L6 = AppendMaybeSquare, OffsetSquare>; +type L7 = AppendMaybeSquare, OffsetSquare>; +type L8 = AppendMaybeSquare, OffsetSquare>; + +impl RunKnightMoveSqs for S +where + S: SquareTy + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare + + RunOffsetSquare, + SLNil: RunAppendMaybeSquare>, + L1: RunAppendMaybeSquare>, + L2: RunAppendMaybeSquare>, + L3: RunAppendMaybeSquare>, + L4: RunAppendMaybeSquare>, + L5: RunAppendMaybeSquare>, + L6: RunAppendMaybeSquare>, + L7: RunAppendMaybeSquare>, +{ + type Output = L8; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/list.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/list.rs new file mode 100644 index 000000000..7bbec4f5f --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/list.rs @@ -0,0 +1,71 @@ +use super::{MaybeMove, MoveTy, NoMove, SomeMove}; +use crate::{ + board_rep::square::{ + offset::{MaybeSquare, NoSquare, SomeSquare}, + SquareTy, + }, + values, +}; +use std::marker::PhantomData; + +pub(crate) trait MoveListTy { + fn reify() -> values::MoveList; +} +pub(crate) struct MLNil; +pub(crate) struct MLCons(PhantomData<(M, L)>); + +impl MoveListTy for MLNil { + fn reify() -> values::MoveList { + values::MoveList(Vec::new()) + } +} +impl MoveListTy for MLCons { + fn reify() -> values::MoveList { + let values::MoveList(mut tail) = L::reify(); + tail.insert(0, M::reify()); + values::MoveList(tail) + } +} + +pub(crate) trait RunAppendMaybeMove: MoveListTy { + type Output: MoveListTy; +} +pub(crate) type AppendMaybeMove = >::Output; + +impl RunAppendMaybeMove for L { + type Output = L; +} +impl RunAppendMaybeMove> for L { + type Output = MLCons; +} + +pub(crate) trait SquareListTy { + fn reify() -> values::SquareList; +} +pub(crate) struct SLNil; +pub(crate) struct SLCons(PhantomData<(S, L)>); + +impl SquareListTy for SLNil { + fn reify() -> values::SquareList { + values::SquareList(Vec::new()) + } +} +impl SquareListTy for SLCons { + fn reify() -> values::SquareList { + let values::SquareList(mut tail) = L::reify(); + tail.insert(0, S::reify()); + values::SquareList(tail) + } +} + +pub(crate) trait RunAppendMaybeSquare: SquareListTy { + type Output: SquareListTy; +} +pub(crate) type AppendMaybeSquare = >::Output; + +impl RunAppendMaybeSquare for L { + type Output = L; +} +impl RunAppendMaybeSquare> for L { + type Output = SLCons; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/ml_from_sl.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/ml_from_sl.rs new file mode 100644 index 000000000..54d1f3a5c --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/ml_from_sl.rs @@ -0,0 +1,109 @@ +use super::{ + list::{AppendMaybeMove, MoveListTy, RunAppendMaybeMove, SLCons, SLNil, SquareListTy}, + MaybeMove, Move, NoMove, SomeMove, +}; +use crate::board_rep::{ + board::{ + idx::{IdxBoard, IdxBoardRank, RunIdxBoardRank, RunIdxRank}, + BoardTy, CellEn, Empty, Filled, + }, + color::{Black, ColorEn, White}, + piece::{ColoredPiece, PieceEn}, + square::{file::FileEn, rank::RankEn, Square, SquareTy}, +}; + +pub(crate) trait RunPMoveLFromSqs< + B: BoardTy, + Start: SquareTy, + MoverC: ColorEn, + MoverP: PieceEn, + ML: MoveListTy, +>: SquareListTy +{ + type Output: MoveListTy; +} +pub(crate) type PMoveLFromSqs = + >::Output; + +impl + RunPMoveLFromSqs for SLNil +{ + type Output = ML; +} +impl< + Rest: SquareListTy, + End: SquareTy, + B: BoardTy, + Start: SquareTy, + MoverC: ColorEn, + MoverP: PieceEn, + ML: MoveListTy, + > RunPMoveLFromSqs for SLCons +where + End: RunPMoveFromSq, + Rest: RunPMoveLFromSqs, + PMoveLFromSqs: + RunAppendMaybeMove<>::Output>, +{ + type Output = AppendMaybeMove< + PMoveLFromSqs, + >::Output, + >; +} + +pub(crate) trait RunPMoveFromSq: + SquareTy +{ + type Output: MaybeMove; +} + +impl + RunPMoveFromSq for Square +where + B: RunIdxBoardRank, + IdxBoardRank: RunIdxRank, + Square: RunPMoveFromSqContents>>, +{ + type Output = as RunPMoveFromSqContents< + Start, + MoverC, + MoverP, + IdxBoard>, + >>::Output; +} + +pub(crate) trait RunPMoveFromSqContents< + Start: SquareTy, + MoverC: ColorEn, + MoverP: PieceEn, + Contents: CellEn, +>: SquareTy +{ + type Output: MaybeMove; +} + +impl + RunPMoveFromSqContents for End +{ + type Output = SomeMove>>; +} +impl + RunPMoveFromSqContents>> for End +{ + type Output = NoMove; +} +impl + RunPMoveFromSqContents>> for End +{ + type Output = NoMove; +} +impl + RunPMoveFromSqContents>> for End +{ + type Output = SomeMove>>; +} +impl + RunPMoveFromSqContents>> for End +{ + type Output = SomeMove>>; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/moves.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/moves.rs new file mode 100644 index 000000000..26ba93175 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/moves.rs @@ -0,0 +1,89 @@ +use crate::{ + board_rep::{ + board::BoardTy, + color::{Black, ColorEn, White}, + square::offset::MaybeSquare, + }, + state::{CastleState, MakeMove, RunMakeMove, State, StateTy}, + util::{Bool, False, True}, +}; + +use super::{ + check::{IsCheck, RunIsCheck}, + list::{AppendMaybeMove, MLCons, MLNil, MoveListTy, RunAppendMaybeMove}, + pmoves::{PMoves, RunPMoves}, + MaybeMove, MoveTy, NoMove, SomeMove, +}; + +pub(crate) trait RunMoves: StateTy { + type Output: MoveListTy; +} +pub(crate) type Moves = ::Output; + +impl RunMoves + for State> +where + B: RunPMoves, + PMoves: + RunMsFromPMs>, MoverC>, +{ + type Output = MsFromPMs< + PMoves, + MLNil, + State>, + MoverC, + >; +} + +pub(crate) trait RunMsFromPMs: + MoveListTy +{ + type Output: MoveListTy; +} +pub(crate) type MsFromPMs = >::Output; + +impl RunMsFromPMs for MLNil { + type Output = ML; +} +impl RunMsFromPMs + for MLCons +where + M: RunMakeMove, + MakeMove: RunIsCheck, + M: RunMMFromIsCheck, Black>>, + Next: RunMsFromPMs, + MsFromPMs: + RunAppendMaybeMove, Black>>>, +{ + type Output = AppendMaybeMove< + MsFromPMs, + MMFromIsCheck, Black>>, + >; +} +impl RunMsFromPMs + for MLCons +where + M: RunMakeMove, + MakeMove: RunIsCheck, + M: RunMMFromIsCheck, White>>, + Next: RunMsFromPMs, + MsFromPMs: + RunAppendMaybeMove, White>>>, +{ + type Output = AppendMaybeMove< + MsFromPMs, + MMFromIsCheck, White>>, + >; +} + +pub(crate) trait RunMMFromIsCheck: MoveTy { + type Output: MaybeMove; +} +pub(crate) type MMFromIsCheck = >::Output; + +impl RunMMFromIsCheck for M { + type Output = NoMove; +} +impl RunMMFromIsCheck for M { + type Output = SomeMove; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/pawn.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/pawn.rs new file mode 100644 index 000000000..d951e62ff --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/pawn.rs @@ -0,0 +1,433 @@ +use super::{ + list::{ + AppendMaybeMove, AppendMaybeSquare, MLCons, MoveListTy, RunAppendMaybeMove, + RunAppendMaybeSquare, SLCons, SLNil, SquareListTy, + }, + MaybeMove, Move, NoMove, SomeMove, +}; +use crate::{ + board_rep::{ + board::{ + idx::{IdxBoard, RunIdxBoard}, + BoardTy, CellEn, Empty, Filled, + }, + color::{Black, ColorEn, White}, + piece::{Bishop, ColoredPiece, ColoredPieceTy, Knight, Pawn, PieceEn, Queen, Rook}, + square::{ + file::FileEn, + offset::{ + MaybeSquare, Neg1, NoSquare, Offset, Offset1DEn, OffsetSquare, OffsetTy, Pos1, + RunOffsetSquare, SomeSquare, Zero, + }, + rank::{self, RankEn}, + RunSquareEq, Square, SquareEq, SquareTy, + }, + }, + util::{Bool, False, True}, +}; + +pub(crate) trait RunForwardO: ColorEn { + type Output: OffsetTy; +} +pub(crate) type ForwardO = >::Output; +impl RunForwardO for White { + type Output = Offset; +} +impl RunForwardO for Black { + type Output = Offset; +} + +pub(crate) trait RunPawnMoves: + SquareTy +{ + type Output: MoveListTy; +} +pub(crate) type PawnMoves = >::Output; + +impl + RunPawnMoves for S +where + S: RunPawnMoveSqs, + S: RunMakeEPMove + RunMakeEPMove, + PawnMoveSqs: RunPawnMlFromSL, + PawnMLFromSl, B, S, MoverC, ML>: + RunAppendMaybeMove>, + AppendMaybeMove< + PawnMLFromSl, B, S, MoverC, ML>, + MakeEPMove, + >: RunAppendMaybeMove>, +{ + type Output = AppendMaybeMove< + AppendMaybeMove< + PawnMLFromSl, B, S, MoverC, ML>, + MakeEPMove, + >, + MakeEPMove, + >; +} + +pub(crate) trait RunPawnMoveSqs: SquareTy { + type Output: SquareListTy; +} +pub(crate) type PawnMoveSqs = >::Output; + +type S1 = Cap>, B, MoverC>; +type S2 = Cap>, B, MoverC>; +type S3 = Forward1>, B, MoverC>; +type S4 = + Forward2>>, B, MoverC>; + +type L1 = AppendMaybeSquare>; +type L2 = AppendMaybeSquare, S2>; +type L3 = AppendMaybeSquare, S3>; +type L4 = AppendMaybeSquare, S4>; + +impl RunPawnMoveSqs for S +where + MoverC: RunForwardO, + MoverC: RunForwardO, + MoverC: RunForwardO, + S: RunOffsetSquare>, + S: RunOffsetSquare>, + S: RunOffsetSquare>, + OffsetSquare>: RunCap, + OffsetSquare>: RunForward1, + S: RunYesIfDoubleSq>>, + YesIfDoubleSq>>: RunForward2, + OffsetSquare>: RunCap, + SLNil: RunAppendMaybeSquare>, + L1: RunAppendMaybeSquare>, + L2: RunAppendMaybeSquare>, + L3: RunAppendMaybeSquare>, +{ + type Output = L4; +} + +pub(crate) trait RunCap: MaybeSquare { + type Output: MaybeSquare; +} +pub(crate) type Cap = >::Output; + +impl RunCap for NoSquare { + type Output = NoSquare; +} +impl RunCap for SomeSquare +where + B: RunIdxBoard, + IdxBoard: RunYesIfOpp, +{ + type Output = YesIfOpp, S, MoverC>; +} + +pub(crate) trait RunForward1: MaybeSquare { + type Output: MaybeSquare; +} +pub(crate) type Forward1 = >::Output; + +impl RunForward1 for NoSquare { + type Output = NoSquare; +} +impl RunForward1 for SomeSquare +where + B: RunIdxBoard, + IdxBoard: RunYesIfEmpty>, +{ + type Output = YesIfEmpty, SomeSquare>; +} + +pub(crate) trait RunForward2: MaybeSquare { + type Output: MaybeSquare; +} +pub(crate) type Forward2 = >::Output; + +impl RunForward2 for NoSquare { + type Output = NoSquare; +} +impl RunForward2 for SomeSquare +where + MoverC: RunForwardO, + S: RunOffsetSquare>, + OffsetSquare>: RunForward1, + B: RunIdxBoard, + IdxBoard: RunYesIfEmpty>, B, MoverC>>, +{ + type Output = + YesIfEmpty, Forward1>, B, MoverC>>; +} + +pub(crate) trait RunMakeEPMove: + SquareTy +{ + type Output: MaybeMove; +} +pub(crate) type MakeEPMove = >::Output; + +impl RunMakeEPMove for S { + type Output = NoMove; +} +impl + RunMakeEPMove> for S +where + MoverC: RunForwardO, + S: RunOffsetSquare>, + EPS: RunEp>>, + S: RunEpMove>>>, +{ + type Output = + >>>>::Output; +} + +pub(crate) trait RunEpMove: + SquareTy +{ + type Output: MaybeMove; +} + +impl RunEpMove + for Start +{ + type Output = NoMove; +} +impl + RunEpMove> for Start +where + Start: RunOffsetSquare>, +{ + type Output = SomeMove< + Move, OffsetSquare>>, + >; +} + +pub(crate) trait RunEp: SquareTy { + type Output: MaybeSquare; +} +pub(crate) type Ep = >::Output; + +impl RunEp for S { + type Output = NoSquare; +} +impl RunEp> for S2 +where + S1: RunSquareEq, + S1: RunEpWMatches>, +{ + type Output = >>::Output; +} + +pub(crate) trait RunEpWMatches: SquareTy { + type Output: MaybeSquare; +} +impl RunEpWMatches for S { + type Output = SomeSquare; +} +impl RunEpWMatches for S { + type Output = NoSquare; +} + +pub(crate) trait RunYesIfDoubleSq: SquareTy { + type Output: MaybeSquare; +} +pub(crate) type YesIfDoubleSq = >::Output; + +impl RunYesIfDoubleSq for Square { + type Output = NoSquare; +} +impl RunYesIfDoubleSq for Square { + type Output = US; +} +impl RunYesIfDoubleSq for Square { + type Output = NoSquare; +} +impl RunYesIfDoubleSq for Square { + type Output = NoSquare; +} +impl RunYesIfDoubleSq for Square { + type Output = NoSquare; +} +impl RunYesIfDoubleSq for Square { + type Output = NoSquare; +} +impl RunYesIfDoubleSq for Square { + type Output = US; +} +impl RunYesIfDoubleSq for Square { + type Output = NoSquare; +} + +pub(crate) trait RunYesIfOpp: CellEn { + type Output: MaybeSquare; +} +pub(crate) type YesIfOpp = >::Output; + +impl RunYesIfOpp for Empty { + type Output = NoSquare; +} +impl RunYesIfOpp for Filled> { + type Output = NoSquare; +} +impl RunYesIfOpp for Filled> { + type Output = NoSquare; +} +impl RunYesIfOpp for Filled> { + type Output = SomeSquare; +} +impl RunYesIfOpp for Filled> { + type Output = SomeSquare; +} + +pub(crate) trait RunYesIfEmpty: CellEn { + type Output: MaybeSquare; +} +pub(crate) type YesIfEmpty = >::Output; + +impl RunYesIfEmpty> for Empty { + type Output = SomeSquare; +} +impl RunYesIfEmpty for Empty { + type Output = NoSquare; +} +impl RunYesIfEmpty for Filled { + type Output = NoSquare; +} + +// Square list is already only the pseudo legal moves. This is just for adding promotions. +pub(crate) trait RunPawnMlFromSL: + SquareListTy +{ + type Output: MoveListTy; +} +pub(crate) type PawnMLFromSl = + >::Output; + +impl + RunPawnMlFromSL for SLNil +{ + type Output = ML; +} +impl< + B: BoardTy, + Start: SquareTy, + MoverC: ColorEn, + ML: MoveListTy, + S: SquareTy, + Next: SquareListTy, + > RunPawnMlFromSL for SLCons +where + Next: RunPawnMlFromSL, + (): RunPawnMFromS>, +{ + type Output = PawnMFromS>; +} + +pub(crate) trait RunPawnMFromS< + B: BoardTy, + Start: SquareTy, + MoverC: ColorEn, + End: SquareTy, + ML: MoveListTy, +> +{ + type Output: MoveListTy; +} +pub(crate) type PawnMFromS = + <() as RunPawnMFromS>::Output; + +impl + RunPawnMFromS, ML> for () +where + (): RunPawnMFromRF, +{ + type Output = <() as RunPawnMFromRF>::Output; +} + +pub(crate) trait RunPawnMFromRF< + B: BoardTy, + Start: SquareTy, + MoverC: ColorEn, + EndR: RankEn, + EndF: FileEn, + ML: MoveListTy, +> +{ + type Output: MoveListTy; +} + +// Promotions +impl + RunPawnMFromRF for () +{ + type Output = MLCons< + Move, ColoredPiece>, + MLCons< + Move, ColoredPiece>, + MLCons< + Move, ColoredPiece>, + MLCons, ColoredPiece>, ML>, + >, + >, + >; +} +impl + RunPawnMFromRF for () +{ + type Output = MLCons< + Move, ColoredPiece>, + MLCons< + Move, ColoredPiece>, + MLCons< + Move, ColoredPiece>, + MLCons, ColoredPiece>, ML>, + >, + >, + >; +} + +// Non Promotions +impl + RunPawnMFromRF for () +{ + type Output = MLCons, ColoredPiece>, ML>; +} +impl + RunPawnMFromRF for () +{ + type Output = MLCons, ColoredPiece>, ML>; +} +impl + RunPawnMFromRF for () +{ + type Output = MLCons, ColoredPiece>, ML>; +} +impl + RunPawnMFromRF for () +{ + type Output = MLCons, ColoredPiece>, ML>; +} +impl + RunPawnMFromRF for () +{ + type Output = MLCons, ColoredPiece>, ML>; +} +impl + RunPawnMFromRF for () +{ + type Output = MLCons, ColoredPiece>, ML>; +} + +pub(crate) trait RunPawnAttackSqs: SquareTy { + type Output: SquareListTy; +} +pub(crate) type PawnAttackSqs = >::Output; + +type AL1 = AppendMaybeSquare>>; +type AL2 = AppendMaybeSquare, OffsetSquare>>; + +impl RunPawnAttackSqs for S +where + MoverC: RunForwardO + RunForwardO, + S: RunOffsetSquare> + RunOffsetSquare>, + SLNil: RunAppendMaybeSquare>>, + AL1: RunAppendMaybeSquare>>, +{ + type Output = AL2; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/pmoves.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/pmoves.rs new file mode 100644 index 000000000..e894193b8 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/pmoves.rs @@ -0,0 +1,171 @@ +use crate::{ + board_rep::{ + board::{ + idx::{IdxBoard, RunIdxBoard}, + BoardTy, CellEn, Empty, Filled, + }, + color::{Black, ColorEn, White}, + piece::{Bishop, ColoredPiece, King, Knight, Pawn, PieceEn, Queen, Rook}, + square::{offset::MaybeSquare, AllSqs, SquareTy}, + }, + util::Bool, +}; + +use super::{ + bishop::{BishopMoveSqs, RunBishopMoveSqs}, + castle::{KingsideCastle, QueensideCastle, RunKingsideCastle, RunQueensideCastle}, + king::{KingMoveSqs, RunKingMoveSqs}, + knight::{KnightMoveSqs, RunKnightMoveSqs}, + list::{AppendMaybeMove, MLNil, MoveListTy, RunAppendMaybeMove, SLCons, SLNil, SquareListTy}, + ml_from_sl::{PMoveLFromSqs, RunPMoveLFromSqs}, + pawn::{PawnMoves, RunPawnMoves}, + queen::{QueenMoveSqs, RunQueenMoveSqs}, + rook::{RookMoveSqs, RunRookMoveSqs}, +}; + +pub(crate) trait RunPMovesForSq: + SquareTy +{ + type Output: MoveListTy; +} +pub(crate) type PMovesForSq = + >::Output; + +impl + RunPMovesForSq for S +where + B: RunIdxBoard, + S: RunPMovesForTypeAtSq, EP, ML>, +{ + type Output = , EP, ML>>::Output; +} + +pub(crate) trait RunPMovesForTypeAtSq< + B: BoardTy, + MoverC: ColorEn, + Type: CellEn, + EP: MaybeSquare, + ML: MoveListTy, +>: SquareTy +{ + type Output: MoveListTy; +} + +impl + RunPMovesForTypeAtSq for S +{ + type Output = ML; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +{ + type Output = ML; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +{ + type Output = ML; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +where + S: RunKnightMoveSqs, + KnightMoveSqs: RunPMoveLFromSqs, +{ + type Output = PMoveLFromSqs, B, S, C, Knight, ML>; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +where + S: RunKingMoveSqs, + KingMoveSqs: RunPMoveLFromSqs, +{ + type Output = PMoveLFromSqs, B, S, C, King, ML>; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +where + S: RunRookMoveSqs, + RookMoveSqs: RunPMoveLFromSqs, +{ + type Output = PMoveLFromSqs, B, S, C, Rook, ML>; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +where + S: RunBishopMoveSqs, + BishopMoveSqs: RunPMoveLFromSqs, +{ + type Output = PMoveLFromSqs, B, S, C, Bishop, ML>; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +where + S: RunQueenMoveSqs, + QueenMoveSqs: RunPMoveLFromSqs, +{ + type Output = PMoveLFromSqs, B, S, C, Queen, ML>; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +where + S: RunPawnMoves, +{ + type Output = PawnMoves; +} +impl + RunPMovesForTypeAtSq>, EP, ML> for S +where + S: RunPawnMoves, +{ + type Output = PawnMoves; +} + +pub(crate) trait RunPMoves: + BoardTy +{ + type Output: MoveListTy; +} +pub(crate) type PMoves = + >::Output; + +impl + RunPMoves for B +where + B: RunPMovesForSqL, + B: RunKingsideCastle, + B: RunQueensideCastle, + >::Output: + RunAppendMaybeMove>, + AppendMaybeMove< + >::Output, + KingsideCastle, + >: RunAppendMaybeMove>, +{ + type Output = AppendMaybeMove< + AppendMaybeMove< + >::Output, + KingsideCastle, + >, + QueensideCastle, + >; +} + +pub(crate) trait RunPMovesForSqL: + BoardTy +{ + type Output: MoveListTy; +} +pub(crate) type PMovesForSqL = >::Output; + +impl RunPMovesForSqL for B { + type Output = MLNil; +} +impl + RunPMovesForSqL, MoverC, EP> for B +where + S: RunPMovesForSq>, + B: RunPMovesForSqL, +{ + type Output = PMovesForSq>; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/queen.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/queen.rs new file mode 100644 index 000000000..b2f741a16 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/queen.rs @@ -0,0 +1,48 @@ +use super::{ + cast::{AddCastSqs, RunAddCastSqs}, + list::{SLNil, SquareListTy}, +}; +use crate::board_rep::{ + board::BoardTy, + square::{ + offset::{Neg1, Offset, Pos1, Zero}, + SquareTy, + }, +}; + +pub(crate) trait RunQueenMoveSqs: SquareTy { + type Output: SquareListTy; +} +pub(crate) type QueenMoveSqs = >::Output; + +type O1 = Offset; +type O2 = Offset; +type O3 = Offset; +type O4 = Offset; +type O5 = Offset; +type O6 = Offset; +type O7 = Offset; +type O8 = Offset; + +type L1 = AddCastSqs; +type L2 = AddCastSqs, B, S, O2>; +type L3 = AddCastSqs, B, S, O3>; +type L4 = AddCastSqs, B, S, O4>; +type L5 = AddCastSqs, B, S, O5>; +type L6 = AddCastSqs, B, S, O6>; +type L7 = AddCastSqs, B, S, O7>; +type L8 = AddCastSqs, B, S, O8>; + +impl RunQueenMoveSqs for S +where + SLNil: RunAddCastSqs, + L1: RunAddCastSqs, + L2: RunAddCastSqs, + L3: RunAddCastSqs, + L4: RunAddCastSqs, + L5: RunAddCastSqs, + L6: RunAddCastSqs, + L7: RunAddCastSqs, +{ + type Output = L8; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/move_gen/rook.rs b/collector/compile-benchmarks/type-system-chess/src/move_gen/rook.rs new file mode 100644 index 000000000..0c3fd4ae3 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/move_gen/rook.rs @@ -0,0 +1,36 @@ +use super::{ + cast::{AddCastSqs, RunAddCastSqs}, + list::{SLNil, SquareListTy}, +}; +use crate::board_rep::{ + board::BoardTy, + square::{ + offset::{Neg1, Offset, Pos1, Zero}, + SquareTy, + }, +}; + +pub(crate) trait RunRookMoveSqs: SquareTy { + type Output: SquareListTy; +} +pub(crate) type RookMoveSqs = >::Output; + +type O1 = Offset; +type O2 = Offset; +type O3 = Offset; +type O4 = Offset; + +type L1 = AddCastSqs; +type L2 = AddCastSqs, B, S, O2>; +type L3 = AddCastSqs, B, S, O3>; +type L4 = AddCastSqs, B, S, O4>; + +impl RunRookMoveSqs for S +where + SLNil: RunAddCastSqs, + L1: RunAddCastSqs, + L2: RunAddCastSqs, + L3: RunAddCastSqs, +{ + type Output = L4; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/state.rs b/collector/compile-benchmarks/type-system-chess/src/state.rs new file mode 100644 index 000000000..37cb9a29a --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/state.rs @@ -0,0 +1,301 @@ +use crate::{ + board_rep::{ + board::{ + write::{MaybeWriteToBoard, RunMaybeWriteToBoard, RunWriteToBoard, WriteToBoard}, + BoardTy, Empty, Filled, + }, + color::{Black, ColorEn, White}, + piece::{Bishop, ColoredPiece, ColoredPieceTy, King, Knight, Pawn, Queen, Rook}, + square::{ + file, + offset::{ + MaybeSquare, Neg1, Neg2, NoSquare, Offset, OffsetSquare, OffsetTy, Pos1, Pos2, + RunOffsetSquare, SomeSquare, Zero, + }, + rank, RunSquareEq, Square, SquareEq, SquareTy, + }, + }, + move_gen::{Move, MoveTy}, + util::{And, Bool, False, Not, Or, RunAnd, RunNot, RunOr, True}, + values, +}; +use std::marker::PhantomData; + +pub mod outcome; + +pub(crate) trait StateTy { + fn reify() -> values::State; +} +pub(crate) struct State( + PhantomData<(ToMove, Pieces, EPSquare, CA)>, +); + +pub(crate) trait CastleStateTy { + fn reify() -> values::CastleState; +} +pub(crate) struct CastleState( + PhantomData<(WK, WQ, BK, BQ)>, +); + +pub(crate) type FullCa = CastleState; +pub(crate) type EmptyCa = CastleState; + +impl CastleStateTy for CastleState { + fn reify() -> values::CastleState { + values::CastleState { + wk: WK::reify(), + wq: WQ::reify(), + bk: BK::reify(), + bq: BQ::reify(), + } + } +} + +impl StateTy + for State +{ + fn reify() -> values::State { + values::State { + to_move: ToMove::reify(), + pieces: Pieces::reify(), + ep_square: EPSquare::reify(), + castle_state: CA::reify(), + } + } +} + +pub(crate) trait RunMakeMove: MoveTy { + type Output: StateTy; +} +pub(crate) type MakeMove = >::Output; + +impl + RunMakeMove> for M +where + M: RunMakeMoveForBoard, + M: RunMakeMoveForEp, + M: RunMakeMoveForCa, +{ + type Output = + State, MakeMoveForEP, MakeMoveForCa>; +} +impl + RunMakeMove> for M +where + M: RunMakeMoveForBoard, + M: RunMakeMoveForEp, + M: RunMakeMoveForCa, +{ + type Output = + State, MakeMoveForEP, MakeMoveForCa>; +} + +pub(crate) trait RunMakeMoveForBoard: MoveTy { + type Output: BoardTy; +} +pub(crate) type MakeMoveForBoard = >::Output; + +impl< + B: BoardTy, + MoverC: ColorEn, + F: SquareTy, + T: SquareTy, + P: ColoredPieceTy, + EP: MaybeSquare, + RFrom: MaybeSquare, + RTo: MaybeSquare, + > RunMakeMoveForBoard for Move +where + B: RunWriteToBoard>, + WriteToBoard>: RunWriteToBoard, + WriteToBoard>, F, Empty>: RunMaybeWriteToBoard, + MaybeWriteToBoard>, F, Empty>, EP, Empty>: + RunMaybeWriteToBoard, + MaybeWriteToBoard< + MaybeWriteToBoard>, F, Empty>, EP, Empty>, + RFrom, + Empty, + >: RunMaybeWriteToBoard>>, +{ + type Output = MaybeWriteToBoard< + MaybeWriteToBoard< + MaybeWriteToBoard>, F, Empty>, EP, Empty>, + RFrom, + Empty, + >, + RTo, + Filled>, + >; +} + +pub(crate) trait RunMakeMoveForEp: MoveTy { + type Output: MaybeSquare; +} +pub(crate) type MakeMoveForEP = ::Output; + +impl RunMakeMoveForEp + for Move> +{ + type Output = NoSquare; +} +impl RunMakeMoveForEp + for Move> +{ + type Output = NoSquare; +} +impl RunMakeMoveForEp + for Move> +{ + type Output = NoSquare; +} +impl RunMakeMoveForEp + for Move> +{ + type Output = NoSquare; +} +impl RunMakeMoveForEp + for Move, NoSquare, RF, RT> +{ + type Output = NoSquare; +} +impl RunMakeMoveForEp + for Move, EP> +where + MoverC: RunForward1O + RunForward2O, + F: RunOffsetSquare> + RunOffsetSquare>, + T: RunMaybeSqEq>>, + OffsetSquare>: + RunSelectIf>>>, +{ + type Output = > as RunSelectIf< + MaybeSqEq>>, + >>::Output; +} + +pub(crate) trait RunMaybeSqEq: SquareTy { + type Output: Bool; +} +pub(crate) type MaybeSqEq = >::Output; + +impl RunMaybeSqEq for S { + type Output = False; +} +impl RunMaybeSqEq> for S2 +where + S1: RunSquareEq, +{ + type Output = SquareEq; +} + +pub(crate) trait RunForward2O: ColorEn { + type Output: OffsetTy; +} +pub(crate) type Forward2O = ::Output; +impl RunForward2O for White { + type Output = Offset; +} +impl RunForward2O for Black { + type Output = Offset; +} + +pub(crate) trait RunForward1O: ColorEn { + type Output: OffsetTy; +} +pub(crate) type Forward1O = ::Output; +impl RunForward1O for White { + type Output = Offset; +} +impl RunForward1O for Black { + type Output = Offset; +} + +pub(crate) trait RunSelectIf: MaybeSquare { + type Output: MaybeSquare; +} + +impl RunSelectIf for NoSquare { + type Output = NoSquare; +} +impl RunSelectIf for SomeSquare { + type Output = NoSquare; +} +impl RunSelectIf for SomeSquare { + type Output = Self; +} + +pub(crate) trait RunMakeMoveForCa: MoveTy { + type Output: CastleStateTy; +} +pub(crate) type MakeMoveForCa = >::Output; + +type A1 = Square; +type E1 = Square; +type H1 = Square; +type A8 = Square; +type E8 = Square; +type H8 = Square; + +impl< + CA: CastleStateTy, + F: SquareTy, + T: SquareTy, + CP: ColoredPieceTy, + EP: MaybeSquare, + RF: MaybeSquare, + RT: MaybeSquare, + > RunMakeMoveForCa for Move +where + T: RunSquareEq + RunSquareEq

+ RunSquareEq + RunSquareEq, + F: RunSquareEq + + RunSquareEq

+ + RunSquareEq + + RunSquareEq + + RunSquareEq + + RunSquareEq, + SquareEq: RunOr>, + SquareEq: RunOr, SquareEq>>, + SquareEq: RunOr>, + SquareEq: RunOr, SquareEq>>, + SquareEq: RunOr>, + SquareEq: RunOr, SquareEq>>, + SquareEq: RunOr>, + SquareEq: RunOr, SquareEq>>, + CA: RunMakeMoveForCaWSqsTouched< + Or, Or, SquareEq>>, + Or, Or, SquareEq>>, + Or, Or, SquareEq>>, + Or, Or, SquareEq>>, + >, +{ + type Output = MakeMoveForCaWSqsTouched< + CA, + Or, Or, SquareEq>>, + Or, Or, SquareEq>>, + Or, Or, SquareEq>>, + Or, Or, SquareEq>>, + >; +} + +pub(crate) trait RunMakeMoveForCaWSqsTouched: + CastleStateTy +{ + type Output: CastleStateTy; +} +pub(crate) type MakeMoveForCaWSqsTouched = + >::Output; + +impl + RunMakeMoveForCaWSqsTouched for CastleState +where + WK2: RunNot, + WQ2: RunNot, + BK2: RunNot, + BQ2: RunNot, + WK1: RunAnd>, + WQ1: RunAnd>, + BK1: RunAnd>, + BQ1: RunAnd>, +{ + type Output = + CastleState>, And>, And>, And>>; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/state/outcome.rs b/collector/compile-benchmarks/type-system-chess/src/state/outcome.rs new file mode 100644 index 000000000..754e6df0f --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/state/outcome.rs @@ -0,0 +1,99 @@ +use std::marker::PhantomData; + +use crate::{ + board_rep::{ + board::BoardTy, + color::{Black, ColorEn, White}, + square::offset::MaybeSquare, + }, + move_gen::{ + check::{IsCheck, RunIsCheck}, + list::{MLCons, MLNil, MoveListTy}, + moves::{Moves, RunMoves}, + MoveTy, + }, + util::{Bool, False, True}, + values, +}; + +use super::{CastleStateTy, State, StateTy}; + +pub(crate) trait OutcomeEn { + fn reify() -> values::Outcome; +} +pub(crate) struct Ongoing; +pub(crate) struct Draw; +pub(crate) struct Checkmate(PhantomData); + +impl OutcomeEn for Ongoing { + fn reify() -> values::Outcome { + values::Outcome::Ongoing + } +} +impl OutcomeEn for Draw { + fn reify() -> values::Outcome { + values::Outcome::Draw + } +} +impl OutcomeEn for Checkmate { + fn reify() -> values::Outcome { + values::Outcome::Checkmate(W::reify()) + } +} + +pub(crate) trait RunOutcome: StateTy { + type Output: OutcomeEn; +} +pub(crate) type Outcome = ::Output; + +impl RunOutcome for State +where + State: RunMoves, + State: RunOutcomeWML>, Black>, +{ + type Output = + as RunOutcomeWML>, Black>>::Output; +} +impl RunOutcome for State +where + State: RunMoves, + State: RunOutcomeWML>, White>, +{ + type Output = + as RunOutcomeWML>, White>>::Output; +} + +pub(crate) trait RunOutcomeWML: StateTy { + type Output: OutcomeEn; +} + +impl RunOutcomeWML, MoverC> + for S +{ + type Output = Ongoing; +} +impl RunOutcomeWML for S +where + S: RunIsCheck, + S: RunOutcomeWIsCheck>, +{ + type Output = >>::Output; +} + +pub(crate) trait RunOutcomeWIsCheck: StateTy { + type Output: OutcomeEn; +} + +impl RunOutcomeWIsCheck for S { + type Output = Draw; +} +impl RunOutcomeWIsCheck + for State +{ + type Output = Checkmate; +} +impl RunOutcomeWIsCheck + for State +{ + type Output = Checkmate; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/test.rs b/collector/compile-benchmarks/type-system-chess/src/test.rs new file mode 100644 index 000000000..197c94008 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/test.rs @@ -0,0 +1,3 @@ +mod make_move; +mod outcome; +mod perft; diff --git a/collector/compile-benchmarks/type-system-chess/src/ui.rs b/collector/compile-benchmarks/type-system-chess/src/ui.rs new file mode 100644 index 000000000..39e9c6511 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/ui.rs @@ -0,0 +1,223 @@ +use crate::{ + board_rep::{ + color::ColorEn, + piece::{ColoredPiece, ColoredPieceTy, PieceEn, PieceEq, RunPieceEq}, + square::{offset::MaybeSquare, RunSquareEq, SquareEq, SquareTy}, + }, + move_gen::{ + list::{MLCons, MLNil, MoveListTy}, + moves::{Moves, RunMoves}, + MaybeMove, Move, MoveTy, NoMove, SomeMove, + }, + state::{ + outcome::{Checkmate, Draw, Ongoing, Outcome, OutcomeEn, RunOutcome}, + MakeMove, RunMakeMove, StateTy, + }, + util::{And, Bool, False, RunAnd, True}, + values, +}; +use std::marker::PhantomData; + +pub(crate) mod prelude { + #![allow(unused)] + + pub(crate) use super::{MakeEM, MakeEMResultEn}; + pub(crate) use crate::board_rep::board::{Board, BoardRank}; + pub(crate) use crate::board_rep::piece::{Bishop, Knight, Queen, Rook}; + pub(crate) use crate::board_rep::square::{all_sqs::*}; + pub(crate) use crate::board_rep::square::offset::{SomeSquare, NoSquare}; + use crate::state::FullCa; + pub(crate) use crate::state::State; + pub(crate) use crate::util::board_creator::*; + pub(crate) use crate::board_rep::color::{White, Black}; + + type StartStateBoard = Board< + // AA BB CC DD EE FF GG HH + BoardRank, // 1 + BoardRank, // 2 + BoardRank<__, __, __, __, __, __, __, __>, // 3 + BoardRank<__, __, __, __, __, __, __, __>, // 4 + BoardRank<__, __, __, __, __, __, __, __>, // 5 + BoardRank<__, __, __, __, __, __, __, __>, // 6 + BoardRank, // 7 + BoardRank, // 8 + >; + pub(crate) type StartState = State; +} + +pub(crate) trait MaybePiece { + fn reify() -> Option; +} +pub(crate) struct NoPiece; + +impl MaybePiece for NoPiece { + fn reify() -> Option { + None + } +} +impl MaybePiece for P { + fn reify() -> Option { + Some(P::reify()) + } +} + +pub(crate) trait EasyMoveTy {} +pub(crate) struct EasyMove( + PhantomData<(From, To, Piece)>, +); + +impl EasyMoveTy for EasyMove {} + +pub(crate) trait RunFindMove: EasyMoveTy { + type Output: MaybeMove; +} +pub(crate) type FindMove = >::Output; + +impl RunFindMove for EM { + type Output = NoMove; +} +impl< + F: SquareTy, + T: SquareTy, + CP: ColoredPieceTy, + EP: MaybeSquare, + RF: MaybeSquare, + RT: MaybeSquare, + Next: MoveListTy, + EMF: SquareTy, + EMT: SquareTy, + > RunFindMove, Next>> for EasyMove +where + F: RunSquareEq, + T: RunSquareEq, + SquareEq: RunAnd>, + EasyMove: RunFindMoveWMatches< + Move, + Next, + And, SquareEq>, + >, +{ + type Output = as RunFindMoveWMatches< + Move, + Next, + And, SquareEq>, + >>::Output; +} +impl< + F: SquareTy, + T: SquareTy, + MoverC: ColorEn, + P: PieceEn, + EP: MaybeSquare, + RF: MaybeSquare, + RT: MaybeSquare, + Next: MoveListTy, + EMF: SquareTy, + EMT: SquareTy, + EMP: PieceEn + MaybePiece, + > RunFindMove, EP, RF, RT>, Next>> + for EasyMove +where + F: RunSquareEq, + T: RunSquareEq, + SquareEq: RunAnd>, + P: RunPieceEq, + PieceEq: RunAnd, SquareEq>>, + EasyMove: RunFindMoveWMatches< + Move, EP, RF, RT>, + Next, + And, And, SquareEq>>, + >, +{ + type Output = as RunFindMoveWMatches< + Move, EP, RF, RT>, + Next, + And, And, SquareEq>>, + >>::Output; +} + +pub(crate) trait RunFindMoveWMatches: + EasyMoveTy +{ + type Output: MaybeMove; +} + +impl RunFindMoveWMatches for EM { + type Output = SomeMove; +} +impl RunFindMoveWMatches for EM +where + EM: RunFindMove, +{ + type Output = FindMove; +} + +pub(crate) trait MakeEMResultEn { + fn reify() -> values::EMResult; +} +pub(crate) struct InvalidMove; + +impl MakeEMResultEn for S { + fn reify() -> values::EMResult { + values::EMResult::Ongoing(S::reify()) + } +} +impl MakeEMResultEn for InvalidMove { + fn reify() -> values::EMResult { + values::EMResult::InvalidMove + } +} +impl MakeEMResultEn for Draw { + fn reify() -> values::EMResult { + values::EMResult::Draw + } +} +impl MakeEMResultEn for Checkmate { + fn reify() -> values::EMResult { + values::EMResult::Checkmate(C::reify()) + } +} + +pub(crate) trait RunMakeEM: StateTy { + type Output: MakeEMResultEn; +} +pub(crate) type MakeEM = >::Output; + +impl RunMakeEM for S +where + S: RunMoves, + EasyMove: RunFindMove>, + S: RunMakeEMwM, Moves>>, +{ + type Output = , Moves>>>::Output; +} + +pub(crate) trait RunMakeEMwM: StateTy { + type Output: MakeEMResultEn; +} + +impl RunMakeEMwM for S { + type Output = InvalidMove; +} +impl RunMakeEMwM> for S +where + M: RunMakeMove, + MakeMove: RunOutcome, + MakeMove: RunMakeEMwO>>, +{ + type Output = as RunMakeEMwO>>>::Output; +} + +pub(crate) trait RunMakeEMwO: StateTy { + type Output: MakeEMResultEn; +} + +impl RunMakeEMwO for S { + type Output = S; +} +impl RunMakeEMwO for S { + type Output = Draw; +} +impl RunMakeEMwO> for S { + type Output = Checkmate; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/util.rs b/collector/compile-benchmarks/type-system-chess/src/util.rs new file mode 100644 index 000000000..ac3a256b2 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/util.rs @@ -0,0 +1,88 @@ +pub(crate) trait Bool { + fn reify() -> bool; +} +pub(crate) struct True; +pub(crate) struct False; + +impl Bool for True { + fn reify() -> bool { + true + } +} +impl Bool for False { + fn reify() -> bool { + false + } +} + +pub(crate) trait RunOr: Bool { + type Output: Bool; +} +pub(crate) type Or = >::Output; + +impl RunOr for True { + type Output = True; +} +impl RunOr for False { + type Output = True; +} +impl RunOr for True { + type Output = True; +} +impl RunOr for False { + type Output = False; +} + +pub(crate) trait RunAnd: Bool { + type Output: Bool; +} +pub(crate) type And = >::Output; + +impl RunAnd for True { + type Output = True; +} +impl RunAnd for False { + type Output = False; +} +impl RunAnd for True { + type Output = False; +} +impl RunAnd for False { + type Output = False; +} + +pub(crate) trait RunNot: Bool { + type Output: Bool; +} +pub(crate) type Not = ::Output; + +impl RunNot for True { + type Output = False; +} +impl RunNot for False { + type Output = True; +} + +pub mod board_creator { + #![allow(unused)] + + use crate::board_rep::{ + board::{Empty, Filled}, + color, + piece::{self, ColoredPiece}, + }; + + pub(crate) type WP = Filled>; + pub(crate) type WB = Filled>; + pub(crate) type WN = Filled>; + pub(crate) type WR = Filled>; + pub(crate) type WQ = Filled>; + pub(crate) type WK = Filled>; + pub(crate) type BP = Filled>; + pub(crate) type BB = Filled>; + pub(crate) type BN = Filled>; + pub(crate) type BR = Filled>; + pub(crate) type BQ = Filled>; + pub(crate) type BK = Filled>; + pub(crate) type __ = Empty; +} diff --git a/collector/compile-benchmarks/type-system-chess/src/values.rs b/collector/compile-benchmarks/type-system-chess/src/values.rs new file mode 100644 index 000000000..e817b05d1 --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/src/values.rs @@ -0,0 +1,416 @@ +#![allow(unused)] + +use std::fmt::{write, Debug, Display}; + +use crate::board_rep::{board::CellEn, square::offset::rank}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Color { + White, + Black, +} + +impl Display for Color { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Color::White => "White", + Color::Black => "Black", + } + ) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Piece { + Pawn, + Bishop, + Knight, + Rook, + Queen, + King, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct ColoredPiece { + pub(crate) piece: Piece, + pub(crate) color: Color, +} +impl Display for ColoredPiece { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let c = match (self.piece, self.color) { + (Piece::Pawn, Color::White) => 'P', + (Piece::Pawn, Color::Black) => 'p', + (Piece::Bishop, Color::White) => 'B', + (Piece::Bishop, Color::Black) => 'b', + (Piece::Knight, Color::White) => 'N', + (Piece::Knight, Color::Black) => 'n', + (Piece::Rook, Color::White) => 'R', + (Piece::Rook, Color::Black) => 'r', + (Piece::Queen, Color::White) => 'Q', + (Piece::Queen, Color::Black) => 'q', + (Piece::King, Color::White) => 'K', + (Piece::King, Color::Black) => 'k', + }; + write!(f, "{c}") + } +} + +#[derive(Clone, Copy, PartialEq, Eq)] +pub(crate) enum Rank { + R1, + R2, + R3, + R4, + R5, + R6, + R7, + R8, +} +impl Debug for Rank { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::R1 => write!(f, "1"), + Self::R2 => write!(f, "2"), + Self::R3 => write!(f, "3"), + Self::R4 => write!(f, "4"), + Self::R5 => write!(f, "5"), + Self::R6 => write!(f, "6"), + Self::R7 => write!(f, "7"), + Self::R8 => write!(f, "8"), + } + } +} +impl Display for Rank { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + ::fmt(self, f) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum File { + A, + B, + C, + D, + E, + F, + G, + H, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +pub(crate) struct Square { + pub(crate) rank: Rank, + pub(crate) file: File, +} +impl Debug for Square { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}{:?}", self.file, self.rank) + } +} +impl Display for Square { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}{:?}", self.file, self.rank) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct SquareList(pub Vec); +impl Display for SquareList { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use File::*; + use Rank::*; + + writeln!(f, " A B C D E F G H ")?; + for rank in [R1, R2, R3, R4, R5, R6, R7, R8] { + write!(f, "{rank} ")?; + for file in [A, B, C, D, E, F, G, H] { + let sq = Square { rank, file }; + let inc = self.0.contains(&sq); + let c = if inc { '#' } else { '.' }; + write!(f, "{c} ")?; + } + writeln!(f, "{rank}")?; + } + writeln!(f, " A B C D E F G H ")?; + + Ok(()) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Cell { + Empty, + Filled(ColoredPiece), +} +impl Display for Cell { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Cell::Empty => write!(f, "."), + Cell::Filled(cp) => write!(f, "{cp}"), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct BoardRank { + pub(crate) a: Cell, + pub(crate) b: Cell, + pub(crate) c: Cell, + pub(crate) d: Cell, + pub(crate) e: Cell, + pub(crate) f: Cell, + pub(crate) g: Cell, + pub(crate) h: Cell, +} +impl BoardRank { + fn file(&self, r: File) -> Cell { + match r { + File::A => self.a, + File::B => self.b, + File::C => self.c, + File::D => self.d, + File::E => self.e, + File::F => self.f, + File::G => self.g, + File::H => self.h, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct Board { + pub(crate) r1: BoardRank, + pub(crate) r2: BoardRank, + pub(crate) r3: BoardRank, + pub(crate) r4: BoardRank, + pub(crate) r5: BoardRank, + pub(crate) r6: BoardRank, + pub(crate) r7: BoardRank, + pub(crate) r8: BoardRank, +} +impl Board { + fn rank(&self, r: Rank) -> &BoardRank { + match r { + Rank::R1 => &self.r1, + Rank::R2 => &self.r2, + Rank::R3 => &self.r3, + Rank::R4 => &self.r4, + Rank::R5 => &self.r5, + Rank::R6 => &self.r6, + Rank::R7 => &self.r7, + Rank::R8 => &self.r8, + } + } +} +impl Display for Board { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use File::*; + use Rank::*; + + writeln!(f, " A B C D E F G H ")?; + for rank in [R1, R2, R3, R4, R5, R6, R7, R8] { + write!(f, "{rank} ")?; + for file in [A, B, C, D, E, F, G, H] { + let sq = Square { rank, file }; + let cell = self.rank(rank).file(file); + write!(f, "{cell} ")?; + } + writeln!(f, "{rank}")?; + } + write!(f, " A B C D E F G H ")?; + + Ok(()) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct SquareSetRank { + pub(crate) a: bool, + pub(crate) b: bool, + pub(crate) c: bool, + pub(crate) d: bool, + pub(crate) e: bool, + pub(crate) f: bool, + pub(crate) g: bool, + pub(crate) h: bool, +} +impl SquareSetRank { + fn file(&self, r: File) -> bool { + match r { + File::A => self.a, + File::B => self.b, + File::C => self.c, + File::D => self.d, + File::E => self.e, + File::F => self.f, + File::G => self.g, + File::H => self.h, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct SquareSet { + pub(crate) r1: SquareSetRank, + pub(crate) r2: SquareSetRank, + pub(crate) r3: SquareSetRank, + pub(crate) r4: SquareSetRank, + pub(crate) r5: SquareSetRank, + pub(crate) r6: SquareSetRank, + pub(crate) r7: SquareSetRank, + pub(crate) r8: SquareSetRank, +} +impl SquareSet { + fn rank(&self, r: Rank) -> &SquareSetRank { + match r { + Rank::R1 => &self.r1, + Rank::R2 => &self.r2, + Rank::R3 => &self.r3, + Rank::R4 => &self.r4, + Rank::R5 => &self.r5, + Rank::R6 => &self.r6, + Rank::R7 => &self.r7, + Rank::R8 => &self.r8, + } + } +} +impl Display for SquareSet { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use File::*; + use Rank::*; + + writeln!(f, " A B C D E F G H ")?; + for rank in [R1, R2, R3, R4, R5, R6, R7, R8] { + write!(f, "{rank} ")?; + for file in [A, B, C, D, E, F, G, H] { + let sq = Square { rank, file }; + let cell = self.rank(rank).file(file); + let char = if cell { '#' } else { '.' }; + write!(f, "{char} ")?; + } + writeln!(f, "{rank}")?; + } + write!(f, " A B C D E F G H ")?; + + Ok(()) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct CastleState { + pub(crate) wk: bool, + pub(crate) wq: bool, + pub(crate) bk: bool, + pub(crate) bq: bool, +} + +impl Display for CastleState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut res = String::with_capacity(4); + if self.wk { + res.push('K') + }; + if self.wq { + res.push('Q') + }; + if self.bk { + res.push('k') + }; + if self.bq { + res.push('q') + }; + if res == "" { + res.push('-') + } + write!(f, "{res}") + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct State { + pub(crate) to_move: Color, + pub(crate) pieces: Board, + pub(crate) ep_square: Option, + pub(crate) castle_state: CastleState, +} + +impl Display for State { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "{}", self.pieces)?; + write!( + f, + "To move: {} | EP: {} | Castle: {}", + self.to_move, + if let Some(sq) = self.ep_square { + format!("{sq}") + } else { + "-".to_string() + }, + self.castle_state + ) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct Move { + pub(crate) from: Square, + pub(crate) to: Square, + pub(crate) piece: ColoredPiece, + pub(crate) ep: Option, + pub(crate) rook_from: Option, + pub(crate) rook_to: Option, +} +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct MoveList(pub Vec); +impl MoveList { + pub fn destinations(&self) -> SquareList { + SquareList(self.0.iter().map(|m| m.to).collect()) + } +} + +#[derive(Debug)] +pub(crate) struct Offset { + pub(crate) rank: i8, + pub(crate) file: i8, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Outcome { + Ongoing, + Draw, + Checkmate(Color), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum EMResult { + InvalidMove, + Ongoing(State), + Draw, + Checkmate(Color), +} + +impl EMResult { + pub(crate) fn unwrap_state(&self) -> &State { + match self { + EMResult::InvalidMove => panic!("Expected state, got invalid move."), + EMResult::Ongoing(s) => s, + EMResult::Draw => panic!("Expected state, got draw."), + EMResult::Checkmate(c) => panic!("Expected state, got checkmate by {c}."), + } + } +} + +impl Display for EMResult { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + EMResult::InvalidMove => write!(f, "Invalid move."), + EMResult::Ongoing(s) => write!(f, "{s}"), + EMResult::Draw => write!(f, "Draw by stalemate."), + EMResult::Checkmate(c) => write!(f, "Checkmate - {c} wins!"), + } + } +} From 611eb8a672fb683357ba7dda4db37e04e2a8e1ce Mon Sep 17 00:00:00 2001 From: Daniel James Date: Fri, 28 Jul 2023 10:59:46 -0400 Subject: [PATCH 2/3] Config --- collector/compile-benchmarks/type-system-chess/Cargo.lock | 7 +++++++ collector/compile-benchmarks/type-system-chess/Cargo.toml | 2 ++ .../compile-benchmarks/type-system-chess/perf-config.json | 4 ++++ 3 files changed, 13 insertions(+) create mode 100644 collector/compile-benchmarks/type-system-chess/Cargo.lock create mode 100644 collector/compile-benchmarks/type-system-chess/perf-config.json diff --git a/collector/compile-benchmarks/type-system-chess/Cargo.lock b/collector/compile-benchmarks/type-system-chess/Cargo.lock new file mode 100644 index 000000000..b21cc6a2d --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/collector/compile-benchmarks/type-system-chess/Cargo.toml b/collector/compile-benchmarks/type-system-chess/Cargo.toml index fb2d35399..fb5ccec62 100644 --- a/collector/compile-benchmarks/type-system-chess/Cargo.toml +++ b/collector/compile-benchmarks/type-system-chess/Cargo.toml @@ -7,3 +7,5 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] + +[workspace] \ No newline at end of file diff --git a/collector/compile-benchmarks/type-system-chess/perf-config.json b/collector/compile-benchmarks/type-system-chess/perf-config.json new file mode 100644 index 000000000..e7dab85ce --- /dev/null +++ b/collector/compile-benchmarks/type-system-chess/perf-config.json @@ -0,0 +1,4 @@ +{ + "category": "secondary", + "artifact": "library" +} \ No newline at end of file From 795456f52b7bac54bc47a3378afe6d6151b75b5b Mon Sep 17 00:00:00 2001 From: Daniel James Date: Fri, 28 Jul 2023 11:26:08 -0400 Subject: [PATCH 3/3] Exclude codegen profiles --- .../compile-benchmarks/type-system-chess/perf-config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/collector/compile-benchmarks/type-system-chess/perf-config.json b/collector/compile-benchmarks/type-system-chess/perf-config.json index e7dab85ce..778f02cf9 100644 --- a/collector/compile-benchmarks/type-system-chess/perf-config.json +++ b/collector/compile-benchmarks/type-system-chess/perf-config.json @@ -1,4 +1,5 @@ { "category": "secondary", - "artifact": "library" + "artifact": "library", + "excluded_profiles": ["Debug", "Opt", "Doc"] } \ No newline at end of file