Skip to content

a-n-d-a-i/prolog.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Editor's Note

This is a JS port (by Claude) of paip-python:

https://github.com/dhconnelly/paip-python

The tests are useless and are included for comedic effect. They were added entirely of Claude's free will.

Everything except this first paragraph is Claude's work, untouched by me.

JavaScript Prolog Interpreter

A pure Prolog interpreter implementation in JavaScript (Node.js), ported from Python. This interpreter provides basic logic programming functionality - the subset of Prolog known as "Pure Prolog".

Features

  • Facts and Rules: Define relationships and logical rules
  • Queries: Prove goals and find variable bindings
  • Unification: Automatic pattern matching and variable binding
  • Backtracking: Automatic search for multiple solutions
  • REPL: Interactive interpreter with command-line interface

Installation

No external dependencies required! Just Node.js.

# Make the script executable (optional)
chmod +x prolog.js

Usage

Starting the REPL

# Basic usage
node prolog.js

# Load a database file
node prolog.js --db family.prolog

# Show help
node prolog.js --help

Syntax

Defining Facts

Facts are unconditionally true relations:

<- parent(tom, bob)
<- female(liz)
<- likes(john, pizza)

Defining Rules

Rules specify when a relation is true based on other relations:

<- grandparent(?x, ?z) :- parent(?x, ?y), parent(?y, ?z)
<- sibling(?x, ?y) :- parent(?z, ?x), parent(?z, ?y)

Variables in rules are prefixed with ?.

Querying (Proving Goals)

Ask questions using queries:

?- parent(tom, bob)          # Is tom the parent of bob?
?- grandparent(?x, jim)      # Who is the grandparent of jim?
?- parent(?x, ?y)            # Find all parent relationships

Example Session

>> <- parent(tom, bob)
>> <- parent(bob, ann)
>> <- grandparent(?x, ?z) :- parent(?x, ?y), parent(?y, ?z)
>> ?- grandparent(?x, ann)
?x : tom
Continue? n

File Structure

  • logic.js: Core logic programming engine

    • Unification algorithm
    • Goal proving with backtracking
    • Data structures (Atom, Var, Relation, Clause)
  • prolog.js: Interpreter frontend

    • Lexer and Parser
    • REPL interface
    • Database loading

Example Database

The included family.prolog demonstrates a simple family tree:

node prolog.js --db family.prolog

Try these queries:

?- parent(?x, bob)           # Who is bob's parent?
?- grandparent(tom, ?x)      # Who are tom's grandchildren?
?- father(?x, ?y)            # Find all father relationships

Supported Features

✅ Facts and rules
✅ Logic variables and unification
✅ Backtracking for multiple solutions
✅ Nested relations
✅ Numbers and atoms
✅ Comments (lines starting with #)

Not Supported (Pure Prolog Limitations)

❌ Lists
❌ Arithmetic evaluation
❌ Built-in predicates (like is, <, >)
❌ User-defined operators
❌ Cut operator (!)
❌ Assert/retract (dynamic predicates)

Implementation Notes

This is a faithful port of a Python implementation inspired by Chapter 11 of "Paradigms of Artificial Intelligence Programming" by Peter Norvig. The implementation demonstrates the core concepts of logic programming:

  1. Uniform Database: Facts and rules stored uniformly as clauses
  2. Unification: Pattern matching with variable binding
  3. Backtracking: Automatic search through solution space

REPL Commands

  • help - Show help message
  • quit - Exit the interpreter
  • <- ... - Add a fact or rule to the database
  • ?- ... - Query the database

License

This is an educational implementation. Original Python version by Daniel Connelly.

Differences from Python Version

  • Async/await used for user input in REPL
  • JavaScript module system (CommonJS) instead of Python imports
  • Object-based approach for data structures
  • Immutable operations preferred (spread operator for bindings)

About

Claude's JS port of https://github.com/dhconnelly/paip-python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published