四种布尔逻辑
四种布尔逻辑归纳
引言
简单的布尔逻辑或亚里士多德逻辑是众所周知的,并且在西方世界被广泛使用。信息技术的发展意识到布尔逻辑对于某些任务来说是不够的。因此,这导致了三值布尔逻辑。这种实现的例子可以在 boost 库中找到。
本质
如果我们仔细观察,会发现三值布尔逻辑操作三个条件:真、假和另外一种情况。更精确地说,这种另外的情况既不是真也不是假。根据逻辑归纳,很明显三值布尔逻辑是不完整的,它需要一个额外的条件:真和假都存在。
我不是在发明什么。四种布尔逻辑是亚洲佛教的常见情况,虽然这对于西方世界来说似乎很突出。
有一个古老的禅宗谜题打破了二元布尔亚里士多德逻辑,这个问题是关于半满或半空的玻璃杯。二元逻辑无法提供答案,而对于四种布尔逻辑来说,这很简单。
以下是四种布尔逻辑的四个条件
- 纯真
- 纯假
- 既非真也非假
- 真且假
我说的是条件,不是状态。状态可以是无限的。这四个条件使布尔逻辑情况完整。无需添加或删除任何内容。
示例
four_boolean.cpp
#include <stdint.h>
#include <inttypes.h>
#include <iostream>
/**
* Let's take a function that implements sending data
*/
uint8_t send_data(
void* data // data to send
, uint32_t len // length of the data
);
/**
* let's suppose the function returning code looks like this:
*
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | bit's number
* ---------------------------------
* | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
* | | |
* ` ----------------------' `--- 1 = true (succeeded in sending)
* | 0 = false (failed to send)
* `------------------ Error code
*/
/**
* some helper C++ classes
*/
namespace four_boolean
{
struct resolver
{
resolver (const uint8_t n) : _n(n) {}
bool is_true() const { return _n & 0x01; } // if 0's bit is set - true
bool is_false() const { return _n & 0xFE ? 1 : 0; } // if there is an error code - false
uint8_t err_code() const { return _n >> 1; }
uint8_t _n;
};
typedef enum BOOL_tag {
TRUE = 0x01 // pure TRUE
, FALSE = 0x02 // pure FALSE
, TRUE_AND_FALSE = TRUE | FALSE // And TRUE and FALSE
, NTRUE_NFALSE = TRUE & FALSE // Neither TRUE nor FALSE
} BOOL; // this is our four-boolean type
template <class R> BOOL booler(const R& r) {
return static_cast<BOOL>((r.is_false() << 1) | r.is_true());
}
}
/**
* For testing purposes let's redefine send_data function
*/
uint8_t send_data_test(
uint8_t retv // ret value
, void* data // data to send
, uint32_t len // length of the data
)
{
return retv;
}
void print_four_boolean(const uint8_t retv)
{
four_boolean::resolver r(retv);
switch (four_boolean::booler(r)) {
case four_boolean::TRUE:
std::cout
<< "Pure True;"
<< " data were sent - no errors;"
<< " Err Code: " << (int)r.err_code() << std::endl;
break;
case four_boolean::FALSE:
std::cout
<< "Pure FALSE;"
<< " data were not sent due to:"
<< " Err code: " << (int)r.err_code() << std::endl;
break;
case four_boolean::TRUE_AND_FALSE:
std::cout
<< "And True and False;"
<< " data were sent, but there were some errors:"
<< " Err Code: " << (int)r.err_code() << std::endl;
break;
case four_boolean::NTRUE_NFALSE:
std::cout
<< "Neither True Nor False;"
<< " something unpredictable happened."
<< " it is not possible to make a statement on this;"
<< " Err Code: " << (int)r.err_code() << std::endl;
break;
}
}
int main()
{
uint8_t retv = 23;
std::cout << retv;
/**
* case 1
* pure TRUE
*/
print_four_boolean(send_data_test(1, 0, 0));
/**
* case 2
* pure FALSE
* 34 means err code 17
*/
print_four_boolean(send_data_test(34, 0, 0));
/**
* case 3
* AND TRUE and FALSE
* 99 means err code 49
*/
print_four_boolean(send_data_test(99, 0, 0));
/**
* case 4
* Neither TRUE nor FALSE
*/
print_four_boolean(send_data_test(0, 0, 0));
return 0;
}
输出
$ g++ ./four-boolean.cpp -o ./four-boolean
$ ./four-boolean
Pure True; data were sent - no errors; Err Code: 0
Pure FALSE; data were not sent due to: Err code: 17
And True and False; data were sent, but there were some errors: Err Code: 49
Neither True Nor False; something unpredictable happened.
It is not possible to make a statement on this; Err Code: 0