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.
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.
% hello.pro - hello for embedding hello(Caller, Greeting) :- strcat($Greetings $, Caller, S1), strcat(S1, $, from Amzi! Prolog.$, Greeting).
In the Prolog interpreter, a call to hello/2 would look like
?- hello($your name$, X). X = Greetings your name from Amzi! Prolog
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
To create hello.xpl from the operating system command line:
c> acmp hello c> alnk hello hello
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.
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:
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.