Esempio n. 1
0
def build_rule(l, r, cons=None):
    lpat = []
    rpat = []
    sym = set()
    cons = cons or {}

    for pat, bind, ty in free(l):
        if bind in sym:
            # TODO unify ty
            lpat.append(bind)
        else:
            lpat.append(bind)
            sym.add(bind)

    for pat, bind, ty in free(r):
        if bind in sym:
            rpat.append(bind)
        else:
            raise Exception('Unbound variable: %s' % pat)

    left  = freev(l)
    right = freev(r)

    ana  = partial(unfold, lpat, left)
    cata = partial(fold, rpat, right)

    rr = partial(hylo, ana, cata)
    return Rule(lpat, rpat, left, right, rr)
Esempio n. 2
0
def test_aspattern3():
    a0 = dslparse('b: a@f(x,y) -> a')
    v = free(a0[0].lhs)

    f = aparse('f(x,y)')

    assert list(v) == [
        (f, 'a', ast.aappl)
    ]
Esempio n. 3
0
def test_aspattern1():
    x = aparse('x')

    a0 = dslparse('b: f(a@x) -> a')
    v = free(a0[0].lhs)

    assert list(v) == [
        (x, 'a', ast.aterm)
    ]
Esempio n. 4
0
def test_linear():
    x = aparse('x')
    y = aparse('y')

    a0 = aparse('f(x,y)')
    v = free(a0)

    assert list(v) == [
        (x, 'x', ast.aterm),
        (y, 'y', ast.aterm)
    ]
Esempio n. 5
0
def test_aspattern2():
    a0 = dslparse('b: @f(x,y) -> a')
    v = free(a0[0].lhs)

    f = aparse('f')
    x = aparse('x')
    y = aparse('y')

    assert list(v) == [
        (f, 'f', ast.aterm),
        (x, 'x', ast.aterm),
        (y, 'y', ast.aterm)
    ]