Пример #1
0
def test_condeseq():
    x = var('x')
    assert set(run(0, x, condeseq(([eq(x, 2)], [eq(x, 3)])))) == {2, 3}
    assert set(run(0, x, condeseq([[eq(x, 2), eq(x, 3)]]))) == set()

    goals = ([eq(x, i)] for i in count())  # infinite number of goals
    assert run(1, x, condeseq(goals)) == (0, )
    assert run(1, x, condeseq(goals)) == (1, )
Пример #2
0
def assocunify(u, v, s, eq=core.eq):
    """ Associative Unification

    See Also:
        eq_assoccomm
    """

    res = unify(u, v, s)
    if res is not False:
        return (res,)  # TODO: iterate through all possibilities

    if isinstance(u, tuple) and isinstance(v, tuple):
        uop, u = u[0], u[1:]
        vop, v = v[0], v[1:]
        s = unify(uop, vop, s)
        if s is False:
            raise StopIteration()
        op = walk(uop, s)

        sm, lg = (u, v) if len(u) <= len(v) else (v, u)

        parts = (groupsizes_to_partition(*gsizes) for gsizes
                                                  in  groupsizes(len(lg), len(sm)))
        ops = (makeops(op, partition(lg, part)) for part in parts)
        goal = condeseq([(eq, a, b) for a, b in zip(sm, lg2)] for lg2 in ops)
        return goaleval(goal)(s)

    return ()
Пример #3
0
def assocunify(u, v, s, eq=core.eq, n=None):
    """ Associative Unification

    See Also:
        eq_assoccomm
    """
    uop, uargs = op_args(u)
    vop, vargs = op_args(v)

    if not uop and not vop:
        res = unify(u, v, s)
        if res is not False:
            return (res,)  # TODO: iterate through all possibilities

    if uop and vop:
        s = unify(uop, vop, s)
        if s is False:
            raise StopIteration()
        op = walk(uop, s)

        sm, lg = (uargs, vargs) if len(uargs) <= len(vargs) else (vargs, uargs)
        ops = assocsized(op, lg, len(sm))
        goal = condeseq([(eq, a, b) for a, b, in zip(sm, lg2)] for lg2 in ops)
        return goaleval(goal)(s)

    if uop:
        op, tail = uop, uargs
        b = v
    if vop:
        op, tail = vop, vargs
        b = u

    ns = [n] if n else range(2, len(tail)+1)
    knowns = (build(op, x) for n in ns for x in assocsized(op, tail, n))

    goal = condeseq([(core.eq, b, k)] for k in knowns)
    return goaleval(goal)(s)
Пример #4
0
def assocunify(u, v, s, eq=core.eq, n=None):
    """ Associative Unification

    See Also:
        eq_assoccomm
    """

    if not isinstance(u, tuple) and not isinstance(v, tuple):
        res = unify(u, v, s)
        if res is not False:
            return (res,)  # TODO: iterate through all possibilities

    if isinstance(u, tuple) and isinstance(v, tuple):
        uop, u = u[0], u[1:]
        vop, v = v[0], v[1:]
        s = unify(uop, vop, s)
        if s is False:
            raise StopIteration()
        op = walk(uop, s)

        sm, lg = (u, v) if len(u) <= len(v) else (v, u)
        ops = assocsized(op, lg, len(sm))
        goal = condeseq([(eq, a, b) for a, b, in zip(sm, lg2)] for lg2 in ops)
        return goaleval(goal)(s)

    if isinstance(u, tuple):
        a, b = u, v
    if isinstance(v, tuple):
        a, b = v, u

    op, tail = a[0], a[1:]

    ns = [n] if n else range(2, len(a))
    knowns = (((op,) + x) for n in ns for x in assocsized(op, tail, n))

    goal = condeseq([(core.eq, b, k)] for k in knowns)
    return goaleval(goal)(s)
def check_prime(x):
    if lc.isvar(x):
        return lc.condeseq([(lc.eq, x, p)] for p in map(prime, it.count(1)))
    else:
        return lc.success if isprime(x) else lc.fail
Пример #6
0
def primo(x):
    """ x is a prime number """
    if isvar(x):
        return condeseq([(eq, x, p)] for p in it.imap(prime, it.count(1)))
    else:
        return success if isprime(x) else fail
Пример #7
0
def primo(x):
    """ x is a prime number """
    if isvar(x):
        return condeseq([(eq, x, p)] for p in it.imap(prime, it.count(1)))
    else:
        return success if isprime(x) else fail