Crates.io | junglefowl |
lib.rs | junglefowl |
version | 0.1.1 |
source | src |
created_at | 2023-05-11 10:39:30.690859 |
updated_at | 2023-05-12 03:12:42.542204 |
description | Brutally murdering Rust's type system one proof at a time. |
homepage | |
repository | https://github.com/wrsturgeon/junglefowl |
max_upload_size | |
id | 861978 |
size | 17,873 |
Junglefowl runs Peano arithmetic on Rust types, verified at compile time.
So we can do theoretically hard stuff, like these const-generic slices:
use junglefowl::*;
// Accept only `u8` arrays with exactly 3 elements:
fn picky<T: Nest<Element = u8, Length = peano!(3)>>(_: &T) {}
// Create an array with 5 elements:
let n12345 = nest![1, 2, 3, 4, 5];
// Split it after its second element without changing anything in memory:
let (left, right) = split!(n12345, 2);
// And we can prove that the second segment will have exactly two elements:
picky(&right);
// picky(&left); // won't compile!
// And know exactly what its elements are:
assert_eq!(nest![3, 4, 5], right);
Here's our Peano encoding:
0 <--> ()
1 <--> ((), ())
2 <--> ((), ((), ()))
3 <--> ((), ((), ((), ())))
Note that, thanks to a clever abuse of Rust's syntax, these are both types and values.
Next, there's a macro so you can forget what you just read:
peano!(0);
--> ()
peano!(42);
--> ((), ((), ((), ((), ((), ((), ((), ((), ((), ...)))))))))
Note that this macro expands to a type, so you would use it like this:
let x: peano!(42) = todo!();
instead of like this:
let x = peano!(42); // bad!
And next, there's a hell of a lot of other stuff, but instead of explaining it all, watch this compile:
static_assert_eq!(peano!(39), sub!(peano!(42), peano!(3)));
expands to
enum False {} // uninstantiable type
// this part vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv evaluates to zero when the two sides are equal
const _: [False; (peano!(39) != sub!(peano!(42), peano!(3))) as usize] = [];
// ... which makes the list length zero, which matches the right-hand side (and couldn't be nonzero since its members are uninstantiable)
// learned the list length trick from the `static_assertions` crate, so all credit there!
Expanding the interesting part above (and inverting so !=
becomes ==
):
peano!(39) == < peano!(42) as peano::Sub< peano!(3) >>::Difference;
peano!(39) == <((), ((), ((), peano!(39)))) as peano::Sub<((), ((), ((), ())))>::Difference;
Here's the definition of peano::Sub
, pretty representative for most operations in this crate:
pub trait Sub<R: peano::N>: peano::N { type Difference: peano::N; } // sealed trait
impl<T: peano::N> Sub<()> for T { type Difference = Self; } // subtracting zero is our super-simple base case
impl<L: peano::N + Sub<R>, R: peano::N> Sub<((), R)> for ((), L) { type Difference = sub!(L, R); } // otherwise, reduce the problem until it's dividing by zero
Begin reduction!
peano!(39) == <((), ((), ((), peano!(39)))) as peano::Sub<((), ((), ((), ())))>::Difference;
peano!(39) == < ((), ((), peano!(39))) as peano::Sub< ((), ((), ())) >::Difference;
peano!(39) == < ((), peano!(39)) as peano::Sub< ((), ()) >::Difference;
peano!(39) == < peano!(39) as peano::Sub< () >::Difference;
peano!(39) == peano!(39) ;
et voila!
A certain well-known theorem prover is named after the French word for cock rooster (coq), so I Googled "rooster" and found (to my amazement!) that they belong to the junglefowl species.
This name sounded suitably cool.