Re: Logic Programming for Perl6 (Was Re: 3 Good Reasons... (typo alert!))

2006-05-25 Thread A. Pagaltzis
* Ovid <[EMAIL PROTECTED]> [2006-05-25 20:45]:
> The first hurdle would be the syntax.  The programmer just
> looking at the code would need to know when one section of code
> represents a snippet of logic programming.  Is the following a
> function call or a Prolog fact?
> 
>   loves( 'foo', 'bar' );
> 
> How would one assert facts and rules in Perl6?  How would one
> know that a variable is a logic variable and not a normal one?

Don’t forget that in Perl 6, the parser grammar is wide open for
modification. That is not an opportunity to be used lightly, but
I believe logic programming is a sufficiently “big” and distinct
extension that it justifies dedicated syntax.

Regards,
-- 
Aristotle Pagaltzis // 


Re: Logic Programming for Perl6 (Was Re: 3 Good Reasons... (typo alert!))

2006-05-25 Thread Ovid
- Original Message 
From: David Romano <[EMAIL PROTECTED]>

> > duplicate results and this is almost always wrong. (See 
> > http://use.perl.org/~Ovid/journal/28378 
> > for an SQL example of this problem).
> I re-read your journal entry and comments (I had read it back when you
> first had posted it), and I'm curious about what obstacles that you
> think need to be overcome for Perl6 to support logic programming.

If anyone wants to know what the heck this is about, you can read 
http://search.cpan.org/dist/AI-Prolog/lib/AI/Prolog/Article.pod

That's an article I originally had published in The Perl Review 
(http://www.theperlreview.com/) and it explains the basics of logic programming 
and how to do it in Perl5.  It also explains the append/3 predicate, something 
I mention below.

The first hurdle would be the syntax.  The programmer just looking at the code 
would need to know when one section of code represents a snippet of logic 
programming.  Is the following a function call or a Prolog fact?

  loves( 'foo', 'bar' );

How would one assert facts and rules in Perl6?  How would one know that a 
variable is a logic variable and not a normal one?  Assignment to a logic 
variable which is still subject to rebinding could break code.  On the other 
hand, using normal variables for logic variables could let us use objects for 
them and I think this might get us contraint programming (long story).

There's also the question of non-logical behavior in logic programming.  What 
happens if you try to use math in Perl6 logic programming?  Generally speaking, 
math is "non-logical" in the sense that it's used in Prolog (see the 
aforementioned article).  Opening and reading from a file is also non-logical 
(you can't backtrack over it). How are those issues to be handled?

My biggest concern, though, is a rule like the following:

  customer_cities(City) :-
700 <= credit_rating(Customer),
customer(Customer, City).

That's logically equivalent to the SQL I posted in a previous email and, like 
that SQL, will very likely return duplicates.  Prolog students are constantly 
faced wtih the "how do I get rid of duplicates?" problem.  This results in 
inefficient queries and a lot of time spent culling the duplicate answers when 
the person just wants to know the list of cities which meets their requirements.

Perl6 grammars may be able to assist with this, but grammars (as far as I can 
tell) are still based around strings and not data structures.  Admittedly, 
Prolog is essentially a fancy string rewriting system, but it's not clear how 
the grammars would be applied to this problem.

For anyone familiar with logic programming and the append/3 predicate, here's 
one way of representing it in Perl5:

  use re 'eval';
  my $string = "abc";
  my $length = length $string;
  my $regex = qr/(\G[$string]{0,$length}(?{print "# [$&][$'][$string]\n"}))/ x 
2;
  $string =~ $regex;

Can you say "yuck"?  However, that only represents a limited subset of 
append/3's functionality and trying to generalize it would be a very difficult 
task.  One approach might be to write a wrapper around 
http://www.perlmonks.org/?node_id=318350.  That's an approach I found to 
leverage Perl5's regex engine for logic programming, but I discovered that 
Perl5's regexen aren't re-entrant, killing that project.  Perl6 rules might be 
perfect for this.

In short, there are lots of questions which would need to be answered to get 
logic programming done correctly in Perl6.  I'm quite happy that @Larry decided 
not to answer those questions since things would have been delayed.  We can 
hack in logic programming later.

Cheers,
Ovid

-- If this message is a response to a question on a mailing list, please send 
follow up questions to the list.
 
Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/






Re: Logic Programming for Perl6 (Was Re: 3 Good Reasons... (typo alert!))

2006-05-25 Thread Larry Wall
This topic may be better suited to perl6-language, unless you consider
its denizens to already be self-selected against logic programming. :)

Larry


Logic Programming for Perl6 (Was Re: 3 Good Reasons... (typo alert!))

2006-05-24 Thread David Romano

Hi Ovid,
On 5/24/06, Ovid <[EMAIL PROTECTED]> wrote:

As an aside for those who, like me, wanted to see support for logic 
programming:  the only significant disappoinment I have with Perl6 is also, 
oddly enough, accompanied by a sigh of relief.  Perl6 will easily support 
imperative, functional and OO syntax.  But what about logic programming?  Perl6 
rules might help, but there will be no support for things that Prolog and 
Mercury do naturally.

The "sigh of relief" stems from my realization that logic programming in Perl6 
would probably be implemented incorrectly because the easiest way to implement it is 
usually wrong, though this can be very tough to see at first.  It's my understanding that 
logic programming wasn't incorporated due to the desire to not delay the initial release 
of Perl6 (I could be wrong).  However, after having done a fair amount of work with logic 
programming, I realize that one of the greatest implementation mistakes with it is that 
results tend to be bags and not sets (SQL shares this problem).  Thus, when one naturally 
wants all of the answers to a query, one often gets duplicate results and this is almost 
always wrong. (See http://use.perl.org/~Ovid/journal/28378 for an SQL example of this 
problem).

I re-read your journal entry and comments (I had read it back when you
first had posted it), and I'm curious about what obstacles that you
think need to be overcome for Perl6 to support logic programming.

Just to let you know, I only used Prolog for a month during a course
called "Programming Languages: Principles and Paradigms", so I don't
have a strong background in it. From what I remember, everything is
about binding in Prolog: if the given data can bind to the predicate
(I think that's what it's called in Prolog) in some way, then the
predicate returns successfully. Why can't grammars (in the sense of
Perl6) be used for this kind of stuff (and here I'm asking about what
Mercury and Prolog can do naturally)? Is it that so much more needs to
be added to Perl6 grammars/rules for them to be as powerful and
versatile as predicates in logic programming, and that these additions
cannot be reconciled with the rest of the Perl6 language? I'm
interested in everyone's opinions.

David