...is basically a Prolog.
With weird syntax.
struct
s
//! Example: Kleene Logic
pub struct True;
pub struct False;
pub struct Unknown;
impl Not for True {
type Output = False; // associated Type
fn not(self) -> Self::Output { unreachable!() }
}
impl Not for False { .. } // Output = True
impl Not for Unknown { .. } // Output = Unknown
impl<X> BitAnd<X> for True {
type Output = X; // generic result type
fn and(self, other: X) -> Self::Output {
unreachable!()
}
}
impl<X> BitAnd<X> for False {
type Output = False; // fixed result type
fn and(self, other: X) -> Self::Output { .. }
}
// non-generic impls for Unknown omitted
<True as Not>::Output // == False
<X as BitAnd<Y>>::Output // == X ∧ Y
<<X as BitOr<Y>>::Output as Not>::Output // == ¬(X ∨ Y)
PhantomData<_>
: bind atoms together
// typenum, src/uint.rs (note the generic types!)
pub struct UInt<U, B> {
_marker: PhantomData<(U, B)>
}
UInt
is zero-sized, but has two component types.
9: • ← 1 ← 0 ← 0 ← 1
Same
/ Cmp
missing impls are type errors
pub trait Same<Rhs = Self> {
type Output;
}
impl<T> Same<T> for T {
type Output = T;
}
pub enum Ternary { T, F, U }
pub trait TernaryType {
fn to_ternary() -> Ternary;
}
impl TernaryType for True {
#[inline] fn to_ternary() -> Ternary { Ternary::T }
}
...
impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero>
Add<PInt<Ur>> for PInt<Ul> {
type Output = PInt<<Ul as Add<Ur>>::Output>; // …
}
leads to
src/uint.rs:241:1: 247:2 error: the trait `core::ops::Add<Ur>`
is not implemented for the type `Ul` [E0277]
src/uint.rs:241 impl<Ul: Unsigned, Ur: Unsigned> Add<Ul, …
where
.
/// From typenum's src/int.rs: `P(Ul) + P(Ur) = P(Ul + Ur)`
impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero>
Add<PInt<Ur>> for PInt<Ul>
where Ul: Add<Ur>,
<Ul as Add<Ur>>::Output: Unsigned + NonZero {
type Output = PInt<<Ul as Add<Ur>>::Output>;
fn add(self, _: PInt<Ur>) -> Self::Output { .. }
}
But: overuse of where clauses can lead to exponential compile time! (#26325)
replace generic impls with (multiple) concrete typed impls
private intermediate traits can help
Did I mention compiletest is awesome?
Build code and run