Getting to Know Visual Prolog 10

Written by on June 6, 2021 in C++, Programming, Prolog with 0 Comments

Visual Prolog has its roots in Turbo Prolog, which was popularized by Borland in the mid 1980s. It is developed and supported by PDC A/S, Denmark.

So how is Visual Prolog different from other Prolog implementations out there, for example, SWI-Prolog or Sicstus Prolog?

Here are the key differences:

1) It is not an ISO Prolog 

2) It is strongly-typed and is object-oriented

3) It is a compiled environment, not interpreted

Visual Prolog is quite rich in functionality. We can quickly build Console mode or GUI-based applications, DLLs, and even Windows Service using the platform. Because of strong typing and compile-only strategy, the efficiency of generated code is likely to be superior compared to other interpreted environments. So, if you don’t need a “Standard” Prolog, then Visual Prolog has a lot to offer.

Although I purchased the Commercial Edition of Visual Prolog a few years ago, I never got around to using it. One reason is that it seems to have a steeper learning curve and there are not many video tutorials to explain the language and the environment. There are a couple of PDF books but they cover an older version (ver 7.x or earlier).

I upgraded to the latest version (ver 10) a couple of weeks ago and decided to spend some time understanding the language and the environment.

Instead of writing the canonical “Hello, world! program, I wanted to start by implementing a DLL that I can call from C/C++. I do this usually with other languages too, see for example this and this. The capability to use DLLs (in Windows environment) written in one language can come in handy if we want to exploit the language’s special features for implementing specific functionalities, even though the main application is written in another language say, Erlang, Go, etc. In that sense, since Prolog is a declarative language and is good at logical inferencing, we might consider using it to implement an application such as a Medical Diagnosis system that has an Expert System subcomponent.

Creating the DLL:

Getting back to our main topic, I decided to create a DLL in Visual Prolog. The DLL provides the following:

1) A function called “factorial” that takes an integer argument and returns the corresponding factorial of the argument.

2) A procedure called “lookup” that takes two arguments: an integer, which acts as a “key” for lookup and and an “out” parameter that corresponds to the looked up value.  In case the integer “key” is not present, it will assign “-999999” to the output argument.

As you can see, Visual Prolog supports functions and procedures too, in addition to the regular non-deterministic “Horn Clauses” . 

Since Visual Prolog is “object oriented”, functionality is implemented in classes, whose code is distributed in three files:

“Interface” file, which defines the interface/behaviour provided by the “objects” of a class

“Class” file, that declares the class-specific interface

“Implementation” file that implements the clauses, etc. declared in the above two files

In some cases, the “interface” is not required (if we are not going to create class instances), but the other two are must.

I created a “New Project” with the type as “DLL”:

Visual Prolog Project

Visual Prolog Project

When I click “Finish”, the IDE generates the necessary files in the selected directory.

Here are the two predicates that I export from the DLL (they are declared in the “class” file):

The Class File

The Class File

Here is the corresponding implementation:

The Implementation File

The Implementation File

When I create the DLL project, the IDE automatically generates the “main” module that takes care of creating and initializing the DLL. Here is the generated code:

DLL Initilization

DLL Initilization

The “Build” command (Control + Shift + B) causes the IDE to compile the project and emit the DLL as well as the LIB file. The DLL is saved in the “exe64” subdirectory and the LIB file in the “Lib64” directory inside the Project directory “DllTest”.

Using the DLL from a C++ Program:

I decided to load the DLL dynamically in my C++ program, so there was no need for the LIB file. I wrote a simple wrapper to interface with the Visual Prolog code:

The Wrapper Class

The Wrapper Class

Take a look at line 40. Since the Prolog “lookup” procedure retuns the result via “out” parameter, I need to pass the address of the “result” variable. Visual Prolog will store the looked up value in this address.

And here is the “main” function:

C++ Main Entry Point

C++ Main Entry Point

First, I have to create an instance of the “wrapper” class, initializing it with the correct path of the DLL to be used. The prolog calls are then routed through this object. Nothing fancy here.

I used Embarcadero’s RAD Studio ver 10.4.2 to build the C++ application (64-bits).

Here is the output from the program:

Program Output

Program Output

The program works as expected. When time permits, I intend to check out the other interesting features that Visual Prolog has to offer. 

You can download the Visual Prolog project here. The C++ source file is here.

Have a nice weekend!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published.

Top