示例#1
0
def _bootstrap():

    from hornet.symbols import P, Q, X, Y, Z, Tail, Object, Goal, List, Rest
    from hornet.symbols import Predicate, A, B, C, D, H, L, T, S, Arity, G, G1
    from hornet.symbols import M, N, length_given_N_

    expressions = (

        cut,

        true,

        fail[_fail],

        # not:
        ~X << X & cut[_fail],
        ~_,

        # or:
        X | _ << X,
        _ | Y << Y,

        # xor:
        X ^ Y << X & ~Y,
        X ^ Y << ~X & Y,

        # if-then-else:
        X >> Y | _ << X & Y,
        X >> _ | Z << ~X & Z,

        repeat,
        repeat << repeat,

        let(X, Y)[_let],

        call(Goal) << Goal,

        once(Goal) << Goal & cut,

        ignore(Goal) << Goal & cut,
        ignore(_),

        equal(P, P),

        unequal(P, P) << cut[_fail],
        unequal(_, _),

        greater(X, Y)[_greater],

        smaller(X, Y)[_smaller],

        throw[_throw],

        findall(Object, Goal, List)[_findall_3],
        findall(Object, Goal, List, Rest)[_findall_4],

        member(H, [H|T]),
        member(G, [H|T]) <<
            member(G, T),

        append([], A, A),
        append([A|B], C, [A|D]) <<
                append(B, C, D),

        reverse(X, Y) <<
            reverse(X, [], Y),

        reverse([], Y, Y),
        reverse([X|P], Q, Y) <<
            reverse(P, [X|Q], Y),

        select(X, [X|T], T),
        select(X, [H|T], [H|Rest]) <<
            select(X, T, Rest),

        write(X)[_write],

        writeln(X)[_writeln],

        lwriteln([H|T]) << writeln(H) & lwriteln(T),
        lwriteln([]) << nl,

        nl[lambda *a: print()],

        listing[_listing_0],

        listing(Predicate)[_listing_1],

        listing(Predicate, Arity)[_listing_2],

        _C_([X|L], X, L),

        atomic(X)[_atomic],

        integer(X)[_integer],

        real(X)[_real],

        numeric(X)[_numeric],

        join(L, S)[_join_2],

        join(L, S, T)[_join_3],

        var(X)[_var],

        nonvar(X)[_nonvar],

        univ(T, L)[_univ],

        arithmetic_equal(X, Y) <<
            let(Z, X) &
            let(Z, Y),

        arithmetic_not_equal(X, Y) <<
            let(Z, X) &
            let(Z, Y) & cut[_fail],
        arithmetic_not_equal(_, _),

        transpose(L, T)[_transpose],

        maplist(G, [H|T]) <<
            cut & univ(G1, [G, H]) & G1 &
            maplist(G, T),
        maplist(_, []),

        length(L, N) <<
            nonvar(N) & cut & ~smaller(N, 0) & length_given_N_(L, N),
        length([], 0),
        length([H|T], N) <<
            length(T, M) &
            let(N, M + 1),

        length_given_N_([], 0) << cut,
        length_given_N_([H|T], N) <<
            let(M, N - 1) &
            length_given_N_(T, M),

    )

    db = ClauseDict()
    indicators = collections.defaultdict(set)

    for expression in expressions:
        clause = make_clause(build_term(expression))
        db[clause.indicator].append(clause)
        indicators[clause.name].add(clause.indicator)

    return (
        tuple((k, tuple(v)) for k, v in db.items()),
        tuple((k, frozenset(v)) for k, v in indicators.items()),
    )
