Chess implemented entirely in the Rust and TS type systems.

Overview

Type System Chess

This repo contains chess implemented entirely in the (stable) Rust and Typescript type systems. Both languages feature turing complete type systems making this possible.

What I mean by a chess implementation is the ability to generate the list of legal moves and then select and play one by specifying the start and end square. See Features for more.

I believe that this is the most complicated program implemented in the Rust type system. If you know of any similarly complex Rust projects please let me know.

Other very complex type systems programs have been written in Typescript. For example, HypeScript.

A showcase of the typescript version of the program working. A chess board is shown in the VSCode hover window while focusing the type PlayChess

Features

The programs implement all chess rules except for draws by repetition and the 50 move rule. That means they cover:

  • Castling
  • Promotion
  • En Passant

Both versions allow you to play moves using long algebraic notation (specifying the start and end square). Because TS allows parsing strings in the type system I also implemented a FEN parser for the TS version.

Check main.rs and index.ts for more on how to use.

Bugs

I have implemented a few tests for each program but it is likely that bugs still exist. If you find a bug or add more tests feel free to open an issue/PR.

Performance

Both programs are rather slow. Running the TS test suite takes about 30 seconds on my computer and running the Rust test suite takes about 10 minutes.

Rust performance was a fair bit better up until the end when I added the various complex pawn moves. I suspect that the compiler might have some extra difficulties with how these are implemented.

I was almost able to get away without increasing the recursion limit for the rust program. It was fine after adding castling kingside but broke when I added queenside castling (i.e. it is very close to not needing the extra recursion).

Inspiration

Typescript

I read a number of different articles about programming in TS types but this one specifically is where I drew most of my inspiration.

Type System Game Engines

A few of the utility types I used are from this article.

Rust

There aren't nearly as many resources about programming in the Rust typesystem (probably because, as I will explain shortly, it is significantly harder than typescript). The only one I found is this article which I believe is the original proof of the Turing Completeness of the Rust type system. I use the methodology described in this article.

Rust's Type System is Turing-Complete

Thoughts

Typescript

As esoteric program languages go TS types aren't really that bad. I would honestly hesitate to even call it a Turing-Tarpit; It's just a slightly strange functional program language. With let bindings, math, and a big speed increase it could be almost pleasant to use.

Rust

Rust types are a real pain to work with. To give a little taste: the Typescript program is about 900 lines of code (including the FEN parser) to the Rust version's 5000.

A few issues1 I encountered:

  • Every Computation has to be performed twice.

    Each time you use a trait you have to rewrite the same thing twice, once in the where clause and once in the output type. In fact, if you nest layers you have to specify each layer again. So Foo<Bar<Baz, Qaz>> requires, roughly, this.

    where Baz: RunBar<Qaz>,
          Bar<Baz, Qaz>: RunFoo,
    type Output = Foo<Bar<Baz, Qaz>>

    For some truly horifying examples of this look in move_gen::castle.

  • There is no form of negative reasoning. This results in the type RankEq (which checks if two ranks are equal) being implemented as a lookup table for all 64 combinations of two ranks.

  • Ironically there is no way to get the Rust compiler to understand that an enum is closed. So just becuase you implement something for colors White and Black doesn't mean the Rust compiler knows that you have implemented it for all implementers of the trait ColorEn. (Again1).

  • Since values can only exist in the top line of an impl block and not in the where clause, matching on a value requires introducing a new nested type every time. You end up with a lot of types like Foo and then FooWithValue and FooWithValueAndOtherValue. It gets grating fast.

  • Compile times are really slow.

In summary: unlike Typescript I would never like to program in the Rust typesystem again.

Footnotes

  1. It is probably unfair to call something that makes it difficult to program chess in your type system a problem. These features exist for sensible reasons in normal Rust. 2

You might also like...
Stockfish/ - UCI chess engine

Overview Stockfish is a free, powerful UCI chess engine derived from Glaurung 2.1. Stockfish is not a complete chess program and requires a UCI-compat

Play jungle chess on the linux terminal.
Play jungle chess on the linux terminal.

Jungle-Chess This is my first project written in Rust. Happy for contributors and feedback! The code is dirty. Play Jungle Chess on an Emoji-Enabled L

