Exemplo n.º 1
0
def gnu_test1(pl):
    #pl = GNUProlog()

    p = c_pred("p", 2)
    f = c_functor("t", 3)
    f1 = p("a", "b")

    pl.assertz(f1)

    X = c_var("X")
    Y = c_var("Y")

    query = p(X, Y)

    r = pl.has_solution(query)
    print("has solution", r)

    rv = pl.query(query)
    print("all solutions", rv)

    f2 = p("a", "c")
    pl.assertz(f2)

    rv = pl.query(query)
    print("all solutions after adding f2", rv)

    func1 = f(1, 2, 3)
    f3 = p(func1, "b")
    pl.assertz(f3)

    rv = pl.query(query)
    print("all solutions after adding structure", rv)

    l = List([1, 2, 3, 4, 5])

    member = c_pred("member", 2)

    query2 = member(X, l)

    rv = pl.query(query2)
    print("all solutions to list membership ", rv)

    r = c_pred("r", 2)
    f4 = r("a", l)
    f5 = r("a", "b")

    pl.asserta(f4)
    pl.asserta(f5)

    query3 = r(X, Y)

    rv = pl.query(query3)
    print("all solutions after adding list ", rv)

    del pl
Exemplo n.º 2
0
def _swipy_to_structure(term):
    name, arity = swipy.swipy_get_name_arity(term)
    name = swipy.swipy_atom_chars(name)
    functor = c_functor(name, arity)

    structure_elements = []
    for arg_ind in range(1, arity + 1):
        elem = swipy.swipy_new_term_ref()
        swipy.swipy_get_arg(arg_ind, term, elem)

        structure_elements.append(_read_swipy(elem))

    return functor(*structure_elements)
Exemplo n.º 3
0
def _pygp_to_structure(term):
    v1 = pygprolog.pygp_Mk_Variable()
    v2 = pygprolog.pygp_Mk_Variable()

    pygprolog.pygp_Builtin_Functor(term, v1, v2)

    functor = pygprolog.pygp_Rd_String(v1)
    arity = pygprolog.pygp_Rd_Integer(v2)
    args = []
    for i in range(1, arity+1):
        v = pygprolog.pygp_Mk_Variable()
        pygprolog.pygp_Builtin_Arg(pygprolog.pygp_Mk_Integer(i), term, v)
        args.append(_read_pygp(v))

    # global global_context

    return Structure(c_functor(functor, arity), args)
Exemplo n.º 4
0
    def test1():
        pl = SWIProlog()

        p = c_pred("p", 2)
        f = c_functor("t", 3)
        f1 = p("a", "b")

        pl.assertz(f1)

        X = c_var("X")
        Y = c_var("Y")

        query = p(X, Y)

        r = pl.has_solution(query)
        print("has solution", r)

        rv = pl.query(query)
        print("all solutions", rv)

        f2 = p("a", "c")
        pl.assertz(f2)

        rv = pl.query(query)
        print("all solutions after adding f2", rv)

        func1 = f(1, 2, 3)
        f3 = p(func1, "b")
        pl.assertz(f3)

        rv = pl.query(query)
        print("all solutions after adding structure", rv)

        l = List([1, 2, 3, 4, 5])

        member = c_pred("member", 2)

        query2 = member(X, l)

        rv = pl.query(query2)
        print("all solutions to list membership ", rv)

        r = c_pred("r", 2)
        f4 = r("a", l)
        f5 = r("a", "b")

        pl.asserta(f4)
        pl.asserta(f5)

        query3 = r(X, Y)

        rv = pl.query(query3)
        print("all solutions after adding list ", rv)

        # Foreign predicates

        def hello(t):
            print("Foreign: Hello", t)

        hello_pred = pl.register_foreign(hello, 1)
        # print(hello_pred)

        f_query = hello_pred("a")

        pl.has_solution(f_query)

        del pl
Exemplo n.º 5
0
    def test7(limit=10):
        pl = XSBProlog("/Users/seb/Documents/programs/XSB")

        p = c_pred("p", 2)
        f = c_functor("t", 3)
        f1 = p("a", "b")

        pl.assertz(f1)

        X = c_var("X")
        Y = c_var("Y")

        query = p(X, Y)

        r = pl.has_solution(query)
        print("has solution", r)

        rv = pl.query(query, time_limit=limit)
        print("all solutions", rv)

        f2 = p("a", "c")
        pl.assertz(f2)

        rv = pl.query(query, time_limit=limit)
        print("all solutions after adding f2", rv)

        func1 = f(1, 2, 3)
        f3 = p(func1, "b")
        pl.assertz(f3)

        rv = pl.query(query, time_limit=limit)
        print("all solutions after adding structure", rv)

        l = List([1, 2, 3, 4, 5])

        member = c_pred("member", 2)
        pl.use_module("lists", predicates=[member])

        query2 = member(X, l)

        rv = pl.query(query2, time_limit=limit)
        print("all solutions to list membership ", rv)

        r = c_pred("r", 2)
        f4 = r("a", l)
        f5 = r("a", "b")

        pl.asserta(f4)
        pl.asserta(f5)

        query3 = r(X, Y)

        rv = pl.query(query3, time_limit=limit)
        print("all solutions after adding list ", rv)

        q = c_pred("q", 2)
        cl = (q("X", "Y") <= r("X", "Y") & r("X", "Z"))

        pl.assertz(cl)
        query4 = q("X", "Y")
        rv = pl.query(query4, time_limit=limit)
        print("all solutions to q: ", rv)

        del pl