Setting up your Prolog environment

Table of Contents

toggle-theme.png

There are a lot of different environments that you can use for writing Prolog, most students for this course I think use the SWIPL Prolog online IDE, SWISH. SWISH is excellent as far as online IDEs go and I wouldn’t discourage anyone from using it. It’s simple and powerful.

Setting up a local environment

If you’d like to install a local environment, then you’ll first need to install a Prolog compiler. I would recommend installing SWIPL. It’s a free and open source Prolog compiler that’s available on Windows, MacOS and Linux.

Once you’ve installed your Prolog compiler, you should be able to use most development environments such as visual studio code, GNU Emacs, Sublime Text, etc. for writing Prolog. SWIPL also comes with a version of GNU Emacs built-in that’s modified for use with Prolog you can use.

Using the built-in editor

To start using the built-in editor, you first need to fire up your Prolog REPL by typing swipl into your terminal. This will load you into an interactive prompt where you’ll be able to enter all of your Prolog queries. To exit swipl, you use the un-guessable halt. command. There’s no exit or quit here like in most languages, no, we use halt in Prolog land. Note that you need to end the line with . because that’s like our semi-colon in other languages. All expressions will need to end with a period. To invoke our editor, you can type emacs. into your REPL and it will launch the editor to a new empty file. You can write your Prolog code in that empty file, then when you’re done, hit File -> Save as to save your Prolog code to a new file. Then you can exit Emacs by doing File -> Quit. Note that when using the buttons from the menu bar, they have the associated shortcuts for doing those actions. Quitting Emacs for example is Ctrl-x Ctrl-c (or C-x C-c as Emacs will show you). So you can use those to learn the shortcuts - sadly Emacs doesn’t use standard ones like Ctrl-s or Ctrl-c / Ctrl-v.

Once you’ve exited Emacs and are back in your REPL, you can load the file that you just created by doing [filename]. without including the file extension. Give your Prolog files the .pl or .prolog extension and this should work fine right away.

Once you’ve got this going, you can get started writing your Prolog code and queries!

Any other editor

You can of course use any text editor for Prolog. VSCode, Vim, Sublime, Emacs, etc. I believe there’s even an Eclipse plugin for Prolog. No matter what you use, you’ll just write your Prolog code in your editor, and then execute it from a terminal.

To load your Prolog file in your compiler, you can run the command

$ swipl -l filename.pl

Then you’ll be loaded into a interactive area that we call a REPL (Read Eval Print Loop). In the REPL is where you’ll write your Prolog queries. You use ; to view the next result or . to stop. To exit the REPL, you use halt..

The Prolog REPL is a little unconventional, and if you get stuck I recommend you check out the SWI Prolog website’s page on getting started as it has command line instructions.

Author: Philip Dumaresq

Email: phdumaresq@protonmail.com

Created: 2021-03-13 Sat 19:09