Hello Prolog

As Kernighan & Ritchie point out in their classic C text, the hardest part is getting the first simple program to work in your environment. The Prolog side of Hello Prolog is the same for each environment, but each host language environment has a different way of connecting to the Logic Server. This section describes:

Under each of the samples subdirectories for the various programming tools (c, cpp, delphi, java, vb, cgi, etc.) you will find a hello subdirectory with that implements Hello Prolog.

Prolog Side of Hello Prolog

The Prolog program to be embedded is hello.pro, which has a single predicate, hello/2, whose first argument is a string supplied by the caller and whose second argument is a string constructed in Prolog for retrieval by the host program.

In the Prolog interpreter, a call to hello/2 would look like

Compiling and Linking

To call Amzi! Prolog from another language, you must have a compiled program to load, a .xpl file. Once a compiled program is loaded, you can then consult other Prolog source files or compiled files (.plm files), but you can't consult another .xpl file.

A compiled version of hello (hello.xpl) is included in the Amzi! directory, but to recreate this file from the IDE

  1. Select Build/Compile from the menu and choose hello.pro from the file selection dialog. This will create the Prolog object module hello.plm.
  2. Select Build/Link from the menu and choose hello.plm from the file selection dialog. This will create the Prolog load module hello.xpl.

To create hello.xpl from the operating system command line:

In either case make sure hello.xpl is either in the path or the directory from which you run the final hello program. If not you will get an error message indicating hello.xpl could not be opened.

Host Language Side of Hello Prolog

All of the source code for various environments is stored in environment-specific subdirectories of samples. Most are also included in the appropriate reference sections.

The host program initializes the Prolog environment, and calls the hello/2 predicate. It checks for error conditions, and, all going well, prints the returned greeting from hello.pro.

The code looks a little longer, because at each step of the way error checking is done. Return codes from the API indicate whether or not a function was successful. The sample code displays status messages at each step, or displays an error message if the step failed.

The error handling uses an API function call to get and display the error message from Prolog. (To test the error handling, simply rename the file hello.xpl to something else, and run the host language program. You should get an error message indicating the load was unsuccessful because hello.xpl couldn't be loaded.)

(Instead of checking the return code from each function, you can use the setjmp/longjmp feature of C to trap Prolog errors and handle them in one place. See the section on error handling for details.)

The host calling program:

  1. Initializes the Prolog environment,
  2. Loads the compiled version of hello,
  3. Constructs a query term such as 'hello($C Programmer$, X)',
  4. Calls the Prolog engine with the query,
  5. Retrieves the resultant value of X into a C string,
  6. Displays the string, and
  7. Closes the Prolog environment.

When the program runs correctly, you will get output similar to:

  Prolog Initialized
  Hello Loaded

  Greetings C Programmer from Amzi! Prolog

  Prolog Successfully Completed

Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.