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.
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".
- 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
No external dependencies required! Just Node.js.
# Make the script executable (optional)
chmod +x prolog.js# Basic usage
node prolog.js
# Load a database file
node prolog.js --db family.prolog
# Show help
node prolog.js --helpFacts are unconditionally true relations:
<- parent(tom, bob)
<- female(liz)
<- likes(john, pizza)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 ?.
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>> <- parent(tom, bob)
>> <- parent(bob, ann)
>> <- grandparent(?x, ?z) :- parent(?x, ?y), parent(?y, ?z)
>> ?- grandparent(?x, ann)
?x : tom
Continue? n
-
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
The included family.prolog demonstrates a simple family tree:
node prolog.js --db family.prologTry 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✅ Facts and rules
✅ Logic variables and unification
✅ Backtracking for multiple solutions
✅ Nested relations
✅ Numbers and atoms
✅ Comments (lines starting with #)
❌ Lists
❌ Arithmetic evaluation
❌ Built-in predicates (like is, <, >)
❌ User-defined operators
❌ Cut operator (!)
❌ Assert/retract (dynamic predicates)
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:
- Uniform Database: Facts and rules stored uniformly as clauses
- Unification: Pattern matching with variable binding
- Backtracking: Automatic search through solution space
help- Show help messagequit- Exit the interpreter<- ...- Add a fact or rule to the database?- ...- Query the database
This is an educational implementation. Original Python version by Daniel Connelly.
- 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)