Re: Storing data and code in a Db with LISP-like interface

From: Nick Malik [Microsoft] <nickmalik_at_hotmail.nospam.com>
Date: Sat, 22 Apr 2006 12:50:35 -0700
Message-ID: <mtmdnfuqeZ1sGtfZRVn-qg_at_comcast.com>


Hi Neo,

You mentioned your 'judge' example in one of the other threads, so I thought I'd answer your challenge.

Your code:

>
> // Create type person.
> (create type instance (new))
> (create (it) name (findElseAdd name instance 'person'))
>
> // Create person instance.
> (create person instance (new))
> (create (it) name (findElseAdd name instance 'john'))
>
> // Create the attribute/column "like" and classify it as a verb.
> // Note: Unlike RM, where each attribute is attached
> // to every tuple in a relation, in the experimental db
> // the attribute is attached to things on an individual basis.
> (create verb instance (new))
> (create (it) name (findElseAdd name instance 'like'))
>
> // Create type [food] entry.
> (create type instance (new))
> (create (it) name (findElseAdd name instance 'entry'))
>
> // Create an entry named 'leftOver1'.
> (create entry instance (new))
> (create (it) name (findElseAdd name instance 'leftOver1'))
>
> // Create type fruit.
> (create type instance (new))
> (create (it) name (findElseAdd name instance 'fruit'))
>
> // Create a fruit entry named apple1.
> (create entry instance (new))
> (create fruit instance (it))
> (create (it) name (findElseAdd name instance 'apple1'))
>
> // Create type vegetable.
> (create type instance (new))
> (create (it) name (findElseAdd name instance 'vegetable'))
>
> // Create a vegetable entry named broccoli1.
> (create entry instance (new))
> (create vegetable instance (it))
> (create (it) name (findElseAdd name instance 'broccoli1'))
>
> // Create a fruit/vegetable entry named tomato1.
> (create entry instance (new))
> (create fruit instance (it))
> (create vegetable instance (it))
> (create (it) name (findElseAdd name instance 'tomato1'))
>
> // Create john likes lefover1.
> (create john like leftOver1)
>
> // Create john likes apple1.
> (create john like apple1)
>
> // Create john likes broccoli1.
> (create john like broccoli1)
>
> // Create john likes tomato1.
> (create john like tomato1)
>
> // Find which entries john likes.
> // Either of the below expressions
> // displays leftOver1, apple1, broccoli1, tomato1
> // in 4 consecutive dialog boxes.
> (msgbox (select john like *))
> (msgbox (and (select john like *)
> (select entry instance *)))
>
> // Find which fruit entries john likes.
> // Displays apple1 and tomato1.
> (msgbox (and (select john like *)
> (select fruit instance *)))
>
> // Find which vegetable entries john likes.
> // Displays broccoli1 and tomato1.
> (msgbox (and (select john like *)
> (select vegetable instance *)))
>
> // Find which fruit/vegetable entries john likes.
> // Displays tomato1 in a dialog box.
> (msgbox (and (select john like *)
> (select fruit instance *)
> (select vegetable instance *)))
>
> If someone is interested in what the above actually looks like when
> viewed in the db's exe, I can email it (script, db file and db exe fit
> on a single floppy).

  likes(john,fruit, X) :- fruit(X).
  likes(john,vegetable, X) :- vegetable(X).
  likes(john, leftover).

  fruit(apple).
  fruit(tomato).
  vegetable(broccoli).
  vegetable(tomato).

   //find a fruit that john likes
   ?- likes(john, fruit, Y)
   reply

      Y = apple

   //find a vegetable that john likes
   ?- likes(john, vegetable, Y)
   reply
     Y = broccoli

   // does john like broccoli?
   ?- likes(john, _, broccoli)
   reply
     true.

   // does john like any vegetable?
   ?- likes(john, vegetable, _)
   reply
     true.

   // is tomato a vegetable?
   ?- vegetable(tomato).
   reply

      true.

Believe it or not, in Prolog, it is a bit tougher to ask: what are all the fruits that John likes, because a unique search is not built in to traditional Prolog. That said, it is not difficult to do, and I have done this in the past. I'm not an expert at Prolog anymore, but I'll try here:   isin(A, [A|_]).
  isin(A, [_|B]) :- isin(A, B).

  allanswers(Verb,Paramlist,Resultlist) :- execute(Verb, Paramlist, Result), 
isin(Result, Resultlist), !, fail.
  allanswers(Verb,Paramlist,[Result,Results]) :- execute(Verb, Paramlist, 
Result), execute(Verb, Paramlist,Results).   allanswers(Verb,Paramlist,[Result]) :- execute(Verb, Paramlist, Result).

Given these statements, then to ask the question: what are all the vegetables that john likes would look like this:   ?- allanswers(likes,[john, vegetable, B], N)   result
    N = [broccoli, tomato]

All fruits that john likes would look like this   ?- allanswers(likes,[john, fruit, B], N)   result
   N = [apple, tomato]

All foods that john likes would look like this   ?- allanswers(likes,[john, _ , B], N)
  result
   N = [apple, tomato, broccoli, leftover]

I hope this helps.

  • Nick
Received on Sat Apr 22 2006 - 21:50:35 CEST

Original text of this message