Logic programming in Python


 
from pyDatalog import pyDatalog
pyDatalog.create_terms('factorial, N')
 
factorial[N] = N*factorial[N-1] 
factorial[1] = 1 print(factorial[3]==N) # prints N=6
  No if's, no loops, just the plain mathematical definition !
 Easy to learn
Based on programmer-friendly Python 

 Be productive
Write short, declarative programs
Batteries included
With full access to the extensive Python library 

Be readable
Order your statements to be literate 
Query 11 SQL databases 
and noSQL, and Python classes 

Use AI
to implement expert systems and complex algorithms


pyDatalog adds the logic programming paradigm to Python's extensive toolbox, in a pythonic way.  

Logic programmers can now use the extensive standard library of Python, and Python programmers can now express complex algorithms quickly.

Datalog is a truly declarative language derived from Prolog, with strong academic foundations.  Datalog excels at managing complexity.  Datalog programs are shorter than their Python equivalent, and Datalog statements can be specified in any order, as simply as formula in a spreadsheet. 

In particular, Datalog can be used for:
  • simulating intelligent behavior (for games or expert systems), 
  • performing recursive algorithms (e.g. in network protocol, code and graph analysis),
  • querying complex sets of related information (e.g. in data integration or Natural Language Processing),
  • solving discrete constraint problems
pyDatalog is derived from previous work by John D. Ramsdell.  It is an open-source project (LGPL) lead by Pierre Carbonnelle (in Belgium).  It is inspired by LogicBlox. 

Core technology

 Core technology in pyDatalog Benefits Sample applications
The resolution engine determines the best sequence of clauses to use to reach a goalSpreadsheet-style programming : faster development; fewer bugs, easier to read afterwardsRule-based models with many input and output, e.g. expert system for price calculation or tax planningaccess right managementrobot controlintelligent agent in games or automated assistants
The same clause can solve various mixes of known and unknown parametersMaximize code reuse : shorter program, simpler query language than SQLCross-database queries, data integration

Intermediate results are memoized.Improved speed by avoiding duplicate work.
Avoid infinite loops in recursive algorithms 
Business logic in 3-tier architecture.
Recursive algorithm on tree or graph data structures, e.g. natural language processingnetwork and code analysis

Features

pyDatalog embeds logic programming in Python :
  • you can assert facts in a datalog knowledge base, and query it.
  • you can use logic clauses to query any relational database via SQLAlchemy, the data access layer for Python which supports 11 dialects of SQL.
  • you can define attributes of Python classes through logic clauses, and use logic queries on Python objects.
More specifically:
  • you can use aggregate functions : len_, sum_, concat_, min_, max_, tuple_, rank_, running_sum_
  • you can define logic functions (e.g. p[X]==Y) , i.e. logic predicate with unicity
  • variables can represent nested lists that you can index and slice
  • you can prefix literals and functions with a Python class name (e.g. Employee.name[X]==Y): their first argument refers to instances of the class.  
  • prefixed literals and functions can be inherited from parent classes.
  • you can use arithmetic literals (X == Y+1).  An expression can contain Python functions and lambda expressions
  • you can negate a literal in the body: ~p(X)
pyDatalog is a fast and lightweight datalog interpreter written in Python:
  • it can solve the 8-queen problem in 0.18 seconds on a PC
  • it is thread safe
  • it uses SLDNF resolution with tabling
  • it uses indexes for the database of datalog facts
  • it can run on pypy (Python with Just-In-Time compiler)
  • it has hooks to let you rewrite performance-critical clauses in pure Python
  • it has less than 2 K lines of executable code.
The depth of recursion is not limited by the stack size.

pyDatalog has been tested with python 2.7, 3.3, pypy 1.9 - 2.0, and SQLAlchemy 0.7 - 0.9, on Windows. (over 400 tests)