Pondering Prolog

Recently, I saw a talk about Prolog and the speaker was trying to make a connection between web development and this language. Prolog has facts, rules, and queries. The connection that was being made was between queries and SQL. I learned a little about the language and got the examples to run on my machine using SWI-Prolog. Though the point was tenuous, let me give my take on what we can see from the languages and queries.In Prolog, facts (clauses) are basically data or variables. Rules (also clauses) are conditionals such as those which check for a certain state of boolean logic. Queries call upon the rules to check the facts.

For example, let’s say the facts are named Homer, Bart, and Lisa that like a soda or energy drink. The queries act upon the rules which are applied to compare whether they have the same taste in drinks.

This could be shown like so:

likes(homer,soda).

likes(bart,energy drink).

likes(lisa,soda).

taste(X, Y) :- likes(X, Z), likes(Y, Z).

After loading the script in Prolog, we can query if these people have similar tastes based on their drink of choice:

taste(homer,bart).

false.

taste(homer,lisa).

true.

Homer (variable X) and Bart (variable Y) do not like the same drink (variable Z) based on the false result due to that facts that Homer likes soda and Bart likes energy drink. The rule above checks to see if the variables of X and Y each have the variable Z in common. The facts (variables) are like the columns of a database table.

Now let’s look at an MySQL table to see if we can get a similar result using an SQL query:

The table is named simpsons which contains First_ Name and Drinks columns. The names and drinks data would be inserted into the respective columns that we used from the Prolog example above.

We could then setup a query like so: 

SELECT * FROM simpsons WHERE drinks=’soda’;

This should give us the result from the simpsons table like:

+——————+————-+

|First_Name | Drinks  |

+——————-+————-+

| homer          |  soda    |

+——————–+————+

| lisa                 | soda      |

+———————+————-+

As you see we get a table that shows all rows that have soda in common.

If we set up a query in SQL like this:

SELECT * FROM simpsons WHERE drinks !=’soda’;

This should give us the result from the simpsons table like:

+——————-+———————-+

| First_Name |Drinks              |

+——————–+———————-+

| bart               |  energy drink |

+———————+———————-+

As you see we get a table that shows all rows that do not have soda in common and only Bart with an energy drink is displayed.

This is not a pure true or false output like the results of the Prolog query we made earlier. Why? Prolog is a 2 value logic (2VL) system (true or false), yet SQL is a 3 value logic (3VL) system comprised of true, false, and null values. A true result will display an output; but if you check for a value (like the above != query) in a column of a table and do not get an output (an empty result set), then it is like a false result.  A null value is also a third possibility in a boolean test of a database.

If we added Marge with a null value in the Drink column to the table and we ran a query of :

SELECT * FROM simpsons WHERE drinks !=’soda’;

This will give us the result from the simpsons table like:

+——————-+———————-+

| First_Name |Drinks              |

+——————–+———————-+

| bart               |  energy drink |

+——————–+———————-+

| marge           | null                  |

+——————–+———————-+

The above data set shows the possibility of a null value in the table making a test for a pure boolean condition more difficult and requiring more manipulation of the query to get the null to be seen as a false result (depending on the particular databases’ query language).

These two language examples are not exactly equivalent due to the constraints of the logic value systems; though you may notice that whether using Prolog or SQL, we can get to what we want to know from the data of who likes soda and who doesn’t. I hope this shows a way that queries from a language that is primarily used in artificial intelligence and one that is used primarily for databases to get a similar result from the same data. Happy coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s