Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Getting started with SWI-Prolog

Getting started with SWI-Prolog

Today (May 2017) I decided to learn a bit about Prolog. Here is how to start using it...

Install it on Ubuntu:

sudo apt-get install swi-prolog

Run it:

swipl
# or:
prolog

Run Prolog and load rules and facts from a file:

prolog -s test
# or:
prolog -s test.pl

Interactively add rules or facts:

assert(male(dave)).

% or:
[user].
male(dave).
male(john).
<Ctrl-D>

("user" means add interactively instead of reading from a file.)

Load rules or facts from a file test.pl:

[test].
% or the full filename:
['path/to/test.pl'].

Open the source file containing a given object in your default editor (e.g. Vim):

edit(male).

The file is automatically reloaded afterwards. To reload files that have been edited independently:

make.

Run a query:

male(X).

Press ; or spacebar after each result to see the next result. Press enter to stop looking for results. Press Ctrl-C then a to abort.

That's all for now. To actually learn the Prolog language, I'm currently reading Learn Prolog Now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment