Re: Ordered Lists

"Jeremy Leibs" <[email protected]> Mon, 26 Nov 2007 11:49:27 -0800
Newsgroups gmane.comp.ai.powerloom
Message-ID <[email protected]>
Thanks for the response.

Your examples definitely give me some clues for how to better handle
some things, but what I was really hoping for a way to handle lists
where length was not known at query time.

It seems the advantage of using a "list" over a relation of particular
arity is the very fact that your list could be of an arbitrary length.

However, you at least gave me some ideas for a workaround for the
moment that gets me a poor man's first:

(deffunction first ((?lst list)) :-> ?x
:<= (or (exists (?a) (= ?lst (listof ?x ?a)))
        (exists (?a ?b) (= ?lst (listof ?x ?a ?b)))
        (exists (?a ?b ?c) (= ?lst (listof ?x ?a ?b ?c)))))

There are probably some performance issues and obviously it requires
knowing ahead of time what the biggest list I care about is, but it
will do for now.

As for what is actually "needed," given how things are usually phrased
in powerloom, I think you are right that append and nth-element are
probably sufficient.

I feel like rest-of is probably a useful construct to have around on
occasion, but I believe rest-of and append can each be defined in
terms of the other so there's probably no real need for it unless one
made substantially more sense than the other from an implementation
standpoint.



On 11/26/07, Thomas Russ <[email protected]> wrote:
>
> On Nov 26, 2007, at 9:28 AM, Jeremy Leibs wrote:
>
> > Are there any relations or functions implemented within powerloom yet
> > that allow us to actually work with ordered lists?
>
> Not too much is currently available.
>
> > A list created using listof is specified to be "ordered," but I can't
> > find any way to make use of this ordering (or even the fact that it's
> > a list for that matter).  Is there a way to at least define some
> > simple list primitives such as first-of, rest-of, and append?
>
> There is the general-purpose matching machinery, which can be used to
> extract specific elements as long as you know at query creation time
> the length of the list.  For example, you can get the first two
> elements of a 4-element list by
>
> (retrieve all (exists (?x ?y) (= (listof ?first ?second ?x ?y)
> (listof 1 2 3 4))))
> There is 1 solution:
>    #1: ?FIRST=1, ?SECOND=2
>
> This works with general functions and relations as well:
>
> (defrelation places (?x (?y LIST)))
>
> (assert (places 1st-race (listof sea-biscuit jenny on-the-money
> limper)))
> (assert (places 2nd-race (listof un deux trois quatre     )))
>
> (retrieve all (exists (?x ?y ?list)
>                  (and (places ?race ?list)
>                       (= (listof ?first ?second ?x ?y) ?list))))
> There are 2 solutions:
>    #1: ?RACE=2ND-RACE, ?FIRST=UN, ?SECOND=DEUX
>    #2: ?RACE=1ST-RACE, ?FIRST=SEA-BISCUIT, ?SECOND=JENNY
>
>
> This doesn't have the full flexibility you ask for.  To do that, one
> would need to provide specialist support for the reasoning.  That
> wouldn't be too hard to do, but it would take a little bit of work to
> make sure it was done robustly.  The basic primitives that I see
> would be:
>
>      append  list1 list2 list1+2
>      nth-element list n element
>
> How important is the rest-of functionality?
>
>
>