Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c57b54

Browse files
committed
More logic.py cleanup, functionally neutral.
1 parent c831e43 commit 7c57b54

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

logic.py

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ def is_literal(s):
287287
>>> is_literal(expr('x')) # XXX I guess this is intended?
288288
True
289289
"""
290+
#XXX return is_symbol(s.op) or (s.op == '~' and is_literal(s.args[0]))
290291
return is_symbol(s.op) or (s.op == '~' and is_symbol(s.args[0].op))
291292

292293
def literals(s):
@@ -372,7 +373,7 @@ def tt_check_all(kb, alpha, symbols, model):
372373
if not symbols:
373374
if pl_true(kb, model): return pl_true(alpha, model)
374375
else: return True
375-
assert result != None
376+
assert result is not None
376377
else:
377378
P, rest = symbols[0], symbols[1:]
378379
return (tt_check_all(kb, alpha, rest, extend(model, P, True)) and
@@ -385,11 +386,8 @@ def prop_symbols(x):
385386
elif is_prop_symbol(x.op):
386387
return [x]
387388
else:
388-
s = set(())
389-
for arg in x.args:
390-
for symbol in prop_symbols(arg):
391-
s.add(symbol)
392-
return list(s)
389+
return list(set(symbol for arg in x.args
390+
for symbol in prop_symbols(arg)))
393391

394392
def tt_true(alpha):
395393
"""Is the sentence alpha a tautology? (alpha will be coerced to an expr.)
@@ -412,31 +410,31 @@ def pl_true(exp, model={}):
412410
return model.get(exp)
413411
elif op == '~':
414412
p = pl_true(args[0], model)
415-
if p == None: return None
413+
if p is None: return None
416414
else: return not p
417415
elif op == '|':
418416
result = False
419417
for arg in args:
420418
p = pl_true(arg, model)
421-
if p == True: return True
422-
if p == None: result = None
419+
if p is True: return True
420+
if p is None: result = None
423421
return result
424422
elif op == '&':
425423
result = True
426424
for arg in args:
427425
p = pl_true(arg, model)
428-
if p == False: return False
429-
if p == None: result = None
426+
if p is False: return False
427+
if p is None: result = None
430428
return result
431429
p, q = args
432430
if op == '>>':
433431
return pl_true(~p | q, model)
434432
elif op == '<<':
435433
return pl_true(p | ~q, model)
436434
pt = pl_true(p, model)
437-
if pt == None: return None
435+
if pt is None: return None
438436
qt = pl_true(q, model)
439-
if qt == None: return None
437+
if qt is None: return None
440438
if op == '<=>':
441439
return pt == qt
442440
elif op == '^':
@@ -589,7 +587,7 @@ def disjuncts(s):
589587
#______________________________________________________________________________
590588

591589
def pl_resolution(KB, alpha):
592-
"Propositional Logic Resolution: say if alpha follows from KB. [Fig. 7.12]"
590+
"Propositional-logic resolution: say if alpha follows from KB. [Fig. 7.12]"
593591
clauses = KB.clauses + conjuncts(to_cnf(~alpha))
594592
new = set()
595593
while True:
@@ -623,19 +621,18 @@ def pl_resolve(ci, cj):
623621
#______________________________________________________________________________
624622

625623
class PropHornKB(PropKB):
626-
"A KB of Propositional Horn clauses."
624+
"A KB of propositional Horn clauses."
627625

628626
def tell(self, sentence):
629-
"Add a Horn Clauses to this KB."
627+
"Add a Horn clause to this KB."
630628
op = sentence.op
631-
assert op == '>>' or is_prop_symbol(op), "Must be Horn Clause"
629+
assert op == '>>' or is_prop_symbol(op), "Must be Horn clause" # XXX use is_definite_clause?
632630
self.clauses.append(sentence)
633631

634632
def ask_generator(self, query):
635-
"Yield the empty substitution if KB implies query; else False"
636-
if not pl_fc_entails(self.clauses, query):
637-
return
638-
yield {}
633+
"Yield the empty substitution if KB implies query."
634+
if pl_fc_entails(self.clauses, query):
635+
yield {}
639636

