Exemplo n.º 1
0
def func_lt(rules, term, env):
    if term.arity != 2: return None
    left = rules.evaluate(term.args[0], env)
    if terms.is_unbound(left): return term
    right = rules.evaluate(term.args[1], env)
    if terms.is_unbound(right): return term
    return left < right
Exemplo n.º 2
0
    def test_term(self):
        self.assertEquals('andrew', self.term1.pred)
        self.assertEquals('andrew', self.term2.pred)
        self.assertEquals('andrew', self.term3.pred)

        self.assertEquals(False, terms.is_const(self.term1))
        self.assertEquals(True, terms.is_bound(self.term1))
        self.assertEquals(False, terms.is_variable(self.term1))
        self.assertEquals(True, terms.is_term(self.term1))
        self.assertEquals(False, terms.is_unbound(self.term1))
        self.assertEquals(False, terms.is_list(self.term1))
    
        self.assertEquals(False, terms.is_const(self.term2))
        self.assertEquals(False, terms.is_bound(self.term2))
        self.assertEquals(False, terms.is_variable(self.term2))
        self.assertEquals(True, terms.is_term(self.term2))
        self.assertEquals(True, terms.is_unbound(self.term2))
        self.assertEquals(False, terms.is_list(self.term2))
    
        self.assertEquals(False, terms.is_const(self.term3))
        self.assertEquals(True, terms.is_bound(self.term3))
        self.assertEquals(False, terms.is_variable(self.term3))
        self.assertEquals(True, terms.is_term(self.term3))
        self.assertEquals(False, terms.is_unbound(self.term3))
        self.assertEquals(False, terms.is_list(self.term3))
Exemplo n.º 3
0
def pred_isnot(rules, term, env):
    if term.arity != 2: return False

    left = rules.evaluate(term.args[0], env)
    right = rules.evaluate(term.args[1], env)

    if terms.is_unbound(left): return False
    if terms.is_unbound(right): return False

    return left != right
Exemplo n.º 4
0
def pred_extract(rules, term, env):
    if term.arity != 3: return False

    left = terms.expand(term.args[0], env)
    middle = terms.expand(term.args[1], env)
    right = terms.expand(term.args[2], env)

    if terms.is_unbound(middle):
        return False

    if not terms.is_list(middle): return False

    if terms.is_bound(right):
        # l,m,r const
        if not terms.is_list(right): return False
        if terms.is_bound(left):
            for (i, e) in enumerate(middle):
                if e == left:
                    rl = middle[0:i] + middle[i + 1:]
                    return rl == right
            return False
        # r,m const l variable
        r = []
        for (i, e) in enumerate(middle):
            env_copy = env.copy()
            if terms.unify(e, env, left, env_copy):
                rl = middle[0:i] + middle[i + 1:]
                if rl == right:
                    r.append(env_copy)
        return r

    # right variable
    if terms.is_unbound(left):
        # r,l var m const
        r = []
        for (i, e) in enumerate(middle):
            env_copy = env.copy()
            if terms.unify(e, env, left, env_copy):
                rl = middle[0:i] + middle[i + 1:]
                if terms.unify(rl, env, right, env_copy):
                    r.append(env_copy)
        return tuple(r)

    # l,m const r var
    r = []
    for (i, e) in enumerate(middle):
        if e == left:
            rl = middle[0:i] + middle[i + 1:]
            env_copy = env.copy()
            if terms.unify(rl, env, right, env_copy):
                r.append(env_copy)

    return tuple(r)
Exemplo n.º 5
0
 def test_variable(self):
     self.assertEquals(False, terms.is_const(self.variable))
     self.assertEquals(False, terms.is_bound(self.variable))
     self.assertEquals(True, terms.is_variable(self.variable))
     self.assertEquals(False, terms.is_term(self.variable))
     self.assertEquals(True, terms.is_unbound(self.variable))
     self.assertEquals(False, terms.is_list(self.variable))
Exemplo n.º 6
0
def pred_meta(engine, term, env):
    if term.arity != 3: return False

    t = terms.expand(term.args[0], env)
    p = terms.expand(term.args[1], env)
    a = terms.expand(term.args[2], env)

    if terms.is_unbound(t):
        if terms.is_unbound(p) or terms.is_unbound(a):
            return False
        if not isinstance(p, str) or not terms.is_list(a):
            return False
        x = terms.make_term(p, *a)
        return terms.unify(x, {}, t, env)

    e = env.copy()
    if terms.unify(t.pred, {}, p, e) and terms.unify(t.args, {}, a, e):
        env.update(e)
        return True

    return False
