Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: AI in Perl - proof of concept

by John M. Dlugosz (Monsignor)
on Jul 15, 2002 at 20:34 UTC ( #181911=note: print w/replies, xml ) Need Help??

in reply to AI in Perl - proof of concept

In college, a programming assignment was to write an inference engine in Pascal. Although everyone else in class thought it was a huge deal (a semester-long project) and turned in huge monstrosoties of programs, I thought it was a simple, beautiful, elegant problem, and made a 5-page solution.

So, I think it's easy enough to implement reusable logic to go through a fact base. It would be easy to represent the facts in Perl, too.

The cool thing that Prolog does is "variable unification", and Perl has nothing like that. There was a Perl6 RFC that was dismissed as being too complex and still not complete enough to be worthwhile, and I reciently read about the backtracking scope in the new regex and wondered if that might be made more general, but it's not really the same thing.

I wonder, though, if full-blown Unification is needed to solve the problems with an inference-engine approach. The big deal about it is that it makes tail-end recursion possible (e.g. no cleanup step after the call returns), while doing the same thing with "undo" code living on the stack would mean you really do need to keep a stack of everything you've been though. Today machines have millions of times more storage, so is that an issue?

Also, you would want "how" and "why" features on your inference engine.

You have your fundimental facts and your rules stored in different ways—lists and subs. The subs are hand-written. How is that reusable? I think it illustrates a "pattern" commonly found in code, to solve a complex problem in a top-down manner.

However, I think it would be more interesting to start by expressing your rules as Perl data, in a simple and natural manner:

[owns => 'merlyn', 'gold'], [valuable => 'gold'], [[steals_from => '$X', '$Y'] => [thief => '$X'], [rich => '$Y']
and come up with a fully reusable function that takes this data and makes inferences on it. Here, I used '$X' etc. as placeholders, like variables in Prolog or the questionmark stuff in CLIPS. Seems like you could generate source code on the fly based on the rule!


Replies are listed 'Best First'.
Re: Re: AI in Perl - proof of concept
by Ovid (Cardinal) on Jul 15, 2002 at 21:09 UTC

    Your points are perfectly correct. I have no intention of simply being able to add facts to the database and forcing the end user to write the rules by hand. This was merely a "proof of concept" to test some ideas. I'm actually preparing a much longer write-up with the intention of starting some useful discussion.

    Auto-generating some of the rules will be a must. If I have the following facts:

    drinks(lemming,scotch). drinks(ovid,scotch). drinks(grep,guiness).

    I should be able to auto-generate queries like the following:

    ?- drinks(ovid,scotch). yes

    And with unification (associating variables with items):

    ?- drinks(grep,Drink). Drink=guiness yes ?- drinks(Who,scotch). Who=lemming Who=ovid yes

    Those should be fairly easy to auto-generate. However, some rules have to be crafted by hand:

    steals(Perp,Item) :- thief(Perp), owns(Victim,Item), Valuable(Item).

    The problem there will be to create an easy to use (and parse!) syntax that supports this. You mention that my facts and rules are stored different ways and I like your example, but I'll have to play with it to see if that's what I really want.

    You also bring up the tail-recursion issue (tail recursion, for those who are wondering, is where the return value of a recursive call returns directly to the caller rather than propogating back through the call stack). You stated that machines today have "millions of times more storage", thus suggesting that this might not be an issue. I think it is an issue because full-blown AI systems can store millions of facts and being able to take advantage of any optimizations will be critical, as Perl is not well-suited to this task. That's an issue I think I can put off for later, though.

    In the meantime, I'll have a fairly detailed post later this evening outlining some of the issues that I need to tackle before I can really begin serious work on this.


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://181911]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2019-07-17 14:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found