Wednesday, February 5, 2014

Prolog-like logic programming in Python - pyDatalog

By Vasudev Ram



pyDatalog is a tool for "logic programming in Python" (*).

I installed and tried it out with this small program, a modified version of the one on the pyDatalog home page. That program calculates factorials; I added calculation of squares.

Here is the program, test_pydatalog.py:
from pyDatalog import pyDatalog
pyDatalog.create_terms('factorial, N')
 
factorial[N] = N*factorial[N-1] 
factorial[1] = 1
 
print(factorial[2]==N) 
print(factorial[3]==N) 
print(factorial[4]==N) 

pyDatalog.create_terms('square, N')
square[N] = N*N

print(square[3]==N)
print(square[4]==N)
print(square[5]==N)

Run the program with the command:
python test_pydatalog.py

That gives the following output:

pyDatalog version 0.14.0
N
-
2
N
-
6
N
--
24
N
-
9
N
--
16
N
--
25

I've only tried out pyDatalog out a little so far. Will try it out more later and may post again about it.

Interesting concept. pyDatalog seems to be inspired by either Datalog or LogicBlox.
Datalog in turn is a subset of Prolog, one of oldest logic programming languages.


- Vasudev Ram - Dancing Bison Enterprises



Contact Page

No comments: