0% found this document useful (0 votes)
6 views6 pages

Expeirment - 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Expeirment - 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 9: Write a program to understand

Prepositional logic using KANREN, SYMPY,


pyDatalog packages in Python

What is Logic Programming?


 Logic programming is a programming paradigm that sees
computation as automatic reasoning over a database of knowledge
made of facts and rules.
 It is a way of programming and is based on formal logic.
 A program in such a language is a set of sentences, in logical form,
one that expresses facts and rules about a problem domain.
 Among others, Datalog is one such major logic programming
language family.

Structure
Let’s talk about facts and rules. Facts are true statements — say, Bucharest
is the capital of Romania. Rules are constraints that lead us to conclusions
about the problem domain. These are logical clauses that express facts. We
use the following syntax to write a rule (as a clause):
H -> B1, …, Bn.
We can read this as:
H if B1 and … and Bn.
Here, "H" is the head of the rule and "B1, …, Bn" is the body. A fact is a rule
with no body:
H.
An example would be:
fallible(X) -> human(X)
Every logic program needs facts based on which to achieve the given goal.
Rules are constraints that get us to conclusions.

Logic and Control


Think of an algorithm as a combination of logic and control.
Algorithm = Logic+Control
In a pure logic programming language, the logic component gets to the
solution alone. We can, however, vary the control component for other
ways to execute a logic program.

Getting Started With Python


Gearing up for logic programming with Python, we will install a couple
of packages. Let’s use pip for this.

 Kanren: It lets us express logic as rules and facts and simplifies


making code for business logic.

pip install kanren

 SymPy: This is a Python library for symbolic mathematics. It is


nearly a full-featured Computer Algebra System.

pip install sympy

Examples of Logic Programming

The following Python code will help you match a mathematical expression

Consider importing the following packages first –
from kanren import run, var, fact
from kanren.assoccomm import eq_assoccomm as eq
from kanren.assoccomm import commutative, associative

We need to define the mathematical operations which we are going to use −


addition = 'add'
multiplication = 'mul'
Both addition and multiplication are communicative processes. Hence, we
need to specify it and this can be done as follows −
fact(commutative, multiplication)
fact(commutative, addition)
fact(associative, multiplication)
fact(associative, addition)
It is compulsory to define variables; this can be done as follows −
x, y = var('a'), var('b')
We need to match the expression with the original pattern. We have the
following original pattern, which is basically (5+a)*b −
OriginalPattern = (multiplication, (addition, 5, x), y)
We have the following two expressions to match with the original pattern −
ex1 = (multiplication, 2, (addition, 3, 1))
ex2 = (addition,5,(multiplication,8,1))
Output can be printed with the following command −
print(run(0, (x,y), eq(OriginalPattern, ex1)))
print(run(0, (x,y), eq(OriginalPattern, ex2)))
After running this code, we will get the following output –
((3,2))
()

Checking for Prime Numbers


With the help of logic programming, we can find the prime numbers from a list of
numbers and can also generate prime numbers.
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_test(n):
if isvar(n):
return condeseq([(eq,n,p)] for p in map(prime,it.count(1)))
else:
return success if isprime(n) else fail
n=var() #Variable to use
set(run(0,n,(membero,n,
(12,14,15,19,21,20,22,29,23,30,41,44,62,52,65,85)),(prime_test,n)))

PyDatalog: pyDatalog is a powerful language with very few syntactic


elements, mostly coming from Python
To Install PyDatalog
pip install pyDatalog

The first step is to import pyDatalog:


from pyDatalog import pyDatalog

Variables and expressions


The next step is to declare the variables we'll use. They must start with an
upper-case letter:

pyDatalog.create_terms('X,Y')

Variables appear in logic queries, which return a printable result

# give me all the X so that X is 1


print(X==1)
X
-
1

Queries can contain several variables and several criteria ('&' is read 'and'):

# give me all the X and Y so that X is True and Y is False


print((X==True) & (Y==False))
X|Y
-----|------
True | False

Some queries return an empty result :

# give me all the X that are both True and False


print((X==True) & (X==False))
[]

# give me all the X and Y so that Y is 1 and Y is X+1


print((Y==1) & (Y==X+1))
[]
To use your own functions in logic expressions, define them in Python, then
ask pyDatalog to create logical terms for them:

def twice(a):
return a+a
pyDatalog.create_terms('twice')
print((X==1) & (Y==twice(X)))
X|Y
--|--
1|2

Loops
Let's first declare the Variables we'll need:
A loop can be created by using the .in() method (we'll see that there are
other ways to create loops later):

from pyDatalog import pyDatalog


pyDatalog.create_terms('X,Y,Z')
# give me all the X so that X is in the range 0..4
print(X.in_((0,1,2,3,4)))
X
-
0
1
3
2
4

Here is the procedural equivalent

for x in range(5):
print(x)
0
1
2
3
4
Logic Functions :
A function defines one value for a given argument. It is similar to a python
dictionary.
A function can be queried.
from pyDatalog import pyDatalog
pyDatalog.create_terms('X,Y,Z, salary, tax_rate, tax_rate_for_salary_above,
net_salary')
salary['foo'] = 60
salary['bar'] = 110
# give me all the X and Y so that the salary of X is Y
print(salary[X]==Y)
X|Y
----|----
bar | 110
foo | 60
# python equivalent:
_salary = dict()
_salary['foo'] = 60
_salary['bar'] = 110
print(_salary.items())
[('foo', 60), ('bar', 110)]

You might also like