logic programming#
I spent more than a day reading the book "Introduction to Logic Programming" (referred to as the book hereafter). I had previously read books on logic programming, but at that time, I mainly focused on learning the Prolog language.
Can logic programming be used as a database?#
Unlike the Prolog books I have read before, this book starts by explaining logic programming from a perspective similar to a database. Typically, Prolog books explain the charm of logic programming starting from classic family relationships, but this book starts with datasets.
The book clearly states the form of logic programming, which is:
facts + rules + query
From the form of logic programming, we can see its close connection with databases. Let's recall the form of a relational database, taking MySQL as an example:
Assume we have a database
--------------------------
-- iD -- name -- age -----
-- 0 -- tim -- 32 -----
-- 1 -- ada -- 23 -----
-- 2 -- tom -- 23 -----
--------------------------
Now, using a MySQL query, we can retrieve all the information of the customer with the name 'tim'
select * from person where name = 'tim';
We can get
--------------------------
-- iD -- name -- age -----
-- 0 -- tim -- 32 -----
--------------------------
If we express the above process using Prolog, we can construct datasets like this:
% facts
person(0,tim,32).
person(1,ada,23).
person(2,tom,23).
% query
person(X,tim,Y).
% result
X = 0.
Y = 32.
As described above, in Prolog code, we first construct a set of facts, then query the desired variables based on these facts, and finally, we can obtain the query result.
However, constructing facts in this way may have some problems. Taking person(0,tim,32) as an example, we cannot determine the meaning of each value. For example, we cannot tell that 0 represents the ID and 32 represents the age. We can construct it in the following way:
% facts
person(id(0),name(tim),age(32)).
person(id(1),name(ada),age(23)).
person(id(2),name(tom),age(23)).
% query
person(X,name(tim),Y).
% result
X = id(0).
Y = age(32).
This way, we can indicate the meaning of each value in each position.
Of course, from this example alone, Prolog does not have many advantages compared to MySQL. Instead, it may add some storage burden.
We can see that when we only do simple queries like this, Prolog cannot demonstrate its advantages. We still need to use the classic example of family relationships to showcase certain features of Prolog (logic programming).
Classic Example of Family Relationships#
We can imagine a large family tree, so let's design a family relationship:
% Some facts about family relationships
male(charles).
male(william).
male(peter).
male(henry).
male(andrew).
male(edward).
male(viscount).
male(savanna).
female(elizabeth).
female(anne).
female(zara).
female(beatrice).
female(eugenie).
female(louise).
female(isla).
parent(elizabeth,charles).
parent(elizabeth,anne).
parent(elizabeth,andrew).
parent(elizabeth,edward).
parent(anne,peter).
parent(anne,zara).
parent(charles,william).
parent(charles,henry).
parent(andrew,beatrice).
parent(andrew,eugenie).
parent(edward,louise).
parent(edward,viscount).
parent(peter,savanna).
parent(peter,isla).
% Here, male represents males, female represents females, and parent(X,Y) represents X being the parent (father or mother) of Y.
When we face some logical problems, using logic programming can greatly reduce our time. For example, when we want to query the grandparent relationship, we just need to write this rule.
grandparent(X,Y) :- parent(X,Z),parent(Z,Y).
% This rule explains the logical relationship of grandparent, which is actually the grandparents of parents.
From here, we can feel some advantages of logic programming. When we write programs, we are actually writing rules of logical constraints, and the engine of logic programming languages (such as Prolog) can search for corresponding solutions based on these rules (essentially searching).
Updates#
After explaining facts and queries, the book proceeds to explain updates, so the book does introduce logic programming from a database perspective.
The book provides an example of update syntax:
p(a,b) & ~q(b) ==> ~p(a,b) & p(b,a)
p(X,Y) & ~q(Y) ==> ~p(X,Y) & p(Y,X)
This way, we update the rules in the database.
Data Structure#
The book then goes on to discuss three types of data structures: lists, sets, and trees. This is not much different from other books introducing Prolog.
Conclusion#
"Introduction to Logic Programming" is worth reading. By reading this book, you can gain a good understanding of logic programming. It is a good choice to read this book before reading other Prolog-related books.