mushroompot

mushroompot

全栈,全干,全菜。 be a fullstack man, just for fun!
twitter

"Quick Reading Notes for Introduction to Logic Programming"

logic programming#

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-based programming, but at that time, my focus was mainly on learning the Prolog language.

Can logic programming be used as a database?#

Unlike other Prolog books I have read before, this book starts by explaining logic-based programming from a perspective similar to that of a database. Typically, Prolog books use classic family relationships as a starting point to illustrate the charm of logic-based programming, but this book begins with datasets.
The book clearly states the form of logic-based programming, which is:
facts + rules + query
From the form of logic-based programming, we can see its close connection with databases. Let's recall the form of a relational database, using MySQL as an example:

Assuming we have a database
--------------------------
-- iD -- name -- age -----
-- 0  -- tim  -- 32  -----
-- 1  -- ada  -- 23  -----
-- 2  -- tom  -- 23  -----
--------------------------
Now, if we use a MySQL query to retrieve all information about the customer with the name 'tim'
select * from person where name = 'tim';
We would 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 obtain the query result.
However, constructing facts in this way may have some issues. Taking person(0,tim,32) as an example, we cannot determine the meaning of each value, such as id and age. We can construct them 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. Of course, from this example alone, Prolog does not have many advantages compared to MySQL, and it may even add some storage burden.
We can see that when it comes to 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-based programming).

Classic Example of Family Relationships#

Let's imagine a large family tree and design a family relationship:

% Some facts about the family relationship
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 encounter some logical problems, using logic-based programming can greatly reduce our time. For example, when we want to query the grandparent relationship, we only need to write this rule:

grandparent(X,Y) :- parent(X,Z),parent(Z,Y).

% This rule explains the logical relationship of grandparent,
% which is essentially the grandparents of parents.

From here, we can feel some advantages of logic-based programming. When we write programs, we are actually writing rules of logical constraints, and the engine of logic-based 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 indeed 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 Structures#

The book then goes on to discuss three types of data structures: lists, sets, and trees. This is not much different from other books that introduce 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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.