Skip to content
An implementation of Prolog in TypeScript
TypeScript JavaScript
Branch: master
Clone or download
Latest commit bf004cc Jul 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin add cli Jul 12, 2019
src stop appending line breaks for each result Jul 14, 2019
test relocate files Jul 12, 2019
.gitignore initial commit Jul 12, 2019
README.md update README.md Jul 14, 2019
package-lock.json update package-lock.json Jul 13, 2019
package.json fix dependencies Jul 14, 2019
tsconfig.json initial commit Jul 12, 2019
tslint.json initial commit Jul 12, 2019

README.md

An implementation of Prolog in TypeScript.

Requirements

  • Node.js (>=10)

Usage

$ npm install ts-prolog -g
$ ts-prolog

Notes

  • Only a subset of the language features are implemented.
  • BFS is used for searching.

Example

rules.pl

mult(z, X, z).
add(z, Y, Y).
add(s(X), Y, s(Z)) :- add(X, Y, Z).
mult(s(X), Y, Z) :- mult(X, Y, P), add(P, Y, Z).
nat(z).
nat(s(X)) :- nat(X).
$ ts-prolog
> ['rules.pl'].
fact added: mult(z, X, z)
fact added: add(z, Y, Y)
rule added: add(s(X), Y, s(Z)) :- [add(X, Y, Z)]
rule added: mult(s(X), Y, Z) :- [mult(X, Y, P), add(P, Y, Z)]
fact added: nat(z)
rule added: nat(s(X)) :- [nat(X)]

> add(s(z), s(s(z)), X).
[X -> s(s(s(z)))]  (ENTER)
search finished

> add(s(z), X, s(s(s(z)))).
[X -> s(s(z))] (ENTER)
search finished

> mult(s(s(z)), s(s(s(z))), X).
[X -> s(s(s(s(s(s(z))))))] (ENTER)
search finished

> nat(X).
[X -> z] (ENTER)
[X -> s(z)] (ENTER)
[X -> s(s(z))] (ENTER)
[X -> s(s(s(z)))] (ENTER)
[X -> s(s(s(s(z))))] (Ctrl-C)

> add(X, Y, s(s(z))).
[X -> z, Y -> s(s(z))] (ENTER)
[X -> s(z), Y -> s(z)] (ENTER)
[X -> s(s(z)), Y -> z] (ENTER)
search finished

> add(X, Y, s(s(z))), mult(X, Y, s(z)).
[X -> s(z), Y -> s(z)] (ENTER)
search finished
You can’t perform that action at this time.