Logic Programming in Python
Pierre Carbonnelle
FOSDEM 2014
No if’s, no loops, just the plain mathematical definition !
Logic programming
(Prolog, SQL)
Functional
(Lisp, ..)
Imperative / OO
(C, Smalltalk)
Logic programming
(Prolog, SQL)
Functional
(Lisp, ..)
Imperative / OO
(C, Smalltalk)
Python
Python + pyDatalog
Why Logic Programming in Python ?
Be productive
Be Readable
Use AI
Easy to learn
Batteries included
Query 11 SQL databases
The anatomy of a prolog program
Fact
Clause
Query
The anatomy of a pyDatalog program
The anatomy of a pyDatalog program
Sample 1
Could be
written in SQL
too !
Sample 1
 Loop
 Calculation
 Test
 Input/output polymorphism !
Shorter
programs !
Sample 1
Batteries
included
 Would need an equation solver
Sample 2
• f[X]=2*X is equivalent to (f[X]==Y) <= (Y==2*x)
• Functions return only 1 value per key (aka dict())
• Functions can appear in expressions
• Resolution starts with the latest clause, and stops on first answer
• Support for aggregate functions : sum, min, max, …
Sample 3 Familiar tuple
notation
Memoized
 Faster
 Avoid infinite loop
Sample 4
Prefixed function
to query
Python classes
or SQL databases
Other pyDatalog features
• Runs on PyPy, Python 2, Python 3
• Accepts predicate definition written in Python
• For I/O, performance, …
• Thread-safe
• For use in multi-threaded web framework
• Multi-model : Switch from one Logic model (= set of clauses)
to another
Applications
Core technology Benefit Applications
The SLG resolution algorithm
determines the sequence of steps
to resolve a query.
Spreadsheet-programming : faster
iterations, fewer bugs, more
readable.
Rule-based expert systems for
pricing, tax calculation, access
right, robot control, intelligent
agent.
Input/output polymorphism. Maximise code reuse : shorter
program, simpler database query
language.
Cross-database queries, data
integration.
Intermediate results are memoized. Avoid re-calculation. Avoid infinite
loop.
Recursive algorithm on tree or
graph structures, natural language
processing, code analysis
Roadmap : Equation solver
Roadmap : Transactions
• Assert facts within transactions, with possible roll-back
• Use logic clauses to validate data operations
• Use logic clauses to define triggers (condition => action)
• Memoize results across queries within the same transaction
• Translate Datalog queries into SQL for server-side processing
Feedback, Help welcome !!
Roadmap : Performance
• Rewrite critical sections in C
• Exploit multi-threading in resolution engine
Feedback, Help welcome !!
Why Logic Programming in Python ?
Easy to learn
Batteries included
Query 11 SQL databases
Be productive
Be Readable
Use AI

Logic programming in python

  • 1.
    Logic Programming inPython Pierre Carbonnelle FOSDEM 2014
  • 2.
    No if’s, noloops, just the plain mathematical definition !
  • 3.
    Logic programming (Prolog, SQL) Functional (Lisp,..) Imperative / OO (C, Smalltalk)
  • 4.
    Logic programming (Prolog, SQL) Functional (Lisp,..) Imperative / OO (C, Smalltalk) Python Python + pyDatalog
  • 5.
    Why Logic Programmingin Python ? Be productive Be Readable Use AI Easy to learn Batteries included Query 11 SQL databases
  • 6.
    The anatomy ofa prolog program Fact Clause Query
  • 7.
    The anatomy ofa pyDatalog program
  • 8.
    The anatomy ofa pyDatalog program
  • 9.
  • 10.
    Sample 1  Loop Calculation  Test  Input/output polymorphism ! Shorter programs !
  • 11.
  • 12.
    Sample 2 • f[X]=2*Xis equivalent to (f[X]==Y) <= (Y==2*x) • Functions return only 1 value per key (aka dict()) • Functions can appear in expressions • Resolution starts with the latest clause, and stops on first answer • Support for aggregate functions : sum, min, max, …
  • 13.
    Sample 3 Familiartuple notation Memoized  Faster  Avoid infinite loop
  • 14.
    Sample 4 Prefixed function toquery Python classes or SQL databases
  • 15.
    Other pyDatalog features •Runs on PyPy, Python 2, Python 3 • Accepts predicate definition written in Python • For I/O, performance, … • Thread-safe • For use in multi-threaded web framework • Multi-model : Switch from one Logic model (= set of clauses) to another
  • 16.
    Applications Core technology BenefitApplications The SLG resolution algorithm determines the sequence of steps to resolve a query. Spreadsheet-programming : faster iterations, fewer bugs, more readable. Rule-based expert systems for pricing, tax calculation, access right, robot control, intelligent agent. Input/output polymorphism. Maximise code reuse : shorter program, simpler database query language. Cross-database queries, data integration. Intermediate results are memoized. Avoid re-calculation. Avoid infinite loop. Recursive algorithm on tree or graph structures, natural language processing, code analysis
  • 17.
  • 18.
    Roadmap : Transactions •Assert facts within transactions, with possible roll-back • Use logic clauses to validate data operations • Use logic clauses to define triggers (condition => action) • Memoize results across queries within the same transaction • Translate Datalog queries into SQL for server-side processing Feedback, Help welcome !!
  • 19.
    Roadmap : Performance •Rewrite critical sections in C • Exploit multi-threading in resolution engine Feedback, Help welcome !!
  • 20.
    Why Logic Programmingin Python ? Easy to learn Batteries included Query 11 SQL databases Be productive Be Readable Use AI