473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Program to prove logical validity

I'm writing a program to calc truth tables of arguments to prove that
they are logically valid. Currently, I have to tell the program to
print a truth table, and check it by hand to prove it's valid. I'm
tired of doing this. I need to write a fn that, given three
functions/one function and two variables or anyother combo, run through
all four rows, and determine if it's valid. An argument is invalid iff
any row has true premises and a false conclusion or vice versa.

Here's a truth table for an invalid arg:

Denying the Antecedent T-Table:
A B A->B ~A .:~B
--------------------------------------------------------------
1 1 1 0 0
0 1 1 1 0
1 0 0 0 1
0 0 1 1 1

And here's my code:

//operators.hpp

#pragma once
bool NOT(bool A);
bool AND(bool A,bool B);
bool OR(bool A, bool B);
bool XOR(bool A, bool B);
bool NAND(bool A,bool B);
bool NOR(bool A, bool B);
bool XNOR(bool A, bool B);
bool IF(bool A, bool B);
bool IMP(bool A, bool B);
bool NIF(bool A, bool B);
bool NIMP(bool A, bool B);

//operators.cpp

#include "operators.hpp"

bool NOT(bool A)
{
if(A==true)
return false;
else return true;
}
bool AND(bool A,bool B)
{
if(A==true&&B==true)
return true;
else if(A==false&&B==true)
return false;
else if(A==true&&B==false)
return false;
else return false;
}
bool OR(bool A, bool B)
{
if(A==true&&B==true)
return true;
else if(A==false&&B==true)
return true;
else if(A==true&&B==false)
return true;
else return false;
}
bool XOR(bool A, bool B)
{
if(A==true&&B==true)
return false;
else if(A==false&&B==true)
return true;
else if(A==true&&B==false)
return true;
else return false;
}
bool NAND(bool A,bool B){return NOT(AND(A,B));}
bool NOR(bool A, bool B){return NOT(OR(A,B));}
bool XNOR(bool A, bool B){return NOT(XOR(A,B));}
bool IF(bool A, bool B){return NOT(AND(A,NOT(B)));}
bool IMP(bool A, bool B){return NOT(AND(B,NOT(A)));}
bool NIF(bool A, bool B){return NOT(IF(A,B));}
bool NIMP(bool A, bool B){return NOT(IMP(A,B));}

//main.cpp

#include "operators.hpp"
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
bool A=true,B=true;
cout << "A" << " " << "B" << " " << "A->B" <<" " << "A" << ".:B" <<
endl;
cout << "----------------------" << endl;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=false;B=true;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=true;B=false;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=false;B=false;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Could you please tell me how to write an IsValid fn? Thanks!!!!!!!

Mar 8 '06 #1
6 2706
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I'm writing a program to calc truth tables of arguments to prove that
they are logically valid. Currently, I have to tell the program to
print a truth table, and check it by hand to prove it's valid. I'm
tired of doing this. I need to write a fn that, given three
functions/one function and two variables or anyother combo, run through
all four rows, and determine if it's valid. An argument is invalid iff
any row has true premises and a false conclusion or vice versa.

Here's a truth table for an invalid arg:

Denying the Antecedent T-Table:
A B A->B ~A .:~B
--------------------------------------------------------------
1 1 1 0 0
0 1 1 1 0
1 0 0 0 1
0 0 1 1 1

And here's my code:

//operators.hpp

#pragma once
bool NOT(bool A);
bool AND(bool A,bool B);
bool OR(bool A, bool B);
bool XOR(bool A, bool B);
bool NAND(bool A,bool B);
bool NOR(bool A, bool B);
bool XNOR(bool A, bool B);
bool IF(bool A, bool B);
bool IMP(bool A, bool B);
bool NIF(bool A, bool B);
bool NIMP(bool A, bool B);

//operators.cpp

#include "operators.hpp"

