- Install
pytest
:pip install pytest
/conda install pytest
- Install
pylog
package:pip install -e pylog/.
- Run examples' tests:
pytest examples/
Abstract (outline from The Programming Journal)
- Context: What is the broad context of the work? What is the importance of the general research area?
- Pylog inhabits three programming contexts.
a) Pylog explores the integration of two distinct programming language paradigms: (i) the modern general purpose programming paradigm, which often includes features of procedural programming, object-oriented programming, functional programming, and meta-programming, here represented by Python, and (ii) logic programming, whose primary features are logic variables (and unification) and built-in depth-first search, here represented by Prolog. These logic programming feature are generally missing from modern general purpose languages. Pylog illustrates how these two features can be implemented in and integated into Python.
b) Pylog demonstrates the breadth and broad applicability of Python. Although Python is now the most widely used programming language for teaching introductory programming, it has also become very widely used for sophisticated programming tasks. One of the reasons for its popularity is the range of capabilities it offers—most of which are not used in elementary programming classes or for the sort of scripting applications with which Python is often associated. Pylog makes effective use of many of those capabilities.
c) Pylog examplifies programming at its best. Pylog is first-of-all a programming exercise: How can the primary features of logic programming be integrated with Python? Secondly, Pylog uses features of Python in ways that aere both intended and innovative. The overall result is software worth studying. From the perspective of The Programming Journal, it would fit into its Art-of-Programming category.
- Inquiry: What problem or question does the paper address? How has this problem or question been addressed by others (if at all)?
- The primary issue addressed in the paper—and in Pylog itself—is how logic variables and backtracking can be integrated cleanly into a Python framework. A fair amount of work has been done in this area: see Related Work. Although well done, most of it has been incomplete in one way or another. Pylog is the first complete system (as far as we know) to achieve the goal of integration.
- Approach: What was done that unveiled new knowledge?
- Pylog, exhibits the integration mentined above. The paper discusses—and Pylog demonstrates—how logic variables and backtracking can be interwoven with standard Python data structures and control structures.
- Knowledge: What new facts were uncovered? If the research was not results oriented, what new capabilities are enabled by the work?
- Pylog is available as a logic programming library for use in Python software.
- Grounding: What argument, feasibility proof, artifacts, or results and evaluation support this work?
- Pylog demonstrates by its existence and functionality that the goal of integrating logic variables and backtracking with Python can be achieved.
- Importance: Why does this work matter?
- Python is known to be compatible with functional programming. This work shows that it is also (and simultaneously) compatible with logic programming. This work also demonstrates the power and elegance of well-designed software.
Prolog, a programming language derived from logic, was developed in the 1970s. It became very popular during the 1980s as an AI language, especially in Japan as part of their 5th generation project.
Prolog went out of favor because it was difficult to trace the execution of Prolog programs—which made debugging very challenging.
Structurally, Prolog is both one of the simplest of all programming languages—you can learn it very quickly—and one of the most interesting. It is quite different from virtually all other programming languages. In Prolog you make assertions of facts and then pose questions about those facts. Answers are unified with (rather than assigned to) variables. Prolog is seductively elegant and powerful.
We can look a few examples here.
SWI-Prolog has kept the Prolog flame burning and seems to have developed a successful community of Prolog users.
- From the SWI-Prolog website
- SWI-Prolog is a versatile implementation of the Prolog language. Although SWI-Prolog gained its popularity primarily in education, its development is mostly driven by the needs for application development. This is facilitated by a rich interface to other IT components by supporting many document types and (network) protocols as well as a comprehensive low-level interface to C that is the basis for high-level interfaces to C++, Java (bundled), C#, Python, etc (externally available). Data type extensions such as dicts and strings as well as full support for Unicode and unbounded integers simplify smooth exchange of data with other components.
SWI-Prolog aims at scalability. Its robust support for multi-threading exploits multi-core hardware efficiently and simplifies embedding in concurrent applications. Its Just In Time Indexing (JITI) provides transparent and efficient support for predicates with millions of clauses.
-
Blackburn, Patrick, Johan Bos, and Kristina Striegnitz (2012) Learn Prolog Now! (This version of the book is embedded in SWI Prolog’s SWI SH (SWI-Prolog for SHaring), an online Prolog interpreter similar to Jupyter.)
-
Wilson, Bill, "Introduction to Prolog Programming," for the course COMP9414/9814 "Artificial Intelligence." Consists mostly of extracts from the first five chapters of
- Bratko, I. (2011) Programming in Prolog for Artificial Intelligence, 4th Edition, Addison-Wesley.
-
Piumarta, Ian (2017) "Programming-language-paradigms" (a course). “Logic programming and Prolog” is explored in week 5 (slides-5, exercises-5), week 6 (slides-6, exercises-6), and week 7 (slides-7, exercises-7). Week 7 is a Prolog tutorial. You should be able to understand it without first reading weeks 5 and 6. But those weeks are important. They show how to implement many Prolog features in Python.
This repository is a Python implementation of many Prolog features. It is a fork of Piumarta’s unify.py
.
As an introductory example, consider the following (Python) code—adapted from Piumarta's week 5 exercises. (You can run it here.) (The type annotations are not required, but they are useful to understand what's going on.)
from typing import Generator
def isEven(i: int) -> Generator[None, None, None]:
if i % 2 == 0:
print(f'{i}-even', end = ', ')
yield
else:
print(f'{i}-odd', end = ', ')
evens = [i for i in range(10) for _ in isEven(i)]
print(f'\n{evens}')
Can you figure out how the preceding produces these results?
0-even, 1-odd, 2-even, 3-odd, 4-even, 5-odd, 6-even, 7-odd, 8-even, 9-odd,
[0, 2, 4, 6, 8]
In particular, what does
for _ in isEven(i)
do in the list comprehension?
In Prolog, program components are understood as predicates. They may succeed or fail. To succeed/fail means that the system was/was not able to establish that the predicate holds given the information available.
Success or failure is implemented in Python through generators. A generator that yields a result (at the Python level) is said to succeed (at the Prolog level); one that does not yield a result, fails (at the Prolog level).
In this case, isEven(i)
succeeds/fails when i
is even/odd. (In either case it produces an output line.) When
for _ in isEven(i)
succeeds/fails for a given i
, the list comprehension completes/fails to complete the iteration for that i
and includes/does not include i
in the generated list.
To be written
To be written
pylog -- Root directory
examples -- A directory of example pylog programs
n_queens.py -- The traditional n-queens problem. Uses minimal pylog features but illustrates the pylog style.
puzzles.py -- A file containing information common to the scholarship_problem and the zebra_problem
scholarship_problem.py -- A traditional Prolog logic puzzle—less complex than the zebra problem
trains.py -- A revised version of the train example from Piumarta
zebra_problem.py -- The well-known logic puzzle often solved with Prolog
sequence_options -- A directory of options for lists and sequences
linked_list.py -- A traditional head/tail list structure. Allows a variable tail, which Python does not
sequences.py -- Implementations of Python lists and tuples
super_sequence.py -- A class that serves as a superclass for all the sequences
control_structures.py -- Implementation of the Prolog control structures
logic_variables.py -- Implementation of Prolog's logic variables
logic_variables: none
control_structures: logic_variables
super_sequence: control_structures, logic_variables
linked_list and sequences: logic_variables, super_sequence
n_queens: logic_variables, sequences
trains: control_structures, logic_variables, sequences
puzzle: super_sequence
scholarship_problem and zebra_problem: control_structures, logic_variables, puzzles, super_sequence
For the most part, Python identifier names follow PEP 8 conventions: all lower case, with underscores between words; no camel case except for class names.
However, since Prolog uses identifiers that begin with an upper case letter for Prolog variables, Python identifiers used as Prolog variables begin with upper case letters.
-
Berger, Shai (2004) Pythologic
-
Bolz, Carl Friedrich (2007) A Prolog Interpreter in Python
-
Delord, Christophe (2009) PyLog
-
Frederiksen, Bruce Frederiksen (2011) Pyke
-
Kopec, David (2019) Constraint-Satisfaction Problems in Python from Classic Computer Science Problems in Python, Manning (GitHub code repository)
-
Maxime, Istasse (2016) Prology: Logic programming for Python3
-
Meyers, Chris (2015) Prolog in Python
-
Niemeyer, Gustavo and Sébastien Celles (2019). python-constraint. Pypi page: python-constraint (Also discussed in Popović, Olivera (2019) Constraint Programming with python-constraint)
-
Orsini, Francesc, Paolo Frasconi, Luc De Raedt (2017) kProbLog: an algebraic Prolog for machine learning (This seems more like a theoretical discussion of kProbLog, which was implementated in Python, rather than an integraton of Python and Prolog. It's a worthwhile distinction to make. One can implement Prolog in any general purpose language. Pylog is notable because it integrates Python and Prolog.)
-
Python Foundation re: Regular expression operations
-
Rocklin, Matthew (2015) Unification
-
Rocklin, Matthew (2019) kanren: Logic Programming in Python (The author of toolz)
-
Santini, Claudio (2018) Pampy: The Pattern Matching for Python you always dreamed of
-
Stack Overflow questions.
-
Thompson, Jeff (2017) Yield Prolog
-
Triska, Markus CLP(FD): Constraint Logic Programming over Finite Domains (also CLP(FD): Constraint Logic Programming over Finite Domains (Repo of examples)