Church numerals: Difference between revisions
→{{header|C++}}: tweeks; clean-up
(→{{header|C++}}: Add subtraction and division) |
(→{{header|C++}}: tweeks; clean-up) |
||
Line 263:
// apply the function zero times (return an identity function)
auto Zero = [](auto){ return [](auto x){ return x; }; };
return [](auto) {▼
// define Church True and False
auto True = [](auto
auto False = [](auto){ return [](auto b){ return b; }; };
}▼
// apply the function f one more time
Line 297:
auto Exp(auto a, auto b) {
return b(a);
▲}
// check if a number is zero
auto IsZero(auto a){
}
Line 309 ⟶ 314:
};
}
)([=](auto)
};
};
Line 317 ⟶ 322:
auto Subtract(auto a, auto b) {
{
};
}
Line 327 ⟶ 328:
namespace
{
// helper functions for division.
// end the recusrion
auto Divr(decltype(Zero
return Zero
}
// count how many times b can be subtracted from a
auto Divr(auto a, auto b) {
auto isZero = IsZero(a_minus_b);
// normalize all Church zeros to be the same (intensional equality).
// In this implemetation, Church numerals have extensional equality
// but not intensional equality. '6 - 3' and '4 - 1' have extensional
// equality because they will both cause a function to be called
// three times but due to the static type system they do not have
// intensional equality. Internally the two numerals are represented
// by different lambdas. Normalize all Church zeros (1 - 1, 2 - 2, etc.)
// to the same zero (Zero) so it will match the function that end the
// recursion.
return isZero
(Zero)
(Successor(Divr(isZero(Zero)(a_minus_b), b)));
}
}
Line 343 ⟶ 357:
// apply the function a / b times
auto Divide(auto a, auto b) {
return Divr
}
// create a Church numeral from an integer at compile time
template <int N> constexpr auto ToChurch() {
if constexpr(N<=0) return Zero
else return Successor(ToChurch<N-1>());
}
Line 354 ⟶ 368:
// use an increment function to convert the Church number to an integer
int ToInt(auto church) {
return church([](int n){ return n + 1; })(0);
}
int main() {
// show some examples
auto zero = Zero
auto three = Successor(Successor(Successor(zero)));
auto four = Successor(three);
auto six = ToChurch<6>();
auto
auto
std::cout << "\n 3 + 4 = " << ToInt(Add(three, four));
std::cout << "\n 4 + 3 = " << ToInt(Add(four, three));▼
std::cout << "\n 3 * 4 = " << ToInt(Multiply(three, four));
std::cout << "\n 3^4 = " << ToInt(Exp(three, four));
std::cout << "\n 4^3 = " << ToInt(Exp(four, three));
std::cout << "\n 0^0 = " << ToInt(Exp(zero, zero));
std::cout << "\n
std::cout << "\n
std::cout << "\n 6 / 3 = " << ToInt(Divide(six, three));
std::cout << "\n 3 / 6 = " << ToInt(Divide(three, six));
auto looloolooo = Add(Exp(
auto looloolool = Successor(looloolooo);
std::cout << "\n 10^9 + 10^6 + 10^3 + 1 = " << ToInt(looloolool)
// calculate the golden ratio by using a Church numeral to
// apply the funtion '1 + 1/x' a thousand times
thousand([](double x){ return 1.0 + 1.0 / x; })(1.0) << "\n";
}</syntaxhighlight>
{{out}}
<pre>
3 + 4 = 7
3 * 4 = 12
3^4 = 81
4^3 = 64
0^0 = 1
6 / 3 = 2
3 / 6 = 0
10^9 + 10^6 + 10^3 + 1 = 1001001001
golden ratio = 1.61803
</pre>
|