Discussion:
Truth-table generator with source code
(too old to reply)
Mike Carroll
2003-12-21 22:14:10 UTC
Permalink
Hi,

I'm looking for a program that will generate truth tables for formulas
of standard propositional logic. I need source code I can modify so
it will generate tables for the non-standard logic I'm investigating.

It needs to be a Windows program. Delphi is the programming language
I'm most comfortable with, but I can get by in Java or C++.

I don't plan to redistribute the program.

E-mail replies preferred as I don't normally monitor this group.

Thanks much.

Mike Carroll
***@pobox.com
David C. Ullrich
2003-12-22 13:43:48 UTC
Permalink
Post by Mike Carroll
Hi,
I'm looking for a program that will generate truth tables for formulas
of standard propositional logic. I need source code I can modify so
it will generate tables for the non-standard logic I'm investigating.
It needs to be a Windows program. Delphi is the programming language
I'm most comfortable with, but I can get by in Java or C++.
I'm a big fan of Delphi. I wouldn't use it here: you'd need to parse
expressions yourself somehow, and also if you do things the way
I think they should be done (with a class hierarchy more or less
as in the demo below) you need to keep track of which objects
should "own" which child objects to make certain everything
gets freed properly.

In a language like Python you can do some magical things.
In the code below P and Q are Variable objects, hence Wff
objects, and it follows that P&Q _is_ a Conjunction object -
you don't need to parse anything, Python does it for you.

Again, for emphasis: we're not going to parse the string
'P&Q' into the appropriate object - instead we set things
up so that P&Q _is_ the appropriate object!

Below is a rudimentary demo that prints the truth table

P=0, Q=0: (P&Q)=0
P=0, Q=1: (P&Q)=0
P=1, Q=0: (P&Q)=0
P=1, Q=1: (P&Q)=1.

Python is keen: I have a lot of logic code that does much more
than what's below, in much more sophisticated ways. Looking
at it, trying to extract a suitable simple demo seemed like it
would be a little work. But one of the keennesses of Python
is that it's so easy to write - instead I wrote the code below
starting from scratch, took less than a half hour and worked
the first time (modulo a few typos):

class Wff:
"""Wff is an abstract class - you never create a Wff object,
just instances of descendant class. But the Wff class still does
some important work - see the comments on the __and__ method"""


def value(self, assignment):
"""You shouldn't be asking for the value of a Wff, instead
you should be asking for values of descendants like Variable
and Conjunction:"""
raise 'abstract error'

def __and__(self, other):
"""This is the magical part: '__and__' is a special method name.
If P and Q are Wff objects then P&Q is whatever this method returns!
So we make it return a Conjunction object.

The other methods below do the same for the operators |, ~ and >>.
You need to implement corresponding classes..."""

return Conjunction(self, other)

def __or__(self, other):
return Disjunction(self, other)

def __invert__(self):
return Negation(self)

def __rshift__(self, other):
return Implication(self, other)

class Variable(Wff):

def __init__(self, name):
self.name = name
self._value = 0

def value(self):
"""We need a value() method - the value() of a Conjunction
is the conjunction of the values()'s, for example. You can _set_
the value of a Variable by setting the _value field.

Note that needing a _value field plus a value() method is
inelegant. I'm still using an old version of Python - I
gather that newer version have _properties_ with get and
set methods, just as in Delphi, so you could just give
Wff a value property and implement appropriate get and set
methods in descendants, much nicer."""
return self._value

def __repr__(self):
"""Another special method name - __repr__ says how Variable
should print itself"""

return self.name

class Conjunction(Wff):

def __init__(self, left, right):
self.left = left
self.right = right

def value(self):
return self.left.value() & self.right.value()

def __repr__(self):
return '(%s&%s)' % (self.left, self.right)

#That's the classes we need, now the demo:

P = Variable('P')
Q = Variable('Q')

#Now P and Q are Variable objects. Magic starts here:

w = P&Q

#Because of the way we wrote Wff.__and__, w is now a
#Conjunction object. You can set the _value of P and
#Q and when you do that w.value() returns what it should.
#A rudimentary truth table:

for j in range(2):
#Ie, "for j - 0 to 1:"
for k in range(2):
P._value = j
Q._value = k
print '%s=%s, %s=%s: %s=%s' % (P,P.value(), Q,Q.value(),
w,w.value())
Post by Mike Carroll
I don't plan to redistribute the program.
E-mail replies preferred as I don't normally monitor this group.
Thanks much.
Mike Carroll
************************

David C. Ullrich
Mike Carroll
2003-12-23 00:37:08 UTC
Permalink
One starting place is Brian Borowski's Truth Table Constructor Java
applet, available for download at:
http://sciris.shu.edu/~borowski/Truth/

Borland's JBuilder Personal addition is also available via download
at:
http://www.borland.com/products/downloads/download_jbuilder.html

I'd still be interested in hearing about other programs available for
download.

Mike Carroll
***@pobox.com

Loading...