An (experimental) chess tactics trainer with spaced repetition
An (experimental) chess tactics trainer with spaced repetition

better-tactics A chess tactics trainer that with spaced repetition. New puzzles will be shown to you from the lichess puzzle db, according to your cal

a prototype crate for creating modular and performant 3D CPU particle systems, inspired by Unity's Shuriken Particle System.

bevy_prototype_particles This is a prototype crate for creating modular and performant 3D CPU particle systems, inspired by Unity's Shuriken Particle

This tool allows you to open one or more notebooks in Visual Studio and go hog wild exploring your systems in Bevy.
This tool allows you to open one or more notebooks in Visual Studio and go hog wild exploring your systems in Bevy.

Bevyrly Bevy is rly useful, but requires some hygiene! Pronounced as /ˈbɛvə(ɹ)li/, derives from Old English, combining befer ("beaver") and leah ("cle

A single-threaded polling-based Rust async executor suitable for use in games, embedded systems or WASM.

simple async local executor An Enlightware® software. Overview A single-threaded polling-based executor suitable for use in games, embedded systems or

A quick and dirty Space Invaders type game in Bevy, with attached tutorial.

This article is in-development and will be released in full form soon. It'll appear on Medium (my publisher likes that), with this as a the accompanyi

Utilities for creating strictly ordered execution graphs of systems for the Bevy game engine

bevy_system_graph This crate provides the utilities for creating strictly ordered execution graphs of systems for the Bevy game engine. Bevy Version S

Experimental type-safe geometric algebra for Rust

Projective Geometric Algebra This library is a Rust code generator, generating the mathematics you need for a geometric algebra library. I made it mos

Owner
null
🎮 A Realtime Multiplayer Server/Client Game example built entirely with Rust 🦀

Example of a ?? Realtime Multiplayer Web Game Server/Client built entirely using Rust ??

Nick Baker 5 Dec 17, 2022
A Chess Engine written in Rust that runs natively and on the web!

About The Project chess-rs is a Chess Engine written from scratch in Rust that runs natively and on web! Live Demo: https://parthpant.github.io/chess-

Parth Pant 109 Apr 6, 2023
A rust chess implementation using a neural network scoring function built on huggingface/candle + rust + wasm

Rusty Chess What is it? Rusty Chess aims to be a high quality embeddable chess engine that runs entirely locally in the browser (no backend required).

Gareth 3 Nov 3, 2023
Walleye is a chess engine written completely in rust.

Walleye is a UCI-compatible engine written using the classical alpha-beta style AI. It supports loading board positions from arbitrary FEN strings, Unicode pretty printing to the console, and UCI communication logs to help with debugging.

Mitchel Paulin 95 Dec 24, 2022
Blackmarlin is a chess engine fully written in Rust.

Blackmarlin WIP UCI Chess Engine Blackmarlin is a chess engine fully written in Rust. Make sure to compile the chess engine with cargo build --release

null 50 Oct 31, 2022
A basic web assembly chess engine written in rust.

This library is a basic implementation of a chess min-max algorithm with alpha-beta pruning (Algorithm Info). It is designed to be compiled down to WebAssembly and used for web applications.

null 2 Nov 25, 2022
Yet another shape chess game in Rust.

shape_chesss_in_rust Yet another shape chess game in Rust. Why the implementation is so slow? The main reason is performance of Vector iteration is ve

Simon Lee 1 Apr 10, 2022
A Chess Engine written in Rust .

Kelp Kelp is a UCI compatible chess engine written in Rust Using standard chess algorithms. Kelp is work in progress. Currently, it can be used as a U

Gautam 5 Sep 3, 2023
A dependency-free chess engine library built to run anywhere.

♔chess-engine♚ A dependency-free chess engine library built to run anywhere. Demo | Docs | Contact Me Written in Rust ?? ?? Why write a Chess engine?

adam mcdaniel 355 Dec 26, 2022
Opening randomizer for Chess

chess-randomizer chess-randomizer is a simple opening randomizer written in Rust using WASM and the Lichess API. To build the project, you need Rust a

null 1 Jan 31, 2022