SWI-Prolog - Powerful Prolog Interpreter

Learn to use SWI-Prolog, a versatile implementation of the Prolog language. Execute Prolog code, consult files, and run goals interactively or non-interactively.

SWI-Prolog Interpreter Commands

Understanding SWI-Prolog

SWI-Prolog is a powerful and versatile implementation of the Prolog programming language, widely used for logic programming, artificial intelligence, and natural language processing. This section details common command-line usages for interacting with the SWI-Prolog interpreter.

Running SWI-Prolog Interactively

To start the SWI-Prolog interpreter in interactive mode, simply type swipl in your terminal. This allows you to load files, query predicates, and debug your Prolog programs directly within the interpreter session.

# Run Prolog interpreter in interactive mode
swipl

Executing Prolog Scripts Non-Interactively

SWI-Prolog can execute specific goals or consult files without entering the interactive interpreter. This is useful for scripting and automated tasks. The -s flag consults a Prolog file, -g specifies a goal to run after consulting, and -t defines a goal to run upon halting.

# Consult file.pl, run goal mygoal(3,foo), and then halt the interpreter without entering interactive mode
swipl -s file.pl -g "mygoal(3,foo)." -t halt.

Key SWI-Prolog Command-Line Options

Understanding the various command-line options for swipl is crucial for efficient development and execution of Prolog programs. The -s option loads a specified Prolog source file, making its predicates available for use. The -g option allows you to execute a specific Prolog goal immediately after loading the file, which is often used to start a program or perform an initial query. Finally, the -t option specifies a goal to be executed when the interpreter is about to exit, commonly used to ensure a clean shutdown or to perform final actions.

Further Resources