Chapter 2

How to Use the Picat System

On this page

2.1 How to Use the Picat Interpreter

The Picat system is written in both C and Picat. The Picat interpreter is provided as a single standalone executable file, named picat.exe for Windows and picat for Unix. The Picat interpreter provides an interactive programming environment for users to compile, load, debug, and execute programs. In order to start the Picat interpreter, users first need to open an OS terminal. In Windows, this can be done by selecting Start -> Run and typing cmd or selecting Start -> Programs -> Accessories -> Command Prompt. In order to start the Picat interpreter in any working directory, the environment variable PATH must be properly set to contain the directory where the executable is located.

2.1.1 How to Enter and Quit the Picat Interpreter

The Picat interpreter is started with the OS command picat.

   OSPrompt picat

where OSPrompt is the OS prompt. After the interpreter is started, it responds with the prompt Picat>, and is ready to accept queries.

In general, the picat command takes the following form:

  picat Opts PicatMainFileName A1 A2 … An

where PicatMainFileName can have the extension .pi and can contain a path of directories, and Opts is a sequence of the following options:

Once the interpreter is started, users can type a query after the prompt. For example,

      Picat> X = 1+1
      X = 2
      Picat> printf("hello"++" picat")
      hello picat

The halt predicate, or the exit predicate, terminates the Picat interpreter. An alternative way to terminate the interpreter is to enter ctrl-d (control-d) when the cursor is located at the beginning of an empty line.

2.1.2 How to Use the Command-line Editor

The Picat interpreter uses the getline program written by Chris Thewalt. The getline program memorizes up to 100 of the most recent queries that the users have typed, and allows users to recall past queries and edit the current query by using Emacs editing commands. The following gives the editing commands:

ctrl-fMove the cursor one position forward.
ctrl-bMove the cursor one position backward.
ctrl-aMove the cursor to the beginning of the line.
ctrl-eMove the cursor to the end of the line.
ctrl-d Delete the character under the cursor.
ctrl-hDelete the character to the left of the cursor.
ctrl-kDelete the characters to the right of the cursor.
ctrl-uDelete the whole line.
ctrl-pLoad the previous query in the buffer.
ctrl-nLoad the next query in the buffer.

Note that the command ctrl-d terminates the interpreter if the line is empty and the cursor is located at the beginning of the line.

2.1.3 How to Compile and Load Programs

A Picat program is stored in one or more text files with the extension name pi. A file name is a string of characters. Picat treats both ’/’ and ’' ’ as path separators. Nevertheless, since ’' ’ is used as the escape character in quoted strings, two consecutive backslashes must be used, as in "c:' ' work' ' myfile.pi", if ’' ’ is used as the separator.

A program file can contain include directives of the form:

  include Name.

The include directive causes the content of the file named Name to be copied verbatim to where the directive occurs.

For the sake of demonstration we assume the existence of a file named welcome.pi in the current working directory that stores the following program:

    main =>
        print(" Welcome to PICAT's world! \n ").

    main(Args) =>
        print(" Welcome to PICAT's world! \n"),
        foreach (Arg in Args)
            printf("%s\n", Arg)
        end.

2.1.4 How to Run Programs

After a program is loaded, users can query the program. For each query, the system executes the program, and reports yes when the query succeeds and no when the query fails. When a query that contains variables succeeds, the system also reports the bindings for the variables. For example,

Picat> cl("welcome")
Compiling:: welcome.pi
welcome.pi compiled in 4 milliseconds
loading...

yes

Picat> main
 Welcome to PICAT's world! 
 
yes

Users can ask the system to find the next solution by typing ’;’ after a solution if the query has multiple solutions. For example,

    Picat> member(X, [1,2,3])
    X = 1;
    X = 2;
    X = 3;
    no

Users can force a program to terminate by typing ctrl-c, or by letting it execute the built-in predicate abort. Note that when the system is engaged in certain tasks, such as garbage collection, users may need to wait for a while in order to see the termination after they type ctrl-c.

2.1.5 How to Run Programs Directly

Programs that define the main/0 predicate or the main/1 predicate can be run directly as an OS command. For example,

$ picat welcome
 Welcome to PICAT's world!

$ picat welcome a b c
 Welcome to PICAT's world!
a
b
c

The ’$’ sign is the prompt of the OS. It is assumed that the environment variable PATH has been set to contain the directory of the executable picat (picat.exe for Windows), and the environment variable PICATPATH has been set to contain the directory of the welcome.pi file or the file is in the current working directory.

2.1.6 Creating Standalone Executables

It is possible to create a script that can be run as a standalone executable. For example, consider the following script welcome.exe for Linux:

#!/bin/bash          
picat welcome.pi
echo " Finished!"

Once the environment variables PATH and PICATPATH are set properly, and the script has execute permission, it can be executed as follows:

$ welcome.exe 
 Welcome to PICAT's world! 
 Finished!

2.2 How to Use the Debugger

The Picat system has three execution modes: non-trace mode, trace mode, and spy mode. In trace mode, it is possible to trace the execution of a program, showing every call in every possible stage. In order to trace the execution, the program must be recompiled while the system is in trace mode. In spy mode, it is possible to trace the execution of individual functions and predicates that are spy points. When the Picat interpreter is started, it runs in non-trace mode. The predicate debug or trace changes the mode to trace. The predicate nodebug or notrace changes the mode to non-trace.

In trace mode, the debugger displays execution traces of queries. An execution trace consists of a sequence of call traces. Each call trace is a line that consists of a stage, the number of the call, and the information about the call itself. For a function call, there are two possible stages: Call, meaning the time at which the function is entered, and Exit, meaning the time at which the call is completed with an answer. For a predicate call, there are two additional possible stages: Redo, meaning a time at which execution backtracks to the call, and Fail, meaning the time at which the call is completed with a failure. The information about a call includes the name of the call, and the arguments. If the call is a function, then the call is followed by = and ? at the Call stage, and followed by = V alue at the Exit stage, where V alue is the return value of the call.

Consider, for example, the following program:

    p(X) ?=> X = a.
    p(X) => X = b.
    q(X) ?=> X = 1.
    q(X) => X = 2.

Assume the program is stored in a file named myprog.pi. The following shows a trace for a query:

 Picat> debug

 {Trace mode}
 Picat> cl(myprog)

 {Trace mode}
 Picat> p(X), q(Y)  
    Call: (1) p(_328) ?
    Exit: (1) p(a) 
    Call: (2) q(_378) ?
    Exit: (2) q(1) 
 X = a
 Y = 1 ?;
    Redo: (2) q(1) ?
    Exit: (2) q(2) 
 X = a
 Y = 2 ?;
    Redo: (1) p(a) ?
    Exit: (1) p(b) 
    Call: (3) q(_378) ?
    Exit: (3) q(1) 
 X = b
 Y = 1 ?;
    Redo: (3) q(1) ?
    Exit: (3) q(2) 
 X = b
 Y = 2 ?;
 no

In trace mode, the debugger displays every call in every possible stage. Users can set spy points so that the debugger only shows information about calls of the symbols on which users have set spy points. Users can use the predicate

  spy $Name/N

to set the functor Name/N as a spy point, where the arity N is optional. If the functor is defined in multiple loaded modules, then all these definitions will be treated as spy points. If no arity is given, then any functor of Name is treated as a spy point, regardless of the arity.

After displaying a call trace, if the trace is for stage Call or stage Redo, then the debugger waits for a command from the users. A command is either a single letter followed by a carriage-return, or just a carriage-return. See Appendix A.2.8 for the debugging commands.