Artificial Intelligence With Python: Logic Programming- Part 1
Shivek Maharaj
Data Analyst @ Old Mutual | Machine Learning & AI Specialist @ edX | AI Engineer { ?? ML ?? RL ?? DL ?? DS ?? NLP ?? CV }
Hi, everyone. I hope you are all doing well. The next few posts that we see will introduce us to an interesting concept called Logic Programming with Python.
We already know that logic is the study of rational reasoning, or to put it another way, it is the study of what follows what. For instance, if two claims are true, then any third statement can be deduced from them.
THE CONCEPT BEHIND LOGIC PROGRAMMING
The term “Logic Programming” combines the terms “Logic” with “Programming.” A programming paradigm is known as “logic programming” and uses program statements to express problems as facts and rules but inside a framework of formal logic. It is a certain method of approaching programming, just as other paradigms like object-oriented, functional, declarative, procedural, etc.
HOW TO USE LOGIC PROGRAMMING TO OVERCOME PROBLEMS
For problem-solving, logic programming uses facts and rules. They are referred to as the Logic Programming building blocks for this reason. In logic programming, a goal must be declared for each program. We need to be aware of the logic programming building pieces, or Facts and Rules, in order to comprehend how an issue might be resolved.
WHAT ARE THE FACTS?
Actually, in order to accomplish the specified objective, any logic program requires facts to operate with. Facts are essentially truthful assertions about the data and program. For instance, India’s capital is Delhi.
WHAT ARE THE RULES?
Actually, the limitations that allow us to draw conclusions about the problem area are the rules. Rules are essentially logical sentences that express different facts. For instance, all of the rules must be specified if we are creating a game.
To solve any problem in logic programming, rules are crucial. Basically, rules are logical conclusions that can express the facts. The syntax of the rule is as follows:
In the above rule, A is the head, and B1, B2,… Bn is the body.
For example:
领英推荐
This can be read as, for every X and Y, if X is the father of Y and Y is an ancestor of Z, X is the ancestor of Z. For every X and Y, X is the ancestor of Z, if X is the father of Y and Y is an ancestor of Z.
PYTHON PACKAGES FOR LOGIC PROGRAMMING
There are two popular packages in Python that are specifically designed for Logic Programming. Their information and installation processes are as follows.
1. KANREN
It gives us the means to make the business logic code we created simpler. It enables us to translate logic into facts and laws. You can install Kanren with the use of the following pip command:
pip install kanren
The PyPI website for kanren may be found here: PyPI Kanren
2. SYMPY
A Python library for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while keeping the code as straightforward as possible to make it understandable and simple to extend. You can install SymPy with the aid of the following pip command:
pip install sympy
The PyPI website for sympy may be found here: PyPI SymPy
And thus, we have reached the conclusion of information for this article.
We shall look at a few examples of logic programming in the next article.
Thank you for your time.
Shivek.