sabren.net   rants   archive   bio   portfolio


« index »

Python Logical Objects? [0529.2000]

(posted to comp.lang.python)

Hey all,

I've been thinking about how objects relate to one another, and it occurred to me that there's a use for "ad hoc" rules.

SPAM AND EGGS

For example, suppose you maintain an online grocery store, and the manager comes to you and says she wants to offer a sale: buy two eggs and get a free can of spam. How do you code it? Is it a method of a Product or Cart or Spam or Egg class? Does it go in a Specials class? Some kind of Coupon mechanism? What if its a temporary sale, and next week you get one free egg with every can of spam?

Or do you just tell the client that the software isn't designed for that kind of thing, and they'll have to handle it manually, or not at all?

This kind of ad-hoc rule seems to be the bread and butter of logical languages like Prolog. For example:

    price(spam, 0) :- bought(eggs,2), !.
    price(spam, 1).

Which roughly means: "The price of spam is 0 if you bought 2 eggs. Otherwise, the price of spam is 1". (I'm only vaguely familiar with prolog, so this might not be the "best" way to do this, but it works.)

In an object-oriented language, I'd usually want to have a .getPrice() method on a Product class... But this spam-and-eggs situation isn't so simple. If we leave getPrice attached to Product, we'll have to pass it some kind of context-representing object, perhaps a reference to a shopping cart. Not only that, Spam would have to be a subclass of Product, or contain some kind of PricingStrategy object. Neither option is really as elegant as the two line prolog script. It almost doesn't seem worth it to code a temporary rule like this.

GAMES

I wish I could see the source for PySol while I'm writing this, but the actual home page seems to be down at the moment. From what I understand, PySol is a card game program that can play many different sets of games.

I'm curious how the author represents rules for all those games. I would guess that he's created a Card class (or perhaps a Deck class) and then he somehow represents the rules of a game as an object, but I won't know until I track down the source.

I wonder if it would be useful to have the rules stored in a prolog-style rule base. I suspect that it would be a little more concise than doing it with objects.

I also suspect this kind of rules-on-top-of-objects thinking would simplify the task of writing "collect stuff and solve puzzle" adventure games. Game objects could have still have their own attributes and behaviors (descriptions, point values, etc) but the relationships between the objects could be summed up in a tidy rulebase.


I haven't thought much further than this, so the idea may have no merit whatsoever in practice... But, I'd like to try it out.

I've found PyLog, which connects to a prolog interpreter over a socket. Has anyone else done anything prolog-ish with Python?

Better yet: is anyone else interested in doing something prolog-ish with python?


« index »