New SIG on logic/CLP programming in Python

Joel Bender jjb5 at cornell.edu
Mon Mar 25 15:23:43 EST 2002


I so boldly said "counter example" on Friday, and while that may have 
been misinterpreted as a counter example to forming a Logic-SIG, what I 
ment was that I had something to kick off a discussion.

I propose to add a few keywords, hopefully I'll be using terms 
consistent with Prolog, but it's been a while, and other comments are 
welcome.

First, add 'goal' to define goals, like functions:

    goal sibling(X,Y):
        parent(Z,X)
        parent(Z,Y)

A goal looks like a function call in the code.

    adam, betsy, clide, dave, edith = range(5)

    assert(parent(adam, clide))     # needs help...
    assert(parent(adam, dave))

    def test1():
        print "Ah!"
        sibling(clide, dave)
        print "Yes!"

    >>> test1()
    Ah!
    Yes!

Then add 'cut' and 'fail' as statements:

    def test2():
        sibling(clide,X)
        print X
        cut
        print "-cut-"
        fail

    >>> test2()
    3
    -cut-

Without the cut statement the output would look like this, because clide 
is considered a sibling of himself:

    def test3():
        sibling(clide,X)
        print X
        fail

    >>> test3()
    3
    2

Unlike Python functions, the same goal could be defined more than one 
way:

    goal sibling(X,Y):
        mom(Z,X)
        mom(Z,Y)

    goal sibling(X,Y):
        dad(Z,X)
        dad(Z,Y)

With my limited Python experience I get the impression that the variable 
'_' is frowned upon, so I would replace it with the keyword 'Any'.

    goal isachild(X):
        parent(Any,X)

Goals can return values, so they can be used like functions:

    goal sibling2(X,Y):
        parent(Z,X)
        parent(Z,Y)
        return Z

    def test4():
        p = sibling2(clide,dave)
        print p

    >>> test4()
    0

Almost every Prolog book has an example of list membership, but while 
the slicing operation can be done simply in Prolog:

    member(X,[X|_]).
    member(X,[_|Y]) :- member(X,Y).

The same kind of thing doesn't seem to fit Python:

    goal member(X,[X|_]): pass

So it would have to be done with a some statements:

    goal member(X,L):
        if (len(x) == 0):
            fail
        if (X != L[0]):
            fail

    goal member(X,L):
        member(X,L[1:])

Comments?


Joel



More information about the Python-list mailing list