Skip to main content

Prolog-like interpreter and tuple store

Project description

A lightweight Prolog-like interpreter with a natural-language style syntax

We closely follow Einstein's "Everything should be made as simple as possible, but no simpler."

At this point, we rely on Python's natural error checking, without doing much to warn about syntactic or semantic errors. This can be added, but this is meant as an executable specification of an otherwise simple and natural logic language that we hereby name Natlog.

Natlog : a succinct overview

  • Terms are represented as nested tuples.

  • A parser and scanner for a simplified Prolog term syntax is used to turn terms into nested Python tuples.

Surface syntax of facts, as read from strings, is just whitespace separated words (with tuples parenthesized) and sentences ended with . or ?. Like in Prolog, variables are capitalized, unless quoted. Example programs are in folder natprogs, for instance tc.nat:

cat is feline.
tiger is feline.
mouse is rodent.
feline is mammal.
rodent is mammal.
snake is reptile.
mammal is animal.
reptile is animal.

tc A Rel B : A Rel B.
tc A Rel C : A Rel B, tc B Rel C.

To query it, try:

>>> n=natlog(file_name="natprogs/tc.nat")
>>> n.query("tc Who is animal ?")

It will return the transitive closure of the is relation.

GOAL PARSED: (('tc', 0, 'is', 'animal'),)
ANSWER: ('tc', 'cat', 'is', 'animal')
ANSWER: ('tc', 'tiger', 'is', 'animal')
ANSWER: ('tc', 'mouse', 'is', 'animal')
ANSWER: ('tc', 'feline', 'is', 'animal')
ANSWER: ('tc', 'rodent', 'is', 'animal')
ANSWER: ('tc', 'snake', 'is', 'animal')
ANSWER: ('tc', 'mammal', 'is', 'animal')
ANSWER: ('tc', 'reptile', 'is', 'animal')

List processing is also supported as in:

app () Ys Ys. 
app (X Xs) Ys (X Zs) : app Xs Ys Zs.

The interpreter supports a yield mechanism, similar to Python's own. Something like ^ my_answer X resulting in my_answer X to be yield as an answer.

The interpreter has also been extended to handle simple function and generator calls to Python using the same prefix operator syntax:

  • `f A B .. Z R, resulting in Python function f(A,B,C) being called and R unified with its result
  • ``f A B .. Z R, resulting in Python generator f(A,B,C) being called and R unified with its multiple yields, one a time
  • ~R A B .. Z for unifying ~ R A B .. Z with matching facts in the term store
  • f A B .. Z, resulting in f(A,B,C,..,Z) being called with no result returned

A nested tuple store for unification-based tuple mining

An indexer in combination with the unification algorithm is used to retrieve ground terms matching terms containing logic variables.

Indexing is on all constants occurring in ground facts placed in a database.

As facts are ground, unification has occurs check and trailing turned off when searching for a match.

To try it out, do:

python3 -i db.py

>>> dbtest()

It gives, after digesting a text and then querying it:

   John has (a car).
   Mary has (a bike).
   Mary is (a student).
   John is (a pilot).

('John', 'has', ('a', 'car'))
('Mary', 'has', ('a', 'bike'))
('Mary', 'is', ('a', 'student'))
('John', 'is', ('a', 'pilot'))


Who has (a What)?
--> ('John', 'has', ('a', 'car'))
--> ('Mary', 'has', ('a', 'bike'))

Who is (a pilot)?
--> ('John', 'is', ('a', 'pilot'))

'Mary' is What?
--> ('Mary', 'is', ('a', 'student'))

'John' is (a What)?
--> ('John', 'is', ('a', 'pilot'))

Who is What?
--> ('Mary', 'is', ('a', 'student'))
--> ('John', 'is', ('a', 'pilot'))

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

natlog-0.1.9.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

natlog-0.1.9-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file natlog-0.1.9.tar.gz.

File metadata

  • Download URL: natlog-0.1.9.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/42.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.4

File hashes

Hashes for natlog-0.1.9.tar.gz
Algorithm Hash digest
SHA256 00b4b5fda4ba822ce0e3a82416504a645be97f0c69dda2f26a3f93cff49bdda6
MD5 a8f7397b6422c0217b5f9f6542248d89
BLAKE2b-256 bcdf2768a6c1fd4a024b45f784158642be4a0881468f5cf8399200995f3cc010

See more details on using hashes here.

File details

Details for the file natlog-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: natlog-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/42.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.4

File hashes

Hashes for natlog-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 9b779a53603ccf00fd5ee05e5c4aa5c09bd245b51d65cb1ed73489fbb62c3792
MD5 44874c5ad85f88e558a819f5e42687fa
BLAKE2b-256 3387dccf5eb9c5b189ea7122913fd7812a776461794f0b0e4a068bef5726ce79

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