Ejemplo n.º 1
0
def unify(u, v, s):  # no check at the moment
    """ Find substitution so that u == v while satisfying s

    >>> from logpy.unify import unify, var
    >>> x = var('x')
    >>> unify((1, x), (1, 2), {})
    {~x: 2}
    """
    u = walk(u, s)
    v = walk(v, s)
    if u == v:
        return s
    if isvar(u):
        return assoc(s, u, v)
    if isvar(v):
        return assoc(s, v, u)
    types = (type(u), type(v))
    if types in unify_dispatch:
        return unify_dispatch[types](u, v, s)
    for (typu, typv), unify_fn in unify_isinstance_list:
        if isinstance(u, typu) and isinstance(v, typv):
            return unify_fn(u, v, s)
    for typ, fn in seq_registry:
        action = False
        if isinstance(u, typ):
            u = fn(u); action=True
        if isinstance(v, typ):
            v = fn(v); action=True
        if action:
            return unify(u, v, s)

    else:
        return False
Ejemplo n.º 2
0
def unify(u, v, s):  # no check at the moment
    """ Find substitution so that u == v while satisfying s

    >>> from logpy.core import unify, var
    >>> x = var('x')
    >>> unify((1, x), (1, 2), {})
    {~x: 2}
    """
    u = walk(u, s)
    v = walk(v, s)
    if u == v:
        return s
    if isvar(u):
        return assoc(s, u, v)
    if isvar(v):
        return assoc(s, v, u)
    if isinstance(u, tuple) and isinstance(v, tuple):
        if len(u) != len(v):
            return False
        for uu, vv in zip(u, v):  # avoiding recursion
            s = unify(uu, vv, s)
            if s is False:
                return False
        return s
    return False
Ejemplo n.º 3
0
def unify(u, v, s):  # no check at the moment
    """ Find substitution so that u == v while satisfying s

    >>> from termpy.unification import unify, var
    >>> x = var('x')
    >>> unify((1, x), (1, 2), {})
    {~x: 2}
    """
    u = walk(u, s)
    v = walk(v, s)
    if u == v:
        return s
    elif isvar(u):
        return assoc(s, u, v)
    elif isvar(v):
        return assoc(s, v, u)
    types = (type(u), type(v))
    if types in unify_dispatch:
        return unify_dispatch[types](u, v, s)
    elif not isleaf(u) and not isleaf(v):
        s = unify(op(u), op(v), s)
        if s is False:
            return s
        else:
            return unify(args(u), args(v), s)
    else:
        return False
Ejemplo n.º 4
0
def unify(u, v, s):  # no check at the moment
    """ Find substitution so that u == v while satisfying s

    >>> from logpy.unification import unify, var
    >>> x = var('x')
    >>> unify((1, x), (1, 2), {})
    {~x: 2}
    """
    u = walk(u, s)
    v = walk(v, s)
    if u == v:
        return s
    if isvar(u):
        return assoc(s, u, v)
    if isvar(v):
        return assoc(s, v, u)
    types = (type(u), type(v))
    if types in unify_dispatch:
        return unify_dispatch[types](u, v, s)
    if (hasattr(u, '_as_logpy') and not isinstance(u, type) and
        hasattr(v, '_as_logpy') and not isinstance(v, type)):
        return unify_seq(u._as_logpy(), v._as_logpy(), s)
    for (typu, typv), unify_fn in unify_isinstance_list:
        if isinstance(u, typu) and isinstance(v, typv):
            return unify_fn(u, v, s)
    for typ, fn in seq_registry:
        if isinstance(u, typ) and isinstance(v, typ):
            return unify_seq(fn(u), fn(v), s)

    else:
        return False
Ejemplo n.º 5
0
def create_error(typestr, *attrs):
    def process_attrs(attrs, acc):
        if attrs == ():
            return Error(Symbol(typestr), List(acc))
        elif len(attrs) >= 2:
            acc.append(Symbol(attrs[0]))
            acc.append(attrs[1])
            return process_attrs(attrs[2:], acc)
        else:
            raise RuntimeError('Invalid error attributes.')

    if type(typestr) not in [str, String]:
        raise RuntimeError('Invalid error type.')

    err = process_attrs(attrs, [])
    try:
        form = assoc(err.attrs, Symbol(':form'))
        err.start_row = form.start_row
        err.start_col = form.start_col
        err.end_row = form.end_row
        err.end_col = form.end_col
        err.filename = form.filename
    except:
        pass

    return err
Ejemplo n.º 6
0
def print_error(e):
    try:
        msg = assoc(e.attrs, Symbol(':msg'))
    except (KeyError, InvalidAssocList):
        msg = ''

    try:
        form = assoc(e.attrs, Symbol(':form'))
    except (KeyError, InvalidAssocList):
        form = e

    if msg:
        print 'Error of type "{}": {}'.format(e.type.name, msg)
    else:
        print 'Error of type "{}".'.format(e.type.name)

    if form:
        display_form(form, True)
Ejemplo n.º 7
0
def run_tests(f, filename, env):
    passed = 0
    failed = 0
    error = 0

    reader = Reader(f, filename)
    while True:
        form = reader.read()

        if form == None:
            break

        result = eval_form(form, env)
        if result == False:
            failed += 1
            print 'Test failed:'
            display_form(form, False)
        elif result == True:
            passed += 1
            print 'Test passed.'
        elif isinstance(result, Error):
            error += 1

            try:
                desc = assoc(result.attrs, Symbol(':msg'))
                print 'Test error ({}): {}'.format(result.type.name, desc)
            except (KeyError, InvalidAssocList) as e:
                print 'Test error ({}).'.format(result.type.name)

            try:
                form = assoc(result, Symbol(':form'))
                display_form(form, False)
            except (KeyError, InvalidAssocList):
                print 'No location information for the error.'
        else:
            error += 1
            print 'Test cases must return either #t or #f; got {}.'.format(
                result)
            display_form(form, False)

    return passed, failed, error
Ejemplo n.º 8
0
def unify(u, v, s):  # no check at the moment
    """ Find substitution so that u == v while satisfying s

    >>> from logpy.unify import unify, var
    >>> x = var('x')
    >>> unify((1, x), (1, 2), {})
    {~x: 2}
    """
    u = walk(u, s)
    v = walk(v, s)
    if u == v:
        return s
    if isvar(u):
        return assoc(s, u, v)
    if isvar(v):
        return assoc(s, v, u)
    types = (type(u), type(v))
    if types in unify_dispatch:
        return unify_dispatch[types](u, v, s)
    else:
        return False
Ejemplo n.º 9
0
def unify(u, v, s):  # no check at the moment
    """ Find substitution so that u == v while satisfying s

    >>> from logpy.unify import unify, var
    >>> x = var('x')
    >>> unify((1, x), (1, 2), {})
    {~x: 2}
    """
    u = walk(u, s)
    v = walk(v, s)
    if u == v:
        return s
    if isvar(u):
        return assoc(s, u, v)
    if isvar(v):
        return assoc(s, v, u)
    types = (type(u), type(v))
    if types in unify_dispatch:
        return unify_dispatch[types](u, v, s)
    else:
        return False
Ejemplo n.º 10
0
 def test_assoc(self):
     self.assertEqual(util.assoc(1, self._ALIST), (1, 6))
     self.assertEqual(util.assoc(2, self._ALIST), None)
     self.assertEqual(util.assoc(5, self._ALIST), None)