Kenichi Sasagawa
2 min readAug 2, 2019

Prolog in Elixir -Classical AI Programming language-

[Introduction]

In 1983, I first learned about Prolog.Prolog was adopted for ICOT in Japan.
At that time, it was difficult to obtain a Prolog processing system that runs on personal computer.More than 35 years have passed since then. I made a very small subset Prolog interpreter by Elixir.

[Example]
mix prolog
Prolog in Elixir
?- assert(append([], Xs, Xs)).
true
?- assert((append([X | Ls], Ys, [X | Zs]) :- append(Ls, Ys, Zs))).
true
?- append([1,2],[a,b],X).
X = [1,2,a,b]
true
?- append(X,Y,[1,2,3]).
X = []
Y = [1,2,3];
X = [1]
Y = [2,3];
X = [1,2]
Y = [3];
X = [1,2,3]
Y = [];
false

The calculation in Prolog is bi-directional. It is also possible to calculate an element for which the append result is [1, 2, 3]. It is a very strange programming language.

[Mechanism]
Prolog has no assignments. It uses unify instead. This unify will proceed with inference. Backtracking allows you to calculate all solutions.

All code is available on Github. https://github.com/sasagawa888/Prolog

It consists of 4 modules.
Prolog, Read, Prove, and Print.
The Prove module is the core.The prove function infers using unify.
Elixir is highly descriptive. Because of this the prove is simple and compact.

[Data structure]
Predicate [: pred, [name | argument]]
Clause [: clause, head, body]]
Head head predicate [: pred, [name | argument]]
Body part, [pred1, pred2, … predn]
Formula [: formula, entity]
List The list of Elixir is diverted
Variables Atom: X upper case, :_ x underbar
Alpha conversion replace in preparation for recursion. {: X 1} Keep in the argument as a natural number n.
(Note) The number of atoms in Elixir is limited to about 1 million, and atoms can not be recovered by GC. So I save atom generation by the above method.
Environment Associative List
Predicate definition Keyword list

[As you like]
This interpreter is small, about 1000 lines by Elixir. The core prove function is small and easily understandable. Please enjoy improvement and remodeling.