Professional Documents
Culture Documents
Ai Lecccnew5
Ai Lecccnew5
Session Outlines
Introduction.
Variables and expressions.
Facts and Clauses.
Logical Functions.
Exercise.
Introduction
pyDatalog is a powerful logic language with very few syntactic elements, mostly coming from Python.
pyDatalog statements are declarative : they describe the result we want, leaving to the computer the task of
finding the appropriate solutions.
In [1]:
!pip install pyDatalog
In [2]:
from pyDatalog import pyDatalog as py
In [3]:
py.create_terms('X,Y')
In [4]:
# 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
True | False
In [5]:
py.create_terms('parent,ancestor,brother, X,Y,Z')
In [6]:
+parent('bill','john adams')
The next step is to insrts facts: The following statement inserts the fact that John Adams is the parent of Bill :
parent(son,father)
In [7]:
print(parent('bill',X))
X
----------
john adams
The following rule explain when X is a brother of Y : if Z is the parent of X,and Z is the parent of a person Y, and X
is not Y.
In [8]:
brother(X,Y) <= (parent(X,Z)) & (parent(Y,Z)) & (X!=Y)
Out[8]:
brother(X,Y) <= parent(X,Z)&parent(Y,Z)&!=(X,Y)
In [9]:
print(brother('bill',Y))
[]
In [10]:
+ parent('Mark','john adams')
In [11]:
print(brother('bill',Y))
Y
----
Mark
The following predicates explain recursively when Y is an ancestor of X : either Y is the parent of X, or Y is the
parent of a person Z who is an ancestor of X.
In [12]:
ancestor(X,Y) <= parent(X,Y)
ancestor(X,Y) <= parent(Z,Y) & ancestor(X,Y)
Out[12]:
ancestor(X,Y) <= parent(Z,Y)&ancestor(X,Y)
In [13]:
print(ancestor('bill', X))
X
----------
john adams
In [14]:
+ parent('john adams','George')
In [15]:
print(ancestor('bill', X))
X
----------
john adams
Exercise
ahmad 90
sami 45
khaled 60
carol 70
mohamad 80
Enter the above data using pydatalog and write a quaries for the following:
Define Terms
In [16]:
py.create_terms('X,Y,marks,grades')
Insert Facts
In [17]:
+marks('ahmad',90)
+marks('sami',45)
+marks('khaled',85)
+marks('carol',70)
+marks('mohammad',80)
Queries
In [18]:
print(marks(X,Y))
X | Y
---------|---
mohammad | 80
carol | 70
khaled | 85
sami | 45
ahmad | 90
In [19]:
print(marks(X,80))
X
--------
mohammad
In [20]:
print(marks('sami',X))
X
--
45
Rules
In [21]:
grades(X,'A') <= marks(X,Y) & (Y>= 90) & (Y<= 100)
grades(X,'B') <= marks(X,Y) & (Y>= 80) & (Y< 90)
grades(X,'C') <= marks(X,Y) & (Y>= 70) & (Y< 80)
grades(X,'D') <= marks(X,Y) & (Y>= 60) & (Y< 70)
grades(X,'F') <= marks(X,Y) & (Y< 60)
Out[21]:
grades(X,'F') <= marks(X,Y)&<(Y,'60')
In [22]:
print(grades(X,Y))
X | Y
---------|--
sami | F
carol | C
khaled | B
mohammad | B
ahmad | A
In [23]:
print(grades(X,'F'))
X
----
sami
Logical Function
A function defines one value for a given argument. It is similar to a python dictionary.
In [25]:
py.create_terms('manager,count_of_direct_reports,X,Y,Z,FirstStd')
In [27]:
# the manager of bill is George ann. Bill has only one manager.
+ (manager['bill']=='George ann')
In [29]:
print(manager['bill']==X) # X is 'John Adams'
X
----------
George ann
In [33]:
py.create_terms("N,Factorial")
Factorial[1]=1
Factorial[N]=N*Factorial[N-1]
In [34]:
from pyDatalog import pyDatalog as py
py.create_terms('Factorial,N')
Factorial[N]=N*Factorial[N-1]
print(Factorial[3]==N)
N
-
6
In [37]:
print(Factorial[5]==N)
N
---
120
Aggregate functions
In [38]:
+manager('tom','marry')
+manager('sam','marry')
+manager('marry','john')
In [39]:
(count_of_direct_reports[Y]==len_(X))<=manager(X,Y)
print(count_of_direct_reports['marry']==Z)
Z
-
2
sum_ (Y, for_each=Z)) : sum of Y for each Z. (Z is used to distinguish possibly identical Y values)
sum_ (Y, for_each=Z)) : sum of Y for each Z. (Z is used to distinguish possibly identical Y values)
In [40]:
(FirstStd[''] == max_(X, order_by= Y))<= (marks(X,Y))
Out[40]:
FirstStd[1]==!2(*,X,Y) <= marks(X,Y)
In [41]:
print(FirstStd['']==Z )
Z
-----
ahmad
In [ ]: