Package: pylog
Abstract (outline fromThe 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 ofThe 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: seeRelated 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.IntroductionProlog, 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 their5th generation project.Prolog went out of favor becauseit 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 areunifiedwith (rather than assigned to) variables.  Prolog is seductively elegant and powerful.We can look a few exampleshere.SWI-PrologSWI-Prologhas kept the Prolog flame burning and seems to have developed a successful community of Prolog users.From theSWI-Prolog websiteSWI-Prolog is a versatile implementation of theProlog 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.Prolog tutorialsBlackburn, Patrick, Johan Bos, and Kristina Striegnitz (2012)Learn Prolog Now!(This version of the book is embedded in SWI Prolog’sSWI SH(SWI-Prologfor 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 ofBratko, 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.Pylog: prolog in PythonThis repository is a Python implementation of many Prolog features. It is a fork of Piumarta’sunify.py.As an introductory example, consider the following (Python) code—adapted fromPiumarta's week 5 exercises. (You can run ithere.) (The type annotations are not required, but they are useful to understand what's going on.)fromtypingimportGeneratordefisEven(i:int)->Generator[None,None,None]:ifi%2==0:print(f'{i}-even',end=', ')yieldelse:print(f'{i}-odd',end=', ')evens=[iforiinrange(10)for_inisEven(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 doesfor_inisEven(i)do in the list comprehension?In Prolog, program components are understood as predicates. They maysucceedorfail. 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 thatyields a result (at the Python level) is said to succeed (at the Prolog level); one that does notyielda result, fails (at the Prolog level).In this case,isEven(i)succeeds/fails wheniis even/odd. (In either case it produces an output line.) Whenfor_inisEven(i)succeeds/fails for a giveni, the list comprehension completes/fails to complete the iteration for thatiand includes/does not includeiin the generated list.Implementing logic variables and unification in PythonTo be writtenImplementing Prolog backtracking in PythonTo be writtenFile organization (not recently updated)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 variablesFile dependencies/imports fromrelations (not recently updated)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_sequenceNaming conventionsFor 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.Previous workBerger, Shai (2004)PythologicBolz, Carl Friedrich (2007)A Prolog Interpreter in PythonDelord, Christophe (2009)PyLogFrederiksen, Bruce Frederiksen (2011)PykeKopec, David (2019)Constraint-Satisfaction Problems in PythonfromClassic Computer Science Problems in Python, Manning (GitHub code repository)Maxime, Istasse (2016)Prology: Logic programming for Python3Meyers, Chris (2015)Prolog in PythonNiemeyer, 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 Foundationre: Regular expression operationsRocklin, Matthew (2015)UnificationRocklin, Matthew (2019)kanren: Logic Programming in Python(The author oftoolz)Santini, Claudio (2018)Pampy: The Pattern Matching for Python you always dreamed ofStack Overflow questions.Implementing the Prolog Unification algorithm in Python? BacktrackingRelational/Logic Programming in Python?Thompson, Jeff (2017)Yield PrologTriska, MarkusCLP(FD): Constraint Logic Programming over Finite Domains(alsoCLP(FD): Constraint Logic Programming over Finite Domains(Repo ofexamples)