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

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from