FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fixed error in unify/occur_check · SharanyaVR/aima-python@8b8527c · GitHub

Repository navigation

Commit 8b8527c

Browse files
authored andcommitted
Fixed error in unify/occur_check
1 parent 61f4730 commit 8b8527c

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

‎logic.py‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,10 @@ def unify(x, y, s):
728728
elif isinstance(x, Expr) and isinstance(y, Expr):
729729
return unify(x.args, y.args, unify(x.op, y.op, s))
730730
elif isinstance(x, str) or isinstance(y, str) or not x or not y:
731-
return if_(x == y, s, None)
731+
# orig. return if_(x == y, s, None) but we already know x != y
732+
return None
732733
elif issequence(x) and issequence(y) and len(x) == len(y):
734+
# Assert neither x nor y is []
733735
return unify(x[1:], y[1:], unify(x[0], y[0], s))
734736
else:
735737
return None
@@ -741,24 +743,43 @@ def is_variable(x):
741743
def unify_var(var, x, s):
742744
if var in s:
743745
return unify(s[var], x, s)
744-
elif occur_check(var, x):
746+
elif occur_check(var, x, s):
745747
return None
746748
else:
747749
return extend(s, var, x)
748750

749-
def occur_check(var, x):
750-
"Return true if var occurs anywhere in x."
751+
def occur_check(var, x, s):
752+
"""Return true if variable var occurs anywhere in x
753+
(or in subst(s, x), if s has a binding for x)."""
754+
751755
if var == x:
752756
return True
757+
elif is_variable(x) and s.has_key(x):
758+
return occur_check(var, s[x], s) # fixed
759+
# What else might x be? an Expr, a list, a string?
753760
elif isinstance(x, Expr):
754-
return var.op == x.op or occur_check(var, x.args)
755-
elif not isinstance(x, str) and issequence(x):
756-
for xi in x:
757-
if occur_check(var, xi): return True
758-
return False
761+
# Compare operator and arguments
762+
return (occur_check(var, x.op, s) or
763+
occur_check(var, x.args, s))
764+
elif isinstance(x, list) and x != []:
765+
# Compare first and rest
766+
return (occur_check(var, x[0], s) or
767+
occur_check(var, x[1:], s))
768+
else:
769+
# A variable cannot occur in a string
770+
return False
771+
772+
#elif isinstance(x, Expr):
773+
# return var.op == x.op or occur_check(var, x.args)
774+
#elif not isinstance(x, str) and issequence(x):
775+
# for xi in x:
776+
# if occur_check(var, xi): return True
777+
#return False
759778

760779
def extend(s, var, val):
761-
"""Copy the substitution s and extend it by setting var to val; return copy.
780+
"""Copy the substitution s and extend it by setting var to val;
781+
return copy.
782+
762783
>>> extend({x: 1}, y, 2)
763784
{y: 2, x: 1}
764785
"""

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL