Wednesday, July 23, 2008

SICP: not not but rather unless

What happens if you search for "not foo" on Google? Surprisingly enough, it works, but it doesn't lead to what you might expect. It leads to pages that contain the phrase "not foo". If you want pages that don't contain the word "foo", the syntax is "-foo". Of course, if you type in "-foo" by itself, you don't get any interesting results. Rather you have to write something like "spam -foo", which leads to pages that contain the word spam, but not foo.

While I was at Foxmarks, I had to implement the logical operators for our own search engine, which is where I originally encountered this problem. If the user asks you for "not foo", you really can't efficiently give him any sort of answer. It only works if the user tries something like "spam and not foo", which is what I implemented.

The SICP lecture I was watching last night was about implementing logic languages like Prolog in Lisp. The same problem happens there. "(not)" in that language acts as a filter. Hence, "(and (color ?c) (not (color blue)))" works, but "(not (color blue))" doesn't. It's the same problem. It's efficient to implement "not" as a filter, but not efficient to let the user ask for everything in the database filtered by "not".

I think the problem is that the word "not" is a bit misleading. If you don't understand its limitations, you might write something like "(and (not slow) (not buggy))", and find out that your code is either slow or buggy.

Drum roll please: I suggest using not "not", but rather "unless". That is basically what Google does, but they use "-" as syntax. It makes sense to write "spam unless foo" or in Lisp "(unless (color ?c) (color blue))". Since "unless" is a binary operator, it forces you to think in the right direction.

Ok, enough about that. Here are some other thoughts.

Last night I realized just how similar SPARQL (a query language for RDF) and MQL (the query language for Freebase) are to Prolog (or at least the logic language I saw last night). All three use "query by example" in order to query graph engines. You give it a pattern, and it checks against everything in the database looking for matches. If something matches, it gives you your original query back, but with all the blanks filled in.

For instance, the language last night let me write "(employee ?name (computer ?subdept))" to find all the employees who work in the computer department. It responded with a list of things like "(employee (Joe Hacker) (computer r-and-d))"

Here's a similar query in MQL:
{
"type" : "/music/artist",
"name" : "The Police",
"album" : []
}
It says give me all the albums by the musical artists "The Police".

I know that SPARQL (at least what little my buddy Drew Perttula has shown me) even allows you to express rules like Prolog does. For instance, (hand waiving) "if A's son is B, and B's son is C, then A is C's grandfather, and C is A's grandson". I can only guess that Prolog had some influence on MQL and SPARQL, but of course I don't know for certain.

One last comment. The "MIT opinion" on Prolog is "interesting". When backed into a corner and forced to comment, Abelson said something like "We wrote a logic-based language like Prolog back in 1971. After about six months, we decided it was not the right thing for various reasons. When Prolog came along, we figured they had fixed all those problems, but it turned out they hadn't. Now, what makes Prolog really nice is that it's fast because of really excellent compiler technology." Heh, interesting.

2 comments:

عبدالمنعم said...

You wrote ""(and (not slow) (not buggy))", and find out that your code is either slow or buggy."

I think you meant (not (and slow buggy)), as this will lead to either slow or buggy. (DeMorgan rule in boolean algebra)

And unless is a more confusing word then not. I think without would have been a better choice.

Ali said...

"""
I can only guess that Prolog had some influence on MQL and SPARQL, but of course I don't know for certain.
"""

Well, it makes sense, looking from the outside. An RDF Triple seems identical to an equivalent Prolog Fact. And they are queried similarly.