Skip to main content

Python implementation of Prolog features.

Project description

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.

Introduction

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

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.

Prolog tutorials

Pylog: prolog 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.

Implementing logic variables and unification in Python

To be written

Implementing Prolog backtracking in Python

To be written

File 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 variables

File dependencies/imports from relations (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_sequence

Naming conventions

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.

Previous work

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylog-1.1.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

pylog-1.1-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file pylog-1.1.tar.gz.

File metadata

  • Download URL: pylog-1.1.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for pylog-1.1.tar.gz
Algorithm Hash digest
SHA256 a053aa2e8d885691dd3585a3e3192b24a03af6298f557ba4907213f27a1ef415
MD5 4c8b350e4317a7be4a29bb7241346980
BLAKE2b-256 fa1a67f294296c23d87e50e4699cbd157bb44214c6ec29804dc848d8509800ac

See more details on using hashes here.

File details

Details for the file pylog-1.1-py3-none-any.whl.

File metadata

  • Download URL: pylog-1.1-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for pylog-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e04ebdad682d48746f2c20a25896b8919feb6481c5507a1c0afb346565b5d7b3
MD5 f8c0bbfde308a84a6594b1b523eea05b
BLAKE2b-256 143ee20b3e8fb78eb7f1629efb4fc77290df02566e683bf41a7abe2e8dc5405c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page