Artificial Intelligence With Python: Logic Programming- Part 2 (Examples)

Artificial Intelligence With Python: Logic Programming- Part 2 (Examples)

Hi, everyone! I hope you are all doing well. This article will demonstrate to us a few examples of Logic Programming with Python using the kanren and sympy packages.

Matching Mathematical Expressions- Example 1

Logic programming is a really efficient approach to finding the unknowable values. Let us begin our examples of Logic Programming. As always, we kick off by importing all necessary packages into our script instance:

from kanren import run, eq, membero, var, conde        

It is compulsory to define variables; this can be done as follows:

x = var()        

Essentially, we are constructing a mathematical operation in which unknown values are to be found. It follows the same logic as equations in mathematics, but with Python programming.

With Kanren, we make use of the run() function to evaluate a logic block. To provide you with a better understanding of the run() function, the built-in documentation is as follows:

We may proceed to create an equation as follows:

run(1, x, eq(x, 5))        

The above line of code is doing the following:

It will run a logic block of code that is seeking to find one answer (value) for the variable called x which is in the equation x = 5

So logically speaking, to balance out the equation, x should be having a value of 5. Because 5 == 5. So, let us print the output of the run operation.

print(run(1, x, eq(x, 5)))        

The output to the above line of code is as follows:

And as we expect, the output is a tuple in which there is one item that has a value of 5.

Matching Mathematical Expressions- Example 2

We begin by importing the necessary packages and dependencies:

from kanren import run, eq, membero, var, conde        

Thereafter, we declare our variables:

x = var()
z = var()        

Next, we set up an equation to evaluate:

run(1, x, eq(x, z), eq(z, 3))        

Finally, we may print the results of the solved logic:

print(run(1, x, eq(x, z), eq(z, 3)))        

With Kanren, it is possible to employ numerous goals and multiple variables at once. Looking at the above line of code we see:

We are running a block of logic. This query is going to search for one solution (value) to the equation that will enable z to be equal to 3. The value of x must also be equal to the value of z. Thus we are expecting the answer to be 3.

The output to the above block of code will show as follows:

Matching Mathematical Expressions- Example 3

The Kanren package utilizes a sophisticated technique called unification to find the solution to logic programs, which is basically an advanced form of pattern matching.

We begin by importing all necessary packages into our script instance:

from kanren import run, eq, membero, var, conde        

Next, we declare logic variables:

x = var()        

Then, we may set up an equation:

run(1, x, eq((1, 2), (1, x)))        

And finally, we may print the result of the equation out to the screen. But before, we do- let us first apply the logic mentally. So essentially, the run function that we have set up above is going to find one value for the unknown variable x, which is going to balance 1=2 & 1=x. Thus, we will expect x to take on a value of 2. And the output shows as follows:

Mathemetical Expressions- Example 4: Finding Prime Numbers

Below is a simple script for finding the prime numbers in a tuple of integer values. This is a simple logic program. Now you know, when we say that Python Programming Language makes it easier for a developer to code it means that we are able to get more done with less syntax. If you are looking to further your knowledge about Logic Programming, you may want to consider reading into Datalog Programming Language and ASP (Answer Set Programming) Language. These two programming language are purely Logic Programming Languages.

from kanren import isvar, run, membero
from kanren.core import success, fail, goaleval, condeseq, eq, var
from sympy.ntheory.generate import prime, isprime
import itertools as it

def prime_check(x):

    if isvar(x):
        return condeseq([(eq, x, p)] for p in map(prime, it.count(1)))
    
    else:
        return success if isprime(x) else fail

x = var()

print(run(10, x, prime_check(x)))        

In the last line of code in the above block, we are going to search for values which x can take on. And these values of x are meeting the set criteria, i.e., each value is a prime number. The output to the above code will show as follows:

If we would like to get the first 40 prime numbers, instead of 10, we may do as follows:

print(run(40, x, prime_check(x)))        

And output will show as follows:

I do hope that you found the concept of Logic Programming interesting.

Thank you for your time.

Shivek Maharaj

Yassine Fatihi ???????

Founded Doctor Project | Systems Architect for 50+ firms | Built 2M+ LinkedIn Interaction (AI-Driven) | Featured in NY Times T List.

1 年

Looking forward to exploring more examples of machine learning algorithms with you! Shivek Maharaj

Faraz Hussain Buriro

?? 24K+ Followers | Real-Time, Pre-Qualified Leads for Businesses | ?? AI Visionary & ?? Digital Marketing Expert | DM & AI Trainer ?? | ?? Founder of PakGPT | Co-Founder of Bint e Ahan ?? | ??DM for Collab??

1 年

Logic programming with Python is absolutely fascinating! Can't wait to see more examples of machine learning algorithms! ??

Stanley Russel

??? Engineer & Manufacturer ?? | Internet Bonding routers to Video Servers | Network equipment production | ISP Independent IP address provider | Customized Packet level Encryption & Security ?? | On-premises Cloud ?

1 年

Logic programming with Python opens up a fascinating realm of problem-solving by blending formal logic with programming techniques. Through examples using the kanren package, we witness its versatility and power. From solving equations to advanced pattern matching and even identifying prime numbers, logic programming offers a declarative approach to tackling complex problems. As we delve deeper into this paradigm, the possibilities for innovation and creative problem-solving seem boundless. How do you envision incorporating logic programming into your projects, and what challenges do you anticipate? Share your insights and experiences with us as we continue to explore the frontiers of artificial intelligence and programming.

Choy Chan Mun

Data Analyst (Insight Navigator), Freelance Recruiter (Bringing together skilled individuals with exceptional companies.)

1 年

Absolutely fascinating topic! Can't wait to see more examples of machine learning algorithms. ?? Shivek Maharaj

Fascinating journey into logic programming! How can we integrate this approach into everyday coding? Shivek Maharaj

要查看或添加评论,请登录

Shivek Maharaj的更多文章

社区洞察

其他会员也浏览了