# This section uses pyDatalog to illustrate datalog queries # # To install pyDatalog do the following: # # install python # install pip (including prerequisites) # type pip install pyDatalog # # To verify installation, start python IDLE, then type: # # from pyDatalog import pyDatalog # # If no package not found error, the package is installed correctly. # ------------------- initialization ------------------ class NullDevice(): def write(self, s): pass import sys stdoutbak = sys.stdout sys.stdout = NullDevice() from pyDatalog import pyDatalog sys.stdout = stdoutbak # Table terms pyDatalog.create_terms("T,A,B") # immediate significant pyDatalog.create_terms("D") #carry bit pyDatalog.create_terms("C") #result pyDatalog.create_terms("R") # variable terms pyDatalog.create_terms("H,L,X,Z") # ------------------- data insert ------------------ # insert into T, 8-length for i in xrange(7,-1,-1): for j in xrange(i-1,-1,-1): + T (i,j) #print T(H,Y) # 1110100 + A(2); + A(4); + A(5); + A(6); # 10100101: + B(0); + B(2); + B(5); + B(7); # print A(X) # print B(X) # ------------------- data insert ------------------ # immediate significant relation D(H,L) <= T(H,L) & ~(T(H,Z) & T(Z,L)) #print D(H,L) # carry bit C(H) <= D(H,L) & A(L) & B(L) C(H) <= D(H,L) & C(L) & A(L) C(H) <= D(H,L) & C(L) & B(L) #print C(H) # final result R(X) <= A(X) & B(X) & C(X) R(X) <= A(X) & ~B(X) & ~C(X) R(X) <= B(X) & ~A(X) & ~C(X) R(X) <= C(X) & ~A(X) & ~B(X) print R(X)