Ejemplo n.º 1
0
def test_unify_variable_with_itself_should_not_unify():
    # Regression test for https://github.com/logpy/logpy/issues/33
    valido = Relation()
    fact(valido, "a", "b")
    fact(valido, "b", "a")
    x = var()
    assert run(0, x, valido(x, x)) == ()
Ejemplo n.º 2
0
def test_unify_variable_with_itself_should_unify():
    valido = Relation()
    fact(valido, 0, 1)
    fact(valido, 1, 0)
    fact(valido, 1, 1)
    x = var()
    assert run(0, x, valido(x, x)) == (1, )
Ejemplo n.º 3
0
def test_fact():
    rel = Relation()
    fact(rel, 1, 2)
    assert (1, 2) in rel.facts
    assert (10, 10) not in rel.facts

    facts(rel, (2, 3), (3, 4))
    assert (2, 3) in rel.facts
    assert (3, 4) in rel.facts
Ejemplo n.º 4
0
def test_relation():
    parent = Relation()
    fact(parent, "Homer", "Bart")
    fact(parent, "Homer", "Lisa")
    fact(parent, "Marge", "Bart")
    fact(parent, "Marge", "Lisa")
    fact(parent, "Abe", "Homer")
    fact(parent, "Jackie", "Marge")

    x = var('x')
    assert set(run(5, x, parent("Homer", x))) == set(("Bart", "Lisa"))
    assert set(run(5, x, parent(x, "Bart"))) == set(("Homer", "Marge"))

    def grandparent(x, z):
        y = var()
        return conde((parent(x, y), parent(y, z)))

    assert set(run(5, x, grandparent(x, "Bart"))) == set(("Abe", "Jackie"))

    foo = Relation('foo')
    assert 'foo' in str(foo)
Ejemplo n.º 5
0
def test_unify_tuple():
    # Tests that adding facts can be unified with unpacked versions of those
    # facts.
    valido = Relation()
    fact(valido, (0, 1))
    fact(valido, (1, 0))
    fact(valido, (1, 1))
    x = var()
    y = var()
    assert set(run(0, x, valido((x, y)))) == set([0, 1])
    assert set(run(0, (x, y), valido((x, y)))) == set([(0, 1), (1, 0), (1, 1)])
    assert run(0, x, valido((x, x))) == (1, )
from kanren.core import var, run, conde

def grandparent(x, y):
    temp = var()
    return conde((parent(x, temp), parent(temp, y)))

def sibling(x, y):
    temp = var()
    return conde((parent(temp, x), parent(temp, y)))

def uncle(x, y):
    temp = var()
    return conde((parent(temp, x), grandparent(temp, y)))

if __name__=='__main__':
    parent = Relation()

    facts(parent, ("Slamet", "Amin"),
                  ("Slamet", "Anang"),
                  ("Amin", "Badu"),
                  ("Amin", "Budi"),
                  ("Anang", "Didi"),
                  ("Anang", "Dudu"),
                  ("Anang", "Dadi"))

    x = var()

    father = "Slamet"
    anak = run(0, x, parent(father, x))
    print("\nNama anak-anak " + father + ": ")
    for item in anak:
from kanren.facts import Relation, facts, fact
from kanren.core import var, run, conde


def get_sibling(x, y):
    temp = var()
    return conde((parent(temp, x), parent(temp, y)))


