Example #1
0
def tribes(db):

    db.tell(

        son(joe, sam),  # joe is the son of sam, etc.
        #son(bob, sam),
        son(jim, joe),
        son(tom, bob),
        son(hal, bob),
        son(dan, jim),
        son(lee, jim),

        # X is a descendant of Y:
        descendant(X, Y) <<
            son(X, Y),          # one's son is one's descendant
        descendant(X, Z) <<
            son(Y, Z) &         # a descendant of one's son
            descendant(X, Y),   # is also one's descendant

        # X is an ancestor of Y:
        ancestor(X, Y) <<
            descendant(Y, X),   # one is an ancestor of one's descendant

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

        related(X, Y, Seen) <<
            directly_related(X, Z) &
            ~member(Z, Seen) &
            related_(Z, Y, Seen),

        related_(X, X, _),
        related_(X, Y, Seen) <<
            related(X, Y, [X | Seen]),

        directly_related(X, Y) << son(X, Y) | son(Y, X),

        # Z is the patriarch of X:
        patriarch(Z, X) <<
            son(X, Y) &
            patriarch(Z, Y) & cut,
        patriarch(Z, Z),

        tribe(X, [X|Tribe]) <<
            findall(Y, related(Y, X), Tribe),
        #tribe(X, [Z | Tribe]) <<
            #patriarch(Z, X) &
            #findall(Y, descendant(Y, Z), Tribe),
            #findall(test(Y, U), descendant(Y, Z), Tribe, U) & equal(U, [patriarch]),
            #findall(test(Y, U, test(U)), descendant(Y, Z), Tribe) &
            #equal(W, bob) & equal(Tribe, [test(_, V, _) | _]) & equal(W, V),
            #findall(test(Y, U), descendant(Y, Z) & equal(Y, U), Tribe),

    )
Example #2
0
def genealogy(db):

    tribes(db)

    print('who is an ancestor of who?')
    for subst in db.ask(ancestor(A, B)):
        print(subst[A], 'of', subst[B])
    print()

    print('who are joe\'s descendants?')
    for subst in db.ask(descendant(A, joe)):
        print(subst[A])
    print()

    print('who are dan\'s ancestors?')
    for subst in db.ask(ancestor(A, dan)):
        print(subst[A])
    print()

    print('who is bob related to?')
    for subst in db.ask(related(bob, A)):
        print(subst[A])
    print()

    print('who is related to bob?')
    for subst in db.ask(related(A, bob)):
        print(subst[A])
    print()

    print('who is lee related to?')
    for subst in db.ask(related(lee, A)):
        print(subst[A])
    print()

    print('who is related to lee?')
    for subst in db.ask(related(A, lee)):
        print(subst[A])
    print()

    print('is lee related to joe?')
    for subst in db.ask(related(lee, joe)):
        print('Yes.')
        break
    else:
        print('No.')
    print()

    print('is lee related to bob?')
    for subst in db.ask(related(lee, bob)):
        print('Yes.')
        break
    else:
        print('No.')
    print()

    print('one is not a relative of oneself. true?')
    for subst in db.ask(~related(A, A)):
        print('Yes.')
        break
    else:
        print('No.')
    print()

    print('who belongs to joe\'s tribe?')
    for subst in db.ask(tribe(joe, A) & lwriteln(A)):
        #print(subst[A])
        pass
    print()

    print('what clauses does the predicate descendant/2 consist of?')
    for subst in db.ask(listing(descendant, 2)):
        print('Yes.')
        break
    else:
        print('No.')
    print()

    print('test')
    for subst in db.ask(related(bob, X) & cut & writeln(X) & fail):
        print('Yes.')
        #break
    else:
        print('No.')
    print()