640637
def retract(self, sentence):
641638
"Remove the sentence's clauses from the KB"
@@ -644,16 +641,17 @@ def retract(self, sentence):
644641
self.clauses.remove(c)
645642

646643
def clauses_with_premise(self, p):
647-
"""The list of clauses in KB that have p in the premise.
644+
"""Return a list of the clauses in KB that have p in their premise.
648645
This could be cached away for O(1) speed, but we'll recompute it."""
649646
return [c for c in self.clauses
650-
if c.op == '>>' and p in conjuncts(c.args[0])]
647+
if c.op == '>>' and p in conjuncts(c.args[0])] # XXX use parse_definite_clause?
651648

652649
def pl_fc_entails(KB, q):
653650
"""Use forward chaining to see if a HornKB entails symbol q. [Fig. 7.14]
654651
>>> pl_fc_entails(Fig[7,15], expr('Q'))
655652
True
656653
"""
654+
# XXX use parse_definite_clause?
657655
count = dict([(c, len(conjuncts(c.args[0]))) for c in KB.clauses
658656
if c.op == '>>'])
659657
inferred = DefaultDict(False)
@@ -714,7 +712,7 @@ def dpll(clauses, symbols, model):
714712
P, value = find_unit_clause(clauses, model)
715713
if P:
716714
return dpll(clauses, removeall(P, symbols), extend(model, P, value))
717-
P = symbols.pop()
715+
P = symbols.pop() # XXX is this side-effect more global than desired?
718716
return (dpll(clauses, symbols, extend(model, P, True)) or
719717
dpll(clauses, symbols, extend(model, P, False)))
720718

@@ -784,8 +782,9 @@ def WalkSAT(clauses, p=0.5, max_flips=10000):
784782
raise NotImplementedError
785783
model[sym] = not model[sym]
786784

787-
785+
#______________________________________________________________________________
788786
# PL-Wumpus-Agent [Fig. 7.19]
787+
789788
class PLWumpusAgent(agents.Agent):
790789
"An agent for the wumpus world that does logical inference. [Fig. 7.19]"""
791790
def __init__(self):
@@ -807,7 +806,7 @@ def program(percept):
807806
if KB.ask('~P_%d,%d & ~W_%d,%d' % (i, j, i, j)) != False:
808807
raise NotImplementedError
809808
KB.ask('~P_%d,%d | ~W_%d,%d' % (i, j, i, j)) != False
810-
if action == None:
809+
if action is None:
811810
action = random.choice(['Forward', 'Right', 'Left'])
812811
return action
813812

@@ -831,7 +830,7 @@ def unify(x, y, s):
831830
>>> ppsubst(unify(x + y, y + C, {}))
832831
{x: y, y: C}
833832
"""
834-
if s == None:
833+
if s is None:
835834
return None
836835
elif x == y:
837836
return s
@@ -865,30 +864,19 @@ def unify_var(var, x, s):
865864
def occur_check(var, x, s):
866865
"""Return true if variable var occurs anywhere in x
867866
(or in subst(s, x), if s has a binding for x)."""
868-
869867
if var == x:
870868
return True
871869
elif is_variable(x) and s.has_key(x):
872-
return occur_check(var, s[x], s) # fixed
873-
# What else might x be? an Expr, a list, a string?
870+
return occur_check(var, s[x], s)
874871
elif isinstance(x, Expr):
875-
# Compare operator and arguments
876872
return (occur_check(var, x.op, s) or
877873
occur_check(var, x.args, s))
878-
elif isinstance(x, list) and x != []:
879-
# Compare first and rest
880-
return (occur_check(var, x[0], s) or
881-
occur_check(var, x[1:], s))
874+
elif isinstance(x, list):
875+
return any(occur_check(var, element, s)
876+
for element in x)
882877
else:
883-
# A variable cannot occur in a string
878+
assert isinstance(x, (str, int)), x
884879
return False
885-
886-
#elif isinstance(x, Expr):
887-
# return var.op == x.op or occur_check(var, x.args)
888-
#elif not isinstance(x, str) and issequence(x):
889-
# for xi in x:
890-
# if occur_check(var, xi): return True
891-
#return False
892880

893881
def extend(s, var, val):
894882
"""Copy the substitution s and extend it by setting var to val;

0 commit comments

Comments
 (0)