Building standalone applications

A standalone application is a program that can be executed without the need to start the B-Prolog interpreter first. Users do not have to use the external language interface in order to build standalone applications. The default initial predicate that the B-Prolog interpreter executes is called main/0. In version 6.9 and later, an initial goal can be given as a command-line argument -g Goal. For example, the following command
      bp myprog.out -g ``mymain(Output),writeln(Output)''
loads the binary file myprog.out, and executes the goal
      mymain(Output),writeln(Output)
instead of the default initial goal main.

Users can also build a Prolog program as a standalone application by redefining the main/0 predicate. The following definition is recommended:

      main:-
          get_main_args(L),
          call_your_program(L).
where get_main_args(L) fetches the command-line arguments as a list of atoms, and call_your_program(L) starts the users' program. If the program does not need the command-line arguments, then the call get_main_args(L) can be omitted.

The second thing that users must do is to compile the program, and to let the user-defined main/0 predicate overwrite the main/0 predicate that exists in the system. Assume that the compiled program is named myprog.out. In order to let the system execute main/0 in myprog.out instead of the main/0 that exists in the system, users must either add myprog.out into the command-line in the shell script bp (bp.bat for Windows), or must start the system with myprog.out as an argument of the command-line, as in the following:

      bp myprog.out

For example, assume that call_your_program(L) only prints out L. Then, the command

      bp myprog.out a b c
gives the following output:
      [a,b,c]

Neng-Fa Zhou 2013-01-25