Exotic Logics

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Jun 17 04:28:20 EDT 2009


On Tue, 16 Jun 2009 22:46:14 -0700, William Clifford wrote:

> I was staring at a logic table the other day, and I asked myself, "what
> if one wanted to play with exotic logics; how might one do it?"


First question: what's an exotic logics?

Do you mean things like three-value logic, fuzzy logic, probabilistic 
reasoning, etc?

Or do you mean logical operators others than and, or, xor, nand, nor, etc?

Or both? Something else?



> I did
> some searching but not being too sure of what to look for and carried
> away with my own enthusiasm for the idea, I didn't find much. What I've
> come up with is, no doubt, inefficient, naive, poor python style, and
> probably wrong in fundamental ways too subtle for me, but here it is:
> 
> import itertools
> import operator
> 
> class Logic():
>     """class of generic logic operators"""
[...]

If (nearly) all your methods are static methods, chances are a class is 
the wrong solution. Why not just define the static methods as top-level 
functions?


> This seemed to be working for the limited tests I did on it, while I was
> doing them. The following checked out last time I tried:
> 
>>>> and_ = Logic(2, 8)
>>>> or_ = Logic(2, 14)
>>>> xor = Logic(2, 6)
> 
> I have no idea how to validate the results of the trinary and beyond
> logics.

The same way you would validate and_, or_ and xor: to validate something, 
you need to know what the correct answer is, then compare the answer you 
get with the answer you should get. For a binary operator and binary 
operands, you can exhaustively check every valid input:

flag1 op flag2

there are four combinations of input flags: 

0 op 0
0 op 1
1 op 0
1 op 1

For three-valued logic, there are 9 combinations. For four-valued, 16. So 
exhaustively testing all the possible inputs is quite simple.

As far as getting the correct answers, you have to decide which three-
valued logic(s) you want to use, then go look them up. Or just make them 
up yourself!

E.g. given trinary flags 0, 1, 2, (say), you might want the operators to 
give the same results with 0, 1 as they would if you were applying it to 
binary flags 0, 1. E.g.:

# binary operands
0 and 0 => 0
0 and 1 => 0
1 and 0 => 0
1 and 1 => 1

# trinary operands

0 and 0 => 0
0 and 1 => 0
0 and 2 => ?  # probably 0
1 and 0 => 0
1 and 1 => 1
1 and 2 => ?  # could be 0, or maybe 1
2 and 0 => ?  # probably 0
2 and 1 => ?  # 0 or 1
2 and 2 => ?  # probably 2

Different choices lead to different versions of ternary logic.




> Thanks for reading and trying this out. Corrections? Criticism?
> Comments?

You're implementation seems rather confusing. I think that's partly 
because you use lots of abbreviated jargon terms that mean little or 
nothing to me: rdx, opr (operator?), lsd, pute.




-- 
Steven



More information about the Python-list mailing list