
🔍
prolog.o
Prolog interpreter implemented in O language. Supports logic programming with facts, rules, queries, and unification.
Usage
Load Prolog module:
o)load[getenv[`OHOME];"prolog"];
Switch to Prolog REPL mode:
o)olog[];
?) // Prolog prompt
Load Prolog facts from file:
?)file["path/to/facts.pl"]
Main elements
| Name | Description / comments |
|---|---|
| be[<fact or rule>] | Add fact or rule to knowledge base |
| search[<term>] | Query the knowledge base |
| olog[] | Switch to Prolog REPL mode |
| orpl[] | Switch back to O REPL mode |
| file[<path>] | Load Prolog facts/rules from file |
| term[<x>] | Create term structure from expression |
| rule[<x>] | Create rule structure |
| goal[<r>;<p>;<e>] | Create goal for search |
Prolog syntax
Facts:
?)parent(tom, bob).
?)parent(tom, liz).
?)parent(bob, ann).
?)parent(bob, pat).
?)parent(pat, jim).
Rules:
?)grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
?)ancestor(X, Z) :- parent(X, Z).
?)ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).
Queries:
?)parent(tom, bob)?
Yes
?)parent(tom, X)?
X=bob
X=liz
?)grandparent(tom, Who)?
Who=ann
Who=pat
?)ancestor(tom, jim)?
Yes
Examples
Family relationships:
o)load[getenv[`OHOME];"prolog"];
o)olog[];
?)
?)// Define facts
?)parent(tom, bob).
?)parent(bob, ann).
?)
?)// Define rule
?)grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
?)
?)// Query
?)grandparent(tom, Who)?
Who=ann
Yes
?)
?)// Switch back to O mode
?)\\
o)
Logic puzzles:
?)// Who likes what?
?)likes(mary, food).
?)likes(mary, wine).
?)likes(john, wine).
?)likes(john, mary).
?)
?)// Query: What does Mary like?
?)likes(mary, X)?
X=food
X=wine
?)
?)// Query: Who likes wine?
?)likes(Who, wine)?
Who=mary
Who=john
List operations:
?)// List membership
?)member(X, [X]).
?)member(X, [H, T]) :- member(X, T).
?)
?)// Query
?)member(2, [1, 2, 3])?
Yes
Global state
| Variable | Description |
|---|---|
| FACTS | Knowledge base (list of facts and rules) |
| QUEUE | Search queue for goal processing |
| TRACE | Enable/disable trace mode (boolean) |
Debugging commands
| Command | Description |
|---|---|
| `trace(1)` | Enable trace mode |
| `trace(0)` | Disable trace mode |
| `dump()` | Print all facts and rules |
| `\` | Exit Prolog mode (return to O REPL) |
Auxiliary elements
| Name | Description |
|---|---|
| env[] | Create empty environment |
| push[<x>] | Push goal onto search queue |
| pop[] | Pop goal from search queue |
| isVar[<x>] | Check if term is a variable (starts with uppercase) |
| isConst[<x>] | Check if term is a constant |
| unify[<src>;<srcEnv>;<dst>] | Unify two terms |
| unifyTerm[<term>;<env>] | Resolve term in environment |
| fmt[<x>] | Format term/rule for display |
| prin[<x>] | Print formatted term |
| dump[<x>] | Print environment bindings |
PEG parser elements
| Name | Description |
|---|---|
| ospce | Parse whitespace |
| oiden | Parse identifier |
| oargs | Parse argument list |
| ofact | Parse fact |
| orule | Parse rule (fact :- goals) |
| oterm | Parse term (fact or rule) |
| oquit | Parse quit command (\) |
| otrce | Parse trace command |
| odump | Parse dump command |
| oqury | Parse query (ends with ?) |
| oexpr | Parse any Prolog expression |
| oxprs | Parse multiple expressions |
How it works
The Prolog interpreter uses:
- Unification: Pattern matching algorithm that binds variables to values
- Search: Depth-first search through the knowledge base
- Backtracking: Automatic exploration of alternative solutions
- Environment: Maps variables to their bindings during search
Example session
o)load[getenv[`OHOME];"prolog"];
o)olog[];
?)
?)// Family tree
?)parent(alice, bob).
?)parent(alice, charlie).
?)parent(bob, david).
?)
?)child(X, Y) :- parent(Y, X).
?)sibling(X, Y) :- parent(Z, X), parent(Z, Y).
?)
?)// Queries
?)parent(alice, Who)?
Who=bob
Who=charlie
?)
?)child(david, Who)?
Who=bob
?)
?)sibling(bob, Who)?
Who=bob
Who=charlie
?)
?)dump()
parent(alice, bob)
parent(alice, charlie)
parent(bob, david)
child(X, Y) :- parent(Y, X)
sibling(X, Y) :- parent(Z, X), parent(Z, Y)
?)
?)\\
o)
Variables in Prolog must start with uppercase letters (X, Y, Who, etc.). Constants and predicates start with lowercase letters.