Various AI::Prolog thoughts

Ovid on 2005-05-27T01:15:22

I just had an epiphany about how to make the AI::Prolog parser work much better. It would be cleaner and faster and probably allow for proper operators. The latter would allow me to do this:

X is Y + Z * 2.

Instead of:

is(X, plus(Y, mult(Z, 2))).

The latter, of course, is just plain ugly. I've also figured out a better way to handle precedence which might allow me to change the Prolog syntax at runtime (op/3 allows this in normal Prolog).

The only problem with my new idea is that I don't have time :(

In other news, Randal found a nifty solution to a weird problem. Someone wanted to count all instances of a string in another string, even if they were overlapping. So 'AA' can be found in 'AAAA' 3 times. Randal's solution, though Perl, is straight from Prolog:

sub match_count {
  my ($string, $pattern) = @_;
  my $n = 0;
  $string =~ /$pattern(?{ $n++ })(?!)/;
  return $n;
}

Neat, eh?


Re:

Aristotle on 2005-05-27T20:03:47

The latter, of course, is just plain ugly.

What are you talking about? </lisp-guy>

In other news, Randal found a nifty solution to a weird problem.

That’s straight from Mastering Regular Expressions, basically. :-)

Re:

Ovid on 2005-05-27T20:25:46

It might be from MRE (Meal, Ready to Eat?), but Randal did give credit specifically to Prolog :) In this case, it's a very common Prolog idiom to force something to fail in order to backtrack and cover all solutions:

person(bob).
person(alice).
person(joe).

report :-
  print('People:'), nl,
  person(X), print(X), nl, # fail forces this to be resatisfied
  fail.

Thus learning Prolog well enough makes solutions like Randal's seem natural.