Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. def substitute(f, substitution):
  2.     if substitution is None:
  3.         return None
  4.     if is_variable(f) and (get_name(f) in substitution):
  5.         return substitute(substitution[get_name(f)], substitution)
  6.     if has_args(f):
  7.         return replace_args(f, [substitute(arg, substitution) for arg in get_args(f)])
  8.     return f
  9.  
  10. # checks if v occurs in t
  11. def occur_check(v, t, subst):
  12.     if v == t:
  13.         return True
  14.     if is_variable(t) and get_name(t) in subst:
  15.         return occur_check(v, substitute(t, subst), subst)
  16.     if is_function_call(t):
  17.         for arg in get_args(t):
  18.             val = occur_check(v, arg, subst)
  19.             if val:
  20.                 return True
  21.     return False
  22.  
  23. # unifies f1 and f2 under a known substitution
  24. def unify(f1, f2, subst = None):
  25.     if subst is None:
  26.         subst = {}
  27.     S = []
  28.     S.append((f1, f2))
  29.     while len(S) > 0:
  30.         (s, t) = S.pop()
  31.         while is_variable(s) and get_name(s) in subst:
  32.             s = substitute(s, subst)
  33.         while is_variable(t) and get_name(t) in subst:
  34.             t = substitute(t, subst)
  35.         if s != t:
  36.             if is_variable(s):
  37.                 if occur_check(s, t, subst):
  38.                     return False
  39.                 else:
  40.                     subst[get_name(s)] = t
  41.             elif is_variable(t):
  42.                 if occur_check(t, s, subst):
  43.                     return False
  44.                 else:
  45.                     subst[get_name(t)] = s
  46.             elif has_args(s) and has_args(t):
  47.                 s_args = get_args(s)
  48.                 t_args = get_args(t)
  49.                 if get_head(s) == get_head(t) and len(s_args) == len(t_args):
  50.                     for i in range(len(s_args)):
  51.                         S.append((s_args[i], t_args[i]))
  52.                 else:
  53.                     return False
  54.             else:
  55.                 return False
  56.     return subst
  57.  
  58. # prints formulas in a human-readable format
  59. def print_formula(f, return_result = False):
  60.     ret = ""
  61.     if is_term(f):
  62.         if is_constant(f):
  63.             ret += str(get_value(f))
  64.         elif is_variable(f):
  65.             ret += "?" + get_name(f)
  66.         elif is_function_call(f):
  67.             ret += str(get_head(f)) + "[" + "".join([print_formula(arg, True) + "," for arg in get_args(f)])[:-1] + "]"
  68.         else:
  69.             ret += "???"
  70.     elif is_atom(f):
  71.         ret += str(get_head(f)) + "(" + "".join([print_formula(arg, True) + ", " for arg in get_args(f)])[:-2] + ")"
  72.     elif is_sentence(f):
  73.         args = get_args(f)
  74.         if len(args) == 1:
  75.             ret += str(get_head(f)) + print_formula(args[0], True)
  76.         else:
  77.             ret += "(" + str(get_head(f)) + "".join([" " + print_formula(arg, True) for arg in get_args(f)]) + ")"
  78.     else:
  79.         ret += "???"
  80.     if return_result:
  81.         return ret
  82.     print(ret)
  83.     return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement