Appendix B
The sys Module
On this page
The sys module, which is imported by default, contains built-ins that are relevant to the Picat system. The built-ins in the sys module perform operations that include compiling programs, tracing execution, and displaying statistics and information about the Picat system.
B.1 Compiling and Loading Programs
The sys module includes a number of built-ins for compiling programs and loading them into memory.
- compile(): This predicate compiles the file .pi and all of its dependent files without loading the generated bytecode files. The destination directory for the bytecode file is the same as the source file’s directory. If the Picat interpreter does not have permission to write into the directory in which a source file resides, then this built-in throws an exception. If .pi imports modules, then these module files are also compiled. The system searches for these module files in the directory in which .pi resides or the directories that are stored in the environment variable PICATPATH.
- compile_bp(): This predicate translates the Picat file .pi into a B-Prolog file .pl. If the file is dependent on other Picat files, then those files are compiled using compile/1. The destination directory for the B-Prolog file is the same as the source file’s directory. If the Picat interpreter does not have permission to write into the directory in which a source file resides, then this built-in throws an exception.
- load(): This predicate loads the bytecode file .qi and all of its dependent bytecode files into the system for execution. For , the system searches for a bytecode file in the directory specified by or the directories that are stored in the environment variable PICATPATH. For the dependent file names, the system searches for a bytecode file in the directory in which .qi resides or the directories that are stored in the environment variable PICATPATH. If the bytecode file .qi does not exist, but the source file .pi exists, then this built-in compiles the source file and loads the bytecodes without creating a qi file. Note that, for the dependent files, if the bytecode file does not exist, but the source file exists, then the source file will be compiled.
- cl(): This predicate compiles and loads the source file named .pi. Note that the extension .pi does not need to be given. The system also compiles and loads all of the module files that are either directly imported or indirectly imported by the source file. The system searches for such dependent files in the directory in which .pi resides or the directories that are stored in the environment variable PICATPATH.
- cl: This predicate compiles and loads a program from the console, ending when the end-of-file character (ctrl-z for Windows and ctrl-d for Unix) is typed.
- cl_facts(): This predicate compiles and loads facts into the system. The argument is a list of ground facts.
- cl_facts(, ): This predicate compiles and loads facts into the system. The argument is a list of ground facts. The argument is a list of indexing information in the form p(, , …, ). Each can either be +, which indicates that the argument is input, or -, which indicates that the argument is output. Facts are indexed on input arguments.
- cl_facts_table(): This predicate is the same as cl_facts/1, except that the facts are all tabled.
- cl_facts_table(, ): This predicate is the same as cl_facts/2, except that the facts are all tabled.
B.2 Tracing Execution
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. The following predicates are used to switch between non-trace mode and trace mode.
- trace: This predicate switches the execution mode to trace mode.
- notrace: This predicate switches the execution mode to non-trace mode.
- debug: This predicate switches the execution mode to trace mode.
- nodebug: This predicate switches the execution mode to non-trace mode.
- spy(): This predicate places a spy point on , which is a function or a predicate, optionally followed by an arity. The creation of a spy point switches the Picat system to spy mode.
- nospy: This predicate removes all spy points, and switches the execution mode to non-trace mode.
- abort: This predicate terminates the current program. This can be used in all three execution modes.
B.2.1 Debugging Commands
In trace mode, the system displays a message when a function or a predicate is entered (Call), exited (Exit), re-entered (Redo) or has failed (Fail). After a function or a predicate is entered or re-entered, the system waits for a command from the user. A command is a single letter followed by a carriage-return, or may simply be a carriage-return. The following commands are available:
- + : create a spy point.
- - : remove a spy point.
- : reset the print depth to 10.
- i : reset the print depth to i.
- a : abort, quit debugging, moving control to the top level.
- <cr> : A carriage return causes the system to show the next call trace.
- c : creep, show the next call trace.
- h : help, display the debugging commands.
- ? : help, display the debugging commands.
- l : leap, be silent until a spy point is encountered.
- n : nodebug, prevent the system from displaying debugging messages for the remainder of the program.
- r : repeat, continue to creep or leap without intervention.
- s : skip, be silent until the call is completed (Exit or Fail).
- t : backtrace, show the backtrace leading to the current call.
- t i : backtrace, show the backtrace from the call numbered i to the current call.
- u : undo what has been done to the current call and redo it.
- u i : undo what has been done to the call numbered i and redo it.
B.3 Information about the Picat System
The sys module contains a number of built-ins that display information about the Picat system. This information includes statistics about the system, including the memory that is used, and the amount of time that it takes to perform a goal.
B.3.1 Statistics
The following built-ins display statistics about the memory that the Picat system uses.
- statistics: This predicate displays the number of bytes that are allocated to each data area, and the number of bytes that are already in use.
- statistics(, ): The statistics concerning are . This predicate gives multiple solutions upon backtracking. Keys include runtime, program, heap, control, trail, table, gc, backtracks, table_blocks and gc_time. The values for most of the keys are lists of two elements. For the key runtime, the first element denotes the amount of time in milliseconds that has elapsed since Picat started, and the second element denotes the amount of time that has elapsed since the previous call to statistics/2 was executed. For the key gc, the number indicates the number of times that the garbage collector has been invoked. For the key gc_time, the value indicates the time spent on garbage collection. For the key table_blocks, the value gives the number of blocks that are used for the table area.1 For the key backtracks, the number indicates the number of backtracks that have been done during the labeling of finite domain variables since Picat was started. For the keys program, heap, control, trail, and table, the first element denotes the size of memory in use, and the second element denotes the size of memory that is still available in the corresponding data area.
- statistics_all() = : This function returns a list of lists that are in the form [Key, Value]. The list contains all of the keys that statistics/2 can display, together with their corresponding values.
Example
Picat> statistics
Stack+Heap: 8, 000, 000 bytes
Stack in use: 1, 156 bytes
Heap in use: 28, 592 bytes
Program: 8, 000, 000 bytes
In use: 1, 448, 436 bytes
Symbols: 5, 300
Trail: 4, 000, 000 bytes
In use: 936 bytes
Memory manager:
GC: Call(0), Time(0 ms)
Expansions: Stack+Heap(0), Program(0), Trail(0), Table(0)
Picat> statistics(Key, Value)
Key = runtime
Value = [359947,66060]?;
Key = program
Value = [1451656,6548344]?;
Key = heap
Value = [34112,7964524]?;
Key = control
Value = [1360,7964524]?;
Key = trail
Value = [1496,3998504]?;
Key = table
Value = [0,4000000]?;
Key = table_blocks
Value = 1?;
Key = gc
Value = 0?;
Key = backtracks
Value = 0 ?;
Key = gc_time
Value = 0
Picat> L = statistics_all()
L = [[runtime, [359947,66060]], [program, [1451656,6548344]],
[heap, [34112,7964524]], [control, [1360,7964524]],
[trail, [1496,3998504]], [table, [0,4000000]],
[table_blocks,1],[gc, 0], [backtracks, 0], [gc_time, 0]]
B.3.2 Time
The following predicates display the amount of CPU time that it takes to perform a goal.
- time(): This predicate calls , and reports the number of seconds of CPU time that were consumed by the execution.
- time2(): This predicate calls , and reports the number of seconds of CPU time that were consumed by the execution, and the number of backtracks that have been performed in labeling finite-domain variables during the execution of .
- time_out(, , ): This predicate is logically equivalent to once , except that it imposes a time limit, in milliseconds, on the evaluation. If is not finished when expires, then the evaluation will be aborted, and will be unified with the atom time_out. If succeeds within the time limit, then will be unified with the atom success. Note that time-out may be delayed or never occur because of the execution of an external C function.
B.3.3 Other System Information
- command() = : This function sends the command to the OS and returns the status that is returned from the OS.
- help: This predicate shows how to use the commands that the system accepts.
- loaded_modules() = : This function returns a list of the modules that are currently loaded in the Picat system. This list includes library modules and user-defined modules. By default, this function returns [basic,math,io,sys].
- nolog: This predicate turns off the logging flag.
- picat_path() = : This function returns the directories that are stored in the environment variable PICATPATH. If the environment variable PICATPATH does not exist, then this function throws an error.
B.4 Garbage Collection
Picat incorporates an incremental garbage collector for the control stack and the heap. The garbage collector is active by default. The sys module includes the following predicates for garbage collection.
- garbage_collect: This predicate starts the garbage collector.
- garbage_collect(): This predicate calls the garbage collector. If there are fewer than words of combined free space on the control stack and heap after garbage collection, then it invokes the memory manager to expand the stack and heap so that there are words of combined free space on the control stack and heap.
B.5 Quitting the Picat System
The following predicates can be used to terminate the Picat interpreter.
1Each block has 1 million words.