Skip to main content

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.


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

Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p