Exemplo n.º 7
0
def pred_last(rules, term, env):
    if term.arity != 3: return False

    left = terms.expand(term.args[0], env)
    middle = terms.expand(term.args[1], env)
    right = terms.expand(term.args[2], env)

    if terms.is_bound(left):
        if not terms.is_list(left) or not left:
            return False
        env_copy = env.copy()
        if not terms.unify(left[-1], env, middle, env_copy):
            return False
        if not terms.unify(left[:-1], env, right, env_copy):
            return False
        return [env_copy]

    if terms.is_unbound(middle) or terms.is_unbound(
            right) or not terms.is_list(right):
        return False

    l2 = right + (middle, )
    return terms.unify(l2, env, left, env)
Exemplo n.º 8
0
def pred_applies(rules, term, env):
    if term.arity < 2: return False

    var = terms.expand(term.args[0], env)
    list = terms.expand(term.args[1], env)
    clauses = [terms.expand(t, env) for t in term.args[2:]]

    if terms.is_unbound(list) or not terms.is_list(list): return False

    for e in list:
        e2 = {}
        if not terms.unify(e, {}, var, e2): return False
        if rules.search_any_env(e2, *clauses) is None: return False

    return True
Exemplo n.º 9
0
def pred_in(rules, term, env):
    if term.arity != 2: return False

    right = terms.expand(term.args[1], env)
    if terms.is_unbound(right) or not terms.is_list(right): return False

    left = terms.expand(term.args[0], env)

    if terms.is_bound(left):
        return left in right

    r = []
    for e in right:
        env_copy = env.copy()
        if terms.unify(e, env, term.args[0], env_copy):
            r.append(env_copy)

    return r
Exemplo n.º 10
0
def func_filter(rules, term, env):
    if term.arity < 2: return ()

    var = terms.expand(term.args[0], env)
    list = terms.expand(term.args[1], env)
    clauses = [terms.expand(t, env) for t in term.args[2:]]

    if terms.is_unbound(list) or not terms.is_list(list): return ()

    r = []

    for e in list:
        e2 = {}
        if not terms.unify(e, {}, var, e2): continue
        if rules.search_any_env(e2, *clauses) is None: continue
        r.append(terms.expand(var, e2))

    return tuple(r)
Exemplo n.º 11
0
def func_extend(rules, term, env):
    if term.arity < 3: return ()

    var1 = terms.expand(term.args[0], env)
    list = terms.expand(term.args[1], env)
    var2 = terms.expand(term.args[2], env)
    clauses = [terms.expand(t, env) for t in term.args[3:]]

    if terms.is_unbound(list) or not terms.is_list(list): return ()

    r = []

    for e in list:
        e2 = {}
        if not terms.unify(e, {}, var1, e2): continue
        for r2 in rules.search_env(e2, *clauses):
            r.append(terms.expand(var2, r2))

    return tuple(r)
Exemplo n.º 12
0
def pred_map(rules, term, env):
    if term.arity < 4: return False

    var = terms.expand(term.args[0], env)
    list = terms.expand(term.args[1], env)
    var2 = terms.expand(term.args[2], env)
    list2 = terms.expand(term.args[3], env)
    clauses = [terms.expand(t, env) for t in term.args[4:]]

    if terms.is_unbound(list) or not terms.is_list(list): return False

    r = []

    for e in list:
        e2 = {}
        if not terms.unify(e, {}, var, e2): return False
        r2 = rules.search_any_env(e2, *clauses)
        if r2 is None: return False
        r.append(terms.expand(var2, r2))

    return terms.unify(tuple(r), env, list2, env)
Exemplo n.º 13
0
 def check_list0(self,list):
     self.assertEquals(True, terms.is_list(list))
     self.assertEquals(True, terms.is_bound(list))
     self.assertEquals(False, terms.is_unbound(list))
     self.assertEquals(True, terms.is_list(list))
     self.assertEquals(False, terms.is_split(list))