bool NOT(bool A)
{
if(A==true)
return false;
else return true;
}
bool AND(bool A,bool B)
{
if(A==true&&B==true)
return true;
else if(A==false&&B==true)
return false;
else if(A==true&&B==false)
return false;
else return false;
}
bool OR(bool A, bool B)
{
if(A==true&&B==true)
return true;
else if(A==false&&B==true)
return true;
else if(A==true&&B==false)
return true;
else return false;
}
bool XOR(bool A, bool B)
{
if(A==true&&B==true)
return false;
else if(A==false&&B==true)
return true;
else if(A==true&&B==false)
return true;
else return false;
}
bool NAND(bool A,bool B){return NOT(AND(A,B));}
bool NOR(bool A, bool B){return NOT(OR(A,B));}
bool XNOR(bool A, bool B){return NOT(XOR(A,B));}
bool IF(bool A, bool B){return NOT(AND(A,NOT(B)));}
bool IMP(bool A, bool B){return NOT(AND(B,NOT(A)));}
bool NIF(bool A, bool B){return NOT(IF(A,B));}
bool NIMP(bool A, bool B){return NOT(IMP(A,B));}

//main.cpp

#include "operators.hpp"
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
bool A=true,B=true;
cout << "A" << " " << "B" << " " << "A->B" <<" " << "A" << ".:B" <<
endl;
cout << "----------------------" << endl;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=false;B=true;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=true;B=false;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=false;B=false;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Could you please tell me how to write an IsValid fn? Thanks!!!!!!!


I don't understand why you are using functions such as NOT, AND, OR, etc..
Why aren't you using the bool operators ~ & | etc...?

Lets take your function:
bool NOT(bool A)
{
if(A==true)
return false;
else return true;
}

This can be simplified to:

bool NOT( bool A )
{
return !A;
}

and further simplyfied by just using
!A

instead of calling the function.

Anyway, on to your question... You want a function to take a series of
functions and do some logic. Well, all your functions return a bool. So
you might as just well accept bools.

I'm really not sure what you're looking for, though. Is it something like
this?

bool IsValid( bool A, bool B, bool C, bool D )
{
// Do whatever checking you want here and just either
return true;
// or
return false;
}

I *really* don't understand what you are trying to do so can't be more
helpful.
Mar 8 '06 #2

Jim Langston wrote:
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I'm writing a program to calc truth tables of arguments to prove that
they are logically valid. Currently, I have to tell the program to
print a truth table, and check it by hand to prove it's valid. I'm
tired of doing this. I need to write a fn that, given three
functions/one function and two variables or anyother combo, run through
all four rows, and determine if it's valid. An argument is invalid iff
any row has true premises and a false conclusion or vice versa.

Here's a truth table for an invalid arg:

Denying the Antecedent T-Table:
A B A->B ~A .:~B
--------------------------------------------------------------
1 1 1 0 0
0 1 1 1 0
1 0 0 0 1
0 0 1 1 1

And here's my code:

//operators.hpp

#pragma once
bool NOT(bool A);
bool AND(bool A,bool B);
bool OR(bool A, bool B);
bool XOR(bool A, bool B);
bool NAND(bool A,bool B);
bool NOR(bool A, bool B);
bool XNOR(bool A, bool B);
bool IF(bool A, bool B);
bool IMP(bool A, bool B);
bool NIF(bool A, bool B);
bool NIMP(bool A, bool B);

//operators.cpp

#include "operators.hpp"

bool NOT(bool A)
{
if(A==true)
return false;
else return true;
}
bool AND(bool A,bool B)
{
if(A==true&&B==true)
return true;
else if(A==false&&B==true)
return false;
else if(A==true&&B==false)
return false;
else return false;
}
bool OR(bool A, bool B)
{
if(A==true&&B==true)
return true;
else if(A==false&&B==true)
return true;
else if(A==true&&B==false)
return true;
else return false;
}
bool XOR(bool A, bool B)
{
if(A==true&&B==true)
return false;
else if(A==false&&B==true)
return true;
else if(A==true&&B==false)
return true;
else return false;
}
bool NAND(bool A,bool B){return NOT(AND(A,B));}
bool NOR(bool A, bool B){return NOT(OR(A,B));}
bool XNOR(bool A, bool B){return NOT(XOR(A,B));}
bool IF(bool A, bool B){return NOT(AND(A,NOT(B)));}
bool IMP(bool A, bool B){return NOT(AND(B,NOT(A)));}
bool NIF(bool A, bool B){return NOT(IF(A,B));}
bool NIMP(bool A, bool B){return NOT(IMP(A,B));}

//main.cpp

#include "operators.hpp"
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
bool A=true,B=true;
cout << "A" << " " << "B" << " " << "A->B" <<" " << "A" << ".:B" <<
endl;
cout << "----------------------" << endl;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=false;B=true;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=true;B=false;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
A=false;B=false;
cout << A << " " << B << " " << IF(A,B) << " " << A << " " << B <<
endl;
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Could you please tell me how to write an IsValid fn? Thanks!!!!!!!


I don't understand why you are using functions such as NOT, AND, OR, etc..
Why aren't you using the bool operators ~ & | etc...?

Lets take your function:
bool NOT(bool A)
{
if(A==true)
return false;
else return true;
}

This can be simplified to:

bool NOT( bool A )
{
return !A;
}

and further simplyfied by just using
!A

instead of calling the function.

Anyway, on to your question... You want a function to take a series of
functions and do some logic. Well, all your functions return a bool. So
you might as just well accept bools.

I'm really not sure what you're looking for, though. Is it something like
this?

bool IsValid( bool A, bool B, bool C, bool D )
{
// Do whatever checking you want here and just either
return true;
// or
return false;
}

I *really* don't understand what you are trying to do so can't be more
helpful.


Take a beginner's course on propositional logic. Find someone from
sci.logic to help us out. And those & | ops are bitwise, not logical.
The reason I made fns for NOT, AND, OR, and XOR is for symmetry. Anyone
else here know how to help? Thanks!!!!!

Mar 8 '06 #3
> And those & | ops are bitwise, not logical.

Then how about && and ||?

Ben
Mar 8 '06 #4

benben wrote:
And those & | ops are bitwise, not logical.


Then how about && and ||?

Ben


First, can we try answering the original question?

Mar 8 '06 #5
Protoman wrote:
benben wrote:
And those & | ops are bitwise, not logical.

Then how about && and ||?

Ben


First, can we try answering the original question?


In all the times you've posted here and been told how to post, you still
cannot.

You have misquoted benben.

IMO what benben wrote is acceptable. IMO what Jim wrote is acceptable.
Yes, | and & are bitwise. You're using bools, how many bits does a
bool represent? Does it matter that the operators are bitwise?

Try reading the FAQ again on how to post. And try using some
indentation in your code.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 8 '06 #6
On Tue, 07 Mar 2006 20:41:33 -0800, Protoman wrote:
I'm writing a program to calc truth tables of arguments to prove that they
are logically valid. Currently, I have to tell the program to print a
truth table, and check it by hand to prove it's valid. I'm tired of doing
this. I need to write a fn that, given three functions/one function and
two variables or anyother combo, run through all four rows, and determine
if it's valid. An argument is invalid iff any row has true premises and a
false conclusion or vice versa.


The most general way to do this is by building a data structure to
represent the boolean expressions. The "real" C++ way to build them is as
a set of classes derived from an abstract base class:

class BoolExp {
public:
virtual bool evaluate(bool *vars) = 0;
};

class Var : public BoolExp {
public:
Var(int n) : var_num(n) {}
bool evaluate(bool *vars) { return vars[var_num]; }
private:
int var_num;
};