if __name__ == '__main__':
    parent = Relation()

    facts(parent, ("Homer", "Bart"), ("Homer", "Lisa"), ("Abe", "Homer"))

    # bisa juga ditulis dalam fact terpisah seperti ini
    # fact(parent, "Homer", "Bart")
    # fact(parent, "Homer", "Lisa")
    # fact(parent, "Abe",  "Homer")

    x = var()
    output = run(0, x, parent(x, "Bart"))
    print("\nNama ayah Bart : ", output)

    ### contoh definisi saudara (sibling) menggunakan relasi baru
    sibling = Relation()
    facts(sibling, ("Bart", "Lisa"), ("Lisa", "Bart"))
    brother = run(0, x, sibling(x, "Lisa"))
    print("\nNama saudara laki-laki Lisa : ", brother[0])
    sister = run(0, x, sibling(x, "Bart"))
    print("\nNama saudara perempuan Bart : ", sister[0])
    ''' 
Ejemplo n.º 8
0
"""Relations pertaining to probability distributions.
"""
from unification import var
from kanren import conde, eq
from kanren.facts import fact

from . import constant_neq, concat
from ..meta import mt

from kanren.facts import Relation

derived_dist = Relation('derived_dist')
stable_dist = Relation('stable_dist')
generalized_gamma_dist = Relation('generalized_gamma_dist')

uniform_mt = mt.UniformRV(var(), var(), size=var(), rng=var(), name=var())
normal_mt = mt.NormalRV(var(), var(), size=var(), rng=var(), name=var())
cauchy_mt = mt.CauchyRV(var(), var(), size=var(), rng=var(), name=var())
halfcauchy_mt = mt.HalfCauchyRV(var(),
                                var(),
                                size=var(),
                                rng=var(),
                                name=var())
gamma_mt = mt.GammaRV(var(), var(), size=var(), rng=var(), name=var())
exponential_mt = mt.ExponentialRV(var(), size=var(), rng=var(), name=var())

# TODO: Add constraints for different variations of this.  Also, consider a
# check for exact equality of the two dists, or simply normalize/canonicalize
# the graph first.
fact(
    derived_dist,
Ejemplo n.º 9
0
import theano.tensor as tt

from unification import var
from kanren import conde, eq
from kanren.facts import fact

from unification.utils import transitive_get as walk

from .. import concat
from ...etuple import etuple
from ...utils import HashableNDArray
from ...theano.meta import TheanoMetaConstant, mt

from kanren.facts import Relation

derived_dist = Relation("derived_dist")
stable_dist = Relation("stable_dist")
generalized_gamma_dist = Relation("generalized_gamma_dist")

uniform_mt = mt.UniformRV(var(), var(), size=var(), rng=var(), name=var())
normal_mt = mt.NormalRV(var(), var(), size=var(), rng=var(), name=var())
cauchy_mt = mt.CauchyRV(var(), var(), size=var(), rng=var(), name=var())
halfcauchy_mt = mt.HalfCauchyRV(var(),
                                var(),
                                size=var(),
                                rng=var(),
                                name=var())
gamma_mt = mt.GammaRV(var(), var(), size=var(), rng=var(), name=var())
exponential_mt = mt.ExponentialRV(var(), size=var(), rng=var(), name=var())

# TODO: Add constraints for different variations of this.  Also, consider a
Ejemplo n.º 10
0
from unification import isvar

from kanren import eq
from kanren.core import lallgreedy
from kanren.facts import Relation
from kanren.goals import goalify
from kanren.term import term, operator, arguments
from kanren.goals import conso

from ..etuple import etuplize

# Hierarchical models that we recognize.
hierarchical_model = Relation("hierarchical")

# Conjugate relationships
conjugate = Relation("conjugate")

concat = goalify(lambda *args: "".join(args))


def buildo(op, args, obj):
    if not isvar(obj):
        if not isvar(args):
            args = etuplize(args, shallow=True)
        oop, oargs = operator(obj), arguments(obj)
        return lallgreedy(eq(op, oop), eq(args, oargs))
    elif isvar(args) or isvar(op):
        return conso(op, args, obj)
    else:
        return eq(obj, term(op, args))
Ejemplo n.º 11
0
from kanren.facts import Relation, facts, fact
from kanren.core import var, run
from kanren.goals import membero

ukuran = Relation()
warna = Relation()
gelap = Relation()
jenis = Relation()

facts(ukuran, ("beruang", "besar"),
			  ("gajah", "besar"),
			  ("semuat", "kecil"),
			  ("kucing", "kecil"))
facts(warna,  ("beruang", "cokelat"),
			  ("kucing", "hitam"),
			  ("semut", "hitam"),
			  ("gajah", "kelabu"))
facts(jenis,  ("beruang", "karnivora"),
			  ("kucing", "karnivora"),
			  ("semut", "omnivora"),
			  ("gajah", "herbivora"))
fact(gelap, "hitam")
fact(gelap, "cokelat")

x = var()

herbivora = run(0, x, jenis(x, "karnivora"))
print("hewan jenis karnivora :", herbivora)
Ejemplo n.º 12
0
import numpy as np
import theano.tensor as tt

from kanren.facts import Relation
from kanren.goals import goalify

from unification.utils import transitive_get as walk

# Hierarchical models that we recognize.
hierarchical_model = Relation('hierarchical')

# Conjugate relationships
conjugate = Relation('conjugate')

concat = goalify(lambda *args: ''.join(args))


def constant_neq(lvar, val):
    """Assert that a constant graph variable is not equal to a specific value.

    Scalar values are broadcast across arrays.
    """
    from symbolic_pymc.meta import MetaConstant

    def _goal(s):
        lvar_val = walk(lvar, s)
        if isinstance(lvar_val, (tt.Constant, MetaConstant)):
            data = lvar_val.data
            if ((isinstance(val, np.ndarray) and not np.array_equal(data, val))
                    or not all(np.atleast_1d(data) == val)):
                yield s
Ejemplo n.º 13
0
from kanren.facts import Relation, facts
from kanren.core import var, run
father = Relation()
facts(father, ("Homer", "Bart"),
              ("Homer", "Lisa"),
              ("Abe",  "Homer"))
x = var()
output = run(1, x, father(x, "Bart"))
print("Nama ayah Bart : ", output[0])