分享
 
 
 

用逻辑类所作的一个反证法的实例

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Power Logic Applications

http://www3.sympatico.ca/qingzhehuang/LogicApp.htm

A.Fourth Edition

This is actually Fourth( Really? I don't remember clearly and who cares?) edition of my logic class. And it would be

more precise to call it an application. However, there is indeed some improvement with little code added but

powerful function achieved!( Really? I guess I won't believe what I said myself.)

B. The problem to solve:

I made little improvement to try to solve an logic proof in COMP238 sample test in other section. The problem is

like following:

//This is what we need to prove:

// [(p||t)&&(p-->q)&&(q-->r)&&(r-->s)&&(s-->t)] --> t

p,q,r,s,t are all propositional functions. And the problem suggests a proof by contradiction. Indeed, if you

don't go this way, it is quite difficult, at least very tedious.

C. What is my approach?

I changed my major function "doAnalysing()" a little bit by adding a parameter of bool byContradiction to make

it look like "void doAnalysing(bool byContradiction = false)". So, when you want to prove by contradiction,

simply pass a true parameter instead the default false parameter. Then in the implementation of the original

function, I only compare the in-passing parameter and change the last result to be false to begin an analysing

if it is "proof by contradiction". That is it.

However, at first the program doesn't give correct outcome except only show that "t is false and all other is

unknown". This is quite normal as when program pass only once in formula, it won't make full use of newly-deducted

result of "t is false". How to make program to invoke new result as long as he gets it? Simply redo when there is

new discovery. That is what I did in changing the function "analysing" of class "Analyse". I simply add a

recursive call of itself when "readStack()" return true which means the program did find new value and change it.

Originally I am so clever to write this "readStack()" as bool-return function. Now it proves it is worth the

efforts!

D. Major function:

A. void Analyse::analysing(bool byContradiction)

Keep finding the "result" type in OpRec(the stack) and call "readStack", doing the analysing. Have to clear stack for

next expression checking. When "readStack()" returns true, recursively call analysing itself to restart

analysing job.

(want to know more about Logic and Analyse class function? click here to see Power Logic.)

E. Moral:

Once our great leader Chairman Mao taught us that "theory cannot be tested and developed unless it is combined with practice".

So, all the program I wrote needs to be put into practice to get qualified and developed.

#include <iostream>

using namespace std;

const int StackLimit = 150;

enum LogicOp

{AND, OR, NOT, CONDITIONAL};

enum OpState

{Operand, Operator, Result};

enum LogicState

{Positive =1, Possible =0, Negative =-1};

bool operator == (LogicState self, LogicState dummy)

{

return ((self - dummy) == 0);

}

//forward declaration

class Analyse;

char* OpStr[4] = {" AND ", " OR ", " NOT "," CONDITIONAL "};

class Logic

{

private:

static Analyse* analyse;

static bool status;

static int logicCount;

char* express;

int index;

LogicState state;

void catName(char*, const char*, LogicOp);

static Logic* temp[100];

static int tempCount;

static Logic* TRUE;

static Logic* FALSE;

bool definedStatus;

LogicState And(LogicState self, LogicState dummy);

LogicState Or(LogicState self, LogicState dummy);

LogicState Not(LogicState self);

void pushStack(const Logic* self, const Logic* pDummy, LogicOp opCode);

Logic* getTemp() { return temp[tempCount -1];}

protected:

void initialize();

bool compare(const Logic& dummy);

void uninitialize();

public:

Analyse* getAnalyse();

Logic(const char* newExpress, const LogicState value=Possible);

~Logic();

Logic();

void prepare();

bool getStatus() const {return definedStatus;}

void setStatus(const bool newStatus) {definedStatus = newStatus;}

void setExpress(const char*);

char* getExpress() const {return express;}

void setIndex(const int newIndex) { index = newIndex;}

int getIndex() const {return index;}

void setState(LogicState newState) { state = newState;}

LogicState getState() const{ return state;}

static const int count() {return logicCount;}

Logic& operator&&(Logic& dummy);

Logic& operator||(Logic& dummy);

Logic& operator!();

bool operator==(const Logic& dummy);

Logic& operator&&(const bool value);

Logic& operator||(const bool value);

Logic& operator=(const Logic& dummy);

Logic& operator>>(Logic& dummy);

void doAnalysing(bool byContradiction = false);

};

union UNKNOWN

{

LogicOp operator = (LogicOp dummy);

Logic* operator = (const Logic* dummy);

Logic* logic;

LogicOp op;

};

struct OpRec

{

OpState opType;

union UNKNOWN unknown;

};

Logic* UNKNOWN::operator =(const Logic* dummy)

{

logic = (Logic*)dummy;

return logic;

}

LogicOp UNKNOWN::operator = (LogicOp dummy)

{

op = dummy;

return op;

}

class Analyse

{

private:

bool analyAnd(Logic* op1, Logic* op2, Logic* result);

bool analyOr(Logic* op1, Logic* op2, Logic* result);

bool analyConditional(Logic* op1, Logic* op2, Logic* result);

bool analyNot(Logic* op1, Logic* result);

static OpRec* opStack[StackLimit];

int top;

void clearStack();

LogicOp findOpCode(int index);

bool setNewOp(Logic* dummy, LogicState newValue);

int findPreResult(int index);

OpRec* getTop(){ if (top>0) return opStack[top -1]; else return NULL;}

public:

Analyse();

~Analyse();

void push(OpRec* newRec) {if (top<StackLimit) opStack[top++] = newRec;}

OpRec* pop() {if (top>0) return opStack[--top];}

bool readStack(int index);

void analysing(bool byContradiction);

};

char* logicStr[3] = {"True", "Unknown", "False"};

//This is what we need to prove:

// [(p||t)&&(p-->q)&&(q-->r)&&(r-->s)&&(s-->t)] --> t

const int ElementNum = 5;

enum ElementIndex {P,Q,R,S,T};

Logic element[ElementNum];

char* factStr[ElementNum] = {"p","q","r","s","t"};

Logic& formular();

void displayResult();

void setFactStr();

int main()

{

setFactStr();

formular().doAnalysing(true);

displayResult();

return 0;

}

Logic& formular()

{

return (((element[P]||element[T])&&(element[P]>>element[Q])&&(

element[Q]>>element[R])&&(element[R]>>element[S])&&(element[S]>>

element[T]))>>element[T]);

}

void setFactStr()

{

for (int i=0; i< ElementNum; i++)

{

element[i].setExpress(factStr[i]);

}

}

void displayResult()

{

for (int i=0; i< ElementNum; i++)

{

cout<<"\nThe logic expression '"<<factStr[i]<<" 'is ";

switch (element[i].getState())

{

case Positive:

cout<<logicStr[0];

break;

case Possible:

cout<<logicStr[1];

break;

case Negative:

cout<<logicStr[2];

break;

}

cout<<"\n";

}

}

Analyse* Logic::getAnalyse()

{

return analyse;

}

void Logic::doAnalysing(bool byContradiction)

{

getAnalyse()->analysing(byContradiction);

}

int Analyse::findPreResult(int index)

{

while (index > 0)

{

index -- ;

if (opStack[index]->opType== Result)

return index;

}

return -1;

}

void Analyse::analysing(bool byContradiction)

{

if (getTop()->opType!= Result)

{

cout<<"\nOpStack is not ended with Result:"<<endl;

return ;

}

else

{

int i = top -1;

if (!byContradiction)

{

opStack[i]->unknown.logic->setState(Positive);

}

else

{

opStack[i]->unknown.logic->setState(Negative);

}

while (i!= -1)

{

if (readStack(i))

{

analysing(byContradiction);

}

//readStack(i);

i = findPreResult(i);

}

}

clearStack();

}

LogicOp Analyse::findOpCode(int index)

{

int i = index;

while (opStack[i]->opType != Operator)

{

i--;

}

return opStack[i]->unknown.op;

}

bool Analyse::readStack(int index)

{

bool result= false;

LogicOp opCode;

if (opStack[index]->opType != Result )

{

cout<<"Stack error!\n";

return false;

}

opCode = findOpCode(index);

switch (opCode)

{

case AND:

result = analyAnd(opStack[index-3]->unknown.logic,

opStack[index - 1]->unknown.logic, opStack[index]->unknown.logic);

break;

case OR:

result = analyOr(opStack[index-3]->unknown.logic,

opStack[index - 1]->unknown.logic, opStack[index]->unknown.logic);

break;

case CONDITIONAL:

result = analyConditional(opStack[index-3]->unknown.logic,

opStack[index - 1]->unknown.logic, opStack[index]->unknown.logic);

break;

case NOT:

result = analyNot(opStack[index -1]->unknown.logic, opStack[index]->unknown.logic);

break;

}

return result;

}

bool Analyse::setNewOp(Logic* dummy, LogicState newValue)

{

if (dummy->getState()!= newValue)

{

if (dummy->getState() != Possible)

{

cout<<"\nYou are changing expression '"<<dummy->getExpress()<<"' value!";

return false;

}

else

{

dummy->setState(newValue);

return true;

}

}

else

return false;

}

bool Analyse::analyAnd(Logic* op1, Logic* op2, Logic* result)

{

bool newOp = false;

switch (result->getState())

{

case Positive:

if (op1->getState()==Negative||op2->getState()==Negative)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

newOp =setNewOp(op1, Positive) || setNewOp(op2 ,Positive);

break;

case Negative:

if (op1->getState()==Positive&&op2->getState() == Positive)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

if (op1->getState()==Positive)

{

newOp = setNewOp(op2, Negative);

}

else

{

if (op2->getState() == Positive)

{

newOp = newOp || setNewOp(op1, Negative);

}

}

break;

case Possible:

break;

}

return false;

}

bool Analyse::analyOr(Logic* op1, Logic* op2, Logic* result)

{

bool newOp = false;

switch(result->getState())

{

case Positive:

if (op1->getState()==Negative && op2->getState()==Negative)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

if (op1->getState() == Negative)

{

newOp = setNewOp(op2, Positive);

}

else

{

if (op2->getState() == Negative)

{

newOp = newOp || setNewOp(op1, Positive);

}

}

break;

case Negative:

if (op1->getState()==Positive || op2->getState()== Positive)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

break;

case Possible:

break;

}

return newOp;

}

bool Analyse::analyConditional(Logic* op1, Logic* op2, Logic* result)

{

bool newOp = false;

switch (result->getState())

{

case Positive:

if (op1->getState()==Positive&& op2->getState()== Negative)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

if (op1->getState()==Positive)

{

newOp = setNewOp(op2, Positive);

}

else

{

if (op2->getState() == Positive)

{

newOp = setNewOp(op1, Positive);

}

}

break;

case Negative:

if (op1->getState() == Negative||op2->getState() == Positive)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

newOp = setNewOp(op2, Negative);

newOp = newOp || setNewOp(op1, Positive);

break;

case Possible:

break;

}

return newOp;

}

bool Analyse::analyNot(Logic* op1, Logic* result)

{

bool newOp = false;

switch (result->getState())

{

case Positive:

if (op1->getState() == Positive)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

else

{

newOp = setNewOp(op1, Negative);

}

break;

case Negative:

if (op1->getState() == Negative)

{

cout<<"\nThere is contradictive at:"<<result->getExpress();

return false;

}

else

{

newOp = setNewOp(op1, Positive);

}

break;

case Possible:

break;

}

return newOp;

}

Analyse* Logic::analyse = new Analyse;

OpRec* Analyse::opStack[StackLimit];

void Analyse::clearStack()

{

for (int i =0; i< top; i++)

{

delete opStack[i];

}

top = 0;

}

Analyse::Analyse()

{

top = 0;

}

Analyse::~Analyse()

{

clearStack();

}

LogicState Logic::And(LogicState self, LogicState dummy)

{

if (self!=dummy)

{

return (LogicState)(self*dummy);

}

else

{

return self;

}

}

LogicState Logic::Or(LogicState self, LogicState dummy)

{

return ((self>= dummy)?self: dummy);

}

LogicState Logic::Not(LogicState self)

{

return (LogicState)(-1* self);

}

void Logic::prepare()

{

for (int i=0; i< tempCount; i++)

{

free(temp[i]);

}

tempCount =0;

}

Logic& Logic::operator >>(Logic& dummy)

{

char buffer[256];

catName(buffer, dummy.getExpress(), CONDITIONAL);

temp[tempCount] = new Logic(buffer);

temp[tempCount]->setState(Or(Not(state), dummy.getState()));

tempCount++;

pushStack(this, &dummy, CONDITIONAL);

return *temp[tempCount-1];

}

void Logic::uninitialize()

{

status = true;

for (int i=0; i< tempCount;i++)

{

delete temp[i];

}

}

bool Logic::status = false;

Logic* Logic::temp[100] = {NULL}; // = new Logic;

int Logic::tempCount = 0;

Logic* Logic::FALSE = new Logic("FALSE", Negative);

Logic& Logic::operator =(const Logic& dummy)

{

setExpress(dummy.getExpress());

setState(dummy.getState());

return *this;

}

Logic* Logic::TRUE = new Logic("TRUE", Positive);;

Logic& Logic::operator &&(const bool value)

{

if (value)

{

return (*this)&&(*TRUE);

}

else

{

return (*this)&&(*FALSE);

}

}

Logic& Logic::operator ||(const bool value)

{

if (value)

{

return (*this)||(*TRUE);

}

else

{

return (*this)||(*FALSE);

}

}

bool Logic::operator ==(const Logic& dummy)

{

return compare(dummy);

}

bool Logic::compare(const Logic& dummy)

{

return (index==dummy.getIndex());

}

void Logic::catName(char* buffer, const char* second, LogicOp opcode)

{

strcpy(buffer, getExpress());

strcat(buffer, OpStr[opcode]);

strcat(buffer, second);

}

Logic& Logic::operator !()

{

char buffer[256];

OpRec* ptr;

strcpy(buffer, OpStr[NOT]);

strcat(buffer, getExpress());

temp[tempCount] = new Logic(buffer);

temp[tempCount]->setState(Not(getState()));

tempCount++;

ptr = new OpRec;

ptr->opType = Operator;

ptr->unknown = NOT;

analyse->push(ptr);

ptr = new OpRec;

ptr->opType = Operand;

ptr->unknown = this;

analyse->push(ptr);

ptr = new OpRec;

ptr->opType = Result;

ptr->unknown = temp[tempCount-1];

analyse->push(ptr);

return *temp[tempCount-1];

}

void Logic::pushStack(const Logic* self, const Logic* pDummy, LogicOp opCode)

{

OpRec* ptr;

ptr = new OpRec;

ptr->opType = Operand;

ptr->unknown = self;

analyse->push(ptr);

ptr = new OpRec;

ptr->opType = Operator;

ptr->unknown = opCode;

analyse->push(ptr);

ptr = new OpRec;

ptr->opType = Operand;

ptr->unknown = pDummy;

analyse->push(ptr);

ptr = new OpRec;

ptr->opType = Result;

ptr->unknown = getTemp();

analyse->push(ptr);

}

Logic& Logic::operator &&(Logic& dummy)

{

char buffer[256];

catName(buffer, dummy.getExpress(), AND);

temp[tempCount] = new Logic(buffer);

temp[tempCount]->setState(And(getState(),dummy.getState()));

tempCount++;

pushStack(this, &dummy, AND);

return *temp[tempCount-1];

}

Logic& Logic::operator ||(Logic& dummy)

{

char buffer[256];

catName(buffer, dummy.getExpress(), OR);

temp[tempCount] = new Logic(buffer);

temp[tempCount]->setState(Or(getState(),dummy.getState()));

tempCount++;

pushStack(this, &dummy, OR);

return *temp[tempCount-1];

}

int Logic::logicCount =0;

void Logic::initialize()

{

express = NULL;

state = Possible;

definedStatus = false;

setIndex(logicCount);

logicCount++;

status = false;

}

Logic::~Logic()

{

if (!status) //if you destroy one, then you destroy all!!!!!

{

uninitialize();

}

if (express!=NULL)

{

free(express);

}

}

Logic::Logic(const char* newExpress, const LogicState value)

{

initialize();

setExpress(newExpress);

setState(value);

}

Logic::Logic()

{

initialize();

}

void Logic::setExpress(const char* newExpress)

{

int i;

i = strlen(newExpress) +1;

if (express==NULL)

{

express = (char*)malloc(i);

}

else

{

express = (char*)realloc((void*)express, i);

}

strcpy(express, newExpress);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有