Simple logical inference system: resolution and model checking for first-order
logic.
# @author Percy Liang
import collections
# Recursively apply str inside map
def rstr(x):
if isinstance(x, tuple): return str(tup
...
Simple logical inference system: resolution and model checking for first-order
logic.
# @author Percy Liang
import collections
# Recursively apply str inside map
def rstr(x):
if isinstance(x, tuple): return str(tuple(map(rstr, x)))
if isinstance(x, list): return str(map(rstr, x))
if isinstance(x, set): return str(set(map(rstr, x)))
if isinstance(x, dict):
newx = {}
for k, v in x.items():
newx[rstr(k)] = rstr(v)
return str(newx)
return str(x)
class Expression:
# Helper functions used by subclasses.
def ensureType(self, arg, wantedType):
if not isinstance(arg, wantedType):
raise Exception('%s: wanted %s, but got %s' %
(self.__class__.__name__, wantedType, arg))
return arg
def ensureFormula(self, arg): return self.ensureType(arg, Formula)
def ensureFormulas(self, args):
for arg in args: self.ensureFormula(arg)
return args
def isa(self, wantedType): return isinstance(self, wantedType)
def join(self, args): return ','.join(str(arg) for arg in args)
def __eq__(self, other): return str(self) == str(other)
def __hash__(self): return hash(str(self))
# Cache the string to be more efficient
def __repr__(self):
if not self.strRepn: self.strRepn = self.computeStrRepn()
return self.strRepn
# A Formula represents a truth value.
class Formula(Expression): pass
# A Term coresponds to an object.
class Term(Expression): pass
# Variable symbol (must start with '$')
# Example: $x
class Variable(Term):
def __init__(self, name):
if not name.startswith('$'): raise Exception('Variable must start with
"$", but got %s' % name)
self.name = name
self.strRepn = None
def computeStrRepn(self): return self.name
[Show More]