class NotExp : public BoolExp {
public:
NotExp(BoolExp *e) : exp(e) {}
~NotExp() { delete exp; }
bool evaluate(bool *vars) { return !exp->evaluate(vars); }
protected:
BoolExp *exp;
};

class BinaryExp : public BoolExp {
public:
BinaryExp(BoolExp *e1, BoolExp *e2) : exp1(e1), exp2(e2) {}
~BinaryExp() { delete exp1; delete exp2; }
protected:
BoolExp *exp1, *exp2;
};

class AndExp : public BinaryExp {
public:
AndExp(BoolExp *e1, BoolExp *e2) : BinaryExp(e1, e2) {}
bool evaluate(bool *vars) {
return exp1->evaluate(vars) && exp2->evaluate(vars);
}
};

class OrExp : public BinaryExp {
public:
OrExp(BoolExp *e1, BoolExp *e2) : BinaryExp(e1, e2) {}
bool evaluate(bool *vars) {
return exp1->evaluate(vars) || exp2->evaluate(vars);
}
};

class NandExp : public NotExp {
public:
NandExp(BoolExp *e1, BoolExp *e2) : NotExp(new AndExp(e1, e2)) {}
};

class NorExp : public NotExp {
public:
NorExp(BoolExp *e1, BoolExp *e2) : NotExp(new OrExp(e1, e2)) {}
};

and so on for any other basic terms you want to define. This looks like a
lot of code, but it is mostly syntactic noise required to establish these
very simple classes. You may wan to include shorthand for the variables
you use a lot:

// Shorthands for A, B etc:

class VarA : public Var {
public:
VarA() : Var(0) {}
};

class VarB : public Var {
public:
VarB() : Var(1) {}
};

Now you can write expressions like this:

BoolExp *exp = new AndExp(new NotExp(new VarA), new VarB);

Given an array of values for the variables (2 in this case) like this:

bool values[2] = { 1, 0 };

you can enquire the value of the expression using:

exp->evaluate(values);

I won't go much further, but to test my syntax a wrote a quite function to
tabulate the value of an expression which is very similar to the function
you want to write, so I include it here:

void tabulate(int n_vars, BoolExp *exp)
{
bool increment(int n, bool *bp);

bool *vars = new bool[n_vars];
int i;
for (i = 0; i < n_vars; i++)
vars[i] = 0;
for (i = 0; i < n_vars; i++)
std::cout << " " << char('A' + i);
std::cout << std::endl;
do {
for (int i = 0; i < n_vars; i++)
std::cout << " " << vars[i];
std::cout << " " << exp->evaluate(vars) << std::endl;
} while (increment(n_vars, vars));
}

bool increment(int n, bool *bp)
{
int i;
for (i = 0; i < n && bp[i]; i++)
bp[i] = 0;
return i < n ? (bp[i] = 1) : 0;
}

Is this getting near your goal?

--
Ben.
Mar 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Aaron Watters | last post by:
I'm doing a heart/lung bypass procedure on a largish Python program at the moment and it prompted the thought that the methodology I'm using would be absolutely impossible with a more "type safe"...
4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
52
by: piaseckiac | last post by:
I am producing a website on air and need a link to change the entire website from standard to metric for temperature, pressure, miles-kilometers, and volume. Thank you.
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
51
by: John Baker | last post by:
Hi: Some time ago I developed a program in Access, and separated the database and the program itself (using the normal access tools).We have the db on our server and the programin the desktop...
40
by: findmadhav | last post by:
I need a program in C (something like a TSR) which will automatically press the function key F6, say about every 5 seconds. Can anyone provide me with an exe of such a program? Thanks in advance.
27
by: cj | last post by:
I run this program and to exit click the X in the upper right corner. But apparently it isn't really ending the program. If I return to VB and make changes then try to rebuild the app it says the...
7
by: Bobo | last post by:
Does anyone have experience with copywriting a program? I'm working on a program that I want to protect somewhat. It's more the finished idea than the source code but that too. Do I need to see...
9
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.