示例#2
0
def test_dcg_transformation():

    from hornet.expressions import promote, mcompose
    from hornet.operators import rearrange
    from hornet.dcg import expand, _C_

    from hornet.symbols import s, np, vp, _0, _1, _2, _3, _4, _5, _6, _7, _8
    from hornet.symbols import it_is, tell, me, it, aint, so, very, different
    from hornet.symbols import a, b, c, u, v, X

    ast_test_all(
        ast_eq,
        mcompose(rearrange, expand),
        rearrange,
        (
            s >> vp,
            s(_0, _1) << vp(_0, _1),
        ),
        (
            s >> np & vp,
            s(_0, _2) << np(_0, _1) & vp(_1, _2),
        ),
        (
            s >> np(a) & vp(a, b),
            s(_0, _2) << np(a, _0, _1) & vp(a, b, _1, _2),
        ),
        (
            s >> a & b & c,
            s(_0, _3) << a(_0, _1) & b(_1, _2) & c(_2, _3),
        ),
        (
            it_is(X) >> [] & [],
            it_is(X, _0, _0),
        ),
        (
            it_is(X) >> [],
            it_is(X, _0, _0),
        ),
        (
            it_is(X) >> [] & so,
            it_is(X, _0, _1) <<
                so(_0, _1),
        ),
        (
            it_is(X) >> so & [],
            it_is(X, _0, _1) <<
                so(_0, _1),
        ),
        (
            it_is(X) >> [] & so & [],
            it_is(X, _0, _1) <<
                so(_0, _1),
        ),
        (
            it_is(X) >> tell & [] & so,
            it_is(X, _0, _2) <<
                tell(_0, _1) &
                so(_1, _2),
        ),
        (
            it_is(X) >> [tell],
            it_is(X, _0, _1) <<
                _C_(_0, tell, _1),
        ),
        (
            it_is(X) >> [tell, me],
            it_is(X, _0, _2) <<
                _C_(_0, tell, _1) &
                _C_(_1, me, _2),
        ),
        (
            it_is(X) >> [tell, me, it],
            it_is(X, _0, _3) <<
                _C_(_0, tell, _1) &
                _C_(_1, me, _2) &
                _C_(_2, it, _3),
        ),
        (
            it_is(X) >> [tell, me, it, aint],
            it_is(X, _0, _4) <<
                _C_(_0, tell, _1) &
                _C_(_1, me, _2) &
                _C_(_2, it, _3) &
                _C_(_3, aint, _4),
        ),
        (
            it_is(X) >> [tell, me, it, aint, so],
            it_is(X, _0, _5) <<
                _C_(_0, tell, _1) &
                _C_(_1, me, _2) &
                _C_(_2, it, _3) &
                _C_(_3, aint, _4) &
                _C_(_4, so, _5),
        ),
        (
            (it_is(X) & [c, tell, me, it]) >>
                [aint, so, a, b],
            it_is(X, _0, _8) <<
                _C_(_0, aint, _1) &
                _C_(_1, so, _2) &
                _C_(_2, a, _3) &
                _C_(_3, b, _4) &
                _C_(_8, c, _7) &
                _C_(_7, tell, _6) &
                _C_(_6, me, _5) &
                _C_(_5, it, _4),
        ),
        (
            (it_is(X) & [tell, me]) >>
                [very, different],
            it_is(X, _0, _4) <<
                _C_(_0, very, _1) &
                _C_(_1, different, _2) &
                _C_(_4, tell, _3) &
                _C_(_3, me, _2),
        ),
        (
            it_is(X) & [me] >>
                it & [very] & so,
            it_is(X, _0, _4) <<
                it(_0, _1) &
                _C_(_1, very, _2) &
                so(_2, _3) &
                _C_(_4, me, _3),
        ),
        (
            it_is(X) & [me] >>
                it & [very] & so & [different],
            it_is(X, _0, _5) <<
                it(_0, _1) &
                _C_(_1, very, _2) &
                so(_2, _3) &
                _C_(_3, different, _4) &
                _C_(_5, me, _4),
        ),
        (
            it_is(X) & [me] >>
                promote([it]) & [very] & [tell] & [different] & so,
            it_is(X, _0, _6) <<
                _C_(_0, it, _1) &
                _C_(_1, very, _2) &
                _C_(_2, tell, _3) &
                _C_(_3, different, _4) &
                so(_4, _5) &
                _C_(_6, me, _5),
        ),
        (
            it_is(X) & [me] >>
                it & [very, tell] & so & [different],
            it_is(X, _0, _6) <<
                it(_0, _1) &
                _C_(_1, very, _2) &
                _C_(_2, tell, _3) &
                so(_3, _4) &
                _C_(_4, different, _5) &
                _C_(_6, me, _5),
        ),
        (
            it_is(X) >> {tell(X)} & me,
            it_is(X, _0, _1) <<
                tell(X) &
                me(_0, _1),
        ),
    )