1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
use crate::{
boolean::{And, False, True},
define,
primitives::{Composed, Constant, Identity},
Function,
};
/// Church numeral for zero.
pub type Zero = crate::primitives::SecondOf;
define! {
/// Successor function. Returns a number plus one.
/// ```text
/// λn.λf.λx.f(nfx)
/// ```
pub fn Successor ::= {
N. F. X. { F, { N, F, X }}
} where
N: F,
{N, F}: X,
F: {N, F, X};
/// Adds two church numerals.
/// ```text
/// S ::= Successor
/// λm.λn.mSn
/// ```
pub fn Add ::= { M. N. { M, Successor, N }} where
M: Successor,
{M, Successor}: N;
/// Multiplies two church numerals - this is simply composition.
pub fn Multiply ::= { X. Y. (Composed<X, Y>) };
/// Predecessor function. Gets the number below a given church numeral.
/// ```text
/// λn.λf.λx. n (λg.λh. h (g f)) (λu.x) (λu.u)
/// ```
pub fn Predecessor ::= { N. F. X. { N, Pred_1<F>, Constant<X>, Identity }} where
N: (Pred_1<F>),
{N, Pred_1<F>}: (Constant<X>),
{N, Pred_1<F>, Constant<X>}: Identity;
fn Pred_1<F> ::= { G. H. { H, { G, F }}} where
H: {G, F},
G: F;
/// Subtracts two church numerals.
/// ```text
/// P ::= Predecessor
/// λm.λn.nPm
/// ```
pub fn Subtract ::= { M. N. { N, Predecessor, M }} where
N: Predecessor,
{N, Predecessor}: M;
/// Raises a numeral to the power of another.
/// ```text
/// λm.λn.nm
/// ```
pub fn Exponent ::= { M. N. { N, M }} where N: M;
/// Returns whether a number is zero.
/// ```text
/// F ::= False
/// T ::= True
/// λn.n(λx.F)T
/// ```
pub fn IsZero ::= { N. { N, Constant<False>, True }} where
N: (Constant<False>),
{N, Constant<False>}: True;
/// Returns whether one number is less than or equal to another.
/// ```text
/// ? ::= IsZero
/// - ::= Subtract
/// λm.λn.?(-mn)
/// ```
pub fn Leq ::= { M. N. { IsZero, { Subtract, M, N }}} where
Subtract: M,
{Subtract, M}: N,
IsZero: { Subtract, M, N };
/// Returns whether two numbers are equal.
/// ```text
/// & ::= And
/// ≤ ::= Leq
/// λm.λn.&(≤mn)(≤nm)
/// ```
pub fn Eq ::= { M. N. { And, { Leq, M, N }, { Leq, N, M }}} where
Leq: M, Leq: N,
{Leq, M}: N, {Leq, N}: M,
And: {Leq, M, N},
{And, { Leq, M, N }}: { Leq, N, M };
/// Converts a church numeral to a constant number. See [`ConstNumber`].
/// ```text
/// λn.n(λ{X}.{X + 1}){0}
/// ```
@[cfg(any(doc, feature = "const-numeral"))]
pub fn ToNumber ::= { N. { N, ConstIncrement, ConstNumber<0> }} where
N: ConstIncrement,
ConstIncrement: (ConstNumber<0>),
{N, ConstIncrement}: (ConstNumber<0>);
}
// Due to const generics, this has to be explicitly declared.
/// Increments a [`ConstNumber`] by one. Used to define [`ToNumber`].
/// ```text
/// λ{X}.{X + 1}
/// ```
#[cfg(any(doc, feature = "const-numeral"))]
pub struct ConstIncrement;
/// Constant number returned by converting a church numeral.
#[cfg(any(doc, feature = "const-numeral"))]
pub struct ConstNumber<const N: u64>;
#[cfg(any(doc, feature = "const-numeral"))]
impl<const N: u64> Function<ConstNumber<N>> for ConstIncrement
where
ConstNumber<{ N + 1 }>: Sized,
{
type Output = ConstNumber<{ N + 1 }>;
}
#[cfg(any(doc, feature = "const-numeral"))]
impl<const N: u64> ConstNumber<N> {
/// Extracts the number argument from a constant number.
#[inline]
pub const fn value() -> u64 { N }
}
#[cfg(test)]
mod test {
#![allow(dead_code)]
use crate::prelude::*;
type Two = call!{Successor, { Successor, Zero }};
type Three = call!{Successor, Two};
#[test]
fn main() {
let _: call!{
Eq, {Multiply, Two, Three}, {Add, Three, Three}
} = <True>::default();
}
}