コード例 #1
0
ファイル: test_match.py プロジェクト: mrocklin/unification
def test_ordering():
    x = var('x')
    y = var('y')
    o = ordering([(1,), (x,), (2,), (y,), (x, x), (1, x), (x, 1), (1, 2)])
    print("Ordering:", o)

    for a, b in zip(o, o[1:]):
        assert supercedes(a, b) or not supercedes(b, a)
コード例 #2
0
ファイル: test_match.py プロジェクト: xmonader/unification
def test_ordering():
    x = var('x')
    y = var('y')
    o = ordering([(1, ), (x, ), (2, ), (y, ), (x, x), (1, x), (x, 1), (1, 2)])
    print("Ordering:", o)

    for a, b in zip(o, o[1:]):
        assert supercedes(a, b) or not supercedes(b, a)
コード例 #3
0
ファイル: test_match.py プロジェクト: mrocklin/unification
def test_dict():
    d = Dispatcher('d')
    x = var('x')
    y = var('y')

    d.add(({'x': x, 'key': 1},), identity)

    d({'x': 1, 'key': 1}) == {'x': 1, 'key': 1}
コード例 #4
0
ファイル: test_match.py プロジェクト: xmonader/unification
def test_dict():
    d = Dispatcher('d')
    x = var('x')
    y = var('y')

    d.add(({'x': x, 'key': 1}, ), identity)

    d({'x': 1, 'key': 1}) == {'x': 1, 'key': 1}
コード例 #5
0
ファイル: test_match.py プロジェクト: mrocklin/unification
def test_supercedes():
    x, y, z = var('x'), var('y'), var('z')
    assert not supercedes(1, 2)
    assert supercedes(1, x)
    assert not supercedes(x, 1)
    assert supercedes((1, 2), (1, x))
    assert not supercedes((1, x), (1, 2))
    assert supercedes((1, x), (y, z))
    assert supercedes(x, y)
    assert supercedes((1, (x, 3)), (1, y))
    assert not supercedes((1, y), (1, (x, 3)))
コード例 #6
0
ファイル: test_match.py プロジェクト: xmonader/unification
def test_supercedes():
    x, y, z = var('x'), var('y'), var('z')
    assert not supercedes(1, 2)
    assert supercedes(1, x)
    assert not supercedes(x, 1)
    assert supercedes((1, 2), (1, x))
    assert not supercedes((1, x), (1, 2))
    assert supercedes((1, x), (y, z))
    assert supercedes(x, y)
    assert supercedes((1, (x, 3)), (1, y))
    assert not supercedes((1, y), (1, (x, 3)))
コード例 #7
0
ファイル: test_match.py プロジェクト: mrocklin/unification
def test_VarDispatcher():
    d = VarDispatcher('d')
    x, y, z = var('x'), var('y'), var('z')

    @d.register(x, y)
    def swap(y, x):
        return y, x

    assert d(1, 2) == (2, 1)

    @d.register((1, z), 2)
    def foo(z):
        return z

    assert d((1, 3), 2) == 3
コード例 #8
0
ファイル: test_match.py プロジェクト: xmonader/unification
def test_VarDispatcher():
    d = VarDispatcher('d')
    x, y, z = var('x'), var('y'), var('z')

    @d.register(x, y)
    def swap(y, x):
        return y, x

    assert d(1, 2) == (2, 1)

    @d.register((1, z), 2)
    def foo(z):
        return z

    assert d((1, 3), 2) == 3
コード例 #9
0
ファイル: test_match.py プロジェクト: mrocklin/unification
def test_complex():
    d = Dispatcher('d')
    x = var('x')
    y = var('y')

    d.add((1,), inc)
    d.add((x,), inc)
    d.add((x, 1), add)
    d.add((y, y), mul)
    d.add((x, (x, x)), foo)

    assert d(1) == 2
    assert d(2) == 3
    assert d(2, 1) == 3
    assert d(10, 10) == 100
    assert d(10, (10, 10)) == (10, (10, 10))
    assert raises(NotImplementedError, lambda : d(1, 2))
コード例 #10
0
ファイル: test_match.py プロジェクト: xmonader/unification
def test_complex():
    d = Dispatcher('d')
    x = var('x')
    y = var('y')

    d.add((1, ), inc)
    d.add((x, ), inc)
    d.add((x, 1), add)
    d.add((y, y), mul)
    d.add((x, (x, x)), foo)

    assert d(1) == 2
    assert d(2) == 3
    assert d(2, 1) == 3
    assert d(10, 10) == 100
    assert d(10, (10, 10)) == (10, (10, 10))
    assert raises(NotImplementedError, lambda: d(1, 2))
コード例 #11
0
ファイル: test_match.py プロジェクト: mrocklin/unification
def test_dispatcher():
    x = var('x')
    @match(1)
    def fib(x):
        return 1

    @match(0)
    def fib(x):
        return 0

    @match(x)
    def fib(n):
        return fib(n - 1) + fib(n - 2)

    assert [fib(i) for i in range(10)] == \
            [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
コード例 #12
0
ファイル: test_match.py プロジェクト: xmonader/unification
def test_dispatcher():
    x = var('x')

    @match(1)
    def fib(x):
        return 1

    @match(0)
    def fib(x):
        return 0

    @match(x)
    def fib(n):
        return fib(n - 1) + fib(n - 2)

    assert [fib(i) for i in range(10)] == \
            [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
コード例 #13
0
ファイル: test_match.py プロジェクト: mrocklin/unification
def test_supercedes_more():
    x, y, z = var('x'), var('y'), var('z')
    assert supercedes((1, x), (y, y))
    assert supercedes((1, x), (x, x))
コード例 #14
0
ファイル: test_match.py プロジェクト: xmonader/unification
def test_supercedes_more():
    x, y, z = var('x'), var('y'), var('z')
    assert supercedes((1, x), (y, y))
    assert supercedes((1, x), (x, x))