Jump to content

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 a){ return [=](auto x) { return xa; }; };
};
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){
return a([](auto) { return False; })(True);
}
 
Line 309 ⟶ 314:
};
}
)([=](auto) { return x; })([](auto y){ return y; });
};
};
Line 317 ⟶ 322:
auto Subtract(auto a, auto b) {
{
auto a_minus_b =return b([](auto c){ return Predecessor(c); })(a);
 
// Each lambda has its own type which gives '3-1' a different
// type than '4-2'. Normalize the number based on successors to 0.
return a_minus_b([=](auto f) {return Successor(f);})(Zero());
};
}
Line 327 ⟶ 328:
namespace
{
// helper functions for division. These funtions are only
// visible in this source file
 
// end the recusrion
auto Divr(decltype(Zero()), auto) {
return Zero();
}
 
// count how many times b can be subtracted from a
auto Divr(auto a, auto b) {
returnauto a_minus_b = Successor(Divr(Subtract(a, b), 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(Subtract(Successor(a), b), b);
}
 
// 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 nineten = ToChurch<910>();
auto tenthousand = SuccessorExp(nineten, three);
 
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 4 * 3 = " << ToInt(Multiply(four, three));
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 94 - 63 = " << ToInt(Subtract(ninefour, sixthree));
std::cout << "\n 93 - 94 = " << ToInt(Subtract(ninethree, ninefour));
std::cout << "\n 6 / 3 = " << ToInt(Divide(six, three));
std::cout << "\n 3 / 6 = " << ToInt(Divide(three, six));
auto looloolooo = Add(Exp(tenthousand, ninethree), Add(Exp(ten, six), Exp(ten, three)thousand));
auto looloolool = Successor(looloolooo);
std::cout << "\n 10^9 + 10^6 + 10^3 + 1 = " << ToInt(looloolool) << "\n";
 
// calculate the golden ratio by using a Church numeral to
// apply the funtion '1 + 1/x' a thousand times
std::cout << "\n 4golden + 3ratio = " << ToInt(Add(four, three));
thousand([](double x){ return 1.0 + 1.0 / x; })(1.0) << "\n";
}</syntaxhighlight>
{{out}}
<pre>
3 + 4 = 7
4 + 3 = 7
3 * 4 = 12
4 * 3 = 12
3^4 = 81
4^3 = 64
0^0 = 1
94 - 63 = 31
93 - 94 = 0
6 / 3 = 2
3 / 6 = 0
10^9 + 10^6 + 10^3 + 1 = 1001001001
golden ratio = 1.61803
</pre>
 
125

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.