Saturday, October 29, 2005

Propositional logic in Python

Propositional logic inference via compressed truth tables. Module entail is presumably slower than your industrial-grade C inference algorithm, but logical statements are given as Python expressions, and entailment can be checked at the interactive prompt. This makes the module easy to use, and fun. (Source Code).

7 comments:

Connelly Barnes said...

Example usage

>>> S = AssumptionSet()
>>> S.assume('a => b')
>>> S.assume('b => c')
>>> S.assume('c => a')
>>> S.implies('a <=> b')
True
>>> S.implies('a')
False
>>> S.assume('a')
>>> S.implies('c')
True

Connelly Barnes said...

See also my post "intelligent minesweeper" [1], based on this module.

أيفون said...

this very professionalism thanks

Dave said...

>>> import entail
>>> Socrates = entail.AssumptionSet()
>>> Socrates.assume('A=>B')
>>> Socrates.implies('A')
False
>>> Socrates.assume('~A')
>>> Socrates.implies('~B')
True
>>>
Error?
Denying the antecedent

Connelly Barnes said...

Hey Dave, you should use 'not A' and 'not B' then it works correctly.

Anonymous said...

Does your library allow for the "AND" and "OR" operators or nesting?

For instance, could I express a proposition like any of the below:

A & B & C => X
A | B | C => X
A & (B | C) => X

I like the sample usage, by the way. Lean and easy to read.

Anonymous said...

I should add that I've never written a line of python in my life, but that I've been searching for a lightweight propositional logic system for some time.

I also, spoke before I looked at the code (big mistake). It does look like it supports nesting, AND, and OR (at least from viewing the unit tests).

It does make me wonder, though. Is there a way to retract assumptions? For instance, can I "S.assume('a')" then later "S.unassume('a')"?