python logical objects?

Martin von Loewis loewis at informatik.hu-berlin.de
Mon May 29 11:49:34 EDT 2000


"Michal Wallace (sabren)" <sabren at manifestation.com> writes:

> 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.

One common approach is to model this closely to the real world. For
every interaction between two object in the real world, also have an
interaction between the programming objects. You'll find that
interactions are often drawn using sequence charts during design.

If such stuff interests you, I recommend reading a book about OO
analysis and design. They often propose a notation in the book as
well; I'd chose one that use UML these days.

> 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?

So how does the interaction work in your store? The customer picks
some products. What next:

a) the store operator suggests: together with what you've got, you can
   get such and such for a reduced price.
b) the customer needs to select the additional products herself, and
   the sale is only considered at the cashier.

In the second case, it is really the salesperson that knows all the
sales, and compares them with the goods purchased. So clearly, the
algorithm computing the total needs to have knowledge about all the
products being purchased, and special sales available.

> 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.)

The problem may not be the way to write this down, but the algorithm
to compute the total. There are a number of options for such
algorithm, such as:

a) take one special after another, and see whether they match. If they do,
   compute the price for the special, and check-mark the goods involved.
   When all special sales are done, add the price of the remaining goods.

b) try to find the minimum price, in case there is overlap between the
   specials. There are probably different algorithms to do so, one is:
   detect all special sales that potentially apply. Then apply algorithm
   a) for all permutations of the sales.

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

It appears that continuations, as provided by the stackless Python,
offer a nice approach to back-tracking. It is not that much the
presentation of the facts that is tricky - I believe an OO language
does much better than Prolog here. It is the problem solving
algorithm, which is partially implicit in the Prolog system, but more
explicit in an OO language. Having back-tracking to explore the
solution space should give you equivalent expressive power.

Regards,
Martin




More information about the Python-list mailing list